Skip to content

Commit 4a237fd

Browse files
re-org package and define public api
config docs
1 parent 6f9ae0b commit 4a237fd

22 files changed

Lines changed: 130 additions & 132 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
.venv
55
.vscode
66
**/.coverage
7+
uv.lock
78

89
# build files
910
**/*.egg-info
10-
**/__pycache__
11+
**/__pycache__
12+
**/build

cdippy/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# import plots library
2+
from . import plots
3+
4+
# import public top-level modules
5+
from . import cdipnc, nchashes, ncstats, ndbc, spectra, stndata
6+
7+
# public API (i.e. "from cdippy import *")
8+
__all__ = ["plots", "cdipnc", "nchashes", "ncstats", "ndbc", "spectra", "stndata"]

cdippy/cdipnc.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from datetime import datetime, timedelta, timezone
22
import os
33

4+
import logging
45
import netCDF4
56

67
import numpy as np
78
import numbers
89
from bisect import bisect_left, bisect_right
910

1011
import cdippy.ndbc as ndbc
11-
import cdippy.utils as cu
12-
import cdippy.url_utils as uu
12+
import cdippy.utils.utils as cu
13+
import cdippy.utils.urls as uu
14+
15+
16+
logger = logging.getLogger(__name__)
1317

1418

1519
class CDIPnc:
@@ -144,7 +148,7 @@ def set_request_info(
144148
is set to True, only nonpub records will be returned.
145149
"""
146150
if start is None:
147-
start = datetime(1975, 1, 1)
151+
start = datetime(1975, 1, 1).replace(tzinfo=timezone.utc)
148152
if end is None:
149153
end = datetime.now(timezone.utc)
150154
self.set_timespan(start, end)
@@ -156,14 +160,19 @@ def set_request_info(
156160
def set_timespan(self, start, end):
157161
"""Sets request timespan"""
158162
if isinstance(start, str):
159-
self.start_dt = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
163+
self.start_dt = datetime.strptime(start, "%Y-%m-%d %H:%M:%S").replace(
164+
tzinfo=timezone.utc
165+
)
160166
else:
161167
self.start_dt = start
162168
if isinstance(end, str):
163-
self.end_dt = datetime.strptime(end, "%Y-%m-%d %H:%M:%S")
169+
self.end_dt = datetime.strptime(end, "%Y-%m-%d %H:%M:%S").replace(
170+
tzinfo=timezone.utc
171+
)
164172
else:
165173
self.end_dt = end
166174
self.start_stamp = cu.datetime_to_timestamp(self.start_dt)
175+
167176
self.end_stamp = cu.datetime_to_timestamp(self.end_dt)
168177

169178
def get_request(self) -> dict:
@@ -365,23 +374,25 @@ def __get_indices(self, times: list, start_stamp: int, end_stamp: int) -> tuple:
365374
e_idx = bisect_right(times, end_stamp, s_idx)
366375
return s_idx, e_idx
367376

368-
def get_nc(self, url: str = None) -> netCDF4.Dataset:
369-
if url is None:
377+
def get_nc(self, url: str = None, retry: bool = False) -> netCDF4.Dataset:
378+
if not url:
370379
url = self.url
371-
# Check if the html page or file exists
372-
if (
373-
url[0:4] == "http" and not uu.url_exists(url + ".html")
374-
) and not os.path.isfile(url):
375-
return None
376380
try:
377-
nc = netCDF4.Dataset(url)
378-
except Exception:
381+
return netCDF4.Dataset(url)
382+
except Exception as e:
379383
# Try again if unsuccessful (nc file not ready? THREDDS problem?)
380-
try:
381-
nc = netCDF4.Dataset(url)
382-
except Exception:
383-
nc = None
384-
return nc
384+
if retry:
385+
logger.warning(
386+
msg=f"Retrying to open dataset at {url} due to an unexpected exception: {e}"
387+
)
388+
try:
389+
return netCDF4.Dataset(url)
390+
except Exception:
391+
pass
392+
logger.exception(
393+
msg=f"Failed to open dataset at {url} due to an unexpected exception: {e}"
394+
)
395+
return None
385396

386397
def byte_arr_to_string(self, b_arr: np.ma.masked_array) -> str:
387398
if np.ma.is_masked(b_arr):
@@ -1027,7 +1038,3 @@ def __init__(self, stn, data_dir=None, org=None):
10271038
"""For parameters see CDIPnc.set_dataset_info."""
10281039
CDIPnc.__init__(self, data_dir)
10291040
self.set_dataset_info(stn, org, "realtimexy")
1030-
1031-
1032-
if __name__ == "__main__":
1033-
pass

cdippy/mobile.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

cdippy/mopdata.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from bisect import bisect_left
33

44
from cdippy.cdipnc import CDIPnc
5-
import cdippy.timestamp_utils as tsu
6-
import cdippy.utils as cu
5+
import cdippy.utils.utils as tsu
6+
import cdippy.utils.utils as cu
77

88

99
class MopData(CDIPnc):
@@ -297,7 +297,3 @@ def get_target_timespan(
297297

298298
# If we get to here there's a problem
299299
return (None, None, None)
300-
301-
302-
if __name__ == "__main__":
303-
pass

cdippy/nchashes.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import cdippy.url_utils as uu
2-
import cdippy.utils as cu
1+
import cdippy.utils.urls as uu
2+
import cdippy.utils.utils as cu
33

44

55
class NcHashes:
@@ -49,7 +49,3 @@ def save_new_hashes(self):
4949

5050
def get_old_hashes(self):
5151
return cu.pkl_load(self.hash_pkl)
52-
53-
54-
if __name__ == "__main__":
55-
pass

cdippy/ncstats.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,3 @@ def make_categorical_flag_var(self, flag_name: str):
116116
self.data[flag_name], categories=self.meta.get_flag_values(flag_name)
117117
)
118118
return cat.rename_categories(self.meta.get_flag_meanings(flag_name))
119-
120-
121-
if __name__ == "__main__":
122-
# - Tests
123-
def t1():
124-
av = NcStats("100p1")
125-
print(av.make_stats())
126-
127-
# t1()

cdippy/ndbc.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import os
44
from datetime import datetime, timezone
55

6-
import cdippy.url_utils as uu
7-
import cdippy.utils as cu
6+
import cdippy.utils.urls as uu
7+
import cdippy.utils.utils as cu
88

99
sos_base = "https://sdf.ndbc.noaa.gov/sos/server.php"
1010
request = "request=DescribeSensor"
@@ -42,7 +42,3 @@ def get_wmo_id(stn):
4242
return ids[stn]
4343
else:
4444
return None
45-
46-
47-
if __name__ == "__main__":
48-
pass

cdippy/plots/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import annual_hs_boxplot, compendium, sst_climatology
2+
3+
__all__ = ["annual_hs_boxplot", "compendium", "sst_climatology"]

cdippy/plots/compendium.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import cdippy.utils as ut
2-
import cdippy.plots.plot_utils as pu
1+
import cdippy.utils.utils as ut
2+
import cdippy.plots.utils as pu
33
from datetime import datetime
44
import numpy as np
55
import calendar

0 commit comments

Comments
 (0)