Fetching POWER data using `nasapower` for small, single queries is easy and straightforward. However, if you wish to have daily data for a larger area it can be trickier to implement.

Here I demonstrate fetching multiple seasons of rainfall data for two states in Brazil and two growing seasons using `nasapower`. An example of this use case can be found in Emerson Del Ponte’s slide deck from the International Congress of Plant Pathology, 2019, “Can rainfall be a useful predictor of epidemic risk across temporal and spatial scales?”

To get the rainfall data for the states in Brazil, we will use `nasapower` (Sparks 2019), `rnaturalearth` (South 2017), `raster` (Hijmans 2019) packages to fetch the data, `dplyr` (Wickham et al. 2019) for data manipulation and `ggplot2` (Wickham 2016) to visualise the final data.

``````library(rnaturalearth)
library(raster)
library(dplyr)
library(nasapower)
library(ggplot2)
library(purrr)``````

## Data import

To get the state data we will use `rnaturalearth` to download simple features data for Brazil and subset the states Rio Grande do Sul and Paraná into separate objects.

``````BRA <- ne_states(country = "Brazil",
returnclass = "sf")

# subset spatial objects of only the states of interest
RS <- BRA[BRA\$name_en == "Rio Grande do Sul", ]
PR <- BRA[BRA\$name_en == "Paraná", ]``````

## Get the rainfall data

Now that we have objects for the states we can create a raster grid to represent the 0.5 x 0.5 degree grid that is the NASA-POWER data and select only cells that fall within the two states of interest.

### POWER source

Create a grid of 0.5 x 0.5 arc degrees and extract the x, y values from it for each state to use the coordinates to query the POWER data.

``````# create a global 0.5 x 0.5 degree raster object
r <- raster(xmn = -180,
xmx = 180,
ymn = -90,
ymx = 90,
resolution = 0.5)

values(r) <- 1:ncell(r)

plot(r, main = "Full global raster at 0.5 x 0.5 degrees")``````

``````
# Extract the two states, first crop by bounding box, then mask the raster
PR_coords <- crop(r, PR)
RS_coords <- crop(r, RS)

plot(PR_coords, main = "Paraná")
# add the sf object, note the use of indexing to plot only the first column
# to avoid warning messages
plot(PR[, 1], col = NA, add = TRUE)``````

``````
plot(RS_coords, main = "Rio Grande do Sul")
plot(RS[, 1], col = NA, add = TRUE)``````

``````
# extract the centroid values of the cells to use querying the POWER data
PR_coords <- as.data.frame(xyFromCell(PR_coords, 1:ncell(PR_coords)))
RS_coords <- as.data.frame(xyFromCell(RS_coords, 1:ncell(RS_coords)))
names(PR_coords) <- names(RS_coords) <- c("LON", "LAT")
coords <- rbind(PR_coords, RS_coords)``````

WARNING This step is time intensive. WARNING

Using nested `for()` loops, query the NASA-POWER database to gather precipitation data for the states where rust was reported and save a CSV file of the rainfall.

``````
power <- vector(mode = "list", 2) # hold two growing seasons
precip <- vector(mode = "list", nrow(coords)) # hold the cells

seasons <- list(
c("2014-11-01", "2015-03-31"),
c("2015-11-01", "2016-03-31")
)

for (i in seq_along(seasons)) {
# two "seasons" (outer loop 2x)
season <- seasons[[i]]

# inner loop for each pair coords
for (j in seq_along(1:nrow(coords))) {
NA_df <-
data.frame(
LON = coords[1, 1],
LAT = coords[1, 2],
YEAR = NA,
MM = NA,
DD = NA,
DOY = NA,
YYYYMMDD = NA,
PRECTOT = NA
)

p_get_power <- possibly(get_power, otherwise = NA_df)

# 312 coordinate pairs (inner loop 312x)
site <- as.numeric(coords[j,])
power_precip <- p_get_power(
community = "AG",
lonlat = site,
pars = "PRECTOT",
dates = season,
temporal_average = "DAILY"
)
precip[[j]] <- power_precip
Sys.sleep(5) # wait 5 seconds between requests so we don't hammer the server
}
precip_df <- bind_rows(precip)
power[[i]] <- precip_df
}
power_df <- bind_rows(power)

power_df <- data.frame(STATE = c(rep("PR", nrow(PR_coords)),
rep("RS", nrow(RS_coords))),
power_df)``````

## Acknowledgements

These data were obtained from the NASA Langley Research Center POWER Project funded through the NASA Earth Science Directorate Applied Science Program.

## References

Lionel Henry and Hadley Wickham (2019). purrr: Functional Programming Tools. R package version 0.3.2. https://CRAN.R-project.org/package=purrr

