11from collections import OrderedDict as _OrderedDict
2+ from collections import defaultdict as _defaultdict
23from io import BytesIO
34import pandas as pd
45import numpy as np
@@ -236,16 +237,19 @@ def request_data(parameter, filenames, proxy=None):
236237 Data filenames on https://www.ndbc.noaa.gov/data/historical/{parameter}/
237238
238239 proxy: dict
239- Proxy dict passed to python requests, (e.g. proxy_dict= {"http": 'http:wwwproxy.yourProxy:80/'})
240+ Proxy dict passed to python requests,
241+ (e.g. proxy_dict= {"http": 'http:wwwproxy.yourProxy:80/'})
240242
241243 Returns
242244 -------
243245 ndbc_data: dict
244246 Dictionary of DataFrames indexed by buoy and year.
245247 '''
246- assert isinstance (filenames , (pd .Series ,pd .DataFrame )), 'filenames must be of type pd.Series'
248+ assert isinstance (filenames , (pd .Series ,pd .DataFrame )), (
249+ 'filenames must be of type pd.Series' )
247250 assert isinstance (parameter , str ), 'parameter must be a string'
248- assert isinstance (proxy , (dict , type (None ))), 'If specified proxy must be a dict'
251+ assert isinstance (proxy , (dict , type (None ))), ('If specified proxy'
252+ 'must be a dict' )
249253
250254 supported = _supported_params (parameter )
251255 if isinstance (filenames ,pd .DataFrame ):
@@ -254,11 +258,9 @@ def request_data(parameter, filenames, proxy=None):
254258 assert len (filenames )> 0 , "At least 1 filename must be passed"
255259 buoy_data = _parse_filenames (parameter , filenames )
256260 parameter_url = f'https://www.ndbc.noaa.gov/data/historical/{ parameter } '
257- ndbc_data = {}
261+ ndbc_data = _defaultdict ( dict )
258262
259- for buoy_id in buoy_data ['id' ].unique ():
260- ndbc_data_buoy = {}
261-
263+ for buoy_id in buoy_data ['id' ].unique ():
262264 buoy = buoy_data [buoy_data ['id' ]== buoy_id ]
263265 years = buoy .year
264266 filenames = buoy .filename
@@ -270,17 +272,21 @@ def request_data(parameter, filenames, proxy=None):
270272 response = requests .get (file_url , proxies = proxy )
271273 try :
272274 data = zlib .decompress (response .content , 16 + zlib .MAX_WBITS )
273- df = pd .read_csv (BytesIO (data ), sep = '\s+' , low_memory = False )
274- ndbc_data_buoy [year ] = df
275+ df = pd .read_csv (BytesIO (data ), sep = '\s+' , low_memory = False )
275276 except zlib .error :
276- print ('Issue decompressing the NDBC file "' + filename + '". Please re-run your code. It may take several tries to run sucessfully.' )
277-
277+ msg = (f'Issue decompressing the NDBC file { filename } '
278+ f'(id: { buoy_id } , year: { year } ). Please request '
279+ 'the data again.' )
280+ print (msg )
278281 except pandas .errors .EmptyDataError :
279- print ('The NDBC file "' + filename + '" is empty or missing data. Please omit this file from your data request in the future.' )
280-
281-
282- ndbc_data [buoy_id ] = ndbc_data_buoy
283-
282+ msg = (f'The NDBC buoy { buoy_id } for year { year } with '
283+ f'filename { filename } is empty or missing '
284+ 'data. Please omit this file from your data '
285+ 'request in the future.' )
286+ print (msg )
287+ else :
288+ ndbc_data [buoy_id ][year ] = df
289+
284290 if len (ndbc_data ) == 1 :
285291 ndbc_data = ndbc_data [buoy_id ]
286292
0 commit comments