HI,
Can somebody help me to read one file at a time in the following raster list? I want to match a condition and then read the corresponding raster file from the "rasterlist". My code below cannot achieve what I want.
Any help is appreciated.
Thanks in advance.
year=2010
month=10
week=[1,2,3,4,5]
rasterlist = [
Week_1_Sum1981_10_Crawford.tif
Week_1_Sum1982_10_Crawford.tif
.
Week_1_Sum2016_10_Crawford.tif
Week_2_Sum1981_10_Crawford.tif
.
Week_2_Sum2016_10_Crawford.tif
.
Week_5_Sum2016_10_Crawford.tif]
for file in fmaskrasters:
if fnmatch.fnmatch(file, 'Week_' + str(week) + '_Sum' + str(year) + '_' + str(month) + '_Crawford.tif'):
print (file)
raster = rasterlist[file]
Before I get too far, you appear to have invalid syntax for building your raster list. Instead of:
rasterlist = [
Week_1_Sum1981_10_Crawford.tif
Week_1_Sum1982_10_Crawford.tif
Week_1_Sum2016_10_Crawford.tif
Week_2_Sum1981_10_Crawford.tif
Week_2_Sum2016_10_Crawford.tif
Week_5_Sum2016_10_Crawford.tif
]
It should look more like:
rasterlist = [
"Week_1_Sum1981_10_Crawford.tif",
"Week_1_Sum1982_10_Crawford.tif",
"Week_1_Sum2016_10_Crawford.tif",
"Week_2_Sum1981_10_Crawford.tif",
"Week_2_Sum2016_10_Crawford.tif",
"Week_5_Sum2016_10_Crawford.tif"
]
Are you trying to generate rasterlist
or is it pre-defined? Since fmaskrasters
isn't defined in your code snippet, I can't quite provide more specific suggestions.
I believe the OP is trying to show an abbreviated file list as in "Week_1_Sum1981" to "Week_1_Sum2016" (advancing by month and year). For some additional context, see his earlier question: Help to find file index based on year, month and week in Python.
Perhaps walking through the directory listing using os.walk() would be an appropriate approach for his question.
This might give you an idea:
rasterlist = [
"Week_1_Sum1981_10_Crawford.tif",
"Week_1_Sum1982_10_Crawford.tif",
"Week_1_Sum2016_10_Crawford.tif",
"Week_2_Sum1981_10_Crawford.tif",
"Week_2_Sum2016_10_Crawford.tif",
"Week_5_Sum2016_10_Crawford.tif"
]
for year in range(1982, 1982+1): # startYear, endYear+1
for month in range(10, 10+1):
for week in range(1,1+1):
raster = "Week_{}_Sum{}_{}_Crawford.tif".format(week,year,month)
if raster in rasterlist:
print "Found: {}".format(raster)
# Output:
# Found: Week_1_Sum1982_10_Crawford.tif
If you want to put all the tif files in a directory into a list you could use something like:
import os, glob
rasterDir = r'C:\Path\To\Rasters'
rasterlist = list(os.path.basename(x) for x in glob.glob(rasterDir + r'\*.tif'))
print rasterlist
Hope this helps.
A clue to your dilemma lies in the code you posted... when you provided the 'list' for people to check, the first thing one should do is see whether what is provided as an example is valid. In your case, I just snagged the first element in the list and knew it hadn't been tested and the error message hadn't been reported. Here is what happens with your input list (reduced to just one element...)
rasterlist = [Week_1_Sum1981_10_Crawford.tif] # ..... as you provided, but 1 element
# ------------ Bam! --------------------------
Traceback (most recent call last):
File "<ipython-input-17-c9b27d519ec2>", line 1, in <module>
rasterlist = [Week_1_Sum1981_10_Crawford.tif]
NameError: name 'Week_1_Sum1981_10_Crawford' is not defined
# ------ meaning... you need to provide file names as a string, not as you did