Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created August 21, 2024 02:57
Show Gist options
  • Save njtierney/1f55bf906570949be363086daeaf97ac to your computer and use it in GitHub Desktop.
Save njtierney/1f55bf906570949be363086daeaf97ac to your computer and use it in GitHub Desktop.
# from https://thredds.nci.org.au/thredds/catalog/gb6/BRAN/BRAN2020/daily/catalog.html?dataset=gb6/BRAN/BRAN2020/daily/ocean_temp_1995_06.nc
library(tidyverse)
library(terra)
nci_url <- "https://thredds.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/ocean_temp_1995_06.nc"
vsi <- function(url) glue::glue("/vsicurl/{url}")
sf::gdal_utils("info", vsi(nci_url))
ncdf4::nc_open()
# SST data of Antarctica
# can we start at a station
# can we scale that up
# SST from now (ish) until 1st Sept 1981 every day
# template the year, month, actual day
# always same format
sst_url <- "https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/202408/oisst-avhrr-v02r01.20240801.nc"
curl::curl_download(sst_url, basename(sst_url))
sst_file <- "oisst-avhrr-v02r01.20240801.nc"
subdata <- function(filename,
varname = NULL,
driver = "NetCDF") {
stopifnot(!is.null(varname))
glue::glue('{driver}:"{filename}":{varname}')
}
subdata(sst_file, "sst")
sst_aug <- subdata(sst_file, "sst") |> rast()
plot(sst_aug)
sst_aug <- rast(subdata(vsi(sst_url)))
date <- as.Date("2024-01-01")
aug <- rast("~/Downloads/oisst-avhrr-v02r01.20240801.nc")
aug
"20240801"
underway_url <- "https://github.com/mdsumner/nuyina.underway/raw/main/data-raw/nuyina_underway.parquet"
curl::curl_download(underway_url, basename(underway_url))
data_underway <- nanoparquet::read_parquet(
basename(underway_url)
)
data_underway$datetime - 1
library(tidyverse)
past_week_of_data <- data_underway |>
filter(between(as.Date(datetime), date - 7, date + 7 )
)
data_underway |>
filter(between(as.Date(datetime), date - 7, date + 7 )
)
data_underway$verysouth <- data_underway$latitude < -60
data_underway$segment <- c(0, cumsum(abs(diff(data_underway$verysouth))))
data_underway$segment[!data_underway$verysouth] <- NA
plot(data_underway$datetime, data_underway$latitude, pch = ".", col = sample(palr::d_pal(data_underway$segment)))
plot(data_underway$datetime, data_underway$sea_water_temperature, pch = ".")
library(duckdb)
dbConnect(duckdb(), "nuyina_underway.parquet")
@mdsumner
Copy link

So this works (on my docker image)

library(duckdb)
underway_url <- "https://github.com/mdsumner/nuyina.underway/raw/main/data-raw/nuyina_underway.parquet"
curl::curl_download(underway_url, basename(underway_url))
con <- dbConnect(duckdb(), "")
x <- dbSendQuery(con, "SELECT longitude, latitude, datetime FROM nuyina_underway.parquet WHERE nuyina_underway.datetime > '2024-08-01 00:00:00';")
dbFetch(x)

