getWind: Connect points to closest wind data in space and time

Description Usage Arguments Value Examples

View source: R/getWind.R

Description

This function takes given points (and their datetime) and connects them with the closest wind data in space.

Usage

1
getWind(x, y, w, PROJ)

Arguments

x

Longitude of the point

y

Latitude of the point

w

Data table with wind data containing u & v wind component and the datetime

PROJ

Projection of the point and wind data (has to be the same)

Value

A list containing the u and v component for the point

Examples

 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
# Load example wind data
PROJ = '+proj=laea +lat_0=90 +lon_0=-156.653428 +x_0=0 +y_0=0 +datum=WGS84 +units=m
        +no_defs +ellps=WGS84 +towgs84=0,0,0 '
wind_data = system.file('ERA_Interrim', 'ERA_Interrim_850mb_4_7_June_2014_10km.RDS',
                         package = 'windR')
w = readRDS(wind_data)
setkey(w, datetime_)

# Create example points within the boarders of the wind data (space and time)
library(raster)
library(rgeos)
# Get extent of the wind data, defining the extent of the map
w1 = w[datetime_ == unique(w$datetime_[1])]
wp = SpatialPoints(cbind(w1$x, w1$y), proj4string = CRS(PROJ))
datBdry = gEnvelope(wp)

ext   = extent(datBdry)
x_ext = seq(ext[1], ext[2])
y_ext = seq(ext[3], ext[4])

n_points = 100

d = data.table(point_id  = 1:n_points,
               x         = sample(x_ext, n_points, replace=TRUE),
               y         = sample(y_ext, n_points, replace=TRUE),
               datetime_ = c(sample(w$datetime_, n_points)) + 60*60) # existing wind data + 1 h

# Assign closest datetime of wind data
setkey(d, point_id)
d[, w_date := closestDatetime(datetime_, unique(w$datetime_)), by = point_id]

# Assign closest wind data
d[, c('u', 'v') := getWind(x = x, y = y, w = w[J(w_date), nomatch=0L], PROJ), by = point_id]

mpio-be/windR documentation built on Feb. 2, 2020, 10:04 a.m.