#--Script performing a linear time interpolation
#--in order to yield every year between decade-years
#--for oxidant files.
#--Th. Lurton, IPSL, 07/2018

dirinOXY=/ccc/work/cont003/gencmip6/lurtont/CMIP6_OXIDANTS/
diroutOXY=${GENCMIP6_CCCWORKDIR}/CMIP6_OXIDANTS/yearly_interp/

if [ ! -d ${diroutOXY} ] ; then mkdir -p ${diroutOXY} ; fi

#--historical
hist=$false
if [ $hist ] ; then
for year in {1850..1999}  #--note that the whole hist period is not covered
do

echo "Historical period, interpolation for year "${year}"..."

if [ ${year} -ge 1850 -a ${year} -lt 1860 ]
then
year1=1850
year2=1860
fi
if [ ${year} -ge 1860 -a ${year} -lt 1870 ]
then
year1=1860
year2=1870
fi
if [ ${year} -ge 1870 -a ${year} -lt 1880 ]
then
year1=1870
year2=1880
fi
if [ ${year} -ge 1880 -a ${year} -lt 1890 ]
then
year1=1880
year2=1890
fi
if [ ${year} -ge 1890 -a ${year} -lt 1900 ]
then
year1=1890
year2=1900
fi
if [ ${year} -ge 1900 -a ${year} -lt 1910 ]
then
year1=1900
year2=1910
fi
if [ ${year} -ge 1910 -a ${year} -lt 1920 ]
then
year1=1910
year2=1920
fi
if [ ${year} -ge 1920 -a ${year} -lt 1930 ]
then
year1=1920
year2=1930
fi
if [ ${year} -ge 1930 -a ${year} -lt 1940 ]
then
year1=1930
year2=1940
fi
if [ ${year} -ge 1940 -a ${year} -lt 1950 ]
then
year1=1940
year2=1950
fi
if [ ${year} -ge 1950 -a ${year} -lt 1960 ]
then
year1=1950
year2=1960
fi
if [ ${year} -ge 1960 -a ${year} -lt 1970 ]
then
year1=1960
year2=1970
fi
if [ ${year} -ge 1970 -a ${year} -lt 1980 ]
then
year1=1970
year2=1980
fi
if [ ${year} -ge 1980 -a ${year} -lt 1990 ]
then
year1=1980
year2=1990
fi
if [ ${year} -ge 1990 -a ${year} -lt 2000 ]
then
year1=1990
year2=2000
fi

file1=${dirinOXY}LOI_AR6_${year1}.nc
file2=${dirinOXY}LOI_AR6_${year2}.nc
filetmp=${diroutOXY}tmp.nc
fileout=${diroutOXY}LOI_AR6_${year}.nc

#--using separate file with time_counter and time_bnds
#--to yield correct time_counter and time_bnds
echo "... Creating time pattern for year "${year}", scenario "${scen}"..."
filetimes=${diroutOXY}times_${year}.nc
cdo addc,$(((${year}-1850)*365*24*3600)) -selname,time_counter,time_bnds ${dirinOXY}LOI_AR6_1850.nc ${filetmp}
#--something weird happens, as time_counter is declared not found, and is not shifted as should be (time_bnds is). I am here forcing the shift by hand.
cdo shifttime,$(((${year}-1850)*365))days ${filetmp} ${filetimes}
ncrename -d x,bnds ${filetimes}

if [ ${year:3:1} == 1 ]
then
cdo add -mulc,0.9 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.1 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 2 ]
then
cdo add -mulc,0.8 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.2 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 3 ]
then
cdo add -mulc,0.7 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.3 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 4 ]
then
cdo add -mulc,0.6 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.4 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 5 ]
then
cdo add -mulc,0.5 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.5 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 6 ]
then
cdo add -mulc,0.4 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.6 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 7 ]
then
cdo add -mulc,0.3 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.7 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 8 ]
then
cdo add -mulc,0.2 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.8 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 9 ]
then
cdo add -mulc,0.1 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.9 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
#--nothing to be done if we are on a decade-year.
if [ ${year:3:1} == 0 ]
then
ncks -v H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi

#--fixing presnivs, presnivs_bnds, time_counter and time_bnds
echo "... Fixing presnivs, presnivs_bnds, time_counter and time_bnds for year "${year}", scenario "${scen}"..."
ncks -A -v time_counter,time_bnds ${filetimes} ${filetmp}
ncks -A -v presnivs,presnivs_bnds ${dirinOXY}LOI_AR6_1850.nc ${filetmp}
cp ${filetmp} ${fileout}
rm -f ${filetmp}