and this ( I don't know how to do this lazy, open_dataset() does not work on the url, but it does on the downloaded parquet)

underway_url <- "https://github.com/mdsumner/nuyina.underway/raw/main/data-raw/nuyina_underway.parquet"
arrow::read_parquet(underway_url)

This also works, so I think it's down to the GDAL version.

library(terra)
nci_url <- "https://thredds.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2020/daily/ocean_temp_2023_12.nc"
vsi <- function(url) glue::glue("/vsicurl/{url}")
rast(vsi(nci_url))
class       : SpatRaster
dimensions  : 1500, 3600, 1530  (nrow, ncol, nlyr)
resolution  : 0.1, 0.1  (x, y)
extent      : -9.507305e-10, 360, -75, 75  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs
source      : ocean_temp_1995_06.nc:temp
varname     : temp (Potential temperature)
names       : temp_~n=2.5, temp_~n=7.5, temp_~=12.5, temp_~11816, temp_~72949, temp_~98828, ...
unit        :   degrees C,   degrees C,   degrees C,   degrees C,   degrees C,   degrees C, ...

also this

sst_url <- "https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/202408/oisst-avhrr-v02r01.20240801.nc"
vsi <- function(url) glue::glue("/vsicurl/{url}")
subdata <- function(filename,
                    varname = NULL,
                    driver = "NetCDF") {
  stopifnot(!is.null(varname))
  glue::glue('{driver}:"{filename}":{varname}')
}

subdata(vsi(sst_url), "sst") |> rast()
class       : SpatRaster
dimensions  : 720, 1440, 1  (nrow, ncol, nlyr)
resolution  : 0.25, 0.25  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
source      : oisst-avhrr-v02r01.20240801.nc:sst
varname     : sst (Daily sea surface temperature)
name        : sst_zlev=0
unit        :    Celsius
time (days) : 2024-08-01

a very complex model extract

 ncdump -h ocean_avg_0538-0610_temp_ymonmean.nc
netcdf ocean_avg_0538-0610_temp_ymonmean {
dimensions:
        ocean_time = UNLIMITED ; // (12 currently)
        xi_rho = 3150 ;
        eta_rho = 2650 ;
        s_rho = 31 ;
variables:
        double ocean_time(ocean_time) ;
                ocean_time:standard_name = "time" ;
                ocean_time:long_name = "averaged time since initialization" ;
                ocean_time:units = "seconds since 2007-01-01 00:00:00" ;
                ocean_time:calendar = "gregorian" ;
                ocean_time:axis = "T" ;
        double s_rho(s_rho) ;
                s_rho:long_name = "S-coordinate at RHO-points" ;
                s_rho:positive = "up" ;
                s_rho:axis = "Z" ;
                s_rho:standard_name = "ocean_s_coordinate_g2" ;
                s_rho:field = "s_rho, scalar" ;
        double Cs_r(s_rho) ;
                Cs_r:long_name = "S-coordinate stretching curves at RHO-points" ;
                Cs_r:field = "Cs_r, scalar" ;
        double h(eta_rho, xi_rho) ;
                h:long_name = "bathymetry at RHO-points" ;
                h:units = "meter" ;
                h:field = "bath, scalar" ;
        float temp(ocean_time, s_rho, eta_rho, xi_rho) ;
                temp:long_name = "time-averaged potential temperature" ;
                temp:units = "Celsius" ;
                temp:_FillValue = 1.e+37f ;
                temp:missing_value = 1.e+37f ;
                temp:time = "ocean_time" ;
                temp:field = "temperature, scalar, series" ;
        float zeta(ocean_time, eta_rho, xi_rho) ;
                zeta:long_name = "time-averaged free-surface" ;
                zeta:units = "meter" ;
                zeta:_FillValue = 1.e+37f ;
                zeta:missing_value = 1.e+37f ;
                zeta:time = "ocean_time" ;
                zeta:field = "free-surface, scalar, series" ;

// global attributes:
                :CDI = "Climate Data Interface version 1.9.8 (https://mpimet.mpg.de/cdi)" ;
                :Conventions = "CF-1.4" ;
                :history = "Fri Oct 09 19:31:22 2020: cdo ymonmean ocean_avg_0538-0610_temp.nc ocean_avg_0538-0610_temp_ymonmean.nc\n",
                        "Mon Nov 12 20:12:18 2018: ncks -v temp ocean_avg_0538.nc ocean_avg_0538_temp.nc\n",
                        "ROMS/TOMS, Version 3.7, Friday - August 10, 2018 -  4:44:35 PM" ;
                :file = "ocean_avg_0538.nc" ;
                :format = "netCDF-3 64bit offset file" ;
                :type = "ROMS/TOMS nonlinear model averages file" ;
                :title = "Whole Antarctic and Ocean Application, 2 km resolution" ;
                :rst_file = "ocean_rst.nc" ;
                :his_base = "ocean_his" ;
                :avg_base = "ocean_avg" ;
                :grd_file = "/g/data2/gh9/oxr581/waom_frc/waom2_grd.nc" ;
                :ini_file = "ocean_rst.nc" ;
                :frc_file_01 = "/g/data2/gh9/oxr581/waom_frc/waom2_tds.nc" ;
                :frc_file_02 = "/g/data2/gh9/oxr581/waom_frc/waom2_shflux.nc" ;
                :frc_file_03 = "/g/data2/gh9/oxr581/waom_frc/waom2_swflux.nc" ;
                :frc_file_04 = "/g/data2/gh9/oxr581/waom_frc/waom2_sustr.nc" ;
                :frc_file_05 = "/g/data2/gh9/oxr581/waom_frc/waom2_svstr.nc" ;
                :frc_file_06 = "/g/data2/gh9/oxr581/waom_frc/waom2_nudge.nc" ;
                :bry_file = "/g/data2/gh9/oxr581/waom_frc/waom2_bry.nc" ;
                :NLM_LBC = "\n",
                        "EDGE:  WEST   SOUTH  EAST   NORTH  \n",
                        "zeta:  Cha    Cha    Cha    Cha    \n",
                        "ubar:  Fla    Fla    Fla    Fla    \n",
                        "vbar:  Fla    Fla    Fla    Fla    \n",
                        "u:     RadNud RadNud RadNud RadNud \n",
                        "v:     RadNud RadNud RadNud RadNud \n",
                        "temp:  RadNud RadNud RadNud RadNud \n",
                        "salt:  RadNud RadNud RadNud RadNud" ;
                :svn_url = "https:://myroms.org/svn/src" ;
                :svn_rev = "exported" ;
                :code_dir = "/short/gh9/oxr581/waom2_fix" ;
                :header_dir = "/short/gh9/oxr581/waom2_fix/ROMS/Include" ;
                :header_file = "waom.h" ;
                :os = "Linux" ;
                :cpu = "x86_64" ;
                :compiler_system = "ifort" ;
                :compiler_command = "/apps/openmpi/wrapper/fortran/mpif90" ;
                :compiler_flags = "-heap-arrays -fp-model precise -ip -O3 -free -free -free" ;
                :tiling = "008x028" ;
                :ana_file = "ROMS/Functionals/ana_btflux.h, ROMS/Functionals/ana_srflux.h" ;
                :CPP_options = "WAOM2, ADD_FSOBC, ADD_M2OBC, ANA_BSFLUX, ANA_BTFLUX, ANA_SRFLUX, ASSUMED_SHAPE, AVERAGES, CURVGRID, DJ_GRADPS, DOUBLE_PRECISION, ICESHELF, LIMIT_BSTRESS, INLINE_2DIO, LMD_BKPP, LMD_CONVEC, LMD_DDMIX, LMD_MIXING, LMD_NONLOCAL, LMD_RIMIX, LMD_SHAPIRO, LMD_SKPP, MASKING, MIX_ISO_TS, MIX_S_UV, MPI, NONLINEAR, NONLIN_EOS, PERFECT_RESTART, POWER_LAW, PROFILE, QCORRECTION, RI_SPLINES, !RST_SINGLE, SALINITY, SCORRECTION, SOLVE3D, SPLINES_VDIFF, SPLINES_VVISC, SPHERICAL, SSH_TIDES, TS_A4HADVECTION, TS_A4VADVECTION, TS_DIF2, UV_ADV, UV_COR, UV_U3HADVECTION, UV_C4VADVECTION, UV_QDRAG, UV_TIDES, UV_VIS2, VAR_RHO_2D" ;
                :NCO = "\"4.6.4\"" ;
                :CDO = "Climate Data Operators version 1.9.8 (https://mpimet.mpg.de/cdo)" ;
}

looks like this to GDAL

Driver: netCDF/Network Common Data Format
Files: ocean_avg_0538-0610_temp_ymonmean.nc
Size is 512, 512
Metadata:
  NC_GLOBAL#ana_file=ROMS/Functionals/ana_btflux.h, ROMS/Functionals/ana_srflux.h
  NC_GLOBAL#avg_base=ocean_avg
  NC_GLOBAL#bry_file=/g/data2/gh9/oxr581/waom_frc/waom2_bry.nc
  NC_GLOBAL#CDI=Climate Data Interface version 1.9.8 (https://mpimet.mpg.de/cdi)
  NC_GLOBAL#CDO=Climate Data Operators version 1.9.8 (https://mpimet.mpg.de/cdo)
  NC_GLOBAL#code_dir=/short/gh9/oxr581/waom2_fix
  NC_GLOBAL#compiler_command=/apps/openmpi/wrapper/fortran/mpif90
  NC_GLOBAL#compiler_flags=-heap-arrays -fp-model precise -ip -O3 -free -free -free
  NC_GLOBAL#compiler_system=ifort
  NC_GLOBAL#Conventions=CF-1.4
  NC_GLOBAL#CPP_options=WAOM2, ADD_FSOBC, ADD_M2OBC, ANA_BSFLUX, ANA_BTFLUX, ANA_SRFLUX, ASSUMED_SHAPE, AVERAGES, CURVGRID, DJ_GRADPS, DOUBLE_PRECISION, ICESHELF, LIMIT_BSTRESS, INLINE_2DIO, LMD_BKPP, LMD_CONVEC, LMD_DDMIX, LMD_MIXING, LMD_NONLOCAL, LMD_RIMIX, LMD_SHAPIRO, LMD_SKPP, MASKING, MIX_ISO_TS, MIX_S_UV, MPI, NONLINEAR, NONLIN_EOS, PERFECT_RESTART, POWER_LAW, PROFILE, QCORRECTION, RI_SPLINES, !RST_SINGLE, SALINITY, SCORRECTION, SOLVE3D, SPLINES_VDIFF, SPLINES_VVISC, SPHERICAL, SSH_TIDES, TS_A4HADVECTION, TS_A4VADVECTION, TS_DIF2, UV_ADV, UV_COR, UV_U3HADVECTION, UV_C4VADVECTION, UV_QDRAG, UV_TIDES, UV_VIS2, VAR_RHO_2D
  NC_GLOBAL#cpu=x86_64
  NC_GLOBAL#file=ocean_avg_0538.nc
  NC_GLOBAL#format=netCDF-3 64bit offset file
  NC_GLOBAL#frc_file_01=/g/data2/gh9/oxr581/waom_frc/waom2_tds.nc
  NC_GLOBAL#frc_file_02=/g/data2/gh9/oxr581/waom_frc/waom2_shflux.nc
  NC_GLOBAL#frc_file_03=/g/data2/gh9/oxr581/waom_frc/waom2_swflux.nc
  NC_GLOBAL#frc_file_04=/g/data2/gh9/oxr581/waom_frc/waom2_sustr.nc
  NC_GLOBAL#frc_file_05=/g/data2/gh9/oxr581/waom_frc/waom2_svstr.nc
  NC_GLOBAL#frc_file_06=/g/data2/gh9/oxr581/waom_frc/waom2_nudge.nc
  NC_GLOBAL#grd_file=/g/data2/gh9/oxr581/waom_frc/waom2_grd.nc
  NC_GLOBAL#header_dir=/short/gh9/oxr581/waom2_fix/ROMS/Include
  NC_GLOBAL#header_file=waom.h
  NC_GLOBAL#history=Fri Oct 09 19:31:22 2020: cdo ymonmean ocean_avg_0538-0610_temp.nc ocean_avg_0538-0610_temp_ymonmean.nc
Mon Nov 12 20:12:18 2018: ncks -v temp ocean_avg_0538.nc ocean_avg_0538_temp.nc
ROMS/TOMS, Version 3.7, Friday - August 10, 2018 -  4:44:35 PM
  NC_GLOBAL#his_base=ocean_his
  NC_GLOBAL#ini_file=ocean_rst.nc
  NC_GLOBAL#NCO="4.6.4"
  NC_GLOBAL#NLM_LBC=
EDGE:  WEST   SOUTH  EAST   NORTH
zeta:  Cha    Cha    Cha    Cha
ubar:  Fla    Fla    Fla    Fla
vbar:  Fla    Fla    Fla    Fla
u:     RadNud RadNud RadNud RadNud
v:     RadNud RadNud RadNud RadNud
temp:  RadNud RadNud RadNud RadNud
salt:  RadNud RadNud RadNud RadNud
  NC_GLOBAL#os=Linux
  NC_GLOBAL#rst_file=ocean_rst.nc
  NC_GLOBAL#svn_rev=exported
  NC_GLOBAL#svn_url=https:://myroms.org/svn/src
  NC_GLOBAL#tiling=008x028
  NC_GLOBAL#title=Whole Antarctic and Ocean Application, 2 km resolution
  NC_GLOBAL#type=ROMS/TOMS nonlinear model averages file
Subdatasets:
  SUBDATASET_1_NAME=NETCDF:"ocean_avg_0538-0610_temp_ymonmean.nc":h
  SUBDATASET_1_DESC=[2650x3150] h (64-bit floating-point)
  SUBDATASET_2_NAME=NETCDF:"ocean_avg_0538-0610_temp_ymonmean.nc":temp
  SUBDATASET_2_DESC=[12x31x2650x3150] temp (32-bit floating-point)
  SUBDATASET_3_NAME=NETCDF:"ocean_avg_0538-0610_temp_ymonmean.nc":zeta
  SUBDATASET_3_DESC=[12x2650x3150] zeta (32-bit floating-point)
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,  512.0)
Upper Right (  512.0,    0.0)
Lower Right (  512.0,  512.0)
Center      (  256.0,  256.0)

