Wednesday, 30 November 2016

arcgis desktop - Daily Rainfall data processing?


i have a .shp file of gridded daily rainfall data for an entire state.


How do i calculate the sum of all 365 days (each day is a field, n recorded for 450 stations) and generate a raster?



I am using ArcGIS Desktop 10.5.



Answer



Would be a simple task for Field Calculator but since you have 365 fields use the da.UpdateCursor instead and list fields using ListFields. The tricky part is to only list the 365 field you want to sum and not ObjectID field, Shape fields etc. You can do it by listing all fields but the ones starting with some string, for example 'SH' = Option 1, or by listing field starting with something, lets say all 365 field names start with 'rain' = Option 2.


1 - Start by adding the field you want to populate with sums using Add Field as type Double.


2 - Modify and execute this in the Python window with the feature class added as a layer to table of contents. You can run everything up to and including the print statement to get the field listing correct, then execute all.


import arcpy

field_to_calc = 'SumOfAll' #Change to match the name of your field to calculate
feature_layer_name = 'features123' #Change to match the name of the feature layer in table of contents


#Choose one, and modify so only the 365 fields are being listed:

#Option 1: List all fields except the ones starting with FI, SH, OBJ or field_to_calc
fields_to_sum = [f.name for f in arcpy.ListFields(feature_layer_name) if not f.name.upper().startswith(('FI','SH','OB', field_to_calc))]

#Option 2: List all fields starting with string XYZ or ABC
fields_to_sum = [f.name for f in arcpy.ListFields(feature_layer_name) if f.name.upper().startswith(('XYZ','ABC'))]

print fields_to_sum


#Calculate field_to_calc as sum of all listed fields
fields_to_sum.append(field_to_calc)
with arcpy.da.UpdateCursor(feature_layer_name,fields_to_sum) as cursor:
for row in cursor:
row[-1] = sum(filter(None,row[:-1]))
cursor.updateRow(row)

3 - Convert to raster with Feature to Raster using calculated field as value field


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...