knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(airportweather)

Introduction

The National Weather Service site https://w1.weather.gov/xml/current_obs/ provides access to observed current weather conditions for about 2,367 locations across 58 US States and Territories.

knitr::include_graphics("../man/figures/select_state_location.png")

With the XML link for an airport code (e.g., KAMW), users can access current weather report for the airport location. Different kinds of weather associated data are shown on the webpage. For instance, the page displays

knitr::include_graphics("../man/figures/Ames_current.png")

The XML file contains more than 20 different types of weather data, as well as the location and time information of the observations. Furthermore, a link to "2 Day History" gives access of an HTML page containing weather history data for the past 72 hours.

knitr::include_graphics("../man/figures/2dayhist.png")

Thanks to the well-organized weather data structure from the National Weather Service website, the user can easily explore both the spatial and temporal aspect of weather of selected locations. The site also provides the Zip files containing all XML files of the current and updated hourly. However, people may be only interested in some selected sites and do not bother to download big whole data. Also, the XML and HTML formats need to be converted to data frames before downstream analysis. Web scraping is a natural idea to handle this. The package airportweather is designed as an efficient tool to scrape, extract, integrate and visualize the weather data of airport locations. The advantage of the package is it scrapes real-time data from the web when calling functions in the package, so the most up-to-date data is obtained.

Package data preparation

The primer key of the weather data is 4-letter airport code of airport locations. Scraping from the root page https://w1.weather.gov/xml/current_obs/ , we have collected all current airport code together with the name of the location and the state/territory abbreviations correspond to the code. The data set all_code.rda contains 2,367 US airport codes from 58 states/territories. This data is used as the package example data to provide currently valid codes of US airports. However, if new codes are added in the National Weather Service site, the airportweather package will still work as long as the format of webpage and structure of the site keeps the same.

data(package = "airportweather", "all_code")
kableExtra::kable(head(all_code)) %>% kableExtra::kable_styling(bootstrap_options = "striped", full_width = F )

Installation

You can download the package airportweather from github , then install (build) it with ctrl+shift+B in Rstudio;

Or you can also install the package using devtools:

devtools::install_github("floatgreen/airportweather")

Functions

current_weather()

