#--weights on RCPs to yield ssp oxidant files
#--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_OXIDANTS/Scenarios/ssp119 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp119 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp126 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp126 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp245 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp245 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp370 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp370 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp434 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp434 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp460 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp460 ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp534-over ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp534-over ; fi
if [ ! -d ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp585 ] ; then mkdir -p ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp585 ; fi

#--creating specific file "RCP0" for RCP26-->ssp119 weighting
fileinRCP0=/ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/LOI_AR6_RCP0.nc
rm -f ${fileinRCP0}
ncrcat /ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/yearly_interp/LOI_AR6_RCP26_201[05].nc /ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/LOI_AR6_RCP26_2010-2015.nc
cdo ymonmean /ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/LOI_AR6_RCP26_2010-2015.nc ${fileinRCP0}
rm -f /ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/LOI_AR6_RCP26_2010-2015.nc

for year in {2015..2100}
do

echo '... processing year '${year}'...'

fileinRCP26=/ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/yearly_interp/LOI_AR6_RCP26_${year}.nc
fileinRCP45=/ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/yearly_interp/LOI_AR6_RCP45_${year}.nc
fileinRCP60=/ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/yearly_interp/LOI_AR6_RCP60_${year}.nc
fileinRCP85=/ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/yearly_interp/LOI_AR6_RCP85sstA2_${year}.nc

cdo add -mulc,19. -divc,26. -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP26} -mulc,7. -divc,26. -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP0} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp119/oxidants_ssp119_${year}.nc
cp ${fileinRCP26} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp126/oxidants_ssp126_${year}.nc
cp ${fileinRCP45} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp245/oxidants_ssp245_${year}.nc
cdo add -mulc,.4 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP85} -mulc,.6 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP60} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp370/oxidants_ssp370_${year}.nc
cdo add -mulc,8. -divc,19. -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP45} -mulc,11. -divc,19. -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP26} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp434/oxidants_ssp434_${year}.nc
cdo add -mulc,.6 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP60} -mulc,.4 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP45} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp460/oxidants_ssp460_${year}.nc
cdo add -mulc,8. -divc,19. -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP45} -mulc,11. -divc,19. -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${fileinRCP26} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp534-over/oxidants_ssp534-over_${year}.nc
cp ${fileinRCP85} ${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/Scenarios/ssp585/oxidants_ssp585_${year}.nc

#--end loop on years
done
