Skip to content

Commit 59f3724

Browse files
committed
Update docstrings, docs, tests, and CHANGELOG.md
1 parent c7db730 commit 59f3724

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Removed:
2727
- World Bank indicator data is now downloaded directly from their API via the function `download_world_bank_indicator`, instead of relying on the `pandas-datareader` package [#1033](https://github.com/CLIMADA-project/climada_python/pull/1033)
2828
- `Exposures.write_hdf5` pickles geometry data in WKB format, which is faster and more sustainable. [#1051](https://github.com/CLIMADA-project/climada_python/pull/1051)
2929
- The online documentation has been completely overhauled, now uses PyData theme: [#977](https://github.com/CLIMADA-project/climada_python/pull/977)
30+
- Add `climada.hazard.xarray` module with helper structures for reading Hazard objects from `xarray` data [#1063](https://github.com/CLIMADA-project/climada_python/pull/1063)
3031

3132
### Fixed
3233

@@ -35,6 +36,8 @@ Removed:
3536

3637
### Deprecated
3738

39+
- `Hazard.from_xarray_raster_file`. Use `Hazard.from_xarray_raster` and pass the file path as `data` argument [#1063](https://github.com/CLIMADA-project/climada_python/pull/1063)
40+
3841
### Removed
3942

4043
- `climada.util.interpolation.round_to_sig_digits` [#1012](https://github.com/CLIMADA-project/climada_python/pull/1012)

climada/hazard/io.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -276,21 +276,24 @@ def from_raster(
276276

277277
@classmethod
278278
@deprecated(
279-
details="Hazard.from_xarray_raster now supports a filepath for the 'data' "
280-
"parameter"
279+
6.0,
280+
details="Hazard.from_xarray_raster now supports a filepath as 'data' parameter",
281281
)
282282
def from_xarray_raster_file(
283283
cls, filepath: Union[pathlib.Path, str], *args, **kwargs
284284
):
285285
"""Read raster-like data from a file that can be loaded with xarray
286286
287-
This wraps :py:meth:`~Hazard.from_xarray_raster` by first opening the target file
287+
.. deprecated:: 6.0
288+
Pass ``filepath`` as ``data`` argument to :py:meth:`from_xarray_raster`
289+
instead.
290+
291+
This wraps :py:meth:`from_xarray_raster` by first opening the target file
288292
as xarray dataset and then passing it to that classmethod. Use this wrapper as a
289293
simple alternative to opening the file yourself. The signature is exactly the
290294
same, except for the first argument, which is replaced by a file path here.
291295
292-
Additional (keyword) arguments are passed to
293-
:py:meth:`~Hazard.from_xarray_raster`.
296+
Additional (keyword) arguments are passed to :py:meth:`from_xarray_raster`.
294297
295298
Parameters
296299
----------
@@ -302,10 +305,6 @@ def from_xarray_raster_file(
302305
-------
303306
hazard : climada.Hazard
304307
A hazard object created from the input data
305-
306-
Examples
307-
--------
308-
309308
"""
310309
args = (filepath,) + args
311310
return cls.from_xarray_raster(*args, **kwargs)
@@ -356,13 +355,13 @@ def from_xarray_raster(
356355
intensity_unit : str
357356
The physical units of the intensity.
358357
intensity : str, optional
359-
Identifier of the `xarray.DataArray` containing the hazard intensity data.
358+
Identifier of the ``xarray.DataArray`` containing the hazard intensity data.
360359
coordinate_vars : dict(str, str), optional
361360
Mapping from default coordinate names to coordinate names used in the data
362361
to read. The default is
363-
``dict(event="time", longitude="longitude", latitude="latitude")``, as most
364-
of the commonly used hazard data happens to have a "time" attribute but no
365-
"event" attribute.
362+
``{"event": "time", "longitude": "longitude", "latitude": "latitude"}``, as
363+
most of the commonly used hazard data happens to have a "time" attribute but
364+
no "event" attribute.
366365
data_vars : dict(str, str), optional
367366
Mapping from default variable names to variable names used in the data
368367
to read. The default names are ``fraction``, ``hazard_type``, ``frequency``,
@@ -377,7 +376,7 @@ def from_xarray_raster(
377376
* ``date``: The ``event`` coordinate interpreted as date or ordinal, or
378377
ones if that fails (which will issue a warning).
379378
* ``fraction``: ``None``, which results in a value of 1.0 everywhere, see
380-
:py:meth:`Hazard.__init__` for details.
379+
:py:meth:`~climada.hazard.base.Hazard.__init__` for details.
381380
* ``hazard_type``: Empty string
382381
* ``frequency``: 1.0 for every event
383382
* ``event_name``: String representation of the event date or empty strings
@@ -386,8 +385,9 @@ def from_xarray_raster(
386385
387386
crs : str, optional
388387
Identifier for the coordinate reference system of the coordinates. Defaults
389-
to ``EPSG:4326`` (WGS 84), defined by ``climada.util.constants.DEF_CRS``.
390-
See https://pyproj4.github.io/pyproj/dev/api/crs/crs.html#pyproj.crs.CRS.from_user_input
388+
to ``"EPSG:4326"`` (WGS 84), defined by
389+
:py:const:`climada.util.coordinates.DEF_CRS`. See
390+
https://pyproj4.github.io/pyproj/dev/api/crs/crs.html#pyproj.crs.CRS.from_user_input
391391
for further information on how to specify the coordinate system.
392392
rechunk : bool, optional
393393
Rechunk the dataset before flattening. This might have serious performance
@@ -403,12 +403,12 @@ def from_xarray_raster(
403403
404404
Returns
405405
-------
406-
hazard : climada.Hazard
406+
Hazard
407407
A hazard object created from the input data
408408
409409
See Also
410410
--------
411-
:py:class:`HazardXarrayReader`
411+
:py:class:`climada.hazard.xarray.HazardXarrayReader`
412412
The helper class used to read the data.
413413
414414
Notes
@@ -426,8 +426,9 @@ def from_xarray_raster(
426426
``data_vars``` parameter documentation above.
427427
* To avoid confusion in the call signature, several parameters are keyword-only
428428
arguments.
429-
* The attributes ``Hazard.haz_type`` and ``Hazard.unit`` currently cannot be
430-
read from the Dataset. Use the method parameters to set these attributes.
429+
* The attributes :py:attr:`~climada.hazard.base.Hazard.haz_type` and
430+
:py:attr:`~climada.hazard.base.Hazard.units` currently cannot be read from the
431+
Dataset. Use the method parameters to set these attributes.
431432
* This method does not read coordinate system metadata. Use the ``crs`` parameter
432433
to set a custom coordinate system identifier.
433434

climada/hazard/test/test_xarray.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def setUpClass(cls):
4242
"""Write a simple NetCDF file to read"""
4343
cls.tempdir = TemporaryDirectory()
4444
cls.netcdf_path = Path(cls.tempdir.name) / "default.nc"
45-
cls.intensity = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]])
45+
cls.intensity = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, np.nan]]])
4646
cls.time = np.array([dt.datetime(1999, 1, 1), dt.datetime(2000, 1, 1)])
4747
cls.latitude = np.array([0, 1])
4848
cls.longitude = np.array([0, 1, 2])
@@ -84,8 +84,10 @@ def _assert_default_values(self, hazard):
8484
self.assertEqual(hazard.centroids.geometry.crs, DEF_CRS)
8585

8686
# Intensity data
87+
# NOTE: NaN converted to zero
88+
self.assertEqual(hazard.intensity.nnz, 10)
8789
np.testing.assert_array_equal(
88-
hazard.intensity.toarray(), [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
90+
hazard.intensity.toarray(), [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 0]]
8991
)
9092

9193
# Fraction default
@@ -172,7 +174,7 @@ def test_event_no_time(self):
172174
hazard = Hazard.from_xarray_raster(dataset, "", "")
173175
self._assert_default_types(hazard)
174176
np.testing.assert_array_equal(
175-
hazard.intensity.toarray(), [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
177+
hazard.intensity.toarray(), [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 0]]
176178
)
177179
np.testing.assert_array_equal(hazard.date, time)
178180
np.testing.assert_array_equal(hazard.event_name, np.full(size, ""))

climada/hazard/xarray.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ def load_from_xarray_or_return_default(
148148
"""Load data for a single Hazard attribute or return the default value
149149
150150
Does the following based on the ``user_key``:
151+
151152
* If the key is an empty string, return the default value
152153
* If the key is a non-empty string, load the data for that key and return it.
153-
* If the key is ``None``, look for the ``default_key`` in the data. If it
154-
exists, return that data. If not, return the default value.
154+
* If the key is ``None``, look for the ``default_key`` in the data. If it exists,
155+
return that data. If not, return the default value.
155156
156157
Parameters
157158
----------
@@ -169,7 +170,7 @@ def load_from_xarray_or_return_default(
169170
170171
Returns
171172
-------
172-
The object that will be stored in the ``Hazard`` attribute ``hazard_attr``.
173+
The object that will be stored in the ``Hazard`` attribute with name ``hazard_attr``
173174
174175
Raises
175176
------

doc/api/climada/climada.hazard.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ climada\.hazard\.io module
2222
:undoc-members:
2323
:show-inheritance:
2424

25+
climada\.hazard\.xarray module
26+
------------------------------
27+
28+
Helper module ro read xarray data as Hazard.
29+
30+
.. automodule:: climada.hazard.xarray
31+
:members:
32+
:undoc-members:
33+
:show-inheritance:
34+
2535
climada\.hazard\.isimip\_data module
2636
------------------------------------
2737

0 commit comments

Comments
 (0)