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

added ncmeta process with opendap #54

Merged
merged 1 commit into from
Aug 22, 2018
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: 2 additions & 0 deletions emu/processes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .wps_esgf import ESGFDemo
from .wps_output_formats import OutputFormats
from .wps_poly_centroid import PolyCentroid
from .wps_ncmeta import NCMeta

processes = [
UltimateQuestion(),
Expand All @@ -30,4 +31,5 @@
ESGFDemo(),
OutputFormats(),
PolyCentroid(),
NCMeta(),
]
64 changes: 64 additions & 0 deletions emu/processes/wps_ncmeta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os

from pywps import Process
from pywps import ComplexInput, ComplexOutput, FORMATS, Format
from pywps.validator.mode import MODE
from pywps.app.Common import Metadata

import logging
LOGGER = logging.getLogger("PYWPS")

FORMAT_OPENDAP = Format('application/x-ogc-dods')


class NCMeta(Process):
"""
Notes
-----

Returns metadata of a NetCDF file or OpenDAP resource.
"""
def __init__(self):
inputs = [
ComplexInput('dataset', 'Dataset',
supported_formats=[FORMATS.NETCDF],
min_occurs=0,
mode=MODE.NONE),
ComplexInput('dataset_opendap', 'Dataset OpenDAP',
supported_formats=[FORMAT_OPENDAP],
min_occurs=0,
mode=MODE.NONE),
]
outputs = [
ComplexOutput('output', 'Metadata',
as_reference=True,
supported_formats=[FORMATS.TEXT]), ]

super(NCMeta, self).__init__(
self._handler,
identifier='ncmeta',
title='Show NetCDF Metadata',
version='4',
metadata=[
Metadata('User Guide', 'http://emu.readthedocs.io/en/latest/'),
],
inputs=inputs,
outputs=outputs,
store_supported=True,
status_supported=True)

def _handler(self, request, response):
from netCDF4 import Dataset
if 'dataset_opendap' in request.inputs:
inpt = request.inputs['dataset_opendap'][0]
ds = Dataset(inpt.url)
else:
inpt = request.inputs['dataset'][0]
ds = Dataset(inpt.file)
with open(os.path.join(self.workdir, 'out.txt'), "w") as fp:
response.outputs['output'].file = fp.name
fp.write("URL: {}\n\n".format(inpt.url))
fp.write("MIME Type: {}\n\n".format(inpt.data_format.mime_type))
for attr in ds.ncattrs():
fp.write("{}: {}\n\n".format(attr, ds.getncattr(attr)))
return response
4 changes: 3 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ channels:
- conda-forge
- defaults
dependencies:
- pywps=4.1.6
- pywps=4.1.7
- jinja2
- click
- psutil
- birdhouse-eggshell=0.3
- libiconv
- defusedxml
# opendap support
- netcdf4
# tests
- pytest-flake8
2 changes: 1 addition & 1 deletion tests/test_wps_caps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def test_wps_caps():
'inout',
'multiple_outputs',
'nap',
'ncmeta',
'output_formats',
'poly_centroid',
'show_error',
'sleep',
'ultimate_question',
'wordcounter',

]
20 changes: 20 additions & 0 deletions tests/test_wps_ncmeta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from pywps import Service
from pywps.tests import assert_response_success

from .common import client_for
from emu.processes.wps_ncmeta import NCMeta

OPENDAP_URL = 'http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/ncep.reanalysis.derived/surface/air.mon.ltm.nc'


@pytest.mark.online
def test_wps_ncmeta_opendap():
client = client_for(Service(processes=[NCMeta()]))
datainputs = "dataset_opendap=@xlink:href={0}".format(OPENDAP_URL)
resp = client.get(
service='wps', request='execute', version='1.0.0',
identifier='ncmeta',
datainputs=datainputs)
assert_response_success(resp)