#devtools::install_github('dbarneche/noaaErddap')
library(noaaErddap)
# downloads chlorophyll data for Jan/2006
linkToCachedFile  <-  erddapDownload(year = 2006, month = 1,
                                     type = 'chlorophyll',
                                     overwrite = FALSE)
## Warning in erddapDownload(year = 2006, month = 1, type = "chlorophyll", :
## File /perm_storage/home/mdsumner/.cache/noaaErddap/chlorophyll/
## January-2006.nc already exists, please set "overwrite = TRUE" if you want
## to overwrite existing file
## devtools::install_github("hypertidy/tidync")
library(tidync)
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
## hone in on first two time steps, and zoom in a bit
tab <- tidync(linkToCachedFile) %>%
  hyper_filter(longitude  = longitude > 130, time = index <= 2, latitude = between(latitude, -60, -30)) %>% hyper_tibble()

## worker functions for plot
##
## local scale [0,1]
scl <- function(x) {rng <- range(x, na.rm = T); (x - rng[1])/diff(rng)}
## chlorophyll NASA ocean colour
pal <- palr::chlPal(palette = TRUE)

library(ggplot2)
ggplot(tab %>% dplyr::filter(!is.na(chlorophyll))) + geom_raster(aes(longitude, latitude, fill = chlorophyll)) +
       facet_wrap(~time) +
scale_fill_gradientn(values = scl(head(pal$breaks, -1)), colours = pal$cols) +
  coord_fixed(ratio = 1/sin(45 * pi/180))