# Th. Lurton, Feb. 2019

# This script operates on Future TSI netcdf files
# and aims at decreasing the TSI (offset)
# over the whole of a decade (as specified by the GeoMIP protocol).

# Work directory
### TO BE CUSTOMISED ###
here=/ccc/work/cont003/gencmip6/lurtont/TSI_G6/

# Input directory
repin=/ccc/work/cont003/igcmg/igcmg/IGCM/ATM/SOLAR/CMIP6/v3.1corr/Future/

### TO BE CUSTOMISED ###
# Parameters
# Absolute-value diminution wanted (W.m-2)
dimin="24.0"
# This will appear as a suffix in the names of the reduced-TSI files created

# Period considered
yr0=2091
yr1=2100
# These years are included, i.e. the period considered is yr0-01-01 to yr1-12-31


for (( year=${yr0}; year<=${yr1}; year++ ))
do

fileout=${here}solarforcing_v3.1_daily_6bands_${year}-${dimin}Wperm2.nc

tsireduced=${here}tsireduced_${year}.nc
ssireduced=${here}ssireduced_${year}.nc

filein=${repin}solarforcing_v3.1_daily_6bands_${year}.nc

cdo subc,${dimin} -selvar,tsi ${filein} ${tsireduced}
cdo subc,${dimin} -selvar,ssi ${filein} ${ssireduced}

ncrename -d bnds,nbd ${tsireduced}
ncrename -d bnds,nbd ${ssireduced}

ncks -x -v tsi,ssi ${filein} ${here}temp_${year}.nc

cdo merge ${here}temp_${year}.nc ${tsireduced} ${ssireduced} ${fileout}

rm -f ${here}temp_${year}.nc ${tsireduced} ${ssireduced}

done

