#!/bin/bash



#module load ecmwfapi
module load conda/22.11.1-2
conda activate condaCerise
export LD_LIBRARY_PATH=/perm/cyce/conda/envs/condaCerise/lib

set -v


#workdir=$1
#model=$2
#  freq=$3
freq="mon"
workdir=$SCRATCH/cerise/data/


for model in ecmwf cmcc meteo_france dwd ukmo; do
#model="ecmwf" #cmcc, meteo_france, dwd, cmcc, ukmo 

  case $model in 

    "ecmwf") origin="ecmf" ; system="51";;
    "meteo_france") origin="lfpw" ; system="8";;
    "dwd") origin="edzw" ; system="21";;
    "cmcc") origin="cmcc" ; system="35" ;;
    "ukmo") origin="egrr" ; system="602" ;;
  esac


  if [[ $freq == "mon" ]]; then
    freq_dir="monthly_mean"
    suffix="_f6h"
  else
    freq_dir="6hourly"
    suffix=""
  fi

  mkdir -p $workdir/esarchive/exp/$model/system$system/$freq_dir
  cd $workdir/esarchive/exp/$model/system$system/$freq_dir

  for year in $( seq 1979 2023); do
    for mon in $( seq -w 1 12); do

      if [[ $freq == "mon" ]]; then

        for var in sfcWind tas tos psl prlr tasmin tasmax tdps; do
          case $var in
            "sfcWind") code="207.128";;
            "tas") code="167.128";;
            "tos") code="34.128";;
            "psl") code="151.128";;
            "prlr") code="228.172";;
            "tasmin") code="52.128";;
            "tasmax") code="51.128";;
            "tdps") code="168.128";;
          esac

	  mkdir -p ${var}${suffix}

	  if [[ ! -f ${var}${suffix}/${var}_${year}${mon}01.grb ]] ; then
mars << EOF
retrieve,
class=c3,
date=$year-$mon-01,
expver=1,
fcmonth=1/2/3/4/5/6,
levtype=sfc,
method=1,
number=0/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50,
origin=$origin,
param=$code,
stream=msmm,
system=$system,
time=00:00:00,
type=fcmean,
expect=any,
target='${var}${suffix}/${var}_${year}${mon}01.grb'

EOF
            cdo shifttime,-15days ${var}${suffix}/${var}_${year}${mon}01.grb ${var}${suffix}/${var}_${year}${mon}01.grb2
            mv ${var}${suffix}/${var}_${year}${mon}01.grb2 ${var}${suffix}/${var}_${year}${mon}01.grb
          fi #file exists
        done #var
      else #freq 6h
        for var in sfcWind tas tos psl prlr tasmin tasmax tdps; do
          case $var in
            "sfcWind") code="207.128";;
            "tas") code="167.128";;
            "tos") code="34.128";;
            "psl") code="151.128";;
            "prlr") code="228.172";;
            "tasmin") code="52.128";;
            "tasmax") code="51.128";;
            "tdps") code="168.128";;
          esac

	  mkdir -p ${var}${suffix}
            if [[ ! -f ${var}${suffix}/${var}_${year}${mon}01.grb ]] ; then
mars << EOF
retrieve,
class=c3,
date=$year-$mon-01
expver=1,
step=1/to/5160/by/6,
levtype=sfc,
method=1,
number=0/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50,
origin=$origin,
param=$code,
stream=mmsf,
system=$system,
time=00:00:00,
type=fc,
expect=any,
target="${var}${suffix}/${var}_${year}${mon}01.grb"
EOF

          fi #file exists
	done #var
      fi #freq
    done #year
  done #mon
done #model
