Skip to content

Commit

Permalink
Bug fix/get filedeck fromfile (#74)
Browse files Browse the repository at this point in the history
* adding option to specify file_deck and advisory when read from file. added warning if not specified 

* changed `test_vortex_track_no_internet` test so that one track (`track_2`) is read without specifying `file_deck` and is therefore not equal to `track_1`

* correcting advisories declaration for functions

* improving docstring for advisories
  • Loading branch information
WPringle authored Apr 5, 2023
1 parent 123b9ef commit 9cb5d77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
24 changes: 20 additions & 4 deletions stormevents/nhc/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import pathlib
import re
import warnings
from datetime import datetime
from datetime import timedelta
from functools import partial
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(
:param start_date: start date of track
:param end_date: end date of track
:param file_deck: ATCF file deck; one of `a`, `b`, `f`
:param advisories: ATCF advisory types; one of `BEST`, `OFCL`, `OFCP`, `HMON`, `CARQ`, `HWRF`
:param advisories: ATCF advisory type; one of ``BEST``, ``OFCL``, ``OFCP``, ``HMON``, ``CARQ``, ``HWRF``
>>> VortexTrack('AL112017')
VortexTrack('AL112017', Timestamp('2017-08-30 00:00:00'), Timestamp('2017-09-13 12:00:00'), <ATCF_FileDeck.BEST: 'b'>, <ATCF_Mode.HISTORICAL: 'ARCHIVE'>, [<ATCF_Advisory.BEST: 'BEST'>], None)
Expand Down Expand Up @@ -115,15 +116,15 @@ def from_storm_name(
start_date: datetime = None,
end_date: datetime = None,
file_deck: ATCF_FileDeck = None,
advisories: [ATCF_Advisory] = None,
advisories: List[ATCF_Advisory] = None,
) -> "VortexTrack":
"""
:param name: storm name
:param year: storm year
:param start_date: start date of track
:param end_date: end date of track
:param file_deck: ATCF file deck; one of ``a``, ``b``, ``f``
:param advisories: ATCF advisory type; one of ``BEST``, ``OFCL``, ``OFCP``, ``HMON``, ``CARQ``, ``HWRF``
:param advisories: list of ATCF advisory types; valid choices are: ``BEST``, ``OFCL``, ``OFCP``, ``HMON``, ``CARQ``, `HWRF``
>>> VortexTrack.from_storm_name('irma', 2017)
VortexTrack('AL112017', Timestamp('2017-08-30 00:00:00'), Timestamp('2017-09-13 12:00:00'), <ATCF_FileDeck.BEST: 'b'>, [<ATCF_Advisory.BEST: 'BEST'>], None)
Expand All @@ -146,24 +147,39 @@ def from_file(
path: PathLike,
start_date: datetime = None,
end_date: datetime = None,
file_deck: ATCF_FileDeck = None,
advisories: List[ATCF_Advisory] = None,
) -> "VortexTrack":
"""
:param path: file path to ATCF data
:param start_date: start date of track
:param end_date: end date of track
:param file_deck: ATCF file deck; one of ``a``, ``b``, ``f``
:param advisories: list of ATCF advisory types; valid choices are: ``BEST``, ``OFCL``, ``OFCP``, ``HMON``, ``CARQ``, `HWRF``
>>> VortexTrack.from_file('tests/data/input/test_vortex_track_from_file/AL062018.dat')
VortexTrack('AL062018', Timestamp('2018-08-30 06:00:00'), Timestamp('2018-09-18 12:00:00'), None, <ATCF_Mode.HISTORICAL: 'ARCHIVE'>, ['BEST', 'OFCL', 'OFCP', 'HMON', 'CARQ', 'HWRF'], PosixPath('/home/zrb/Projects/StormEvents/tests/data/input/test_vortex_track_from_file/AL062018.dat'))
>>> VortexTrack.from_file('tests/data/input/test_vortex_track_from_file/irma2017_fort.22')
VortexTrack('AL112017', Timestamp('2017-09-05 00:00:00'), Timestamp('2017-09-12 00:00:00'), None, <ATCF_Mode.HISTORICAL: 'ARCHIVE'>, ['BEST', 'OFCL', 'OFCP', 'HMON', 'CARQ', 'HWRF'], PosixPath('/home/zrb/Projects/StormEvents/tests/data/input/test_vortex_track_from_file/irma2017_fort.22'))
"""

if file_deck is None and advisories is None:
warnings.warn(
"It is recommended to specify the file_deck and/or advisories when reading from file"
)

try:
path = pathlib.Path(path)
except:
pass

return cls(storm=path, start_date=start_date, end_date=end_date)
return cls(
storm=path,
start_date=start_date,
end_date=end_date,
file_deck=file_deck,
advisories=advisories,
)

@property
def name(self) -> str:
Expand Down
8 changes: 3 additions & 5 deletions tests/test_nhc.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def test_vortex_track_no_internet():
with pytest.raises((ConnectionError, SocketBlockedError)):
VortexTrack(storm="al062018", start_date="20180911", end_date=None)

track_1 = VortexTrack.from_file(input_directory / "fort.22")
track_1 = VortexTrack.from_file(input_directory / "fort.22", file_deck="b")
track_1.to_file(output_directory / "vortex_1.22", overwrite=True)

track_2 = VortexTrack.from_file(track_1.filename)
Expand All @@ -266,10 +266,8 @@ def test_vortex_track_no_internet():
track_3 = copy(track_1)
track_3.to_file(output_directory / "vortex_3.22", overwrite=True)

assert track_1 == track_2
assert (
track_1 != track_3
) # these are not the same because of the velocity recalculation
assert track_1 != track_2 # because file_deck is not specified for track_2
assert track_1 == track_3

check_reference_directory(output_directory, reference_directory)

Expand Down

0 comments on commit 9cb5d77

Please sign in to comment.