#!/bin/bash
set -x

ulimit -s unlimited

#-----------------------------------------------------------------------
# MAIN MONARCH RUN SCRIPT - Define MN settings first
#-----------------------------------------------------------------------
INPES=16                      # Number inpes    
JNPES=32                      # Number jnpes   
WRTSK=08                      # Number write tasks  
#-----------------------------------------------------------------------
# Global-regional switch - Model domain setup global/regional
#-----------------------------------------------------------------------
DOMAIN=0                      # GLOBAL/REGIONAL (0/1)
LM=48                         # Vertical model layers
CASE=template_ga              # Name of the experiment case
MODE=GLOBAL_AEROSOLS          # Base configuration (DUST/GLOBAL_AEROSOLS/REGIONAL_CHEM) 
#-----------------------------------------------------------------------
# If Global - domain setup
#-----------------------------------------------------------------------
DT_INT1=90                    # Run time step (integer seconds) 
TLM0D1=0.0                    # Center point longitudinal (E/W)
TPH0D1=0.0                    # Center point latitudinal (S/N)
WBD1=-180.0                   # Western boundary (from center point)
SBD1=-90.0                    # Southern boundary (from center point)
DLMD1=0.703125                # Longitudinal grid resolution
DPHD1=0.5                     # Latitudinal grid resolution
PTOP1=500.                    # Pressure top of the domain (Pa)
DCAL1=0.46                    # Mineral Dust Emission Calibration Factor
NRADS1=40                     # Number of timesteps between radiation calls (short)
NRADL1=40                     # Number of timesteps between radiation calls (long)
#-----------------------------------------------------------------------
# If Regional - domain setup
#-----------------------------------------------------------------------
DT_INT2=24                    # regional           
TLM0D2=6.0                    # regional           
TPH0D2=42.5                   # regional           
WBD2=-26.16                   # regional          
SBD2=-21.8                    # regional          
DLMD2=0.109                   # regional            
DPHD2=0.109                   # regional            
PTOP2=5000.                   # Pressure top of the domain (Pa)
DCAL2=0.3                     # Mineral Dust Emission Calibration Factor
NRADS2=150                    # Number of timesteps between radiation calls (short)
NRADL2=150                    # Number of timesteps between radiation calls (long)
#-----------------------------------------------------------------------
# Ensemble perturbation factors  
#-----------------------------------------------------------------------
DCALBIN1=1.                   # Perturbation factor for the vertical flux per bin, rewritten by preprare script
DCALBIN2=1.
DCALBIN3=1.
DCALBIN4=1.
DCALBIN5=1.
DCALBIN6=1.
DCALBIN7=1.
DCALBIN8=1.
PERTURB_USTH=1.               # Perturbation factor for the threshold on the friction velocity, rewritten by preprare script
#-----------------------------------------------------------------------
#TUNING FOR SOIL HUMIDITY THRESHOLD AND WET DEPOSITION
#-----------------------------------------------------------------------
FECAN=1.75              # Fecan coefficient (typically >= 1)
SMCCOEF=1.0             # soil moisture scaling (typically =< 1)
                        # recommended to use either fecan or smccoef and not both at the same time
CTHR=1.0                # erosion threshold scaling (typically <= 1)
BCL_MP_C=50.            # tuning factor for coarse dust below cloud scavenging (grid scale clouds)
BCL_MP_M=100.           # tuning factor for medium dust below cloud scavenging (grid scale clouds)
BCL_MP_F=100.           # tuning factor for fine dust below cloud scavenging (grid scale clouds)
BCL_BMJ_C=50.           # tuning factor for coarse dust below cloud scavenging (convective clouds)
BCL_BMJ_M=100.          # tuning factor for medium dust below cloud scavenging (convective clouds)
BCL_BMJ_F=100.          # tuning factor for fine dust below cloud scavenging (convective clouds)
ICL_MP=1.               # tuning factor for dust incloud scavenging (grid scale clouds)
ICL_BMJ=4.              # tuning factor for dust incloud scavenging (convective clouds)

SEDIM_TYPE=1            # sedimentation scheme (0-implicit,1 explicit) both upwind (pretty diffusive)
VSED5=0.5               # correction factor of sedimentation velocity for bin 5 to correct for diffusive scheme 
VSED6=0.3               # correction factor of sedimentation velocity for bin 6 to correct for diffusive scheme
VSED7=0.2               # correction factor of sedimentation velocity for bin 7 to correct for diffusive scheme
VSED8=0.1               # correction factor of sedimentation velocity for bin 8 to correct for diffusive scheme

VDRY=0.5                # tuning factor for dry deposition velocity (excluding gravitational settling

#-----------------------------------------------------------------------
#CHOICE OF DUST SOURCE, EMITTED SIZE DISTRIBUTION AND EMISSION SCHEME
#-----------------------------------------------------------------------
SRC=3                  # 0 Old topographic source from Ginoux et al. (2001) derived at 0.25 degrees (DUSCHEME=0,1,2,3)
                       # 1 MODIS DB C6 based foo (climatology) derived at 0.1 degrees (both natural and anthropogenic together, HYDE 2) (DUSCHEME=0,1,2,3)
                       # 2 MODIS DB C6 based foo (climatology) derived at 0.1 degrees (tagged natural and anthropogenic, HYDE 2) (DUSCHEME=0,1,2,3)
                       # 3 Like #1 to identify the location of sources but like #0 (topo) for the calculation of the vertical flux (DUSCHEME=0,1,2)
                       # 4 Like #2 to identify the location of sources but like #0 (topo) for the calculation of the vertical flux (DUSCHEME=0,1,2)
#                        (#3 and #4 are equivalent to #1 and #2 respectively in the case of DUSCHEME=3)
#                        (For the options above, in the case of DUSCHEME>=3 (Kok, Shao, Klose), SRC is only used to identify the location of the sources) 
                       # 5 No source based on topo or MODIS (only DMASK) and no tagging
                       # 6 No source based on topo or MODIS (only DMASK) and tagging for anthropogenic and natural based on MODIS DB 
#                      [[[ the following options [7-14] use #0 (topo) for vertical flux in the case of DUSCHEME=0,1,2; no effect for DUSCHEME > 2 ]]]
                       # 7  MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (natural and anthropogenic together), actual year
                       # 8  MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (tagged natural and anthropogenic [cropland and pasture]), actual year
                       # 9  MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (tagged natural and anthropogenic [cropland, pasture, and converted rangeland]), actual year
                       # 10 MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (tagged natural and anthropogenic [cropland, pasture, conv. rangeland, and rangeland]), actual year
                       # 11 MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (natural and anthropogenic together), climatology
                       # 12 MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (tagged natural and anthropogenic [cropland and pasture]), climatology
                       # 13 MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (tagged natural and anthropogenic [cropland, pasture, and converted rangeland]), climatology
                       # 14 MODIS DB foo and HYDE 3.2.1 land use at 0.1 degrees (tagged natural and anthropogenic [cropland, pasture, conv. rangeland, and rangeland]), climatology

DMASK=1                # 0 Standard desert mask based on monthly vegetation fraction (Perez et al. 2011)
#                          (If SRC=0 it uses the fixed and already masked topo source)
                       # 1 Based on MODIS C5 monthly LAI for the specific year (available for 2001 to 2015)
                       # 2 Based on MODIS C5 monthly LAI climatology (2001 to 2015)
                       # 3 Based on Landsat and MODIS reflectance (Guerschman et al. 2015) (monthly data, 01/2001 to 08/2018)
                       # 4 Based on Landsat and MODIS reflectance (Guerschman et al. 2015) (monthly climatology, 2001-2017)

VEGF_UNI=1             # 0 Use default USGS climatological vegetation for meteorology and land-surface while using vegetation from DMASK/DRAG_PART for dust 
                       # 1 Use same vegetation (Guerschman et al. 2015) for meteorology and land-surface as for dust (only applies if DMASK = 3 or 4 and DRAG_PART = 4)

INTERP=1               # 0 Bilinear interpolation of the New MODIS DB C6 foo sources and LAI to the model grid (recommended for resolutions < 0.1 deg)
                       # 1 Averaging the 0.1 deg new MODIS DB C6 foo sources and LAI over coarser resolution grid (recommended for resolutions > 0.1deg)  

INTERP2=1              # Always 1 for pringent roughness

SZDSTR=1               # 0 Emitted size distribution from D'Almeida
                       # 1 Emitted size distribution from Kok