pick on var variable (subdataset)

Warning 1: dimension #3 (xi_rho) is not a Longitude/X dimension.
Warning 1: dimension #2 (eta_rho) is not a Latitude/Y dimension.
Driver: netCDF/Network Common Data Format
Files: ocean_avg_0538-0610_temp_ymonmean.nc
Size is 3150, 2650
Metadata:
  NC_GLOBAL#ana_file=ROMS/Functionals/ana_btflux.h, ROMS/Functionals/ana_srflux.h
  NC_GLOBAL#avg_base=ocean_avg
  NC_GLOBAL#bry_file=/g/data2/gh9/oxr581/waom_frc/waom2_bry.nc
  NC_GLOBAL#CDI=Climate Data Interface version 1.9.8 (https://mpimet.mpg.de/cdi)
  NC_GLOBAL#CDO=Climate Data Operators version 1.9.8 (https://mpimet.mpg.de/cdo)
  NC_GLOBAL#code_dir=/short/gh9/oxr581/waom2_fix
  NC_GLOBAL#compiler_command=/apps/openmpi/wrapper/fortran/mpif90
  NC_GLOBAL#compiler_flags=-heap-arrays -fp-model precise -ip -O3 -free -free -free
  NC_GLOBAL#compiler_system=ifort
  NC_GLOBAL#Conventions=CF-1.4
  NC_GLOBAL#CPP_options=WAOM2, ADD_FSOBC, ADD_M2OBC, ANA_BSFLUX, ANA_BTFLUX, ANA_SRFLUX, ASSUMED_SHAPE, AVERAGES, CURVGRID, DJ_GRADPS, DOUBLE_PRECISION, ICESHELF, LIMIT_BSTRESS, INLINE_2DIO, LMD_BKPP, LMD_CONVEC, LMD_DDMIX, LMD_MIXING, LMD_NONLOCAL, LMD_RIMIX, LMD_SHAPIRO, LMD_SKPP, MASKING, MIX_ISO_TS, MIX_S_UV, MPI, NONLINEAR, NONLIN_EOS, PERFECT_RESTART, POWER_LAW, PROFILE, QCORRECTION, RI_SPLINES, !RST_SINGLE, SALINITY, SCORRECTION, SOLVE3D, SPLINES_VDIFF, SPLINES_VVISC, SPHERICAL, SSH_TIDES, TS_A4HADVECTION, TS_A4VADVECTION, TS_DIF2, UV_ADV, UV_COR, UV_U3HADVECTION, UV_C4VADVECTION, UV_QDRAG, UV_TIDES, UV_VIS2, VAR_RHO_2D
  NC_GLOBAL#cpu=x86_64
  NC_GLOBAL#file=ocean_avg_0538.nc
  NC_GLOBAL#format=netCDF-3 64bit offset file
  NC_GLOBAL#frc_file_01=/g/data2/gh9/oxr581/waom_frc/waom2_tds.nc
  NC_GLOBAL#frc_file_02=/g/data2/gh9/oxr581/waom_frc/waom2_shflux.nc
  NC_GLOBAL#frc_file_03=/g/data2/gh9/oxr581/waom_frc/waom2_swflux.nc
  NC_GLOBAL#frc_file_04=/g/data2/gh9/oxr581/waom_frc/waom2_sustr.nc
  NC_GLOBAL#frc_file_05=/g/data2/gh9/oxr581/waom_frc/waom2_svstr.nc
  NC_GLOBAL#frc_file_06=/g/data2/gh9/oxr581/waom_frc/waom2_nudge.nc
  NC_GLOBAL#grd_file=/g/data2/gh9/oxr581/waom_frc/waom2_grd.nc
  NC_GLOBAL#header_dir=/short/gh9/oxr581/waom2_fix/ROMS/Include
  NC_GLOBAL#header_file=waom.h
  NC_GLOBAL#history=Fri Oct 09 19:31:22 2020: cdo ymonmean ocean_avg_0538-0610_temp.nc ocean_avg_0538-0610_temp_ymonmean.nc
