In this section, you’ll use Python and SQLAlchemy to perform basic climate analysis and data exploration of your climate database. Complete the following tasks by using SQLAlchemy ORM queries, Pandas, and Matplotlib.
- Use SQLAlchemy’s
create_engine
to connect to your SQLite database.
- Use SQLAlchemy’s
automap_base()
to reflect your tables into classes and save a reference to those classes calledStation
andMeasurement
.
- Link Python to the database by creating a SQLAlchemy session.
To perform an analysis of precipitation in the area, do the following:
- Find the most recent date in the dataset.
- Using this date, retrieve the previous 12 months of precipitation data by querying the 12 previous months of data. Select only the
date
andprcp
values.
- Load the query results into a Pandas DataFrame, and set the index to the date column. Sort the DataFrame values by
date
.
- Plot the results by using the DataFrame
plot
method.
- Use Pandas to print the summary statistics for the precipitation data.
To perform an analysis of stations in the area, do the following:
- Design a query to calculate the total number of stations in the dataset.
-
Design a query to find the most active stations (the stations with the most rows).
-
Design a query to retrieve the previous 12 months of temperature observation data (TOBS).
-
Close out your session.
Now that you have completed your initial analysis, you’ll design a Flask API based on the queries that you have just developed.
Creating the Database connection
Use Flask to create your routes, as follows:
-
/
-
/api/v1.0/precipitation
-
/api/v1.0/stations
-
/api/v1.0/tobs
-
/api/v1.0/<start>
and/api/v1.0/<start>/<end>
-
Return a JSON list of the minimum temperature, the average temperature, and the maximum temperature for a given start or start-end range.
-
When given the start only, calculate
TMIN
,TAVG
, andTMAX
for all dates greater than or equal to the start date. -
When given the start and the end date, calculate the
TMIN
,TAVG
, andTMAX
for dates from the start date through the end date.
-
Menne, M.J., I. Durre, R.S. Vose, B.E. Gleason, and T.G. Houston, 2012: An overview of the Global Historical Climatology Network-Daily Database. Journal of Atmospheric and Oceanic Technology, 29, 897-910, https://doi.org/10.1175/JTECH-D-11-00103.1
© 2022 Trilogy Education Services, a 2U, Inc. brand. All Rights Reserved.