#--end loop on historical years
done

#--skip historical
fi 

#--Scenarios

for scen in "RCP26" "RCP45" "RCP60" "RCP85sstA2"
do

for year in {2000..2100}
do

echo "Scenario "${scen}", interpolation for year "${year}"..."

if [ ${year} -ge 2000 -a ${year} -lt 2010 ]
then
year1=2000
year2=2010
fi
if [ ${year} -ge 2010 -a ${year} -lt 2020 ]
then
year1=2010
year2=2020
fi
if [ ${year} -ge 2020 -a ${year} -lt 2030 ]
then
year1=2020
year2=2030
fi
if [ ${year} -ge 2030 -a ${year} -lt 2040 ]
then
year1=2030
year2=2040
fi
if [ ${year} -ge 2040 -a ${year} -lt 2050 ]
then
year1=2040
year2=2050
fi
if [ ${year} -ge 2050 -a ${year} -lt 2060 ]
then
year1=2050
year2=2060
fi
if [ ${year} -ge 2060 -a ${year} -lt 2070 ]
then
year1=2060
year2=2070
fi
if [ ${year} -ge 2070 -a ${year} -lt 2080 ]
then
year1=2070
year2=2080
fi
if [ ${year} -ge 2080 -a ${year} -lt 2090 ]
then
year1=2080
year2=2090
fi
if [ ${year} -ge 2090 -a ${year} -lt 2100 ]
then
year1=2090
year2=2100
fi
if [ ${year} == 2100 ]
then
year1=2100
year2=2100
fi

file1=${dirinOXY}LOI_AR6_${scen}_${year1}.nc
file2=${dirinOXY}LOI_AR6_${scen}_${year2}.nc
filetmp=${diroutOXY}tmp.nc
fileout=${diroutOXY}LOI_AR6_${scen}_${year}.nc

#--specific case for junction with historical -- year2=2010 is correct, but year1=2000 does not exist for scenarios. Let's override that...
if [ ${year} -lt 2010 ]
then
file1=${dirinOXY}LOI_AR6_2000.nc
fi

rm -f ${diroutOXY}tmp.nc

#--using separate file with time_counter and time_bnds
#--to yield correct time_counter and time_bnds
echo "... Creating time pattern for year "${year}", scenario "${scen}"..."
filetimes=${diroutOXY}times_${year}.nc
cdo addc,$(((${year}-2000)*365*24*3600)) -selname,time_counter,time_bnds ${dirinOXY}LOI_AR6_2000.nc ${filetmp}
#--something weird happens, as time_counter is declared not found, and is not shifted as should be (time_bnds is). I am here forcing the shift by hand.
cdo shifttime,$(((${year}-2000)*365))days ${filetmp} ${filetimes}
ncrename -d x,bnds ${filetimes}

rm -f ${filetmp}

if [ ${year:3:1} == 1 ]
then
cdo add -mulc,0.9 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.1 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 2 ]
then
cdo add -mulc,0.8 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.2 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 3 ]
then
cdo add -mulc,0.7 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.3 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 4 ]
then
cdo add -mulc,0.6 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.4 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 5 ]
then
cdo add -mulc,0.5 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.5 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 6 ]
then
cdo add -mulc,0.4 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.6 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 7 ]
then
cdo add -mulc,0.3 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.7 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 8 ]
then
cdo add -mulc,0.2 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.8 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 9 ]
then
cdo add -mulc,0.1 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file1} -mulc,0.9 -selname,H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi
if [ ${year:3:1} == 0 ]
then
ncks -v H2O2,HNO3,NO2,NO3,O1D,O3,OH ${file2} ${filetmp}
fi

#--fixing presnivs, presnivs_bnds, time_counter and time_bnds
echo "... Fixing presnivs, presnivs_bnds, time_counter and time_bnds for year "${year}", scenario "${scen}"..."
ncks -A -v time_counter,time_bnds ${filetimes} ${filetmp}
ncks -A -v presnivs,presnivs_bnds ${dirinOXY}LOI_AR6_RCP26_2010.nc ${filetmp}
cp ${filetmp} ${fileout}
rm -f ${filetmp}

#--end loop on years
done

rm -f ${diroutOXY}times*.nc

#--end loop on scenario names
done
