Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/logic/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ def calculate_tc(
"""
if not mean_slope:
mean_slope = 0.00001
tc_hr = const_a * math.pow(max_flow_length, const_b) * math.pow((mean_slope / 100), const_c)
return tc_hr
return (
const_a
* math.pow(max_flow_length, const_b)
* math.pow((mean_slope / 100), const_c)
)
Comment on lines -39 to +43
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function calculate_tc refactored with the following changes:


def calculate_peak_flow(
catchment_area_sqkm,
Expand Down
15 changes: 9 additions & 6 deletions core/logic/data_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function precip_table_etl_cnrccep refactored with the following changes:

next(g)

k=1
Expand All @@ -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(
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function precip_table_etl_noaa refactored with the following changes:

# for events within freq range, convert to cm, adjust for future rainfall
t3 = etl\
.cut(t2, h)\
Expand Down
73 changes: 36 additions & 37 deletions core/logic/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function prep_cn_raster refactored with the following changes:

msg("Clipping...")
# clip the curve number raster, since it is likely for a broader study area
clipped_cn = so("cn_clipped")
Expand All @@ -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,
Expand All @@ -110,7 +107,7 @@ def prep_cn_raster(
resampling_type="NEAREST",
cell_size=dem.meanCellWidth
)

return {
"curve_number_raster": Raster(prepped_cn)
}
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function build_cn_raster refactored with the following changes:

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")
Expand Down Expand Up @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function derive_data_from_catchments refactored with the following changes:

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")
Expand All @@ -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")
Expand All @@ -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)}
Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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:
Expand Down
10 changes: 4 additions & 6 deletions core/logic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function so refactored with the following changes:

  • Simplify conditional into switch-like form (switch)

# create and return full path
if suffix == "unique":
return CreateUniqueName(prefix, location)
Expand Down
8 changes: 2 additions & 6 deletions core/peakflow_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function lite refactored with the following changes:

inlets=inlets,
pour_point_field=pour_point_field,
flow_dir_raster=flow_dir_raster,
Expand All @@ -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')
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function full refactored with the following changes:

inlets=inlets,
pour_point_field=pour_point_field,
flow_dir_raster=flow_dir_raster,
Expand All @@ -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')
Expand Down