A little exercise

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)


['/net/atmos/data/erainterim/cdf/2010/09/Z20100903_00', '/net/atmos/data/erainterim/cdf/2010/09/Z20100903_06', '/net/atmos/data/erainterim/cdf/2010/09/Z20100903_12', '/net/atmos/data/erainterim/cdf/2010/09/Z20100903_18', '/net/atmos/data/erainterim/cdf/2010/09/Z20100904_00', '/net/atmos/data/erainterim/cdf/2010/09/Z20100904_06', '/net/atmos/data/erainterim/cdf/2010/09/Z20100904_12', '/net/atmos/data/erainterim/cdf/2010/09/Z20100904_18', '/net/atmos/data/erainterim/cdf/2010/09/Z20100905_00', '/net/atmos/data/erainterim/cdf/2010/09/Z20100905_06', '/net/atmos/data/erainterim/cdf/2010/09/Z20100905_12', '/net/atmos/data/erainterim/cdf/2010/09/Z20100905_18', '/net/atmos/data/erainterim/cdf/2010/09/Z20100906_00', '/net/atmos/data/erainterim/cdf/2010/09/Z20100906_06', '/net/atmos/data/erainterim/cdf/2010/09/Z20100906_12', '/net/atmos/data/erainterim/cdf/2010/09/Z20100906_18', '/net/atmos/data/erainterim/cdf/2010/09/Z20100907_00', '/net/atmos/data/erainterim/cdf/2010/09/Z20100907_06', '/net/atmos/data/erainterim/cdf/2010/09/Z20100907_12', '/net/atmos/data/erainterim/cdf/2010/09/Z20100907_18', '/net/atmos/data/erainterim/cdf/2010/09/Z20100908_00', '/net/atmos/data/erainterim/cdf/2010/09/Z20100908_06', '/net/atmos/data/erainterim/cdf/2010/09/Z20100908_12', '/net/atmos/data/erainterim/cdf/2010/09/Z20100908_18']

In [ ]: