Skip to content
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

Flood of cast warnings after improved hdf5 I/O #84

Open
emanuel-schmid opened this issue Jun 29, 2023 · 4 comments
Open

Flood of cast warnings after improved hdf5 I/O #84

emanuel-schmid opened this issue Jun 29, 2023 · 4 comments
Assignees

Comments

@emanuel-schmid
Copy link
Contributor

climada_python PR #735 started a flood of Runtime Warnings in tc_track_forecast.TCForecast.from_hdf5.

To reproduce, run:

python -m unittest climada_petals.hazard.test.test_tc_tracks_forecast.TestECMWF.test_hdf5_io

...bash
xarray\coding\times.py:254: RuntimeWarning: invalid value encountered in cast
  flat_num_dates_ns_int = (flat_num_dates * _NS_PER_TIME_DELTA[delta]).astype(
...

The warnings are raised in climada.hazard.tc_tracks.TCTracks.from_hdf5:

    def from_hdf5(cls, file_name):
        _raise_if_legacy_or_unknown_hdf5_format(file_name)
        ds_combined = xr.open_dataset(file_name)

        for varname in ds_combined.data_vars:
            if ds_combined[varname].dtype == "object":
                ds_combined[varname] = ds_combined[varname].astype(str)
        data = []
        for i in range(ds_combined.sizes["storm"]):
### most warnings are raised here #################################################
            track = (
                ds_combined
                .isel(storm=i)
                .dropna(dim="step", how="any", subset=["time", "lat", "lon"])
            )
###################################################################################
            track = track.drop_vars(["storm", "step"]).rename(step="time")
            track = track.assign_coords(time=track["time"]).compute()
            attr_vars = [v for v in track.data_vars if track[v].ndim == 0]
            track = (
                track
                .assign_attrs({v: track[v].item() for v in attr_vars})
                .drop_vars(attr_vars)
            )
            track.attrs['orig_event_flag'] = bool(track.attrs['orig_event_flag'])
            data.append(track)
        return cls(data)

I have not really a clue whether we can safely ignore it or whether this points to a serious problem. 🤷

@tovogt
Copy link
Collaborator

tovogt commented Jun 29, 2023

Yes, it's a known issue in xarray (see pydata/xarray#7098, and pydata/xarray#7827). The warnings can be ignored. I use the following to deal with it:

import warnings
warnings.filterwarnings(
    "ignore",
    message="invalid value encountered in cast",
    module="xarray",
    category=RuntimeWarning,
)

@emanuel-schmid
Copy link
Contributor Author

Thanks a lot! 🙌 I'll do the same here.

@emanuel-schmid
Copy link
Contributor Author

I've made an attempt and suppressed the warnings in the parent classes' hdf5 methods. see CLIMADA-project/climada_python#742
Not sure whether it's the perfect place. Perhaps we should suppress it once for all? but perhaps not? and if - how?

@tovogt
Copy link
Collaborator

tovogt commented Jul 3, 2023

I get the same warning when loading any other NetCDF file, it's not only related to the new implementation of TCTracks.from_hdf5. For example, when loading the IBTrACS NetCDF data:

>>> from climada.hazard import TCTracks
>>> TCTracks.from_ibtracs_netcdf(year_range=(2015, 2015))
$CONDA_PREFIX/lib/python3.9/site-packages/xarray/coding/times.py:254: RuntimeWarning: invalid value encountered in cast
  flat_num_dates_ns_int = (flat_num_dates * _NS_PER_TIME_DELTA[delta]).astype(
$CONDA_PREFIX/lib/python3.9/site-packages/xarray/coding/times.py:254: RuntimeWarning: invalid value encountered in cast
  flat_num_dates_ns_int = (flat_num_dates * _NS_PER_TIME_DELTA[delta]).astype(

So, I would consider suppressing the warnings at a central place during CLIMADA setup and maybe mention pydata/xarray#7098 in the code so that future developers know where this comes from, and have an easy benchmark when to remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants