#!/bin/bash

# Prepares regridded 2x2 degree files for use with the RK PI scripts
# starting from the output of the HiResClim postprocessing tool
# This scripts substitutes post_data.sc of the RK PI scripts
# The output nc files are in nc1 classic format
# j.vonhardenberg@isac.cnr.it

#Updated by P. Davini (ISAC-CNR) - <p.davini@isac.cnr.it> 
#December 2014
# Updated by Francois Massonnet (UCL/BSC) Nov 2017

# Usage:   ./postprocess.sh EXP MEMBER YEARSTART YEAREND
# Example: ./postprocess io01 fc3 1990 2000

set -o nounset
set -o errexit

if [ $# -ne 4 ]
then
  echo "Usage:   ./postprocess.sh exp MEMBER YEARSTART YEAREND"
  echo "Example: ./postprocess.sh e00x fc0 1990 2000"
  exit 1
fi


# experiment name
exp=$1
MEMBER=$2
# years to be processed
year1=$3
year2=$4

#set config.sh to configure folders and cdo details
. config.sh

# Where the PI climatologies are located
PICLIM=${MAINDIR}/Climate_netcdf

############################################
# No need to touch below this line 
# (but check the FBASE line below and adapt for your system)
############################################
REFGRID=$PICLIM/reference_grid_2x2.nc
OCEREFGRID=$PICLIM/opa_grid_2x2.nc
CLIMDIR2=${CLIMDIR}/${MEMBER}

mkdir -p $CLIMDIR2 || exit -1

#cleaning directories
rm -f $CLIMDIR2/*_mon_2x2.nc  $CLIMDIR2/*_mean_2x2.nc

#loop on years
for (( year=$year1; year<=$year2; year++)); do

  #select files
  FBASE=$SAVEDIR/${MEMBER}/Post_$year/${exp}_${STARTDATE}_${MEMBER}_${year}0101-${year}1231



  # 1. Remapping and, in some cases, conversion of units
  # ----------------------------------------------------

  #  T2M [convert to Celcius]
  $cdonc cat -$remap,$REFGRID -addc,-273.15 ${FBASE}_tas.nc $CLIMDIR2/t2m_mon_2x2.nc

  #  MSL [from Pa to hPa]
  $cdonc cat -$remap,$REFGRID -divc,100.0 ${FBASE}_msl.nc   $CLIMDIR2/msl_mon_2x2.nc

  # IFS Net Surface Heat Flux QNET=SLHF+SSHF+SSR+STR [already in W/m2]
  $cdonc add ${FBASE}_slhf.nc ${FBASE}_sshf.nc                $CLIMDIR2/shf$$.nc
  $cdonc add ${FBASE}_str.nc ${FBASE}_ssr.nc                  $CLIMDIR2/sr$$.nc
  $cdonc cat -$remap,$REFGRID -add $CLIMDIR2/sr$$.nc           $CLIMDIR2/shf$$.nc $CLIMDIR2/qnet_mon_2x2.nc

  rm $CLIMDIR2/sr$$.nc $CLIMDIR2/shf$$.nc

  # Total precipitation [from Kg/m2/s to mm/day] 
  $cdonc cat -$remap,$REFGRID -mulc,86400.0 ${FBASE}_totp.nc  $CLIMDIR2/tp_mon_2x2.nc

  # East-west surface stress [already in N/m2]
  $cdonc cat -$remap,$REFGRID ${FBASE}_ewss.nc                $CLIMDIR2/ewss_mon_2x2.nc

  # North-south surface stress [already in N/m2]
  $cdonc cat -$remap,$REFGRID ${FBASE}_nsss.nc                $CLIMDIR2/nsss_mon_2x2.nc

  # Sea ice (on atmosphere grid, FM modification to read sea ice from atm model since no sea ice data is output)
  $cdonc cat -$remap,$REFGRID ${FBASE}_ci.nc                  $CLIMDIR2/SICE_mon_2x2.nc

  # T, U, V, Q on p levels [units already ok]
  $cdonc cat -$remap,$REFGRID ${FBASE}_t.nc                   $CLIMDIR2/T_mon_2x2.nc
  $cdonc cat -$remap,$REFGRID ${FBASE}_u.nc                   $CLIMDIR2/U_mon_2x2.nc
  $cdonc cat -$remap,$REFGRID ${FBASE}_v.nc                   $CLIMDIR2/V_mon_2x2.nc
  $cdonc cat -$remap,$REFGRID ${FBASE}_q.nc                   $CLIMDIR2/Q_mon_2x2.nc

  # NEMO fields: we reconstruct the mask from the salinity field
  OCEMASK=$CLIMDIR2/tmask.nc
  $cdonc seltimestep,1 -gtc,0 ${FBASE}_sosaline.nc  $OCEMASK

  #SST, SSS
  $cdonc cat -invertlat -remapbil,$OCEREFGRID -ifthen $OCEMASK ${FBASE}_sosstsst.nc $CLIMDIR2/SST_mon_2x2.nc
  $cdonc cat -invertlat -remapbil,$OCEREFGRID -ifthen $OCEMASK ${FBASE}_sosaline.nc $CLIMDIR2/SSS_mon_2x2.nc
  rm $OCEMASK

done

# 2. Time mean for all fields
# ---------------------------

for vv in t2m msl qnet tp ewss nsss T U V Q SST SSS SICE ; do
        $cdonc timmean $CLIMDIR2/${vv}_mon_2x2.nc $CLIMDIR2/${vv}_mean_2x2.nc
done

# 2x2 mask, will be needed for PI 
$cdonc -R setctomiss,0 -ltc,0.5 -$remap,$CLIMDIR2/qnet_mon_2x2.nc -selcode,172 $maskfile $CLIMDIR2/ocean_mask2x2.nc
