library(rerddap)
#devtools::install_github("hypertidy/tidync")
library(tidync)

url <- "https://salishsea.eos.ubc.ca/erddap/"
d <- ed_datasets(url=url, which='griddap')
out <- info(d$Dataset.ID[18], url=url)

## digging under hood of griddap()...
x <- rerddap:::as.info(out, url)
d <- attr(out, "datasetid")


## now to dig into the actual problem data source
source_nc <- sprintf("%sgriddap/%s", url, d)
## note this only downloads metadata
tnc <- tidync(source_nc)
## not a file: 
## ' https://salishsea.eos.ubc.ca/erddap/griddap/ubcSSg3DBiologyFields1hV17-02 '
## 
## ... attempting remote connection
## Connection succeeded.
## these are very large 4D grids
## with many variables in the D1,D2,D0,D3 grid
tnc
## 
## Data Source (1): ubcSSg3DBiologyFields1hV17-02 ...
## 
## Grids (5) <dimension family> : <associated variables> 
## 
## [1]   D1,D2,D0,D3 : ammonium, biogenic_silicon, ciliates, diatoms, dissolved_organic_nitrogen, flagellates, Fraser_tracer, mesozooplankton, microzooplankton, nitrate, particulate_organic_nitrogen, silicon    **ACTIVE GRID** ( 423738182400  values per variable)
## [2]   D0          : depth
## [3]   D1          : gridX
## [4]   D2          : gridY
## [5]   D3          : time
## 
## Dimensions (4): 
##   
##   dimension    id name   length unlim coord_dim active start count 
##   <chr>     <dbl> <chr>   <dbl> <lgl> <lgl>     <lgl>  <int> <int> 
## 1 D0         0    depth    40.0 F     T         T          1    40 
## 2 D1         1.00 gridX   398   F     T         T          1   398 
## 3 D2         2.00 gridY   898   F     T         T          1   898 
## 4 D3         3.00 time  29640   F     T         T          1 29640
## analogous filtering as used for griddap
## can remove 'index' and use the values, but
## this won't work for time
d <- tnc %>% hyper_filter(
  #gridY = gridY >= 505 & gridY <= 510, 
  #                   gridX = gridX >= 204 & gridX <= 216, 
                     time = index < 3, 
                     depth = index < 4) %>% 
  ## a good idea to select a small number of vars and see what we have
  hyper_tibble(select_var = "nitrate")

library(ggplot2)
ggplot(d, aes(gridX, gridY, fill = nitrate)) + geom_raster() + facet_wrap(~time+depth)