Skip to content

Instantly share code, notes, and snippets.

@godber
Last active December 19, 2015 10:28
Show Gist options
  • Select an option

  • Save godber/5940105 to your computer and use it in GitHub Desktop.

Select an option

Save godber/5940105 to your computer and use it in GitHub Desktop.
Comparing Winter 2013 PHX Temperatures to the 40 year average using Python Pandas and GSOD (https://www.ncdc.noaa.gov/cgi-bin/res40.pl) data.
#!/usr/bin/env python
import pandas as pd
import matplotlib.pyplot as plt
# Read in the CSV Temperature data retrieved from Global Surface Summary of Day
# https://www.ncdc.noaa.gov/cgi-bin/res40.pl
temps = pd.read_csv('phx-temps.csv', index_col=0,
names=['highs', 'lows'],
parse_dates=True)
# Add an Average Column
temps['avg'] = (temps.highs + temps.lows) / 2
# Add a Day of Year Column and then group by that column
# This will allow us to align years by DOY so we can take the mean of each DOY
temps['doy'] = temps.index.dayofyear
temps_group = temps.groupby(temps['doy'])
# Get an array of Days of Year that matches the ROI
roi_doy = temps['20121101':'20130401'].index.dayofyear
# Get the matching ROI DateTime Index
roi_index = temps['20121101':'20130401'].index
# Dataframe containing the 2012-2013 values in the ROI as well as the 40 year
# mean, mapped into the ROI by day of year
df = pd.DataFrame({
'highs': pd.Series(temps['20121101':'20130401'].highs),
'mean_highs': pd.Series(temps_group.highs.mean()[roi_doy].values,
index=roi_index),
'avg': pd.Series(temps['20121101':'20130401'].avg),
'mean_avg': pd.Series(temps_group.avg.mean()[roi_doy].values,
index=roi_index),
'lows': pd.Series(temps['20121101':'20130401'].lows),
'mean_lows': pd.Series(temps_group.lows.mean()[roi_doy].values,
index=roi_index)
})
# Compute Rolling Means
df['highs_10day_ra'] = pd.rolling_mean(df['highs'], 10)
df['avg_10day_ra'] = pd.rolling_mean(df['avg'], 10)
df['lows_10day_ra'] = pd.rolling_mean(df['lows'], 10)
# Begin Plotting
y_max = 90
y_min = 28
df[['avg', 'mean_avg']].plot(ylim=[y_min, y_max], label=['A', 'B'])
plt.title('Phoenix Winter 2013 Average Daily Temperatures')
plt.ylabel('Temperature (F)')
plt.savefig('phxtemps-winter2012-ADT.png')
df[['avg_10day_ra', 'mean_avg']].plot(ylim=[y_min, y_max])
plt.title('Phoenix Winter 2013 Average Daily Temperatures')
plt.ylabel('Temperature (F)')
plt.savefig('phxtemps-winter2012-ADT-ra.png')
df[['lows', 'mean_lows']].plot(ylim=[y_min, y_max])
plt.title('Phoenix Winter 2013 Daily Low Temperatures')
plt.ylabel('Temperature (F)')
plt.savefig('phxtemps-winter2012-lows.png')
df[['lows_10day_ra', 'mean_lows']].plot(ylim=[y_min, y_max])
plt.title('Phoenix Winter 2013 Daily Low Temperatures')
plt.ylabel('Temperature (F)')
plt.savefig('phxtemps-winter2012-lows-ra.png')
df[['highs', 'mean_highs']].plot(ylim=[y_min, y_max])
plt.title('Phoenix Winter 2013 Daily High Temperatures')
plt.ylabel('Temperature (F)')
plt.savefig('phxtemps-winter2012-highs.png')
df[['highs_10day_ra', 'mean_highs']].plot(ylim=[y_min, y_max])
plt.title('Phoenix Winter 2013 Daily High Temperatures')
plt.ylabel('Temperature (F)')
plt.savefig('phxtemps-winter2012-highs-ra.png')
#!/usr/bin/env python
# uses gsod module: https://github.com/imankulov/gsod
from gsod import StationLoader
from gsod import WeatherLoader
# figure out what station identifiers are
#loader = StationLoader()
#stations = loader.get_stations()
#for station in stations:
# if station['station_name'] == 'PHOENIX/SKY HARBOR':
# phx_usaf = station['usaf']
# phx_wban = station['wban']
# PHX Sky Harbor IDs
u = 722780
w = 23183
wloader = WeatherLoader()
for year in range(1950,2014):
weather = wloader.get_weather(u, w, year)
for obs in weather:
print str(obs['date']) + ',' + str(obs['max_temp']) + ',' + str(obs['min_temp'])
Bottleneck==0.6.0
argparse==1.2.1
distribute==0.6.24
ipython==0.13.2
matplotlib==1.2.1
numexpr==2.1
numpy==1.7.1
openpyxl==1.6.2
pandas==0.11.0
python-dateutil==2.1
pytz==2013b
pyzmq==13.1.0
scipy==0.12.0
six==1.3.0
statsmodels==0.5.0
tornado==3.1
wsgiref==0.1.2
xlrd==0.9.2
xlwt==0.7.5
@godber
Copy link
Copy Markdown
Author

godber commented Jul 6, 2013

Note that the requirements file is a bit excessive for just this example. These requirements give a working IPython notebook environment as well as include pandas features not used here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment