44import gzip
55import datetime as dt
66
7+ class open_gzip_or_dat :
8+ def __init__ (self , filename ):
9+ self .filename = filename
10+
11+ def __enter__ (self ):
12+ if self .filename .endswith ('.gz' ):
13+ self .file = gzip .open (self .filename )
14+ else :
15+ self .file = open (self .filename , 'rb' )
16+ return self .file
17+
18+ def __exit__ (self , exc_type , exc_value , traceback ):
19+ self .file .close ()
20+
721def mask_to_int (mask ):
822 """ Convert object array of mask strings to integers"""
923 if len (mask .shape ) == 0 :
@@ -223,7 +237,7 @@ def __init__(self,filename):
223237 """
224238 self .file = filename
225239
226- with gzip . open (self .file ) as f :
240+ with open_gzip_or_dat (self .file ) as f :
227241 for line_no , line in enumerate (f ):
228242 if line .startswith (b'Analysis program:' ):
229243 analysis_program = line .decode ().split (':' )[1 :]
@@ -305,7 +319,7 @@ def gen_sta_info(self):
305319 of the station names.
306320 """
307321 nstations = self .station_data_start - self .station_info_start - 1
308- with gzip . open (self .file ) as f :
322+ with open_gzip_or_dat (self .file ) as f :
309323 for i in range (self .station_info_start + 1 ):
310324 line = next (f )
311325 for line in range (nstations ):
@@ -323,7 +337,7 @@ def gen_sta_data(self):
323337 """
324338 nstations = self .station_data_start - self .station_info_start - 1
325339
326- with gzip . open (self .file ) as f :
340+ with open_gzip_or_dat (self .file ) as f :
327341 for i in range (self .station_data_start + 1 ):
328342 line = next (f )
329343 for line in range (nstations ):
@@ -348,7 +362,11 @@ def readfile(self):
348362 """
349363 # Read in data. Catch case where there is no data.
350364 try :
351- lmad = pd .read_csv (self .file ,compression = 'gzip' ,delim_whitespace = True ,
365+ if self .file .endswith ('.gz' ):
366+ comp = 'gzip'
367+ else :
368+ comp = None
369+ lmad = pd .read_csv (self .file ,compression = comp ,delim_whitespace = True ,
352370 header = None ,skiprows = self .data_starts + 1 ,on_bad_lines = 'skip' )
353371 lmad .columns = self .names
354372 except pd .errors .EmptyDataError :
0 commit comments