DUSCHEME=1             # 0 Marticorena and Bergametti, NMMB default dust emission scheme (Perez et al 2011)
                       # 1 GOCART dust emission scheme from Paul Ginoux using 10 m wind (Klose et al., 2020)
                       # 2 GOCART dust emission scheme from Paul Ginoux using friction velocity (Klose et al., 2020)                     
                       # 3 KOK dust emission scheme (Kok et al. 2014)
                       # 4 SHAO (2001), JGR
                       # 5 SHAO (2004), JGR
                       # 6 SHAO et al. (2011), JGR - simplification of #5
 
VSLAYER=1              # 0 NMMB default with viscous sublayer
                       # 1 no viscous sublayer (only applies for NMMB default dust scheme) 

SMCCOR=0               # soil moisture correction for threshold friction velocity in DUSCHEMES 0-6 (DUSCHEME 7 has different treatment)                                 
                       # 0 default (2 for DUSCHEME 0; 1 for DUSCHEMES 1-2; 3 for DUSCHEME 3; 4 for DUSCHEMES 4-6)                                                       
                       # 1 Belly (1964)/Ginoux et al. (2001)                        
                       # 2 Fecan et al. (1999) - grav. soil moisture, fixed coefficients (Zender et al., 2003, Perez et al., 2011)     
                       # 3 Shao and Jung (2000), Klose et al. (2014) - vol. soil moisture, soil-texture dependent coefficients

DRAG_PART=3            # 0 no drag partition scheme in the emission
                       # 1 drag partition following Marticorena with X parameter = 10.
                       # 2 drag partition following Marticorena with X parameter = 12255.
                       # 3 drag partition following Marticorena with X parameter = 40.
                       # 4 drag partition following Raupach 1993 (automatically chosen for DUSCHEME >= 4) 
                       #   [vegetation fraction product determined by DMASK: DMASK=3-4 -> new Guerschman vegetation; else -> USGS climatology]

USTDP=1                # 0 apply drag partition to ustt
                       # 1 apply drag partition to ust                       
                       
DEXT=0                 # 0 default dust extinction (spherical particles) 
                       # 1 dust extinction assuming tri-axial spheroids

#-----------------------------------------------------------------------
#CALCULATE NUMBER OF BINS BASED ON SOURCE CHOICE
#-----------------------------------------------------------------------
NDUST=8                   # 0 (No dust) or 8.   No tagging for SRC options 0 and 1
CATAERO=2                 # 2 output dod (all sky and clear sky) (only 2 works) 
#if [ $SRC -eq 2 ] || [ $SRC -eq 4 ] || [ $SRC -eq 6 ] ; then  
if [ $SRC -gt 1 ] && [ $SRC -ne 3 ] && [ $SRC -ne 5 ] && [ $SRC -ne 7 ] && [ $SRC -ne 11 ]; then  
echo "SRC = " $SRC
let NDUST=$NDUST*2        # Tagging natural and anthropogenic dust for SRC=2
let CATAERO=$CATAERO*2
fi
if [ $MODE == "DUST" ] ; then
NCHEM=$NDUST
elif [ $MODE == "GLOBAL_AEROSOLS" ] ; then
NCHEM=42
elif [ $MODE == "REGIONAL_CHEM" ] ; then
NCHEM=110
fi
CLIM=1
if [ $DMASK -eq 2 ]; then # for fbare_modis.f90 select actual year or climatology
let CLIM=2
let DMASK=1
elif [ $DMASK -eq 4 ]; then # for vegfra_csiro_modis.f90 select actual year or climatology
let CLIM=2
fi

#-----------------------------------------------------------------------
# Select flags for Aerosol-Radiation-Interaction
#-----------------------------------------------------------------------
XMDL=3                 # flag for ARI module (mdlari in configfile)
                       #   : 0 -> OPAC climatology
                       #   : 3 -> MONARCH dynamic coupling
XMLC=0                 # flag for multiple call (mlcari in configfile)
                       #   : 0 -> no DRE
                       #   : 1 -> DRE of all types
                       #   : 2 -> DRE of single types (alone) + all types
                       #   : 3 -> DRE of single types (missing) + all types
XTBL=1                 # flag for optical table (tblari in configfile)
                       #   : 1 -> OPAC refractive indices (homogeneous dust components)
                       #   : 2 -> OPAC refractive indices + emission p50 for dust imaginary index in (0.25-2.00)µm
                       #   : 3 -> mineral refractive indices: external mixing
                       #   : 4 -> mineral refractive indices: internal mixing for hematite-clays (Maxwell-Garnett mixing rule)
XKOK=0                 # flag for shape (only for XTBL=1 or XTBL=2)
                       #   : 0 -> spheres
                       #   : 1 -> tri-axial spheroids for dust in (0.25-2.00)µm

#-----------------------------------------------------------------------
# Case selection
#-----------------------------------------------------------------------
DO_FIXED=1                    # RUN FIXED (0/1) 
DO_HERMES=1                   # RUN HERMESv3 (0/1)
DO_VRBL=1                     # RUN VRBL (0/1) 
DO_UMO=1                      # RUN UMO (0/1) 
DO_PREMEGAN=1                 # PREPARE MEGAN METEO INPUT FILES (0/1) 
#-----------------------------------------------------------------------
# Select START and ENDING Times
#-----------------------------------------------------------------------
START=20160301                # First day of simulation
END=20160302                  # Last day of simulation (if just one day --> START = END)
HOUR=0                        # Choose the time of initial input data (hours)
SPINUP=0                      # Hours of spinup chemistry
NHOURS=24                     # Length of the forecast (hours)
NHOURS_INIT=24                # History init previous day timestep
HIST=3                        # Frequency of history output (up to 552 hours)
HIST_M=180                    # Frequency of history output (in minutes)
BOCO=6                        # Frequency of boundary condition update for REGIONAL (hours)
TYPE_GFSINIT="GFS"            # FNL or GFS or ERA
TYPE_BCCHEM="CAMS"            # CAMS or MOZART (only for REGIONAL_CHEM)
FCST=.true.                   # Is this a forecast run? only used in global_aerosols or regional_chem (.true./.false.)
#-----------------------------------------------------------------------
# Select IC of chemistry for run with COUPLE_DUST_INIT=0
#-----------------------------------------------------------------------
INIT_CHEM=0                   # 0. IC from ideal conditions 
                              # 1 initialize from a previous run
                              # 2 initialize from inca chem data
                              # 3 initialize from global nmmb-ctm run for regional
                              # 4 initialize from a previous run of different resolution
#-----------------------------------------------------------------------
# Couple dust
#-----------------------------------------------------------------------
COUPLE_DUST=1                 # Couple dust for the next run (0/1)
COUPLE_DUST_INIT=0            # Couple dust from the beginning (0/1)
#-----------------------------------------------------------------------
# Put the necessary paths here (CHANGE THE UMO_PATH WITH YOUR OWN)
#-----------------------------------------------------------------------
export UMO_PATH=/gpfs/scratch/group/user/tmp/PREPARE_REGIONAL_RUNS/nmmb-monarch 
export DATAGEOG=/gpfs/projects/group/models/monarch/data
export FIX=$UMO_PATH/OUTPUT/$CASE/FIXED
export VRB=$UMO_PATH/OUTPUT/$CASE/VARIABLE
export OUTPUT=$UMO_PATH/OUTPUT/$CASE/output
export POST1=$UMO_PATH/OUTPUT/$CASE/POSTPROC/v1.0
export POST2=$UMO_PATH/OUTPUT/$CASE/POSTPROC/v2.0
export UMO_ROOT=$UMO_PATH/JOB/TEMPLATE  
export SRCDIR=$UMO_PATH/MODEL
export UMO_OUT=$UMO_PATH/OUTPUT/$CASE/CURRENT_RUN
export GRB=$UMO_PATH/DATA/initial
export DATMOD=$UMO_PATH/DATA/static
export CHEMIC=/gpfs/scratch/group/user/tmp/PREPARE_GLOBAL_FCST/DATA_required/T0-monarch2.0-global-runs
#export CHEMIC=/gpfs/scratch/group/user/tmp/PREPARE_REGIONAL_RUNS/DATA_required/T0-monarch2.0-regional-runs
export BC_CHEM=/gpfs/scratch/group/user/tmp/PREPARE_REGIONAL_RUNS/DATA_required/BC-$TYPE_BCCHEM
export HERMES=/gpfs/scratch/group/user/tmp/PREPARE_GLOBAL_FCST/HERMESv3 
#export HERMES=/gpfs/scratch/group/user/tmp/PREPARE_REGIONAL_RUNS/HERMESv3
export OUTNMMB=$UMO_PATH/OUTPUT
export PREMEGAN=$UMO_PATH/OUTPUT/$CASE/megan
export DATAMEGAN=/gpfs/projects/group/models/monarch/data/static/emissions/DATA_megan_v2.04
export HERMES_DATA_GFAS=/gpfs/scratch/group/user/DATA
export HERMES_DATA_ANTHRO=/gpfs/projects/group/models/hermesv3/data

