#!/bin/bash # shell type
shopt -s extglob # enable extended globbing

#===========================================================================
# Execute this script: batch_dlnq_calculation.sh
#===========================================================================

Directory="/projects/rsmas/bsoden/hhe/PDRMIP/Analysis"

Mode=( 'Coupled' 'Fsst' )
mode=( 'coupled' 'fsst' )
Type=( 'SFC' 'TOA' )
Model=( 'CanESM2' 'ECHAM-HAM' 'ECHAM-HAM' 'GISS-E2-R' 'HadGEM2' 'HadGEM3' 'IPSL-CM5A' 'MIROC-SPRINTARS' 'MPI-ESM' 'NCAR-CESM1-CAM4' 'NCAR-CESM1-CAM5' 'NorESM1' )
Modeler=( 'CanESM2' 'ECHAM-HAM_Leipzig' 'ECHAM-HAM_Oxford' 'GISS-E2-R' 'HadGEM2' 'HadGEM3' 'IPSL-CM5A' 'MIROC-SPRINTARS' 'MPI-ESM' 'NCAR-CESM1-CAM4' 'NCAR-CESM1-CAM5' 'NorESM1' )
exp=( 'co2x2' 'ch4x3' 'solar' 'bcx10' 'sulx5' )

for Mode_id in {0..1}; do # Loop over 2 modes
  echo "${Mode[Mode_id]}"
  for Type_id in {1..1}; do # Loop over 2 Types
    echo "${Type[Type_id]}"
    if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt" ]; then
      mkdir $Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt
    fi
    
    for Model_id in {0..11}; do # Loop over 12 models
      echo "${Model[Model_id]}"
      if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt/${Modeler[Model_id]}" ]; then
        mkdir $Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt/${Modeler[Model_id]}
      fi
      rm $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_base_${mode[Mode_id]}.nc
      cdo ln $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/hus_${Model[Model_id]}_base_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_base_${mode[Mode_id]}.nc

      for exp_id in {0..4}; do # Loop over 5 experiments
        echo "${exp[exp_id]}"

        if [ ! -f "$Directory/${Mode[Mode_id]}/base_fields/${Modeler[Model_id]}/ta_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc" ]; then
          echo "ta_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc"
        else
          #read lines from .ncl file
          #write these lines into temporary .ncl file
          cat "$Directory/dlnq_calculation.ncl" >> TEMP.ncl

          #replace MODELER with ${Modeler[Model_id]}
          sed -i "s#MODELER#${Modeler[Model_id]}#g" TEMP.ncl

          #replace MODEL with ${Model[Model_id]}
          sed -i "s#MODEL#${Model[Model_id]}#g" TEMP.ncl

          #replace TYPE with ${Type[Type_id]}
          sed -i "s#TYPE#${Type[Type_id]}#g" TEMP.ncl

          #replace EXPERIMENT with ${exp[exp_id]}
          sed -i "s#EXPERIMENT#${exp[exp_id]}#g" TEMP.ncl

          #replace MODE with ${Mode[Mode_id]}
          sed -i "s#MODE#${Mode[Mode_id]}#g" TEMP.ncl

          #replace mode with ${mode[Mode_id]}
          sed -i "s#mode#${mode[Mode_id]}#g" TEMP.ncl

          ncl TEMP.ncl
          rm TEMP.ncl

          cdo ln $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/hus_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
          cdo ymonsub, -selname,hus $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc -ymonmean, -selname,hus $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_base_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt/${Modeler[Model_id]}/dlnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}_temp.nc
          rm $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
          
          cdo selname,dlnq, -setattribute,dlnq@standard_name="logarithmic_specific_humidity_change",dlnq@long_name="Logarithmic specific humidity change",dlnq@units="1", -chname,hus,dlnq $Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt/${Modeler[Model_id]}/dlnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}_temp.nc $Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt/${Modeler[Model_id]}/dlnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
          rm $Directory/${Mode[Mode_id]}/Kernels/${Type[Type_id]}/results_dlnqdt/${Modeler[Model_id]}/dlnq_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}_temp.nc
        fi
      done
      rm $Directory/${Mode[Mode_id]}/mon/${Modeler[Model_id]}/lnq_${Model[Model_id]}_base_${mode[Mode_id]}.nc
    done
  done
done

