library(tidync)

## I found an old stale oscar.nc in my legacy public_html/R/ directory
## so I found this OpenDAP source to revisit

## we have Copernicus these days but Oscar was an early 
u <- "https://podaac-opendap.jpl.nasa.gov:443/opendap/allData/oscar/preview/L4/oscar_third_deg/oscar_vel1992.nc.gz"
(tnc <- tidync(u))
## not a file: 
## ' https://podaac-opendap.jpl.nasa.gov:443/opendap/allData/oscar/preview/L4/oscar_third_deg/oscar_vel1992.nc.gz '
## 
## ... attempting remote connection
## Connection succeeded.
## 
## Data Source (1): oscar_vel1992.nc.gz ...
## 
## Grids (6) <dimension family> : <associated variables> 
## 
## [1]   D2,D1,D0,D3 : u, um, v, vm    **ACTIVE GRID** ( 8087534  values per variable)
## [2]   D0          : depth
## [3]   D1          : latitude
## [4]   D2          : longitude
## [5]   D3          : time
## [6]   D4          : year
## 
## Dimensions (5): 
##   
##   dimension    id      name length unlim coord_dim 
##       <chr> <dbl>     <chr>  <dbl> <lgl>     <lgl> 
## 1        D0     0     depth      1 FALSE      TRUE 
## 2        D1     1  latitude    481 FALSE      TRUE 
## 3        D2     2 longitude   1201 FALSE      TRUE 
## 4        D3     3      time     14 FALSE      TRUE 
## 5        D4     4      year     14 FALSE      TRUE
tnc %>% hyper_filter()
## # A tibble: 1 x 2
##                access
##                <dttm>
## 1 2017-08-03 00:51:00
## # ... with 1 more variables: source <chr>
## filtered dimension summary: 
## # A tibble: 4 x 5
##        name coord_dim   min   max length
##       <chr>     <lgl> <dbl> <dbl>  <int>
## 1 longitude      TRUE    20   420   1201
## 2  latitude      TRUE   -80    80    481
## 3     depth      TRUE    15    15      1
## 4      time      TRUE    16    82     14
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
d <- tnc %>% hyper_filter(longitude = between(longitude, 120, 220),
                     latitude = between(latitude, -60, -30), 
                     time = index < 7) %>% 
  hyper_tibble()

library(ggplot2)
ggplot(d, aes(x  = longitude, y = latitude, fill = sqrt(u*u + v * v))) + 
  geom_raster() + scale_fill_gradient2() + facet_wrap(~time)

ggplot(d %>% dplyr::filter(longitude %in% seq(120, 220, by = 10)), 
       aes(y = time, x = latitude, fill = sqrt(u* u + v * v))) + 
  geom_raster() + scale_fill_gradient2() + facet_wrap(~longitude)

## old style
library(RNetCDF)
print.nc(open.nc(u))
## dimensions:
##         depth = 1 ;
##         latitude = 481 ;
##         longitude = 1201 ;
##         time = 14 ;
##         year = 14 ;
## variables:
##         int time(time) ;
##                 time:units = "day since 1992-10-05 00:00:00" ;
##                 time:long_name = "Day since 1992-10-05 00:00:00" ;
##         float year(year) ;
##                 year:units = "time in years" ;
##                 year:long_name = "Time in fractional year" ;
##         float depth(depth) ;
##                 depth:units = "meter" ;
##                 depth:long_name = "Depth" ;
##         double latitude(latitude) ;
##                 latitude:units = "degrees-north" ;
##                 latitude:long_name = "Latitude" ;
##         double longitude(longitude) ;
##                 longitude:units = "degrees-east" ;
##                 longitude:long_name = "Longitude" ;
##         double u(longitude, latitude, depth, time) ;
##                 u:units = "meter/sec" ;
##                 u:long_name = "Ocean Surface Zonal Currents" ;
##                 u:missing_value = NaN ;
##         double v(longitude, latitude, depth, time) ;
##                 v:units = "meter/sec" ;
##                 v:long_name = "Ocean Surface Meridional Currents" ;
##                 v:missing_value = NaN ;
##         double um(longitude, latitude, depth, time) ;
##                 um:units = "meter/sec" ;
##                 um:long_name = "Ocean Surface Zonal Currents Maximum Mask" ;
##                 um:missing_value = NaN ;
##         double vm(longitude, latitude, depth, time) ;
##                 vm:units = "meter/sec" ;
##                 vm:long_name = "Ocean Surface Meridional Currents Maximum Mask" ;
##                 vm:missing_value = NaN ;
## 
## // global attributes:
##                 :VARIABLE = "Ocean Surface Currents" ;
##                 :DATATYPE = "1/72 YEAR Interval" ;
##                 :DATASUBTYPE = "unfiltered" ;
##                 :GEORANGE = "20 to 420 -80 to 80" ;
##                 :PERIOD = "Oct.21,1992 to Dec.26,1992" ;
##                 :year = "1992" ;
##                 :description = "OSCAR Third Degree Sea Surface Velocity" ;
##                 :CREATION_DATE = "12:38 18-Sep-2012" ;
##                 :version = 2009 ;
##                 :source = "Gary Lagerloef, ESR (lager@esr.org) and Kathleen Dohan, ESR (kdohan@esr.org)" ;
##                 :contact = "Kathleen Dohan (kdohan@esr.org) or John T. Gunn (gunn@esr.org)" ;
##                 :company = "Earth & Space Research, Seattle, WA" ;
##                 :reference = "Bonjean F. and G.S.E. Lagerloef, 2002 ,Diagnostic model and analysis of the surface currents in the tropical Pacific ocean, J. Phys. Oceanogr., 32, 2,938-2,954" ;
##                 :note1 = "Maximum Mask velocity is the geostrophic component at all points + any concurrent Ekman and buoyancy components" ;
##                 :note2 = "Longitude extends from 20 E to 420 E to avoid a break in major ocean basins. Data repeats in overlap region." ;