|
POST
|
Check out the Cell Statistics tool that Curtis Price mentioned. It takes a list of raster filenames and calculates an output raster with whatever summary statistic type you choose. Using my file grouping code above, you can do something like... for date, rasters in raster_days.items():
# Execute CellStatistics
outCellStatistics = arcpy.sa.CellStatistics(rasters, "MEAN")
# Save the output
outCellStatistics.save("C:/example/cellstats_output") Note that it looks like you do need the spatial analyst extension for Cell Statistics. # Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
... View more
06-20-2017
08:46 AM
|
2
|
1
|
2655
|
|
POST
|
Glad you found a solution! Don't forget to Mark a reply as the Correct Answer.
... View more
06-19-2017
08:15 AM
|
0
|
0
|
2266
|
|
POST
|
Curtis Price already answered this for you above... .getOutput is a method called on a result object. This is the most direct way to get the result value from GetCount (or any other tool that returns a result.) The result is a string, so that's why he wrapped it with int() -- to convert '45' to 45 for a count of 45 rows.
... View more
06-15-2017
11:33 AM
|
1
|
1
|
2266
|
|
POST
|
I just tested and FC Basson is correct, Table To Excel does work with a feature class. That means your code should work as-is. However, I think Ian Murray is correct about line 26 in your code so read through his comment carefully. If you get stuck, try going back to square one and run the geoprocessing tool manually in ArcCatalog to make sure you're using the correct inputs. You can copy the python snippet from a completed process in the geoprocessing results as a starting point.
... View more
06-14-2017
02:53 PM
|
0
|
0
|
4115
|
|
POST
|
That's true, bixb0012 There are indeed many ways to solve this puzzle. I know in our scheduled task scripts we do both ways (bail immediately or try to continue) depending on the script.
... View more
06-14-2017
02:07 PM
|
0
|
1
|
22059
|
|
BLOG
|
Great update! Where did you even find out about "GPInMemoryWorkspace"?
... View more
06-14-2017
09:34 AM
|
0
|
0
|
1129
|
|
POST
|
Not sure what's happening with if table() == fc(): Are these supposed to be custom functions? if so, could you post the code for those. It looks like you don't have any field names specified in the fields parameter, which is "the fields from the join table to be included in the join." I think you need to tell it what fields you want appended to your input feature class, otherwise it doesn't really do what you want. You're already using the os module for the path separator, you should go all the way and use os.path.join() to build your paths. /blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python
... View more
06-14-2017
09:05 AM
|
0
|
0
|
970
|
|
POST
|
Using the with keyword to open the text file only acts as a try/finally block just for that file. It doesn't handle exceptions. Using the with inside the try/except like Method #2 allows you to write the success or failure of the script. Alternatively, you could do something like... try:
log_msg = ""
do something useful
log_msg += "something useful completed\n"
do something special
log_msg += "something special completed\n"
except Exception as err:
log_msg += str(err)
finally:
try:
with open(logFile, 'a') as f:
f.write(log_msg)
except Exception:
pass This would allow you to log success and failure messages and only deal with writing to the log file once. The with is also put inside a try/except, allowing any other cleanup code in the finally block to actually finish in case something goes wrong when trying to write to the log file.
... View more
06-14-2017
07:48 AM
|
1
|
3
|
22059
|
|
POST
|
Assuming the characters before the date in the filename are always the same length and all of your rasters are together in the same folder, see if this prints out the raster file names for each day that exists in the filename dates. import arcpy
from collections import defaultdict
def main():
raster_dir = r"D:\NASA\Data\SMAP\2014\Processed"
arcpy.env.workspace = raster_dir
raster_days = defaultdict(list)
for r in arcpy.ListRasters():
raster_days[r[15:23]].append(r)
for date, rasters in raster_days.items():
print(date)
for r in rasters:
print("\t{}".format(r))
if __name__ == '__main__':
main()
This creates a dictionary where the key is a date string (YYYYMMDD) and the value is a list of raster filenames that all have that date. If that works, you should be able to use this dictionary to calculate your statistics however you want. EDIT: Fixed typo on raster_days variable name in for loop.
... View more
06-13-2017
04:19 PM
|
1
|
9
|
4263
|
|
POST
|
Is the prefix and suffix of the filenames (the stuff before and after the date) always the same? If not, is the length the same? What are the rules on how those are generated?
... View more
06-13-2017
03:51 PM
|
0
|
11
|
4263
|
|
POST
|
Can you use raster properties to average the average of every raster for the day? In other words, loop over each raster for the day to grab its average and put it in a list, then average the list; like numpy.mean() Or are you also asking how to look at all these raster files and identify which ones are from the same day with the file name?
... View more
06-13-2017
03:43 PM
|
0
|
13
|
4263
|
|
POST
|
Would it be easier to use the MEAN raster property for each raster?
... View more
06-13-2017
03:21 PM
|
0
|
18
|
4263
|
|
POST
|
I don't see any reason why you would not want to use the with statement when working with files. Our organization does similar to method #2 for our own scheduled task script logging, although I write the data to a table in our enterprise geodatabase rather than a text file.
... View more
06-13-2017
02:04 PM
|
2
|
5
|
22059
|
|
POST
|
Also I would recommend using the os module for working with filepaths(see os.path.join), its cleaner than using string backslashes and concatenating filenames together. I agree. Dan Patterson wrote an insightful blog post on the topic. /blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python
... View more
06-13-2017
01:58 PM
|
1
|
0
|
3080
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 07-10-2026 07:32 AM | |
| 1 | 10-23-2025 03:53 PM | |
| 1 | 04-28-2026 07:25 AM | |
| 1 | 03-19-2026 08:59 AM | |
| 1 | 02-12-2026 01:37 PM |