Skip to content

Commit

Permalink
use new StormEvents API (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyburnett authored Apr 20, 2022
1 parent 44a78c9 commit 1c05e8d
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 178 deletions.
89 changes: 46 additions & 43 deletions adcircpy/forcing/winds/best_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from shapely import ops
from shapely.geometry import Point, Polygon
from stormevents.nhc import VortexTrack
from stormevents.nhc.atcf import ATCF_Mode
import utm

from adcircpy.forcing.winds.base import WindForcing
Expand All @@ -24,51 +23,52 @@

class BestTrackForcing(VortexTrack, WindForcing):
def __init__(
self,
storm: Union[str, PathLike, DataFrame, io.BytesIO],
nws: int = None,
interval_seconds: int = None,
start_date: datetime = None,
end_date: datetime = None,
mode: ATCF_Mode = None,
self,
storm: Union[str, PathLike, DataFrame, io.BytesIO],
nws: int = None,
interval_seconds: int = None,
start_date: datetime = None,
end_date: datetime = None,
):
if nws is None:
nws = 20

valid_nws_values = [8, 19, 20]
assert (
nws in valid_nws_values
nws in valid_nws_values
), f'ATCF BestTrack can only use `nws` values in {valid_nws_values}'

if interval_seconds is None:
interval_seconds = 3600

VortexTrack.__init__(
self,
storm=storm,
start_date=start_date,
end_date=end_date,
file_deck='b',
mode=mode,
record_type='BEST',
self,
storm=storm,
start_date=start_date,
end_date=end_date,
file_deck='b',
advisories=['BEST'],
)
WindForcing.__init__(self, nws=nws, interval_seconds=interval_seconds)

@classmethod
def from_fort22(
cls,
fort22: PathLike,
nws: int = None,
interval_seconds: int = None,
start_date: datetime = None,
end_date: datetime = None,
cls,
fort22: PathLike,
nws: int = None,
interval_seconds: int = None,
start_date: datetime = None,
end_date: datetime = None,
) -> 'WindForcing':
instance = cls.from_file(path=fort22, start_date=start_date, end_date=end_date)
WindForcing.__init__(instance, nws=nws, interval_seconds=interval_seconds)
instance = cls.from_file(path=fort22, start_date=start_date,
end_date=end_date)
WindForcing.__init__(instance, nws=nws,
interval_seconds=interval_seconds)
return instance

def summary(
self, output: Union[str, os.PathLike] = None, overwrite: bool = False,
self, output: Union[str, os.PathLike] = None,
overwrite: bool = False,
):
min_storm_speed = numpy.min(self.data['speed'])
max_storm_speed = numpy.max(self.data['speed'])
Expand All @@ -77,7 +77,8 @@ def summary(
min_central_pressure = numpy.min(self.data['central_pressure'])
max_wind_speed = numpy.max(self.data['max_sustained_wind_speed'])
start_loc = (self.data['longitude'][0], self.data['latitude'][0])
end_loc = (self.data['longitude'].iloc[-1], self.data['latitude'].iloc[-1])
end_loc = (
self.data['longitude'].iloc[-1], self.data['latitude'].iloc[-1])
f = [
f'Summary of storm: {self.nhc_code}',
f'min./max. track speed: {min_storm_speed} m/s, {max_storm_speed} m/s',
Expand Down Expand Up @@ -143,13 +144,13 @@ def clip_to_bbox(self, bbox, bbox_crs):
msg = f'bbox must be a {Bbox} instance.'
assert isinstance(bbox, Bbox), msg
bbox_pol = Polygon(
[
[bbox.xmin, bbox.ymin],
[bbox.xmax, bbox.ymin],
[bbox.xmax, bbox.ymax],
[bbox.xmin, bbox.ymax],
[bbox.xmin, bbox.ymin],
]
[
[bbox.xmin, bbox.ymin],
[bbox.xmax, bbox.ymin],
[bbox.xmax, bbox.ymax],
[bbox.xmin, bbox.ymax],
[bbox.xmin, bbox.ymin],
]
)
_switch = True
unique_dates = numpy.unique(self.data['datetime'])
Expand All @@ -166,7 +167,8 @@ def clip_to_bbox(self, bbox, bbox_crs):
transformer = Transformer.from_crs(df_crs, utm_crs, always_xy=True)
p = Point(*transformer.transform(lon, lat))
pol = p.buffer(radii)
transformer = Transformer.from_crs(utm_crs, bbox_crs, always_xy=True)
transformer = Transformer.from_crs(utm_crs, bbox_crs,
always_xy=True)
pol = ops.transform(transformer.transform, pol)
if _switch is True:
if not pol.intersects(bbox_pol):
Expand All @@ -185,15 +187,16 @@ def clip_to_bbox(self, bbox, bbox_crs):
break

if _found_start_date is False:
raise Exception(f'No data within mesh bounding box for storm {self.storm_id}.')
raise Exception(
f'No data within mesh bounding box for storm {self.storm_id}.')

def plot_track(
self,
axis: Axis = None,
show: bool = False,
color: str = 'k',
coastline: bool = True,
**kwargs,
self,
axis: Axis = None,
show: bool = False,
color: str = 'k',
coastline: bool = True,
**kwargs,
):
kwargs.update({'color': color})
if axis is None:
Expand All @@ -207,7 +210,7 @@ def plot_track(
axis.quiver(row['longitude'], row['latitude'], U, V, **kwargs)
if i % 6 == 0:
axis.annotate(
row['datetime'], (row['longitude'], row['latitude']),
row['datetime'], (row['longitude'], row['latitude']),
)
if show:
axis.axis('scaled')
Expand All @@ -221,6 +224,6 @@ def plot_wind_swath(self, isotach: int, segments: int = 91):
plot_polygons(isotachs)
plot_polygons(swath)
pyplot.suptitle(
f'{self.nhc_code} - isotach {isotach} kt ({self.start_date} - {self.end_date})'
f'{self.nhc_code} - isotach {isotach} kt ({self.start_date} - {self.end_date})'
)
pyplot.show()
2 changes: 1 addition & 1 deletion adcircpy/fort15.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def within_wind_swath(
if wind_speed is None:
wind_speed = 34

combined_wind_swaths = ops.unary_union(list(track.wind_swaths(wind_speed).values()))
combined_wind_swaths = ops.unary_union(list(track.wind_swaths(wind_speed)['BEST'].values()))

return cls(
station_types=station_types,
Expand Down
2 changes: 1 addition & 1 deletion tests/data/reference/example_3/fort.15.coldstart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
created on 2022-03-10 16:14 ! RUNDES - 32 CHARACTER ALPHANUMERIC RUN DESCRIPTION
created on 2022-04-20 15:31 ! RUNDES - 32 CHARACTER ALPHANUMERIC RUN DESCRIPTION
Shinacock Inlet Coarse Grid ! RUNID - 24 CHARACTER ALPANUMERIC RUN IDENTIFICATION
1 ! NFOVER - NONFATAL ERROR OVERRIDE OPTION
1 ! NABOUT - ABREVIATED OUTPUT OPTION PARAMETER
Expand Down
2 changes: 1 addition & 1 deletion tests/data/reference/example_3/fort.15.hotstart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
created on 2022-03-10 16:14 ! RUNDES - 32 CHARACTER ALPHANUMERIC RUN DESCRIPTION
created on 2022-04-20 15:31 ! RUNDES - 32 CHARACTER ALPHANUMERIC RUN DESCRIPTION
Shinacock Inlet Coarse Grid ! RUNID - 24 CHARACTER ALPANUMERIC RUN IDENTIFICATION
1 ! NFOVER - NONFATAL ERROR OVERRIDE OPTION
1 ! NABOUT - ABREVIATED OUTPUT OPTION PARAMETER
Expand Down
38 changes: 19 additions & 19 deletions tests/data/reference/example_3/fort.22
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
AL, 18, 2012102118, , BEST, 0,143N, 774W, 25, 1006, LO, 0, , 0, 0, 0, 0, 1008, 180, 150, 35, 0, L, 0, , 0, 0, INVEST ,1
AL, 18, 2012102200, , BEST, 0,139N, 778W, 25, 1005, LO, 0, , 0, 0, 0, 0, 1008, 180, 150, 35, 0, L, 0, ,224, 3, INVEST ,2
AL, 18, 2012102206, , BEST, 0,135N, 782W, 25, 1003, LO, 0, , 0, 0, 0, 0, 1008, 225, 75, 35, 0, L, 0, ,224, 3, INVEST ,3
AL, 18, 2012102212, , BEST, 0,131N, 786W, 30, 1002, TD, 0, , 0, 0, 0, 0, 1007, 250, 75, 0, 0, L, 0, ,224, 3, EIGHTEEN ,4
AL, 18, 2012102218, , BEST, 0,127N, 787W, 35, 1000, TS, 34, NEQ, 50, 60, 0, 0, 1007, 250, 60, 45, 0, L, 0, ,194, 2, EIGHTEEN ,5
AL, 18, 2012102300, , BEST, 0,126N, 784W, 40, 998, TS, 34, NEQ, 50, 60, 0, 0, 1007, 250, 40, 50, 0, L, 0, ,109, 2, SANDY ,6
AL, 18, 2012102306, , BEST, 0,129N, 781W, 40, 998, TS, 34, NEQ, 70, 80, 0, 0, 1005, 240, 60, 50, 0, L, 0, , 44, 2, SANDY ,7
AL, 18, 2012102312, , BEST, 0,134N, 779W, 40, 995, TS, 34, NEQ, 100, 100, 0, 0, 1005, 240, 60, 55, 0, L, 0, , 21, 3, SANDY ,8
AL, 18, 2012102318, , BEST, 0,140N, 776W, 45, 993, TS, 34, NEQ, 100, 120, 0, 0, 1005, 240, 60, 55, 0, L, 0, , 26, 3, SANDY ,9
AL, 18, 2012102400, , BEST, 0,147N, 773W, 55, 990, TS, 34, NEQ, 100, 150, 40, 40, 1007, 300, 60, 60, 0, L, 0, , 23, 4, SANDY ,10
AL, 18, 2012102400, , BEST, 0,147N, 773W, 55, 990, TS, 50, NEQ, 0, 70, 0, 0, 1007, 300, 60, 60, 0, L, 0, , 23, 4, SANDY ,10
AL, 18, 2012102406, , BEST, 0,156N, 771W, 60, 987, TS, 34, NEQ, 100, 150, 50, 50, 1007, 300, 45, 65, 0, L, 0, , 12, 5, SANDY ,11
AL, 18, 2012102406, , BEST, 0,156N, 771W, 60, 987, TS, 50, NEQ, 50, 70, 20, 20, 1007, 300, 45, 65, 0, L, 0, , 12, 5, SANDY ,11
AL, 18, 2012102412, , BEST, 0,166N, 769W, 65, 981, HU, 34, NEQ, 120, 160, 70, 70, 1006, 285, 40, 75, 0, L, 0, , 11, 5, SANDY ,12
AL, 18, 2012102412, , BEST, 0,166N, 769W, 65, 981, HU, 50, NEQ, 50, 60, 40, 30, 1006, 285, 40, 75, 0, L, 0, , 11, 5, SANDY ,12
AL, 18, 2012102412, , BEST, 0,166N, 769W, 65, 981, HU, 64, NEQ, 20, 20, 0, 0, 1006, 285, 40, 75, 0, L, 0, , 11, 5, SANDY ,12
AL, 18, 2012102418, , BEST, 0,177N, 767W, 75, 972, HU, 34, NEQ, 150, 180, 70, 70, 1005, 290, 25, 85, 0, L, 0, , 10, 6, SANDY ,13
AL, 18, 2012102418, , BEST, 0,177N, 767W, 75, 972, HU, 50, NEQ, 50, 60, 40, 40, 1005, 290, 25, 85, 0, L, 0, , 10, 6, SANDY ,13
AL, 18, 2012102418, , BEST, 0,177N, 767W, 75, 972, HU, 64, NEQ, 25, 30, 20, 25, 1005, 290, 25, 85, 0, L, 0, , 10, 6, SANDY ,13
AL, 18, 2012102118, , BEST, 0, 143N,774W, 25, 1006, LO, 0, , 0, 0, 0, 0, 1008, 180, 150, 35, 0, L, 0, , 0, 0, INVEST, 1
AL, 18, 2012102200, , BEST, 0, 139N,778W, 25, 1005, LO, 0, , 0, 0, 0, 0, 1008, 180, 150, 35, 0, L, 0, ,224, 3, INVEST, 2
AL, 18, 2012102206, , BEST, 0, 135N,782W, 25, 1003, LO, 0, , 0, 0, 0, 0, 1008, 225, 75, 35, 0, L, 0, ,224, 3, INVEST, 3
AL, 18, 2012102212, , BEST, 0, 131N,786W, 30, 1002, TD, 0, , 0, 0, 0, 0, 1007, 250, 75, 0, 0, L, 0, ,224, 3, EIGHTEEN, 4
AL, 18, 2012102218, , BEST, 0, 127N,787W, 35, 1000, TS, 34, NEQ, 50, 60, 0, 0, 1007, 250, 60, 45, 0, L, 0, ,194, 2, EIGHTEEN, 5
AL, 18, 2012102300, , BEST, 0, 126N,784W, 40, 998, TS, 34, NEQ, 50, 60, 0, 0, 1007, 250, 40, 50, 0, L, 0, ,109, 2, SANDY, 6
AL, 18, 2012102306, , BEST, 0, 129N,781W, 40, 998, TS, 34, NEQ, 70, 80, 0, 0, 1005, 240, 60, 50, 0, L, 0, , 44, 2, SANDY, 7
AL, 18, 2012102312, , BEST, 0, 134N,779W, 40, 995, TS, 34, NEQ, 100, 100, 0, 0, 1005, 240, 60, 55, 0, L, 0, , 21, 3, SANDY, 8
AL, 18, 2012102318, , BEST, 0, 140N,776W, 45, 993, TS, 34, NEQ, 100, 120, 0, 0, 1005, 240, 60, 55, 0, L, 0, , 26, 3, SANDY, 9
AL, 18, 2012102400, , BEST, 0, 147N,773W, 55, 990, TS, 34, NEQ, 100, 150, 40, 40, 1007, 300, 60, 60, 0, L, 0, , 23, 4, SANDY, 10
AL, 18, 2012102400, , BEST, 0, 147N,773W, 55, 990, TS, 50, NEQ, 0, 70, 0, 0, 1007, 300, 60, 60, 0, L, 0, , 23, 4, SANDY, 10
AL, 18, 2012102406, , BEST, 0, 156N,771W, 60, 987, TS, 34, NEQ, 100, 150, 50, 50, 1007, 300, 45, 65, 0, L, 0, , 12, 5, SANDY, 11
AL, 18, 2012102406, , BEST, 0, 156N,771W, 60, 987, TS, 50, NEQ, 50, 70, 20, 20, 1007, 300, 45, 65, 0, L, 0, , 12, 5, SANDY, 11
AL, 18, 2012102412, , BEST, 0, 166N,769W, 65, 981, HU, 34, NEQ, 120, 160, 70, 70, 1006, 285, 40, 75, 0, L, 0, , 11, 5, SANDY, 12
AL, 18, 2012102412, , BEST, 0, 166N,769W, 65, 981, HU, 50, NEQ, 50, 60, 40, 30, 1006, 285, 40, 75, 0, L, 0, , 11, 5, SANDY, 12
AL, 18, 2012102412, , BEST, 0, 166N,769W, 65, 981, HU, 64, NEQ, 20, 20, 0, 0, 1006, 285, 40, 75, 0, L, 0, , 11, 5, SANDY, 12
AL, 18, 2012102418, , BEST, 0, 177N,767W, 75, 972, HU, 34, NEQ, 150, 180, 70, 70, 1005, 290, 25, 85, 0, L, 0, , 10, 6, SANDY, 13
AL, 18, 2012102418, , BEST, 0, 177N,767W, 75, 972, HU, 50, NEQ, 50, 60, 40, 40, 1005, 290, 25, 85, 0, L, 0, , 10, 6, SANDY, 13
AL, 18, 2012102418, , BEST, 0, 177N,767W, 75, 972, HU, 64, NEQ, 25, 30, 20, 25, 1005, 290, 25, 85, 0, L, 0, , 10, 6, SANDY, 13
Loading

0 comments on commit 1c05e8d

Please sign in to comment.