-
Notifications
You must be signed in to change notification settings - Fork 15
Process for averaging over a polygon #315
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
Draft
huard
wants to merge
3
commits into
bird-house:master
Choose a base branch
from
huard:fix-314
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| from pywps import Process | ||
| from .subset_base import Subsetter, resource, variable, start, end, output, metalink, shape, mosaic | ||
|
|
||
|
|
||
| class AveragePolygonProcess(Process, Subsetter): | ||
| """Subset a NetCDF file using WFS geometry.""" | ||
|
|
||
| def __init__(self): | ||
| inputs = [resource, shape, mosaic, start, end, variable] | ||
| outputs = [output, metalink] | ||
|
|
||
| super().__init__( | ||
| self._handler, | ||
| identifier='average-polygon', | ||
| title='Average over polygon', | ||
| version='0.1', | ||
| abstract=('Return the average of the data for which grid cells intersect the ' | ||
| 'selected polygon for each input dataset as well as' | ||
| 'the time range selected.'), | ||
| inputs=inputs, | ||
| outputs=outputs, | ||
| status_supported=True, | ||
| store_supported=True, | ||
| ) |
This file contains hidden or 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,74 @@ | ||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| from pywps import Process, FORMATS | ||
| from pywps.inout.outputs import MetaFile, MetaLink4 | ||
|
|
||
| from .subset_base import Subsetter, resource, variable, start, end, output, metalink, typename, \ | ||
| featureids, geoserver, mosaic | ||
|
|
||
| import ocgis | ||
| import ocgis.exc | ||
|
|
||
|
|
||
| class AverageWFSPolygonProcess(Process, Subsetter): | ||
| """Subset a NetCDF file using WFS geometry.""" | ||
|
|
||
| def __init__(self): | ||
| inputs = [resource, typename, featureids, geoserver, mosaic, start, end, variable] | ||
| outputs = [output, metalink] | ||
|
|
||
| super(AverageWFSPolygonProcess, self).__init__( | ||
| self._handler, | ||
| identifier='average-wfs-polygon', | ||
| title='Average over polygon', | ||
| version='0.1', | ||
| abstract=('Return the average of the data for which grid cells intersect the ' | ||
| 'selected polygon for each input dataset as well as' | ||
| 'the time range selected.'), | ||
| inputs=inputs, | ||
| outputs=outputs, | ||
| status_supported=True, | ||
| store_supported=True, | ||
| ) | ||
|
|
||
| def _handler(self, request, response): | ||
|
|
||
| # Gather geometries, aggregate if mosaic is True. | ||
| geoms = self.parse_feature(request) | ||
| dr = self.parse_daterange(request) | ||
|
|
||
| ml = MetaLink4('subset', workdir=self.workdir) | ||
|
|
||
| if geoms.get('_shp_', None) is not None: | ||
| geom = geoms['_shp_'] | ||
| else: | ||
| geom = [g for g in geoms.values()] | ||
|
|
||
| for res in self.parse_resources(request): | ||
| variables = self.parse_variable(request, res) | ||
| rd = ocgis.RequestDataset(res, variables) | ||
| prefix = Path(res).stem | ||
|
|
||
| try: | ||
| ops = ocgis.OcgOperations( | ||
| dataset=rd, geom=geom, | ||
| spatial_operation='clip', aggregate=True, | ||
| time_range=dr, output_format='nc', | ||
| interpolate_spatial_bounds=True, | ||
| prefix=prefix, dir_output=tempfile.mkdtemp(dir=self.workdir)) | ||
|
|
||
| out = ops.execute() | ||
|
|
||
| mf = MetaFile(prefix, fmt=FORMATS.NETCDF) | ||
| mf.file = out | ||
| ml.append(mf) | ||
|
|
||
| except ocgis.exc.ExtentError: | ||
| continue | ||
|
|
||
| response.outputs['output'].file = ml.files[0].file | ||
| response.outputs['metalink'].data = ml.xml | ||
| response.update_status("Completed", 100) | ||
|
|
||
| return response |
This file contains hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| from pywps import Service | ||
| from pywps.tests import client_for, assert_response_success | ||
|
|
||
| from .common import get_output | ||
| from flyingpigeon.processes.wps_average_wfs_polygon import AverageWFSPolygonProcess | ||
|
|
||
|
|
||
| def test_wps_subset(): | ||
| client = client_for(Service(processes=[AverageWFSPolygonProcess()])) | ||
|
|
||
| datainputs = "resource=@xlink:href={nc};" \ | ||
| "typename={typename};" \ | ||
| "geoserver={geoserver};" \ | ||
| "featureids={fid1};" \ | ||
| "featureids={fid2};" \ | ||
| "mosaic={mosaic}" | ||
|
|
||
| inputs = dict(nc="https://pavics.ouranos.ca/twitcher/ows/proxy/thredds/dodsC/birdhouse/nrcan" | ||
| "/nrcan_northamerica_monthly" | ||
| "/tasmax/nrcan_northamerica_monthly_2015_tasmax.nc", | ||
| typename="public:USGS_HydroBASINS_lake_na_lev12", | ||
| geoserver="https://pavics.ouranos.ca/geoserver/wfs", | ||
| fid1="USGS_HydroBASINS_lake_na_lev12.67061", | ||
| fid2="USGS_HydroBASINS_lake_na_lev12.67088", | ||
| mosaic=False) | ||
|
|
||
| resp = client.get( | ||
| "?service=WPS&request=Execute&version=1.0.0&identifier=average-wfs-polygon&datainputs={}".format( | ||
| datainputs.format(**inputs))) | ||
| assert_response_success(resp) | ||
|
|
||
| out = get_output(resp.xml) | ||
| assert 'metalink' in out | ||
|
|
||
| inputs['mosaic'] = True | ||
| resp = client.get( | ||
| "?service=WPS&request=Execute&version=1.0.0&identifier=average-wfs-polygon&datainputs={}".format( | ||
| datainputs.format(**inputs))) | ||
| assert_response_success(resp) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Taking this out is causing Travis CI to find all written tests, including deprecated ones. Might want to place this back in.