#!/bin/bash

# keskmised
#cdo daymean ice_hourly.nc ice_daymean.nc

# andmed kokku
cdo mergetime ice_daymean* merged_ice_daymean.nc

# jata alles 1 oktoober kuni 31 mai
cdo selmonth,10,11,12,1,2,3,4,5 merged_ice_daymean.nc cut_ice_daymean.nc

# kustuta 29 veb
cdo del29feb cut_ice_daymean.nc noveb_ice_daymean.nc

#---------------------------------------------------------------------------
# GoR
cdo sellonlatbox,22,25,56.5,59 data/noveb_ice_daymean.nc gor/gor_daymean.nc

# joonis 1 koostamiseks
cdo timmax -gec,0.15 -selvar,siconc gor/gor_daymean.nc gor/joonis1.nc


# Votame valja jaa konsentratsiooni
cdo -setctomiss,0 -setclonlatbox,0,22,22.2,58,58.3 -setclonlatbox,0,22,24,58.575,59 -setclonlatbox,0,22,23,58.4,59 -selvar,siconc gor/gor_daymean.nc gor/icefrac.nc

# joonis 2 koostamiseks
cdo timmax -gec,0.15 gor/icefrac.nc gor/joonis2.nc

# jaa mask kus frac => 0.15
cdo -setctomiss,0 -gec,0.15 gor/icefrac.nc gor/ice_mask.nc

# korrutame maski ja konsentratsioonid
cdo mul gor/icefrac.nc gor/ice_mask.nc gor/ice_masked.nc

#------------------ pindala
# gridarea GoR
cdo gridarea gor/gor_daymean.nc gor/grid.nc

# multyply with ice_frac
cdo mul gor/ice_masked.nc gor/grid.nc gor/area.nc

# m^2 to km^2
cdo divc,1e6 gor/area.nc gor/area_km.nc

# pindala kokku
cdo fldsum gor/area_km.nc gor/area_fsum.nc

#----------------YDAYMEAN-----------------------
# keskmised paeva pindalad aasta jooksul
cdo ydaymean gor/area_fsum.nc gor/area_ydaymean.nc

# loikame okt,nov,dek
cdo -shifttime,-1year -selmonth,10,11,12 gor/area_ydaymean.nc gor/area_ond.nc

# uhendame andmed
cdo mergetime gor/area_ond.nc gor/area_ydaymean.nc gor/area_ydaymean_lai.nc

# Jatame alles okt-mai
cdo seldate,2019-10-01,2020-05-31 gor/area_ydaymean_lai.nc gor/area_ydaymean.nc

#-----------------KUU--GRAAFIKUD-------------------
# pindalaga
#cdo ymonmean gor/area_km.nc gor/area_ymonmean.nc

# >15% jaa konsentratsiooniga
cdo ymonmean gor/ice_masked.nc gor/ice_frac_ymonmean.nc

#---------------------RUUMALA--------------------------------
# Valime paksuse
cdo -setctomiss,0 -setclonlatbox,0,22,22.2,58,58.3 -setclonlatbox,0,22,24,58.575,59 -setclonlatbox,0,22,23,58.4,59 -selvar,sithick gor/gor_daymean.nc gor/ice_thick.nc

# ruumala m^3
cdo mul gor/ice_thick.nc gor/area.nc gor/icevol.nc

# keskmine paksus ule kogu aja
cdo timmean gor/icevol.nc gor/icevol_timmean.nc

# keskmine paksus kuus
cdo ymonmean gor/icevol.nc gor/icevol_ymonmean.nc

# kogu jaa paksus
cdo fldsum gor/icevol.nc gor/icevol_fldsum.nc

#---------YDAYMEAN-----RUUMALA--------------------
# jaa keskmine kogupaksuse muutus
cdo ydaymean gor/icevol_fldsum.nc gor/icevol_ydaymean.nc

# loikame okt,nov,dek
cdo -shifttime,-1year -selmonth,10,11,12 gor/icevol_ydaymean.nc gor/vol_ond.nc

# uhendame andmed
cdo mergetime gor/vol_ond.nc gor/icevol_ydaymean.nc gor/icevol_ydaymean_lai.nc

# Jatame alles okt-mai
cdo seldate,2019-10-01,2020-05-31 gor/icevol_ydaymean_lai.nc gor/icevol_ydaymean.nc

#---------PAEVADE---STATISTIKA----------------

# nihutame andmed 5 kuud tagasi ja summeerime jaapaevad aasta kaupa
cdo -setctomiss,0 -shifttime,-5mo gor/ice_mask.nc gor/ice_days.nc


# ja summeerime jaapaevad aasta kaupa
cdo yearsum gor/ice_days.nc gor/ice_ysum.nc

# jaapaevade kaart
cdo timmean gor/ice_ysum.nc gor/ice_days_mean.nc
