In the next session, we need several files from the ERA-Interim reanalysis dataset.
Write a function that takes year, month, startday, and endday as parameters, and returns
list a of corresponding Z-level filenames (with absolute paths).
The input parameters have to be numeric (not string).
In [2]:
# The path to erainterim
root = '/net/atmos/data/erainterim/cdf'
# below that directory the path is <year>/<month>/.
# <year> has values from 1979 - 2014, <month> from 01 - 12.
# Under the <month> directory is a large number of files.
# The Z-files have the form:
# Z<year><month><day>_hour
# For example:
# <root>/2010/09/Z20100903_18
# refers to Sept. 3, 2010, 18:00h
In [3]:
# helpful modules
import os.path
import glob
# example parameters
startday = 3
endday = 8
month = 9
year = 2010
# your function here:
def get_zfile(startday, endday, month, year):
path = os.path.join(root,str(year),"{:02d}".format(month))
filenames = []
for day in range(startday, endday + 1):
fn1 = "Z{}{:02d}{:02d}*".format(year,month,day)
filenames = filenames + glob.glob(os.path.join(path, fn1))
#print(filenames)
filenames.sort()
return(filenames)
files = get_zfile(startday, endday, month, year)
print(files)
In [ ]: