11from datetime import datetime , timedelta , timezone
22import os
33
4+ import logging
45import netCDF4
56
67import numpy as np
1213import cdippy .url_utils as uu
1314
1415
16+ logger = logging .getLogger (__name__ )
17+
18+
1519class CDIPnc :
1620 """A base class used by the class StnData for retrieving data from
1721 CDIP netCDF (nc) files located either locally or remotely.
@@ -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
0 commit comments