-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add process to average over shape (#251)
* Added average_shape process to perform average over shape * Added tests in rook/tests for wps_average_shape * Fix alignment logic to it does not consider polygon averages aligned * Set valid polygon for average_shape test * Update requirements and environments to require latest version of clisops and daops supporting the spatial averager * Get tests working for shape average operation. * Adjust title * updated daops requirements following 0.11.0 release. * pep8 * removed artefact from merge --------- Co-authored-by: charlesgauthier-udm <[email protected]> Co-authored-by: charlesgauthier-udm <[email protected]>
- Loading branch information
1 parent
b5c5332
commit ee091a7
Showing
11 changed files
with
222 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import logging | ||
import os | ||
|
||
from pywps import FORMATS, ComplexOutput, Format, LiteralInput, Process, ComplexInput | ||
from pywps.app.Common import Metadata | ||
from pywps.app.exceptions import ProcessError | ||
from pywps.inout.outputs import MetaFile, MetaLink4 | ||
|
||
from ..director import wrap_director | ||
from ..utils.input_utils import parse_wps_input | ||
from ..utils.metalink_utils import build_metalink | ||
from ..utils.response_utils import populate_response | ||
from ..utils.average_utils import run_average_by_shape | ||
|
||
LOGGER = logging.getLogger() | ||
|
||
|
||
class AverageByShape(Process): | ||
def __init__(self): | ||
inputs = [ | ||
LiteralInput( | ||
"collection", | ||
"Collection", | ||
abstract="A dataset identifier or list of comma separated identifiers. " | ||
"Example: c3s-cmip5.output1.ICHEC.EC-EARTH.historical.day.atmos.day.r1i1p1.tas.latest", | ||
data_type="string", | ||
min_occurs=1, | ||
max_occurs=1, | ||
), | ||
ComplexInput( | ||
"shape", | ||
"Vector Shape", | ||
abstract="An ESRI Shapefile, GML, GeoPackage, JSON or GeoJSON file." | ||
" The ESRI Shapefile must be zipped and contain the .shp, .shx, and .dbf.", | ||
supported_formats=[ | ||
FORMATS.GML, | ||
FORMATS.GEOJSON, | ||
FORMATS.SHP, | ||
FORMATS.JSON, | ||
FORMATS.ZIP, | ||
], | ||
min_occurs=1, | ||
max_occurs=1, | ||
), | ||
LiteralInput( | ||
"pre_checked", | ||
"Pre-Checked", | ||
data_type="boolean", | ||
abstract="Use checked data only.", | ||
default="0", | ||
min_occurs=1, | ||
max_occurs=1, | ||
), | ||
LiteralInput( | ||
"apply_fixes", | ||
"Apply Fixes", | ||
data_type="boolean", | ||
abstract="Apply fixes to datasets.", | ||
default="1", | ||
min_occurs=1, | ||
max_occurs=1, | ||
), | ||
] | ||
outputs = [ | ||
ComplexOutput( | ||
"output", | ||
"METALINK v4 output", | ||
abstract="Metalink v4 document with references to NetCDF files.", | ||
as_reference=True, | ||
supported_formats=[FORMATS.META4], | ||
), | ||
ComplexOutput( | ||
"prov", | ||
"Provenance", | ||
abstract="Provenance document using W3C standard.", | ||
as_reference=True, | ||
supported_formats=[FORMATS.JSON], | ||
), | ||
ComplexOutput( | ||
"prov_plot", | ||
"Provenance Diagram", | ||
abstract="Provenance document as diagram.", | ||
as_reference=True, | ||
supported_formats=[ | ||
Format("image/png", extension=".png", encoding="base64") | ||
], | ||
), | ||
] | ||
|
||
super(AverageByShape, self).__init__( | ||
self._handler, | ||
identifier="average_shape", | ||
title="Average over polygonal shape", | ||
abstract="Run averaging over a specified shape on climate model data.", | ||
metadata=[ | ||
Metadata("DAOPS", "https://github.com/roocs/daops"), | ||
], | ||
version="1.0", | ||
inputs=inputs, | ||
outputs=outputs, | ||
store_supported=True, | ||
status_supported=True, | ||
) | ||
|
||
def _handler(self, request, response): | ||
# show me the environment used by the process in debug mode | ||
LOGGER.debug(f"Environment used in average_shape: {os.environ}") | ||
|
||
collection = parse_wps_input( | ||
request.inputs, "collection", as_sequence=True, must_exist=True | ||
) | ||
|
||
inputs = { | ||
"collection": collection, | ||
"output_dir": self.workdir, | ||
"apply_fixes": parse_wps_input(request.inputs, "apply_fixes", default=True), | ||
"pre_checked": parse_wps_input( | ||
request.inputs, "pre_checked", default=False | ||
), | ||
"shape": parse_wps_input(request.inputs, "shape", default=None), | ||
} | ||
|
||
# Let the director manage the processing or redirection to original files | ||
director = wrap_director(collection, inputs, run_average_by_shape) | ||
|
||
ml4 = build_metalink( | ||
"average-shape-result", | ||
"Averaging by shape result as NetCDF files.", | ||
self.workdir, | ||
director.output_uris, | ||
) | ||
|
||
populate_response( | ||
response, "average_shape", self.workdir, inputs, collection, ml4 | ||
) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pytest | ||
|
||
from pywps import Service | ||
from pywps.tests import assert_process_exception, assert_response_success, client_for | ||
from pywps.app.exceptions import ProcessError | ||
from rook.processes.wps_average_shape import AverageByShape | ||
from shapely import Polygon | ||
import geopandas as gpd | ||
import xarray as xr | ||
|
||
from .common import PYWPS_CFG, get_output, extract_paths_from_metalink | ||
|
||
|
||
POLY = Polygon([[5.8671874999999996, 57.326521225217064], | ||
[-15.468749999999998, 48.45835188280866], | ||
[-16.171875, 24.84656534821976], | ||
[-3.8671874999999996, 13.581920900545844], | ||
[21.796875, 25.799891182088334], | ||
[22.8515625, 52.482780222078226], | ||
[5.8671874999999996, 57.326521225217064]]) | ||
|
||
|
||
def test_wps_average_shape_cmip6(tmp_path): | ||
# Save POLY to tmpdir | ||
tmp_poly_path = tmp_path / "tmppoly.json" | ||
gpd.GeoDataFrame([{'geometry': POLY}]).to_file(tmp_poly_path) | ||
|
||
# test the case where the inventory is used | ||
client = client_for(Service(processes=[AverageByShape()], cfgfiles=[PYWPS_CFG])) | ||
datainputs = "collection=c3s-cmip6.ScenarioMIP.INM.INM-CM5-0.ssp245.r1i1p1f1.Amon.rlds.gr1.v20190619" | ||
datainputs += f";shape={tmp_poly_path}" | ||
resp = client.get( | ||
f"?service=WPS&request=Execute&version=1.0.0&identifier=average_shape&datainputs={datainputs}" | ||
) | ||
assert_response_success(resp) | ||
assert "output" in get_output(resp.xml) | ||
assert_geom_created(path=get_output(resp.xml)["output"]) | ||
|
||
|
||
def assert_geom_created(path): | ||
assert "meta4" in path | ||
paths = extract_paths_from_metalink(path) | ||
assert len(paths) > 0 | ||
ds = xr.open_dataset(paths[0]) | ||
assert "geom" in ds.coords | ||
|
||
|
||
def test_wps_average_no_shape(): | ||
client = client_for(Service(processes=[AverageByShape()], cfgfiles=[PYWPS_CFG])) | ||
datainputs = "collection=c3s-cmip6.ScenarioMIP.INM.INM-CM5-0.ssp245.r1i1p1f1.Amon.rlds.gr1.v20190619" | ||
resp = client.get( | ||
f"?service=WPS&request=Execute&version=1.0.0&identifier=average_shape&datainputs={datainputs}" | ||
) | ||
assert_process_exception(resp, code="MissingParameterValue") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters