Homework 2: Download and Plot Weather Station Data using the Synoptic API - SOLUTIONS
1 Overview
In this exercise, you will import and plot a) temperature and b) precipitation data for a weather station of your choice using the Synoptic API. The outputs will look something like:
2 Preparation
2.1 1. Create a Synoptic Account
Create an account on Synoptic Data (instructions). Either an open access or a commercial trial account will work. Create a private key and public token. You will use your public token when calling the API.
Zoom and pan to find the station of your choice. Try to find a weather station that records both temperature and precipitation (this includes stations in the CIMIS network, RAWS, Global METAR, and others).
Click on a station to view the station ID and data availability.
2.3 3. Read the docs
Read the documentation for the Synoptic time series end point. Take note of:
3 Part 1. Download hourly temperature data, compute daily min and max, and plot
3.1 Create the API Request
Create an API request object for the Synoptic Time Series End Point, getting hourly temperature and precipitation data for the station of your choice from Oct 1, 2023 thru yesterday (i.e., the current water year).
library(httr2)library(lubridate)|>suppressPackageStartupMessages()synoptic_ts_baseurl<-"https://api.synopticdata.com/v2/stations/timeseries"my_token<-readLines("~/My Tokens/my_synoptic_token_hw02.txt", n =1)station_id_chr<-"HSEC1"## Create the start time (midnight Oct 1, 2023 > converted to UTC > converted to a character ## string formatted as YYYYmmddHHMM)start_utc_chr<-make_datetime(year =2023, month =10, day =1, hour =0, min =0, sec =0, tz ="America/Los_Angeles")|>with_tz("UTC")|>format("%Y%m%d%H%M")start_utc_chr
[1] "202310010700"
## Create the end time (11:59pm yesterday >> converted to UTC >> converted to a character ## string formatted as YYYYmmddHHMM)end_utc_chr<-(as_datetime(today()-days(1), tz ="America/Los_Angeles")+hours(23)+minutes(59))|>with_tz("UTC")|>format("%Y%m%d%H%M")end_utc_chr
[1] "202405210659"
weather_vars<-"air_temp"## Create the request object:holister_tas_req<-request(synoptic_ts_baseurl)|>req_headers("Accept"="application/json")|>req_url_query(token =my_token, start =start_utc_chr, end =end_utc_chr, stid =station_id_chr, vars =weather_vars, units ="english", obtimezone ="local", .multi ="comma")holister_tas_req
<httr2_request>
GET
https://api.synopticdata.com/v2/stations/timeseries?token=8a8a02a7adb14d74af68b54e494e30ec&start=202310010700&end=202405210659&stid=HSEC1&vars=air_temp&units=english&obtimezone=local
Headers:
• Accept: 'application/json'
Body: empty
3.2 Perform the request
## Do a dry run# holister_tas_req |> req_dry_run() ## Perform the requestholister_tas_resp<-holister_tas_req|>req_perform()
View the response:
holister_tas_resp
<httr2_response>
GET
https://api.synopticdata.com/v2/stations/timeseries?token=8a8a02a7adb14d74af68b54e494e30ec&start=202310010700&end=202405210659&stid=HSEC1&vars=air_temp&units=english&obtimezone=local
Status: 200 OK
Content-Type: application/json
Body: In memory (179848 bytes)
3.3 Convert the response into a tibble
Extract the body of the response as a list object:
holister_tas_lst<-holister_tas_resp|>resp_body_json()## Inspect the structure## holister_tas_lst |> View()
3.5 Plot the minimum and maximum daily temperature
library(ggplot2)ggplot(holister_tas_dly_tbl, mapping =aes(x =date))+geom_line(mapping =aes(y =tasmin), col ="blue")+geom_line(mapping =aes(y =tasmax), col ="red")+ylab("temp (F)")+xlab("")+labs(title ="Daily Minimum and Maximum Temperature for Hollister, CA", subtitle ="Oct 2023 - May 2024")
4 Part II (Bonus): Download and plot daily prepcipitation
To download the precipitation values, we can re-use most of the contents of our temperature request object, but change the vars parameter to precip, and add precip = 1 to enable derived precipitation (see the Time Series endpoint docs for details).
4.1 Create the request object
holister_pr_req<-request(synoptic_ts_baseurl)|>req_headers("Accept"="application/json")|>req_url_query(token =my_token, start =start_utc_chr, end =end_utc_chr, stid =station_id_chr, vars ="precip", precip =1, ## recommended in the API Docs units ="english", obtimezone ="local", .multi ="comma")holister_pr_req
<httr2_request>
GET
https://api.synopticdata.com/v2/stations/timeseries?token=8a8a02a7adb14d74af68b54e494e30ec&start=202310010700&end=202405210659&stid=HSEC1&vars=precip&precip=1&units=english&obtimezone=local
GET
https://api.synopticdata.com/v2/stations/timeseries?token=8a8a02a7adb14d74af68b54e494e30ec&start=202310010700&end=202405210659&stid=HSEC1&vars=precip&precip=1&units=english&obtimezone=local