#!/bin/bash

echo " Scripti algus"


years=$(seq 2000 2019)
months=$(seq 1 12)
months2=$(printf '%02d ' $months)


for yy in $years ; do
for mm in $months2 ; do

tstr=$yy$mm

ifolder=/mnt/hpc/public/mko/CMEMS/BS_ice_conc/
ifile=$ifolder/BS_SAT_SST.day.$tstr.nc


opt='-L -O -f nc4 -z zip1'

cdo $opt mergetime $ifolder/BS_SAT_SST.day.$yy$mm*nc /home/study/S24_HenrikH/out1/kodutoo.mergetime.$yy$mm.nc

cdo $opt selvar,sea_ice_fraction -sellonlatbox,22,30,56,61 out1/kodutoo.mergetime.$yy$mm.nc out1/kodutoo.ice.EST.$yy$mm.nc



echo $tstr
done
cdo $opt mergetime out1/kodutoo.ice.EST.$yy*nc out1/kodutoo.mergetime.ice.EST.$yy.nc
done
cdo $opt mergetime out1/kodutoo.mergetime.ice.EST.*nc kodutoo.mergetime.ice.EST.nc


# Average ice days per year 
cdo $opt yearsum -gtc,0 kodutoo.mergetime.ice.EST.nc analyys/kodutoo.year_ice_days.nc
cdo $opt timavg analyys/kodutoo.year_ice_days.nc analyys/kodutoo.average_ice_days.nc


# Mean & std yearday timeseries of Ice extent of Gulf of Riga
cdo $opt sellonlatbox,22,25,56.5,58.5 kodutoo.mergetime.ice.EST.nc analyys/kodutoo.ice.GOR.nc
cdo $opt -gtc,0 analyys/kodutoo.ice.GOR.nc analyys/above_0_GOR.nc
cdo $opt fldsum analyys/above_0_GOR.nc analyys/sum_above_0_GOR.nc
cdo $opt ydaymean analyys/sum_above_0_GOR.nc analyys/mean_ice_extent_GOR.nc
cdo $opt ydaystd analyys/sum_above_0_GOR.nc analyys/std_ice_extent_GOR.nc


# Mean & std yearday timeseries of Ice extent of Gulf of Finland
cdo $opt sellonlatbox,23.3,30,59,61 kodutoo.mergetime.ice.EST.nc analyys/kodutoo.ice.GOF.nc
cdo $opt -gtc,0 analyys/kodutoo.ice.GOF.nc analyys/above_0_GOF.nc
cdo $opt fldsum analyys/above_0_GOF.nc analyys/sum_above_0_GOF.nc
cdo $opt ydaymean analyys/sum_above_0_GOF.nc analyys/mean_ice_extent_GOF.nc
cdo $opt ydaystd analyys/sum_above_0_GOF.nc analyys/std_ice_extent_GOF.nc