Mon Nov 12 20:12:18 2018: ncks -v temp ocean_avg_0538.nc ocean_avg_0538_temp.nc
ROMS/TOMS, Version 3.7, Friday - August 10, 2018 -  4:44:35 PM
  NC_GLOBAL#his_base=ocean_his
  NC_GLOBAL#ini_file=ocean_rst.nc
  NC_GLOBAL#NCO="4.6.4"
  NC_GLOBAL#NLM_LBC=
EDGE:  WEST   SOUTH  EAST   NORTH
zeta:  Cha    Cha    Cha    Cha
ubar:  Fla    Fla    Fla    Fla
vbar:  Fla    Fla    Fla    Fla
u:     RadNud RadNud RadNud RadNud
v:     RadNud RadNud RadNud RadNud
temp:  RadNud RadNud RadNud RadNud
salt:  RadNud RadNud RadNud RadNud
  NC_GLOBAL#os=Linux
  NC_GLOBAL#rst_file=ocean_rst.nc
  NC_GLOBAL#svn_rev=exported
  NC_GLOBAL#svn_url=https:://myroms.org/svn/src
  NC_GLOBAL#tiling=008x028
  NC_GLOBAL#title=Whole Antarctic and Ocean Application, 2 km resolution
  NC_GLOBAL#type=ROMS/TOMS nonlinear model averages file
  NETCDF_DIM_EXTRA={ocean_time,s_rho}
  NETCDF_DIM_ocean_time_DEF={12,6}
  NETCDF_DIM_ocean_time_VALUES={255096000,257256000,259848000,262440000,263304000,236520000,239112000,241704000,244296000,246888000,249480000,252072000}
  NETCDF_DIM_s_rho_DEF={31,6}
  NETCDF_DIM_s_rho_VALUES={-0.9838709677419355,-0.9516129032258064,-0.9193548387096774,-0.8870967741935484,-0.8548387096774194,-0.8225806451612903,-0.7903225806451613,-0.7580645161290323,-0.7258064516129032,-0.6935483870967741,-0.6612903225806451,-0.6290322580645161,-0.5967741935483871,-0.564516129032258,-0.532258064516129,-0.5,-0.4677419354838709,-0.4354838709677419,-0.4032258064516129,-0.3709677419354839,-0.3387096774193548,-0.3064516129032258,-0.2741935483870968,-0.2419354838709677,-0.2096774193548387,-0.1774193548387097,-0.1451612903225806,-0.1129032258064516,-0.08064516129032258,-0.04838709677419355,-0.01612903225806452}
  ocean_time#axis=T
  ocean_time#calendar=gregorian
  ocean_time#long_name=averaged time since initialization
  ocean_time#standard_name=time
  ocean_time#units=seconds since 2007-01-01 00:00:00
  s_rho#axis=Z
  s_rho#field=s_rho, scalar
  s_rho#long_name=S-coordinate at RHO-points
  s_rho#positive=up
  s_rho#standard_name=ocean_s_coordinate_g2
  temp#field=temperature, scalar, series
  temp#long_name=time-averaged potential temperature
  temp#missing_value=9.9999999e+36
  temp#time=ocean_time
  temp#units=Celsius
  temp#_FillValue=9.9999999e+36
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 2650.0)
Upper Right ( 3150.0,    0.0)
Lower Right ( 3150.0, 2650.0)
Center      ( 1575.0, 1325.0)
Band 1 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature
    missing_value=9.9999999e+36
    NETCDF_DIM_ocean_time=255096000
    NETCDF_DIM_s_rho=-0.9838709677419355
    NETCDF_VARNAME=temp
    time=ocean_time
    units=Celsius
    _FillValue=9.9999999e+36
