#!/bin/sh

# Routine to download daily MODFIRE data files from https://feer.gsfc.nasa.gov/
#   
#   Version: 1.2
#   
#   History:
#      v1.0, 29-Oct-2015, Luke Ellison: Original routine.
#      v1.1, 01-Feb-2017, Luke Ellison: Changed to secure protocol (HTTP to HTTPS); included options to wget and curl commands to ignore certificate checks.
#      v1.2, 06-Feb-2019, Luke Ellison: Altered path to files to accommodate new organizational structure.
#   
#   Required commands:
#      wget|curl, date, mkdir
#   
#   Syntax: getMODFIREdata download_directory satellite collection dates
#      Available satellites:
#         all -- selects data from all available products
#         Terra -- selects MOD14 data product
#         Aqua -- selects MYD14 data product
#      Available collections:
#         005  Collection 5 data
#         006  Collection 6 data
#      Available dates:
#         MOD14 -- begin:2000-02-24
#         MYD14 -- begin:2002-07-04
#   
#   Example 1 - download MODFIRE data from both Terra and Aqua Collection 6 products for 2014-12-31 into a folder called 'MODFIRE' in the user's current working directory:
#      getMODFIREdata ./MODFIRE all 6 20141231
#      getMODFIREdata ./MODFIRE Terra,Aqua 6 20141231
#   Example 2 - download Aqua Collection 6 data for the month of Dec. 2014:
#      getMODFIREdata ./MODFIRE Aqua 6 20141201-20141231
#   Example 3 - download Terra Collection 6 data for the first 3 days and the last 1 day of Dec. 2014:
#      getMODFIREdata ./MODFIRE Terra 6 20141201-20141203,20141231
#   

# Ensure correct number of arguments
if [[ $# -ne 4 ]]; then
	echo 'ERROR: Number of arguments is not correct!  Correct syntax:'
	echo '   getMODFIREdata download_directory satellite collection dates'
	exit 1
fi

# Check for existence of wget or curl
if loc="$(type -p wget)" || [ -z "$loc" ]; then
	cmd="wget"
elif loc="$(type -p curl)" || [ -z "$loc" ]; then
	cmd="curl"
else
	echo 'ERROR: Could not detect existence of wget or curl commands!'
	exit 1
fi

# Check for instance of date command
if date --version 2>&1 | grep -q 'GNU coreutils'; then
	datecmd="GNU"
else
	datecmd="BSD"
fi

# Save arguments
opath="$1"
satellites="$2"
collection="$3"
dates="$4"

# Change directory
eval cd "$opath"		# use eval for case of tilde

# Convert satellites to product list
products=()
for sat in "${satellites[@]}"; do
	case $sat in
		all) products+=(MOD14 MYD14) ;;
		Terra) products+=(MOD14) ;;
		Aqua) products+=(MYD14) ;;
	esac
done

# Format collection
collection=${collection//.}
collection=$(printf "%03.0f" "$collection")

# Convert dates to list
dateranges=(${dates//,/ })
dates=()
for daterange in "${dateranges[@]}"; do
	dateextrema=(${daterange//-/ })
	datestart="${dateextrema[0]}"
	if [[ ${#dateextrema[@]} -gt 1 ]]; then dateend="${dateextrema[1]}"; else dateend="$datestart"; fi
	day="$datestart"
	while [ "$day" -le "$dateend" ]; do
		if [ "$datecmd" == "GNU" ]; then
			dates+=($(date -d "$day" +"%Y%j"))
			day=$(date -d "$day +1 day" +"%Y%m%d")
		elif [ "$datecmd" == "BSD" ]; then
			dates+=($(date -j -f "%Y%m%d" "$day" +"%Y%j"))
			day=$(date -j -f "%Y%m%d" -v+1d "$day" +"%Y%m%d")
		fi
	done
done

# Loop thru species
for prod in "${products[@]}"; do
	# Loop thru dates
	for idate in "${dates[@]}"; do
		# Get download and target file paths
		filename="${prod}_${collection}_Fire_Table_${idate}.dat"
		datedirs="${idate:0:4}"
		savedir="${collection}/${prod}/${datedirs}"
		downloadpath="https://feer.gsfc.nasa.gov/data/frp/MODFIRE/${savedir}/${filename}"
		
		# Ensure local directory tree exists
		mkdir -p "$savedir"
		
		# Download data file
		if [ "$cmd" == "wget" ]; then
			wget --no-check-certificate -NP "$savedir" "$downloadpath"
		elif [ "$cmd" == "curl" ]; then
			curl -Rko "$savedir/${filename}" -z "$savedir/${filename}" "$downloadpath"
		fi
	done
done

# Finish
echo "Download completed."
exit 0