#!/bin/bash

# Documentation on how to use this Bash script:

# This script is written in Bash, a shell and scripting language included with Mac OS X and most Linux distributions. The cURL utility is 
# required, and included with most UNIX and Linux OSes.This script is inteneded to download multiple coordinates from the predictors 
# interface at https://climate-scenarios.canada.ca/?page=pred-cmip6. It is written to allow the download of rectangular grids, although 
# its structure could be repurposed to allow for more complex shapes.

# To use this script, define the output directory for the downloaded files and look through the example code blocks for the grid pattern you 
# would like to download, either vertical, horizontal, or rectangular. Next, modify the longitudinal (Start_X and End_X) and latitudinal 
# (Start_Y and End_Y) grid cell variables under the header ‘Download predictor files’ to select for your grid cells of interest. All models 
# within the modelSelection array are included in the download; a comprehensive list of available global climate models and reanalysis 
# datasets is provided below.

# X values (longitudes) start at 001 and end at 128 (inclusive)
# Y values (latitudes) start at 01 and end at 64 (inclusive)

# This section defines the directory for the downloaded files:

# Default: Current location of this script
Dest="$PWD"

# To save to downloads, use:
# $Dest = "$HOME\Downloads"

# To get a straight horizontal line, e.g. from x = 005 to x = 10 (inclusive) at y = 50:

#Start_X = 5
#End_X = 10
#Start_Y = 50
#End_Y = 50

# To get a straight vertical line, e.g. from y = 09 to y = 20 (inclusive) at x = 100:

#Start_Y = 9
#End_Y = 20
#Start_X = 100
#End_X = 100

# To get a rectangular box, e.g. from x = 005 to x = 010 (inclusive) and from y = 06 to y = 11 (inclusive):

#Start_X = 5
#End_X = 10
#Start_Y = 6
#End_Y = 11

# Select models from the following list:
# ("CanESM5_historical", "CanESM5_ssp119", "CanESM5_ssp126", "CanESM5_ssp245", "CanESM5_ssp370", "CanESM5_ssp585", "ECMWF_ERA5", "MPI-ESM1-2-HR_historical", "MPI-ESM1-2-HR_ssp126", "MPI-ESM1-2-HR_ssp245", "MPI-ESM1-2-HR_ssp370", "MPI-ESM1-2-HR_ssp585", "NCEP-DOE", "NorESM2-MM_historical", "NorESM2-MM_ssp126", "NorESM2-MM_ssp245", "NorESM2-MM_ssp370", "NorESM2-MM_ssp585")

# For example:
#modelSelection="CanESM5_ssp119 CanESM5_historical"


# Download predictor files:
# ========================

Start_X=8
End_X=10
Start_Y=9
End_Y=11
modelSelection="CanESM5_historical CanESM5_ssp119 CanESM5_ssp126 CanESM5_ssp245 CanESM5_ssp370 CanESM5_ssp585 ECMWF_ERA5 MPI-ESM1-2-HR_historical MPI-ESM1-2-HR_ssp126 MPI-ESM1-2-HR_ssp245 MPI-ESM1-2-HR_ssp370 MPI-ESM1-2-HR_ssp585 NCEP-DOE NorESM2-MM_historical NorESM2-MM_ssp126 NorESM2-MM_ssp245 NorESM2-MM_ssp370 NorESM2-MM_ssp585"
 
for model in $modelSelection; do
    for X_I in $(seq $Start_X $End_X); do
        for Y_I in $(seq $Start_Y $End_Y); do
            X_padded=$(printf "%03d" "$X_I")
            Y_padded=$(printf "%02d" "$Y_I")
            Fname=${model}_BOX_${X_padded}X_${Y_padded}Y.zip
            Url="https://climate-scenarios.canada.ca/files/CMIP6_predictors/$model/$Fname"
            echo $Url # Prints filename that will be downloaded
            echo "$Dest/$Fname"
            curl $Url -o $Dest/$Fname
        done
    done
done
