def selectEVIFiles(base_path, doy, years, filenames): f_base = filenames[0] #'idn_phy_MOD13Q1' ext = filenames[1] #'.tif' all_files = buildFileList(base_path, ext) yrs = [] dy = [] for fl in all_files: yrs.append(getEVIYear(fl)) dy.append(getEVIDoY(fl)) if not years: # do all - get all files, work out what years are included years = set(yrs) if not doy: # do all days = set(dy) files = set(all_files) fileList = [] for d in days: yrList = [] for y in years: # create file fn = "{0}{1}.A{2}{3}.005.*{4}".format(base_path, f_base, y, d, ext) filtered = filter(files, fn) if filtered: if path.isfile(filtered[0]): yrList.append(filtered[0]) yrList.sort() t = d, yrList fileList.append(t) fileList.sort() return [fileList, days, years]
def calcLongTermAverageEVI(base_path, output_path, functionList = [], filenames = ('idn_cli_MOD13A3', '.tif')): # tls_phy_MOD13A3.A2014182_005.1_km_monthly_EVI.tif ext = filenames[1] all_files = buildFileList(base_path, ext) # do all - get all files, work out what years are included yrs = [] daysOfYear = collections.defaultdict(list) suffix = all_files[0].rsplit('.', 3)[2] for fl in all_files: p, f = path.split(fl) ydoy = f.split('.', 2)[1] y = int(ydoy[1:5]) d = int(ydoy[-7:-4]) if y%4 == 0 and d>60: # leap year and day after Feb 29, subtract one from day d = d-1 daysOfYear[str("{0:03d}".format(d))].append(fl) yrs.append(y) years = set(yrs) syr = min(years) #1981 eyr = max(years) numyrs = eyr - syr for dd in daysOfYear.keys(): newfilename = '{0}.{1}-{2}.{3}.{4}.{5}yrs'.format(filenames[0], syr, eyr, dd, suffix, str(numyrs)) if not functionList: # default is to calculate the minimum and maximum functionList.append('MIN') functionList.append('MAX') performCalculations(daysOfYear[dd], newfilename, output_path, functionList) return 0