#!/bin/sh
###############################################################################
#                   POSTPROCESSING ECEARTH3 EXPERIMENT
###############################################################################
#
#BSUB -J test
#BSUB -oo test_%J.out
#BSUB -eo test_%J.err
#BSUB -W 04:00
#BSUB -n 32
#BSUB -R "span[ptile=16]"
#
###############################################################################

# Copy the model output in the same directory where test.cmd is located (for nemo rebuilt output is needed)
# remove *.cmor if exist
# nohup ./test.cmd >& test.log &

#source /opt/modules/init/bash
module load intel/2013sp1
module load hdf5/1.8.11
module load szip/2.1
module load netcdf/4.1.3
module load nco

PATH=/gpfs/projects/ecm86/opt/grib_api-1.9.9/intel-13/bin:${PATH}
PATH=/scratch/cfu/opt/cdo-1.4.5.1/intel-13/bin:${PATH}

set -xuv
date

#
# General Stuff
#
EXPID=h008
START_date=19930501
MEMBER=fc0
Chunk_start_date=19930501
Chunk_end_date=19930831
RUN_months=4
output_dir=$(pwd)

#
# Cmorization of ocean and atmos.'s output
#
cd $output_dir
CMORIZATION=TRUE
CMORFAMILY=seaIceClimInit
SPECS2CMOR=/scratch/cfu/models/specs2cmor
MODEL=ecearth
MODEL_ID=EC-EARTH3
VERSION=v3.0.1
#   experiment_id="${CMORFAMILY}$(echo $START_date | cut -c1-4)"
if [[ $CMORIZATION == 'TRUE' && ! -a $output_dir/cmor ]]; then
   experiment_id="${CMORFAMILY}"
   real=$(($(echo $MEMBER | cut -d 'c' -f 2)+1))
   init=1
   phys=1
   reftime=$(echo `echo $START_date | cut -c1-4 ``echo "-"``echo $START_date | cut -c5-6``echo "-"``echo $START_date | cut -c7-8``echo "(T00:00:00Z)"`) # yyyy-mm-dd(Thh:mm:ssZ)
#   reftime=$(echo $START_date | cut -c1-6) # yyyymm
   PATH=$SPECS2CMOR/models/$MODEL/bin:${PATH}
   # ocean
   date
   if [[ ! -a $output_dir/cmoro ]]; then
      #available: rebuilt ocean output in *.nc (depends on iodef.xml defined)
      cmoroi=$output_dir/cmoro_input
      cmoroo=$output_dir/cmoro_output
      mkdir -p $cmoroi $cmoroo
      cd $cmoroi
      ln -sf $output_dir/${EXPID}_??_*.nc .
      ln -sf $SPECS2CMOR/common/Tables .
      startyear=$(echo $Chunk_start_date | cut -c1-4)
      endyear=$(echo $Chunk_end_date | cut -c1-4)
      startmonth=$(echo $Chunk_start_date | cut -c5-6)
      endmonth=$(echo $Chunk_end_date | cut -c5-6)
      i=1
      while [ $i -le $RUN_months ]; do
       ncra -d time_counter,$((($i-1)*$RUN_months+1)),$(($i*$RUN_months)) ${EXPID}_1d_${Chunk_start_date}_${Chunk_end_date}_diaptr.nc tmp${i}.nc
       i=$(($i+1))
      done
      rm -f ${EXPID}_1m_${Chunk_start_date}_${Chunk_end_date}_diaptr.nc 
      ncrcat tmp* ${EXPID}_1m_${Chunk_start_date}_${Chunk_end_date}_diaptr.nc
      rm tmp*
 #realization=$real,
# ecearth=3,
cat << EOF > namelist_cmoro
&NAMEXP
 inpath='$cmoroi/',
 outpath='$cmoroo/',
 expname='$EXPID',
 experiment_id='$experiment_id',
 source='$MODEL_ID $VERSION official release from SMHI',
 model_id='$MODEL_ID',
 forcing='Nat,Ant',
 parent_experiment_id='N/A',
 parent_experiment_rip='N/A',
 institution='Institut Catala de Ciences del Clima, www.ic3.cat',
 institute_id='IC3',
 realization='5',
 history='',
 comment='',
 references='',
 contact='PA Bretonnière <pierre-antoine.bretonniere@ic3.cat>',
 startyear=$startyear,
 endyear=$endyear,
 startmonth=$startmonth,
 endmonth=$endmonth,
 initialization_method=$init,
 initialization_description='N/A',
 physics_version=$phys,
 physics_description='N/A',
 associated_model='N/A',
 forecast_reference_time='$reftime',
 prefix='$EXPID',
 series='$CMORFAMILYSERIES'
