73
73
# ' as well as plotting interactive maps to the viewer which gives you an idea of
74
74
# ' which grid cells were selected relative to the shapefile you provided.
75
75
# '
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.)
77
77
# ' @param directory (string) path to desired working directory (default: working directory)
78
78
# ' @param fromdate (string) date and time for start of time series (ie. "2012-09-01 10:00:00")
79
79
# ' @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 <-
125
125
preview = TRUE
126
126
){
127
127
# 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
+ }
128
134
if (is.null(grid_resolution )){
129
135
mt_print(preview , " get_metno_reanalysis3" , text = " `grid_resolution` not chosen, defaulting to 1 x 1 km grid.." )
130
136
grid_resolution = 1
@@ -165,6 +171,9 @@ get_metno_reanalysis3 <-
165
171
mt_print(preview , " get_metno_reanalysis3" , " getting coordinates..." )
166
172
bounding_coords <- get_coord_window(area_path = area , area_buffer , preview )
167
173
174
+ metadist <- bounding_coords $ metadist
175
+ bounding_coords <- bounding_coords [- length(bounding_coords )]
176
+
168
177
mt_print(preview , " get_metno_reanalysis3" , " building query..." )
169
178
queries <- build_query(bounding_coords , mn_variables , fromdate , todate , grid_resolution , verbose )
170
179
@@ -235,7 +244,8 @@ get_metno_reanalysis3 <-
235
244
lon = paste(" lon = " , ncdownload $ lon_crop ),
236
245
x = paste(" X =" , ncdownload $ x_crop ),
237
246
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 )
239
249
)
240
250
241
251
source = paste0(
@@ -282,6 +292,7 @@ get_metno_reanalysis3 <-
282
292
# ' @importFrom readr read_csv write_csv
283
293
# ' @importFrom lubridate date
284
294
# ' @importFrom stringr str_split
295
+ # ' @importFrom methods is
285
296
reanalysis3_daily <- function (path , outpath = NULL , verbose = FALSE , precision = 2 ){
286
297
287
298
# remove trailing "/"
@@ -399,7 +410,16 @@ get_coord_window <- function(area_path, area_buffer, preview){
399
410
projection <- " +proj=lcc +lat_0=63 +lon_0=15 +lat_1=63 +lat_2=63 +no_defs +R=6371000"
400
411
proj_crs <- sf :: st_crs(projection ) # replace with sf::crs()
401
412
# 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
+
403
423
# Transform the shapefile to the metno projection
404
424
area <- sf :: st_transform(area , crs = proj_crs )
405
425
@@ -424,22 +444,24 @@ get_coord_window <- function(area_path, area_buffer, preview){
424
444
index_x <- which(min_diff_x == x_diff )
425
445
index_y <- which(min_diff_y == y_diff )
426
446
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
+
427
452
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
+
431
454
area $ name = " provided file"
432
455
plot = mapview :: mapview(df , layer.name = " Nearest Re-analysis Gridpoint" , col.regions = " orange" )+
433
456
mapview :: mapview(area , layer.name = " User location" , col.regions = " blue" )
434
-
435
457
print(plot )
436
458
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 )
439
460
}
440
461
441
462
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 ))
443
465
444
466
} else {
445
467
# do the polygon stuff
@@ -488,7 +510,8 @@ get_coord_window <- function(area_path, area_buffer, preview){
488
510
index_xmax = index_xmax ,
489
511
index_ymin = index_ymin ,
490
512
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"
492
515
))
493
516
}
494
517
}
0 commit comments