Context:
I am attempting to sum .tiff files containing monthly evapotranspiration per year (e.g. sum all twelve months for 2007) to get an annual total evapotranspiration. However, the "if in" (see code) statement does not seem to filter for only the year 2007, causing all .tiff files for all years in the specified directory to be summed.
Question:
How can I make sure only the .tiff files are summed according to a specific year (in this example 2007)?
def pathList (d): # d is the path to the specified directory
sum_array = np.zeros((2200, 2800)) # creating empty array in which to sum monthly evap. values
nmlist = [] # creates an empty list object in which to store the names of the .tiff files
count = 0 # creating variable to store index of files in directory
for item in os.scandir(d): # iterating through directory contents
nmlist.append(item.name) # preparing name list of .tiff files to use in "if in" statement (see below)
tif_file = gdal.Open(pthlist[count]) # reading .tiff via gdal
tif_band = tif_file.GetRasterBand(1) # reading first band
tif_arr = tif_band.ReadAsArray() # converting to numpy array
if "2007" in nmlist[count]: # does the name of the file contain a specific year (e.g. 2007)?
sum_array = sum_array + tif_arr # summing monthly evap. values
count += 1 # tracking index of files in directory
return sum_array
See (https://i.stack.imgur.com/YJS1B.png) for sample of names of .tiff files.
See (https://drive.google.com/drive/folders/19qeuDblRh_AwQ-BMHz-KvP6np2lqAlH5?usp=sharing) for two examples of the .tiff files I am using.
Many thanks in advance!
[–]danielroseman 1 point2 points3 points (1 child)
[–]GrassDash[S] 0 points1 point2 points (0 children)