knitr::opts_chunk$set(warning = FALSE, message = FALSE, fig.width = 12, fig.height = 6, fig.path = "README_figures/README-")
The goal of wtkapi is to get data from the Gridded Atmospheric Wind Integration National Dataset Toolkit in tidy format to leverage technical analysis of atmospheric data.
It is required to sign up for an API key on the NREL Developer Network. Please also consider the API call rate limits.
You can install the development version of wtkapi from GitHub with:
# install.packages("devtools") devtools::install_github("floresfdev/wtkapi")
At the moment this package is heavy on dependencies (see Imports
on DESCRIPTION), so in order to have more control over your environment, avoid install/upgrade dependencies with:
devtools::install_github("floresfdev/wtkapi", dependencies = FALSE, upgrade_dependencies = FALSE)
This is a basic example which shows you how to get data:
library(dplyr) library(lubridate) library(ggplot2) library(glue) library(wtkapi) # API parameters endpoint <- "https://developer.nrel.gov/api/hsds/" host <- "/nrel/wtk-us.h5" api_key <- readr::read_lines(here::here("api_key.txt")) # Get the info of the available datasets datasets <- get_datasets(endpoint, host, api_key) # Info to select the year 2007, with 1-hour resolution data datetime_info <- compute_datetime_info(datetime_from = "2007-01-01 00:00:00", datetime_to = "2007-12-31 23:00:00", datetime_step = 1) # Info to select the location: New York latitude <- 40.7128 longitude <- -74.0059 # Info to select the datasets as columnar data datasets$title dataset_titles <- c("temperature_10m", "windspeed_10m", "winddirection_10m", "temperature_40m", "windspeed_40m", "winddirection_40m", "temperature_80m", "windspeed_80m", "winddirection_80m") # Get data from the API wind_temp_2007 <- get_dataset(datasets, api_key, dataset_titles, datetime_info, latitude, longitude) # Number of rows: 8760 = 365 days * 24 hours nrow(wind_temp_2007) # Check first and last records head(wind_temp_2007) tail(wind_temp_2007) # Plots plot_subtitle <- glue("2007, New York (lat {latitude}, lon {longitude})") ## Daily min/max temperature at 10m wind_temp_2007 %>% ## Convert temperature from Kelvin to Celsius mutate(temperature_10m = temperature_10m - 273.15) %>% ## Get the min and max temperature for each day mutate(date = as_date(datetime)) %>% group_by(date) %>% summarize(min_temperature_10m = min(temperature_10m), max_temperature_10m = max(temperature_10m)) %>% ## Plot timeseries ggplot() + geom_line(aes(x = date, y = min_temperature_10m), color = "blue") + geom_line(aes(x = date, y = max_temperature_10m), color = "red") + labs(x = "Date", y = "Temperature (°C)", title = "Daily min/max temperature at 10m", subtitle = plot_subtitle, caption = "Blue = daily min - Red = daily max") + theme_classic() ## Daily average wind speed at 80m wind_temp_2007 %>% ## Get the average of windspeed for each day mutate(date = as_date(datetime)) %>% group_by(date) %>% summarize(avg_windspeed_80m = mean(windspeed_80m)) %>% ## Plot timeseries ggplot() + geom_line(aes(x = date, y = avg_windspeed_80m)) + labs(x = "Date", y = "Wind speed (m/s)", title = "Daily average wind speed at 80m", subtitle = plot_subtitle) + theme_classic() ## Histogram of hourly wind speed at 80m ggplot(wind_temp_2007) + geom_histogram(aes(x = windspeed_80m), fill = "white", color = "black") + labs(x = "Wind speed (m/s)", y = "Count", title = "Histogram of hourly wind speed at 80m", subtitle = plot_subtitle) + theme_classic()
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Copyright (c) 2018, Fernando Flores
License TBD.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.