#!/bin/bash
# ./preprocess.sh exp yearb yeare
#
# -- Author: F. Massonnet 
# -- Date  : 20 Jan 2015
# -- Place : IC3, Barcelona
# -- Adapted: November 2017, UCL
#
# -- Purpose: Prepares EC-Earth output to comply with ECmean standard metrics format
#             Works for MMA* and MMO* types of outputs only
#             So far, only verified for monthly mean outputs starting Jan. the 1st and 12 month chunks
#
# -- To do  : Change units of stresses and fluxes

set -o nounset  # Prevents use of unset variables
set -o errexit  # Prevents snowballing

# -------------
# Dry run
if [[ $# -ne 4 ]]; then
  echo "preprocess.sh exp MEMBER yearb yeare"
  echo "Example:"
  echo "preprocess.sh m069 fc1 1854 1858"
  exit
fi
#
#-------------

# ------------------
# Retrieve arguments
exp=$1
MEMBER=$2
yearb=$3
yeare=$4
# ------------------

# ----------------------------
# Load config file from ECmean
. ./config.sh
# ----------------------------

for year in `seq $yearb $yeare`; do
  echo "============== Processing year $year"
  TMPDIR=${SCRADIR}/TMP_${year}_${RANDOM}
  echo "TEMPORARY DIRECTORY: >>>>>>> $TMPDIR <<<<<<<< "
  mkdir -p $TMPDIR

  mkdir -p $SAVEDIR/Post_${year}/ 

  # PHASE 1/ EXTRACTION OF THE RAW DATA
  # -----------------------------------
  for suffix in A O; do
    echo "Untarring MM${suffix} data..."
    # 1.1. Untar the data
    # -------------------
    tarfile=$DATADIR/MM${suffix}_${exp}_${STARTDATE}_${MEMBER}_${year}0101-${year}1231.tar
    if [[ ! -f $tarfile ]]; then
      echo "File $tarfile not found, thus aborting"
      exit
    fi
        
    tar xf $tarfile -C $TMPDIR      # The -C option specifies the directory where to untar

    # 1.2 Dezip
    # ---------
    echo "Unzipping MM${suffix} data..."
    for file in `ls $TMPDIR/*.nc.gz`; do
      echo "Unzipping $file..."
      gunzip $file
    done

    #1.3 Concatenate
    # ---------------
    if [[ $suffix == A ]]; then
      echo "Concatenating MM${suffix} data"
      for typ in GG SH; do    # For gaussian and spectral grids
        cdo cat ${TMPDIR}/MM${suffix}_${exp}_${typ}_${year}??.nc ${TMPDIR}/MM${suffix}_${exp}_${typ}_${year}0101-${year}1231.nc
      done

      # Interpolate to gaussian grid
      cdo -r sp2gp ${TMPDIR}/MM${suffix}_${exp}_SH_${year}0101-${year}1231.nc ${TMPDIR}/MMA_${exp}_GG3D_${year}0101-${year}1231.nc
    fi 
    # (Note that the two operations of concatenation and interpolation do not have to be applied to ocean or ice outputs)
  done # for suffix

  # PHASE 2/ SELECTING, RENAMING, OPERATIONS ON THE ORIGINAL DATA
  # -------------------------------------------------------------

  # In the end, there must be one file "${exp}_${STARTDATE}_${MEMBER}_${year}0101-${year}1231_${var}.nc" 
  # where var is each of the elements below
  listout=("tas" "msl" "slhf" "sshf" "str" "ssr" "totp" "ewss" "nsss" "t" "u" "v" "q" "sosaline" "sosstsst" "ci")

  for var in ${listout[@]}; do
    echo "Preparing data for variable $var..."
    fileout=${TMPDIR}/${exp}_${STARTDATE}_${MEMBER}_${year}0101-${year}1231_${var}.nc

    # 2.1. Processing
    # ---------------
    case $var in
      msl|q|ci)
        filein=$TMPDIR/MMA_${exp}_GG_${year}0101-${year}1231.nc
        cdo selvar,${var^^} $filein $fileout        # ^^ = Lower-to-upper case conversion
        ncrename -v ${var^^},${var} $fileout        # ^^ = Lower-to-upper case conversion
        ;;
      slhf|sshf|str|ssr|ewss|nsss)
        filein=$TMPDIR/MMA_${exp}_GG_${year}0101-${year}1231.nc
        SCALEF=`bc -l  <<< "1/(${NFRP}*3600)"`
        cdo mulc,${SCALEF} -selvar,${var^^} $filein $TMPDIR/tmp.nc
        ncrename -v ${var^^},${var} $TMPDIR/tmp.nc $fileout
                                 # Fluxes are expressed in [W/m2*s] (i.e., in J/m2, or accumulated energy over NFRP hours)
                                 # They have to be multiplied by 1/(NFRP*3600) to recover a dimension of power per unit area
                                 #
                                 # Stresses are expressed in [N/m2*s] (i.e. accumulated stress). Same, they have to be divided
                                 # by NFRP*3600 to recover stress units
        ;;
        
      t|u|v)
        filein=$TMPDIR/MMA_${exp}_GG3D_${year}0101-${year}1231.nc
        cdo selvar,${var^^} $filein $fileout        # ^^ = Lower-to-upper case conversion
        ncrename -v ${var^^},${var} $fileout        # ^^ = Lower-to-upper case conversion
        ;;
      tas)
        filein=$TMPDIR/MMA_${exp}_GG_${year}0101-${year}1231.nc
        cdo selvar,T2M $filein $fileout
        ncrename -v T2M,tas $fileout
        ;;
      totp)
        filein=$TMPDIR/MMA_${exp}_GG_${year}0101-${year}1231.nc
        SCALEF=`bc -l  <<< "1000/(${NFRP}*3600)"`

        cdo mulc,${SCALEF} -selvar,LSP $filein $TMPDIR/tmp1.nc
        cdo mulc,${SCALEF} -selvar,CP  $filein $TMPDIR/tmp2.nc
        cdo add $TMPDIR/tmp1.nc $TMPDIR/tmp2.nc $TMPDIR/tmp3.nc 
        ncrename -v LSP,totp    $TMPDIR/tmp3.nc
        cdo chunit,m,kg/m2/s    $TMPDIR/tmp3.nc $fileout 
                                                     # Conversion of [m] to kg/m2/s
                                                     # 1 [m] = 1 m3/m2 = 1000 kg/m2
                                                     # Since the values for LSP and CP are accumulated over $NFRP hours,
                                                     # the instantaneous mass flux is (LSP+CP) * 1000 / ($NFRP * 3600)
                                                     #                                ---[m]--
                                                     #                                ----[kg/m2]----
                                                     #                                ------------kg/m2/s--------------
                                                     # Note the difference compared to atmosphere_diagnostics where the
                                                     # final units are m/s
        ;;
      sosstsst)
        filein=$TMPDIR/${exp}_1m_${year}0101_${year}1231_grid_T.nc
        cdo selvar,tos $filein $fileout
        ;;
      sosaline)
        filein=$TMPDIR/${exp}_1m_${year}0101_${year}1231_grid_T.nc
        cdo -sellevidx,1 -selvar,so $filein $fileout
        ncrename -v so,${var} $fileout
        ;;
      ice)
        filein=$TMPDIR/${exp}_1m_${year}0101_${year}1231_icemod.nc
        cdo selvar,siconc $filein $fileout
        ;;
      *)
        echo "Error: variable not found"
    esac
    
    # 2.2 Saving the data
    # -------------------
    echo "Saving the data..."
    cp $fileout $SAVEDIR/Post_${year}/
  done    # var
done # year 
