Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Martins Palma Perez committed May 10, 2019
1 parent d4c0ebc commit 9447209
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions climIndices/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import numpy as np
import pandas as pd



def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
Expand Down Expand Up @@ -45,13 +47,18 @@ def get_data(index, source = 'NOAA'):

call(["curl","-s", "-o", 'temp.txt', URL], stdout=open(os.devnull, 'wb'))
flen = file_len('temp.txt')
df = pd.read_csv('temp.txt',sep='\s+', skiprows=[0],error_bad_lines=False)
df = pd.read_csv('temp.txt',sep='\s+', skiprows=[0], header = None)
df_nan = df[df.isnull().any(1)]
string_nan = df_nan.iloc[0,0]
print(string_nan)

df =df.dropna()
call(['rm', 'temp.txt'])
df = format_data(df, index)
df = format_data(df, index, string_nan)
return df


def format_data(df, index):
def format_data(df, index, string_nan):
colnames=['year']
[colnames.append(i) for i in range(1,13)]
df.columns=colnames
Expand All @@ -60,16 +67,18 @@ def format_data(df, index):
df = df.reset_index()
df.columns = ['month','year','value']
df = df.sort_values(['year','month'])
df = df.replace('-99.99', np.NaN)
df = df.dropna()
#df = df.replace('-99.99', np.NaN)
#df = df.dropna()

indexes = pd.date_range(start='{year:0d}-{month}-01'.format(year=int(df['year'].iloc[0]), month=int(df['month'].iloc[0])),
end='{year:0d}-{month}-31'.format(year=int(df['year'].iloc[-1]), month=int(df['month'].iloc[-1])),freq='M')
df['time']=indexes
df = df.set_index('time')
df = df.drop(['month','year'], axis=1)
df.columns = [index]
df = df.replace(-99.99, np.NaN)
df[index] = df[index].astype(float)
df = df.replace(float(string_nan), np.NaN)

df = df.dropna()
return df

Expand Down
Binary file modified climIndices/tools.pyc
Binary file not shown.

0 comments on commit 9447209

Please sign in to comment.