AustralianAntarcticDivision / raadtools

Tools for Synoptic Environmental Spatial Data
http://australianantarcticdivision.github.io/raadtools/
24 stars 4 forks source link

chlorophyll extract #95

Open mdsumner opened 5 years ago

mdsumner commented 5 years ago

Here's a stab at getting a "neighbourhood" of values from the bins.

## extract bin values with distance radius and within a negative time window (in days)
bin_extract <- function(x, radius = 50000, days = 8) {
## input vectors of lon, lat, dates, lab for query-stations
  lon <- x[[1]]
  lat <- x[[2]]
  dates <- as.Date(x[[3]])
  lab <- as.character(x[[4]])
  l <- vector("list", length(lon))
  for (i in seq_along(dates)) {
    ll <- vector("list", days)
    for (j in seq_len(days)) {
      rt <- bin_extract1(lon[i], lat[i], dates[i] - j + 1, radius)
     if (!is.null(rt))   rt$distance_t <- unclass(as.Date(dates[i])) - unclass(as.Date(rt$date))
     ll[[j]] <- rt
    }
    #browser()
    st <- do.call(rbind, ll)
    if (!is.null(st)) st$label <- lab[i]
    l[[i]] <- st
  }
  do.call(rbind, l)
}
bin_extract1 <- function(lon, lat, date, radius) {
  ex <- extent(lon + c(-1, 1) * (radius / (1852 * 60)) * 1/cos(lat * pi/180),
               lat + c(-1, 1) * (radius / (1852 * 60)))
  init <- croc::initbin(4320)
  bins <- raadtools:::.crop_init(init, ex)
  out <- read_oc_sochla(date, bins = tibble::tibble(bin_num = bins))
  if (nrow(out) < 1) return(NULL)
  #browser()
  xy <- croc::bin2lonlat(out$bin_num, 4320)

  out$distance_m <- traipse::track_distance_to(xy[,1], xy[,2], rep(lon, nrow(xy)), rep(lat, nrow(xy)))
  out[c("blon", "blat")] <- xy
  out
}

bin_extract(kax[1, c("lon", "lat", "date", "Trawl Number")])
mdsumner commented 5 years ago

There's a lot of repeat read over days, so