Skip to content

Commit f97403e

Browse files
committed
Add lon/lat as attributes of get_parameters() to solve #4
1 parent 3f6b0a4 commit f97403e

File tree

9 files changed

+71
-56
lines changed

9 files changed

+71
-56
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# resourcecode (development version)
22

3+
- Add node coordinates as attributes of the output from `get_parameters()` to solve #4;
4+
35
# resourcecode 0.5.3
46

57
- Custom labels are now handled properly in `cut_seasons()` ;

R/download_data.R

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ get_parameters_raw <- function(
2626
end_str <- strftime(end, format = "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
2727

2828
# Cassandra database start indexing at 1, so decrements node number
29-
node <- node - 1
29+
node_cassandra <- node - 1
3030

3131
request_url <- paste0(
3232
rcd_cassandra_url,
3333
"api/timeseries",
3434
"?parameter=",
3535
single_parameter,
3636
"&node=",
37-
node,
37+
node_cassandra,
3838
"&start=",
3939
start_str,
4040
"&end=",
@@ -117,6 +117,8 @@ get_parameters_raw <- function(
117117
tz = "UTC"
118118
) # Convert UNIX time (ms) to POSIXct format
119119
attr(data, "node") <- node
120+
attr(data, "longitude") <- resourcecodedata::rscd_field$longitude[node]
121+
attr(data, "latitude") <- resourcecodedata::rscd_field$latitude[node]
120122
data
121123
}
122124

@@ -143,26 +145,48 @@ get_parameters <- function(
143145
start = as.POSIXct("1994-01-01 00:00:00", tz = "UTC"),
144146
end = as.POSIXct("1994-12-31 23:00:00", tz = "UTC")
145147
) {
146-
parameters <- tolower(parameters)
148+
valid_parameters <- tolower(parameters)
147149

148-
if (any(parameters %nin% c("tp", resourcecodedata::rscd_variables$name))) {
149-
errors <- parameters[
150-
parameters %nin% c("tp", resourcecodedata::rscd_variables$name)
150+
if (any(c("lon", "longitude", "lat", "latitude") %in% valid_parameters)) {
151+
valid_parameters = valid_parameters[
152+
valid_parameters %nin% c("lon", "longitude", "lat", "latitude")
151153
]
152-
stop(
153-
"Requested parameters do not exists in the database: ",
154-
paste0(errors, collapse = ", "),
155-
"."
154+
warning(
155+
"Coordinates are given as attributes of the resulting data.frame and should not be requested."
156156
)
157157
}
158158

159+
if (length(valid_parameters) == 0) {
160+
rlang::abort("No valid parameters are provided. Exiting.")
161+
}
162+
163+
if (
164+
any(valid_parameters %nin% c("tp", resourcecodedata::rscd_variables$name))
165+
) {
166+
errors <- valid_parameters[
167+
valid_parameters %nin% c("tp", resourcecodedata::rscd_variables$name)
168+
]
169+
valid_parameters = valid_parameters[
170+
valid_parameters %in% c("tp", resourcecodedata::rscd_variables$name)
171+
]
172+
if (length(valid_parameters) == 0) {
173+
rlang::abort("No valid parameters are provided. Exiting.")
174+
} else {
175+
warning(
176+
"Requested parameters do not exist in the database and are ignored: ",
177+
paste0(errors, collapse = ", "),
178+
"."
179+
)
180+
}
181+
}
182+
159183
node <- as.integer(node)
160184

161185
if (length(node) != 1) {
162-
stop("The function can retreive only one location a time.")
186+
rlang::abort("The function can retreive only one location a time.")
163187
}
164188
if (node %nin% resourcecodedata::rscd_field$node) {
165-
stop("The requested location do no exist in the database.")
189+
rlang::abort("The requested location do no exist in the database.")
166190
}
167191

168192
if (is.character(start)) {
@@ -216,33 +240,33 @@ get_parameters <- function(
216240
}
217241

218242
out <- get_parameters_raw(
219-
parameters[1],
243+
valid_parameters[1],
220244
node = node,
221245
start = start,
222246
end = end
223247
)
224248

225249
# If first parameter retrieval failed, return NULL
226250
if (is.null(out)) {
227-
message("Failed to retrieve parameter: ", parameters[1])
251+
message("Failed to retrieve parameter: ", valid_parameters[1])
228252
return(NULL)
229253
}
230254

231-
for (i in seq_len(length(parameters) - 1)) {
255+
for (i in seq_len(length(valid_parameters) - 1)) {
232256
temp <- get_parameters_raw(
233-
parameters[i + 1],
257+
valid_parameters[i + 1],
234258
node = node,
235259
start = start,
236260
end = end
237261
)
238262

239263
# If any subsequent parameter retrieval fails, return NULL
240264
if (is.null(temp)) {
241-
message("Failed to retrieve parameter: ", parameters[i + 1])
265+
message("Failed to retrieve parameter: ", valid_parameters[i + 1])
242266
return(NULL)
243267
}
244268

245-
out <- cbind.data.frame(out, temp[, 2])
269+
out[valid_parameters[i + 1]] <- temp[2]
246270
}
247271
out
248272
}

R/spectral_data_download.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ get_1d_spectrum_raw <- function(point, year, month) {
243243
#' }
244244
#' @export
245245
#'
246-
#' @examples
246+
#' @examplesIf (identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true"))
247247
#' spec2D <- get_2d_spectrum("SEMREVO", start = "1994-01-01", end = "1994-02-28")
248248
#' if(!is.null(spec2D)){
249249
#' image(spec2D$dir, spec2D$freq, spec2D$efth[, , 1],
250-
#' xlab = "Direction (°)",
251-
#' ylab = "Frequency (Hz"
250+
#' xlab = "Direction (\u00B0)",
251+
#' ylab = "Frequency (Hz)"
252252
#' )
253253
#' }
254254
get_2d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") {
@@ -369,7 +369,7 @@ get_2d_spectrum <- function(point, start = "1994-01-01", end = "1994-02-28") {
369369
#' }
370370
#' @export
371371
#'
372-
#' @examples
372+
#' @examplesIf (identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true"))
373373
#' spec1D <- get_1d_spectrum("SEMREVO", start = "1994-01-01", end = "1994-02-28")
374374
#' if(!is.null(spec1D)){
375375
#' r <- as.POSIXct(round(range(spec1D$forcings$time), "month"))

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ Finistère:
8383
``` r
8484
data <- get_parameters(node = "134865", parameters = "hs")
8585
str(data)
86-
#> tibble [8,760 × 2] (S3: tbl_df/tbl/data.frame)
86+
#> tibble [8,760 x 2] (S3: tbl_df/tbl/data.frame)
8787
#> $ time: POSIXct[1:8760], format: "1994-01-01 00:00:00" "1994-01-01 01:00:00" ...
8888
#> $ hs : num [1:8760] 4.82 4.98 5.19 5.37 5.48 ...
89-
#> - attr(*, "node")= num 134864
89+
#> - attr(*, "node")= int 134865
90+
#> - attr(*, "longitude")= num -4.96
91+
#> - attr(*, "latitude")= num 48.3
9092
if (!is.null(data)) plot(data, type = "l")
9193
```
9294

917 Bytes
Loading

man/get_1d_spectrum.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_2d_spectrum.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/Rplots.pdf

0 Bytes
Binary file not shown.

tests/testthat/tests_download_parameters.R

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ test_that("get_parameters handles numeric date inputs", {
7171

7272
# Error handling tests (these don't need vcr as they fail before API call)
7373
test_that("get_parameters validates parameter names", {
74-
expect_error(
74+
expect_warning(
7575
get_parameters(
7676
parameters = c("hs", "invalid_param"),
7777
node = 42
7878
),
79-
"Requested parameters do not exists"
79+
"Requested parameters do not exist in the database"
8080
)
8181
})
8282

@@ -268,49 +268,32 @@ test_that("get_parameters handles partial failure with multiple parameters", {
268268

269269

270270
test_that("Errors in 'get_parameters()' are handled correcly", {
271+
expect_warning(
272+
get_parameters(parameters = c("hs", "tépé")),
273+
"Requested parameters do not exist in the database"
274+
)
271275
expect_error(
272276
get_parameters("tépé"),
273-
"Requested parameters do not exists in the database: tépé"
277+
"No valid parameters are provided. Exiting."
274278
)
275279
expect_error(
276280
get_parameters(node = 0),
277281
"The requested location do no exist in the database."
278282
)
283+
expect_error(
284+
suppressWarnings(get_parameters(node = "un")),
285+
"The requested location do no exist in the database."
286+
)
279287
expect_error(
280288
get_parameters(node = c(10, 100)),
281289
"The function can retreive only one location a time."
282290
)
283291
expect_error(
284292
get_parameters(start = 1),
285-
paste0(
286-
"'start' is outside the covered period: ",
287-
paste(
288-
format(
289-
c(
290-
resourcecode:::rscd_casandra_start_date,
291-
resourcecode:::rscd_casandra_end_date
292-
),
293-
format = "%Y-%m-%d %H:%M %Z"
294-
),
295-
collapse = " \u2014 "
296-
)
297-
)
293+
"'start' is outside the covered period: "
298294
)
299295
expect_error(
300-
get_parameters(end = 1e10),
301-
paste0(
302-
"'end' is outside the covered period: ",
303-
paste(
304-
format(
305-
c(
306-
resourcecode:::rscd_casandra_start_date,
307-
resourcecode:::rscd_casandra_end_date
308-
),
309-
format = "%Y-%m-%d %H:%M %Z"
310-
),
311-
collapse = " \u2014 "
312-
)
313-
)
296+
get_parameters(end = 1e12),
314297
)
315298
expect_error(
316299
get_parameters(

0 commit comments

Comments
 (0)