Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chlorophyll extract #95

Open
mdsumner opened this issue Aug 1, 2019 · 1 comment
Open

chlorophyll extract #95

mdsumner opened this issue Aug 1, 2019 · 1 comment

Comments

@mdsumner
Copy link
Member

mdsumner commented Aug 1, 2019

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
Copy link
Member Author

mdsumner commented Aug 1, 2019

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

  • batch station-queries into days (inside-out the i/j loops)
  • find common extent of every location that day
  • filter the table
  • extract out each station by distance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant