-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -553,13 +555,13 @@ def from_xarray_raster( | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand what this is meaning here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
* To avoid confusion in the call signature, several parameters are keyword-only | ||
arguments. | ||
* The attributes ``Hazard.haz_type`` and ``Hazard.unit`` currently cannot be | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it load lazily now? That would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ordinals must be larger equal to 1. Is the comment thus incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -553,13 +555,13 @@ def from_xarray_raster( | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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.date
and 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