Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0b09281

Browse files
committedAug 27, 2024
Manage zarr and netcdf output in the same way
1 parent 1a604f8 commit 0b09281

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed
 

‎pygeoapi/api/__init__.py

+23-20
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
F_JPEG = 'jpeg'
8989
F_MVT = 'mvt'
9090
F_NETCDF = 'NetCDF'
91+
F_ZARR = 'zarr'
9192

9293
#: Formats allowed for ?f= requests (order matters for complex MIME types)
9394
FORMAT_TYPES = OrderedDict((
@@ -98,6 +99,7 @@
9899
(F_JPEG, 'image/jpeg'),
99100
(F_MVT, 'application/vnd.mapbox-vector-tile'),
100101
(F_NETCDF, 'application/x-netcdf'),
102+
(F_ZARR, 'application/zip+zarr'),
101103
))
102104

103105
#: Locale used for system responses (e.g. exceptions)
@@ -1113,27 +1115,28 @@ def describe_collections(self, request: Union[APIRequest, Any],
11131115
'title': l10n.translate('Coverage data', request.locale),
11141116
'href': f'{self.get_collections_url()}/{k}/coverage?f={F_JSON}' # noqa
11151117
})
1116-
if collection_data_format is not None:
1117-
title_ = l10n.translate('Coverage data as', request.locale) # noqa
1118-
title_ = f"{title_} {collection_data_format['name']}"
1119-
collection['links'].append({
1120-
'type': collection_data_format['mimetype'],
1121-
'rel': f'{OGC_RELTYPES_BASE}/coverage',
1122-
'title': title_,
1123-
'href': f"{self.get_collections_url()}/{k}/coverage?f={collection_data_format['name']}" # noqa
1124-
})
1125-
11261118
# Hardcode netcdf format for xarray provider
1127-
if (collection_data['name'] == 'xarray' and
1128-
collection_data_format['name'] == 'zarr'):
1129-
title_ = l10n.translate('Coverage data as', request.locale)
1130-
title_ = f"{title_} {F_NETCDF}"
1131-
collection['links'].append({
1132-
'type': FORMAT_TYPES[F_NETCDF],
1133-
'rel': f'{OGC_RELTYPES_BASE}/coverage',
1134-
'title': title_,
1135-
'href': f"{self.get_collections_url()}/{k}/coverage?f={F_NETCDF}" # noqa
1136-
})
1119+
if collection_data['name'] == 'xarray':
1120+
data_formats = [
1121+
{'name': F_NETCDF, 'mimetype': FORMAT_TYPES[F_NETCDF]},
1122+
{'name': F_ZARR, 'mimetype': FORMAT_TYPES[F_ZARR]}
1123+
]
1124+
elif collection_data_format is not None:
1125+
data_formats = [collection_data_format]
1126+
else:
1127+
data_formats = []
1128+
1129+
for data_format in data_formats:
1130+
title_ = l10n.translate('Coverage data as', request.locale) # noqa
1131+
title_ = f"{title_} {data_format['name']}"
1132+
collection['links'].append(
1133+
{
1134+
'type': data_format['mimetype'],
1135+
'rel': f'{OGC_RELTYPES_BASE}/coverage',
1136+
'title': title_,
1137+
'href': f"{self.get_collections_url()}/{k}/coverage?f={data_format['name']}", # noqa
1138+
}
1139+
)
11371140

11381141
if dataset is not None:
11391142
LOGGER.debug('Creating extended coverage metadata')

‎pygeoapi/provider/xarray_.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ def __init__(self, provider_def):
5959

6060
super().__init__(provider_def)
6161

62+
self.native_format = None
6263
try:
6364
if provider_def['data'].endswith('.zarr'):
65+
self.native_format = 'zarr'
6466
open_func = xarray.open_zarr
6567
else:
68+
self.native_format = 'netcdf'
6669
if '*' in self.data:
6770
LOGGER.debug('Detected multi file dataset')
6871
open_func = xarray.open_mfdataset
@@ -250,19 +253,15 @@ def query(self, properties=[], subsets={}, bbox=[], bbox_crs=4326,
250253
LOGGER.debug('Creating output in CoverageJSON')
251254
return self.gen_covjson(out_meta, data, properties)
252255
elif format_ == 'zarr':
253-
LOGGER.debug('Returning data in native zarr format')
256+
LOGGER.debug('Returning data in zarr format')
254257
return _get_zarr_data(data)
255258
elif format_ == 'netcdf':
256259
LOGGER.debug('Returning data in netcdf format')
257260
return _get_netcdf_data(data)
258-
else: # return data in native format
259-
with tempfile.NamedTemporaryFile() as fp:
260-
LOGGER.debug('Returning data in native NetCDF format')
261-
data.to_netcdf(
262-
fp.name
263-
) # we need to pass a string to be able to use the "netcdf4" engine # noqa
264-
fp.seek(0)
265-
return fp.read()
261+
else:
262+
msg = f'Unsupported format: {format_}'
263+
LOGGER.error(msg)
264+
raise ProviderQueryError(msg)
266265

267266
def gen_covjson(self, metadata, data, fields):
268267
"""

0 commit comments

Comments
 (0)
Please sign in to comment.