#!/bin/sh

# Routine to download daily FEER emissions files from https://feer.gsfc.nasa.gov/
#   
#   Version: 2.1
#   
#   History:
#      v1.0, 16-Oct-2015, Luke Ellison: Original routine.
#      v1.1, 20-Oct-2015, Luke Ellison: Added capability for GNU version of date command; made program check for only newer files on the server.
#      v2.0, 16-Sep-2016, Luke Ellison: Added 'time_resolution' input.
#      v2.1, 01-Feb-2017, Luke Ellison: Changed to secure protocol (HTTP to HTTPS); included options to wget and curl commands to ignore certificate checks.
#   
#   Required commands:
#      wget|curl, date, grep, sort, uniq, mkdir
#   
#   Syntax: getFEERdata download_directory FEER_product_name species dates time_resolution
#      Available FEER products:
#         FEERv1.0-G1.0 -- uses FEER Ce v1.0 and GFAS FRP v1.0
#         FEERv1.0-G1.2 -- uses FEER Ce v1.0 and GFAS FRP v1.2
#      Available species:
#         all   all available species
#         C     Total Carbon
#         CO2   Carbon Dioxide
#         CO    Carbon Monoxide
#         CH4   Methane (CH4)
#         TVOC  Total Volatile Organic Compounds
#         C2H2  Acetylene (C2H2)
#         C2H6  Ethane (C2H6)
#         C3H6  Propene (C3H6)
#         C3H8  Propane (C3H8)
#         nBut  Butane (C4H10)
#         iBut  Isobutane (C4H10)
#         CH4O  Methanol (CH4O)
#         CH2O  Formaldehyde (CH2O)
#         MeCHO Acetaldehyde (C2H4O)
#         Acet  Acetone (C3H6O)
#         MEK   Methyl Ethyl Ketone (C4H8O)
#         CH2O2 Formic Acid (CH2O2)
#         AcOH  Acetic Acid (C2H4O2)
#         H2    Hydrogen
#         NOx   Mono-Nitrogen Oxides
#         N2O   Nitrous Oxide
#         NH3   Ammonia (NH3)
#         HCN   Hydrogen Cyanide (HCN)
#         SO2   Sulfur Dioxide
#         COS   Carbonyl Sulfide (COS)
#         PM2.5 Particulate Matter under 2.5μm
#         TPM   Total Particulate Matter
#         TC    Total Carbonaceous Aerosols
#         OC    Organic Carbon
#         BC    Black Carbon
#      Available dates: 
#         FEERv1.0-G1.0 -- begin:2003-01-01, end:2013-12-31
#         FEERv1.0-G1.2 -- begin:2003-01-01
#      Available time resolutions: 
#         D -- Daily (default)
#         M -- Monthly
#   
#   Example 1 - download all species from FEERv1.0-G1.2 daily product for 2014-12-31 into a folder called 'FEER' in the user's current working directory:
#      getFEERdata ./FEER FEERv1.0-G1.2 all 20141231
#   Example 2 - download monthly TPM, OC and BC for Oct.-Dec. 2014:
#      getFEERdata ./FEER FEERv1.0-G1.2 TPM,OC,BC 20141001-20141201 M
#   Example 3 - download daily NOx for the first 3 days and the last 1 day of Dec. 2014:
#      getFEERdata ./FEER FEERv1.0-G1.2 NOx 20141201-20141203,20141231
#   

# Ensure correct number of arguments
if [[ $# -lt 4 || $# -gt 5 ]]; then
	echo 'ERROR: Number of arguments is not correct!  Correct syntax:'
	echo '   getFEERdata download_directory FEER_product_name species dates time_resolution'
	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"
FEERprod="$2"
species="$3"
dates="$4"
[[ $# -eq 5 ]] && res="${5:0:1}" || res="D"

# Validate time resolution
if [[ "$res" =~ ^[Dd] ]]; then
	res="D"
	RES="DAILY"
	Res="Daily"
elif [[ "$res" =~ ^[Mm] ]]; then
	res="M"
	RES="MONTHLY"
	Res="Monthly"
else
	echo 'ERROR: Unrecognized time_resolution input!'
	exit 1
fi

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

# Extract FEER version
FEERvers=${FEERprod:4:4}

# Convert species to list
if [ "$species" == "all" ]; then
	species="C,CO2,CO,CH4,TVOC,C2H2,C2H6,C3H6,C3H8,nBut,iBut,CH4O,CH2O,MeCHO,Acet,MEK,CH2O2,AcOH,H2,NOx,N2O,NH3,HCN,SO2,COS,PM2.5,TPM,TC,OC,BC"
fi
species=(${species//,/ })

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

# Loop thru species
for ispecies in "${species[@]}"; do
	# Loop thru dates
	for idate in "${dates[@]}"; do
		# Get download and target file paths
		filename="${FEERprod}_${Res}_${ispecies}_${idate}.nc"
		[[ "$res" == "D" ]] && datedirs="${idate:0:4}/${idate:4:2}" || datedirs="${idate:0:4}"
		savedir="${FEERvers}/Emissions/${FEERprod}/${RES}/${ispecies}/${datedirs}"
		echo "$savedir"
		downloadpath="https://feer.gsfc.nasa.gov/data/emissions/${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