-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ncmeta process with opendap (#54)
- Loading branch information
Showing
5 changed files
with
90 additions
and
2 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
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 |
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,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) |