Data set details

Data set description: Rainfall data worldwide and in China
Source: NASA POWER: The POWER Project
Details on the retrieved data: Monthly and yearly rainfall in Australia.
Spatial and temporal resolution: Hourly, daily, monthly, or yearly data for regions defined by the lonlat parameter. Each cell corresponds to 1/2 x 1/2 degree.

Downloading rainfall data with nasapower

The nasapower package aims at making it quick and easy to automate downloading NASA POWER (NASA Prediction of Worldwide Energy Resource) global meteorology, surface solar energy and climatology data.

Installing nasapower package

The nasapower package can be directly downloaded from CRAN as follows.

install.packages("nasapower")

Loading the nasapower package:

library(nasapower)

Using get_power() to fetch data

The get_power() function has five arguments and returns a data frame with a metadata header in the current R session. It has the following arguments:

  • pars: character vector of solar, meteorological or climatology parameters to download.

  • community: character vector providing community name. Supported values are "AG", "SB" or "RE".

"AG": provides access to the agroclimatology archive, which contains industry-friendly parameters for input to crop models.

"SB": provides access to the sustainable buildings archive, which contains parameters for the building community.

"RE": provides access to the renewable energy archive, which contains parameters very specific to assist in the design of solar and wind powered renewable energy systems.

  • temporal_api: temporal API end-point for data being queried. Supported values are "HOURLY", "DAILY", "MONTHLY", or "CLIMATOLOGY".

"HOURLY": hourly average of pars by hour, day, month and year.

"DAILY": daily average of pars by day, month and year.

"MONTHLY": monthly average of pars by month and year.

"CLIMATOLOGY": provide parameters as 22-year climatologies (solar) and 30-year climatologies (meteorology); the period climatology and monthly average, maximum, and/or minimum values.

  • lonlat: numeric vector of geographic coordinates for a cell or region or "GLOBAL" for global coverage.

For single point supply a length-two numeric vector giving the decimal degree longitude and latitude in that order for the data to download.

For regional coverage supply a length-four numeric as lower left (lon, lat) and upper right (lon, lat) coordinates as lonlat = c(xmin, ymin, ymax, ymax)

  • dates: start and end dates. If only one date is provided, it will be treated as both the start and the end date and only a day’s values will be returned. This argument should not be used when temporal_api is set to "CLIMATOLOGY".

To know the different weather values from POWER provided within this function, type ?get_power, and in the arguments section, click on the highlighted parameters, which goes to a page which has all the available parameters.

To download rainfall, we use the pars = "PRECTOTCORR".

Fetching daily data for single point

data <- get_power(
  community = "RE",
  lonlat = c(134.489563, -25.734968),
  dates = c("2000-01-01", "2000-05-01"),
  temporal_api = "DAILY",
  pars = "PRECTOTCORR"
)

data %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Fetching daily data for an area

daily_area <- get_power(
  community = "AG",
  lonlat = c(150.5, -28.5, 153.5, -25.5),
  pars = "PRECTOTCORR",
  dates = c("2004-09-19", "2004-09-29"),
  temporal_api = "DAILY"
)

daily_area %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Fetching climatology data

Using lonlat equivalent to Australia, we will set temporal_api = "CLIMATOLOGY". Since the maximum processed area is 4.5 x 4.5 degrees (100 points), we will do this in step-by-step.

flag <- 1
for (i in seq(110, 150, 5)) {
  for (j in seq(-40, -10, 5)) {
    climate_avg_temp <- get_power(community = "AG",
                                  pars = "PRECTOTCORR",
                                  lonlat = c(i, (j - 5), (i + 5), j),
                                  temporal_api = "CLIMATOLOGY")
    if (flag == 1) {
      climate_avg <- climate_avg_temp
      flag <- 0
    } else{
      climate_avg <- rbind(climate_avg, climate_avg_temp)
    }
  }
}

climate_avg %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Next, we will plot the retrieved data.

Creating a map of annual rainfall using all data retrieved

library(rnaturalearth)
library(raster)

# Getting world map
map <- ne_countries(country = 'australia', returnclass = "sf")

# Converting data to raster
r <- rasterFromXYZ(climate_avg[, c("LON", "LAT", "ANN")])

