#!/bin/bash
###############################################################################
#                   POSTPROCESSING ECEARTH3 EXPERIMENT
###############################################################################
#
#SBATCH -J oce__exp__
#SBATCH -o cmoroce__exp__.out
#SBATCH -t 23:23:00
#SBATCH -n 1
#
###############################################################################

# 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 &

#module load intel/13.0.1
#module load hdf5/1.8.10
#module load szip/2.1
#module load netcdf/4.1.3
#module load NCO/4.2.3
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/transfer/specs2cmor-1.0/libs/lib

export PATH=~/transfer/specs2cmor-1.0/models/ecearth/bin:${PATH}
#PATH=/gpfs/projects/ecm86/opt/grib_api-1.9.9/intel-13/bin:${PATH}

set -xuv
date

#
# General Stuff
#
EXPID=__exp__
#EXPID=m019
for START_date in $( ls  /cfunas/exp/ecearth/${EXPID}/ | grep [1-2] ) ; do
  for MEMBER in $( ls  /cfunas/exp/ecearth/${EXPID}/${START_date}/ ); do  
    for file in $( ls /cfunas/exp/ecearth/${EXPID}/${START_date}/${MEMBER}/outputs/MMO*tar ); do
      Chunk_start_date=$( basename  $file | cut -b 23-30)
      Chunk_end_date=$( basename $file | cut -b 32-39)
      RUN_months=4
      output_dir=~/transfer/specs2cmor-1.0/models/ecearth/test
      realization=$( echo $MEMBER | cut -b 3- )
      realization=$(( $realization + 1 ))

#
# Cmorization of ocean and atmos.'s output
#
      cd ~/transfer/specs2cmor-1.0/models/ecearth/test
#      cd $output_dir
      SPECS2CMOR=~/transfer/specs2cmor-1.0
      MODEL=ecearth
      MODEL_ID=EC-EARTH3
      VERSION=v3.0.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)
      PATH=$SPECS2CMOR/models/$MODEL/bin:${PATH}
   # ocean
      date
      if [ ! -f $output_dir/checkfiles/cmoro_${EXPID}_${START_date}_${MEMBER} ]; then
#        cmoroi="${output_dir}/cmoro_input___exp__"
#        cmoroo="${output_dir}/cmoro_output___exp__"
        cmoroi=~/transfer/specs2cmor-1.0/models/ecearth/test/cmoro_input_${EXPID}
        cmoroo=~/transfer/specs2cmor-1.0/models/ecearth/test/cmoro_output_${EXPID}
        mkdir -p $cmoroi $cmoroo
        cd $cmoroi
        tar xvf $file 
        for file in $( ls *gz ); do
          gunzip $file
        done
        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)
        ncks -v sosstsst ${EXPID}_1d_${Chunk_start_date}_${Chunk_end_date}_grid_T.nc ${EXPID}_1d_${Chunk_start_date}_${Chunk_end_date}_sst.nc
        cdo monmean ${EXPID}_1d_${Chunk_start_date}_${Chunk_end_date}_sst.nc ${EXPID}_1m_${Chunk_start_date}_${Chunk_end_date}_sst.nc
        ncks -h -A ${EXPID}_1m_${Chunk_start_date}_${Chunk_end_date}_sst.nc ${EXPID}_1m_${Chunk_start_date}_${Chunk_end_date}_grid_T.nc
        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*
# ecea rth=3,
        cat ${SPECS2CMOR}/models/ecearth/test/namelist_${EXPID}_ref | sed -e s/_realization_/$realization/g > namelist_${EXPID}
        sed -i -e s/_sy_/$startyear/g namelist_${EXPID}
        sed -i -e s/_ey_/$endyear/g namelist_${EXPID}
        sed -i -e s/_sm_/$startmonth/g namelist_${EXPID}
        sed -i -e s/_em_/$endmonth/g namelist_${EXPID}
        sed -i -e s/_frt_/$reftime/g namelist_${EXPID}
      
