Skip to content

Commit f157b92

Browse files
balck
1 parent 32a7cff commit f157b92

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

climada/hazard/tc_tracks.py

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,11 +1620,44 @@ def from_netcdf(cls, folder_name):
16201620
data.append(track)
16211621
return cls(data)
16221622

1623+
@staticmethod
1624+
def compute_central_pressure(basin, v_max):
1625+
"""Compute central pressure of tropical cyclone given the maximal
1626+
wind speed of the storm. Method needed to load tracks from_netcdf_fast.
1627+
1628+
Holland, Greg. (2008). A Revised Hurricane Pressure Wind Model.
1629+
Monthly Weather Review - MON WEATHER REV. 136. 10.1175/2008MWR2395.1.
1630+
1631+
Parameters:
1632+
-----------
1633+
basin : str
1634+
Basin of generation of the TC
1635+
v_max : np.array
1636+
1D vector of maximal wind speed along the track
1637+
1638+
Returns:
1639+
--------
1640+
Pc : np.array
1641+
1D vector of central pressure along the track
1642+
"""
1643+
a = 3.4
1644+
Pn = np.full(len(v_max), BASIN_ENV_PRESSURE[basin])
1645+
Pc = Pn - (v_max ** (1000 / 644)) / a
1646+
1647+
return Pc
1648+
1649+
@staticmethod
1650+
def compute_radius_max_winds():
1651+
pass
1652+
1653+
@staticmethod
1654+
def define_category_storm(self):
1655+
pass
1656+
16231657
@classmethod
16241658
def from_netcdf_fast(cls, folder_name):
16251659
"""Create new TCTracks object from NetCDF files created with the FAST model
16261660
of Jonathan Lin.
1627-
16281661
GitHub Repository: https://github.com/linjonathan/tropical_cyclone_risk?
16291662
tab=readme-ov-file
16301663
Publication: https://agupubs.onlinelibrary.wiley.com/doi/epdf/10.1029/2023MS003686
@@ -1633,6 +1666,8 @@ def from_netcdf_fast(cls, folder_name):
16331666
----------
16341667
folder_name : str
16351668
Folder name from where to read files.
1669+
storm_id : int
1670+
Number of the simulated storm
16361671
16371672
Returns:
16381673
-------
@@ -1648,6 +1683,9 @@ def from_netcdf_fast(cls, folder_name):
16481683
continue
16491684
with xr.open_dataset(file) as ds:
16501685
for i in ds.n_trk:
1686+
# if storm_id:
1687+
# i == storm_id
1688+
16511689
# Select track
16521690
track = ds.sel(n_trk=i.item())
16531691

@@ -1660,8 +1698,6 @@ def from_netcdf_fast(cls, folder_name):
16601698
time_step_vector = np.full(time.shape[0], track.time.data[1])
16611699
max_sustained_wind = track.v_trks.data
16621700
basin_vector = np.full(time.shape[0], track.tc_basins.data.item())
1663-
central_pressure = np.nan # work in progress: get them from model
1664-
radius_max_wind = np.nan # work in progress: get them from model
16651701
env_pressure = BASIN_ENV_PRESSURE[track.tc_basins.data.item()]
16661702
env_pressure_vect = np.full(time.shape[0], env_pressure)
16671703

@@ -1673,17 +1709,19 @@ def from_netcdf_fast(cls, folder_name):
16731709
len(SAFFIR_SIM_CAT), max_sustained_wind_kn
16741710
) < np.array(SAFFIR_SIM_CAT)
16751711
category = np.argmax(category_test) - 1
1676-
track_name = track.n_trk.item()
16771712
id_no = track.n_trk.item()
1713+
# Define central pressure
1714+
central_pressure = TCTracks.compute_central_pressure(
1715+
v_max=max_sustained_wind, basin=track.tc_basins.data.item()
1716+
)
16781717

16791718
data.append(
16801719
xr.Dataset(
16811720
{
16821721
"time_step": ("time", time_step_vector),
16831722
"max_sustained_wind": ("time", max_sustained_wind),
1684-
# "central_pressure": ("time", central_pressure),
1685-
# "radius_max_wind": ("time", radius_max_wind),
16861723
"environmental_pressure": ("time", env_pressure_vect),
1724+
"central_pressure": ("time", central_pressure),
16871725
"basin": ("time", basin_vector),
16881726
},
16891727
coords={
@@ -1692,12 +1730,10 @@ def from_netcdf_fast(cls, folder_name):
16921730
"lon": ("time", lon),
16931731
},
16941732
attrs={
1695-
"max_sustained_wind_unit": "kn",
1696-
"central_pressure_unit": "mb",
1697-
"name": track_name,
1698-
"sid": track_name,
1699-
"orig_event_flag": False,
1733+
"max_sustained_wind_unit": "m/s",
1734+
"central_pressure_unit": "hPa",
17001735
"data_provider": "FAST",
1736+
"orig_event_flag": False,
17011737
"id_no": id_no,
17021738
"category": category,
17031739
},

climada/hazard/test/test_tc_tracks.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"tc_month": (("n_trk",), np.random.randint(1, 13, size=20)),
6464
"tc_basins": (
6565
("n_trk",),
66-
np.random.choice(["AU", "EP", "NA", "NI", "SI", "SP", "WP"], size=20),
66+
np.full(20, "WP"),
6767
),
6868
"tc_years": (("n_trk",), np.full(20, 2025)),
6969
"seeds_per_month": (
@@ -641,16 +641,42 @@ def test_from_simulations_storm(self):
641641
tc_track = tc.TCTracks.from_simulations_storm(TEST_TRACK_STORM, years=[7])
642642
self.assertEqual(len(tc_track.data), 0)
643643

644+
def test_compute_central_pressure(self):
645+
pass
646+
644647
def test_from_netcdf_fast(self):
645648
"""test the import of netcdf files from fast model"""
646649

647-
# create dummy .nc file to read, delate it in the end
650+
# create dummy .nc file to be read
648651
file_path = DATA_DIR.joinpath("fast_test_tracks.nc")
649652
TEST_TRACKS_FAST_dummy.to_netcdf(file_path)
650653
tc_track = tc.TCTracks.from_netcdf_fast(file_path)
651-
# test various instances
652654

653-
# remove file
655+
expected_attributes = {
656+
"max_sustained_wind_unit": "m/s",
657+
"central_pressure": "hPa",
658+
"data_provider": "FAST",
659+
"orig_event_flag": False,
660+
"id_no": 0,
661+
"category": -1,
662+
}
663+
664+
self.assertIsInstance(
665+
tc_track, tc.TCTracks, "tc_track is not an instance of TCTracks"
666+
)
667+
self.assertIsInstance(
668+
tc_track.data, list, "tc_track.data is not an instance of list"
669+
)
670+
self.assertIsInstance(
671+
tc_track.data[0],
672+
xr.Dataset,
673+
"tc_track.data[0] not an instance of xarray.Dataset",
674+
)
675+
self.assertEqual(len(tc_track.data), 20)
676+
self.assertEqual(tc_track.data[0].attrs, expected_attributes)
677+
self.assertEqual(tc_track.data[0].environmental_pressure.data[0], 1005)
678+
self.assertEqual(list(tc_track.data[0].coords.keys()), ["time", "lat", "lon"])
679+
654680
os.remove(file_path)
655681

656682
def test_to_geodataframe_points(self):

0 commit comments

Comments
 (0)