Allow reading Hazard events that are not dates from xarray#837
Allow reading Hazard events that are not dates from xarray#837
Conversation
* Set default value of `Hazard.event_name` to empty string. * Try interpreting values of the event coordinate as dates or ordinals for default values of `Hazard.date`. If that fails, issue a warning and set default values to zeros. * Update tests.
…thub.com/CLIMADA-project/climada_python into feature/hazard-xarray-read-no-time-event
|
Failing tests have nothing to do with this PR |
climada/hazard/base.py
Outdated
| * ``date``: The ``event`` coordinate interpreted as date or ordinal, or | ||
| zeros if that fails (which will issue a warning). |
There was a problem hiding this comment.
I am not sure about the value 0 since this is then not a valid ordinal (must be larger equal to 1). Thus, one would get a non-valid event_date which then creates easily problems down the line when a method expects an ordinal. This then might lead to hard-to-debug/understand error messages for users. Any other idea?
There was a problem hiding this comment.
You are correct. I actually messed up the test. It is fixed now. I also did not realize that 0 does not work as ordinal. I now chose 1 as default value, the ordinal of date 01.01.0001.
climada/hazard/base.py
Outdated
| and Examples) before loading the Dataset as Hazard. | ||
| * Single-valued data for variables ``frequency``. ``event_name``, and | ||
| ``event_date`` will be broadcast to every event. | ||
| * The ``event`` dimension need not be a time or date. |
There was a problem hiding this comment.
I do not understand what this is meaning here.
There was a problem hiding this comment.
Yes, could be clearer. This way, maybe?
| * The ``event`` dimension need not be a time or date. | |
| * The values of the ``event`` coordinate need not be times or dates. |
| read from the Dataset. Use the method parameters to set these attributes. | ||
| * This method does not read coordinate system metadata. Use the ``crs`` parameter | ||
| to set a custom coordinate system identifier. | ||
| * This method **does not** read lazily. Single data arrays must fit into memory. |
There was a problem hiding this comment.
Does it load lazily now? That would be great.
There was a problem hiding this comment.
It has been doing so for quite a while, I just missed deleting this line 🙃
| * ``event_name``: String representation of the event date or empty strings | ||
| if that fails (which will issue a warning). |
There was a problem hiding this comment.
Just to be sure I understand: if the data contains a field 'name', it will be ignored by default?
There was a problem hiding this comment.
This list only specifies the default values. These are chosen if the dataset does not contain a field event_name OR the user chose to ignore it via read_xarray_raster(..., data_vars=dict(event_name=""))
| # Integers | ||
| time = np.arange(size) | ||
| dataset["time"] = time | ||
| self.time = ["1970-01-01", "1970-01-02"] # These will be 0, 1 as ordinals |
There was a problem hiding this comment.
I think ordinals must be larger equal to 1. Is the comment thus incorrect?
There was a problem hiding this comment.
Sorry, the test was wrong. You are correct.
|
Great, good improved fix. A few comments mostly for clarity and one main question: is it smart to use a non-valid ordinal as the default date? |
climada/hazard/base.py
Outdated
| and Examples) before loading the Dataset as Hazard. | ||
| * Single-valued data for variables ``frequency``. ``event_name``, and | ||
| ``event_date`` will be broadcast to every event. | ||
| * The ``event`` dimension need not be a time or date. |
There was a problem hiding this comment.
| * The ``event`` dimension need not be a time or date. | |
| * The values of the ``event`` coordinates can be of any type (times, dates, strings, ...) |
|
Suggestion for the intro docstring wording: |
|
Excellent work! Ready to merge upon consideration of the suggested docstring changes. |
Changes proposed in this PR:
Hazard.date. If that fails, issue a warning and set default values to zeros.Hazard.event_name. If that fails, issue a warning and set default values to empty strings.The method still tries to read the values as dates but issues a warning if that does not work. The fallback is zeros in case of
Hazard.dateand empty strings forHazard.event_name.This extends #795, which actually introduced tighter restrictions for the 'event' coordinate. This causes several issues in my new flood module, see CLIMADA-project/climada_petals#64.
This fixes #829.
PR Author Checklist
develop)PR Reviewer Checklist