From 9ebe0bc97ddf315ad116c0e7deae0eca4aadcf89 Mon Sep 17 00:00:00 2001 From: William Pringle Date: Tue, 30 Jul 2024 08:52:40 -0400 Subject: [PATCH 1/2] Overwrite existing output file in test (#108) adding overwrite=True to one of stormevent tests to fix any problems with rerunning tests in local space --- tests/test_stormevent.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_stormevent.py b/tests/test_stormevent.py index f9b373d..38cb578 100644 --- a/tests/test_stormevent.py +++ b/tests/test_stormevent.py @@ -140,8 +140,8 @@ def test_storm_event_track(florence2018, ida2021): florence_track = florence2018.track() ida_track = ida2021.track() - florence_track.to_file(output_directory / "florence2018.fort.22") - ida_track.to_file(output_directory / "ida2021.fort.22") + florence_track.to_file(output_directory / "florence2018.fort.22", overwrite=True) + ida_track.to_file(output_directory / "ida2021.fort.22", overwrite=True) check_reference_directory(output_directory, reference_directory) From 0ce50e63a2569a4cc608d6092e4f645b338289f4 Mon Sep 17 00:00:00 2001 From: Soroosh Mani <77082694+SorooshMani-NOAA@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:14:49 -0400 Subject: [PATCH 2/2] Bugfix/misc (#111) * Fix usgs stormlist duplication * Fix test ref due to updated response * Reduce warnings * Styling --- stormevents/nhc/storms.py | 14 ++++++-------- stormevents/nhc/track.py | 5 ++--- stormevents/usgs/events.py | 11 +++-------- .../reference/test_usgs_flood_storms/storms.csv | 3 --- tests/test_stormevent.py | 4 ++-- 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/stormevents/nhc/storms.py b/stormevents/nhc/storms.py index 91e1baf..66e6beb 100644 --- a/stormevents/nhc/storms.py +++ b/stormevents/nhc/storms.py @@ -65,13 +65,9 @@ def nhc_storms(year: int = None) -> pandas.DataFrame: url, header=0, names=columns, - parse_dates=["start_date", "end_date"], - date_parser=lambda x: ( - pandas.to_datetime(x.strip(), format="%Y%m%d%H") - if x.strip() != "9999999999" - else numpy.nan - ), ) + for i in ["start_date", "end_date"]: + storms[i] = pandas.to_datetime(storms[i], errors="coerce", format="%Y%m%d%H") storms = storms.astype( {"start_date": "datetime64[s]", "end_date": "datetime64[s]"}, @@ -107,7 +103,9 @@ def nhc_storms(year: int = None) -> pandas.DataFrame: ) if len(gis_archive_storms) > 0: gis_archive_storms[["start_date", "end_date"]] = pandas.to_datetime(numpy.nan) - storms = pandas.concat([storms, gis_archive_storms[storms.columns]]) + storms = pandas.concat( + [storms, gis_archive_storms[storms.columns].astype(storms.dtypes.to_dict())] + ) for string_column in ["name", "class", "source"]: storms.loc[storms[string_column].str.len() == 0, string_column] = pandas.NA @@ -211,7 +209,7 @@ def nhc_storms_gis_archive(year: int = None) -> pandas.DataFrame: year = list(range(NHC_GIS_ARCHIVE_START_YEAR, datetime.today().year + 1)) if isinstance(year, Iterable) and not isinstance(year, str): - years = sorted(pandas.unique(year)) + years = sorted(pandas.unique(numpy.array(year))) return pandas.concat( [ nhc_storms_gis_archive(year) diff --git a/stormevents/nhc/track.py b/stormevents/nhc/track.py index 7bf6f31..c2acebe 100644 --- a/stormevents/nhc/track.py +++ b/stormevents/nhc/track.py @@ -638,8 +638,7 @@ def atcf(self, advisory: ATCF_Advisory = None) -> DataFrame: atcf["isotach_radius_for_NWQ"].astype("string").str.pad(5) ) - atcf["background_pressure"].fillna(method="ffill", inplace=True) - atcf["background_pressure"] = atcf["background_pressure"].astype(int) + atcf["background_pressure"] = atcf["background_pressure"].ffill().astype(int) atcf["central_pressure"] = atcf["central_pressure"].astype(int) press_cond_nobg = ~atcf["central_pressure"].isna() & ( @@ -1362,7 +1361,7 @@ def clamp(n, minn, maxn): data={"forecast_hours": fcsthrs_12hr, "radius_of_maximum_winds": rmw_12hr}, index=dt_12hr, ) - rmw_rolling = df_temp.rolling(window="24.01 H", center=True, min_periods=1)[ + rmw_rolling = df_temp.rolling(window="24.01 h", center=True, min_periods=1)[ "radius_of_maximum_winds" ].mean() for valid_time, rmw in rmw_rolling.items(): diff --git a/stormevents/usgs/events.py b/stormevents/usgs/events.py index 3e7c4c6..f724b91 100644 --- a/stormevents/usgs/events.py +++ b/stormevents/usgs/events.py @@ -176,7 +176,7 @@ def usgs_flood_storms(year: int = None) -> DataFrame: storm_names = sorted(pandas.unique(storms["name"].str.strip())) for storm_name in storm_names: event_storms = events[ - events["usgs_name"].str.contains(storm_name, flags=re.IGNORECASE) + events["usgs_name"].str.contains(f"\\b{storm_name}\\b", flags=re.IGNORECASE) ] for _, event in event_storms.iterrows(): storms_matching = storms[ @@ -188,13 +188,8 @@ def usgs_flood_storms(year: int = None) -> DataFrame: matching_event = events.loc[events["usgs_id"] == event["usgs_id"]].iloc[ 0 ] - if matching_event["nhc_code"] is None: - events.at[matching_event.name, "nhc_name"] = storm["name"] - events.at[matching_event.name, "nhc_code"] = storm.name - else: - matching_event["nhc_name"] = storm["name"] - matching_event["nhc_code"] = storm.name - events.loc[len(events)] = matching_event + events.at[matching_event.name, "nhc_name"] = storm["name"] + events.at[matching_event.name, "nhc_code"] = storm.name events = events.loc[ ~pandas.isna(events["nhc_code"]), diff --git a/tests/data/reference/test_usgs_flood_storms/storms.csv b/tests/data/reference/test_usgs_flood_storms/storms.csv index 45944ad..012bffc 100644 --- a/tests/data/reference/test_usgs_flood_storms/storms.csv +++ b/tests/data/reference/test_usgs_flood_storms/storms.csv @@ -4,7 +4,6 @@ Hurricane Wilma was the most intense tropical cyclone ever recorded in the Atlan AL092012,18,2012 Isaac,2012,ISAAC,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,36,[],2022-09-09 14:57:23.815290,2062.0,2012-08-27 04:00:00,2012-09-02 04:00:00 AL182005,19,2005 Rita,2005,RITA,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,515,[],2022-09-08 20:10:38.533682,1.0,2005-09-23 04:00:00,2005-09-25 04:00:00 AL092011,23,2011 Irene,2011,IRENE,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,36,[],2022-09-09 15:04:03.637296,2062.0,2011-08-26 04:00:00,2011-08-29 04:00:00 -AL092011,23,2011 Irene,2011,IRENE,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,36,[],2022-09-09 15:04:03.637296,2062.0,2011-08-26 04:00:00,2011-08-29 04:00:00 AL182012,24,2012 Sandy,2012,SANDY,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,36,[],2022-09-09 15:04:13.715276,2062.0,2012-10-21 04:00:00,2012-10-30 04:00:00 AL072008,25,2008 Gustav,2008,GUSTAV,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,515,[],2022-09-09 15:04:24.293240,2062.0,2008-08-31 04:00:00,2008-09-03 04:00:00 AL092008,26,2008 Ike,2008,IKE,historical hurricane data loaded by the data archive team,HURRICANE,COMPLETED,515,[],2022-09-09 15:04:47.855383,2062.0,2008-09-11 04:00:00,2008-09-12 04:00:00 @@ -19,12 +18,10 @@ AL122017,190,2017 Jose,2017,JOSE,Tropical storm impacting the northeast us,HURRI AL162017,196,2017 Nate,2017,NATE,TD16 will be developing into Nate. North Gulf deployment,HURRICANE,COMPLETED,36,[],2022-09-09 19:08:52.507058,2062.0,2017-10-05 04:00:00,2017-10-14 04:00:00 EP142018,281,2018 Lane,2018,LANE,Hurricane Lane in the central Pacific Ocean.,HURRICANE,ACTIVE,35,[],2022-09-09 19:33:46.421776,2062.0,2018-08-22 04:00:00,2018-09-15 04:00:00 AL072018,282,2018 Gordon,2018,GORDON,TS/Hurricane Gordon in Gulf of Mexico,HURRICANE,ACTIVE,35,[],2022-09-09 19:33:33.625101,2062.0,2018-09-04 04:00:00,2018-10-04 04:00:00 -AL072018,282,2018 Gordon,2018,GORDON,TS/Hurricane Gordon in Gulf of Mexico,HURRICANE,ACTIVE,35,[],2022-09-09 19:33:33.625101,2062.0,2018-09-04 04:00:00,2018-10-04 04:00:00 AL062018,283,2018 Florence,2018,FLORENCE,,HURRICANE,ACTIVE,35,[],2022-09-09 19:33:14.016012,2062.0,2018-09-07 04:00:00,2018-10-07 04:00:00 AL092018,284,2018 Isaac,2018,ISAAC,,HURRICANE,COMPLETED,3,[],2022-09-09 19:33:02.531819,2062.0,2018-09-11 04:00:00,2018-09-18 04:00:00 AL142018,287,2018 Michael,2018,MICHAEL,storm hit the Mexico Beach area of the Florida panhandle,HURRICANE,COMPLETED,3,[],2022-09-09 19:32:14.235677,2062.0,2018-10-08 04:00:00,2018-10-15 04:00:00 AL052019,291,2019 Dorian,2019,DORIAN,,HURRICANE,ACTIVE,864,[],2022-09-09 19:31:39.126837,2062.0,2019-08-28 04:00:00,2019-09-20 04:00:00 -AL052019,291,2019 Dorian,2019,DORIAN,,HURRICANE,ACTIVE,864,[],2022-09-09 19:31:39.126837,2062.0,2019-08-28 04:00:00,2019-09-20 04:00:00 AL092020,301,2020 Isaias,2020,ISAIAS,,HURRICANE,ACTIVE,1001,[],2022-09-09 19:29:43.878587,2062.0,2020-07-31 04:00:00,2020-08-07 04:00:00 AL132020,303,2020 Laura,2020,LAURA,,HURRICANE,ACTIVE,864,[],2022-09-09 19:29:38.331805,2062.0,2020-08-22 04:00:00,2020-08-30 04:00:00 AL192020,304,2020 Sally,2020,SALLY,,HURRICANE,ACTIVE,864,[],2022-09-09 19:29:25.785112,2062.0,2020-09-13 04:00:00,2020-09-20 04:00:00 diff --git a/tests/test_stormevent.py b/tests/test_stormevent.py index 38cb578..50becdc 100644 --- a/tests/test_stormevent.py +++ b/tests/test_stormevent.py @@ -72,14 +72,14 @@ def test_storm_event_lookup(): assert henri2021.year == 2021 assert henri2021.nhc_code == "AL082021" assert henri2021.usgs_id == 310 - assert henri2021.start_date == datetime(2021, 8, 20, 18) + assert henri2021.start_date == datetime(2021, 8, 20, 4) assert henri2021.end_date == datetime(2021, 8, 24, 18) assert ida2021.name == "IDA" assert ida2021.year == 2021 assert ida2021.nhc_code == "AL092021" assert ida2021.usgs_id == 312 - assert ida2021.start_date == datetime(2021, 8, 27, 18) + assert ida2021.start_date == datetime(2021, 8, 26, 12) assert ida2021.end_date == datetime(2021, 9, 4, 18) # Similar names are not swapped (like [B]ETA)