#!/bin/tcsh

#
#script to dowload MERRA2 data.
# Ashwanth Srinivasan Tendral LLC.
# Initial version: 2018/05/01
# Changes: added some documentation. 2018/07/02

#see Bosilovich et al. report on MERRA2, for details on dataset and available variables: https://ntrs.nasa.gov/search.jsp?R=20150019760
#need to register with Earthdata Login and then authorize NASA GESDISC Data Access. 
#Instructions are here: https://disc.gsfc.nasa.gov/registration/registration-for-data-access

# we set the script up to download files yearly by providing a start and end day for a chosen year.
# for example to download for 2008; we have to provide a start and an end day.
# start day is the number of elapsed days from the start we want and today. 
# say we want Jan 2008/01/01 then start day will be no of days betwen 2007/12/31 and today (2018/07/02) ie 3836 days
# date +%Y%m%d -d "3836 days ago" gives 20071231
# end day will be 365(6) days after start day
# here it will be 366 days since 2008 was a leap year.



set stday = 3127 # start day is the number of days elapsed since the start day we want and today.
set enday = 3127 # end day is days before the start day

foreach dm (`seq $stday -1 $enday`)
set YMD = `date +%Y%m%d -d "$dm days ago" `
set M=`date +%m -d "$dm days ago" `
set Y=`date +%Y -d "$dm days ago" `
echo $YMD $M $Y
echo $stday $enday $dm

end
