#--scripts to regrid oxidant files from AR5 to AR6 
#--Olivier Boucher / Yves Balkanski 
#--30/01/2017
#--Thibaut Lurton 28/06/2017 for the future period.
#
OUT=$CCCWORKDIR/CMIP6_OXIDANTS/

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

# Booleans for possible bypass of processing (1=do bypass ; 0=run)
bypass_hist=0
bypass_RCP=0

#--silly if statements to perform or bypass historical files
if [ ${bypass_hist} == 0 ]; then
#
#HISTORICAL
DIR=/ccc/store/cont003/dsm/p24lava/ARCHIVE/dmf_import/IGCM_OUT/LMDZORINCA/NMHC_AER/LOI_IPCC_3/CHM/Output/MO/INCA_AVGR_NC/
#
#--loop on years
for yr in `seq 1850 10 2000`
do 
  #--loop on months
  for mth in 0{1..9} {10..12} 
  do
    echo $yr $mth
    filein=${DIR}'LOI_IPCC_3_'${yr}'_'${mth}'_MM_inca_avgr.nc'
    fileout=${OUT}'LOI_AR5_'${yr}'_'${mth}'.nc'
    #--remapping
    cdo remapbil,grid-lmdz-lonlat_144x143 -selname,O3,HNO3,OH,NO3,H2O2,O1D,NO2 ${filein} ${fileout}
  done
  #--merging monthly means into single sile
  cdo mergetime ${OUT}'LOI_AR5_'${yr}'_'??'.nc' ${OUT}'LOI_AR5_'${yr}'.nc'
  rm -f ${OUT}'LOI_AR5_'${yr}'_'??'.nc'
  #--change time axis - this does not work
  #secsinyr=31557600
  #yearref=1850
  #if [ $yr != 1850 ]; then
  #   ((nbyear=$yr-$yearref))
  #   echo "yr, secsinyr=" $year $secsinyr
  #   ((secsdiff = $yr * $secsinyr ))
  #   echo "secsdiff=" $secsdiff
  #   rm -f time_${yr}.nc
  #   ncap2 -s"time=${secsdiff}" time_1850.nc time_${yr}.nc   
  #fi
  #--remove time
  ncks -x -v time ${OUT}'LOI_AR5_'${yr}'.nc' ${OUT}'LOI_AR5_'${yr}'_notime.nc'
  #--add time from Yves files
  ncks -A -v time time_${yr}.nc ${OUT}'LOI_AR5_'${yr}'_notime.nc'
  #--rename file
  mv ${OUT}'LOI_AR5_'${yr}'_notime.nc' ${OUT}'LOI_AR5_'${yr}'_time.nc'
  #--regrid vertically 
ferret << EOF
set MEMORY/SIZE=1024
use "${OUT}LOI_AR5_${yr}_time.nc"
use grid.nc
define grid/z=Z[g=OZONE] grid79L
let H2O2L79=H2O2[d=1,g=grid79L,gz=@AVE] 
let HNO3L79=HNO3[d=1,g=grid79L,gz=@AVE] 
let NO2L79=NO2[d=1,g=grid79L,gz=@AVE] 
let NO3L79=NO3[d=1,g=grid79L,gz=@AVE]
let O1DL79=O1D[d=1,g=grid79L,gz=@AVE]
let O3L79=O3[d=1,g=grid79L,gz=@AVE] 
let OHL79=OH[d=1,g=grid79L,gz=@AVE]
SAVE/format=CDF/CLOBBER/file="${OUT}LOI_AR6_${yr}.nc" H2O2L79,HNO3L79,NO2L79,NO3L79,O1DL79,O3L79,OHL79
quit
EOF
  rm -f ferret.jnl*
  #--rename variables 
  ncrename -v PRESNIVS1,presnivs           ${OUT}LOI_AR6_${yr}.nc
  ncrename -d PRESNIVS1,presnivs           ${OUT}LOI_AR6_${yr}.nc
  ncrename -v PRESNIVS1_bnds,presnivs_bnds ${OUT}LOI_AR6_${yr}.nc
  ncrename -v H2O2L79,H2O2                 ${OUT}LOI_AR6_${yr}.nc
  ncrename -v HNO3L79,HNO3                 ${OUT}LOI_AR6_${yr}.nc
  ncrename -v NO2L79,NO2                   ${OUT}LOI_AR6_${yr}.nc
  ncrename -v NO3L79,NO3                   ${OUT}LOI_AR6_${yr}.nc
  ncrename -v O1DL79,O1D                   ${OUT}LOI_AR6_${yr}.nc
  ncrename -v O3L79,O3                     ${OUT}LOI_AR6_${yr}.nc
  ncrename -v OHL79,OH                     ${OUT}LOI_AR6_${yr}.nc
  ncrename -v LON,x                        ${OUT}LOI_AR6_${yr}.nc
  ncrename -d LON,x                        ${OUT}LOI_AR6_${yr}.nc
  ncrename -v LAT,y                        ${OUT}LOI_AR6_${yr}.nc
  ncrename -d LAT,y                        ${OUT}LOI_AR6_${yr}.nc
  ncrename -v TIME,time_counter            ${OUT}LOI_AR6_${yr}.nc
  ncrename -d TIME,time_counter            ${OUT}LOI_AR6_${yr}.nc
  ncrename -v TIME_bnds,time_bnds          ${OUT}LOI_AR6_${yr}.nc
  #--This stupid ferret inverses both the vertical axis AND the latitude
  rm -f ${OUT}toto.nc
  #ncpdq -Oh --reorder -presnivs,-lat ${OUT}LOI_AR6_${yr}.nc ${OUT}toto.nc
  ncpdq -Oh --reorder -presnivs,-y ${OUT}LOI_AR6_${yr}.nc ${OUT}toto.nc
  ncatted -Oh -a units,x,o,c,"degrees_east"  ${OUT}toto.nc 
  mv ${OUT}toto.nc ${OUT}LOI_AR6_${yr}.nc
  rm -f ${OUT}'LOI_AR5_'${yr}'.nc' ${OUT}'LOI_AR5_'${yr}'_notime.nc' ${OUT}'LOI_AR5_'${yr}'_time.nc'
  done 
  #
