## use ncdump to treat NetCDF like a virtual DB
library(ncdf4)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ncdump)
f <- "data/eclipse.ncdc.noaa.gov/pub/OI-daily-v2/NetCDF/2017/AVHRR/avhrr-only-v2.20170502_preliminary.nc"
x <- NetCDF(f)
x
## character(0)
## [1] "(sst, anom, err, ice)"
## [1] "Dimension:"
## name length unlimited
## 1 time 1 FALSE
## 2 zlev 1 FALSE
## 3 lat 720 FALSE
## 4 lon 1440 FALSE
nctive(x)
## NULL
## push sst to the front for an extraction
x <- nctivate(x, "sst")
hyper_slab <- filtrate(x, lon = lon > 100, lat = lat < 30)
## Joining, by = "id"
## Joining, by = "id"
## Joining, by = "id"
## it's alive, test the extraction
nc <- nc_open(f)
var <- ncvar_get(nc, "sst", start = bind_rows(hyper_slab)$start, count = bind_rows(hyper_slab)$count)
image(var)

## push a different var to the front
x <- nctivate(x, "anom")
hyper_slab <- filtrate(x, lon = between(lon, 50, 147), lat = between(lat, -42, 20))
## Joining, by = "id"
## Joining, by = "id"
## Joining, by = "id"
var <- ncvar_get(nc, "sst", start = bind_rows(hyper_slab)$start, count = bind_rows(hyper_slab)$count)
image(var)

hyper_slab
## $lon
## # A tibble: 1 × 3
## name start count
## <chr> <dbl> <int>
## 1 lon 201 388
##
## $lat
## # A tibble: 1 × 3
## name start count
## <chr> <dbl> <int>
## 1 lat 193 248
##
## $zlev
## # A tibble: 1 × 3
## name start count
## <chr> <dbl> <int>
## 1 zlev 1 1
##
## $time
## # A tibble: 1 × 3
## name start count
## <chr> <dbl> <int>
## 1 time 1 1