-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhumidity.Rmd
More file actions
240 lines (175 loc) · 6.31 KB
/
humidity.Rmd
File metadata and controls
240 lines (175 loc) · 6.31 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
---
title: "Humidity"
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 description:** | Humidity data worldwide and in Australia |
| **Source:** | [NASA POWER](https://power.larc.nasa.gov/): The POWER Project |
| **Details on the retrieved data:** | Monthly and yearly relative humidity in Northern Territory and Western Australia (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 humidity 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.
Here, we will use the `nasapower` package to retrieve the relative humidity data for specific countries or for the world.
We have also used the `nasapower` package to retrieve rainfall data [here](https://rspatialdata.github.io/rainfall).
The rainfall tutorial includes an introduction on the `nasapower` package and how its functions work.
## Installing `nasapower` package
We can install the package from CRAN and load it as follows.
```{r CRAN,eval=FALSE}
install.packages("nasapower")
```
```{r loading}
library(nasapower)
```
## Using `get_power()` to fetch data
First let us have a look at how to get the daily data for humidity in agriculture. This can be done using the `get_power()` function.
### Fetching daily data for single point
We use `get_power()` function arguments `pars = "RH2M"` which means relative humidity at 2 meters, `temporal_api = "DAILY"`, and `longlat` equal to a single location.
```{r getdata}
data_RH <- get_power(
community = "AG",
lonlat = c(134.489563, -25.734968),
dates = c("2010-09-23", "2010-12-23"),
temporal_api = "DAILY",
pars = "RH2M"
)
data_RH %>% 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_humidity <- get_power(
community = "AG",
lonlat = c(150, -30, 155, -25),
pars = "RH2M",
dates = c("2004-09-19", "2004-09-29"),
temporal_api = "DAILY"
)
daily_humidity %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
deferRender = TRUE,
scrollY = 350,
scrollX = 350,
dom = "t",
scroller = TRUE,
fixedColumns = list(leftColumns = 3)
))
```
### Fetching climatology data
For `pars = "RH2M"`, and as in the [rainfall tutorial](https://rspatialdata.github.io/rainfall) we will focus on Australia territory and set `community = "AG"` and `temporal_api = "CLIMATOLOGY"`.
```{r climate, cache=TRUE}
flag <- 1
for (i in seq(110, 150, 5)) {
for (j in seq(-40, -10, 5)) {
climate_avg_RH_temp <- get_power(community = "AG",
pars = "RH2M",
lonlat = c(i, (j - 5), (i + 5), j),
temporal_api = "CLIMATOLOGY")
if (flag == 1) {
climate_avg_RH <- climate_avg_RH_temp
flag <- 0
} else{
climate_avg_RH <- rbind(climate_avg_RH, climate_avg_RH_temp)
}
}
}
climate_avg_RH %>% datatable(extensions = c("Scroller", "FixedColumns"), options = list(
deferRender = TRUE,
scrollY = 350,
scrollX = 350,
dom = "t",
scroller = TRUE,
fixedColumns = list(leftColumns = 3)
))
```
## Creating a map of annual humidity 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_RH[, 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 = "Relative humidity",
fill = "Humidity",
subtitle = "Annual relative humidity in Australia"
)
```
## Creating a map of annual humidity using a subset of the data retrieved
```{r climbox}
library(rnaturalearth)
# Getting map for China
AUS <- ne_states(country = "Australia", returnclass = "sf")
# Getting administrative boundaries for regions
NT <- AUS[AUS$name == "Northern Territory", ]
WA <- AUS[AUS$name == "Western Australia", ]
# Converting data to raster
r <- rasterFromXYZ(climate_avg_RH[, c("LON", "LAT", "ANN")])
# Subset values for the region and converting the raster into a data.frame
rr <- mask(crop(r, NT), NT)
r_df <- as.data.frame(rr, xy = TRUE, na.rm = TRUE)
ggplot() +
geom_raster(data = r_df, aes(x = x, y = y, fill = ANN)) +
geom_sf(data = AUS, inherit.aes = FALSE, fill = NA) +
scale_fill_viridis() +
theme_minimal() +
labs(title = "Relative Humidity in Northern Territory, Australia", fill = "Humidity")
# Subset values for the region and converting the raster into a data.frame
rr <- mask(crop(r, WA), WA)
r_df <- as.data.frame(rr, xy = TRUE, na.rm = TRUE)
ggplot() +
geom_raster(data = r_df, aes(x = x, y = y, fill = ANN)) +
geom_sf(data = AUS, inherit.aes = FALSE, fill = NA) +
scale_fill_viridis() +
theme_minimal() +
labs(title = "Relative Humidity in Western Australia, Australia", fill = "Humidity")
```
## Creating maps of monthly humidity
```{r}
r <- list()
for (k in colnames(climate_avg_RH)[-c(1:3, 16)]) {
r[[k]] <- rasterFromXYZ(climate_avg_RH[, c("LON", "LAT", k)])
}
r <- stack(r)
plot(r)
```
## 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/humidity.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>