fi

#RCP
#--silly if statements to perform or bypass RCP files
if [ ${bypass_RCP} == 0 ]; then
for rcp in RCP45 RCP60 RCP85sstA2
do
  DIR=/ccc/store/cont003/dsm/p24lava/ARCHIVE/dmf_import/IGCM_OUT/LMDZORINCA/NMHC_AER/LOI_IPCC_${rcp}/CHM/Output/MO/
  for yr in `seq 2010 10 2100`
  do 
  for mth in 0{1..9} {10..12} 
  do
    echo $yr $mth
    filein=$DIR'LOI_IPCC_'${rcp}'_'${yr}'_'${mth}'_MM_inca_avgr.nc'
    fileout=$OUT'LOI_AR5_'${rcp}'_'${yr}'_'${mth}'.nc'
    #--remapping
    cdo remapbil,grid-lmdz-lonlat_144x143 -selname,O3,HNO3,OH,NO3,H2O2,O1D,NO2 $filein $fileout
  done
  #--merging monthly means into single file
  cdo mergetime $OUT'LOI_AR5_'${rcp}'_'${yr}'_'??'.nc' $OUT'LOI_AR5_'${rcp}'_'${yr}'.nc'
  rm -f $OUT'LOI_AR5_'${rcp}'_'${yr}'_'??'.nc'
  #--remove time
  ncks -x -v time ${OUT}'LOI_AR5_'${rcp}'_'${yr}'.nc' ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_notime.nc'
  #--add time
  ncks -A -v time time_${yr}.nc ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_notime.nc'
  #--rename file
  mv ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_notime.nc' ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_time.nc'

  #--regrid vertically 
ferret << EOF
set MEMORY/SIZE=1024
use "${OUT}LOI_AR5_${rcp}_${yr}_time.nc"
use grid.nc
define grid/z=Z[g=OZONE] grid79L
let H2O2L79=H2O2[d=1,g=grid79L,gz=@AVE] 
let HNO3L79=HNO3[d=1,g=grid79L,gz=@AVE] 
let NO2L79=NO2[d=1,g=grid79L,gz=@AVE] 
let NO3L79=NO3[d=1,g=grid79L,gz=@AVE]
let O1DL79=O1D[d=1,g=grid79L,gz=@AVE]
let O3L79=O3[d=1,g=grid79L,gz=@AVE] 
let OHL79=OH[d=1,g=grid79L,gz=@AVE]
SAVE/format=CDF/CLOBBER/file="${OUT}LOI_AR6_${rcp}_${yr}.nc" H2O2L79,HNO3L79,NO2L79,NO3L79,O1DL79,O3L79,OHL79
quit
EOF
  rm -f ferret.jnl*
  #--rename variables 
  ncrename -v PRESNIVS1,presnivs           ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d PRESNIVS1,presnivs           ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v PRESNIVS1_bnds,presnivs_bnds ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v H2O2L79,H2O2                 ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v HNO3L79,HNO3                 ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v NO2L79,NO2                   ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v NO3L79,NO3                   ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v O1DL79,O1D                   ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v O3L79,O3                     ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v OHL79,OH                     ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v LON,x                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d LON,x                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v LAT,y                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d LAT,y                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v TIME,time_counter            ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d TIME,time_counter            ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v TIME_bnds,time_bnds          ${OUT}LOI_AR6_${rcp}_${yr}.nc
  #--This stupid ferret inverses both the vertical axis AND the latitude
  rm -f ${OUT}toto.nc
  #ncpdq -Oh --reorder -presnivs,-lat ${OUT}LOI_AR6_${yr}.nc ${OUT}toto.nc
  ncpdq -Oh --reorder -presnivs,-y ${OUT}LOI_AR6_${rcp}_${yr}.nc ${OUT}toto.nc
  ncatted -Oh -a units,x,o,c,"degrees_east"  ${OUT}toto.nc 
  mv ${OUT}toto.nc ${OUT}LOI_AR6_${rcp}_${yr}.nc
  rm -f ${OUT}'LOI_AR5_'${rcp}'_'${yr}'.nc' ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_notime.nc' ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_time.nc'
