Skip to content
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

Calculate SedNitr value from SoilN raster #2261

Merged
merged 2 commits into from
Sep 15, 2017
Merged
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
2 changes: 1 addition & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
guest: 2375,
host: 2375
}.merge(VAGRANT_NETWORK_OPTIONS)
# SJS
# Geoprocessing Service
worker.vm.network "forwarded_port", {
guest: 8090,
host: 8090
Expand Down
6 changes: 3 additions & 3 deletions src/mmw/apps/modeling/geoprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def geoprocess(data, retry=None):
'Details: {}'.format(response.text))


def parse(sjs_result):
def parse(result):
"""
Converts raw JSON results from Spark JobServer to dictionary of tuples

Expand All @@ -124,10 +124,10 @@ def parse(sjs_result):
(4, 5): 6
}

:param sjs_result: Dictionary mapping strings like 'List(a,b,c)' to ints
:param result: Dictionary mapping strings like 'List(a,b,c)' to ints
:return: Dictionary mapping tuples of ints to ints
"""
return {make_tuple(key[4:]): val for key, val in sjs_result.items()}
return {make_tuple(key[4:]): val for key, val in result.items()}


def to_one_ring_multipolygon(area_of_interest):
Expand Down
64 changes: 42 additions & 22 deletions src/mmw/apps/modeling/mapshed/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ def collect_data(geop_result, geojson):

z['P'] = p_factors(z['AvSlope'], ag_lscp)

z['SedNitr'] = geop_result['soiln'] * 4.0

return z


@shared_task(throws=Exception)
def nlcd_streams(sjs_result):
def nlcd_streams(result):
"""
From a dictionary mapping NLCD codes to the count of stream pixels on
each, return a dictionary with keys 'ag_stream_pct', 'low_urban_stream_pct'
Expand Down Expand Up @@ -187,13 +189,12 @@ def nlcd_streams(sjs_result):
This task should be used as a template for making other geoprocessing
post-processing tasks, to be used in geop_tasks.
"""
if 'error' in sjs_result:
raise Exception('[nlcd_streams] {}'.format(sjs_result['error']))
if 'error' in result:
raise Exception('[nlcd_streams] {}'.format(result['error']))

# Parse SJS results
# This can't be done in mapshed_finish because the keys may be tuples,
# This can't be done in geoprocessing.run because the keys may be tuples,
# which are not JSON serializable and thus can't be shared between tasks
result = parse(sjs_result)
result = parse(result)

ag_count = sum(result.get(nlcd, 0) for nlcd in AG_NLCD_CODES)
low_urban_count = sum(result.get(nlcd, 0) for nlcd in [21, 22])
Expand All @@ -220,15 +221,15 @@ def nlcd_streams(sjs_result):


@shared_task(throws=Exception)
def nlcd_streams_drb(sjs_result):
def nlcd_streams_drb(result):
"""
This callback is run when the geometry falls within the DRB. We calculate
the percentage of DRB streams in each land use type.
"""
if 'error' in sjs_result:
raise Exception('[nlcd_streams_drb] {}'.format(sjs_result['error']))
if 'error' in result:
raise Exception('[nlcd_streams_drb] {}'.format(result['error']))

result = parse(sjs_result)
result = parse(result)
total = sum(result.values())

lu_stream_pct_drb = [0.0] * NLU
Expand All @@ -243,7 +244,7 @@ def nlcd_streams_drb(sjs_result):


@shared_task(throws=Exception)
def nlcd_soils(sjs_result):
def nlcd_soils(result):
"""
Results are expected to be in the format:
{
Expand All @@ -253,10 +254,10 @@ def nlcd_soils(sjs_result):
We calculate a number of values relying on various combinations
of these raster datasets.
"""
if 'error' in sjs_result:
raise Exception('[nlcd_soils] {}'.format(sjs_result['error']))
if 'error' in result:
raise Exception('[nlcd_soils] {}'.format(result['error']))

ngt_count = parse(sjs_result)
ngt_count = parse(result)

# Raise exception if no NLCD values
if len(ngt_count.values()) == 0:
Expand All @@ -283,15 +284,15 @@ def nlcd_soils(sjs_result):


@shared_task(throws=Exception)
def gwn(sjs_result):
def gwn(result):
"""
Derive Groundwater Nitrogen and Phosphorus
"""
if 'error' in sjs_result:
if 'error' in result:
raise Exception('[gwn] {}'
.format(sjs_result['error']))
.format(result['error']))

result = parse(sjs_result)
result = parse(result)
gr_nitr_conc, gr_phos_conc = groundwater_nitrogen_conc(result)

return {
Expand All @@ -301,23 +302,41 @@ def gwn(sjs_result):


@shared_task(throws=Exception)
def avg_awc(sjs_result):
def avg_awc(result):
"""
Get `AvgAwc` from MMW-Geoprocessing endpoint

Original at Class1.vb@1.3.0:4150
"""
if 'error' in sjs_result:
if 'error' in result:
raise Exception('[awc] {}'
.format(sjs_result['error']))
.format(result['error']))

result = parse(sjs_result)
result = parse(result)

return {
'avg_awc': result.values()[0]
}


@shared_task(throws=Exception)
def soiln(result):
"""
Get `SoilN` from MMW-Geoprocessing endpoint

Originally a static value of 2000 at Class1.vb@1.3.0:9587
"""
if 'error' in result:
raise Exception('[soiln] {}'
.format(result['error']))

result = parse(result)

return {
'soiln': result.values()[0]
}


@shared_task(throws=Exception)
def nlcd_slope(result):
if 'error' in result:
Expand Down Expand Up @@ -413,6 +432,7 @@ def nlcd_kfactor(result):
def geoprocessing_chains(aoi, wkaoi, errback):
task_defs = [
('nlcd_soils', nlcd_soils, {'polygon': [aoi]}),
('soiln', soiln, {'polygon': [aoi]}),
('gwn', gwn, {'polygon': [aoi]}),
('avg_awc', avg_awc, {'polygon': [aoi]}),
('nlcd_slope', nlcd_slope, {'polygon': [aoi]}),
Expand Down
12 changes: 12 additions & 0 deletions src/mmw/mmw/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,18 @@ def get_env_setting(setting):
'operationType': 'RasterGroupedAverage',
'zoom': 0
}
},
'soiln': {
'input': {
'polygon': [],
'polygonCRS': 'LatLng',
'rasters': [],
'targetRaster': 'soiln-epsg5070',
'pixelIsArea': True,
'rasterCRS': 'ConusAlbers',
'operationType': 'RasterGroupedAverage',
'zoom': 0
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/mmw/mmw/settings/gwlfe_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
'SoilPFlag': b.YES, # Flag: Soil P Layer Detected (0 No; 1 Yes)
'GWNFlag': b.YES, # Flag: Groundwater N Layer Detected (0 No; 1 Yes)
'SedAAdjust': 1.4, # Default Percent ET
'SedNitr': 2000, # Soil Concentration: N (mg/l)
'BankNFrac': 0.25, # % Bank N Fraction (0 - 1)
'BankPFrac': 0.25, # % Bank P Fraction (0 - 1)
'ManuredAreas': 2, # Manure Spreading Periods (Default = 2)
Expand Down