Band 2 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature
    missing_value=9.9999999e+36
    NETCDF_DIM_ocean_time=255096000
    NETCDF_DIM_s_rho=-0.9516129032258064
    NETCDF_VARNAME=temp
    time=ocean_time
    units=Celsius
    _FillValue=9.9999999e+36
Band 3 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature
    missing_value=9.9999999e+36
    NETCDF_DIM_ocean_time=255096000
    NETCDF_DIM_s_rho=-0.9193548387096774
    NETCDF_VARNAME=temp
    time=ocean_time
    units=Celsius
    _FillValue=9.9999999e+36
Band 4 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature
    missing_value=9.9999999e+36
    NETCDF_DIM_ocean_time=255096000
    NETCDF_DIM_s_rho=-0.8870967741935484
    NETCDF_VARNAME=temp
    time=ocean_time
    units=Celsius
    _FillValue=9.9999999e+36
Band 5 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature
    missing_value=9.9999999e+36
    NETCDF_DIM_ocean_time=255096000
    NETCDF_DIM_s_rho=-0.8548387096774194
    NETCDF_VARNAME=temp
    time=ocean_time
    units=Celsius
    _FillValue=9.9999999e+36
Band 6 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature
    missing_value=9.9999999e+36
    NETCDF_DIM_ocean_time=255096000
    NETCDF_DIM_s_rho=-0.8225806451612903
    NETCDF_VARNAME=temp
    time=ocean_time
    units=Celsius
    _FillValue=9.9999999e+36
