## the raadtools weekly OISST
f <- "/rdsi/PUBLIC/raad/data/ftp.cdc.noaa.gov/Datasets/noaa.oisst.v2/sst.wkmean.1990-present.nc"
data("ellie", package = "bsam")
## use raster cells
library(raster)
## Loading required package: sp
sst <- brick(f)
## Loading required namespace: ncdf4
## cell is 2D, so pre-allocate the spatial and temporal index
cells <- data.frame(cell = cellFromXY(sst, cbind(ellie$lon, ellie$lat)),
timeslice = findInterval(ellie$date, as.POSIXct(getZ(sst))),
order = seq_len(nrow(ellie)))
unique(cells$timeslice)
## [1] 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
## [15] 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
## [29] 1210
## now use split to group by the slices in time, and extract from each time step
## in the brick
result <- do.call(rbind,
lapply(split(cells, cells$timeslice),
function(subdf) {
subdf$sst <- raster::extract(sst[[ subdf$timeslice[1L] ]], subdf$cell)
subdf
})
)
## restore to native order and plot
ellie$sst <- result[order(result$order), ]$sst
library(ggplot2)
ggplot(ellie, aes(date, sst, colour = id)) + geom_point()