The current_weather function is the basic function for data retrieving. It reads the weather information of an airport from XML file on National Weather Service website (e.g., https://w1.weather.gov/xml/current_obs/KAMW.xml), and output a single row data frame of location, station_id, latitude, longitude, observation_time, and selected types of the weather elements.

Description of input parameters is as follows:

|Parameter|Description|Type| |:---|:---|:---| |id| one 4-letter airport code |chr| |type|a vector of weather elements|chr vector|

The possible types of weather elements the function can deal with are

Below is an example of calling this function to retrieve current "wind_mph", "temp_f", "relative_humidity" in "Ames, Ames Municipal Airport, IA (airport code "KAMW")".

 current_weather("KAMW", c("wind_mph", "temp_f", "relative_humidity"))
c_weather <- current_weather("KAMW", c("wind_mph", "temp_f", "relative_humidity"))

kableExtra::kable(c_weather) %>% kableExtra::kable_styling(bootstrap_options = "striped", full_width = F )

current_weather_more()

The current_weather_more function calls the current_weather function to retrieve weather elements from multiple airports, and output integrated data frame.

Description of input parameters is as follows:

|Parameter|Description|Type| |:---|:---|:---| |id|a vector of 4-letter airport code |chr vector| |type|a vector of weather elements|chr vector|

Below is an example of calling this function to retrieve current "wind_mph", "temp_f", "relative_humidity" in three locations:

"Ames, Ames Municipal Airport, IA (airport code "KAMW")", "Cedar Rapids Airport, IA (airport code "KCID")", and "Des Moines International Airport, IA (airport code "KDSM")".

current_weather_more(c("KAMW", "KCID", "KDSM"), c("wind_mph", "temp_f", "relative_humidity"))
c_weather<- current_weather_more(c("KAMW", "KCID", "KDSM"), c("wind_mph", "temp_f", "relative_humidity"))
kableExtra::kable(c_weather) %>% kableExtra::kable_styling(bootstrap_options = "striped", full_width = F )

plot_weather_cat()

Theplot_weather_cat function plot one categorical weather component of several airports on the map of US.

Description of input parameters is as follows:

|Parameter|Description|Type| |:---|:---|:---| |id_vector|a vector of multiple airport codes|chr vector| |type|a weather element |chr| |label|label= TRUE: label airport code on the plot|logical|

Below is an example to plot a categorical weather component, weather.

plot_weather_cat(c("KAMW", "KAIO", "KCID", "KCNC"),
            type = "weather",
            label = TRUE)

plot_weather_num()

Theplot_weather_num function plot one numeric element weather component of several airports on the map of US.

Description of input parameters is as follows:

|Parameter|Description|Type| |:---|:---|:---| |id_vector|a vector of multiple airport codes|chr vector| |type|a weather element |chr| |label|label= TRUE: label airport code on the plot|logical| |number|number= TRUE: this element is numeric|logical|

Below is an example to plot a numerical/continuous weather component, such as "temp_f".

plot_weather_num(c("KAMW", "KAIO", "KCID", "KCNC"),
            type = "temp_f",
            label = TRUE)

plot_weather_us()

The plot_weather_us function plot one component of all airports and the user's location on the map of the US with contour lines. The component has to be continuous, such as "temp_c".

Description of input parameters is as follows:

|Parameter|Description|Type| |:---|:---|:---| |type|a weather elements|chr| |you.long| user provided longitude |num| |you.lat| user provided latitude |num| |bin|the binwidth of the contour lines|num|

plot_weather_us(type = "temp_c",bin = 0.5)

It takes a relatively long time to run this function. Below is a previous result as a demonstration.

knitr::include_graphics("../man/figures/plot_weather_us-1.png")

obhistory()

The function obhistory is to retrieve 3 days history of weather and temperature of one location, as in general, the "weather condition" (Sunny, Fog, etc..) and "air temperature" ( in degrees Fahrenheit) are the two most important elements in weather data. The input of this function is the id, one 4-letter airport code; the output is a data frame of 9 columns.

obhistory("KAMW")
KAMW_Table <- obhistory("KAMW")
kableExtra::kable(head(KAMW_Table)) %>% kableExtra::kable_styling(bootstrap_options = "striped", full_width = F )

The description of each column is as follows:

|Variable|Description|Type| |:---|:---|:---| |code|one 4-letter airport code|chr| |loc_name|name of the airport location|chr| |date|the same as in the original html table|chr| |time|the same as in the original html table|chr| |localtime|the local time|chr| |time_UTC|UTC time|POSIXct| |Weather|the Weather|factor| |Temperature|air temperature|num| |hday|define the 1st, 2nd and 3rd 24 hours|factor|

Note that the returned data frame has a various number of rows, as the observation frequency is not fixed at each airport site, and the observation time points are not evenly distributed. So it will be important that the table contains detailed time information. A POSIXct formatted time variable (in UTC) is also generated based on data. If the user needs to compare data from different time zones, the UTC time will make things tidier.

plot_temp_history()

The function plot_temp_history is to generate the plot of air temperature for 3 days until the current time. The resulting plot has the airport location name in the title, local hours labeled on the X axis, and 3 lines each for one 24-hour.

plot_temp_history("KAMW")
knitr::include_graphics("../man/figures/temp_plot_full_size.png")

runShiny()

A shiny app was designed to demonstrate selected functions in the airportweather package. The spatial visualization tab, "Weather information on map", call current_weather_more to show selected weather variables for single or multiple airports on interactive Maps with leaflet, and generate an integrated table for multiple locations as well.

Launch shiny spp,

runShiny()

and you should see the panel as below:

knitr::include_graphics("../man/figures/launch.png")

Choose the airport code by typing the letter or just choose from the menu. When you type letter, the menu will show all choice with the letters. You can choose multiple airports.

knitr::include_graphics("../man/figures/code.png")

Choose the weather feature by clicking on the box. You can also choose several weather feature on this panel.

knitr::include_graphics("../man/figures/weather.png")

After selection of airport code and weather feature, a map with the airport position will be shown below the tab Weather information on map. The map shows the airports you selected, with the selected weather information on the points. You can also drag, zoom in or zoom out the map.

knitr::include_graphics("../man/figures/map.png")

Below the map, you can also see a table with selected weather information as well as airport location information for the selected airport.

knitr::include_graphics("../man/figures/info.png")

When you switch to the tab History weather at one location, you will see a reminder if you choose more than one location. The tab shows the latest 72h temperature for an airport, so you can only choose one location.

knitr::include_graphics("../man/figures/history.png")

The history temperature plot shows 3 days temperature records, which presented as three different colors.

knitr::include_graphics("../man/figures/temp_plot.png")

A detailed temperature history table is shown below the temperature plot. It includes all temperature records for every hour. You can also search a certain time, weather or temperature in the search box.

knitr::include_graphics("../man/figures/temp_table.png")

Future work

Combined with flights information, our package can be used to evaluate the performance of airports and airlines. By analyzing the airport weather data and flight information data, we can find out which airport and which airline has the most delays due to weather, which weather components cause the most flight delays, and so on.



floatgreen/airportweather documentation built on May 14, 2019, 3:06 a.m.