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

#===========================================================================
# Execute this script: batch_forcing_climate-responses_calculation.sh
#===========================================================================

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

Mode=( 'Coupled' 'Fsst' )
mode=( 'coupled' 'fsst' )
kernel=( 'CloudSat' 'CESM-CAM5' )
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' )
timaxis_model=( '2001-01-15' '2002-01-15' '2003-01-15' '2004-01-15' '2005-01-15' '2006-01-15' '2007-01-15' '2008-01-15' '2009-01-15' '2010-01-15' '2011-01-15' '2012-01-15' )
timaxis_mode=( '2020-01-15' '2015-01-15' )
cloud_masking=( '0.21' '0.19' '0.21' '-0.41' '0.76' )
exp=( 'co2x2' 'ch4x3' 'solar' 'bcx10' 'sulx5' )
var=( 'temperature_response' 'water_vapor_response' 'lapse_rate_water_vapor_response' 'albedo_response' 'instantaneous_radiative_forcing' 'cloud_radiative_response' 'total_radiative_response' )

for Mode_id in {0..1}; do # Loop over 2 modes
  echo "${Mode[Mode_id]}"
  for kernel_id in {0..1}; do # Loop over 2 kernels
    echo "${kernel[kernel_id]}"
    if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results" ]; then
      mkdir $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results
    fi
    if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE" ]; then
      mkdir $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE
    fi
    rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/*.nc
    for Model_id in {0..11}; do # Loop over 12 models
      echo "${Model[Model_id]}"
      if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}" ]; then
        mkdir $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}
      fi
      rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/*.nc
    done
  done
done

for Mode_id in {0..1}; do # Loop over 2 modes
  echo "${Mode[Mode_id]}"
  for kernel_id in {0..1}; do # Loop over 2 kernels
    echo "${kernel[kernel_id]}"
    if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results" ]; then
      mkdir $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results
    fi
    if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE" ]; then
      mkdir $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE
    fi
    for Model_id in {0..11}; do # Loop over 12 models
      echo "${Model[Model_id]}"
      if [ ! -d "$Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}" ]; then
        mkdir $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}
      fi

      if [ "${Model[Model_id]}" = "HadGEM3" ]; then
        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/forcing_climate-responses_calculation_HadGEM3.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 KERNEL with ${kernel[kernel_id]}
            sed -i "s#KERNEL#${kernel[kernel_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

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

            ncl TEMP.ncl
            rm TEMP.ncl

            for var_id in {0..6}; do # Loop over 7 categories of variables
              echo "${var[var_id]}"
              # monthly
              cdo ymonmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo settaxis,${timaxis_model[Model_id]},12:00:00,1mon $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo cat $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc

              # annual mean
              cdo timmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo settime,12:00:00, -setdate,${timaxis_model[Model_id]} $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo cat $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc
              
              rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
            done
          fi
        done
      elif [ "${Model[Model_id]}" = "IPSL-CM5A" ]; then
        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/forcing_climate-responses_calculation_IPSL-CM5A.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 KERNEL with ${kernel[kernel_id]}
            sed -i "s#KERNEL#${kernel[kernel_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

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

            ncl TEMP.ncl
            rm TEMP.ncl

            for var_id in {0..6}; do # Loop over 7 categories of variables
              echo "${var[var_id]}"
              # monthly
              cdo ymonmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo settaxis,${timaxis_model[Model_id]},12:00:00,1mon $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo cat $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc

              # annual mean
              cdo timmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo settime,12:00:00, -setdate,${timaxis_model[Model_id]} $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo cat $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc
              
              rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
            done
          fi
        done
      else
        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/forcing_climate-responses_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 KERNEL with ${kernel[kernel_id]}
            sed -i "s#KERNEL#${kernel[kernel_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

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

            ncl TEMP.ncl
            rm TEMP.ncl

            for var_id in {0..6}; do # Loop over 7 categories of variables
              echo "${var[var_id]}"
              # monthly
              cdo ymonmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo settaxis,${timaxis_model[Model_id]},12:00:00,1mon $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo cat $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc

              # annual mean
              cdo timmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo settime,12:00:00, -setdate,${timaxis_model[Model_id]} $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/${Modeler[Model_id]}/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              cdo cat $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc
              
              rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
              rm $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_${Model[Model_id]}_${exp[exp_id]}_${mode[Mode_id]}.nc
            done
          fi
        done
      fi
    done
  done
done

for Mode_id in {0..1}; do # Loop over 2 modes
  echo "${Mode[Mode_id]}"
  for kernel_id in {0..1}; do # Loop over 2 kernels
    echo "${kernel[kernel_id]}"
    for exp_id in {0..4}; do # Loop over 5 experiments
      echo "${exp[exp_id]}"
      for var_id in {0..6}; do # Loop over 7 categories of variables
        echo "${var[var_id]}"
        # monthly
        cdo ymonmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Monthly_${var[var_id]}_ensemble_${exp[exp_id]}_${mode[Mode_id]}.nc
        # annual mean
        cdo timmean $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_ALL-MODELS_${exp[exp_id]}_${mode[Mode_id]}.nc $Directory/${Mode[Mode_id]}/Kernels/TOA/${kernel[kernel_id]}/results/ENSEMBLE/Annual_${var[var_id]}_ENSEMBLE_${exp[exp_id]}_${mode[Mode_id]}.nc
      done
    done
  done
done
