#!/bin/bash

# SLURM configuration for MN5
# information about the queue : https://www.bsc.es/supportkc/docs/MareNostrum5/slurm

# HOW to SUBMIT 
# sbatch -A, --account={account} -q, --qos={qos} {job_script} [args]

# Single job with a single node (default)
#SBATCH --qos=gp_debug
##SBATCH --qos=gp_bsces
#SBATCH --output=serial_%j.out
#SBATCH --error=serial_%j.err
#SBATCH --ntasks=112
#SBATCH --time=02:00:00
#SBATCH --mail-type=end
#SBATCH --mail-user=amirpasha.mozaffari@bsc.es

# SLURM configuration for NORD3
# more information here : https://earth.bsc.es/wiki/doku.php?id=computing:nord3-v2

# HOW to SUBMIT 
# sbatch -A, --account={account} -q, --qos={qos} {job_script} [args]
# sbatch -A bsc32 -q bsc_es launch_remap_NR3.sh

# Single job with a single node (default)
##SBATCH --qos=bsc_es
##SBATCH --output=serial_%j.out
##SBATCH --error=serial_%j.err
##SBATCH --ntasks=10
##SBATCH --time=00:30:00
##SBATCH --mail-type=end
##SBATCH --mail-user=amirpasha.mozaffari@bsc.es
##SBATCH --constraint=highmem
##SBATCH --exclusive

conda deactivate 

if [ "$BSC_MACHINE" = mn5 ]; then
    module load miniconda
    conda init
    export MODULEPATH=$MODULEPATH:/apps/GPP/EASYBUILD/modules/all
    module load GDAL/3.8.5-foss-2023b
    module load NCO/5.2.4-foss-2023b
    module load hdf5/1.14.1-2
    module load pnetcdf/1.12.3
    module load netcdf/2023-06-14
elif [ "$BSC_MACHINE" = nord3v2 ]; then
    module load GDAL/3.3.2-foss-2019b-Python-3.7.4
    module load pycdo/1.5.3-foss-2019b-Python-3.7.4
    module load pynco/0.0.2-foss-2019b-Python-3.7.4
fi

idir1="/gpfs/projects/bsc32/cerise/ESA/landcovercci_v2.1.1"
idir2="/gpfs/projects/bsc32/cerise/ESA/landcovercci_v2.0.7b"
odir="/gpfs/projects/bsc32/cerise/outputs/bsc032013/0.0089_deg"
tmpdir="/gpfs/projects/bsc32/cerise/outputs/bsc032013/tmp"
grid_target_file="/gpfs/projects/bsc32/cerise/ECMWF_files/CONFESS_LAI_20140105_1km.nc"
resolution_type="f"  # f: file or v: value

resolution="0.008"          # Approximately 1km resolution used by ECMWF
resolution="0.00833333333"  # Approximately 1km resolution
resolution="0.0089"         # Resolution for LAI CONFESS 

# Process years 1992 to 2015 from idir2
years1=$(seq 1992 2015)

for year in ${years1}; do
  echo "Processing year: ${year} from idir2"
  ifile="${idir2}/ESACCI-LC-L4-LCCS-Map-300m-P1Y-${year}-v2.0.7b.nc"
  ofile="${odir}/lccsclass_${year}_${resolution}deg.nc"
  version="207b"
  ./script-remap-lccci.sh $ifile $ofile $tmpdir $resolution $year $grid_target_file $resolution_type ${version}

done

#Process years 2016 to 2020 from idir1
years2=$(seq 2016 2020)
for year in ${years2}; do
  echo "Processing year: ${year} from idir1"
  ifile="${idir1}/C3S-LC-L4-LCCS-Map-300m-P1Y-${year}-v2.1.1.nc"
  ofile="${odir}/lccsclass_${year}_${resolution}deg.nc"
  version="211"
  ./script-remap-lccci.sh $ifile $ofile $tmpdir $resolution $year $grid_target_file $resolution_type ${version}
done
