POST
|
Hi, I need to read multiple folders and read the .tif files and hold it a rasterlist. I have mycode below but it holds only last year tif. files. Can somebody help me to read all the .tif files and hold in the raster list. ? Thanks in advance. Please see my code below: years = [1981,1982,1983]
for year in years:
lstFiles = []
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum' + "\\" + str(year)
lstFiles = arcpy.ListRasters("*","TIF")
for raster in lstFiles:
print(raster)
lstFiles.sort()
>>> lstFiles
[u'Week_1_Sum1983_10.tif', u'Week_1_Sum1983_11.tif', u'Week_1_Sum1983_9.tif', u'Week_2_Sum1983_10.tif', u'Week_2_Sum1983_11.tif', u'Week_2_Sum1983_9.tif', u'Week_3_Sum1983_10.tif', u'Week_3_Sum1983_11.tif', u'Week_3_Sum1983_9.tif', u'Week_4_Sum1983_10.tif', u'Week_4_Sum1983_11.tif', u'Week_4_Sum1983_9.tif', u'Week_5_Sum1983_10.tif', u'Week_5_Sum1983_11.tif', u'Week_5_Sum1983_9.tif']
... View more
09-13-2017
11:13 AM
|
0
|
10
|
3583
|
POST
|
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]
... View more
09-12-2017
11:43 AM
|
0
|
4
|
821
|
POST
|
Hi, I need to find the file index from a list of rasters that matches year, month and week. My code below work when I give the index manually. But, I need to loop through many raster files. My fmaskrasters list has 180 rasters and as follows: fmaskrasters = [ 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] normal raster list has 5 rasters). normal = [Crawford_Normal_Week_1_1981_2016_10.tif, Crawford_Normal_Week_2_1981_2016_10.tif, . Crawford_Normal_Week_5_1981_2016_10.tif] Can somebody help me to find the index from each raster list that matches, year, month and week so that I calculate the percent difference. My code below works fine but I need to pass the index that matches year, month and week criteria to find the index of raster from "fmaskrasters" and "normal". Thanks in advance, months = [10]
weeks = [1]
years = [1981]
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum\Crawford\All'
out = r'H:\PRISM_800m_weekly_sum\Crawford\All\output'
for year in years:
for month in months:
for week in weeks:
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum\Crawford\All'
fmaskrasters = arcpy.ListRasters()
fmaskrasters.sort()
#"Week_" + str(week) + "_Sum" + str(year) + "_" + str(month) + "_Crawford.tif"
#for file in fmaskrasters:
# if fnmatch.fnmatch(file, 'Week_' + str(week) + '_Sum' + str(year) + '_' + str(month) + '_Crawford.tif'):
# print (file)
fmaskrasters1=fmaskrasters[0]
weekras1 = arcpy.Raster(fmaskrasters1)
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum\Crawford\All\output'
normal = arcpy.ListRasters()
normal.sort()
#normalras = arcpy.Raster(normal["Crawford_Normal_Week_" + str(week) + "_1981_2016_" + str(month) + ".tif"])
normalras1=normal[0]
outcalc1 = Minus(normalras1, weekras1)
Final = (outcalc1/normalras1)*100
finras1 = out + "\\" + "Crawford_Normal_Week_" + str(week) + "_Difference" + "_" + str(year) + "_" + str(month) + ".tif"
arcpy.CopyRaster_management(Final,finras1 + "","","","","","")
... View more
09-11-2017
07:20 AM
|
0
|
0
|
578
|
POST
|
I am selecting one raster layer using the week, month and year defined in the main script. Therefore, "normal" and "fmaskrasters" have one raster layer. I have revised the code with what you said but still I am getting the same error.
... View more
09-08-2017
08:18 PM
|
0
|
1
|
661
|
POST
|
Thanks for the reply. My rasters are located in two different work spaces. I want to obtain the percent differences for the corresponding week rasters. For example, long term weekly avarege rasters are read as normals and the weekly rasters for each year are located in fmaskrasters. I am getting the two rasters correct in the above code but when I run Minus(normal,fmaskrasters) it gives the error mentioned above. Normal Rasters:
Crawford_Normal_Week_1_1981_2016_10.tif
Crawford_Normal_Week_2_1981_2016_10.tif
Crawford_Normal_Week_3_1981_2016_10.tif
Crawford_Normal_Week_4_1981_2016_10.tif
Crawford_Normal_Week_5_1981_2016_10.tif
Weekly Rasters:
Week_1_Sum1981_10_Crawford.tif
.
.
Week_5_Sum1981_10_Crawford.tif
.
.
Week_1_Sum2010_10_Crawford.tif
Week_2_Sum2010_10_Crawford.tif
Week_3_Sum2010_10_Crawford.tif
Week_4_Sum2010_10_Crawford.tif
Week_5_Sum2010_10_Crawford.tif
.
.
Week_1_Sum2016_10_Crawford.tif
.
.
Week_5_Sum2016_10_Crawford.tif
Percent Difference = ((Week_1_Sum2010_10_Crawford.tif - Crawford_Normal_Week_1_1981_2016_10.tif) / Crawford_Normal_Week_1_1981_2016_10.tif) *100
... View more
09-08-2017
03:33 PM
|
0
|
3
|
661
|
POST
|
Hi, I need to calculate the percent difference between two rasters. Below is my code and I am getting the following error. Can somebody help me to correct my code? Thanks in advance. Error: Traceback (most recent call last):
File "C:\Users\jayaskeradl\Desktop\Arkansas Flood\My_Phyton_Scripts\Normal_Difference.py", line 73, in <module>
outcalc2 = Minus(normal, fmaskrasters)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\sa\Functions.py", line 4247, in Minus
in_raster_or_constant2)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\sa\Functions.py", line 4244, in Wrapper
["Minus", in_raster_or_constant1, in_raster_or_constant2])
TypeError: expected a raster or layer name months = [10]
weeks = [1]
year = [2010]
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum\Crawford\All'
out = r'H:\PRISM_800m_weekly_sum\Crawford\All\output'
for month in months:
for week in weeks:
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum\Crawford\All'
fmaskrasters = arcpy.ListRasters("Week_" + str(week) + "_Sum" + str(year) + "_" + str(month) + "*","*")
arcpy.env.workspace = r'H:\PRISM_800m_weekly_sum\Crawford\All\output'
normal = arcpy.ListRasters("Crawford_Normal_Week_" + str(week) + "_*_" + str(month) + "*")
outcalc2 = Minus(normal, fmaskrasters)
Final = (outcalc2/normal)*100
finras1 = out + "\\" + "Crawford_Normal_Week_" + str(week) + "_Difference" + "_" + str(month) + ".tif"
arcpy.CopyRaster_management(Final,finras1 + "_Normal_Difference_Crawford.tif","","","","","")
... View more
09-08-2017
02:49 PM
|
0
|
5
|
1376
|
POST
|
Dan. I did what you suggested to test manually (Please see the code above and it works). But I need to pass "year" into the code block in the main script.
... View more
09-07-2017
12:01 PM
|
0
|
0
|
1119
|
POST
|
OK. I updated the code inside the code block manually to reflect what you suggested and it worked. (Please see the revised code above). But, I need to pass the value in "year" in the main script into the code block. For example in this case 1997. I need to avoid manually enter the year into the code block so I can run the script for multiple years. How can I achieve that?
... View more
09-07-2017
11:45 AM
|
0
|
2
|
1119
|
POST
|
Thanks Dan for the reply. I did as you suggested but still getting the same error. Please see the revised error message and code above.
... View more
09-07-2017
11:28 AM
|
0
|
4
|
1119
|
POST
|
Hi, I want to loop the below process for multiple years so tested with one year. I am getting an error when it tries to change the (YEAR) as calculate field as a number variable with the corresponding year. Cannot understand what I am doing wrong here. Everything else up to line 29 is working. Problem is inside the code block section. Any help is greatly appreciated. Thanks in advance. ExecuteError: ERROR 000539: Error running expression: getClass(1998.0)
Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 4, in getClass
NameError: global name 'yr' is not defined
Failed to execute (CalculateField).
years = [1997]
# Write the selected features to a new featureclass
for year in years:
arcpy.CopyFeatures_management("PLSS_KS_All_GCS", r"C:/Users/Desktop/Flood/KS_All.gdb/PLSS_KS_All_WklyPrSum_PRISM_800m_" + str(year)+ "_new")
# Set environment settings
arcpy.env.workspace = "H:/PRISM_800m_weekly_sum_tif"
# Set local variables
inPointFeatures = r"C:/Users/Desktop/Flood/KS_All.gdb/PLSS_KS_All_WklyPrSum_PRISM_800m_" + str(year)+ "_new.shp"
# Set local variables
inRasterList = [["Week_1_Sum" + str(year)+ "_10.tif", "Oct_PCPwk1"],
["Week_2_Sum" + str(year)+ "_10.tif", "Oct_PCPwk2"],
["Week_3_Sum" + str(year)+ "_10.tif", "Oct_PCPwk3"],
["Week_4_Sum" + str(year)+ "_10.tif", "Oct_PCPwk4"],
["Week_5_Sum" + str(year)+ "_10.tif", "Oct_PCPwk5"],
["Week_1_Sum" + str(year)+ "_11.tif", "Nov_PCPwk1"],
["Week_2_Sum" + str(year)+ "_11.tif", "Nov_PCPwk2"]]
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute ExtractValuesToPoints
ExtractMultiValuesToPoints(inPointFeatures, inRasterList, "BILINEAR") #NONE
fieldName = "YEAR"
expression = "getClass(!YEAR!)"
codeblock = """def getClass(YEAR):
global yr
yr=1997
if YEAR >= 1998:
return yr
else:
return -9999"""
# Execute AddField
#arcpy.AddField_management(inTable, fieldName, "SHORT")
# Execute CalculateField
arcpy.CalculateField_management(inPointFeatures, fieldName, expression, "PYTHON_9.3", codeblock)
... View more
09-07-2017
10:52 AM
|
0
|
9
|
1889
|
POST
|
Hi, I need to pass year to the path inside a for loop in python script. years = [2010,2011,2012]
months = [9]
for year in years:
for month in months:
arcpy.env.workspace = r'H:\Test_ONLY\prism_800m_'+ year[:4]
scratch = r'H:\Test_ONLY\scratch'
if year % 4 > 0 and month == 9:
fmaskrasters1 = fmaskrasters[243:249]
fmaskrasters2 = fmaskrasters[249:255]
fmaskrasters3 = fmaskrasters[255:261]
fmaskrasters4 = fmaskrasters[261:267]
fmaskrasters5 = fmaskrasters[267:273]
else:
print("Not configured in the script!")
I want to change r'H:\Test_ONLY\prism_800m_2010 each time for the months from Sep. How can I achieve this by modifying the above code. I think I am not doing something right in arcpy.env.workspace = r'H:\Test_ONLY\prism_800m_'+ year[:4] can somebody help me to correct this? Thanks in advance.
... View more
09-06-2017
09:07 AM
|
0
|
3
|
552
|
POST
|
I ran the code upto inPointFeatures line in Python window in ArcGIS and gave this error below. Seems to be the code is correct. Cannot understand why. Runtime error Traceback (most recent call last): File "<string>", line 13, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 6477, in MakeFeatureLayer raise e ExecuteError: ERROR 000622: Failed to execute (Make Feature Layer). Parameters are not valid. ERROR 000628: Cannot set input into parameter in_features.
... View more
08-29-2017
08:16 AM
|
0
|
2
|
1179
|
POST
|
I just checked the shape file and I have used a point shape file. I have corrected to the polygon shape file. (Please see the updated code) But, the code above does not give an error and shows it completed the run. But, I cannot see the output. What am I doing wrong here?
... View more
08-28-2017
08:43 PM
|
0
|
4
|
1179
|
Title | Kudos | Posted |
---|---|---|
1 | 01-23-2015 04:00 PM | |
1 | 01-29-2015 04:14 PM | |
1 | 01-30-2015 12:59 PM | |
1 | 02-03-2015 12:04 PM | |
2 | 10-09-2017 09:41 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|