Skip to content

Commit

Permalink
better handling of optional inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcaron committed May 21, 2019
1 parent 991bd6b commit fb461fc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
6 changes: 6 additions & 0 deletions finch/processes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def _handler_wrapped(self, request, response):
self.sentry_configure_scope(request)
return self.old_handler(request, response)

def get_input_or_none(self, inputs, identifier):
try:
return inputs[identifier][0].data
except KeyError:
return None

def try_opendap(self, input, chunks=None):
"""Try to open the file as an OPeNDAP url and chunk it. If OPeNDAP fails, access the file directly. In both
cases, return an xarray.Dataset.
Expand Down
21 changes: 8 additions & 13 deletions finch/processes/wps_xsubset_bccaqv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from finch.processes.subset import SubsetProcess
from finch.processes.utils import get_bccaqv2_inputs

ALL = "ALL"


class SubsetBCCAQV2Process(SubsetBboxProcess):
"""Subset a NetCDF file using bounding box geometry."""
Expand All @@ -21,16 +19,18 @@ def __init__(self):
"NetCDF Variable",
abstract="Name of the variable in the NetCDF file.",
data_type="string",
default=ALL,
allowed_values=[ALL, "tasmin", "tasmax", "pr"],
default=None,
min_occurs=0,
allowed_values=["tasmin", "tasmax", "pr"],
),
LiteralInput(
"rcp",
"RCP Scenario",
abstract="Representative Concentration Pathway (RCP)",
data_type="string",
default=ALL,
allowed_values=[ALL, "rcp26", "rcp45", "rcp85"],
default=None,
min_occurs=0,
allowed_values=["rcp26", "rcp45", "rcp85"],
),
LiteralInput(
"lat0",
Expand Down Expand Up @@ -134,8 +134,8 @@ def __init__(self):
def _handler(self, request: WPSRequest, response: ExecuteResponse):
self.write_log("Processing started", response, 5)

variable = request.inputs["variable"][0].data
rcp = request.inputs["rcp"][0].data
variable = self.get_input_or_none(request.inputs, "variable")
rcp = self.get_input_or_none(request.inputs, "rcp")
output_format = request.inputs["output_format"][0].data

output_filename = f"BCCAQv2_subset_{rcp}_{variable}"
Expand All @@ -146,11 +146,6 @@ def _handler(self, request: WPSRequest, response: ExecuteResponse):
response.outputs["output"].file = output_csv
return response

if variable == ALL:
variable = None
if rcp == ALL:
rcp = None

self.write_log("Fetching BCCAQv2 datasets", response, 6)
request.inputs = get_bccaqv2_inputs(request.inputs, variable, rcp)

Expand Down
12 changes: 4 additions & 8 deletions finch/processes/wps_xsubsetbbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from finch.processes.subset import SubsetProcess
import logging
from contextlib import suppress


LOGGER = logging.getLogger("PYWPS")
Expand Down Expand Up @@ -134,16 +133,13 @@ def __init__(self):

def subset(self, wps_inputs, response, start_percentage=10, end_percentage=85) -> MetaLink4:
lon0 = wps_inputs["lon0"][0].data
lon1 = wps_inputs["lon1"][0].data
lat0 = wps_inputs["lat0"][0].data
lat1 = wps_inputs["lat1"][0].data
lon1 = self.get_input_or_none(wps_inputs, "lon1")
lat1 = self.get_input_or_none(wps_inputs, "lat1")
# dt0 = wps_inputs['dt0'][0].data or None
# dt1 = wps_inputs['dt1'][0].data or None
y0, y1 = None, None
with suppress(KeyError):
y0 = wps_inputs["y0"][0].data
with suppress(KeyError):
y1 = wps_inputs["y1"][0].data
y0 = self.get_input_or_none(wps_inputs, "y0")
y1 = self.get_input_or_none(wps_inputs, "y1")
variables = [r.data for r in wps_inputs.get("variable", [])]

nones = [lat1 is None, lon1 is None]
Expand Down
9 changes: 2 additions & 7 deletions finch/processes/wps_xsubsetpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from contextlib import suppress

from pywps import LiteralInput, ComplexInput, ComplexOutput, FORMATS
from pywps.inout.outputs import MetaLink4
from xclim.subset import subset_gridpoint
Expand Down Expand Up @@ -115,11 +113,8 @@ def subset(self, wps_inputs, response, start_percentage=10, end_percentage=85) -
lat = wps_inputs["lat"][0].data
# dt0 = wps_inputs['dt0'][0].data or None
# dt1 = wps_inputs['dt1'][0].data or None
y0, y1 = None, None
with suppress(KeyError):
y0 = wps_inputs["y0"][0].data
with suppress(KeyError):
y1 = wps_inputs["y1"][0].data
y0 = self.get_input_or_none(wps_inputs, "y0")
y1 = self.get_input_or_none(wps_inputs, "y1")
variables = [r.data for r in wps_inputs.get("variable", [])]

n_files = len(wps_inputs["resource"])
Expand Down

0 comments on commit fb461fc

Please sign in to comment.