# Converting the raster into a data.frame
r_df <- as.data.frame(r, xy = TRUE, na.rm = TRUE)

# Plot
ggplot() +
  geom_raster(data = r_df, aes(x = x, y = y, fill = ANN)) +
  geom_sf(data = map, inherit.aes = FALSE, fill = NA) +
  scale_fill_viridis() +
  labs(
    title = "Rainfall in inches",
    fill = "Annual Rainfall",
    subtitle = "Annual rainfall in Australia"
  ) + 
  labs(x = "Longitude", y = "Latitude")

Creating maps of monthly rainfall

r <- list()
for (k in colnames(climate_avg)[-c(1:3, 16)]) {
  r[[k]] <- rasterFromXYZ(climate_avg[, c("LON", "LAT", k)])
}
r <- stack(r)
plot(r, asp = 1)

References


Last updated: 2021-11-30
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/rainfall.Rmd

Tutorial was complied using: (click to expand)
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] raster_3.5-2              sp_1.4-5                 
##  [3] nasapower_4.0.0           rgeoboundaries_0.0.0.9000
##  [5] viridis_0.6.2             viridisLite_0.4.0        
##  [7] rnaturalearth_0.1.0       terra_1.4-11             
##  [9] DT_0.19                   forcats_0.5.1            
## [11] stringr_1.4.0             dplyr_1.0.7              
## [13] purrr_0.3.4               readr_2.0.2              
## [15] tidyr_1.1.4               tibble_3.1.5             
## [17] ggplot2_3.3.5             tidyverse_1.3.1          
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.0           sf_1.0-3           bit64_4.0.5        lubridate_1.8.0   
##  [5] httr_1.4.2         tools_4.1.1        backports_1.3.0    bslib_0.3.1       
##  [9] utf8_1.2.2         R6_2.5.1           KernSmooth_2.23-20 rgeos_0.5-8       
## [13] DBI_1.1.1          colorspace_2.0-2   withr_2.4.2        tidyselect_1.1.1  
## [17] gridExtra_2.3      bit_4.0.4          curl_4.3.2         compiler_4.1.1    
## [21] cli_3.1.0          rvest_1.0.2        xml2_1.3.2         labeling_0.4.2    
## [25] triebeard_0.3.0    sass_0.4.0         scales_1.1.1       classInt_0.4-3    
## [29] proxy_0.4-26       rappdirs_0.3.3     digest_0.6.28      rmarkdown_2.11    
## [33] pkgconfig_2.0.3    htmltools_0.5.2    highr_0.9          dbplyr_2.1.1      
## [37] fastmap_1.1.0      htmlwidgets_1.5.4  rlang_0.4.11       readxl_1.3.1      
## [41] rstudioapi_0.13    httpcode_0.3.0     farver_2.1.0       jquerylib_0.1.4   
## [45] generics_0.1.1     jsonlite_1.7.2     crosstalk_1.1.1    vroom_1.5.5       
## [49] magrittr_2.0.1     s2_1.0.7           Rcpp_1.0.7         munsell_0.5.0     
## [53] fansi_0.5.0        lifecycle_1.0.1    stringi_1.7.5      yaml_2.2.1        
## [57] grid_4.1.1         parallel_4.1.1     crayon_1.4.2       lattice_0.20-44   
## [61] haven_2.4.3        hms_1.1.1          knitr_1.36         pillar_1.6.4      
## [65] codetools_0.2-18   wk_0.5.0           crul_1.1.0         reprex_2.0.1      
## [69] glue_1.5.0         evaluate_0.14      hoardr_0.5.2       modelr_0.1.8      
## [73] vctrs_0.3.8        tzdb_0.1.2         urltools_1.7.3     cellranger_1.1.0  
## [77] gtable_0.3.0       assertthat_0.2.1   cachem_1.0.6       xfun_0.26         
## [81] broom_0.7.9        countrycode_1.3.0  e1071_1.7-9        class_7.3-19      
## [85] memoise_2.0.0      units_0.7-2        ellipsis_0.3.2

Corrections: If you see mistakes or want to suggest changes, please create an issue on the source repository or submit a pull request
Contributions: If you want to contribute or collaborate on the project, please see the guidelines for collaborating
Reuse: Text and figures are licensed under Creative Commons Attribution CC BY 4.0.