Band 7 Block=3150x1 Type=Float32, ColorInterp=Undefined
  NoData Value=1e+37
  Unit Type: Celsius
  Metadata:
    field=temperature, scalar, series
    long_name=time-averaged potential temperature


@mdsumner
Copy link

code as start to comparing sat to underway (will be interesting to look at how the pitch/roll and temperature seen by the ship compares minute by minute to how the daily remotes sensing data looks)

underway_url <- "https://github.com/mdsumner/nuyina.underway/raw/main/data-raw/nuyina_underway.parquet"
nuy <-arrow::read_parquet(underway_url) |> dplyr::filter(as.Date(datetime) > "2022-09-01")
 
 
tst <- nuy$latitude < -55
nuy$seg <- c(0, (cumsum(abs(diff(tst)))))
nuy$seg[!tst]  <- NA
nuy$seg <- as.integer(factor(nuy$seg))
library(ggplot2)

ggplot(nuy |> dplyr::filter(!is.na(seg)), aes(datetime, latitude, col = factor(seg))) + geom_point(pch = ".")

(dt <- as.Date(range(nuy$datetime[nuy$seg == 1], na.rm = T)) + c(0, 1))
library(dplyr)
voyage <- nuy |> dplyr::filter(between(as.Date(datetime), dt[1], dt[2]))

