Skip to content

Commit

Permalink
Merge pull request #5 from gabrielmpp/solves_4
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
gabrielmpp authored Nov 26, 2021
2 parents 309179d + 663099d commit d3b8fbb
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions climIndices/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from copy import deepcopy

SOURCES = ['NOAA', 'CPC']
PID = os.getpid()
TMP_FILE_PATH = os.environ['HOME'] + f'/temp_file_climIndices_{PID}.txt'


def file_len(fname):
with open(fname) as f:
Expand Down Expand Up @@ -77,7 +80,7 @@ def download_df(index, source):
print(URL)
raise ValueError(f"URL does not exist for index {index}")

call(["curl", "-s", "-o", 'temp.txt', URL], stdout=open(os.devnull, 'wb'))
call(["curl", "-s", "-o", TMP_FILE_PATH, URL], stdout=open(os.devnull, 'wb'))

assert source in SOURCES, f'source {source} not valid.'
_sources = deepcopy(SOURCES)
Expand All @@ -92,7 +95,7 @@ def format_datetime(x):
download_df(index, source)

try:
df_temp = pd.read_csv('temp.txt', sep='\s+', skiprows=[0], header=None)
df_temp = pd.read_csv(TMP_FILE_PATH, sep='\s+', skiprows=[0], header=None)
except EmptyDataError:
print("Data is empty, trying another source")
else:
Expand All @@ -102,7 +105,7 @@ def format_datetime(x):
except NameError:
raise Exception(f'ClimIndices could not download index {index}')
try:
call(['rm', 'temp.txt'])
call(['rm', TMP_FILE_PATH])
except:
print('Could not remove temp file.')
if source == 'CPC':
Expand All @@ -123,6 +126,6 @@ def format_datetime(x):
import matplotlib.pyplot as plt
plt.style.use('bmh')
df = get_data(['nina34', 'soi'])
df.plot(subplots=True, sharex=True, title='Climate indices', legend='False', figsize=[10, 10])
plt.savefig('../figs/example.png')
plt.close()
# df.plot(subplots=True, sharex=True, title='Climate indices', legend='False', figsize=[10, 10])
# plt.savefig('../figs/example.png')
# plt.close()

0 comments on commit d3b8fbb

Please sign in to comment.