This code-through demonstrates how to retrieve NOAA CO-OPS data via the CO-OPS API using the R package ‘rnoaa’, and how to use the ‘ggplot2’ package to visualize changes in meteorological and hydrological variables during the hurricane season.
First, we load the necessary packages and define the time range for which we want to retrieve the data. We then use the ‘rnoaa’ package to retrieve the air temperature, water temperature, wind speed, air pressure, and water level data for the specified time range.
Install and load package “rnoaa”:
install.packages("rnoaa")library('rnoaa') Set the time window:
begin.date = 20050820
end.date = 20050905Retrieve data using function coops_search():
# retrieve data at site Vaca Key (8723970) during the Hurricane Katrina(Aug.23-31, 2005)
air.temp <- coops_search(station_name = 8723970, begin_date = begin.date,
end_date = end.date, product = "air_temperature")
water.temp <- coops_search(station_name = 8723970, begin_date = begin.date, end_date = end.date, product = "water_temperature")
wind <- coops_search(station_name = 8723970, begin_date = begin.date, end_date = end.date, product = "wind")
air.pres <- coops_search(station_name = 8723970, begin_date = begin.date, end_date = end.date, product = "air_pressure")
water.level <- coops_search(station_name = 8723970, begin_date = begin.date, end_date = end.date, datum = "stnd", product = "water_level")To retrieve data, you need to specify the station name, observation beginning and end dates, and the type of product. For water level products, you also need to specify the datum.
The function returns a list of length two, with the first element being a list of metadata containing the station ID, name, and location (latitude and longitude), and the second element being the data frame with the variable you need.
air.temp[[1]]## $id
## [1] "8723970"
##
## $name
## [1] "Vaca Key, Florida Bay"
##
## $lat
## [1] "24.7110"
##
## $lon
## [1] "-81.1065"
head(air.temp[[2]])In this step, you retrieve the data frame of each variables, remove missing values in the data set and normalize the data using z-score normalization.
station.info <- air.temp[[1]] #get station info
air.temp <- air.temp[[2]]%>% #get data.frame of air temperature
filter(!is.na(v)) %>% #remove missing values
mutate(air.temp_norm = (v - mean(v)) / sd(v))
#z-score normalization
water.temp<- water.temp[[2]]%>% #get data.frame of water temperature
filter(!is.na(v)) %>%
mutate(water.temp_norm = (v - mean(v)) / sd(v))
wind <- wind[[2]]%>% #get data.frame of wind
filter(!is.na(s)) %>%
mutate(wind.speed_norm = (s - mean(s)) / sd(s))
air.pres <- air.pres[[2]]%>% #get data.frame of air pressure
filter(!is.na(v)) %>%
mutate(air.pres_norm = (v - mean(v)) / sd(v))
water.level <- water.level[[2]]%>%
filter(!is.na(v)) %>% #get data.frame of water level
mutate(water.level_norm = (v - mean(v)) / sd(v))Use ‘ggplot2’ to create a set of plots for each variable, which show the changes in each variable over time. We customize each plot by adjusting the color palette, adding appropriate labels and highlighting the hurricane life time.
Install and load package “ggplot2”:
install.packages("ggplot2")library(ggplot2)Create plot that shows the changes in wind speed over time. To customize the plot, you can set the line color in function aes() and highlight the hurricane life time by adding function geom_rect().
plot1 <- ggplot() +
geom_rect(aes(xmin = as.POSIXct("2005-08-23"), xmax =as.POSIXct("2005-08-31"), ymin = -Inf, ymax = Inf), fill = "orange", alpha = 0.5) +
geom_line(data = wind, aes(x = t, y = wind.speed_norm, color = "Wind Speed")) +
scale_color_manual(values = c("Wind Speed" = "steelblue")) +
labs(title = "Wind Speed")
plot1Create plot that shows the changes in air temperature and water temperature over time.
plot2 <- ggplot() +
geom_rect(aes(xmin = as.POSIXct("2005-08-23"), xmax =as.POSIXct("2005-08-31"), ymin = -Inf, ymax = Inf), fill = "orange", alpha = 0.5) +
geom_line(data = air.temp, aes(x = t, y = air.temp_norm, color = "Air Temp")) +
geom_line(data = water.temp, aes(x = t, y = water.temp_norm, color = "Water Temp")) +
scale_color_manual(values = c("Air Temp" = "tomato2", "Water Temp" = "steelblue")) +
labs(title = "Air and Water temperature")
plot2Create plot that shows the changes in air pressure and water level over time.
plot3 <- ggplot() +
geom_rect(aes(xmin = as.POSIXct("2005-08-23"), xmax =as.POSIXct("2005-08-31"), ymin = -Inf, ymax = Inf), fill = "orange", alpha = 0.5) +
geom_line(data = air.pres, aes(x = t, y = air.pres_norm, color = "Air Pressure")) +
geom_line(data = water.level, aes(x = t, y = water.level_norm, color = "Water Level")) +
scale_color_manual(values = c("Air Pressure" = "tomato2", "Water Level" = "steelblue")) +
labs(title = "changes of Air Pressure and Water Level")
plot3To arrange the three plots into a single column, you can use the ‘grid.arrange’ function from the ‘gridExtra’ package.
Install and load the package:
install.packages("gridExtra")library(gridExtra)Using function grid.arrange() arrange the plots. The ‘ncol’ argument specifies the number of columns we want to arrange the plots into.
# Arrange the plots
grid.arrange(plot1, plot2, plot3, ncol = 1)Based on the visualization, we can identify several characteristics of hurricanes, including strong winds, low air and water temperatures, low air pressure, and high water levels.
This code-through has demonstrated how to retrieve and preprocess NOAA CO-OPS data using ‘rnoaa’, and how to use ‘ggplot2’ to visualize changes in meteorological and hydrological variables during the hurricane season. By visually exploring the data, we can gain insights into the characteristics of hurricanes.
reference: https://cran.r-project.org/web/packages/rnoaa/rnoaa.pdf