Skip to content

Commit a428419

Browse files
add iteration over year
1 parent 1504128 commit a428419

File tree

1 file changed

+77
-66
lines changed

1 file changed

+77
-66
lines changed

climada/hazard/tc_tracks.py

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,74 +1685,85 @@ def from_FAST(cls, folder_name: str):
16851685
if Path(file).suffix != ".nc":
16861686
continue
16871687
with xr.open_dataset(file) as dataset:
1688-
for i in dataset.n_trk:
1689-
1690-
# Select track
1691-
track = dataset.sel(n_trk=i)
1692-
# chunk dataset at first NaN value
1693-
lon = track.lon_trks.data
1694-
last_valid_index = np.where(np.isnan(lon))[0][0]
1695-
track = track.isel(time=slice(0, last_valid_index))
1696-
# Select lat, lon
1697-
lat = track.lat_trks.data
1698-
lon = track.lon_trks.data
1699-
# Convert lon from 0-360 to -180 - 180
1700-
lon = ((lon + 180) % 360) - 180
1701-
# Convert time to pandas Datetime "yyyy.mm.dd"
1702-
reference_time = (
1703-
f"{track.tc_years.item()}-{int(track.tc_month.item())}-01"
1704-
)
1705-
time = pd.to_datetime(
1706-
track.time.data, unit="s", origin=reference_time
1707-
).astype("datetime64[s]")
1708-
# Define variables
1709-
max_sustained_wind_knots = track.vmax_trks.data * 1.943844
1710-
env_pressure = BASIN_ENV_PRESSURE[track.tc_basins.data.item()]
1711-
cen_pres = _estimate_pressure(
1712-
np.full(lat.shape, np.nan), lat, lon, max_sustained_wind_knots
1713-
)
1688+
for year in dataset.year:
1689+
for i in dataset.n_trk:
1690+
1691+
# Select track
1692+
track = dataset.sel(n_trk=i, year=year)
1693+
# chunk dataset at first NaN value
1694+
lon = track.lon_trks.data
1695+
last_valid_index = np.where(np.isfinite(lon))[0][-1]
1696+
track = track.isel(time=slice(0, last_valid_index + 1))
1697+
# Select lat, lon
1698+
lat = track.lat_trks.data
1699+
lon = track.lon_trks.data
1700+
# Convert lon from 0-360 to -180 - 180
1701+
lon = ((lon + 180) % 360) - 180
1702+
# Convert time to pandas Datetime "yyyy.mm.dd"
1703+
reference_time = (
1704+
f"{track.tc_years.item()}-{int(track.tc_month.item())}-01"
1705+
)
1706+
time = pd.to_datetime(
1707+
track.time.data, unit="s", origin=reference_time
1708+
).astype("datetime64[s]")
1709+
# Define variables
1710+
max_sustained_wind_knots = track.vmax_trks.data * 1.943844
1711+
env_pressure = BASIN_ENV_PRESSURE[track.tc_basins.data.item()]
1712+
cen_pres = _estimate_pressure(
1713+
np.full(lat.shape, np.nan),
1714+
lat,
1715+
lon,
1716+
max_sustained_wind_knots,
1717+
)
17141718

1715-
data.append(
1716-
xr.Dataset(
1717-
{
1718-
"time_step": (
1719-
"time",
1720-
np.full(time.shape[0], track.time.data[1]),
1721-
),
1722-
"max_sustained_wind": ("time", track.vmax_trks.data),
1723-
"central_pressure": ("time", cen_pres),
1724-
"radius_max_wind": (
1725-
"time",
1726-
estimate_rmw(np.full(lat.shape, np.nan), cen_pres),
1727-
),
1728-
"environmental_pressure": (
1729-
"time",
1730-
np.full(time.shape[0], env_pressure),
1731-
),
1732-
"basin": (
1733-
"time",
1734-
np.full(time.shape[0], track.tc_basins.data.item()),
1735-
),
1736-
},
1737-
coords={
1738-
"time": ("time", time),
1739-
"lat": ("time", lat),
1740-
"lon": ("time", lon),
1741-
},
1742-
attrs={
1743-
"max_sustained_wind_unit": "m/s",
1744-
"central_pressure_unit": "hPa",
1745-
"name": f"storm_{track.n_trk.item()}",
1746-
"sid": track.n_trk.item(),
1747-
"orig_event_flag": True,
1748-
"data_provider": "FAST",
1749-
"id_no": track.n_trk.item(),
1750-
"category": TCTracks.define_tc_category_FAST(
1751-
max_sustained_wind_knots
1752-
),
1753-
},
1719+
data.append(
1720+
xr.Dataset(
1721+
{
1722+
"time_step": (
1723+
"time",
1724+
np.full(time.shape[0], track.time.data[1]),
1725+
),
1726+
"max_sustained_wind": (
1727+
"time",
1728+
track.vmax_trks.data,
1729+
),
1730+
"central_pressure": ("time", cen_pres),
1731+
"radius_max_wind": (
1732+
"time",
1733+
estimate_rmw(
1734+
np.full(lat.shape, np.nan), cen_pres
1735+
),
1736+
),
1737+
"environmental_pressure": (
1738+
"time",
1739+
np.full(time.shape[0], env_pressure),
1740+
),
1741+
"basin": (
1742+
"time",
1743+
np.full(
1744+
time.shape[0], track.tc_basins.data.item()
1745+
),
1746+
),
1747+
},
1748+
coords={
1749+
"time": ("time", time),
1750+
"lat": ("time", lat),
1751+
"lon": ("time", lon),
1752+
},
1753+
attrs={
1754+
"max_sustained_wind_unit": "m/s",
1755+
"central_pressure_unit": "hPa",
1756+
"name": f"storm_{track.n_trk.item()}",
1757+
"sid": track.n_trk.item(),
1758+
"orig_event_flag": True,
1759+
"data_provider": "FAST",
1760+
"id_no": track.n_trk.item(),
1761+
"category": TCTracks.define_tc_category_FAST(
1762+
max_sustained_wind_knots
1763+
),
1764+
},
1765+
)
17541766
)
1755-
)
17561767

17571768
return cls(data)
17581769

0 commit comments

Comments
 (0)