-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClimate App.py
More file actions
195 lines (155 loc) · 7.14 KB
/
Climate App.py
File metadata and controls
195 lines (155 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import numpy as np
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
from flask import Flask, jsonify
import datetime as dt
from dateutil.relativedelta import relativedelta
#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///./Resources/hawaii.sqlite")
# Reflect an existing database into a new model.
Base = automap_base()
# Reflect the tables.
Base.prepare(engine, reflect=True)
# Save reference to the tables.
Measurement = Base.classes.measurement
Station = Base.classes.station
# print(Base.classes.keys())
#################################################
# Flask Setup
#################################################
app = Flask(__name__,static_url_path='/Images/surfs-up.png')
#################################################
# Flask Routes
#################################################
# Set the home page,and List all routes that are available. For easy to use I hyperlink the list
@app.route("/")
def home():
"""Available API Routes."""
return (
f"<h1>Welcome to the Climate App API!</h1>"
f"<h1>Step 2 - Climate App</h1>"
f"<h2>Here are the available routes:</h2>"
f"/api/v1.0/precipitation<br/>"
f"/api/v1.0/stations<br/>"
f"/api/v1.0/tobs<br/>"
f"/api/v1.0/start<br/>"
f"/api/v1.0/start/end<br/>"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
"""Query to retrieve the last 12 months of precipitation data and return the results."""
# Create our session (link) from Python to the DB.
session = Session(engine)
# Calculate the date 1 year ago from the last data point in the database.
last_measurement_data_point_tuple = session.query(
Measurement.date).order_by(Measurement.date.desc()).first()
(latest_date, ) = last_measurement_data_point_tuple
latest_date = dt.datetime.strptime(latest_date, '%Y-%m-%d')
latest_date = latest_date.date()
date_year_ago = latest_date - relativedelta(years=1)
# Perform a query to retrieve the data and precipitation scores.
data_from_last_year = session.query(Measurement.date, Measurement.prcp).filter(
Measurement.date >= date_year_ago).all()
session.close()
# Convert the query results to a dictionary using date as the key and prcp as the value.
all_precipication = []
for date, prcp in data_from_last_year:
if prcp != None:
precip_dict = {}
precip_dict[date] = prcp
all_precipication.append(precip_dict)
# Return the JSON representation of dictionary.
return jsonify(all_precipication)
@app.route("/api/v1.0/tobs")
def tobs():
"""Query for the dates and temperature observations from a year from the last data point for the most active station."""
# Create our session (link) from Python to the DB.
session = Session(engine)
# Calculate the date 1 year ago from the last data point in the database.
last_measurement_data_point_tuple = session.query(
Measurement.date).order_by(Measurement.date.desc()).first()
(latest_date, ) = last_measurement_data_point_tuple
latest_date = dt.datetime.strptime(latest_date, '%Y-%m-%d')
latest_date = latest_date.date()
date_year_ago = latest_date - relativedelta(years=1)
# Find the most active station.
most_active_station = session.query(Measurement.station).\
group_by(Measurement.station).\
order_by(func.count().desc()).\
first()
# Get the station id of the most active station.
(most_active_station_id, ) = most_active_station
print(
f"The station id of the most active station is {most_active_station_id}.")
# Perform a query to retrieve the data and temperature scores for the most active station from the last year.
data_from_last_year = session.query(Measurement.date, Measurement.tobs).filter(
Measurement.station == most_active_station_id).filter(Measurement.date >= date_year_ago).all()
session.close()
# Convert the query results to a dictionary using date as the key and temperature as the value.
all_temperatures = []
for date, temp in data_from_last_year:
if temp != None:
temp_dict = {}
temp_dict[date] = temp
all_temperatures.append(temp_dict)
# Return the JSON representation of dictionary.
return jsonify(all_temperatures)
@app.route("/api/v1.0/stations")
def stations():
"""Return a JSON list of stations from the dataset."""
# Create our session (link) from Python to the DB
session = Session(engine)
# Query for stations.
stations = session.query(Station.station, Station.name,
Station.latitude, Station.longitude, Station.elevation).all()
session.close()
# Convert the query results to a dictionary.
all_stations = []
for station, name, latitude, longitude, elevation in stations:
station_dict = {}
station_dict["station"] = station
station_dict["name"] = name
station_dict["latitude"] = latitude
station_dict["longitude"] = longitude
station_dict["elevation"] = elevation
all_stations.append(station_dict)
# Return the JSON representation of dictionary.
return jsonify(all_stations)
@app.route('/api/v1.0/<start>', defaults={'end': None})
@app.route("/api/v1.0/<start>/<end>")
def determine_temps_for_date_range(start, end):
"""Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start or start-end range."""
"""When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date."""
"""When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive."""
# Create our session (link) from Python to the DB.
session = Session(engine)
# If we have both a start date and an end date.
if end != None:
temperature_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
filter(Measurement.date >= start).filter(
Measurement.date <= end).all()
# If we only have a start date.
else:
temperature_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
filter(Measurement.date >= start).all()
session.close()
# Convert the query results to a list.
temperature_list = []
no_temperature_data = False
for min_temp, avg_temp, max_temp in temperature_data:
if min_temp == None or avg_temp == None or max_temp == None:
no_temperature_data = True
temperature_list.append(min_temp)
temperature_list.append(avg_temp)
temperature_list.append(max_temp)
# Return the JSON representation of dictionary.
if no_temperature_data == True:
return f"No temperature data found for the given date range. Try another date range."
else:
return jsonify(temperature_list)
if __name__ == '__main__':
app.run(debug=True)