@@ -35,19 +35,21 @@ class GetRSTN(object):
3535 Where the files are/will be stored.
3636 station: str
3737 Station (default: Sagamore Hill).
38+ dataframe: pandas.DataFrame
39+ The dataframe containing the data. (default: None)
3840
3941 """
4042
4143 def __init__ (self , day , month , year , path , station = 'Sagamore Hill' ):
42- self ._day = self .__format_day (day )
43- self ._month = self .__format_month (month )
44- self ._year = str (year )
45-
46- self ._path = str (path )
47- if self ._path [- 1 ] != "/" :
48- self ._path += "/"
49- if not os .path .exists (self ._path ):
50- os .mkdir (self ._path )
44+ self .day = self .__format_day (day )
45+ self .month = self .__format_month (month )
46+ self .year = str (year )
47+
48+ self .path = str (path )
49+ if self .path [- 1 ] != "/" :
50+ self .path += "/"
51+ if not os .path .exists (self .path ):
52+ os .mkdir (self .path )
5153 self ._station = station
5254 self ._filename = None
5355 self .dataframe = None
@@ -65,6 +67,10 @@ def __init__(self, day, month, year, path, station='Sagamore Hill'):
6567 "lower" : "apl" , "upper" : "APL"
6668 }
6769 }
70+ self .frequencies_columns = [
71+ "f245" , "f410" , "f610" , "f1415" ,
72+ "f2695" , "f4995" , "f8800" , "f15400"
73+ ]
6874
6975 def __format_day (self , day ):
7076 """Formats the day as a string in the format dd.
@@ -135,12 +141,12 @@ def __format_station_for_url(self, station):
135141
136142 def __cast_to_int16 (self , number ):
137143 """Casts a number to the numpy int16 type.
138-
144+
139145 Parameters
140146 ----------
141147 number: int or str
142148 The number that will be changed.
143-
149+
144150 Returns
145151 -------
146152 number: np.int16 or np.nan
@@ -171,7 +177,7 @@ def __change_month_upper(self):
171177 ]
172178
173179 # Returns the corresponding month to download the file.
174- index = int (self ._month ) - 1
180+ index = int (self .month ) - 1
175181 return months [index ]
176182
177183 def __change_month_lower (self ):
@@ -190,7 +196,7 @@ def __change_month_lower(self):
190196 ]
191197
192198 # Returns the corresponding month to download the file.
193- index = int (self ._month ) - 1
199+ index = int (self .month ) - 1
194200 return months [index ]
195201
196202 def __set_file_extension_upper (self , file_gzip = True ):
@@ -248,7 +254,7 @@ def file_exists(self):
248254
249255 """
250256
251- files = os .listdir (self ._path )
257+ files = os .listdir (self .path )
252258 filename_upper = self .__set_filename (
253259 True ) + self .__set_file_extension_upper (False )
254260 filename_lower = self .__set_filename (
@@ -277,9 +283,9 @@ def __set_filename(self, upper):
277283 """
278284
279285 if upper :
280- filename = self ._day + self .__change_month_upper () + self ._year [2 :]
286+ filename = self .day + self .__change_month_upper () + self .year [2 :]
281287 else :
282- filename = self ._day + self .__change_month_lower () + self ._year [2 :]
288+ filename = self .day + self .__change_month_lower () + self .year [2 :]
283289
284290 return filename
285291
@@ -309,12 +315,12 @@ def __set_url(self, upper):
309315
310316 url = "https://www.ngdc.noaa.gov/stp/space-weather/solar-data/"
311317 url += "solar-features/solar-radio/rstn-1-second/"
312- url += station_name + '/' + self ._year + '/' + self ._month + '/'
318+ url += station_name + '/' + self .year + '/' + self .month + '/'
313319 url += filename + file_extension
314320
315321 return url
316322
317- def download_data (self ):
323+ def download_file (self ):
318324 """Downloads the file via https.
319325
320326 Returns
@@ -329,59 +335,46 @@ def download_data(self):
329335
330336 """
331337
332- if self .file_exists ():
333- print ("File already downloaded." )
334- return False
335-
336338 # Tries to download with the file extension in upper case.
337339 # Then tries to download with the file extension in lower case.
338340 try :
339341 url = self .__set_url (upper = True )
340342 filename = wget .download (url )
341- os .rename (filename , os .path .join (self ._path , filename ))
343+ os .rename (filename , os .path .join (self .path , filename ))
342344 except HTTPError :
343345 try :
344346 url = self .__set_url (upper = False )
345347 filename = wget .download (url )
346- os .rename (filename , os .path .join (self ._path , filename ))
348+ os .rename (filename , os .path .join (self .path , filename ))
347349 except HTTPError :
348350 raise FileNotFoundOnServer (
349351 "The file on: " + url + " was not found on server." )
350352
351353 self ._filename = filename
352354 return True
353355
354- def get_file_content (self , download = True ):
356+ def decompress_file (self ):
355357 """Gets gzipped file content.
356358
357359 It doesn't decompress the file. It reads the compressed data and
358360 writes it in a new file without the .gz extension.
359361
360- Parameters
361- ----------
362- download: bool, optional
363- Downloads the file or not.
364-
365362 Returns
366363 -------
367364 str
368365 File's final name.
369366
370367 """
371368
372- if download :
373- if not self .download_data ():
374- return False
375-
376- with gzip .open (os .path .join (self ._path , self ._filename ), 'rb' ) as gzipped_file :
369+ with gzip .open (os .path .join (self .path , self ._filename ), 'rb' ) as gzipped_file :
377370 file_content = gzipped_file .read ()
378371 # Separates the .gz extension from the filename.
379372 final_name = self ._filename .split ('.gz' )[0 ]
380- with open (os .path .join (self ._path , final_name ), 'wb' ) as final_file :
373+ with open (os .path .join (self .path , final_name ), 'wb' ) as final_file :
381374 # Saves the content to a new file.
382375 final_file .write (file_content )
383376
384- os .remove (os .path .join (self ._path , self ._filename ))
377+ os .remove (os .path .join (self .path , self ._filename ))
385378 self ._filename = final_name
386379
387380 return final_name
@@ -405,22 +398,23 @@ def read_file(self):
405398 If filename is not set.
406399
407400 """
401+
408402 if self ._filename is None :
409403 raise FilenameNotSetError (
410- "The file " + self ._filename + "has an invalid name." )
404+ "The file " + self ._filename + " has an invalid name." )
411405
412406 rstn_data = {"time" : [], "f245" : [], "f410" : [], "f610" : [],
413407 "f1415" : [], "f2695" : [], "f4995" : [], "f8800" : [],
414408 "f15400" : []
415409 }
416410
417411 # Sets the interval for each frequency column.
418- if int (self ._year ) >= 2008 :
412+ if int (self .year ) >= 2008 :
419413 interval = 7
420414 else :
421415 interval = 6
422416
423- with open (os .path .join (self ._path , self ._filename )) as _file :
417+ with open (os .path .join (self .path , self ._filename )) as _file :
424418 for line in _file .readlines ():
425419 year = int (line [4 :8 ])
426420 month = int (line [8 :10 ])
@@ -470,11 +464,9 @@ def create_dataframe(self):
470464 data = self .read_file ()
471465 except FilenameNotSetError :
472466 raise FileNotFoundError (
473- "The file: " + self ._filename + "was not found." )
467+ "The file " + self ._filename + " was not found." )
474468
475- columns = ["f245" , "f410" , "f610" , "f1415" ,
476- "f2695" , "f4995" , "f8800" , "f15400" ]
477- self .dataframe = pd .DataFrame (data , columns = columns ,
469+ self .dataframe = pd .DataFrame (data , columns = self .frequencies_columns ,
478470 index = data ["time" ])
479471
480472 self .dataframe .index = self .dataframe .index .tz_localize ("UTC" )
0 commit comments