#!/bin/bash
# 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 &

set -xuve
date

#
# General Stuff
#
EXPID=i03n
START_date=19900101
MEMBER=fc0
Chunk_start_date=19900101
Chunk_end_date=19900131
output_dir=$(pwd)
PATH=/scratch/cfu/opt/cdo-1.4.5.1/intel-13/bin:${PATH}

#
# Cmorization of ocean and atmos.'s output
#
cd $output_dir
CMORIZATION=TRUE
SPECS2CMOR=/scratch/cfu/opt/specs2cmor-1.0
MODEL=ecearth
NFRP=6
RFP3P="85000,50000,20000" # need special care by user, in case if require to modify the list of levels
if [[ $CMORIZATION == 'TRUE' && ! -a $output_dir/i03n_19900101_fc0_1_post.cmor ]]; then
   real=$(($(echo $MEMBER | cut -d 'c' -f 2)+1))
   init=1
   phys=1
   reftime=$(echo $START_date | cut -c1-6) # yyyymm
   PATH=$SPECS2CMOR/models/$MODEL/bin:${PATH}
   # ocean
   if [[ ! -a $output_dir/i03n_19900101_fc0_1_post.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)
cat << EOF > namelist_cmoro
&NAMEXP
 inpath='$cmoroi/',
 outpath='$cmoroo/',
 expname='${EXPID}',
 experiment_id='historical1990',
 source='EC-EARTH 3.0.1 official release from SMHI',
 model_id='EC-EARTH',
 ecearth=3,
 forcing='N/A',
 parent_experiment_id='piControl',
 parent_experiment_rip='r$((real+1))i${init}p${phys}',
 institution='IC3',
 realization=$real,
 history='',
 comment='',
 references='',
 contact='PA Bretonnière <pierre-antoine.bretonniere@ic3.cat>',
 startyear=$startyear,
 endyear=$endyear,
 startmonth=$startmonth,
 endmonth=$startmonth,
 initialization_method=$init,
 initialization_description='N/A',
 physics_version=$phys,
 physics_description='N/A',
 associated_model='N/A',
 forecast_ref_time=$reftime,
 prefix='$EXPID',
 series='series1'
/
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 $tarfile
      touch $output_dir/i03n_19900101_fc0_1_post.cmoro
   fi
   # atmosphere
   if [[ ! -a $output_dir/i03n_19900101_fc0_1_post.cmora ]]; then
      # available: daily grib (3h or 6h) on monthly basis
      cmorai=$output_dir/cmora_input
      cmorao=$output_dir/cmora_output
      mkdir -p $cmorai $cmorao
      cd $cmorai
      ln -sf $SPECS2CMOR/common/Tables .
      ln -sf $SPECS2CMOR/models/$MODEL/input/* .
      cnt=$(ls -1 $output_dir/ICMGG${EXPID}* | wc -l)
      yyyymms=$(ls -1 $output_dir/ICMGG${EXPID}* | cut -d '+' -f 2 | tail -n $((cnt-1)))
      for yyyymm in $yyyymms; do
       cdo sp2gpl $output_dir/ICMSH${EXPID}+${yyyymm} fcdata0
       cat fcdata0 $output_dir/ICMGG${EXPID}+${yyyymm}  > fcdata
       rm fcdata0
       NYEAR0=$(echo $START_date | cut -c1-4)
       NYEAR=$(echo ${yyyymm} | cut -c1-4)
       NMONTH=$(echo ${yyyymm} | cut -c5-6)
       NMDAYS=$(cal ${NMONTH} ${NYEAR} | grep -v [a-z] | grep -v '^$' | wc -w)
       if [[ ${NYEAR} == 1850 && ${NMONTH} == 01 ]]; then
        suff=1
       else
        suff=""
       fi
cat << EOF > namelist_cmora
&NAMCTL
 NYEAR0=$NYEAR0,
 NYEAR=$NYEAR,
 NMONTH=$NMONTH,
 NMDAYS=$NMDAYS,
 NFRP=$NFRP,
 LWGRIB=.FALSE.,
 NPRINTLEV=0,
 suffix_freq_sfc='$suff',
 RFP3P=$RFP3P
/
&NAMDATASET
 outpath='$cmorao/',
 experiment_id='historical1990',
 source='EC-EARTH 3.0.1 official release from SMHI',
 model_id='EC-EARTH',
 forcing='N/A',
 parent_experiment_id='piControl',
 parent_experiment_rip='r$((real+1))i${init}p${phys}',
 institution='IC3',
 realization=$real,
 history='',
 comment='',
 references='',
 contact='PA Bretonnière <pierre-antoine.bretonniere@ic3.cat>',
 initialization_method=$init,
 initialization_description='N/A',
 project_id='SPECS project',
 physics_version=$phys,
 physics_description='N/A',
 associated_model='N/A',
 forecast_ref_time=$reftime,
 series='series1'
/
EOF
       # actual run
       cmor-atm.exe namelist_cmora
      done
      # 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 $tarfile
      touch $output_dir/i03n_19900101_fc0_1_post.cmora
   fi
   if [[ -a $output_dir/i03n_19900101_fc0_1_post.cmoro && -a $output_dir/i03n_19900101_fc0_1_post.cmora ]]; then
      rm $output_dir/i03n_19900101_fc0_1_post.cmoro $output_dir/i03n_19900101_fc0_1_post.cmora
      rm -rf $cmoroi $cmoroo $cmorai $cmorao
      touch $output_dir/i03n_19900101_fc0_1_post.cmor
   fi
fi

date

