#!/bin/bash
#
# main script to control subsetting process
#
# by Piotr
# Jan 2022
#
#
# usage: this script is to be edited for given project
#        datasets and variables have to be customized
#        it is best to comment and uncomment individual steps, and process each dataset separately
#
#




if [ $# != 1 ]; then

echo too few arguments, 1 expected

exit
fi
jobsfile=$1

#this need to be here otherwise searchpath is expanded here and not passed as string to the python script
set -f
set -o noglob

#source /home/wolski/miniconda2/bin/activate /home/wolski/.conda/envs/conda3

#this is where the terra directory tree (or part thereof) will be duplicated, does not need to be edited normally
outputrootdir=../data  #should not end with /

#subsetcode -  will be the name of the last level directory to which data will be written (corresponding to the "native" in terra directory structure)
#cdocmd  - cdo command that will be used to subset data as in: cdo $cdocmd $inputfile $outputfile
# can be sellonlatbox,lon1,lon2,lat1,lat2 or remapbil,griddefinitionfile.txt or something similar


subsetcode=wcape
cdocmd="sellonlatbox,13,25,-25,-36"
#cdocmd="sellonlatbox,13,25,-25,-36 -sellevel,85000"

# processing datasets


#--------------------------------------------------------------------------------------- 
# projections data - cmip5, cmip6, cordex
#

vars=tasmax
#vars=uas,vas
freqs=day

for datatype in cmip6; do
    if [ $datatype == cmip6 ]; then
        #rcps=ssp534-over
        #rcps=ssp370
        #rcps=historical,ssp245,ssp585,ssp370
        rcps=hist-nat,ssp245-nat
        #rcps=historical
    else
        rcps=historical,rcp45,rcp85
    fi

    if [ $datatype == cordex ]; then
        #this one works,
        searchpath='/terra/data/cordex/AFR-44/*/*/*/*/native'
        #searchpath='/terra/data/cordex/AFR-22/*/*/*/*/native'
    else
        # cmip5 and cmip6 models are multi-ensemble, word "first" will trigger selecting only one member, * will select all
        # /terra/data/cmip5/global/rcp85/MIROC5/r1i1p1/day/native
        #searchpath="/terra/data/$datatype/global/*/*/first/*/native"
        #this processes all memebers
        searchpath=/terra/data/$datatype/global/*/*/*/*/native
        #this processes only members starting with r1
        #searchpath=/terra/data/$datatype/global/*/*/r1i*/*/native
    fi



    #preparing jobsfile
    #note that $rcps variable will overwrite pattern at relevant level in the searchpath
    # and that $freqs variable will overwrite pattern at relevant level in the searchpath

    #makejobs_subset.py creates list of jobs and submits them to cluster
    #echo python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $rcps $jobsfile $cdocmd
    python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $rcps $jobsfile $cdocmd


done



#--------------------------------------------------------------------------------------- 
freqs=day
vars=tas,tasmin,tasmax
dataset=era
searchpath=/terra/data/reanalysis/global/reanalysis/ECMWF/ERA5/day/native  #should not end with slash
activity=reanalysis

#sbatch run.slrm ./makejobs.sh python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $activity $jobsfile
#python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $activity $jobsfile $cdocmd


#--------------------------------------------------------------------------------------- 
freqs=day
vars=tasmax,tasmin,tas,tdps,uas,vas
#vars=uas
dataset=era
searchpath=/terra/data/reanalysis/global/reanalysis/ECMWF/ERA5-Land/day/africa  #should not end with slash
activity=reanalysis

#python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $activity $jobsfile $cdocmd


#--------------------------------------------------------------------------------------- 
freqs=day
vars=tasmax,tasmin
dataset=mswx
activity=gridded
searchpath=/terra/data/observed/global/gridded/GloH2O/MSWX/day/africa
#python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $activity $jobsfile $cdocmd


#--------------------------------------------------------------------------------------- 
freqs=day
vars=tasmax,tasmin
dataset=cpc
activity=gridded
searchpath=/terra/data/observed/global/gridded/CPC/Global-Daily-Temperature/day/native/
#python makejobs_subset.py $searchpath $outputrootdir $subsetcode $vars $freqs $activity $jobsfile $cdocmd

