Skip to content

Commit

Permalink
save event_name as string of date and time
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-hlsn committed Nov 9, 2023
1 parent 77d2727 commit 30464ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def maybe_repeat(values: np.ndarray, times: int) -> np.ndarray:
None,
np.ones(num_events),
np.array(range(num_events), dtype=int) + 1,
[str(x) for x in data[coords["event"]].values],
list(data[coords["event"]].dt.strftime("%Y-%m-%d")),
np.array(u_dt.datetime64_to_ordinal(data[coords["event"]].values)),
],
# The accessor for the data in the Dataset
Expand Down
9 changes: 7 additions & 2 deletions climada/hazard/test/test_base_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _assert_default_values(self, hazard):
self.assertEqual(hazard.units, "")
np.testing.assert_array_equal(hazard.event_id, [1, 2])
np.testing.assert_array_equal(
hazard.event_name, [str(x) for x in hazard.event_id]
hazard.event_name, [x.strftime("%Y-%m-%d") for x in self.time]
)
np.testing.assert_array_equal(
hazard.date, [val.toordinal() for val in self.time]
Expand Down Expand Up @@ -342,7 +342,9 @@ def test_missing_dims(self):
ds = ds.isel(time=0).squeeze()
hazard = Hazard.from_xarray_raster(ds, "", "")
self._assert_default_types(hazard)

np.testing.assert_array_equal(
hazard.event_name, [self.time[0].strftime("%Y-%m-%d")]
)
np.testing.assert_array_equal(hazard.date, [self.time[0].toordinal()])
np.testing.assert_array_equal(hazard.centroids.lat, [0, 0, 0, 1, 1, 1])
np.testing.assert_array_equal(hazard.centroids.lon, [0, 1, 2, 0, 1, 2])
Expand All @@ -367,6 +369,9 @@ def test_missing_dims(self):
ds = ds.expand_dims(time=[np.datetime64("2022-01-01")])
hazard = Hazard.from_xarray_raster(ds, "", "")
self._assert_default_types(hazard)
np.testing.assert_array_equal(
hazard.event_name, ["2022-01-01"]
)
np.testing.assert_array_equal(
hazard.date, [dt.datetime(2022, 1, 1).toordinal()]
)
Expand Down

0 comments on commit 30464ee

Please sign in to comment.