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

Process for averaging over a polygon #315

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions flyingpigeon/processes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

# from .wps_say_hello import SayHello
from .wps_average_wfs_polygon import AverageWFSPolygonProcess
from .wps_subset_wfs_polygon import SubsetWFSPolygonProcess
from .wps_subset_bbox import SubsetBboxProcess
from .wps_subset_continents import SubsetcontinentProcess
Expand All @@ -17,6 +18,7 @@

processes = [
# SayHello(),
AverageWFSPolygonProcess(),
SubsetWFSPolygonProcess(),
SubsetBboxProcess(),
SubsetcontinentProcess(),
Expand Down
29 changes: 29 additions & 0 deletions flyingpigeon/processes/subset_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@
as_reference=True,
supported_formats=[FORMATS.META4])

typename = LiteralInput('typename',
'TypeName',
abstract='Name of the layer in GeoServer.',
data_type='string',
min_occurs=0,
max_occurs=1)

featureids = LiteralInput('featureids',
'Feature Ids',
abstract='fid(s) of the feature in the layer.',
data_type='string',
min_occurs=0,
max_occurs=1000)

geoserver = LiteralInput('geoserver',
'Geoserver',
abstract=('Typically of the form '
'http://host:port/geoserver/wfs'),
data_type='string',
min_occurs=0)

mosaic = LiteralInput('mosaic',
'Union of Feature Ids',
abstract=('If True, selected regions will be '
'merged into a single geometry.'),
data_type='boolean',
min_occurs=0,
default=False)


def get_feature(url, typename, features):
"""Return geometry from WFS server."""
Expand Down
73 changes: 73 additions & 0 deletions flyingpigeon/processes/wps_average_wfs_polygon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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.2',
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)

# Remove properties because it crashes ocgis
for geom in geoms.values():
geom.pop("properties")

ml = MetaLink4('subset', workdir=self.workdir)

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=[g for g in geoms.values()],
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
37 changes: 5 additions & 32 deletions flyingpigeon/processes/wps_subset_wfs_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from pywps import Process, LiteralInput, FORMATS
from pywps.inout.outputs import MetaFile, MetaLink4

from .subset_base import Subsetter, resource, variable, start, end, output, metalink
from .subset_base import Subsetter, resource, variable, start, end, output, metalink, typename, \
featureids, geoserver, mosaic

import ocgis
import ocgis.exc
Expand All @@ -14,42 +15,14 @@ class SubsetWFSPolygonProcess(Process, Subsetter):
"""Subset a NetCDF file using WFS geometry."""

def __init__(self):
inputs = [
resource,
LiteralInput('typename',
'TypeName',
abstract='Name of the layer in GeoServer.',
data_type='string',
min_occurs=0,
max_occurs=1),
LiteralInput('featureids',
'Feature Ids',
abstract='fid(s) of the feature in the layer.',
data_type='string',
min_occurs=0,
max_occurs=1000),
LiteralInput('geoserver',
'Geoserver',
abstract=('Typically of the form '
'http://host:port/geoserver/wfs'),
data_type='string',
min_occurs=0),
LiteralInput('mosaic',
'Union of Feature Ids',
abstract=('If True, selected regions will be '
'merged into a single geometry.'),
data_type='boolean',
min_occurs=0,
default=False),
start, end, variable]

inputs = [resource, typename, featureids, geoserver, mosaic, start, end, variable]
outputs = [output, metalink]

super(SubsetWFSPolygonProcess, self).__init__(
self._handler,
identifier='subset-wfs-polygon',
title='Subset',
version='0.2',
version='0.3',
abstract=('Return the data for which grid cells intersect the '
'selected polygon for each input dataset as well as'
'the time range selected.'),
Expand Down Expand Up @@ -79,7 +52,7 @@ def _handler(self, request, response):
try:
ops = ocgis.OcgOperations(
dataset=rd, geom=geom['geom'],
spatial_operation='clip', aggregate=True,
spatial_operation='clip', aggregate=False,
time_range=dr, output_format='nc',
interpolate_spatial_bounds=True,
prefix=prefix, dir_output=tempfile.mkdtemp(dir=self.workdir))
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ replace = Version="{new_version}"
addopts =
--strict
--tb=native
tests/
Copy link
Contributor

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.


python_files = test_*.py
markers =
online: mark test to need internet connection
Expand Down