#!/bin/bash

#SBATCH -n 1
#SBATCH --mem 5000
#SBATCH -t 1:00:00

gridTemplate=/esarchive/recon/ecmwf/erainterim/6hourly/psl/psl_197901.nc

### for interpolating 6-hourly data:
### input=/esarchive/exp/ecmwf/monthly_ensforhc/6hourly/psl/2018$week/psl_$year$week.nc
### output=/esarchive/scratch/ncortesi/Subestacional/hindcasts_data/6hourly/psl/psl_$year$week.nc
### output1=/esarchive/scratch/ncortesi/Subestacional/hindcasts_data/6hourly/psl/psl_$year${week}.nc01.nc
### output2=/esarchive/scratch/ncortesi/Subestacional/hindcasts_data/6hourly/psl/psl_$year${week}.nc02.nc
### cdo splitgrid $input $output ## split the file in its two grids, one based on "realization" variable and the other on "psl"
### rm $output1 ## delete the grid file based on "realization" variable
### cdo -f nc -remapbil,$gridTemplate $output2 $output ## interpolate the grid file base on "psl" variable
### rm $output2

### for interpolating daily data:
input=/esarchive/exp/ecmwf/monthly_ensforhc/daily_mean/psl_f6h/2018$week/psl_$year$week.nc
output=/esarchive/scratch/ncortesi/Subestacional/hindcasts_data/daily_mean/psl_f6h/psl_$year$week.nc

cdo -f nc -remapbil,$gridTemplate $input $output ## interpolate the grid file base on "psl" variable

if [ $? -eq 0 ]; then echo "Job num." $year$week "completed"; else echo "Job num." $year$week "terminated before it could finish"; fi

## call this script from this line of terminal:
##
## for week in 0104 0111 0118 0125 0201 0208 0215 0222 0301 0308 0315 0322 0329 0405 0412 0419 0426 0503 0510 0517 0524 0531 0607 0614 0621 0628 0705 0712 0719 0726 0802 0809 0816 0823 0830 0906 0913 0920 0927 1004 1011 1018 1025 1101 1108 1115 1122 1129 1206 1213 1220 1227; do for year in {1998..2017}; do rm -f chunk$year$week.*; sbatch -J int$year$week --output=chunk$year$week.out --error=chunk$year$week.err --export=week=$week,year=$year /esarchive/scratch/ncortesi/scripts/interp9.sh; done; done
