From f89e4968edd4d864039c4d10ebac284a6dde7e54 Mon Sep 17 00:00:00 2001 From: emanuel-schmid Date: Fri, 27 Sep 2024 16:21:41 +0200 Subject: [PATCH] suppress datetime conversion warning --- climada_petals/hazard/tc_tracks_forecast.py | 58 ++++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/climada_petals/hazard/tc_tracks_forecast.py b/climada_petals/hazard/tc_tracks_forecast.py index ef40f9bf..786b3c1d 100644 --- a/climada_petals/hazard/tc_tracks_forecast.py +++ b/climada_petals/hazard/tc_tracks_forecast.py @@ -30,6 +30,7 @@ import os import tempfile from pathlib import Path +import warnings # additional libraries import eccodes as ec @@ -506,33 +507,38 @@ def _subset_to_track(msg, index, provider, timestamp_origin, name, id_no): # 0 means the deterministic analysis, which we want to flag. # See documentation for link to ensemble types. ens_bool = msg['ens_type'][index] != 0 - try: - track = xr.Dataset( - data_vars={ - 'max_sustained_wind': ('time', np.squeeze(wnd)), - 'central_pressure': ('time', np.squeeze(pre)/100), - 'radius_max_wind': ('time', np.squeeze(rad)), - 'ts_int': ('time', timestep_int), - }, - coords={ - 'time': timestamp, - 'lat': ('time', lat), - 'lon': ('time', lon), - }, - attrs={ - 'max_sustained_wind_unit': 'm/s', - 'central_pressure_unit': 'mb', - 'name': name, - 'sid': sid, - 'orig_event_flag': False, - 'data_provider': provider, - 'id_no': (int(id_no) + index / 100), - 'ensemble_number': msg['ens_number'][index], - 'is_ensemble': ens_bool, - 'run_datetime': timestamp_origin, - } - ) + with warnings.catch_warnings(): + # prevent issueing a million warnings about conversion of non-nanosecond precision + # datetime to nanosecond precision, e.g. in fetch_ecmwf + # TODO: fix it through converting those _before_ creating the xr.Dataset + warnings.simplefilter(action='error', category=UserWarning) + + track = xr.Dataset( + data_vars={ + 'max_sustained_wind': ('time', np.squeeze(wnd)), + 'central_pressure': ('time', np.squeeze(pre)/100), + 'radius_max_wind': ('time', np.squeeze(rad)), + 'ts_int': ('time', timestep_int), + }, + coords={ + 'time': timestamp, + 'lat': ('time', lat), + 'lon': ('time', lon), + }, + attrs={ + 'max_sustained_wind_unit': 'm/s', + 'central_pressure_unit': 'mb', + 'name': name, + 'sid': sid, + 'orig_event_flag': False, + 'data_provider': provider, + 'id_no': (int(id_no) + index / 100), + 'ensemble_number': msg['ens_number'][index], + 'is_ensemble': ens_bool, + 'run_datetime': timestamp_origin, + } + ) except ValueError as err: LOGGER.warning( 'Could not process track %s subset %d, error: %s',