## raadtools lookup   (here Mike should cache this lookup for every Nuyina point)
library(raadtools)
voyage$iceconc <- extract(readice, voyage |> dplyr::select(longitude, latitude, datetime), setNA = T)
voyage$oisst <- extract(readsst, voyage |> dplyr::select(longitude, latitude, datetime))
voyage$ghrsst <- extract(readghrsst, voyage |> dplyr::select(longitude, latitude, datetime))

par(mfrow = c(2, 1))

plot(voyage$datetime, voyage$ghrsst)
lines(voyage$datetime, voyage$sea_water_temperature)


plot(voyage$datetime, voyage$oisst)
lines(voyage$datetime, voyage$sea_water_temperature)


ggplot(voyage, aes(datetime, iceconc)) + geom_path()

dateice <- voyage$datetime[min(which(voyage$iceconc > 0))] 
dateice

sst_url <- glue::glue("https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/v2.1/access/avhrr/{format(dateice, '%Y%m')}/oisst-avhrr-v02r01.{format(dateice, '%Y%m%d')}.nc")
sst_file <- basename(sst_url)
if (!file.exists(sst_file)) curl::curl_download(sst_url, sst_file)



subdata <- function(filename,
                    varname = NULL,
                    driver = "NetCDF") {
  stopifnot(!is.null(varname))
  glue::glue('{driver}:"{filename}":{varname}')
}
#vsi <- function(url) glue::glue("/vsicurl/{url}")

