#!/bin/bash
###############################################################################
#                   POSTPROCESSING ECEARTH3 EXPERIMENT
###############################################################################
#
#SBATCH -J atm__exp__
#SBATCH -o cmoratm__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
#PATH=/gpfs/projects/ecm86/opt/grib_api-1.9.9/intel-13/bin:${PATH}

set -xuv
date

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

#
# Cmorization of ocean and atmos.'s output
#
      cd $output_dir
      SPECS2CMOR=/home/pabretonniere/transfer/specs2cmor-1.0
      MODEL=ecearth
      MODEL_ID=EC-EARTH3
      VERSION=v3.0.1
      reftime="${START_date:0:4}-${START_date:4:2}-${START_date:6:2}(T00:00:00Z)"
#     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 [ ! -a $output_dir/cmoro ]; then
#        cmoroi=$output_dir/cmoro_input____exp___
#        cmoroo=$output_dir/cmoro_output____exp___
#        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)
#        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 ../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
#        cmor-oce.exe namelist_${EXPID}
#
#      # post cmorization steps
#        cd $cmoroo
#        scp -r  output/IC3/EC-EARTH3/* jasmin-xfer1.ceda.ac.uk:/group_workspaces/jasmin/specs/IC3/batch5/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/cmoro
##      fi
#    done #oceanfile
#   # atmosphere
#   date
#   cd $output_dir
#   if [ ! -f $output_dir/cmora_${EXPID}_${START_date}_${MEMBER} ]; then
   if [ ! -f $output_dir/cmora_${EXPID}_${START_date}_${MEMBER} ]; then
      # available: daily grib (3h or 6h) on monthly basis
      NFRP=6
      cmorai="$output_dir/cmora_input___exp__"
      cmorao="$output_dir/cmora_output___exp__"
      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)


    list=""
    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]=$( echo ${csy}${csm} )
       list=$( echo $list $(printf %4d%02d $csy $csm) )
       div=$((((csm+1))/13))
       rem=$((((csm+1))%13))
       csy=$((csy+div))
       csm=$rem
#       if [ $rem -eq 0 ]; then
#        csm=01
#       fi
      done

#    for y in $(seq $startyear $endyear); do
#        for m in {1..12}; do
#            yyyymm=$(printf %4d%02d $y $m)
#            if [ $yyyymm -ge $startyear$startmonth ] && [ $yyyymm -le $endyear$endmonth ]; then
#                list=$list" "$yyyymm
#            fi
#        done
#    done
    echo "Expected dates monthly atmosphere"
    echo ${list}


#      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]=$( echo ${csy}${csm} )
#       list[$c]=$( echo ${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[@]}
       mkdir tmp_dir_means
       cd tmp_dir_means
       file=fcdata
       ln -sf ../ICMSH${EXPID}*.grb .
       ln -sf ../ICMGG${EXPID}*.grb .
      for yyyymm in ${list}; do #199305 199306 199307 199308 ; do #${list[@]} ; do
       rm -rf tmp_dir_means fcdata fcdata0
       #means
       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%.*}

#new from smhi
param_6hr="151 167 168 164 165 166 129"
param_day="167 165 166 151 164 168 169 177 179 228 201 202 130"
param_mon="167 201 202 165 166 151 144 228 205 182 164 146 147 176 169 177 175 212 141 180 181 179 168 243 129 130 131 132 133"
cdo_reftime=$( echo "${reftime:0:10},00:00" )

#

      echo FILTER_FILE ${file} ${yyyymm} 
      echo "if ( dataDate >= ${yyyymm}01 ) { write ; }" > rules_files

          # get first timestep for each month from previous file (if possible)
          ym1=$(date -u -d "$NYEAR-$NMONTH-01 - 1 month" +%Y%m)
          for XX in SH GG; do
              rm -f ICM${XX}
              if [ -f ICM${XX}${EXPID}+${ym1}.grb ]; then
                  grib_filter -o ICM${XX} rules_files \
                      ICM${XX}${EXPID}+${ym1}.grb ICM${XX}${EXPID}+${yyyymm}.grb
              else
                  ln -sf ICM${XX}${EXPID}+${yyyymm}.grb ICM${XX}
              fi

           done
         # remap on regular Gauss grid
          cdo splitparam -sp2gpl ICMSH ${file}_
          cdo -R splitparam ICMGG ${file}_

          # total precipitation (remove negative values)
          cdo setcode,228 -setmisstoc,0 -setvrange,0,Inf \
              -add ${file}_{142,143}.128.grb \
              ${file}_228.128.grb

          # daily variables
          for param in $param_day; do
              new_units=""
              case $param in
              ( 169 | 177 | 179 )
                  # radiation
                  new_units="W m-2"
                  cdo_operator="-divc,$((NFRP*3600)) -daymean -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 228 )
                  # precipitation
                  new_units="kg m-2 -s"
                  cdo_operator="-mulc,1000 -divc,$((NFRP*3600)) -daymean -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 201 )
                  # maximum
                  cdo_operator="-daymax -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 202 )
                  # minmimum
                  cdo_operator="-daymin -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 130 )
                  # 850 hPa
                  cdo_operator="-daymean -sellevel,85000 -selmon,$NMONTH" ;;
              ( * )
                  # default, plain daily mean
                  cdo_operator="-daymean -selmon,$NMONTH" ;;
              esac

              cdo -f nc -t ecmwf setreftime,$cdo_reftime $cdo_operator \
                  ${file}_${param}.128.grb ${file}_${param}_day.nc

              if [ -n "$new_units" ]; then
                  var=$(cdo showvar ${file}_${param}_day.nc | tr -d ' ')
                  ncatted -O -a units,$var,m,c,"$new_units" ${file}_${param}_day.nc
              fi

              # concat all vars in one file for day
              ncks -A ${file}_${param}_day.nc \
                  ${EXPID}_day_${yyyymm}01-${yyyymm}${NMDAYS}.nc
          done

          # monthly variables
          for param in  $param_mon; do
              new_units=""
              case $param in
              ( 146 | 147 | 176 | 169 | 177 | 175 | 179 | 212 )
                  # radiation/heat
                  new_units="W m-2"
                  cdo_operator="-divc,$((NFRP*3600)) -monmean -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 180 | 181 )
                  # momentum flux
                  new_units="N m-2"
                  cdo_operator="-divc,$((NFRP*3600)) -monmean -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 144 | 228 | 205 | 182 )
                  # precipitation/evaporation/runoff
                  new_units="kg m-2 s-1"
                  cdo_operator="-mulc,1000 -divc,$((NFRP*3600)) -monmean -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 201 )
                  # mean daily maximum
                  cdo_operator="-timmean -daymax -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 202 )
                  # mean daily minmimum
                  cdo_operator="-timmean -daymin -selmon,$NMONTH -shifttime,-${NFRP}hours" ;;
              ( 130 | 131 | 132 | 133 )
                  # upper-air
                  cdo_operator="-timmean -sellevel,5000,20000,50000,85000 -selmon,$NMONTH" ;;
