#!/bin/ksh

## Main setup script.
## creates main.ksh and config.ksh 
## Default to run with 2t,2d,10u,10v
## Emanuel Dutra, November 2017 
## Etienne Tourigny, February 2018

set -eux 

EXPNAME='aaaa'  # experiment name 

## check is SCRATCH is present
SCRATCH=${SCRATCH:-unset}
if [[ $SCRATCH == unset ]]; then
  echo "define SCRATCH to your local system"
  exit 1
fi

## Create scripts and data directory 
SCRIPTS=$SCRATCH/osm/$EXPNAME/scripts # to copy the "running script"
DATA=$SCRATCH/osm/$EXPNAME/work       # where the actual work is done 
BASE=$(dirname $(readlink -f "$0"))  # where we are now, the original scripts
rm -rfv $SCRIPTS/*
rm -rfv $DATA/*
mkdir -p $SCRIPTS
mkdir -p $DATA

## copy original script to $SCRIPTS directory 
cp $BASE/* $SCRIPTS 

##====================================
## create config.ksh file 
cat > $SCRIPTS/config.ksh << EOF
#!/bin/ksh

# General variables definitions to be used in osm
# should be added always as . config.ksh 

echo "====================="
echo "config.ksh start "
set -a
EXPNAME=$EXPNAME 

##=========================================
## Generic YMD as starting data 
YMD=\${YMD:-''} # YYYYMMDD
YM=\${YMD:0:6}  # YYYYMM


## Simulation specific 
DSTART=19900101 
DEND=19901231
DCHUNK="MON"  # monthly chunks 

## machine specific, do not edit 
## checks for the presence of ECACCOUNT variable that normaly is defined by default 
if [[ "\${ECACCOUNT:-unset}" != "unset" ]]; then
  echo "We're at ECMWF"
  # Files location at ECMWF
  ECINIT=/perm/ms/pt/ptb/ec-earth/ece3-inidata/ifs/T255L91/
  module load cdo
  module load nco
  module load netcdf4
  module load eccodes
  module load python 
elif [[ "\${BSC_MACHINE:-unset}" == "mn4" ]]; then
  echo "We're on MN4"
  ECINIT="/gpfs/projects/bsc32/models/ecearth/v3.2.3-esm/inidata/ifs/T255L91"
  #load modules before launching script since "module" does not work with ksh on MN4
  #TODO use bash instead of ksh!
  #module load gsl grib netcdf hdf5 CDO udunits nco python/2.7.13 ; module list
  export LD_LIBRARY_PATH=$HOME/soft/lib64:$HOME/soft/lib:$LD_LIBRARY_PATH
  export PYTHONPATH="\${HOME}/soft/lib/python2.7/site-packages/gribapi/:\${HOME}/soft/lib/python2.7/site-packages/eccodes/:\${PYTHONPATH:-''}"
else
  ECINIT=""
  echo "define ECINIT for your local system"
  exit 1
fi 

## Data specific - change this!
#ECMWF
FORCRAW="/perm/ms/pt/ptb/ec-earth/osm_data/"
### assumes the code is in $SCRATCH/ec-earth/r4381-surface_only/surf/ 
OSMBIN="$SCRATCH/ec-earth/r4381-surface_only/osm/cce-opt/build/bin/"

# MN4
#FORCRAW="$SCRATCH/osm/a0ts/45min/output/"
FORCRAW="$SCRATCH/osm/a0ts/3hr/output/"
OSMBIN="$SCRATCH/svn/r4971_osm_fixes/sources/ifs-36r4/src/osm/intel-opt/build/bin/"

# your own local 
#ECINIT="/disk1/data/work/ec-earth/ece3-inidata/ifs/T255L91/"
#FORCRAW="/disk1/data/work/ec-earth/ifs_test_data2/"
#OSMBIN="/home/emanuel/data/work/ec-earth/r4381-surface_only/osm/gnu-opt/build/bin/"

CLIMATE_DIR="\${ECINIT}/climate/"   # monthly climate fields
ICMGG00CLM="\${ECINIT}/19900101/ICMGGECE3INIT" # static, lsm, clv, tvl, etc... 
# z_n128.grb was processed from the spectral (requires mars)
#mars <<EOF
#read,source="ICMSHECE3INIT",para=z,grid=N128,target="z_n128.grb"
#EOF
ICMGG00Z="\${ECINIT}/19900101/z_n128.grb"  #grid-point orography 

# forcing info  
FLEV=91 # forcing level
FEXP=a0pq # forcing experiment name
ICMGG00="\${FORCRAW}/ICMGG\${FEXP}+000000" #initial conditions 
FTYPE=SFC # "SFC" to use 2t,2d,10v, 10v; "ML" to use u,v,t,q from lowest model level 
HAS_ML=true # set to false if ML output was not saved (TODO check for ML data)

# model setup
FREQ_PP_OSM=10800 
OSMTSTEP=3600 # default OSM timestep
#OSMTSTEP=2700 # default EC-Earth T255 timestep
OMP_NUM_THREADS=8
NCDFTYPE=4   # netcdf 4 type by default
#NCDFTYPE=3   # netcdf 4 type by default - use 3 at BSC for now

##=========================================
## 1 - Working folders locations
# Assumed: 
FIXDIR=${DATA}/inidata/fix/      # fixed climate data
DATADIR=${DATA}/inidata/\${YMD}/   # Forcing data 
RUNDIR=${DATA}/outdata/\${YMD}/    # Model run / post-processing 

set -a 
echo "====================="
echo "config.ksh end "
EOF


cat > $SCRIPTS/main.ksh << EOF
#!/bin/ksh

# Main control script 

set -eux


export SCRIPTS=$SCRIPTS
export DATA=$DATA 
export PATH="\$SCRIPTS:\${PATH:-''}"
export PYTHONPATH="\$SCRIPTS:\${PYTHONPATH:-''}"


. config.ksh 

abort=no

## Start by creating initial and climatological files 
## This is done only once 
time create_init_clim.ksh || abort=yes
[[ \$abort = yes ]]  &&  exit 1

## Loop on dates (monthly chunks )
export YMD=\$DSTART

while [[ \$YMD -le \$DEND ]];
do

  time create_forcing.ksh || abort=yes
  [[ \$abort = yes ]]  &&  exit 1
  time surface_model.ksh || abort=yes
  [[ \$abort = yes ]]  &&  exit 1
  time post_output.ksh || abort=yes
  [[ \$abort = yes ]]  &&  exit 1
  ## Update YMD 
  if [[ \$DCHUNK == "MON" ]]; then
    YMD=\$( date -d "\$YMD + 1 month" +"%Y%m%d" )
    export YMD
  else
    echo "\$DCHUNK not coded!!!"
  fi 
done

exit 0 
EOF
chmod +x $SCRIPTS/*







