diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..a637189 --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a3bb552 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# climate_indices +## Installation +pip install climate_indices diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/climate_indices/download.py b/climate_indices/download.py new file mode 100644 index 0000000..a392645 --- /dev/null +++ b/climate_indices/download.py @@ -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) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b88034e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..617df08 --- /dev/null +++ b/setup.py @@ -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 = 'gabrielmpp2@gmail.com', # 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', + ], +)