Skip to content

Commit 67c93d1

Browse files
authored
Merge pull request #28 from moritzshore/direkt-sf
SF object support, metadata and validation changes
2 parents f969741 + bea823a commit 67c93d1

6 files changed

+39
-14
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Imports:
1818
dplyr,
1919
lubridate,
2020
mapview,
21+
methods,
2122
ncdf4,
2223
purrr,
2324
readr,
@@ -29,7 +30,6 @@ Imports:
2930
Suggests:
3031
devtools,
3132
doParallel,
32-
euptf2,
3333
fs,
3434
ggplot2,
3535
ggrepel,

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ importFrom(lubridate,hour)
5151
importFrom(lubridate,month)
5252
importFrom(lubridate,year)
5353
importFrom(mapview,mapview)
54+
importFrom(methods,is)
5455
importFrom(ncdf4,nc_close)
5556
importFrom(ncdf4,nc_open)
5657
importFrom(ncdf4,ncatt_get)

R/metnorenal3.R

+34-11
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
#' as well as plotting interactive maps to the viewer which gives you an idea of
7474
#' which grid cells were selected relative to the shapefile you provided.
7575
#'
76-
#' @param area (string) path to geo-referenced shapefile (polygon or point) of the desired area
76+
#' @param area (string) path to geo-referenced shapefile (polygon or point) of the desired area. (optionally, you can pass a `sf` object directly.)
7777
#' @param directory (string) path to desired working directory (default: working directory)
7878
#' @param fromdate (string) date and time for start of time series (ie. "2012-09-01 10:00:00")
7979
#' @param todate (string) date and time for end of time series (ie. "2013-09-01 10:00:00")
@@ -125,6 +125,12 @@ get_metno_reanalysis3 <-
125125
preview = TRUE
126126
){
127127
# validate input
128+
if(as_datetime(todate) > as_datetime("2023-01-31 23:00:00")){
129+
stop("Reanalysis 3 is only until '2023-01-31 23:00:00', contact maintainer if this changes..")
130+
}
131+
if(as_datetime(fromdate) < as_datetime("2012-09-01 03:00:00")){
132+
stop("Reanalysis 3 only starts '2012-09-01 03:00:00', contact maintainer if this changes..")
133+
}
128134
if(is.null(grid_resolution)){
129135
mt_print(preview, "get_metno_reanalysis3", text = "`grid_resolution` not chosen, defaulting to 1 x 1 km grid..")
130136
grid_resolution = 1
@@ -165,6 +171,9 @@ get_metno_reanalysis3 <-
165171
mt_print(preview, "get_metno_reanalysis3", "getting coordinates...")
166172
bounding_coords <- get_coord_window(area_path = area, area_buffer, preview)
167173

174+
metadist <- bounding_coords$metadist
175+
bounding_coords<- bounding_coords[-length(bounding_coords)]
176+
168177
mt_print(preview, "get_metno_reanalysis3", "building query...")
169178
queries <- build_query(bounding_coords, mn_variables, fromdate, todate, grid_resolution, verbose)
170179

@@ -235,7 +244,8 @@ get_metno_reanalysis3 <-
235244
lon = paste("lon = ", ncdownload$lon_crop),
236245
x = paste("X =", ncdownload$x_crop),
237246
y = paste("Y =", ncdownload$y_crop),
238-
elevation = paste("ELEVATION = ", ncdownload$alt_crop)
247+
elevation = paste("ELEVATION = ", ncdownload$alt_crop),
248+
distance_to_gridcell = paste0("DISTANCE TO NEAREST GRIDCELL (in meters) = ", metadist)
239249
)
240250

241251
source = paste0(
@@ -282,6 +292,7 @@ get_metno_reanalysis3 <-
282292
#' @importFrom readr read_csv write_csv
283293
#' @importFrom lubridate date
284294
#' @importFrom stringr str_split
295+
#' @importFrom methods is
285296
reanalysis3_daily <- function(path, outpath = NULL, verbose = FALSE, precision = 2){
286297

287298
# remove trailing "/"
@@ -399,7 +410,16 @@ get_coord_window <- function(area_path, area_buffer, preview){
399410
projection <- "+proj=lcc +lat_0=63 +lon_0=15 +lat_1=63 +lat_2=63 +no_defs +R=6371000"
400411
proj_crs <- sf::st_crs(projection) # replace with sf::crs()
401412
# load in the shape file
402-
area <- sf::read_sf(area_path)
413+
# if it already is a shape file, then no need to read it
414+
if(methods::is(area_path, "sf")){
415+
area <- area_path
416+
}else if(methods::is(area_path, "character")){
417+
# if it is a string, then read it
418+
area <- sf::read_sf(area_path)
419+
}else{
420+
stop("`area` parameter not recognized! please pass either a filepath to a .shp file, or an `sf` object!\n")
421+
}
422+
403423
# Transform the shapefile to the metno projection
404424
area <- sf::st_transform(area, crs = proj_crs)
405425

@@ -424,22 +444,24 @@ get_coord_window <- function(area_path, area_buffer, preview){
424444
index_x <- which(min_diff_x == x_diff)
425445
index_y <- which(min_diff_y == y_diff)
426446

447+
df <- sf::st_as_sf(x = data.frame(x = x[index_x], y = y[index_y]),
448+
coords = c("x", "y"),
449+
crs = sf::st_crs(proj_crs))
450+
metadist <- (sf::st_distance(df, area) %>% round(0))[1,1]
451+
427452
if(preview){
428-
df <- sf::st_as_sf(x = data.frame(x = x[index_x], y = y[index_y]),
429-
coords = c("x", "y"),
430-
crs = sf::st_crs(proj_crs))
453+
431454
area$name = "provided file"
432455
plot = mapview::mapview(df, layer.name = "Nearest Re-analysis Gridpoint", col.regions = "orange")+
433456
mapview::mapview(area, layer.name = "User location", col.regions = "blue")
434-
435457
print(plot)
436458
mt_print(preview, "get_metno_reanalysis3 ",
437-
"Note: Selected grid cell distance (in meters) to desired location:",
438-
(sf::st_distance(df, area) %>% round(0)))
459+
"Note: Selected grid cell distance (in meters) to desired location:",metadist)
439460
}
440461

441462

442-
return(list(index_x = index_x, index_y = index_y))
463+
464+
return(list(index_x = index_x, index_y = index_y, metadist = metadist))
443465

444466
} else{
445467
# do the polygon stuff
@@ -488,7 +510,8 @@ get_coord_window <- function(area_path, area_buffer, preview){
488510
index_xmax = index_xmax,
489511
index_ymin = index_ymin,
490512
index_ymax = index_ymax,
491-
area_buff = area_buff, area_shp = area
513+
area_buff = area_buff, area_shp = area,
514+
metadist = "not relevant for polygon shapefile"
492515
))
493516
}
494517
}

man/get_metno_reanalysis3.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

miljotools.Rproj

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 75e4e4c8-c8c1-4d4f-bfa0-45a09151efb0
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

vignettes/metno_reanal.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ editor_options:
1212

1313
```{r setup, include=FALSE}
1414
knitr::opts_chunk$set(echo = TRUE)
15-
runmetnovig = TRUE # runs the vignette code or not. only enable for when you want to release a new package version, since it takes a long time to run.
15+
runmetnovig = FALSE # runs the vignette code or not. only enable for when you want to release a new package version, since it takes a long time to run.
1616
```
1717

1818
[Author]{.underline}: Moritz Shore

0 commit comments

Comments
 (0)