Skip to content

Commit 99843e9

Browse files
authored
Add tool to import OPeNDAP datasets using xarray (#181)
* Add tool to import OPeNDAP dataset using xarray This uses xarray and netcdf4 to open remote datasets available over OPeNDAP * fix linting and tests * fix python linting * fix import order, docs and test output type * add validator for protocol
1 parent 9439bd5 commit 99843e9

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

tools/xarray_import_data/.shed.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
categories:
2+
- Ecology
3+
owner: ecology
4+
remote_repository_url: https://github.com/galaxyecology/tools-ecology/tree/master/tools/xarray_import_data
5+
homepage_url: http://xarray.pydata.org
6+
long_description: |
7+
A data import tool for opening OPeNDAP datasets and storing as netCDF files.
8+
type: unrestricted
9+
auto_tool_repositories:
10+
name_template: "{{ tool_id }}"
11+
description_template: "A data import tool for opening OPeNDAP datasets and storing as netCDF files: {{ tool_name }}."
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Import OPeNDAP dataset using xarray to a netCDF file."""
2+
3+
import argparse
4+
5+
import xarray as xr
6+
7+
parser = argparse.ArgumentParser()
8+
9+
parser.add_argument(
10+
"opendap_url",
11+
help=(
12+
"A valid OPeNDAP URL, also see "
13+
"https://docs.xarray.dev/en/stable/user-guide/io.html#opendap"
14+
),
15+
)
16+
parser.add_argument(
17+
"decode_times",
18+
type=lambda x: x == "true",
19+
help='If time should be decoded, e.g. "True" or "False"',
20+
)
21+
parser.add_argument(
22+
"decode_cf",
23+
type=lambda x: x == "true",
24+
help=(
25+
"Whether to decode according to "
26+
'CF conventions e.g. "true" or "false"'
27+
),
28+
)
29+
parser.add_argument("output_dataset", help="netCDF file to output")
30+
args = parser.parse_args()
31+
32+
xr.open_dataset(
33+
args.opendap_url.strip(),
34+
decode_cf=args.decode_cf,
35+
decode_times=args.decode_times,
36+
).to_netcdf(args.output_dataset)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<tool id="xarray_import_data" name="Xarray Import Data" version="0.1.0" profile="22.05">
2+
<description>
3+
Import a dataset from an OPeNDAP URL and convert it to a local NetCDF file using xarray.
4+
</description>
5+
<requirements>
6+
<requirement type="package" version="2025.4.0">xarray</requirement>
7+
<requirement type="package" version="3">python</requirement>
8+
<requirement type="package" version="1.6.0">netcdf4</requirement>
9+
</requirements>
10+
<command detect_errors="exit_code"><![CDATA[
11+
python '$__tool_directory__/xarray_import_data.py' '$opendap_url' '$decode_times' '$decode_cf' '$output_dataset'
12+
]]></command>
13+
<inputs>
14+
<param name="opendap_url" type="text" format="netcdf" label="OPeNDAP URL"
15+
help="OPeNDAP URL to a dataset, also see https://docs.xarray.dev/en/stable/user-guide/io.html#opendap"
16+
optional="false">
17+
<validator type="regex" message="Type a valid url">^(https:|http:|\.)\S*</validator>
18+
</param>
19+
<param name="decode_times" type="boolean" label="Decode times"
20+
help="If true, decode time variables to datetime objects. If false, keep them as raw data."
21+
checked="true" />
22+
<param name="decode_cf" type="boolean" label="Decode CF conventions"
23+
help="Whether to decode according to CF conventions"
24+
checked="true" />
25+
</inputs>
26+
<outputs>
27+
<data name="output_dataset" from_work_dir="xarray_imported_data" format="netcdf"
28+
label="xarray dataset" />
29+
</outputs>
30+
<tests>
31+
<test>
32+
<param name="opendap_url"
33+
value="http://test.opendap.org:8080/opendap/catalog/data/nc/data.nc" />
34+
<output name="output_dataset" ftype="netcdf">
35+
<assert_contents>
36+
<has_size min="17K" max="18K" />
37+
</assert_contents>
38+
</output>
39+
</test>
40+
</tests>
41+
<help><![CDATA[
42+
==================
43+
Xarray Import Data
44+
==================
45+
46+
**What it does**
47+
48+
Open a dataset from an OPeNDAP URL and convert it to a NetCDF file using xarray and netcdf4.
49+
50+
|
51+
52+
**How to use it**
53+
54+
Pass a valid OPeNDAP URL to the `opendap_url` parameter. The tool will download the dataset and save it as a local NetCDF file.
55+
56+
For big datasets consider subsetting the dataset in the url using `<URL>?var_name1[start:step:end],var_name2[start:step:end]`
57+
58+
|
59+
60+
**Links**
61+
https://docs.xarray.dev/en/stable/user-guide/io.html#opendap
62+
https://www.opendap.org/
63+
]]>
64+
</help>
65+
<citations>
66+
<citation type="doi">10.5334/jors.148</citation>
67+
</citations>
68+
</tool>

0 commit comments

Comments
 (0)