-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-Microclimate_algorithms.Rmd
More file actions
206 lines (147 loc) · 5.91 KB
/
13-Microclimate_algorithms.Rmd
File metadata and controls
206 lines (147 loc) · 5.91 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
---
pagetitle: Microclimate Algorithms
---
# Microclimate Algorithms
We demonstrate using NichMapR's microclimate functions micro_era5, micro_usa, micro_global, and micro_ncep that are forced with ERA-5, gridMET, NEW01, and NCEP data, respectively. The functions are designed for point locations.
## How to use NicheMapR microclimate functions
1. Install the NicheMapR package. For micro_era5, install the elevatr, ecmwfr, mcera5, lubridate, dplyr and tidync packages. For micro_ncep, install the RNCEP, elevatr, and microclima packages. For
2. Save the coordinates of the location of interest in an array. `lonlat <- c(lon, lat) # (longitude, latitude)`
3. Save the start and end date for which you want to obtain data.
```
dstart <- paste0("01/0", month, "/2017") # start date
dfinish <- paste0("31/0", month, "/2017") # end date
```
3. Save 10 different depths below ground as a variable. The values have to start from 0 and must have exactly 10 values regardless of what variables you are looking to obtain. `DEP <- c(0, 3, 5, 10, 15, 20, 30, 50, 100, 200)`
4. Save the desired height at which all variables should be calcualted. `Usrhyt <- .01` for a 1cm height is common.
5. Run the function with the above variables. Variables are stored under different categories depending on what you are looking for. `?micro_ncep` will give you a list of what each category includes and how each variable is named.
## Code example
***Getting hourly air temperatures in Nunn, CO (-104.73°, 40.87°) for January 1-31 in 2017***
### micro_era5()
First, register for ERA5 data [here](https://cds.climate.copernicus.eu/user/register). Download the data using the mcera5 package.
```
# Fill $$$ will credentials from CDS
uid <- "$$$$$$"
cds_api_key <- "$$$$$$$$-$$$$-$$$$-$$$$-$$$$$$$$$$$$"
ecmwfr::wf_set_key(user = uid, key = cds_api_key, service = "cds")
# bounding coordinates (in WGS84 / EPSG:4326)
xmn <- -104
xmx <- -105
ymn <- 40
ymx <- 41
# temporal extent
st_time <- lubridate::ymd("2017:01:01")
en_time <- lubridate::ymd("2017:01:31")
# filename and location for downloaded .nc files
file_prefix <- "era5"
op <- "C:/Spatial_Data/"
# build a request (covering multiple years)
req <- build_era5_request(xmin = xmn, xmax = xmx,
ymin = ymn, ymax = ymx,
start_time = st_time,
end_time = en_time,
outfile_name = file_prefix)
str(req)
request_era5(request = req, uid = uid, out_path = op)
```
Once data downloads, run micro_era5()
```
library(NicheMapR)
library(lubridate)
library(dplyr)
library(tidync)
lonlat <- c(-104.73, 40.87) # (longitude, latitude)
dstart <- paste0("01/01/2017") # start date
dfinish <- paste0("31/01/2017") # end date
DEP <- c(0, 3, 5, 10, 15, 20, 30, 50, 100, 200)
micro <- micro_era5(loc = lonlat, dstart = dstart, dfinish = dfinish, DEP = DEP,
runmoist = 0, runshade = 0, Usrhyt = 0.01)
variable <- micro$metout[, "TALOC"]
variableDOY <- micro$metout[, "DOY"]
variableHOUR <- micro$metout[, "TIME"]
vals <- c()
begin <- 1
end <- begin + 24*31
vals <- variable[begin:end]
valsDOY <- variableDOY[begin:end]
valsHOUR <- variableHOUR[begin:end]
days <- c()
for (i in 1:31) {
days <- c(days, paste0("2017-0", month, "-", i))
}
df <- data.frame("Date" = rep(days, each = 24)[1 : (24 * 31)],
"Hour" = rep(0 : 23, 31)[1 : (24 * 31)],
"Data" = vals[0 : (24 * 31)])
df$Date <- format(as.POSIXct(paste0(df$Date, " ", df$Hour, ":00")), format = "%Y-%m-%d %H:%M")
```
### micro_usa
```
library(NicheMapR)
micro <- micro_USA(loc = lonlat, dstart = dstart, dfinish = dfinish, DEP = DEP,
runmoist = 0, runshade = 0, Usrhyt = 0.01)
variable <- micro$metout[, "TALOC"]
variableDOY <- micro$metout[, "DOY"]
variableHOUR <- micro$metout[, "TIME"]
vals <- c()
begin <- 1
end <- begin + 24*31
vals <- variable[begin:end]
valsDOY <- variableDOY[begin:end]
valsHOUR <- variableHOUR[begin:end]
days <- c()
for (i in 1:31) {
days <- c(days, paste0("2017-0", month, "-", i))
}
df <- data.frame("Date" = rep(days, each = 24)[1 : (24 * 31)],
"Hour" = rep(0 : 23, 31)[1 : (24 * 31)],
"Data" = vals[0 : (24 * 31)])
df$Date <- format(as.POSIXct(paste0(df$Date, " ", df$Hour, ":00")), format = "%Y-%m-%d %H:%M")
```
### micro_global
```
library(NicheMapR)
micro <- micro_global(loc = lonlat, dstart = dstart, dfinish = dfinish, DEP = DEP,
runmoist = 0, runshade = 0, Usrhyt = 0.01)
variable <- micro$metout[, "TALOC"]
variableDOY <- micro$metout[, "DOY"]
variableHOUR <- micro$metout[, "TIME"]
vals <- c()
begin <- 1
end <- begin + 24*31
vals <- variable[begin:end]
valsDOY <- variableDOY[begin:end]
valsHOUR <- variableHOUR[begin:end]
days <- c()
for (i in 1:31) {
days <- c(days, paste0("2017-0", month, "-", i))
}
df <- data.frame("Date" = rep(days, each = 24)[1 : (24 * 31)],
"Hour" = rep(0 : 23, 31)[1 : (24 * 31)],
"Data" = vals[0 : (24 * 31)])
df$Date <- format(as.POSIXct(paste0(df$Date, " ", df$Hour, ":00")), format = "%Y-%m-%d %H:%M")
```
### micro_ncep
```
library(NicheMapR)
library(RNCEP)
library(elevatr)
librar(microclima)
micro <- micro_ncep(loc = lonlat, dstart = dstart, dfinish = dfinish, DEP = DEP,
runmoist = 0, runshade = 0, Usrhyt = 0.01)
variable <- micro$metout[, "TALOC"]
variableDOY <- micro$metout[, "DOY"]
variableHOUR <- micro$metout[, "TIME"]
vals <- c()
begin <- 1
end <- begin + 24*31
vals <- variable[begin:end]
valsDOY <- variableDOY[begin:end]
valsHOUR <- variableHOUR[begin:end]
days <- c()
for (i in 1:31) {
days <- c(days, paste0("2017-0", month, "-", i))
}
df <- data.frame("Date" = rep(days, each = 24)[1 : (24 * 31)],
"Hour" = rep(0 : 23, 31)[1 : (24 * 31)],
"Data" = vals[0 : (24 * 31)])
df$Date <- format(as.POSIXct(paste0(df$Date, " ", df$Hour, ":00")), format = "%Y-%m-%d %H:%M")
```