Robert J. Hijmans (2019). raster: Geographic Data Analysis and Modeling. R package version 3.0-2. https://CRAN.R-project.org/package=raster

Andy South (2017). rnaturalearth: World Map Data from Natural Earth. R package version 0.1.0. https://CRAN.R-project.org/package=rnaturalearth

Adam Sparks (2018). nasapower: A NASA POWER Global Meteorology, Surface Solar Energy and Climatology Data Client for R. Journal of Open Source Software, 3(30), 1035, https://doi.org/10.21105/joss.01035

Adam Sparks (2019). nasapower: NASA-POWER Data from R. R package version 1.1.2, <URL: https://CRAN.R-project.org/package=nasapower>.

Hadley Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016.

Hadley Wickham, Romain François, Lionel Henry and Kirill Müller (2019). dplyr: A Grammar of Data Manipulation. R package version 0.8.3. https://CRAN.R-project.org/package=dplyr

## Session information

``````sessioninfo::session_info()
#> ─ Session info ─────────────────────────────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 3.6.1 (2019-07-05)
#>  os       macOS Mojave 10.14.6
#>  system   x86_64, darwin15.6.0
#>  ui       RStudio
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Brisbane
#>  date     2019-11-18
#>
#> ─ Packages ─────────────────────────────────────────────────────────────────────────────────────────
#>  package            * version    date       lib source
#>  APSIM                0.9.3      2019-05-22 [1] CRAN (R 3.6.0)
#>  assertthat           0.2.1      2019-03-21 [1] CRAN (R 3.6.0)
#>  backports            1.1.5      2019-10-02 [1] CRAN (R 3.6.0)
#>  bit                  1.1-14     2018-05-29 [1] CRAN (R 3.6.0)
#>  bit64                0.9-7      2017-05-08 [1] CRAN (R 3.6.0)
#>  blob                 1.2.0      2019-07-09 [1] CRAN (R 3.6.0)
#>  callr                3.3.2      2019-09-22 [1] CRAN (R 3.6.0)
#>  class                7.3-15     2019-01-01 [1] CRAN (R 3.6.1)
#>  classInt             0.4-2      2019-10-17 [1] CRAN (R 3.6.0)
#>  cli                  1.1.0      2019-03-19 [1] CRAN (R 3.6.0)
#>  codetools            0.2-16     2018-12-24 [1] CRAN (R 3.6.1)
#>  colorspace           1.4-1      2019-03-18 [1] CRAN (R 3.6.0)
#>  crayon               1.3.4      2017-09-16 [1] CRAN (R 3.6.0)
#>  crul                 0.9.0      2019-11-06 [1] CRAN (R 3.6.0)
#>  curl                 4.2        2019-09-24 [1] CRAN (R 3.6.0)
#>  data.table           1.12.6     2019-10-18 [1] CRAN (R 3.6.0)
#>  DBI                  1.0.0      2018-05-02 [1] CRAN (R 3.6.0)
#>  desc                 1.2.0      2018-05-01 [1] CRAN (R 3.6.0)
#>  devtools           * 2.2.1      2019-09-24 [1] CRAN (R 3.6.0)
#>  digest               0.6.22     2019-10-21 [1] CRAN (R 3.6.0)
#>  dplyr              * 0.8.3      2019-07-04 [1] CRAN (R 3.6.0)
#>  e1071                1.7-2      2019-06-05 [1] CRAN (R 3.6.0)
#>  ellipsis             0.3.0      2019-09-20 [1] CRAN (R 3.6.0)
#>  evaluate             0.14       2019-05-28 [1] CRAN (R 3.6.0)
#>  fansi                0.4.0      2018-10-05 [1] CRAN (R 3.6.0)
#>  fs                   1.3.1      2019-05-06 [1] CRAN (R 3.6.0)
#>  ggplot2            * 3.2.1      2019-08-10 [1] CRAN (R 3.6.0)
#>  glue                 1.3.1      2019-03-12 [1] CRAN (R 3.6.0)
#>  gtable               0.3.0      2019-03-25 [1] CRAN (R 3.6.0)
#>  highr                0.8        2019-03-20 [1] CRAN (R 3.6.0)
#>  hms                  0.5.2      2019-10-30 [1] CRAN (R 3.6.0)
#>  httpcode             0.2.0      2016-11-14 [1] CRAN (R 3.6.0)
#>  jsonlite             1.6        2018-12-07 [1] CRAN (R 3.6.0)
#>  KernSmooth           2.23-15    2015-06-29 [1] CRAN (R 3.6.1)
#>  knitr              * 1.26       2019-11-12 [1] CRAN (R 3.6.1)
#>  lattice              0.20-38    2018-11-04 [1] CRAN (R 3.6.1)
#>  lazyeval             0.2.2      2019-03-15 [1] CRAN (R 3.6.0)
#>  lubridate            1.7.4      2018-04-11 [1] CRAN (R 3.6.0)
#>  magrittr             1.5        2014-11-22 [1] CRAN (R 3.6.0)
#>  memoise              1.1.0      2017-04-21 [1] CRAN (R 3.6.0)
#>  munsell              0.5.0      2018-06-12 [1] CRAN (R 3.6.0)
#>  nasapower          * 1.1.3      2019-11-18 [1] CRAN (R 3.6.1)
#>  packrat              0.5.0      2018-11-14 [1] CRAN (R 3.6.0)
#>  pillar               1.4.2      2019-06-29 [1] CRAN (R 3.6.0)
#>  pkgbuild             1.0.6      2019-10-09 [1] CRAN (R 3.6.0)
#>  pkgconfig            2.0.3      2019-09-22 [1] CRAN (R 3.6.0)
#>  pkgload              1.0.2      2018-10-29 [1] CRAN (R 3.6.0)
#>  plyr                 1.8.4      2016-06-08 [1] CRAN (R 3.6.0)
#>  prettyunits          1.0.2      2015-07-13 [1] CRAN (R 3.6.0)
#>  processx             3.4.1      2019-07-18 [1] CRAN (R 3.6.0)
#>  ps                   1.3.0      2018-12-21 [1] CRAN (R 3.6.0)
#>  purrr              * 0.3.3      2019-10-18 [1] CRAN (R 3.6.0)
#>  R6                   2.4.0      2019-02-14 [1] CRAN (R 3.6.0)
#>  raster             * 3.0-7      2019-09-24 [1] CRAN (R 3.6.0)
#>  Rcpp                 1.0.3      2019-11-08 [1] CRAN (R 3.6.0)
#>  readr                1.3.1      2018-12-21 [1] CRAN (R 3.6.0)
#>  remotes              2.1.0      2019-06-24 [1] CRAN (R 3.6.0)
#>  rgdal                1.4-7      2019-10-28 [1] CRAN (R 3.6.0)
#>  rgeos                0.5-2      2019-10-03 [1] CRAN (R 3.6.0)
#>  rlang                0.4.1      2019-10-24 [1] CRAN (R 3.6.0)
#>  rnaturalearth      * 0.1.0      2017-03-21 [1] CRAN (R 3.6.0)
#>  rnaturalearthhires   0.2.0      2019-08-13 [1] local
#>  rprojroot            1.3-2      2018-01-03 [1] CRAN (R 3.6.0)
#>  RSQLite              2.1.2      2019-07-24 [1] CRAN (R 3.6.0)
#>  rstudioapi           0.10       2019-03-19 [1] CRAN (R 3.6.0)
#>  scales               1.0.0      2018-08-09 [1] CRAN (R 3.6.0)
#>  sessioninfo          1.1.1      2018-11-05 [1] CRAN (R 3.6.0)
#>  sf                   0.8-0      2019-09-17 [1] CRAN (R 3.6.0)
#>  sirad                2.3-3      2016-10-18 [1] CRAN (R 3.6.0)
#>  sp                 * 1.3-2      2019-11-07 [1] CRAN (R 3.6.0)
#>  stringi              1.4.3      2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr              1.4.0      2019-02-10 [1] CRAN (R 3.6.0)
#>  testthat             2.3.0      2019-11-05 [1] CRAN (R 3.6.0)
#>  tibble               2.1.3      2019-06-06 [1] CRAN (R 3.6.0)
#>  tidyselect           0.2.5      2018-10-11 [1] CRAN (R 3.6.0)
#>  triebeard            0.3.0      2016-08-04 [1] CRAN (R 3.6.0)
#>  units                0.6-5      2019-10-08 [1] CRAN (R 3.6.0)
#>  urltools             1.7.3      2019-04-14 [1] CRAN (R 3.6.0)
#>  usethis            * 1.5.1.9000 2019-11-13 [1] Github (r-lib/[email protected])
#>  utf8                 1.1.4      2018-05-24 [1] CRAN (R 3.6.0)
#>  vctrs                0.2.0      2019-07-05 [1] CRAN (R 3.6.0)
#>  withr                2.1.2      2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun                 0.11       2019-11-12 [1] CRAN (R 3.6.1)
#>  zeallot              0.1.0      2018-01-28 [1] CRAN (R 3.6.0)
#>  zoo                  1.8-6      2019-05-28 [1] CRAN (R 3.6.0)
#>
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library``````