#!/bin/sh

# Routine to download daily VNPFIRE data files from https://feer.gsfc.nasa.gov/
#   
#   Version: 1.1
#   
#   History:
#      v1.0, 13-Apr-2018, Luke Ellison: Original routine.
#      v1.1, 06-Feb-2019, Luke Ellison: Altered path to files to accommodate new organizational structure.
#   
#   Required commands:
#      wget|curl, date, mkdir
#   
#   Syntax: getMODFIREdata download_directory resolution collection dates
#      Available resolutions:
#         all -- selects data from all available products
#         M -- selects VNP14 (M-band) data product
#         I -- selects VNP14IMG (I-band) data product
#      Available collections:
#         001  Collection 1 data
#      Available dates:
#         VNP14    -- begin:2012-01-19
#         VNP14IMG -- begin:2012-01-20
#   
#   Example 1 - download VNPFIRE data from both I-band and M-band Collection 1 products for 2017-12-31 into a folder called 'VNPFIRE' in the user's current working directory:
#      getVNPFIREdata ./VNPFIRE all 1 20171231
#      getVNPFIREdata ./VNPFIRE I,M 1 20171231
#   Example 2 - download M-band Collection 1 data for the month of Dec. 2017:
#      getVNPFIREdata ./VNPFIRE M 1 20171201-20171231
#   Example 3 - download I-band Collection 1 data for the first 3 days and the last 1 day of Dec. 2017:
#      getVNPFIREdata ./VNPFIRE I 1 20171201-20171203,20171231
#   

# Ensure correct number of arguments
if [[ $# -ne 4 ]]; then
	echo 'ERROR: Number of arguments is not correct!  Correct syntax:'
	echo '   getVNPFIREdata download_directory resolution 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"
resolutions="$2"
collection="$3"
dates="$4"

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

# Convert resolutions to product list
products=()
for sat in "${resolutions[@]}"; do
	case $sat in
		all) products+=(VNP14 VNP14IMG) ;;
		M) products+=(VNP14) ;;
		I) products+=(VNP14IMG) ;;
	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/VNPFIRE/${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
