-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrainfall.Rmd
More file actions
221 lines (164 loc) · 6.69 KB
/
rainfall.Rmd
File metadata and controls
221 lines (164 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
---
title: "Rainfall"
output:
html_document:
toc: true
toc_float: true
number_sections: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(tidyverse)
library(DT)
library(terra)
library(rnaturalearth)
library(viridis)
library(rgeoboundaries)
```
#### Data set details
|||
| ----------- | ----------- |
| **Data set description:** | Rainfall data worldwide and in China |
| **Source:** | [NASA POWER](https://power.larc.nasa.gov/): 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](https://power.larc.nasa.gov/) (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.
```{r eval=FALSE}
install.packages("nasapower")
```
Loading the `nasapower` package:
```{r loading}
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
```{r getdata}
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
```{r daily}
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.
```{r climate}
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
```{r}
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}
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
- `nasapower` package: https://github.com/ropensci/nasapower
- NASAPOWER project: https://power.larc.nasa.gov/
---
<p style ="color:grey; font-size: 13px;">
Last updated: `r Sys.Date()`
Source code: https://github.com/rspatialdata/rspatialdata.github.io/blob/main/rainfall.Rmd
<details>
<summary><span style ="color:grey; font-size: 13px;">Tutorial was complied using: (click to expand)</span></summary>
```{r echo=FALSE}
sessionInfo()
```
</details>
</p>