sstraster <- subdata(sst_file, "sst") |> rast()
iceraster <- subdata(sst_file, "ice") |> rast()

plot(voyage$longitude, voyage$latitude, asp = 1/cos(mean(voyage$latitude) * pi/180), type = "n")
plot(iceraster, add = TRUE)
lines(voyage$longitude, voyage$latitude)
points(cbind(voyage$longitude, voyage$latitude)[findInterval(as.POSIXct(dateice, tz = "UTC"), voyage$datetime),, drop = F ], pch = "X", col = "hotpink")

plot(voyage$datetime, voyage$sea_water_temperature, pch = ".")
abline(v = dateice, h = -1.8)

plot(voyage$datetime, pmax(voyage$platform_pitch_fore_up, voyage$platform_heave_down, voyage$platform_roll_starboard_down), pch = ".")
abline(v = (dateice) + c(-1, 0, 1) * 24 * 3600)

@njtierney
Copy link
Author

Thanks, @mdsumner !

I'm getting this error for raadtools, do I need to install something? Or is this perhaps a GDAL thing?

> library(raadtools)
Loading required package: raster
Loading required package: sp

Attaching package:rasterThe following object is masked frompackage:dplyr:

    select

> voyage$iceconc <- extract(readice, voyage |> dplyr::select(longitude, latitude, datetime), setNA = T)
no files found in the 'raadfiles.filename.database'
and no root directories found.
Error in .find_files_generic(pattern) : no files found

@mdsumner
Copy link

oh sorry, you can't install raadtools - you have to be on our system/s for now - which I meant to do. I'm just keeping notes here, but I'll make you an account and email deets :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment