Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active March 27, 2025 03:34
Show Gist options
  • Save mdsumner/65c506aae5e13b01055a274f00e46513 to your computer and use it in GitHub Desktop.
Save mdsumner/65c506aae5e13b01055a274f00e46513 to your computer and use it in GitHub Desktop.
##  docker run --rm -ti ghcr.io/mdsumner/gdal-builds:rocker-gdal-dev-python bash
reticulate::use_python("/workenv/bin/python3")
library(reticulate)
py_require("virtualizarr")
open_virtual_mfdataset <- import("virtualizarr")$open_virtual_mfdataset
ds <- open_virtual_mfdataset(list("https://thredds.nci.org.au/thredds/fileServer/gb6/BRAN/BRAN2023/daily/atm_flux_diag_2024_06.nc"))

<xarray.Dataset> Size: 8GB
Dimensions:     (lon: 3600, lat: 1500, Time: 30, nv: 2)
Coordinates:
  * lon         (lon) float64 29kB 0.05 0.15 0.25 0.35 ... 359.8 359.9 360.0
  * lat         (lat) float64 12kB -74.95 -74.85 -74.75 ... 74.75 74.85 74.95
  * Time        (Time) datetime64[ns] 240B 2024-06-01T12:00:00 ... 2024-06-30...
  * nv          (nv) float64 16B 1.0 2.0
Data variables: (12/18)
    land_mask   (lat, lon) float32 22MB ManifestArray<shape=(1500, 3600), dty...
    ice_mask    (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    wind        (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    t_surf      (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    tau_x       (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    tau_y       (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    ...          ...
    q_ref       (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    t_ref       (Time, lat, lon) float32 648MB ManifestArray<shape=(30, 1500,...
    average_T1  (Time) float64 240B ManifestArray<shape=(30,), dtype=float64,...
    average_T2  (Time) float64 240B ManifestArray<shape=(30,), dtype=float64,...
    average_DT  (Time) float64 240B ManifestArray<shape=(30,), dtype=float64,...
    Time_bnds   (Time, nv) float64 480B ManifestArray<shape=(30, 2), dtype=fl...
Attributes:
    filename:           atm_flux_diag_2024_06_01.nc
    title:              BRAN2023
    grid_type:          regular
    grid_tile:          N/A
    history:            Mon Dec 23 18:04:11 2024: ncrcat -4 --dfl_lvl 1 --cnk...
    NCO:                netCDF Operators version 5.0.5 (Homepage = http://nco...
    catalogue_doi_url:  https://dx.doi.org/10.25914/2wxj-vt48
    acknowledgement:    BRAN output is made freely available by CSIRO Bluelin...

A more full version in GDAL multdim is illustrated here, this open_virtual_mfdataset is very new:

https://github.com/mdsumner/pymdim/blob/main/inst/examples/create_vrt_mdim.R

py_require("gdal")
gdal <- import("osgeo.gdal")
gdal$UseExceptions()

ds <- gdal$OpenEx("/vsicurl/https://raw.githubusercontent.com/mdsumner/pymdim/refs/heads/main/inst/examples/bluelink_ocean_salt.vrt",
     gdal$OF_MULTIDIM_RASTER)

rg <- ds$GetRootGroup()
rg$GetMDArrayNames()
# [1] "Time"           "Time_bounds"    "average_DT"     "average_T1"
# [5] "average_T2"     "nv"             "salt"           "st_edges_ocean"
# [9] "st_ocean"       "xt_ocean"       "yt_ocean"
 
lapply(rg$OpenMDArray("salt")$GetDimensions(), \(.x) .x$GetSize())
[[1]]
[1] 11322

[[2]]
[1] 51

[[3]]
[1] 1500

[[4]]
[1] 3600

rg <- ds$OpenRootGroup() rg$

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