1
1
"""Module for functions related to the ibtracs database"""
2
2
3
3
from urllib .request import urlretrieve
4
- import os
5
4
import warnings
6
5
import pathlib
7
- from contextlib import contextmanager
6
+
7
+ from . import _csv
8
8
9
9
here = pathlib .Path (__file__ ).parent
10
10
ibdata_dir = here / "_ibtracs_files/"
11
11
12
12
wmo_file = str (ibdata_dir / "wmo.csv" )
13
13
usa_file = str (ibdata_dir / "usa.csv" )
14
14
15
-
16
- @contextmanager
17
- def online (subset , filename = "ibtracs.csv" , clean = True ):
15
+ online_default_kwargs = (
16
+ dict (
17
+ header = 0 ,
18
+ skiprows = [1 ],
19
+ converters = {
20
+ "SID" : str ,
21
+ "SEASON" : int ,
22
+ "BASIN" : str ,
23
+ "SUBBASIN" : str ,
24
+ "LON" : float ,
25
+ "LAT" : float ,
26
+ },
27
+ ),
28
+ )
29
+
30
+
31
+ def load (subset , filename , ** kwargs ):
32
+ if subset .lower () in ["wmo" , "usa" ]:
33
+ return offline (subset )
34
+ else :
35
+ return online (subset , filename = filename , ** kwargs )
36
+
37
+
38
+ def online (subset , filename = None , ** kwargs ):
18
39
"""
19
- Downloads an load into the current workspace the specified ibtracs subset from the IBTrACS archive online.
40
+ Downloads and load into the current workspace the specified ibtracs subset from the
41
+ IBTrACS archive online.
20
42
21
43
Parameters
22
44
----------
@@ -26,35 +48,34 @@ def online(subset, filename="ibtracs.csv", clean=True):
26
48
* ALL: Entire IBTrACS database
27
49
* Specific basins: EP, NA, NI, SA, SI, SP, WP
28
50
* last3years: self-explanatory
29
- * since1980: Entire IBTrACS database since 1980 (advent of satellite era, considered reliable from then on)
30
-
31
- filename : str
32
- (temporary) file to which to save the data
33
- The default is "tmp/ibtracs_ACTIVE.csv".
51
+ * since1980: Entire IBTrACS database since 1980 (advent of satellite era,
52
+ considered reliable from then on)
34
53
35
- clean : bool
36
- If True (default), remove the temporary file after loading the data.
54
+ filename : str, optional
55
+ file to which to save the raw data. None to use a temporary file. Default is
56
+ None
37
57
38
58
Returns
39
59
-------
40
- ib : the IBTrACS subset requested
41
-
60
+ xarray.DataArray
61
+ the IBTrACS subset requested
42
62
"""
43
- # TODO: Make it so that the user does not need to specify the filename
63
+ # Put IBTrACS specific arguments to read_csv second, so it
64
+ # overwrites any arguments passed
65
+ kwargs = {** kwargs , ** online_default_kwargs }
66
+
44
67
url = (
45
- "https://www.ncei.noaa.gov/data/international-best-track-archive-for-climate-stewardship-ibtracs/v04r01/access/csv/ibtracs. "
46
- + subset
47
- + " .list.v04r01.csv"
68
+ "https://www.ncei.noaa.gov/data/"
69
+ "international-best-track-archive-for-climate-stewardship-ibtracs/"
70
+ f"v04r01/access/csv/ibtracs. { subset } .list.v04r01.csv"
48
71
)
49
72
73
+ # filename=None downloads the data to a temporary file
50
74
# Ruff (Flake8 bandit) complains that this url isn't checked, but it explicitly has
51
75
# "https:/" at the start anyway
52
- urlretrieve (url , filename ) # noqa: S310
53
-
54
- yield filename
76
+ filename , _ = urlretrieve (url , filename ) # noqa: S310
55
77
56
- if clean :
57
- os .remove (filename ) # Somehow, this is slower than the rest ofthe function (??)
78
+ return _csv .load (filename , ** kwargs )
58
79
59
80
60
81
def offline (subset = "wmo" ):
@@ -103,9 +124,9 @@ def offline(subset="wmo"):
103
124
which means in particular that wind speeds are in knots and averaged over different time periods.\n \
104
125
For more information, see the IBTrACS column documentation at https://www.ncei.noaa.gov/sites/default/files/2021-07/IBTrACS_v04_column_documentation.pdf"
105
126
)
106
- return wmo_file
127
+ return _csv . load ( wmo_file )
107
128
if subset .lower () in ["usa" , "jtwc" ]:
108
- return usa_file
129
+ return _csv . load ( usa_file )
109
130
110
131
111
132
# TODOS:
0 commit comments