#                  cdo_operator="-timmean -sellevel,85000,50000,20000,5000 -selmon,$NMONTH" ;;
              ( 129 )
                  # upper-air geopotential
                  new_units="m"
                  cdo_operator="-divc,9.81 -timmean -sellevel,5000,20000,50000,85000 -selmon,$NMONTH" ;;
#                  cdo_operator="-divc,9.81 -timmean -sellevel,85000,50000,20000,5000 -selmon,$NMONTH" ;;
              ( * )
                  # default, plain monthly mean
                  cdo_operator="-timmean -selmon,$NMONTH" ;;
              esac

              cdo -f nc -t ecmwf setreftime,$cdo_reftime $cdo_operator \
                  ${file}_${param}.128.grb ${file}_${param}_mon.nc

              if [ -n "$new_units" ]; then
                  var=$(cdo showvar ${file}_${param}_mon.nc | tr -d ' ')
                  ncatted -O -a units,$var,m,c,"$new_units" ${file}_${param}_mon.nc
              fi

              # concat all vars in one file for mon
              ncks -A ${file}_${param}_mon.nc \
                  ${EXPID}_mon_${yyyymm}01-${yyyymm}${NMDAYS}.nc
          done

          # 6-hourly variables
          for param in  $param_6hr; do
              new_units=""
              case $param in
              ( 129 )
                  # geopotential
                  new_units="m"
                  cdo_operator="-divc,9.81 -sellevel,50000 -selmon,$NMONTH" ;;
              ( * )
                  # default, plain monthly mean
                  cdo_operator="-selmon,$NMONTH" ;;
              esac

              cdo -f nc -t ecmwf setreftime,$cdo_reftime $cdo_operator \
                  ${file}_${param}.128.grb ${file}_${param}_6hr.nc

              if [ -n "$new_units" ]; then
                  var=$(cdo showvar ${file}_${param}_mon.nc | tr -d ' ')
                  ncatted -O -a units,$var,m,c,"$new_units" ${file}_${param}_6hr.nc
              fi

              # concat all vars in one file for mon
              ncks -A ${file}_${param}_6hr.nc \
                  ${EXPID}_6hr_${yyyymm}01-${yyyymm}${NMDAYS}.nc
          done

cp ${EXPID}_*nc ../.
rm fcdata*grb  
      done # loop over month
#for freq in 6h day mon 
#do
#  cdo merge ${EXPID}_${freq}_* ${EXPID}_${freq}_${startyear}${startmonth}01_${endyear}${endmonth}${NMDAYS}.nc
#done 


#new from smhi
       cd ..
       date
       if [ $NYEAR == 1850 && $NMONTH == 01 ]; then
        suff="1"
       else
        suff=""
       fi
        cat /home/pabretonniere/transfer/specs2cmor-1.0/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}
        sed -i -e s/cmoro/cmora/g namelist_${EXPID}
       # actual run
       cmor-atm.exe namelist_${EXPID}
      unset list
      # post cmorization steps
      cd $cmorao
        scp -r  ${SPECS2CMOR}/models/ecearth/test/cmora_output_${EXPID}/output/IC3/EC-EARTH3/* jasmin-xfer1.ceda.ac.uk:/group_workspaces/jasmin/specs/IC3/batch6/output/IC3/EC-EARTH3/.
      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}.gz /cfunas/exp/ecearth/cmorfiles/$EXPID/${START_date}/${MEMBER}/outputs/.
      touch $output_dir/cmora_${EXPID}_${START_date}_${MEMBER}
   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
done


date

