#--Weights on RCPs to yield ssp limit files.
#--This script requires output from script SST_SIC_anomaly.sh.
#--Th. Lurton, IPSL, 07/2018.

# ssp1-19 = 19/26*RCP26
# ssp1-26 = RCP26
# ssp2-45 = RCP45
# ssp3-70 = .4*RCP85 + .6*RCP60
# ssp4-34 = 8/19*RCP45 + 11/19*RCP26
# ssp4-60 = .6*RCP60 + .4*RCP45
# ssp5-34-over = 8/19*RCP45 + 11/19*RCP26
# ssp5-85 = RCP85

if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp119 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp119 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp126 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp126 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp245 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp245 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp370 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp370 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp434 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp434 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp460 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp460 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp534-over ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp534-over ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp585 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp585 ; fi

#--We need specific data for the ssp119 ponderation, which will be refered to as RCP0
fileSSTAMIP=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/AMIP/tos_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-4_gn_187001-201712.nc
fileSICAMIP=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/AMIP/siconc_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-4_gn_187001-201712.nc
gridfile=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/GRID/grid_MR_CMIP5
#--For SST
cdo ymonmean -seldate,2006-01-01,2017-12-31 ${fileSSTAMIP} ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc
cdo addc,273.15 -selvar,tos ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp1.nc
cdo remapcon,${gridfile} ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp1.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp2.nc
ncrename -v tos,tsol_oce ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp2.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/SST_RCP0.nc
rm -f ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp1.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp2.nc
#--For SIC
cdo ymonmean -seldate,2006-01-01,2017-12-31 ${fileSICAMIP} ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc
cdo remapcon,${gridfile} ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp1.nc
ncrename -v siconc,pourc_sic ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp1.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp2.nc
cdo ymonmean -seldate,2006-01-01,2017-12-31 ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/rcp26/v5.rcp26MR1_20060101_21001231_1M_pourc_lic.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc
ncrename -v pourc_lic,pourc_sic ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/lic_RCP0.nc
cdo sub ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp2.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/lic_RCP0.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp3.nc
ncap2 -s 'where(pourc_sic>100.) pourc_sic=100;' ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp3.nc -O ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc
ncap2 -s 'where(pourc_sic<0.) pourc_sic=0;' ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/SIC_RCP0.nc
rm -f ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp1.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp2.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/tmp3.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/lic_RCP0.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/SSTAMIP.nc ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/SICAMIP.nc

for year in {2015..2100}
do

fileSSTRCP26=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp26/OUT_anocorr/v5.rcp26MR1_tsol_oce_${year}_anocorr.nc
fileSSTRCP45=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp45/OUT_anocorr/v5.rcp45MR1_tsol_oce_${year}_anocorr.nc
fileSSTRCP60=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp60/OUT_anocorr/v5.rcp60MR1_tsol_oce_${year}_anocorr.nc
fileSSTRCP85=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp85/OUT_anocorr/v5.rcp85MR1_tsol_oce_${year}_anocorr.nc

fileSICRCP26=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp26/OUT_anocorr/v5.rcp26MR1_pourc_sic_${year}_anocorr.nc
fileSICRCP45=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp45/OUT_anocorr/v5.rcp45MR1_pourc_sic_${year}_anocorr.nc
fileSICRCP60=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp60/OUT_anocorr/v5.rcp60MR1_pourc_sic_${year}_anocorr.nc
fileSICRCP85=/ccc/work/cont003/gencmip6/lurtont/CMIP6_LIMIT/Scenarios/rcp85/OUT_anocorr/v5.rcp85MR1_pourc_sic_${year}_anocorr.nc

outSSTssp119=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp119/tsol_oce_ssp119_${year}.nc
outSSTssp126=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp126/tsol_oce_ssp126_${year}.nc
outSSTssp245=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp245/tsol_oce_ssp245_${year}.nc
outSSTssp370=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp370/tsol_oce_ssp370_${year}.nc
outSSTssp434=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp434/tsol_oce_ssp434_${year}.nc
outSSTssp460=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp460/tsol_oce_ssp460_${year}.nc
outSSTssp534=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp534-over/tsol_oce_ssp534-over_${year}.nc
outSSTssp585=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp585/tsol_oce_ssp585_${year}.nc

outSICssp119=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp119/pourc_sic_ssp119_${year}.nc
outSICssp126=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp126/pourc_sic_ssp126_${year}.nc
outSICssp245=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp245/pourc_sic_ssp245_${year}.nc
outSICssp370=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp370/pourc_sic_ssp370_${year}.nc
outSICssp434=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp434/pourc_sic_ssp434_${year}.nc
outSICssp460=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp460/pourc_sic_ssp460_${year}.nc
outSICssp534=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp534-over/pourc_sic_ssp534-over_${year}.nc
outSICssp585=${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/Scenarios/ssp585/pourc_sic_ssp585_${year}.nc

cdo -L add -mulc,19. -divc,26. -selname,tsol_oce ${fileSSTRCP26} -mulc,7. -divc,26. -selname,tsol_oce ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/SST_RCP0.nc ${outSSTssp119}
cp ${fileSSTRCP26} ${outSSTssp126}
cp ${fileSSTRCP45} ${outSSTssp245}
cdo -L add -mulc,.4 -selname,tsol_oce ${fileSSTRCP85} -mulc,.6 -selname,tsol_oce ${fileSSTRCP60} ${outSSTssp370} 
cdo -L add -mulc,8. -divc,19. -selname,tsol_oce ${fileSSTRCP45} -mulc,11. -divc,19. -selname,tsol_oce ${fileSSTRCP26} ${outSSTssp434} 
cdo -L add -mulc,.6 -selname,tsol_oce ${fileSSTRCP60} -mulc,.4 -selname,tsol_oce ${fileSSTRCP45} ${outSSTssp460}
cdo -L add -mulc,8. -divc,19. -selname,tsol_oce ${fileSSTRCP45} -mulc,11. -divc,19. -selname,tsol_oce ${fileSSTRCP26} ${outSSTssp534}
cp ${fileSSTRCP85} ${outSSTssp585}

cdo -L add -mulc,19. -divc,26. -selname,pourc_sic ${fileSICRCP26} -mulc,7. -divc,26. -selname,pourc_sic ${GENCMIP6_CCCWORKDIR}/CMIP6_LIMIT/SIC_RCP0.nc ${outSICssp119}
cp ${fileSICRCP26} ${outSICssp126}
cp ${fileSICRCP45} ${outSICssp245}
cdo -L add -mulc,.4 -selname,pourc_sic ${fileSICRCP85} -mulc,.6 -selname,pourc_sic ${fileSICRCP60} ${outSICssp370}
cdo -L add -mulc,8. -divc,19. -selname,pourc_sic ${fileSICRCP45} -mulc,11. -divc,19. -selname,pourc_sic ${fileSICRCP26} ${outSICssp434}
cdo -L add -mulc,.6 -selname,pourc_sic ${fileSICRCP60} -mulc,.4 -selname,pourc_sic ${fileSICRCP45} ${outSICssp460}
cdo -L add -mulc,8. -divc,19. -selname,pourc_sic ${fileSICRCP45} -mulc,11. -divc,19. -selname,pourc_sic ${fileSICRCP26} ${outSICssp534}
cp ${fileSICRCP85} ${outSICssp585}

#--end loop on years
done