# actual run
      #  pwd ; ls ; ls ..
        cmor-oce.exe namelist_${EXPID}
      # post cmorization steps
#        cd $cmoroo
        scp -r  ${SPECS2CMOR}/models/ecearth/test/cmoro_output_${EXPID}/output/IC3/EC-EARTH3/* jasmin-xfer1.ceda.ac.uk:/group_workspaces/jasmin/specs/IC3/batch6/output/IC3/EC-EARTH3/.
        tarfile=CMORO_${EXPID}_${START_date}_${MEMBER}_${Chunk_start_date}-${Chunk_end_date}.tar
        tar -cvf $tarfile output
        gzip -9 -f $tarfile

        mkdir -p /cfunas/exp/ecearth/cmorfiles/$EXPID/${START_date}/${MEMBER}/outputs/
        mv ${tarfile}.gz /cfunas/exp/ecearth/cmorfiles/$EXPID/${START_date}/${MEMBER}/outputs/.
        cd 
        rm -rf $cmoroi $cmoroo
        touch $output_dir/checkfiles/cmoro_${EXPID}_${START_date}_${MEMBER}
      fi
    done #oceanfile
done
done

#   # atmosphere
#   date
#   cd $output_dir
#   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 .
#      for file in $(ls /cfunas/exp/ecearth/gribfiles/$EXPID/${START_date}/${MEMBER}/outputs/*grb ); do
#        ln -sf $file .
#        sh=$( echo $file | sed -e s/GG/SH/g ) 
#        ln -sf $sh .
#      done
#      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 ../ICMSH${EXPID}+${yyyymm}.grb fcdata0
#       cat fcdata0 ../ICMGG${EXPID}+${yyyymm}.grb  > fcdata
#       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 fcdata  #${file}_${param}.grb, 6h data
#       
#       cdo -r add fcdata_142.128.grb fcdata_143.128.grb fcdata_228.128.grb
#       cdo -r mulc,1000 fcdata_228.128.grb fcdata_228.128.grb2
#       mv fcdata_228.128.grb2 fcdata_228.128.grb
#       rm fcdata_142.128.grb fcdata_143.128.grb
#       for param in 146 147 169 175 176 177 179 228
#       do
#        cdo -r divc,3600.*${NFRP} fcdata_${param}.128.grb fcdata_${param}.128.grb2
#        mv fcdata_${param}.128.grb2 fcdata_${param}.128.grb
#       done
#       #monthly 00GMT: extract good timesteps
#       for param in 141 146 147 168 169 172 175 176 177 179 201 202 228 243 #212
#       do
#        cdo seltimestep,$(seq -s ',' 1 ${NSTP_DAY} $(( ${NMDAYS}*${NSTP_DAY}))) fcdata_${param}.128.grb  fcdata_${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 ))) fcdata_${param}.128.grb  fcdata_${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 fcdata_${param}.128.grb rg
#        cdo -f nc -r -timmean rg* fcdata_${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` fcdata_${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 fcdata_${param}.128.grb rg
#        cdo -f nc copy rg* fcdata_${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* fcdata*nc
#       cd ..
#       date
#       if [ $NYEAR == 1850 && $NMONTH == 01 ]; then
#        suff=1
#       else
#        suff=""
#       fi
#      done
#        cat ../namelist_${EXPID}_ref | sed -e s/_realization_/$real/g > namelist_${EXPID}
#        sed -i -e s/_sy_/$startyear/g namelist_${EXPID}
#        sed -i -e s/_ey_/$endyear/g namelist_${EXPID}
#        sed -i -e s/_sm_/$startmonth/g namelist_${EXPID}
#        sed -i -e s/_em_/$endmonth/g namelist_${EXPID}
#        sed -i -e s/_frt_/$reftime/g namelist_${EXPID}
#       # actual run
#       cmor-atm.exe namelist_${EXPID}
#      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
#      mv $tarfile /cfunas/exp/ecearth/cmorfiles/$EXPID/${START_date}/${MEMBER}/outputs/.
#      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
#  done
#done
#
#
#date
#