/
EOF
      # actual run
      cmor-oce.exe namelist_cmoro
      # post cmorization steps
      cd $cmoroo
      tarfile="$output_dir/CMORO_${EXPID}_${START_date}_${MEMBER}_${Chunk_start_date}-${Chunk_end_date}.tar"
      tar -cvf $tarfile *
      gzip -9 -f $tarfile
      rm -rf $cmoroi $cmoroo
      touch $output_dir/cmoro
   fi
   # atmosphere
   date
   if [[ ! -a $output_dir/cmora ]]; then
      # available: daily grib (3h or 6h) on monthly basis
      NFRP=6
      cmorai=$output_dir/cmora_input
      cmorao=$output_dir/cmora_output
      mkdir -p $cmorai $cmorao
      cd $cmorai
      ln -sf $SPECS2CMOR/common/Tables .
      NYEAR0=$(echo $START_date | cut -c1-4)
      NMONTH0=$(echo $START_date | cut -c5-6)
      csy=$(echo $Chunk_start_date | cut -c1-4) # chunk start year
      csm=$(echo $Chunk_start_date | cut -c5-6) # chunk start month
      cs=$RUN_months # chunk size (in months)
      for c in $(seq 1 $cs); do
       csm=${csm#0}
       list[$c]="${csy}$(printf "%02d" $csm)"
       div=$((((csm+1))/13))
       rem=$((((csm+1))%13))
       csy=$((csy+div))
       csm=$rem
       if [[ $rem -eq 0 ]]; then
        csm=01
       fi
      done
      echo ${list[@]}
      for yyyymm in ${list[@]}; do
       file=fcdata
       rm -rf tmp_dir_means fcdata fcdata0
       #means
       mkdir tmp_dir_means
       cd tmp_dir_means
       cdo sp2gpl $output_dir/ICMSH${EXPID}+${yyyymm} fcdata0
       cat fcdata0 $output_dir/ICMGG${EXPID}+${yyyymm}  > $file
       NYEAR=$(echo $yyyymm | cut -c1-4)
       NMONTH=$(echo $yyyymm | cut -c5-6)
       NMDAYS=$(cal $NMONTH $NYEAR | grep -v "[a-z]" | grep -v "^$" | wc -w)
       NSTP_DAY_R=$(( 24 / $NFRP ))
       NSTP_DAY=${NSTP_DAY_R%.*}
       echo write \"${file}_[param].grb\"\; > rules_file
       grib_filter rules_file ${file} #${file}_${param}.grb, 6h data
       
       cdo -r add ${file}_142.128.grb ${file}_143.128.grb ${file}_228.128.grb
       cdo -r mulc,1000 ${file}_228.128.grb ${file}_228.128.grb2
       mv ${file}_228.128.grb2 ${file}_228.128.grb
       rm ${file}_142.128.grb ${file}_143.128.grb
       for param in 146 147 169 175 176 177 179 228
       do
        cdo -r divc,3600.*${NFRP} ${file}_${param}.128.grb ${file}_${param}.128.grb2
        mv ${file}_${param}.128.grb2 ${file}_${param}.128.grb
       done
       #monthly 00GMT: extract good timesteps
       for param in 141 146 147 168 169 175 176 177 179 201 202 228 243 #212
       do
        cdo seltimestep,$(seq -s ',' 1 ${NSTP_DAY} $(( ${NMDAYS}*${NSTP_DAY}))) ${file}_${param}.128.grb  ${file}_${param}_mon0.grb
       done
       #split daily
       for param in 169 177 179 201 202 228
       do
        i=1
        while [[ $i -le $NMDAYS ]]
        do
         cdo seltimestep,$(seq -s ',' $(( $i * 4 - 3)) $(( $i * 4 ))) ${file}_${param}.128.grb  ${file}_${param}_d_$i.grb
         i=`expr $i + 1 `
        done
       done
       
       #mon0: conv2nc + mean
       for infile in `ls *mon0.grb`
       do
         cdo -R -r -t ecmwf -f nc splitvar $infile rg
         cdo -f nc -r -timmean rg* ${infile%???}nc
         rm -f rg*
       done
       #  cdo -f nc -r -timmean $infile ${infile%???}nc 
       
       #monthly means with 6h
       for param in 151 164 165 166 167 168
       do
        cdo -R -r -t ecmwf -f nc splitvar ${file}_${param}.128.grb rg
        cdo -f nc -r -timmean rg* ${file}_${param}_mon.nc
        rm -f rg*
       done
       # cdo -f nc -r -timmean ${file}_${param}.128.grb ${file%????}_${param}_mon.nc
       
       #daily: mean+conv2nc+concat
       for param in 169 177 179 201 202 228
       do
        for infile in `ls *${param}_d_*.grb`
        do
         cdo -R -r -t ecmwf -f nc splitvar $infile rg
         cdo -f nc -r -timmean rg* ${infile%???}nc #new 
         rm -f rg*
        done
         i=1
         rm -f l
        while [[ $i -le $NMDAYS ]]
        do
         ls *${param}_d_${i}.nc >> l
         i=`expr $i + 1 `
        done
        ncrcat `cat l` ${file}_${param}_d.nc
       #before ncrcat *${param}_d_*.nc  ${file}_${param}_d.nc
       # ncrcat ${infile%???}nc  ${file}_${param}_d.nc
       done
       rm -f *_d_*
       #  cdo -f nc -r -timmean $infile ${infile%???}nc 
       
       #6hour
       for param in 151 164 165 166 167 168
       do
        cdo -R -r -t ecmwf -f nc splitvar ${file}_${param}.128.grb rg
        cdo -f nc copy rg* ${file}_${param}_6h.nc
        rm -f rg*
       done
       # cdo -f nc copy ${file}_${param}.128.grb ${file}_${param}_6h.nc
       
       #concat all vars in one file for mon,day,6h
       for infile in `ls *mon*.nc`
       do
        ncks -A $infile ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       done
       cdo showvar ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       ncrename -v LSP,TP ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       ncatted -O -a units,TP,m,c,"kg m-2 s-1" ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       for var in SSHF SLHF SSRD STR STRD TTR SSR TP
       do
        pst=`ncdump -h ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc | grep $var | head -n1 | cut -c 2`
        if [ ! -z $pst ]; then
         ncatted -O -a units,$var,m,c,"W m-2" ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        fi
        pst=''
       done
       
       for infile in `ls *d.nc`
       do
        ncks -A $infile ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       done
        cdo showvar ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        ncrename -v LSP,TP ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        ncatted -O -a units,TP,m,c,"kg m-2 s-1" ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
        for var in SSRD STR STRD TTR TP
        do
         pst=`ncdump -h ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc | grep $var | head -n1 | cut -c 2`
         if [ ! -z $pst ]; then
          ncatted -O -a units,$var,m,c,"W m-2" ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
         fi
         pst=''
        done
       
       for infile in `ls *6h*.nc`
       do
        ncks -A $infile ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc
       done
        for var in SSHF SLHF SSRD STR STRD TTR
        do
         pst=`ncdump -h ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc | grep $var | head -n1 | cut -c 2`
         if [ ! -z $pst ];then
          ncatted -O -a units,$var,m,c,"W m-2" ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc
         fi
         pst=''
        done
#       exit
       mv ${EXPID}_???_${yyyymm}01-${yyyymm}${NMDAYS}.nc ../.
       rm -f *grb *mon0* ${file}*nc
       cd ..
       date
       if [[ $NYEAR == 1850 && $NMONTH == 01 ]]; then
        suff=1
       else
        suff=""
       fi
cat << EOF > namelist_cmora
&NAMEXP
 inpath='$cmorai/',
 outpath='$cmorao/',
 expname='$EXPID',
 experiment_id='$experiment_id',
 source='$MODEL_ID $VERSION official release from SMHI',
 model_id='$MODEL_ID',
 forcing='Nat,Ant',
 parent_experiment_id='N/A',
 parent_experiment_rip='N/A',
 institution='Institut Catala de Ciences del Clima, www.ic3.cat',
 institute_id='IC3',
 realization=$real,
 history='',
 comment='',
 references='',
 contact='PA Bretonnière <pierre-antoine.bretonniere@ic3.cat>',
 startyear=$NYEAR,
 endyear=$NYEAR,
 startmonth=$NMONTH,
 endmonth=$NMONTH,
 initialization_method=$init,
 initialization_description='N/A',
 physics_version=$phys,
 physics_description='N/A',
 associated_model='N/A',
 forecast_reference_time='$reftime',
 prefix='$EXPID',
 series='$CMORFAMILYSERIES'
/
EOF
       # actual run
       cmor-atm.exe namelist_cmora
      done
      unset list
      # post cmorization steps
      cd $cmorao
      tarfile="$output_dir/CMORA_${EXPID}_${START_date}_${MEMBER}_${Chunk_start_date}-${Chunk_end_date}.tar"
      tar -cvf $tarfile *
      gzip -9 -f $tarfile
      rm -rf $cmorai $cmorao
      touch $output_dir/cmora
   fi
   if [[ -a $output_dir/cmoro && -a $output_dir/cmora ]]; then
      rm $output_dir/cmoro $output_dir/cmora
      touch $output_dir/cmor
      date
   fi
fi

date

