##--NOT YET FULLY TESTED 
##--SCRIPT TO PREPARE TRANSITION FOR 2015-2024 PERIOD

lmdz='LR'
#lmdz='VLR'
#lmdz='VVLR'

output='/data/oboucher/CMIP6/VOLC/'${lmdz}
cd ${output}

#--LW properties

file1=taulwstrat.2D.2014.nc
file2=taulwstrat.2D.ave.nc

#--10 year transition from 2015 to 2023 
#--then from 2024 onwards use ave conditions

for year in {2015..2023}
do 
fileout=taulwstrat.2D.${year}.nc
rm $fileout
w1=`echo "scale=4;((${year}-2014)/10.)" | bc`
w2=`echo "scale=4;(1.-${w1})" | bc`
echo $year $w1 $w2
cdo add -mulc,${w1} $file1 -mulc,${w2} $file2 $fileout
done

#--SW properties

file1=tauswstrat.2D.2014.nc
file2=tauswstrat.2D.ave.nc

#--10 year transition from 2015 to 2023
#--then from 2024 onwards use ave conditions

for year in {2015..2023}
do
w1=`echo "scale=4;((${year}-2014)/10.)" | bc`
w2=`echo "scale=4;(1.-${w1})" | bc`
echo $year $w1 $w2
fileout=tauswstrat.2D.${year}.nc
fileout_aod=aod.2D.${year}.nc
fileout_ome=ome.2D.${year}.nc
fileout_cgg=cgg.2D.${year}.nc 
fileout_m1=m1.2D.${year}.nc 
fileout_m2=m2.2D.${year}.nc 
fileout_aod_ome=aod_ome.2D.${year}.nc
fileout_aod_ome_cgg=aod_ome_cgg.2D.${year}.nc 
rm -f $fileout $fileout_m1 $fileout_m2 $fileout_aod $fileout_ome $fileout_cgg $fileout_aod_ome $fileout_aod_ome_cgg
cdo add -mulc,${w1} -selname,TAU_SUN $file1 -mulc,${w2} -selname,TAU_SUN $file2 $fileout_aod
cdo add -mulc,${w1} -expr,'tau_ome=TAU_SUN*OME_SUN' $file1 -mulc,${w2} -expr,'tau_ome=TAU_SUN*OME_SUN' $file2 $fileout_aod_ome
cdo add -mulc,${w1} -expr,'tau_ome_cgg=TAU_SUN*OME_SUN*GGG_SUN' $file1 -mulc,${w2} -expr,'tau_ome_cgg=TAU_SUN*OME_SUN*GGG_SUN' $file2 $fileout_aod_ome_cgg
cdo merge $fileout_aod_ome $fileout_aod $fileout_m1
cdo expr,'OME_SUN=tau_ome/TAU_SUN' $fileout_m1 $fileout_ome
cdo merge $fileout_aod_ome_cgg $fileout_aod_ome $fileout_m2 
cdo expr,'GGG_SUN=tau_ome_cgg/tau_ome' $fileout_m2 $fileout_cgg
cdo merge $fileout_aod $fileout_ome $fileout_cgg $fileout
rm -f $fileout_m1 $fileout_m2 $fileout_aod $fileout_ome $fileout_cgg $fileout_aod_ome $fileout_aod_ome_cgg
done
