-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabriel Martins Palma Perez
committed
May 9, 2019
0 parents
commit b192509
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 YOUR NAME | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# climate_indices | ||
## Installation | ||
pip install climate_indices |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import datetime | ||
from subprocess import call | ||
import os | ||
import sys | ||
import requests | ||
import numpy as np | ||
import pandas as pd | ||
|
||
def file_len(fname): | ||
with open(fname) as f: | ||
for i, l in enumerate(f): | ||
pass | ||
return i + 1 | ||
|
||
def exists(URL): | ||
r = requests.head(URL) | ||
return r.status_code == requests.codes.ok | ||
|
||
def create_url(index, source): | ||
""" | ||
Return the valid URL for download | ||
:param variable: string | ||
:param level: string | ||
:param date: datetime | ||
:return: sring | ||
""" | ||
if source == 'NOAA': | ||
base_url = 'https://www.esrl.noaa.gov/psd/data/correlation/{index}.data' | ||
|
||
else: | ||
raise ValueError("Source not supported") | ||
|
||
base_url = base_url.format(index=index) | ||
|
||
return base_url | ||
|
||
def get_data(index, source = 'NOAA'): | ||
URL = create_url(index, source) | ||
|
||
if not exists(URL): | ||
print(URL) | ||
raise ValueError("This URL does not exist") | ||
|
||
|
||
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,flen-1]) | ||
call(['rm', 'temp.txt']) | ||
df = format_data(df) | ||
return df | ||
|
||
|
||
def format_data(df): | ||
colnames=['year'] | ||
[colnames.append(i) for i in range(1,13)] | ||
df.columns=colnames | ||
df = df.set_index('year') | ||
df = df.unstack() | ||
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() | ||
|
||
print(df) | ||
print('{year}-{month}-31'.format(year=df['year'].iloc[-1], month=df['month'].iloc[-1])) | ||
indexes = pd.date_range(start='{year}-{month}-01'.format(year=df['year'].iloc[0], month=df['month'].iloc[0]), | ||
end='{year}-{month}-31'.format(year=df['year'].iloc[-1], month=df['month'].iloc[-1]),freq='M') | ||
print(indexes) | ||
df['time']=indexes | ||
df = df.set_index(time) | ||
df=df.dropna() | ||
return df | ||
|
||
if __name__=='__main__': | ||
df = get_data('nina34') | ||
print(df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from distutils.core import setup | ||
|
||
setup( | ||
name = 'climate_indices', # How you named your package folder (MyLib) | ||
packages = ['climate_indices'], # Chose the same as "name" | ||
version = '0.1', # Start with a small number and increase it with every change you make | ||
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository | ||
description = 'Methods to download NOAA climate indices and format them into a pandas df', # Give a short description about your library | ||
author = 'Gabriel Perez', # Type in your name | ||
author_email = '[email protected]', # Type in your E-Mail | ||
url = 'https://github.com/gabrielmpp/meteomath', # Provide either the link to your github or to your website | ||
download_url = 'https://github.com/gabrielmpp/meteomath/archive/v_0.2.tar.gz', # I explain this later on | ||
keywords = ['METEOROLOGY', 'FLUIDS', 'XARRAY'], # Keywords that define your package best | ||
install_requires=[ # I get to this in a second | ||
'xarray', | ||
'numpy', | ||
], | ||
classifiers=[ | ||
'Development Status :: 3 - Alpha', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package | ||
|
||
'Intended Audience :: Developers', # Define that your audience are developers | ||
'Topic :: Software Development :: Build Tools', | ||
|
||
'License :: OSI Approved :: MIT License', | ||
|
||
'Programming Language :: Python :: 3.6', | ||
], | ||
) |