export FNL=/gpfs/scratch/group/user/DATA/FNL
export GFS=/gpfs/scratch/group/user/DATA/GFS
export ERA=/gpfs/scratch/group/user/DATA/ERA
#-----------------------------------------------------------------------
# END USER MODIFICATION SECTION
# HANDS OFF FROM SETTING SECTION BELOW !!!
#-----------------------------------------------------------------
# Clean output folder
#-----------------------------------------------------------------
rm -r $UMO_OUT/*

#-----------------------------------------------------------------
# MN settings in run_rrtm.cmd
#-----------------------------------------------------------------

PROC=$(echo "scale=0 ; $INPES*$JNPES+$WRTSK" | bc)

echo "Number of processors" $PROC

if [ $DOMAIN -eq 0 ] ; then
   WBD=$WBD1
   SBD=$SBD1
   DLMD=$DLMD1
   DPHD=$DPHD1
elif [ $DOMAIN -eq 1 ] ; then
   WBD=$WBD2
   SBD=$SBD2
   DLMD=$DLMD2
   DPHD=$DPHD2
fi

IMI=$(echo "scale=1 ; -2.0*$WBD/$DLMD+1.5" | bc)
JMI=$(echo "scale=1 ; -2.0*$SBD/$DPHD+1.5" | bc)
IMI=${IMI/.*}
JMI=${JMI/.*}

if [ $DOMAIN -eq 0 ] ; then
   let IM=$IMI+2
   let JM=$JMI+2
elif [ $DOMAIN -eq 1 ] ; then
   IM=$IMI
   JM=$JMI
fi

echo " "
echo "Model grid size - IM/JM/LM: " $IMI " / " $JMI " / " $LM
echo "Extended domain - IM/JM/LM: " $IM " / " $JM " / " $LM
echo " "

let HOURCTL=$HOUR+$HIST
HOUR=`printf "%.2d" $HOUR`
HOURCTL=`printf "%.2d" $HOURCTL`

#-----------------------------------------------------------------
# Date conversion
#-----------------------------------------------------------------

DATE=$START

YEAR=`date -ud "$DATE" +%Y`
YY=`date -ud "$DATE" +%y`
MM=`date -ud "$DATE" +%m`
DD=`date -ud "$DATE" +%d`
MTH=`date -ud "$DATE" +%b`

DATENEW=$YY$MM$DD$HOUR
DATENEW2=$YY$MM$DD
DATENEW3=$MM$DD
COUPLE_TMP=$COUPLE_DUST
HOURNEW="t"$HOUR"z"

DATE_YESTERDAY=`date -ud "$DATE -1day" +%Y%m%d`

#-----------------------------------------------------------------
# Define and create folders by case
#-----------------------------------------------------------------

if [ -d $UMO_PATH"/DATA" ] ; then
   echo "DATA STATIC already linked"
else
   echo "Creating DATA STATIC link"
   ln -sf $DATAGEOG $UMO_PATH/DATA
fi

mkdir -p $UMO_PATH/OUTPUT/$CASE/
mkdir -p $UMO_PATH/OUTPUT/$CASE/output
mkdir -p $UMO_PATH/OUTPUT/$CASE/CURRENT_RUN

rm $UMO_PATH/OUTPUT/$CASE/geodata
rm $UMO_PATH/OUTPUT/$CASE/GTOPO30

ln -sf $UMO_PATH/DATA/static/geodata $UMO_PATH/OUTPUT/$CASE/geodata
ln -sf $UMO_PATH/DATA/static/GTOPO30 $UMO_PATH/OUTPUT/$CASE/GTOPO30 

#-----------------------------------------------------------------
#   Fixed process (do before main time looping)
#-----------------------------------------------------------------

if [ $DO_FIXED -eq 1 ] ; then

   echo "Enter Fixed process"

   echo "Importing Fixed Files to case" 

   if [ -d "$FIX" ]
   then
    	echo "Fixed folder already exists. Not copying."
   else
        cp -rp $UMO_PATH/PREPROC/FIXED/ $UMO_PATH/OUTPUT/$CASE/
   fi

   cd $FIX

   rm -f $OUTPUT/*

   ############ prepare model grid setup files global ###############

   if [ $DOMAIN -eq 0 ] ; then

      rm modelgrid.inc
      rm lmimjm.inc
      sed -e  "s/TLMD/$TLM0D1/" \
          -e  "s/TPHD/$TPH0D1/" \
          -e  "s/WBDN/$WBD1/" \
          -e  "s/SBDN/$SBD1/" \
          -e  "s/DLMN/$DLMD1/" \
          -e  "s/DPHN/$DPHD1/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP1/" \
          -e  "s/KKK/$LM/"  modelgrid_rrtm.tmp > modelgrid.inc
      sed -e  "s/TLMD/$TLM0D1/" \
          -e  "s/TPHD/$TPH0D1/" \
          -e  "s/WBDN/$WBD1/" \
          -e  "s/SBDN/$SBD1/" \
          -e  "s/DLMN/$DLMD1/" \
          -e  "s/DPHN/$DPHD1/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP1/" \
          -e  "s/KKK/$LM/"  lmimjm_rrtm.tmp > lmimjm.inc

      ############ run global job  #####################################

     if $(sbatch -V &> /dev/null) ; then
          echo "Scheduler is SLURM"
          ssh login4 "$FIX/runfixed_rrtm.scr $FIX"
     elif $(bsub -V &> /dev/null) ; then 
          echo "Scheduler is LSF"
          $FIX/runfixed_rrtm.scr $FIX
     else
           echo "Scheduler not identified"
      fi

   ############ prepare model grid setup files regional #############

   elif [ $DOMAIN -eq 1 ] ; then

      rm modelgrid.inc
      rm lmimjm.inc
      sed -e  "s/TLMD/$TLM0D2/" \
          -e  "s/TPHD/$TPH0D2/" \
          -e  "s/WBDN/$WBD2/" \
          -e  "s/SBDN/$SBD2/" \
          -e  "s/DLMN/$DLMD2/" \
          -e  "s/DPHN/$DPHD2/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP2/" \
          -e  "s/KKK/$LM/"  modelgrid_rrtm.tmp > modelgrid.inc
      sed -e  "s/TLMD/$TLM0D2/" \
          -e  "s/TPHD/$TPH0D2/" \
          -e  "s/WBDN/$WBD2/" \
          -e  "s/SBDN/$SBD2/" \
          -e  "s/DLMN/$DLMD2/" \
          -e  "s/DPHN/$DPHD2/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP2/" \
          -e  "s/KKK/$LM/"  lmimjm_rrtm.tmp > lmimjm.inc

     if $(sbatch -V &> /dev/null) ; then
          echo "Scheduler is SLURM"
          ssh login4 "$FIX/runfixed_rrtm.scr $FIX"
     elif $(bsub -V &> /dev/null) ; then 
          echo "Scheduler is LSF"
          $FIX/runfixed_rrtm.scr $FIX
     else
           echo "Scheduler not identified"
      fi

   fi

   echo "Fixed process finished"

fi

#-----------------------------------------------------------------
# Start main time loop
#-----------------------------------------------------------------
while [ $DATE -le $END ]; do

echo "DATE: " $DATE "simulation started"

#-----------------------------------------------------------------
# Define model output folder by case and date
#-----------------------------------------------------------------
FOLDER_OUTPUT_CASE=$OUTNMMB/$CASE
FOLDER_OUTPUT=$OUTNMMB/$CASE/$DATE$HOUR

#-----------------------------------------------------------------
#   HERMESv3 process
#-----------------------------------------------------------------

if [ $DO_HERMES -eq 1 ] ; then

   if [ $COUPLE_DUST_INIT -eq 1 ] ; then
    echo "Executing HERMES, it's a warm start"

   module load intel/2017.4
   module load impi/2017.4
   module load mkl/2017.4
   module load udunits/2.2.25
   module load esmf
   module load python/2.7.13
   module load CDO
   module load hdf5/1.8.19
   module load netcdf/4.4.1.1


   if [ $FCST == ".true." ];then
     HERMES_DATE_GFAS=$DATE_YESTERDAY$HOUR
   else
     HERMES_DATE_GFAS=$DATE$HOUR
   fi
   HERMES_DATE=$DATE$HOUR

   if [ $DOMAIN -eq 0 ] && [ $MODE == "GLOBAL_AEROSOLS" ] ; then

      let OUTPUT_TIMESTEP_FREQ=$NHOURS-$SPINUP

      cd $HERMES/IN/src

      mpirun -np 128 python hermes.py --my-config ../conf/hermes_glob_anthro_aerosols.con --input_dir $HERMES/IN --data_path $HERMES_DATA_ANTHRO --output_dir $HERMES/OUT --output_name "HERMESv3_glob_<date>.nc" --start_date $HERMES_DATE --output_timestep_type hourly --output_timestep_num $OUTPUT_TIMESTEP_FREQ --output_timestep_freq 1 --domain_type global --inc_lat $DPHD1 --inc_lon $DLMD1 || exit 1 # "<input_dir>/conf/EI_configuration_GFAS.csv" 

      mpirun -np 128 python hermes.py --my-config ../conf/hermes_glob_gfas_aerosols.con --input_dir $HERMES/IN --data_path $HERMES_DATA_GFAS --output_dir $HERMES/OUT --output_name "GFAS_glob_<date>.nc" --start_date $HERMES_DATE_GFAS --output_timestep_type daily --output_timestep_num 1 --output_timestep_freq 1 --domain_type global --inc_lat $DPHD1 --inc_lon $DLMD1 || exit 1 # "<input_dir>/conf/EI_configuration_GFAS.csv" 

   elif [ $DOMAIN -eq 1 ] && [ $MODE == "REGIONAL_CHEM" ] ; then

      let OUTPUT_TIMESTEP_FREQ=$NHOURS-$SPINUP

      cd $HERMES/IN/src

      mpirun -np 128 python hermes.py --my-config ../conf/hermes_reg_anthro_fullchem.conf --input_dir $HERMES/IN --data_path $HERMES_DATA_ANTHRO --output_dir $HERMES/OUT --output_name "HERMESv3_reg_<date>.nc" --start_date $HERMES_DATE --output_timestep_type hourly --output_timestep_num $OUTPUT_TIMESTEP_FREQ --output_timestep_freq 1 --domain_type rotated --centre_lat $TPH0D2 --centre_lon $TLM0D2 --west_boundary $WBD2 --south_boundary $SBD2 --inc_rlat $DPHD2 --inc_rlon $DLMD2 || exit 1

      mpirun -np 128 python hermes.py --my-config ../conf/hermes_reg_gfas_aerosols.conf --input_dir $HERMES/IN --data_path $HERMES_DATA_GFAS --output_dir $HERMES/OUT --output_name "GFAS_reg_<date>.nc" --start_date $HERMES_DATE --output_timestep_type daily --output_timestep_num 1 --output_timestep_freq 1 --domain_type rotated --centre_lat $TPH0D2 --centre_lon $TLM0D2 --west_boundary $WBD2 --south_boundary $SBD2 --inc_rlat $DPHD2 --inc_rlon $DLMD2 || exit 1

   fi
  
   fi

fi

#-----------------------------------------------------------------
#   Vrbl process
#-----------------------------------------------------------------

if [ $DO_VRBL -eq 1 ] ; then

   echo "Enter Vrbl process"

   echo "Importing Variable Files to case" 

   if [ -d "$VRB" ]
   then
        echo "Variable folder already exists. Not copying."
   else
        cp -rp $UMO_PATH/PREPROC/VARIABLE/ $UMO_PATH/OUTPUT/$CASE/
        ln -s $BC_CHEM $UMO_PATH/OUTPUT/$CASE/.
   fi

   echo "Cleaning output folder"

   cd $OUTPUT
   rm *.gfs
   rm gfs.*
   rm sst2dvar_grb_0.5
   rm *ERAINT*.grb
   rm fcst
   rm boco.*
   rm boco_chem.*
   rm $VRB/sstgrb
   rm $VRB/content*
   rm llstmp
   rm llsmst
   rm llgsno
   rm llgcic
   rm llgsst
   rm llspl.000
   rm llgsst05
   rm albedo
   rm albase
   rm vegfrac
   rm z0base
   rm z0
   rm ustar
   rm sst05
   rm dzsoil
   rm tskin
   rm sst
   rm snow
   rm snowheight
   rm cice
   rm seamaskcorr
   rm landusecorr
   rm landusenewcorr
   rm topsoiltypecorr
   rm vegfraccorr
   rm z0corr
   rm z0basecorr
   rm emissivity
   rm canopywater
   rm frozenprecratio
   rm smst
   rm sh2o
   rm stmp
   rm bc_chem.nc*
   rm mz4geos_fcst_*.nc
   rm fc_*.nc
   rm vegfrc_new
   rm source_new_nat
   rm source_new_ant

   BOCOS=`printf "%.2d" $BOCO`

   if [ $MODE == "REGIONAL_CHEM" ] ; then

   if [ $TYPE_BCCHEM == "MOZART" ] ; then

# prepare bc_chem data
# use as many mz4geos_fcst_....nc as needed by the length of the fcst and cat them to bc_chem.nc

      for i in $(seq $HOUR $BOCO $NHOURS)
      do
         var=`printf "%.2d" $i`
         hday=`printf "%.2d" $(($var % 24))`
         sum_day=$(($var / 24))
         day=`date -d "${DATE} ${sum_day} days" +%Y%m%d`
         day_mod=`date -d "${DATE} ${sum_day} days" +%m%d`
         if [ ${hday} == $HOUR ] ; then
            ln -s $BC_CHEM/mz4geos_fcst_2016${day_mod}.nc $OUTPUT/mz4geos_fcst_${day}.nc
         fi
      done

      ncrcat ./mz4geos_fcst_*nc ./bc_chem.nc

   fi

   if [ $TYPE_BCCHEM == "CAMS" ] ; then
# prepare bc_chem data
# use as many fc_yyyymmdd.nc as needed by the length of the fcst and cat them to bc_chem.nc

       let HOUR_INI=$HOUR #+$SPINUP
       DATE_INI=`date -ud "$DATE $HOUR_INI hours" +%Y%m%d`
       cp $BC_CHEM/fc_${DATE_INI}.nc $OUTPUT/bc_chem.nc

   fi

   fi

   echo "-------------------"
   echo "execute vrbl script: prep_rrtm"
   echo "-------------------"
   
   cd $VRB

############ prepare input grid setup file #######################

   rm llgrid.inc

   if [ $TYPE_GFSINIT == "FNL" ] || [ $TYPE_GFSINIT == "GFS" ]; then
      sed -e  "s/LLL/$NHOURS/" \
          -e  "s/HH/$HOUR/" \
          -e  "s/UPBD/$BOCO/" \
          -e  "s/YYYYMMDD/$DATENEW2/" llgrid_rrtm_$TYPE_GFSINIT.tmp > llgrid.inc 
   elif [ $TYPE_GFSINIT == "ERA" ]; then
      sed -e  "s/LLL/$NHOURS/" \
          -e  "s/UPBD/$BOCO/" llgrid_rrtm_$TYPE_GFSINIT.tmp > llgrid.inc
   fi
############ prepare global job  #################################

   if [ $DOMAIN -eq 0 ] ; then

      rm allprep_rrtm.f
      ln -s allprep_rrtm.org.f allprep_rrtm.f

      if [ $TYPE_GFSINIT == "FNL" ] ; then
        day=`date -d "${DATE}" +%Y%m%d`
        if [ -e $FNL/fnl_${YEAR}${MM}${DD}_${HOUR}_00.grib2 ] ; then
           cnvgrib -g21 $FNL/fnl_${YEAR}${MM}${DD}_${HOUR}_00.grib2 $OUTPUT/gfs.t${HOUR}z.pgrbf00 #2006
        else
           cp $FNL/fnl_${YEAR}${MM}${DD}_${HOUR}_00.grib1 $OUTPUT/gfs.t${HOUR}z.pgrbf00
        fi
      elif [ $TYPE_GFSINIT == "GFS" ] ; then
	day_start=`date -d "${DATE}" +%Y%m%d`
        echo "Converting wafs.00.0P5DEG from grib2 to grib1"
        cnvgrib -g21 $GFS/$day_start${HOUR}/gfs.t${HOUR}z.pgrb2.0p50.f000 $OUTPUT/gfs.t${HOUR}z.pgrbf00
      elif [ $TYPE_GFSINIT == "ERA" ] ; then
        echo "ERA files"
        ln -s $ERA/${YEAR}_ERAINT2D_0-360.grib $OUTPUT/ERAINT2D_${YEAR}.grb
        ln -s $ERA/${YEAR}_ERAINT3D_0-360.grib $OUTPUT/ERAINT3D_${YEAR}.grb
        ln -s $ERA/content.txt.${YEAR} $VRB/content.txt.${YEAR}
        ln -s $ERA/content2.txt.${YEAR} $VRB/content2.txt.${YEAR}
      fi

      let UPGLOB=$HOUR+3

      UPGLOB=`printf "%.2d" $UPGLOB`

      if [ $TYPE_GFSINIT == "FNL" ] || [ $TYPE_GFSINIT == "GFS" ]; then
      sed -e  "s/NN/$HOUR/" \
          -e  "s/EE/$NHOURS/" \
          -e  "s/UP/$BOCOS/ "  prep_rrtm.tmp > prep_rrtm
      elif [ $TYPE_GFSINIT == "ERA" ]; then
      sed -e  "s/NN/$HOUR/" \
          -e  "s/EE/$NHOURS/" \
          -e  "s/UP/$BOCOS/" \
          -e  "s/YEAR/$YEAR/" \
          -e  "s/MONTH/$MM/" \
          -e  "s/DAY/$DD/" \
          -e  "s?ERA_INT?$ERA?" prep_rrtm_ERA.tmp > prep_rrtm
      fi

      if [ $SRC -lt 7 ]; then 
         sed -e  "s/INTERP/$INTERP/" compile_read_paul_source_mdb2_2018.tmp > compile_read_paul_source_mdb2_2018 
      else
         sed -e  "s/INTERP/$INTERP/" \
             -e  "s/SRC/$SRC/" compile_read_source_foo_landuse.tmp > compile_read_source_foo_landuse
      fi
      sed -e  "s/INTERP/$INTERP/" \
          -e  "s/CLIM/$CLIM/" runfbare_modis.tmp > runfbare_modis

      sed -e  "s/CLIM/$CLIM/" runvegfra_new.tmp > runvegfra_new

      sed -e  "s/INTERP/$INTERP2/" compile_read_roughness_prigent.tmp > compile_read_roughness_prigent

      chmod 755 prep_rrtm
      chmod 755 compile_read_paul_source_mdb2_2018
      chmod 755 compile_read_source_foo_landuse
      chmod 755 compile_read_roughness_prigent 
      chmod 755 runfbare_modis
      chmod 755 runvegfra_new

      ############ prepare model grid setup files global ###############

      rm modelgrid.inc
      rm lmimjm.inc
      sed -e  "s/TLMD/$TLM0D1/" \
          -e  "s/TPHD/$TPH0D1/" \
          -e  "s/WBDN/$WBD1/" \
          -e  "s/SBDN/$SBD1/" \
          -e  "s/DLMN/$DLMD1/" \
          -e  "s/DPHN/$DPHD1/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP1/" \
          -e  "s/KKK/$LM/"  modelgrid_rrtm.tmp > modelgrid.inc
      sed -e  "s/TLMD/$TLM0D1/" \
          -e  "s/TPHD/$TPH0D1/" \
          -e  "s/WBDN/$WBD1/" \
          -e  "s/SBDN/$SBD1/" \
          -e  "s/DLMN/$DLMD1/" \
          -e  "s/DPHN/$DPHD1/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP1/" \
          -e  "s/KKK/$LM/"  lmimjm_rrtm.tmp > lmimjm.inc

      ############ Running Variable Global Script  ###########################

     if $(sbatch -V &> /dev/null) ; then
          echo "Scheduler is SLURM"
          ssh login4 "$VRB/prep_rrtm $VRB"
     elif $(bsub -V &> /dev/null) ; then 
          echo "Scheduler is LSF"
          $VRB/prep_rrtm $VRB
     else
           echo "Scheduler not identified"
      fi

   ############ prepare regional job  ###############################

   elif [ $DOMAIN -eq 1 ] ; then

     if [ $MODE == "REGIONAL_CHEM" ]; then
        # prepare llgrid_chem_....inc and allprep_rrtm.f here for regional_chem run
        if [ $TYPE_BCCHEM == "MOZART" ] ; then
           rm llgrid_chem_mozart.inc
           let NHS=$NHOURS #-$SPINUP
           sed -e  "s/LLL/$NHS/" \
               -e  "s/UPBD/$BOCO/" llgrid_chem_mozart.tmp > llgrid_chem_mozart.inc
           rm allprep_rrtm.f
           ln -s allprep_MOZART.f allprep_rrtm.f
        elif [ $TYPE_BCCHEM == "CAMS" ] ; then
           rm llgrid_chem_cams.inc
           let NHS=$NHOURS #-$SPINUP
           sed -e  "s/LLL/$NHS/" \
               -e  "s/UPBD/$BOCO/" llgrid_chem_cams.tmp > llgrid_chem_cams.inc
           rm allprep_rrtm.f
           ln -s allprep_CAMS.f allprep_rrtm.f
        fi
     else
        # use the default one with no chemistry boundary conditions
        rm allprep_rrtm.f
        ln -s allprep_rrtm.org.f allprep_rrtm.f
     fi

	if [ $TYPE_GFSINIT == "FNL" ] ; then
	
		for i in $(seq $HOUR $BOCO $NHOURS)
		do
	    		var=`printf "%.2d" $i`
	    		hday=`printf "%.2d" $(($var % 24))`
	    		sum_day=$(($var / 24))
	    		day=`date -d "${DATE} ${sum_day} days" +%Y%m%d`
        if [ -e $FNL/fnl_${YEAR}${MM}${DD}_${HOUR}_00.grib2 ] ; then
           cnvgrib -g21 $FNL/fnl_${YEAR}${MM}${DD}_${HOUR}_00.grib2 $OUTPUT/gfs.t${HOUR}z.pgrbf${var}
        else
           cp $FNL/fnl_${YEAR}${MM}${DD}_${HOUR}_00.grib1 $OUTPUT/gfs.t${HOUR}z.pgrbf${var}
        fi
		done

	elif [ $TYPE_GFSINIT == "GFS" ] ; then

		day_start=`date -d "${DATE}" +%Y%m%d`

		for i in $(seq 00 $BOCO $NHOURS)
		do
	    		var=`printf "%.2d" $i`
			echo "Converting wafs.${var}.0P5DEG from grib2 to grib1"
                        cnvgrib -g21 $GFS/$day_start${HOUR}/gfs.t${HOUR}z.pgrb2.0p50.f0${var} $OUTPUT/gfs.t${HOUR}z.pgrbf${var}
		done
        elif [ $TYPE_GFSINIT == "ERA" ] ; then
           echo "ERA files"
           ln -s $ERA/${YEAR}_ERAINT2D_0-360.grib $OUTPUT/ERAINT2D_${YEAR}.grb
           ln -s $ERA/${YEAR}_ERAINT3D_0-360.grib $OUTPUT/ERAINT3D_${YEAR}.grb
           ln -s $ERA/content.txt.${YEAR} $VRB/content.txt.${YEAR}
           ln -s $ERA/content2.txt.${YEAR} $VRB/content2.txt.${YEAR}
	fi

      if [ $TYPE_GFSINIT == "FNL" ] || [ $TYPE_GFSINIT == "GFS" ]; then
      sed -e  "s/NN/$HOUR/" \
          -e  "s/EE/$NHOURS/" \
          -e  "s/UP/$BOCOS/ "  prep_rrtm.tmp > prep_rrtm
      elif [ $TYPE_GFSINIT == "ERA" ]; then
      sed -e  "s/NN/$HOUR/" \
          -e  "s/EE/$NHOURS/" \
          -e  "s/UP/$BOCOS/" \
          -e  "s/YEAR/$YEAR/" \
          -e  "s/MONTH/$MM/" \
          -e  "s/DAY/$DD/" \
          -e  "s?ERA_INT?$ERA?" prep_rrtm_ERA.tmp > prep_rrtm
      fi

      if [ $SRC -lt 7 ]; then 
         sed -e  "s/INTERP/$INTERP/" compile_read_paul_source_mdb2_2018.tmp > compile_read_paul_source_mdb2_2018
      else 
         sed -e  "s/INTERP/$INTERP/" \
             -e  "s/SRC/$SRC/" compile_read_source_foo_landuse.tmp > compile_read_source_foo_landuse
      fi
      sed -e  "s/INTERP/$INTERP/" \
          -e  "s/CLIM/$CLIM/" runfbare_modis.tmp > runfbare_modis

      sed -e  "s/CLIM/$CLIM/" runvegfra_new.tmp > runvegfra_new

      sed -e  "s/INTERP/$INTERP2/" compile_read_roughness_prigent.tmp > compile_read_roughness_prigent
    
      chmod 755 prep_rrtm
      chmod 755 compile_read_paul_source_mdb2_2018
      chmod 755 compile_read_source_foo_landuse
      chmod 755 compile_read_roughness_prigent
      chmod 755 runfbare_modis
      chmod 755 runvegfra_new

      ############ prepare model grid setup files regional #############

      rm modelgrid.inc
      rm lmimjm.inc
      sed -e  "s/TLMD/$TLM0D2/" \
          -e  "s/TPHD/$TPH0D2/" \
          -e  "s/WBDN/$WBD2/" \
          -e  "s/SBDN/$SBD2/" \
          -e  "s/DLMN/$DLMD2/" \
          -e  "s/DPHN/$DPHD2/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP2/" \
          -e  "s/KKK/$LM/"  modelgrid_rrtm.tmp > modelgrid.inc
      sed -e  "s/TLMD/$TLM0D2/" \
          -e  "s/TPHD/$TPH0D2/" \
          -e  "s/WBDN/$WBD2/" \
          -e  "s/SBDN/$SBD2/" \
          -e  "s/DLMN/$DLMD2/" \
          -e  "s/DPHN/$DPHD2/" \
          -e  "s/III/$IMI/" \
          -e  "s/JJJ/$JMI/" \
          -e  "s/IBDY/$IM/" \
          -e  "s/JBDY/$JM/" \
          -e  "s/PTOP/$PTOP2/" \
          -e  "s/KKK/$LM/"  lmimjm_rrtm.tmp > lmimjm.inc

      ############ Running Variable Regional Script  ###########################

     if $(sbatch -V &> /dev/null) ; then
          echo "Scheduler is SLURM"
          ssh login4 "$VRB/prep_rrtm $VRB"
     elif $(bsub -V &> /dev/null) ; then 
          echo "Scheduler is LSF"
          $VRB/prep_rrtm $VRB
     else
           echo "Scheduler not identified"
     fi
      
   fi

   #We need for postprocess
   cp lmimjm.inc $FOLDER_OUTPUT_CASE/

   echo "Vrbl process finished"
fi

#-----------------------------------------------------------------
#   UMO model run
#-----------------------------------------------------------------

if [ $DO_UMO -eq 1 ] ; then

   echo "Enter UMO model run"

   cd $UMO_OUT

   rm isop.dat
   rm lai*.dat
   rm meteo-data.dat
   rm pftp_*.dat
   rm *.nc

   if [ $COUPLE_DUST_INIT -eq 1 ] ; then

   if [ $MODE == "GLOBAL_AEROSOLS" ] ; then
   #megan input files
   cp $PREMEGAN/out/isop.dat .
   cp $PREMEGAN/out/lai*.dat .
   cp $PREMEGAN/out/meteo-data.dat .
   cp $PREMEGAN/out/pftp_*.dat .

   #offline o3,oh,ho2 and phot(dz) climatologies from Badia et al. (2017)GMD
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_03h.dat" ./nmmb_O3_03h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_06h.dat" ./nmmb_O3_06h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_09h.dat" ./nmmb_O3_09h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_12h.dat" ./nmmb_O3_12h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_15h.dat" ./nmmb_O3_15h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_18h.dat" ./nmmb_O3_18h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_21h.dat" ./nmmb_O3_21h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_O3_$MM"_24h.dat" ./nmmb_O3_24h.dat

   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_03h.dat" ./nmmb_OH_03h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_06h.dat" ./nmmb_OH_06h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_09h.dat" ./nmmb_OH_09h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_12h.dat" ./nmmb_OH_12h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_15h.dat" ./nmmb_OH_15h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_18h.dat" ./nmmb_OH_18h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_21h.dat" ./nmmb_OH_21h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_OH_$MM"_24h.dat" ./nmmb_OH_24h.dat

   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_03h.dat" ./nmmb_HO2_03h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_06h.dat" ./nmmb_HO2_06h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_09h.dat" ./nmmb_HO2_09h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_12h.dat" ./nmmb_HO2_12h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_15h.dat" ./nmmb_HO2_15h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_18h.dat" ./nmmb_HO2_18h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_21h.dat" ./nmmb_HO2_21h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_HO2_$MM"_24h.dat" ./nmmb_HO2_24h.dat

   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_03h.dat" ./nmmb_phot_03h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_06h.dat" ./nmmb_phot_06h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_09h.dat" ./nmmb_phot_09h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_12h.dat" ./nmmb_phot_12h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_15h.dat" ./nmmb_phot_15h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_18h.dat" ./nmmb_phot_18h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_21h.dat" ./nmmb_phot_21h.dat
   cp -vf $CHEMIC/gas-climatology/nmmb_phot_$MM"_24h.dat" ./nmmb_phot_24h.dat

   # hermesv3 anthropogenic emissions
   cd $HERMES/OUT
   cp HERMESv3_glob_$YEAR$MM$DD".nc" $UMO_OUT/hermesv3_nmmb_global.nc
   # gfasv1.2 biomass burning emissions
   if [ $FCST == ".true." ]; then
     cp GFAS_glob_$DATE_YESTERDAY".nc" $UMO_OUT/gfasv1.2_nmmb_global.nc
   else
     cp GFAS_glob_$YEAR$MM$DD".nc" $UMO_OUT/gfasv1.2_nmmb_global.nc
   fi
#   rm -f GFAS_$YEAR$MM$DD".nc"
   cd $UMO_OUT

   #mozart dms emissions - TEMPORAL WILL BE REPLACED WITH HERMESv3
   rm dms.nc
   cp -vf $CHEMIC/dms_mozart/dms2000$MM".nc" dms.nc
   fi

   if [ $MODE == "REGIONAL_CHEM" ] ; then
      # hermesv3 anthropogenic emissions
      cp $HERMES/OUT/HERMESv3_reg_$YEAR$MM$DD".nc" $UMO_OUT/emiss_reg.nc #hermesv3_nmmb_regional.nc

      # gfasv1.2 biomass burning emissions
      if [ $FCST == ".true." ]; then
        cp $HERMES/OUT/GFAS_reg_$DATE_YESTERDAY".nc" $UMO_OUT/gfasv1.2_nmmb_regional.nc
      else
        cp $HERMES/OUT/GFAS_reg_$YEAR$MM$DD".nc" $UMO_OUT/gfasv1.2_nmmb_regional.nc
      fi
      cd $UMO_OUT

      #mozart dms emissions - TEMPORAL WILL BE REPLACED WITH HERMESv3
      cd $CHEMIC/dms_mozart/calc
      rm ../output/emiss_dms.nc
      ./remap_mozart.sh ../org/dms2000$MM.nc # dms mozart global
      cp ../output/emiss_dms.nc $OUTPUT/.
   fi

   fi

   rm PET*txt
   rm PET*File
   rm boco.*
   rm boco_chem.*
   rm chemic-reg
   rm nmm_b_history.*
   rm main_input_filename
   rm main_input_filename2
   rm GWD.bin
   rm configure_file
   rm tr*
   rm co2_trans
   rm ETAMPNEW_AERO
   rm ETAMPNEW_DATA
   rm RRT*
   rm *.TBL

   # Clean previous runs
   rm fcstdone.* restartdone.*
   rm nmmb_rst_* nmmb_hst_*

   cd $UMO_ROOT

   #-----------------------------------------------------------------
   #   Dust coupling part 1
   #-----------------------------------------------------------------

   if [ $DATE -eq $START ] ; then
       COUPLE_DUST=0
   else
       COUPLE_DUST=$COUPLE_TMP
   fi

   if [ $COUPLE_DUST_INIT -eq 1 ] ; then
       COUPLE_DUST=$COUPLE_DUST_INIT
   fi

   echo "couple_dust" $COUPLE_DUST
   echo "domain" $DOMAIN

   if [ $COUPLE_DUST -eq 1 ] ; then
      DUST_FLAG="EEEE/true"
      LCHEMON="LCHEMON/true"
   else
      DUST_FLAG="EEEE/false"
      LCHEMON="LCHEMON/false"
   fi     

   #Cheking if configfile_rrtm_chem.$MODE.tmp is created
   if [ ! -f configfile_rrtm_chem.$MODE.tmp ];
   then
   	echo "File configfile_rrtm_chem.$MODE.tmp does not exist."
	echo "Model will not run."
	echo "Aborting. Create a configfile_rrtm_chem.$MODE.tmp file"
	exit
   fi
 
   if [ $DOMAIN -eq 0 ] ; then
        sed -e  "s/III/$IMI/" \
            -e  "s/JJJ/$JMI/" \
            -e  "s/KKK/$LM/" \
            -e  "s/TPHD/$TPH0D1/" \
            -e  "s/TLMD/$TLM0D1/" \
            -e  "s/WBD/$WBD1/" \
            -e  "s/SBD/$SBD1/" \
            -e  "s/INPES/$INPES/" \
            -e  "s/JNPES/$JNPES/" \
            -e  "s/WRTSK/$WRTSK/" \
            -e  "s/DTINT/$DT_INT1/" \
            -e  "s/YYYY/$YEAR/" \
            -e  "s/MM/$MM/" \
            -e  "s/DD/$DD/" \
            -e  "s/HH/$HOUR/" \
            -e  "s/LLL/$NHOURS/" \
            -e  "s/STT/$HIST_M/" \
            -e  "s/DOM/true/" \
            -e  "s/$DUST_FLAG/" \
            -e  "s/BBBB/$DCAL1/" \
            -e  "s/PERT1/$DCALBIN1/" \
            -e  "s/PERT2/$DCALBIN2/" \
            -e  "s/PERT3/$DCALBIN3/" \
            -e  "s/PERT4/$DCALBIN4/" \
            -e  "s/PERT5/$DCALBIN5/" \
            -e  "s/PERT6/$DCALBIN6/" \
            -e  "s/PERT7/$DCALBIN7/" \
            -e  "s/PERT8/$DCALBIN8/" \
            -e  "s/PEUS1/$PERTURB_USTH/" \
            -e  "s/SFFF/$SPINUP/" \
            -e  "s/FFFF/$FECAN/" \
            -e  "s/SMCCOEF/$SMCCOEF/" \
            -e  "s/CTHR/$CTHR/" \
            -e  "s/BMPC/$BCL_MP_C/" \
            -e  "s/BMPM/$BCL_MP_M/" \
            -e  "s/BMPF/$BCL_MP_F/" \
            -e  "s/BBMJC/$BCL_BMJ_C/" \
            -e  "s/BBMJM/$BCL_BMJ_M/" \
            -e  "s/BBMJF/$BCL_BMJ_F/" \
            -e  "s/IMP/$ICL_MP/" \
            -e  "s/IBMJ/$ICL_BMJ/" \
            -e  "s/SEDIM_TYPE/$SEDIM_TYPE/" \
            -e  "s/VSED5/$VSED5/" \
            -e  "s/VSED6/$VSED6/" \
            -e  "s/VSED7/$VSED7/" \
            -e  "s/VSED8/$VSED8/" \
            -e  "s/VDRY/$VDRY/" \
            -e  "s/SRC/$SRC/" \
            -e  "s/SZDSTR/$SZDSTR/" \
            -e  "s/DUSCHEME/$DUSCHEME/" \
            -e  "s/VSLAYER/$VSLAYER/" \
            -e  "s/SMCCOR/$SMCCOR/" \
            -e  "s/DRAG_PART/$DRAG_PART/" \
            -e  "s/USTDP/$USTDP/" \
            -e  "s/DMASK/$DMASK/" \
            -e  "s/VEGF_UNI/$VEGF_UNI/" \
            -e  "s/MOIST_COR_01/$MOIST_COR_01/" \
            -e  "s/VEG_COR_01/$VEG_COR_01/" \
            -e  "s/$LCHEMON/" \
            -e  "s/NCHEM/$NCHEM/" \
            -e  "s/NDUST/$NDUST/" \
            -e  "s/XMDL/$XMDL/" \
            -e  "s/XMLC/$XMLC/" \
            -e  "s/XTBL/$XTBL/" \
            -e  "s/NRADS/$NRADS1/" \
            -e  "s/NRADL/$NRADL1/" \
            -e  "s/CCCC/$INIT_CHEM/" configfile_rrtm_chem.$MODE.tmp > configfile_rrtm_chem.$MODE
    elif [ $DOMAIN -eq 1 ] ; then
        sed -e  "s/III/$IMI/" \
            -e  "s/JJJ/$JMI/" \
            -e  "s/KKK/$LM/" \
            -e  "s/TPHD/$TPH0D2/" \
            -e  "s/TLMD/$TLM0D2/" \
            -e  "s/WBD/$WBD2/" \
            -e  "s/SBD/$SBD2/" \
            -e  "s/INPES/$INPES/" \
            -e  "s/JNPES/$JNPES/" \
            -e  "s/WRTSK/$WRTSK/" \
            -e  "s/DTINT/$DT_INT2/" \
            -e  "s/YYYY/$YEAR/" \
            -e  "s/MM/$MM/" \
            -e  "s/DD/$DD/" \
            -e  "s/HH/$HOUR/" \
            -e  "s/LLL/$NHOURS/" \
            -e  "s/STT/$HIST_M/" \
            -e  "s/DOM/false/" \
            -e  "s/$DUST_FLAG/" \
            -e  "s/BBBB/$DCAL2/" \
            -e  "s/PERT1/$DCALBIN1/" \
            -e  "s/PERT2/$DCALBIN2/" \
            -e  "s/PERT3/$DCALBIN3/" \
            -e  "s/PERT4/$DCALBIN4/" \
            -e  "s/PERT5/$DCALBIN5/" \
            -e  "s/PERT6/$DCALBIN6/" \
            -e  "s/PERT7/$DCALBIN7/" \
            -e  "s/PERT8/$DCALBIN8/" \
            -e  "s/PEUS1/$PERTURB_USTH/" \
            -e  "s/SFFF/$SPINUP/" \
            -e  "s/FFFF/$FECAN/" \
            -e  "s/SMCCOEF/$SMCCOEF/" \
            -e  "s/CTHR/$CTHR/" \
            -e  "s/BMPC/$BCL_MP_C/" \
            -e  "s/BMPM/$BCL_MP_M/" \
            -e  "s/BMPF/$BCL_MP_F/" \
            -e  "s/BBMJC/$BCL_BMJ_C/" \
            -e  "s/BBMJM/$BCL_BMJ_M/" \
            -e  "s/BBMJF/$BCL_BMJ_F/" \
            -e  "s/IMP/$ICL_MP/" \
            -e  "s/IBMJ/$ICL_BMJ/" \
            -e  "s/SEDIM_TYPE/$SEDIM_TYPE/" \
            -e  "s/VSED5/$VSED5/" \
            -e  "s/VSED6/$VSED6/" \
            -e  "s/VSED7/$VSED7/" \
            -e  "s/VSED8/$VSED8/" \
            -e  "s/VDRY/$VDRY/" \
            -e  "s/SRC/$SRC/" \
            -e  "s/SZDSTR/$SZDSTR/" \
            -e  "s/DUSCHEME/$DUSCHEME/" \
            -e  "s/VSLAYER/$VSLAYER/" \
            -e  "s/SMCCOR/$SMCCOR/" \
            -e  "s/DRAG_PART/$DRAG_PART/" \
            -e  "s/USTDP/$USTDP/" \
            -e  "s/DMASK/$DMASK/" \
            -e  "s/VEGF_UNI/$VEGF_UNI/" \
            -e  "s/MOIST_COR_01/$MOIST_COR_01/" \
            -e  "s/VEG_COR_01/$VEG_COR_01/" \
            -e  "s/$LCHEMON/" \
            -e  "s/NCHEM/$NCHEM/" \
            -e  "s/NDUST/$NDUST/" \
            -e  "s/XMDL/$XMDL/" \
            -e  "s/XMLC/$XMLC/" \
            -e  "s/XTBL/$XTBL/" \
            -e  "s/NRADS/$NRADS2/" \
            -e  "s/NRADL/$NRADL2/" \
            -e  "s/CCCC/$INIT_CHEM/" configfile_rrtm_chem.$MODE.tmp > configfile_rrtm_chem.$MODE
   fi
   
   echo ""
   echo "Executing NMMB.x NMMB-MONARCH model"
   echo ""

   cd $UMO_OUT 

   echo $OUTPUT
   cp $OUTPUT/boco.* .
   cp $OUTPUT/boco_chem.* .
   cp $OUTPUT/chemic-reg .
   cp $OUTPUT/fcst input_domain_01 #main_input_filename
   cp $OUTPUT/soildust main_input_filename2
   cp $OUTPUT/GWD.bin .
   mv $UMO_ROOT/configfile_rrtm_chem.$MODE configure_file

   cp $DATMOD/* .
   cp $DATMOD/nam_micro_lookup.dat ETAMPNEW_DATA
   cp $DATMOD/wrftables/* .
   cp $DATMOD/co2data/* .

   # for aerosols scavenging coeff
   cp $OUTPUT/lookup_aerosol2.dat.rh00 ETAMPNEW_AERO_RH00
   cp $OUTPUT/lookup_aerosol2.dat.rh50 ETAMPNEW_AERO_RH50
   cp $OUTPUT/lookup_aerosol2.dat.rh70 ETAMPNEW_AERO_RH70
   cp $OUTPUT/lookup_aerosol2.dat.rh80 ETAMPNEW_AERO_RH80
   cp $OUTPUT/lookup_aerosol2.dat.rh90 ETAMPNEW_AERO_RH90
   cp $OUTPUT/lookup_aerosol2.dat.rh95 ETAMPNEW_AERO_RH95
   cp $OUTPUT/lookup_aerosol2.dat.rh99 ETAMPNEW_AERO_RH99

   # for rrtm radiation
   cp $DATMOD/fix/fix_rad/global_climaeropac_global.txt aerosol.dat
   cp $DATMOD/fix/fix_rad/co2historicaldata* .
   cp $DATMOD/fix/fix_rad/solarconstantdata.txt .
   cp $DATMOD/fix/fix_rad/volcanic_aerosols_* .

   # for gocart climatology conc. and opt. properties
   cp $DATMOD/fix/fix_gocart_clim/2000* .
   cp $DATMOD/fix/fix_aeropt_luts/NCEP_AEROSOL.bin .

   # for aerosol-radiation-interaction bsc opt. properties
   if [ $XKOK -eq 0 ] ; then
     ln -s ${DATMOD}/fix/fix_aeropt_bsc/tables2018/mnch_tables_opc.txt
     ln -s ${DATMOD}/fix/fix_aeropt_bsc/tables2018/mnch_tables_clq.txt
   else
     ln -s ${DATMOD}/fix/fix_aeropt_bsc/tables2018/mnch_tables_opc_kok.txt mnch_tables_opc.txt
     ln -s ${DATMOD}/fix/fix_aeropt_bsc/tables2018/mnch_tables_clq_kok.txt mnch_tables_clq.txt
   fi
   ln -s ${DATMOD}/fix/fix_aeropt_bsc/tables2018/mnch_tables_ext.txt
   ln -s ${DATMOD}/fix/fix_aeropt_bsc/tables2018/mnch_tables_int.txt

   cp configure_file configure_file_01
   cp configure_file model_configure

   ln -sf $DATMOD/global_o3prdlos.f77 fort.28
   ln -sf $DATMOD/global_o3clim.txt fort.48

   cp $SRCDIR/NAMELISTS/solver_state.txt .
   cp $SRCDIR/NAMELISTS/nests.txt .
   cp $SRCDIR/NAMELISTS/filt_vars.txt .
#  cp $SRCDIR/NAMELISTS/ts_locations.nml

   # Coupling previous day
   cp $FOLDER_OUTPUT_CASE/history_INIT.hhh .

   date
   time mpirun -np $PROC $SRCDIR/exe/NMMB.x  >& nmm_rrtm.out
   date

   echo ""
   echo "Finished Executing NMMB.x NMMB-MONARCH model"
   echo ""

   #-----------------------------------------------------------------
   # Dust coupling part 2
   #-----------------------------------------------------------------

   rm $UMO_OUT/history_INIT.hhh

   mkdir -p $FOLDER_OUTPUT

   if [ $COUPLE_TMP -eq 1 ] ; then
      if [ $NHOURS_INIT -lt 100 ] ; then
         if [ $NHOURS_INIT -lt 10 ] ; then
            cp $UMO_OUT/nmmb_hst_01_bin_000${NHOURS_INIT}h_00m_00.00s $FOLDER_OUTPUT_CASE/history_INIT.hhh 
         else
            cp $UMO_OUT/nmmb_hst_01_bin_00${NHOURS_INIT}h_00m_00.00s $FOLDER_OUTPUT_CASE/history_INIT.hhh
         fi
      else
         cp $UMO_OUT/nmmb_hst_01_bin_0${NHOURS_INIT}h_00m_00.00s $FOLDER_OUTPUT_CASE/history_INIT.hhh
      fi
   fi

   mv $UMO_OUT/nmmb_hst_01_bin_* $FOLDER_OUTPUT/.
   mv $UMO_OUT/nmm_rrtm.out $FOLDER_OUTPUT/.
   mv $UMO_OUT/configure_file $FOLDER_OUTPUT/.
   mv $OUTPUT/boundary_ecmwf.nc $FOLDER_OUTPUT/.

fi
#-----------------------------------------------------------------
#   PREMEGAN section
#-----------------------------------------------------------------

if [ $DO_PREMEGAN -eq 1 ] ; then

  if [ $MODE == "GLOBAL_AEROSOLS" ] || [ $MODE == "REGIONAL_CHEM" ] ; then

  if [ ! -d $UMO_PATH/OUTPUT/$CASE/megan ]; then
    cd $UMO_PATH/OUTPUT/$CASE
    cp -r $UMO_PATH/PREPROC/EMISSIONS/megan .
    cd $UMO_PATH/OUTPUT/$CASE/megan
    ln -s $DATAMEGAN DATA
    cd $UMO_PATH/OUTPUT/$CASE/megan/bin
    ln -s ../src/prepmegan-meteo.x prepmegan-meteo.x
    ln -s ../src/prepmegan.x prepmegan.x
  fi

  HST_FILE_MEGAN="MONARCH_d01_"$DATE".nc"
  PATH_MEGAN=$FOLDER_OUTPUT
  cd $PREMEGAN/src

  if [ $SPINUP == 0 ] ; then
     SPINUPMEG=$HIST
     NHOURSMEG=$NHOURS
  else
     SPINUPMEG=$SPINUP
     NHOURSMEG=$NHOURS
  fi

  if [ $DOMAIN -eq 0 ] ; then #global  

    sed -e "s|_IN_IMI_|$IMI|" \
        -e "s|_IN_JMI_|$JMI|" \
        -e "s|_TLM0D_|$TLM0D1|" \
        -e "s|_TPH0D_|$TPH0D1|" \
        -e "s|_WBD_|$WBD1|" \
        -e "s|_SBD_|$SBD1|" \
        -e "s|_DLMD_|$DLMD1|" \
        -e "s|_DPHD_|$DPHD1|" \
        -e "s|_GLOBAL_|.true.|" \
        -e "s|_SPINUP_|$SPINUPMEG|" \
        -e "s|_NHOURS_|$NHOURSMEG|" \
        -e "s|_IN_FREQ_|$HIST|" \
        -e "s|_HST_FILE_NAME_|$HST_FILE_MEGAN|" \
        -e "s|_IN_PATH_|$PATH_MEGAN|" namelist.input.tmp > namelist.input

  elif [ $DOMAIN -eq 1 ] ; then #regional

    sed -e "s|_IN_IMI_|$IMI|" \
        -e "s|_IN_JMI_|$JMI|" \
        -e "s|_TLM0D_|$TLM0D2|" \
        -e "s|_TPH0D_|$TPH0D2|" \
        -e "s|_WBD_|$WBD2|" \
        -e "s|_SBD_|$SBD2|" \
        -e "s|_DLMD_|$DLMD2|" \
        -e "s|_DPHD_|$DPHD2|" \
        -e "s|_GLOBAL_|.false.|" \
        -e "s|_SPINUP_|$SPINUPMEG|" \
        -e "s|_NHOURS_|$NHOURSMEG|" \
        -e "s|_IN_FREQ_|$HIST|" \
        -e "s|_HST_FILE_NAME_|$HST_FILE_MEGAN|" \
        -e "s|_IN_PATH_|$PATH_MEGAN|" namelist.input.tmp > namelist.input

  fi

    cd $PREMEGAN/bin
    rm -f namelist.input
    ln -s ../src/namelist.input namelist.input

    if [ ! -e $UMO_PATH/OUTPUT/$CASE/megan/out/lai01.dat ]; then
       ./prepmegan.x
    fi

    ./prepmegan-meteo.x

    cp ../out/meteo-data.dat $PREMEGAN/out/.
    cp ../out/meteo-data.nc $FOLDER_OUTPUT/.

    fi

fi

#-----------------------------------------------------------------
# End processing - prepare for next day
#-----------------------------------------------------------------

echo "DATE: " $DATE "simulation finished"

#Getting next simulation day
YEAR=`date -ud "$DATE + next day" +%Y`
YY=`date -ud "$DATE + next day" +%y`
MM=`date -ud "$DATE + next day" +%m`
DD=`date -ud "$DATE + next day" +%d`
MTH=`date -ud "$DATE" +%b`
DATE=$YEAR$MM$DD
DATENEW=$YY$MM$DD$HOUR
DATENEW2=$YY$MM$DD
DATENEW3=$MM$DD

# Select IC of chemistry for next day run 
INIT_CHEM=1                  # 0. IC from ideal conditions 
                             # 1 initialize from a previous run
                             # 2 initialize from inca chem data
                             # 3 initialize from global nmmb-ctm run for regional
                             # 4 initialize from a previous run of different resolution

done

exit
