Before working on this assignment please read these instructions fully. In the submission area, you will notice that you can click the link to Preview the Grading for each step of the assignment. This is the criteria that will be used for peer grading. Please familiarize yourself with the criteria before beginning the assignment.
An NOAA dataset has been stored in the file data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv. The data for this assignment comes from a subset of The National Centers for Environmental Information (NCEI) Daily Global Historical Climatology Network (GHCN-Daily). The GHCN-Daily is comprised of daily climate records from thousands of land surface stations across the globe.
Each row in the assignment datafile corresponds to a single observation.
The following variables are provided to you:
For this assignment, you must:
The data you have been given is near Ann Arbor, Michigan, United States, and the stations the data comes from are shown on the map below.
In [1]:
%matplotlib notebook
In [2]:
import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd
def leaflet_plot_stations(binsize, hashid):
df = pd.read_csv('data/C2A2_data/BinSize_d{}.csv'.format(binsize))
station_locations_by_hash = df[df['hash'] == hashid]
lons = station_locations_by_hash['LONGITUDE'].tolist()
lats = station_locations_by_hash['LATITUDE'].tolist()
plt.figure(figsize=(8,8))
plt.scatter(lons, lats, c='r', alpha=0.7, s=200)
return mplleaflet.display()
leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89')
Out[2]:
In [161]:
import numpy as np
data= pd.read_csv('data/C2A2_data/BinSize_d400.csv')
city = "NEW YORK"
location_hash = data[data['NAME'].str.contains(city)].loc[:,'hash'].values[0]
file_path = 'data/C2A2_data/BinnedCsvs_d400/'+location_hash+'.csv'
city_df = pd.read_csv(file_path)
city_df.head()
#placeholder
#city_df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv')
Out[161]:
In [162]:
city_df['Date'] = pd.to_datetime(city_df['Date'].astype(str), format='%Y-%m-%d')
dates_2005_2014 = city_df[(city_df['Date']>= '2005-01-01') & (city_df['Date'] <= '2014-12-31')]
t_max = dates_2005_2014[dates_2005_2014['Element'] == 'TMAX']
t_min = dates_2005_2014[dates_2005_2014['Element'] == 'TMIN']
t_max['Month'] = dates_2005_2014['Date'].apply(lambda x : x.month)
t_max['Day'] = dates_2005_2014['Date'].apply(lambda x : x.day)
t_max.drop(t_max[(t_max['Day'] == 29) & (t_max['Month'] == 2)].index, inplace=True)
t_min['Month'] = dates_2005_2014['Date'].apply(lambda x : x.month)
t_min['Day'] = dates_2005_2014['Date'].apply(lambda x : x.day)
t_min.drop(t_min[(t_min['Day'] == 29) & (t_min['Month'] == 2)].index, inplace=True)
In [164]:
f = lambda x: x.sort('Data_Value', ascending=True)
grouped_data_min = t_min.groupby(['Month', 'Day']).apply(f)
grouped_data_min = grouped_data_min.groupby(['Month', 'Day']).first().reset_index()
df_min = pd.DataFrame()
df_min['Date'] = grouped_data_min['Date']
df_min['TMIN'] = (grouped_data_min['Data_Value']/10)
df_min.head()
Out[164]:
In [165]:
f = lambda x: x.sort('Data_Value', ascending=False)
grouped_data_max = t_max.groupby(['Month', 'Day']).apply(f)
grouped_data_max = grouped_data_max.groupby(['Month', 'Day']).first().reset_index()
df_max = pd.DataFrame()
df_max['Date'] = grouped_data_max['Date']
df_max['TMAX'] = (grouped_data_max['Data_Value']/10)
df_max.head()
Out[165]:
In [166]:
dates_2015 = city_df[city_df['Date'] >= '2015-01-01']
t_max_2015 = dates_2015[dates_2015['Element'] == 'TMAX']
t_min_2015 = dates_2015[dates_2015['Element'] == 'TMIN']
t_max_2015['Month'] = dates_2015['Date'].apply(lambda x : x.month)
t_max_2015['Day'] = dates_2015['Date'].apply(lambda x : x.day)
t_max_2015.drop(t_max_2015[(t_max_2015['Day'] == 29) & (t_max_2015['Month'] == 2)].index, inplace=True)
t_min_2015['Month'] = dates_2015['Date'].apply(lambda x : x.month)
t_min_2015['Day'] = dates_2015['Date'].apply(lambda x : x.day)
t_min_2015.drop(t_min_2015[(t_min_2015['Day'] == 29) & (t_min_2015['Month'] == 2)].index, inplace=True)
In [167]:
f = lambda x: x.sort('Data_Value', ascending=True)
grouped_data_min_2015 = t_min_2015.groupby(['Month', 'Day']).apply(f)
grouped_data_min_2015 = grouped_data_min_2015.groupby(['Month', 'Day']).first().reset_index()
df_min_2015 = pd.DataFrame()
df_min_2015['Date'] = grouped_data_min_2015['Date']
df_min_2015['TMIN'] = (grouped_data_min_2015['Data_Value']/10)
df_min_2015.head()
Out[167]:
In [168]:
f = lambda x: x.sort('Data_Value', ascending=False)
grouped_data_max_2015 = t_max_2015.groupby(['Month', 'Day']).apply(f)
grouped_data_max_2015 = grouped_data_max_2015.groupby(['Month', 'Day']).first().reset_index()
df_max_2015 = pd.DataFrame()
df_max_2015['Date'] = grouped_data_max_2015['Date']
df_max_2015['TMAX'] = (grouped_data_max_2015['Data_Value']/10)
df_max_2015.head()
Out[168]:
In [169]:
result_min = df_min['TMIN']
result_max = df_max['TMAX']
result_min_2015 = df_min_2015['TMIN']
result_max_2015 = df_max_2015['TMAX']
broken_record_max = pd.DataFrame()
broken_record_min = pd.DataFrame()
broken_record_max['TMAX'] = df_max_2015[df_max_2015['TMAX'] > df_max['TMAX']]['TMAX']
broken_record_min['TMIN'] = df_min_2015[df_min_2015['TMIN'] < df_min['TMIN']]['TMIN']
In [176]:
plt.figure(figsize=(10,6))
plt.plot(result_min, '-', result_max, '-', zorder=1)
plt.scatter(broken_record_max.index, broken_record_max['TMAX'], marker='o', c='red', s=20, zorder=2)
plt.scatter(broken_record_min.index, broken_record_min['TMIN'], marker='o', c='red', s=20, zorder=2)
plt.xticks(np.array([0,31,60,91,121,152,182,213,244,274,305,335]),
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
fontsize=10, alpha=0.8)
plt.ylabel('Temperatures in C°', alpha=0.8)
#Not necessary
#plt.xlabel('Months', alpha=0.8)
plt.title('10 years Low & High temp. and 2015 breaking temp. for New York City', alpha=0.8)
plt.legend(['2005-2014 Low Temperatures', '2005-2014 High Temperatures', '2015 Low & High Breaking Temp.'])
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.tick_params(top='off', bottom='off', left='off', right='off', labelleft='on', labelbottom='on')
ax = plt.gca()
ax.fill_between(range(len(res_min)), res_min, res_max, facecolor='blue', alpha=0.15)
Out[176]:
In [ ]: