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

function to produce arbitrarily oriented rasters #47

Open
mdsumner opened this issue Jul 25, 2017 · 2 comments
Open

function to produce arbitrarily oriented rasters #47

mdsumner opened this issue Jul 25, 2017 · 2 comments

Comments

@mdsumner
Copy link
Collaborator

This would be useful for building rasters and other formats:

hf <- tidync(source) %<% hyper_filter(...) 
raw <- hyper_slice(hf)
## modify time filter or update file/source
raw2 <- hyper_slice(hf)

hyper_combine(hf, raw) %>% hyper_output("raster") ## something like this
@mdsumner
Copy link
Collaborator Author

I think we can just do hyper_raster. This needs a whole lot more, to allow 3D slices, or to collapse multiple 2D variables onto a brick.

hyper_raster <- function(x, ...) {
  UseMethod("hyper_raster")
}
hyper_raster.tidync <- function(x, ...) {
  tnc <- hyper_filter(x, ...) 
  ax <- tidync:::active_axis_transforms(tnc)
  revy <- identical(order(ax[[2]][[names(ax)[2]]]), rev(seq_len(nrow(ax[[2]]))))
  arr <- hyper_slice(tnc)
  if (length(arr) > 1) warning("only first variable will be used")
  arr <- arr[[1]]
  xx <- ax[[1]] %>% dplyr::filter(.data$selected)
  yy <- ax[[2]] %>% dplyr::filter(.data$selected)
  if (revy) {
    yy <- yy[nrow(yy):1, ]
    arr <- arr[, ncol(arr):1]
  } else {
    #arr <- arr[nrow(arr):1, ]
  }
  ## need to buffer the range by half cell here
  ex <- raster::extent(range(xx[[names(ax)[1]]]), range(yy[[names(ax)[2]]]))
  setExtent(raster::raster(t(arr[, ncol(arr):1])), ex)
}


file <- system.file("extdata", "oceandata", "S20092742009304.L3m_MO_CHL_chlor_a_9km.nc", package = "tidync")
#file <- raadtools::sstfiles()$fullname[1]
library(raster)
library(tidync)

x <- tidync(file)
tnc <- hyper_filter(x, lon = dplyr::between(lon, 100, 200), lat = lat < 0 & lat > -55)
plot(hyper_raster(tnc), col = palr::sstPal(1000))
maps::map(add = TRUE)

@mdsumner mdsumner changed the title function to link hyper_slice() and hyper_filter() function to produce arbitrarily oriented rasters Dec 19, 2017
@mdsumner
Copy link
Collaborator Author

mdsumner commented Apr 16, 2018

I inadvertently recreated hyper_raster with the y-direction check: https://github.com/hypertidy/tidync/blob/expt-group-by/R/filter-groupby.R

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