-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery Starbot ⭐ refactored redblackeagleman/peak-flow-calculator #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,9 @@ def precip_table_etl_cnrccep( | |
| # Open the precipitation data csv and read all the precipitations out. | ||
| with open(ws_precip) as g: | ||
| input_precip= csv.reader(g) | ||
|
|
||
| # Skip the first 10 rows of the csv, which are assorted header information. | ||
| for j in range (1, 11): | ||
| for _ in range (1, 11): | ||
|
Comment on lines
-33
to
+35
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| next(g) | ||
|
|
||
| k=1 | ||
|
|
@@ -44,7 +44,7 @@ def precip_table_etl_cnrccep( | |
| if k>8: | ||
| break | ||
| else: | ||
| k=k+1 | ||
| k += 1 | ||
| return precips | ||
|
|
||
| def precip_table_etl_noaa( | ||
|
|
@@ -89,9 +89,12 @@ def precip_table_etl_noaa( | |
| .select(t1, desc_field, lambda v: v == duration_val)\ | ||
| .cutout(desc_field) | ||
| # generate a new header with only columns within frequency min/max | ||
| h = tuple([ | ||
| i for i in list(etl.header(t2)) if (int(i) >= frequency_min and int(i) <= frequency_max) | ||
| ]) | ||
| h = tuple( | ||
| i | ||
| for i in list(etl.header(t2)) | ||
| if (int(i) >= frequency_min and int(i) <= frequency_max) | ||
| ) | ||
|
|
||
|
Comment on lines
-92
to
+97
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # for events within freq range, convert to cm, adjust for future rainfall | ||
| t3 = etl\ | ||
| .cut(t2, h)\ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,7 @@ def prep_cn_raster( | |
| # make the DEM an ArcPy Raster object, so we can get the raster properties | ||
| if not isinstance(dem,Raster): | ||
| dem = Raster(dem) | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| msg("Clipping...") | ||
| # clip the curve number raster, since it is likely for a broader study area | ||
| clipped_cn = so("cn_clipped") | ||
|
|
@@ -93,15 +93,12 @@ def prep_cn_raster( | |
| clipping_geometry="NONE", | ||
| maintain_clipping_extent="NO_MAINTAIN_EXTENT" | ||
| ) | ||
|
|
||
| # set the snap raster for subsequent operations | ||
| env.snapRaster = dem | ||
|
|
||
| # reproject and resample he curve number raster to match the dem | ||
| if not out_cn_raster: | ||
| prepped_cn = so("cn_prepped") | ||
| else: | ||
| prepped_cn = out_cn_raster | ||
| prepped_cn = so("cn_prepped") if not out_cn_raster else out_cn_raster | ||
| msg("Projecting and Resampling...") | ||
| ProjectRaster_management( | ||
| in_raster=clipped_cn, | ||
|
|
@@ -110,7 +107,7 @@ def prep_cn_raster( | |
| resampling_type="NEAREST", | ||
| cell_size=dem.meanCellWidth | ||
| ) | ||
|
|
||
| return { | ||
| "curve_number_raster": Raster(prepped_cn) | ||
| } | ||
|
|
@@ -154,11 +151,11 @@ def build_cn_raster( | |
| env.cellSize = reference_raster.meanCellWidth | ||
| env.extent = reference_raster | ||
| env.outputCoordinateSystem = reference_raster | ||
|
|
||
| cs = env.outputCoordinateSystem.exportToString() | ||
|
|
||
| # SOILS ------------------------------------- | ||
|
|
||
|
Comment on lines
-157
to
+158
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| msg("Processing Soils...") | ||
| # read the soils polygon into a raster, get list(set()) of all cell values from the landcover raster | ||
| soils_raster_path = so("soils_raster") | ||
|
|
@@ -196,14 +193,16 @@ def build_cn_raster( | |
| msg("Processing Lookup Table...") | ||
| # read the lookup csv, clean it up, and use the lookups from above to limit it to just | ||
| # those values in the rasters | ||
| t = etl\ | ||
| .fromcsv(lookup_csv)\ | ||
| .convert('utc', int)\ | ||
| .convert('cn', int)\ | ||
| .select('soil', lambda v: v in lookup_from_soils.keys())\ | ||
| .convert('soil', lookup_from_soils)\ | ||
| t = ( | ||
| etl.fromcsv(lookup_csv) | ||
| .convert('utc', int) | ||
| .convert('cn', int) | ||
| .select('soil', lambda v: v in lookup_from_soils) | ||
| .convert('soil', lookup_from_soils) | ||
| .select('utc', lambda v: v in landcover_values) | ||
|
|
||
| ) | ||
|
|
||
|
|
||
| # This gets us a table where we the landcover class (as a number) corresponding to the | ||
| # correct value in the converted soil raster, with the corresponding curve number. | ||
|
|
||
|
|
@@ -229,7 +228,7 @@ def build_cn_raster( | |
| resampling_type="NEAREST", | ||
| cell_size=env.cellSize | ||
| ) | ||
|
|
||
| # cn_raster.save(out_cn_raster) | ||
| return out_cn_raster | ||
|
|
||
|
|
@@ -329,12 +328,12 @@ def derive_data_from_catchments( | |
| # store the results, keyed by a catchment ID (int) that comes from the | ||
| # catchments layer gridcode | ||
| results = {} | ||
|
|
||
| # make a raster object with the catchment raster | ||
| if not isinstance(catchment_areas,Raster): | ||
| c = Raster(catchment_areas) | ||
| else: | ||
| if isinstance(catchment_areas,Raster): | ||
| c = catchment_areas | ||
|
Comment on lines
-332
to
337
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| else: | ||
| c = Raster(catchment_areas) | ||
| # if the catchment raster does not have an attribute table, build one | ||
| if not c.hasRAT: | ||
| BuildRasterAttributeTable_management(c, "Overwrite") | ||
|
|
@@ -350,7 +349,7 @@ def derive_data_from_catchments( | |
| with SearchCursor(catchment_table, [raster_field]) as catchments: | ||
|
|
||
| # TODO: implement multi-processing for this loop. | ||
|
|
||
| ResetProgressor() | ||
| SetProgressor('step', "Mapping flow length for catchments", 0, catchment_count, 1) | ||
| # msg("Mapping flow length for catchments") | ||
|
|
@@ -365,7 +364,7 @@ def derive_data_from_catchments( | |
| flow_direction_raster, | ||
| length_conv_factor | ||
| ) | ||
| if this_id in results.keys(): | ||
| if this_id in results: | ||
| results[this_id]["max_fl"] = clean(fl_max) | ||
| else: | ||
| results[this_id] = {"max_fl": clean(fl_max)} | ||
|
|
@@ -381,11 +380,11 @@ def derive_data_from_catchments( | |
| for r in c: | ||
| this_id = r[0] | ||
| this_area = r[1] | ||
| if this_id in results.keys(): | ||
| if this_id in results: | ||
| results[this_id]["avg_cn"] = clean(this_area) | ||
| else: | ||
| results[this_id] = {"avg_cn": clean(this_area)} | ||
|
|
||
| # calculate average slope within each catchment for all catchments | ||
| table_slopes = so("slopes_zs_table","timestamp","fgdb") | ||
| msg("Slopes Table: {0}".format(table_slopes)) | ||
|
|
@@ -395,22 +394,22 @@ def derive_data_from_catchments( | |
| for r in c: | ||
| this_id = r[0] | ||
| this_area = r[1] | ||
| if this_id in results.keys(): | ||
| if this_id in results: | ||
| results[this_id]["avg_slope"] = clean(this_area) | ||
| else: | ||
| results[this_id] = {"avg_slope": clean(this_area)} | ||
|
|
||
| # calculate area of each catchment | ||
| #ZonalGeometryAsTable(catchment_areas,"Value","output_table") # crashes like an mfer | ||
| cp = so("catchmentpolygons","timestamp","in_memory") | ||
| #RasterToPolygon copies our ids from raster_field into "gridcode" | ||
| RasterToPolygon_conversion(catchment_areas, cp, "NO_SIMPLIFY", raster_field) | ||
|
|
||
| # Dissolve the converted polygons, since some of the raster zones may have corner-corner links | ||
| if not out_catchment_polygons: | ||
| cpd = so("catchmentpolygonsdissolved","timestamp","in_memory") | ||
| else: | ||
| if out_catchment_polygons: | ||
| cpd = out_catchment_polygons | ||
| else: | ||
| cpd = so("catchmentpolygonsdissolved","timestamp","in_memory") | ||
| Dissolve_management( | ||
| in_features=cp, | ||
| out_feature_class=cpd, | ||
|
|
@@ -423,29 +422,29 @@ def derive_data_from_catchments( | |
| for r in c: | ||
| this_id = r[0] | ||
| this_area = r[1] * area_conv_factor | ||
| if this_id in results.keys(): | ||
| if this_id in results: | ||
| results[this_id]["area_up"] = clean(this_area) | ||
| else: | ||
| results[this_id] = {"area_up": clean(this_area)} | ||
|
|
||
| # flip results object into a records-style array of dictionaries | ||
| # (this makes conversion to table later on simpler) | ||
| # msg(results,"warning") | ||
| records = [] | ||
| for k in results.keys(): | ||
| for k, v in results.items(): | ||
| record = { | ||
| "area_up":0, | ||
| "avg_slope":0, | ||
| "max_fl":0, | ||
| "avg_cn":0, | ||
| "tc_hr":0 | ||
| } | ||
| for each_result in record.keys(): | ||
| if each_result in results[k].keys(): | ||
| for each_result in record: | ||
| if each_result in v.keys(): | ||
| record[each_result] = results[k][each_result] | ||
| record["id"] = k | ||
| records.append(record) | ||
|
|
||
| if out_catchment_polygons: | ||
| return records, cpd | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,15 +57,13 @@ def so(prefix, suffix="random", where="fgdb"): | |
| """ | ||
|
|
||
| # set workspace location | ||
| if where == "in_memory": | ||
| location = "in_memory" | ||
| elif where == "fgdb": | ||
| location = env.scratchGDB | ||
| elif where == "folder": | ||
| if where == "folder": | ||
| location = env.scratchFolder | ||
| elif where == "in_memory": | ||
| location = "in_memory" | ||
| else: | ||
| location = env.scratchGDB | ||
|
|
||
|
Comment on lines
-60
to
+66
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # create and return full path | ||
| if suffix == "unique": | ||
| return CreateUniqueName(prefix, location) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ def lite(inlets, flow_dir_raster, slope_raster, cn_raster, precip_table_noaa, ou | |
| """ | ||
| Peak-Flow Calculator "Lite". Use with pre-calculated slope and flow direction rasters. | ||
| """ | ||
| output_data = main( | ||
| return main( | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| inlets=inlets, | ||
| pour_point_field=pour_point_field, | ||
| flow_dir_raster=flow_dir_raster, | ||
|
|
@@ -40,7 +40,6 @@ def lite(inlets, flow_dir_raster, slope_raster, cn_raster, precip_table_noaa, ou | |
| precip_table_noaa=precip_table_noaa, | ||
| output=output | ||
| ) | ||
| return output_data | ||
|
|
||
| @calc.command() | ||
| @calc.argument('inlets') | ||
|
|
@@ -67,7 +66,7 @@ def full(inlets, dem, cn_raster, precip_table_noaa, pour_point_field, output): | |
| flow_dir_raster = derived_rasters['flow_direction_raster'] | ||
| slope_raster = derived_rasters['slope_raster'] | ||
|
|
||
| output_data = main( | ||
| return main( | ||
|
Comment on lines
-70
to
+69
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| inlets=inlets, | ||
| pour_point_field=pour_point_field, | ||
| flow_dir_raster=flow_dir_raster, | ||
|
|
@@ -76,9 +75,6 @@ def full(inlets, dem, cn_raster, precip_table_noaa, pour_point_field, output): | |
| precip_table_noaa=precip_table_noaa, | ||
| output=output | ||
| ) | ||
| return output_data | ||
|
|
||
| return | ||
|
|
||
| @calc.command() | ||
| @calc.argument('original_cn_raster') | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
calculate_tcrefactored with the following changes:inline-immediately-returned-variable)