done 
done

# Slightly different input format for RCP26...
for rcp in RCP26
do
  DIR=/ccc/store/cont003/dsm/p24lava/ARCHIVE/dmf_import/IGCM_OUT/LMDZORINCA/NMHC_AER/LOI_IPCC_${rcp}/CHM/Output/MO/
  for yr in `seq 2010 10 2100`
  do
    echo $yr
    filein=$DIR'LOI_IPCC_'${rcp}'_'${yr}'_allMM_inca_avgr.nc'
    fileout=$OUT'LOI_AR5_'${rcp}'_'${yr}'.nc'
    #--remapping
    cdo remapbil,grid-lmdz-lonlat_144x143 -selname,O3,HNO3,OH,NO3,H2O2,O1D,NO2 $filein $fileout
  #--add time
  ncks -A -v time time_${yr}.nc ${OUT}'LOI_AR5_'${rcp}'_'${yr}'.nc'

  #--regrid vertically
ferret << EOF
set MEMORY/SIZE=1024
use "${OUT}LOI_AR5_${rcp}_${yr}.nc"
use grid.nc
define grid/z=Z[g=OZONE] grid79L
let H2O2L79=H2O2[d=1,g=grid79L,gz=@AVE]
let HNO3L79=HNO3[d=1,g=grid79L,gz=@AVE]
let NO2L79=NO2[d=1,g=grid79L,gz=@AVE]
let NO3L79=NO3[d=1,g=grid79L,gz=@AVE]
let O1DL79=O1D[d=1,g=grid79L,gz=@AVE]
let O3L79=O3[d=1,g=grid79L,gz=@AVE]
let OHL79=OH[d=1,g=grid79L,gz=@AVE]
SAVE/format=CDF/CLOBBER/file="${OUT}LOI_AR6_${rcp}_${yr}.nc" H2O2L79,HNO3L79,NO2L79,NO3L79,O1DL79,O3L79,OHL79
quit
EOF
  rm -f ferret.jnl*
  #--rename variables
  ncrename -v PRESNIVS1,presnivs           ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d PRESNIVS1,presnivs           ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v PRESNIVS1_bnds,presnivs_bnds ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v H2O2L79,H2O2                 ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v HNO3L79,HNO3                 ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v NO2L79,NO2                   ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v NO3L79,NO3                   ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v O1DL79,O1D                   ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v O3L79,O3                     ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v OHL79,OH                     ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v LON,x                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d LON,x                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v LAT,y                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d LAT,y                        ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v TIME,time_counter            ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -d TIME,time_counter          ${OUT}LOI_AR6_${rcp}_${yr}.nc
  ncrename -v TIME_bnds,time_bnds          ${OUT}LOI_AR6_${rcp}_${yr}.nc
  #--This stupid ferret inverses both the vertical axis AND the latitude
  rm -f ${OUT}toto.nc
  #ncpdq -Oh --reorder -presnivs,-lat ${OUT}LOI_AR6_${yr}.nc ${OUT}toto.nc
  ncpdq -Oh --reorder -presnivs,-y ${OUT}LOI_AR6_${rcp}_${yr}.nc ${OUT}toto.nc
  ncatted -Oh -a units,x,o,c,"degrees_east"  ${OUT}toto.nc
  mv ${OUT}toto.nc ${OUT}LOI_AR6_${rcp}_${yr}.nc
  rm -f ${OUT}'LOI_AR5_'${rcp}'_'${yr}'.nc' ${OUT}'LOI_AR5_'${rcp}'_'${yr}'_time.nc'
done
done

fi
