Last active
August 29, 2015 13:57
-
-
Save mbstacy/9606193 to your computer and use it in GitHub Desktop.
Oklahoma Mesonet Pandas DataFrame
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
#!/Users//anaconda/bin/python | |
""" | |
Python main module requires the correct python path.(see above). | |
Args sites startdate enddate | |
sites: string - comma sperated sites | |
startdate: YYY-MM-DD | |
enddate: YYYY-MM-DD | |
example: | |
Single site: | |
./ok_mesonet.py adax 2014-01-01 2014-02-01 | |
Multiple Sites: | |
./ok_mesonet.py adax,nrmn 2014-01-01 2014-02-01 | |
""" | |
import pandas as pd | |
from urllib2 import urlopen | |
from datetime import datetime,timedelta | |
import numpy as np | |
import sys | |
def get_station_data(sites,start_date,end_date): | |
""" | |
returns a pandas dataFrame | |
sites: list of sites | |
start_date: date | |
end_date: date | |
""" | |
url_template ="https://www.mesonet.org/index.php/dataMdfMts/dataController/getFile/%d%02d%02d%s/mts/TEXT/" | |
r = (end_date+timedelta(days=1)-start_date).days | |
dates = [start_date+timedelta(days=i) for i in range(r)] | |
pieces=[] | |
for site in sites: | |
for day in dates: | |
url = url_template % (day.year,day.month,day.day,site) | |
page = urlopen(url) | |
df = pd.read_fwf(page,header=2,widths=[5,6,6,7,7,7,7,6,7,7,7,8,9,6,7,7,7,7,7,7,7,8,8,8],na_values=['-999','-998','-996','-995','-997'],skipfooter=1) | |
df['DATE'] = datetime(day.year,day.month,day.day) | |
pieces.append(df) | |
return pd.concat(pieces) | |
def celcius2Fahrenheit(num): | |
""" Returns Fahrenheit from Celcius""" | |
return (num * 9/5) + 32 | |
if __name__ == "__main__": | |
pd.options.display.max_columns = 30 | |
pd.options.display.max_rows = 5000 | |
site=sys.argv[1].split(',') | |
try: | |
start_date=datetime.strptime(sys.argv[2],"%Y-%m-%d").date() | |
end_date =datetime.strptime(sys.argv[3],"%Y-%m-%d").date() | |
except: | |
raise("Second and third argument must be date format YYYY-MM-DD") | |
df =get_station_data(site,start_date,end_date) | |
print "AIR Temperature Celcius - UTC Time" | |
grouped = df.groupby(['STID',"DATE"]) | |
print grouped['TAIR'].agg([np.min, np.mean, np.max,np.size]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment