#!/bin/bash



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

set -v


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

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

  mkdir -p $workdir/esarchive/recon/ecmwf/era5/$freq_dir
  cd $workdir/esarchive/recon/ecmwf/era5/$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="10m_wind_speed";;
            "tas") code="2m_temperature";;
            "tos") code="sea_surface_temperature";;
            "psl") code="mean_sea_level_pressure";;
            "prlr") code="total_precipitation";;
            "tdps") code="2m_dewpoint_temperature";;
          esac

	  mkdir -p ${var}${suffix}

	  if [[ ! -f ${var}${suffix}/${var}_${year}${mon}01.grb ]] ; then
python << EOF
import cdsapi

c = cdsapi.Client()

c.retrieve(
    'reanalysis-era5-single-levels-monthly-means',
    {
        'format': 'grib',
        'product_type': 'monthly_averaged_reanalysis',
        'variable': '$code',
        'year': '$year',
        'month': '$mon',
        'time': '00:00',
    },
    '${var}${suffix}/${var}_${year}${mon}.grib')
EOF
          fi #file exists
        done #var
      else #freq 6h
        for var in uas vas tas tos psl prlr tasmin tasmax tdps; do
          case $var in
            "sfcWind") code="10m_wind_speed";;
            "tas") code="2m_temperature";;
            "uas") code="10m_u_component_of_wind";;
            "vas") code="10m_v_component_of_wind";;
            "tos") code="sea_surface_temperature";;
            "psl") code="mean_sea_level_pressure";;
            "prlr") code="total_precipitation";;
            "tdps") code="2m_dewpoint_temperature";;
          esac

	  mkdir -p ${var}${suffix}
            if [[ ! -f ${var}${suffix}/${var}_${year}${mon}01.grb ]] ; then
python << EOF

import cdsapi

c = cdsapi.Client()

c.retrieve(
    'reanalysis-era5-single-levels',
    {
        'product_type': 'reanalysis',
        'format': 'grib',
        'variable': $code,
        'time': [
            '00:00', '01:00', '02:00',
            '03:00', '04:00', '05:00',
            '06:00', '07:00', '08:00',
            '09:00', '10:00', '11:00',
            '12:00', '13:00', '14:00',
            '15:00', '16:00', '17:00',
            '18:00', '19:00', '20:00',
            '21:00', '22:00', '23:00',
        ],
        'day': [
            '01', '02', '03',
            '04', '05', '06',
            '07', '08', '09',
            '10', '11', '12',
            '13', '14', '15',
            '16', '17', '18',
            '19', '20', '21',
            '22', '23', '24',
            '25', '26', '27',
            '28', '29', '30',
        ],
        'year': '$year',
        'month': '$mon',
    },
    '${var}${suffix}/${var}_${year}${mon}.grb')

EOF

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