-
Notifications
You must be signed in to change notification settings - Fork 57
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
Freezing rain events #1778
base: main
Are you sure you want to change the base?
Freezing rain events #1778
Conversation
xclim/indices/_threshold.py
Outdated
# Reduce time dim to event dimension | ||
mask = (ds.run_lengths > 0).any(dim=[d for d in ds.dims if d != "time"]) | ||
ds = ds.where(mask).dropna(dim="time").rename({"time": "event"}) |
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 should be different, to fix the number of events... need to change this, I have code for this somewhere, will do soon
xclim/indices/_threshold.py
Outdated
# Filter events: Reduce time dimension | ||
def _filter_events(da, rl, max_event_number): | ||
out = np.full(max_event_number, np.NaN) | ||
events_start = da[rl > 0] | ||
out[: len(events_start)] = events_start | ||
return out | ||
|
||
max_event_number = int(np.ceil(pr.time.size / (window_start + window_stop))) | ||
v_attrs = {v: ds[v].attrs for v in ds.data_vars} | ||
ds = xarray.apply_ufunc( | ||
_filter_events, | ||
ds, | ||
ds.run_lengths, | ||
input_core_dims=[["time"], ["time"]], | ||
output_core_dims=[["event"]], | ||
kwargs=dict(max_event_number=max_event_number), | ||
output_sizes={"event": max_event_number}, | ||
dask="parallelized", | ||
vectorize=True, | ||
).assign_attrs(ds.attrs) | ||
|
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 change allows to filter the events, reorder this dimension. This requires xr.apply_ufunc and contiguous time dimension though. I don't think the previous implementation would have worked for without contiguous time anyways.
However! The rest of the code uses calls to run_length and I believe this can be done with chunks over the time dimension. One other approach could to keep all time coords at first, and allow the use of yearly time chunks for instance, then save the output. Then, in a second pass, filter events using this apply_ufunc above.
freq = xr.infer_freq(data.time) | ||
mag, units, _, _ = parse_offset(freq) |
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.
freq
is an input, but is simply redefined in the function?
Pull Request Checklist:
number
) and pull request (:pull:number
) has been addedWhat kind of change does this PR introduce?
freezing_rain_events
computes the sequence of freezing rain event, giving a Dataset with various informations about the runs in the output (cumulative precipitation of runs, duration of runs, ...)run_length
that can compute the run_length of sequence determined by two conditions. A first condition determines when runs should start, and a second one determines when they stop. This is calledruns_with_holes
as one possibility with the addtition of the second condition is this: consider a normal run_length, but the second condition allows for holes in thoses sequences, where those holes cannot exceed a given thresholdwindow_stop
.runs_with_holes
is in fact more general, so perhaps another name should be found.Does this PR introduce a breaking change?
No
Other information: