Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active March 26, 2025 07:31
Show Gist options
  • Save mdsumner/638848fb77d3b6d36a36ce0ea3e37350 to your computer and use it in GitHub Desktop.
Save mdsumner/638848fb77d3b6d36a36ce0ea3e37350 to your computer and use it in GitHub Desktop.
reticulate::use_python("/workenv/bin/python3")
reticulate::py_require("numcodecs")
numcodecs <- reticulate::import("numcodecs")

## just found by eye
zlib <- numcodecs$zlib$Zlib(4L)

file <- "NSIDC0051_SEAICE_PS_S25km_19781026_v2.0.nc"

virtualizarr <- reticulate::import("virtualizarr")
varname <- "N07_ICECON"

## I can get the encoding if I *load* it, which means the reference-encoding is somewhere else in the xarray
ds <- virtualizarr$open_virtual_dataset(file, loadable_variables = varname)

enc <- ds[[varname]]$encoding
ds <- virtualizarr$open_virtual_dataset(file)

## the manifest of references, we just grab one (there is only one in this file)
mani <- ds[[varname]]$variable$data$manifest$dict()[[1]]

## raw binary connection
con <- file(mani$path, "rb")

## seek to start of compressed chunk
seek(con, mani$offset)
endian <- "little"
bytes <- readBin(con, "raw", size = 1, n = mani$length, endian = "big", signed = TRUE)
close(con)


## these are uint8
ints <- readBin(as.raw(zlib$decode(bytes)), "int", size = 1, signed = FALSE, n = prod(unlist(enc$chunksizes)))
ximage::ximage(matrix(ints, rev(unlist(enc$chunksizes))[2]))

image

@mdsumner
Copy link
Author

 mani
$path
[1] "file:///home/NSIDC0051_SEAICE_PS_S25km_19781026_v2.0.nc"

$offset
[1] 48113

$length
[1] 29672

> str(enc)
List of 13
 $ chunksizes      :List of 3
  ..$ : int 1
  ..$ : int 332
  ..$ : int 316
 $ fletcher32      : logi FALSE
 $ shuffle         : logi TRUE
 $ preferred_chunks:List of 3
  ..$ time: int 1
  ..$ y   : int 332
  ..$ x   : int 316
 $ zlib            : logi TRUE
 $ complevel       : int 4
 $ source          : chr "<fsspec.implementations.local.LocalFileOpener object at 0x7fe7df5ad780>"
 $ original_shape  :List of 3
  ..$ : int 1
  ..$ : int 332
  ..$ : int 316
 $ dtype           :uint8
 $ _FillValue      : int 255
 $ scale_factor    : num 0.004
 $ add_offset      : num 0
 $ coordinates     : chr "time y x"

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