Skip to content

Commit 6c10ce7

Browse files
committed
Update v0.5.1
Update the API to be more concise and user friendly.
1 parent d8f57fc commit 6c10ce7

5 files changed

Lines changed: 39 additions & 61 deletions

File tree

README.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ ax = rstn.plot()
3232
plt.plot()
3333
```
3434

35-
## Building documentation
36-
37-
```bash
38-
cd docs/
39-
make html
40-
```
41-
42-
or
43-
44-
```bash
45-
cd docs/
46-
sphinx-build -b html source/ build/
47-
```
48-
4935
## Compatibility
36+
5037
The library was tested with Python 3.6, and Python 2.7.14. It'll probably work with other python3 versions, but i don't know if it'll work with Python 2.6 or older versions.

getrstn/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
from .rstn import GetRSTN
1919

2020
name = "getrstn"
21-
__version__ = "0.5"
21+
__version__ = "0.5.1"

getrstn/rstn.py

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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")

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ wget>=3.2
22
numpy>=1.13
33
pandas>=0.21
44
matplotlib>=2.2.2
5-
sphinx>=1.8

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="getrstn",
8-
version="0.5",
8+
version="0.5.1",
99
author="Edison Neto and Douglas Silva",
1010
author_email="ednetoali@gmail.com",
1111
description="Downloads rstn-1-second data",

0 commit comments

Comments
 (0)