A WebAssembly-powered library for parsing meteorological and geospatial data formats. New here? Jump to Choosing your pathway — it routes you from "what I have" to the right function in one screen.
Supported formats:
| Format | Extension(s) | Notes |
|---|---|---|
| GRIB2 | .grb2, .grib2 |
Grid templates 0, 30, 40, 101; simple + complex packing; Section-6 bitmaps (masked points → NaN) |
| NetCDF3 Classic | .nc3 |
Full CF coordinate support |
| NetCDF4 / HDF5 | .nc, .nc4 |
Uses h5wasm under the hood; requires Node 18+ / browser |
| Zarr v2 (zip) | .zarr.zip, .zip |
Compressors null/gzip/zlib (built-in) + blosc/zstd/lz4 (via numcodecs); synthetic axes (see below) |
| TIFF / GeoTIFF | .tif, .tiff |
UInt8/16/Int16/Float32; LZW/Deflate; horizontal/FP predictor; WGS84/UTM/sinusoidal; tile + strip; COG over HTTP Range |
There's more than one way through this library. The two questions that actually branch the API are "point or area?" and "object or file-ready output?". Pick your path here, then jump to that function's reference below.
You have a file / URL / bytes
│
├─ "What format is this?" ..................... detectFormat → 'grib2'…'tiff' | null
├─ "What's inside?" (vars, time axis, bbox) ... scan → metadata object
│
└─ "Give me the data"
│
├─ at ONE point (lat/lon) ............... extract → object
│ extractOutput → json csv netcdf3 zarr
│
└─ over an AREA (bbox) .................. extractGrid → Float32 grid
extractGridOutput → + geotiff png imagedata
│
├─ draw on a web map → gridToImageData / gridToPNG (+ ramps)
└─ save as a raster → gridToGeoTIFF / gridToJSON
Want a smaller file, same format out? ............... trim → { bytes, … }
Reusing one loaded file for many queries? ............ HydroSlice (class) → instance
| I want to… | Function | Returns |
|---|---|---|
| Sniff the format without parsing | detectFormat |
'grib2'…'tiff' | null |
| List variables / time axis / bbox | scan |
metadata object |
| One value at a lat/lon | extract |
object |
| …as a file in any supported format | extractOutput |
string | Uint8Array |
| A whole bbox grid (parallel, abortable) | extractGrid |
{ data: Float32Array, … } |
| …as a file in any supported format | extractGridOutput |
string | Uint8Array | ImageData |
| Encode a grid I already have | encodeGrid / encodeSeries |
string | Uint8Array |
| Render a grid for a web map | gridToImageData / gridToPNG |
RGBA / PNG |
| Save a grid as a raster | gridToGeoTIFF / gridToJSON |
Uint8Array / string |
| Trim a huge file, same format | trim |
{ bytes, … } |
| Reuse one loaded file across queries | HydroSlice class |
instance |
Every reader produces the same scan / extract / extractGrid shapes
regardless of format, so once you've chosen a pathway it works identically for
GRIB2, NetCDF3/4, Zarr, and TIFF/COG. For the internals of how each format is
decoded, read the reader sources under lib/.
hydroslice/
├── index.js ← front-facing entry point (import from here)
├── index.d.ts ← TypeScript types
├── lib/ ← JavaScript library source (internal — do not import directly)
│ ├── hydroslice-api.js functional API (scan/extract/extractGrid/trim/…)
│ ├── hydroslice-lib.js core class (HydroSlice)
│ ├── zarr-helper.js Zarr v2 helper tiff-helper.js TIFF/GeoTIFF
│ ├── render/ color ramps + grid → RGBA / PNG
│ └── trim/ in-place file trimming (per-format)
├── wasm/ ← compiled C core (internal)
│ ├── hydroslice.js Emscripten WASM loader
│ └── hydroslice.wasm compiled C binary
├── worker/ ← Web Worker for parallel bbox extraction
└── dist/ ← the published, bundled package (produced by `npm run build`)
All public symbols are re-exported through index.js (which npm run build
bundles into dist/index.js). Files inside lib/, wasm/, and worker/ are
internal — import only from the package root ('hydroslice').
# from GitHub (until published to npm)
npm install git+https://github.com/uihilab/HydroSlice.git// Functional API — recommended for most consumers
import { detectFormat, scan, extract, extractOutput } from 'hydroslice';
// Error classes — same package, no extra import path needed
import { HydroSliceError, UnsupportedFormatError, VariableNotFoundError } from 'hydroslice';
// Class-based API — for advanced / low-level use
import { HydroSlice } from 'hydroslice';TypeScript types ship with the package — no @types/* needed.
Every function takes a source as its first argument:
| Type | Notes |
|---|---|
Uint8Array |
Raw bytes — fastest, no copy |
ArrayBuffer |
Wrapped into Uint8Array automatically |
Blob / File |
Browser <input type="file"> or fetch() response body |
URL / string URL |
Fetched fully into memory (http://, https://, file://) |
Magic-byte sniff only — does not initialise WASM. Returns null for anything unrecognised.
await detectFormat(file); // 'grib2'
await detectFormat('https://…/forecast.nc'); // 'netcdf4'
await detectFormat(zarrZip); // 'zarr'
await detectFormat(new Uint8Array([0,1,2,3])); // null| Magic bytes | Detected as |
|---|---|
GRIB (0x47525942) |
'grib2' |
CDF\x01 / CDF\x02 |
'netcdf3' |
\x89HDF\r\n\x1a\n |
'netcdf4' |
PK\x03\x04 (ZIP) |
'zarr' |
II*\x00 (little-endian TIFF) |
'tiff' |
Loads and scans the file. Returns metadata and the full variable list.
const meta = await scan(file);
console.log(meta.format); // 'grib2' | 'netcdf3' | 'netcdf4'
console.log(meta.total_variables); // 12
console.log(meta.variable_names); // ['2t', 'sp', ...]
console.log(meta.variables[0]);
// {
// index: 0, name: '2t', supported: true,
// grid_template: 101, data_template: 0,
// nx: 2949120, ny: 1, messages: 1
// }Format-specific fields in the result:
| Format | Extra fields on ScanResult |
|---|---|
| GRIB2 | grid_templates[], data_templates[], units[], bbox |
| NetCDF4 | shapes[], units[], bbox |
A NetCDF file on a projected grid carries x/y axes in metres plus a CRS
container variable, not latitude and longitude. Read as degrees those axes are
meaningless — an x of -1902530 is 1902 km west of the projection origin, not
a longitude — so the library detects the case and converts.
Detection uses three signals, strongest first: standard_name of
projection_x_coordinate / projection_y_coordinate; linear axis units
(m, metre, km, …); and, as a backstop for files that declare neither, axis
values outside geographic range (|lat| > 90 or |lon| > 360). The CRS comes from
the variable named by a data variable's grid_mapping attribute, or from any
variable carrying a grid_mapping_name.
Converted grid_mapping_name values: latitude_longitude,
polar_stereographic, lambert_conformal_conic, albers_conical_equal_area,
sinusoidal, transverse_mercator. Spherical (earth_radius) and ellipsoidal
(semi_major_axis + semi_minor_axis / inverse_flattening) earth figures are
both honoured — the difference is several grid cells, not a rounding.
transverse_mercator shares one implementation with UTM, which is the same
projection with lon0, k0, and the false origin fixed by zone. A file that
omits scale_factor_at_central_meridian gets 1, not UTM's 0.9996 — the
latter is a property of the UTM system, and assuming it would shrink every
distance by 400 ppm.
Everything else CF defines is detected but not converted: scan() reports
crs: "unconverted" and a warning naming the projection, rather than passing
native axis values off as degrees. rotated_latitude_longitude matters most
here — its rlat/rlon axes are degrees in ordinary geographic range, so no
range check can flag them, and read as plain lat/lon they yield coordinates that
look reasonable and point at the wrong continent. It is identified by CF's
mandated standard_name of grid_latitude / grid_longitude.
The grid stays in its native units internally, because it is regular there and
only there: on a polar-stereographic grid longitude varies along every row, so
no 1-D lon[] can describe it. Conversion happens at the API edge —
scan().bboxreports WGS84, computed by walking the whole projected boundary (a projected domain's extremes are not always at its corners).extract({lat, lon})forward-projects the request, then reportslocation: { lat, lon, x, y, crs }— degrees for the cell found, plus the native pair it was read from.extractGrid({bbox})projects each output pixel individually. Pixels outside the domain areNaN, never the nearest edge cell.
If a grid is detected as projected but cannot be converted — no CRS variable, or
a grid_mapping_name this build cannot invert — axis values are passed through
in native units and the reason is reported rather than silently mislabelled.
| NetCDF3 | shapes[], units[], bbox |
| Zarr v2/v3 | shapes[], dtypes[], compressors[], bbox |
| Parquet | gridTypes[], bbox |
A GRIB2 message does not carry a name or a unit. It carries three integers —
discipline (Section 0), parameter category and parameter number (Section 4) —
and the meaning lives in WMO Code Table 4.2. HydroSlice ships that table
(formats/grib2/grib2_param_table.h, generated), so scan(), extract() and
extractGrid() all report a real name and real units. scan() also exposes the
discipline and centre a name depends on.
Numbers 192–254 are reserved by WMO for the originating centre and are not in Table 4.2 at all, so the centre's own table is shipped for the centres that define them: NCEP (7), ECMWF (98), DWD (78), Rome (80) and Helsinki (86). On a GFS file the local range is the majority of the parameters.
Units are normalised to CF-style spelling (kg m-2, not kg m**-2 or kg/m2),
because a single file draws on both tables and they disagree.
A parameter in neither table keeps an honest label rather than a guess:
{ name: 'Variable (discipline=0, cat=3, num=196)',
warnings: ["Not in WMO Code Table 4.2, nor in the local table for centre 7"] }Variables are identified by (discipline, category, number), not by (category, number): one file can carry the same pair under two disciplines meaning two different quantities. A GFS file carries disciplines 0, 2 and 10, and seven of its pairs occur under two of them.
To refresh the table (it changes rarely, and only additively):
npm run tables:grib2:check # report what would change, exit 1 if anything does
npm run tables:grib2 # regenerate lib/grib2/param-table.jsThe generator pins the exact upstream commit it read, and prints every added, removed, renamed or re-united parameter — so a refresh is a reviewable diff.
const meta = await scan(file);
meta.bbox; // [minLon, minLat, maxLon, maxLat] in degrees, or undefinedUse it as the bbox you pass to extractGrid, and as the
bounds you place the resulting image at. extractGrid requires a bbox and returns the
same one it was given, so those two must agree or the raster lands in the wrong place.
For projected grids (polar stereographic, Lambert) this is the envelope of the whole grid
rectangle, which is wider than the area holding valid data — that is intentional. The extra
cells come back as NaN, and the image is correctly georeferenced. NCEP Stage IV, for
example, reports [-134.04, 19.81, -59.96, 57.84]: its maximum latitude occurs in the
middle of the north edge, not at a corner, so a four-corner box would clip it by 4°.
Note Leaflet expects [[south, west], [north, east]] while bbox is longitude-first:
const [w, s, e, n] = meta.bbox;
L.imageOverlay(pngUrl, [[s, w], [n, e]]).addTo(map);bbox is undefined when the extent cannot be derived. Treat it as optional. Known cases:
| Case | Why |
|---|---|
| NetCDF3/4 without CF coordinates | No variable identifiable as latitude/longitude by name, axis, standard_name or units. |
| NetCDF3 whose axes are record variables | Record data is interleaved, so the axis is not contiguous and is not read. |
| GRIB2 template 101 (unstructured/ICON) | Cell coordinates live in an external grid file, not the GRIB2. |
| GRIB2 templates other than 0/20/30/40 | Coordinates are not built for them. |
| Zarr with synthetic axes | No real lat/lon coordinate arrays in the store. |
All formats derive bbox through one entry point (_geoBboxFor), which dispatches on the
detected format. Adding a format means adding one case, not another attach site.
Each variable in variables[] also carries format-specific fields:
// GRIB2 variable
{ index: 0, name: 'Total precipitation', units: 'kg m-2', supported: true,
discipline: 0, centre: 7, cat: 1, num: 8,
grid_template: 20, nx: 1121, ny: 881, messages: 120 }
// NetCDF3 / NetCDF4 variable
{ index: 0, name: 'precipitation', supported: true, long_name: '...', units: 'mm', shape: '1x721x1440', ndims: 3 }
// Zarr v2 variable
{ index: 0, name: 'temperature', supported: true, shape: [12, 721, 1440], chunks: [1, 721, 1440], dtype: '<f4', compressor: 'zlib' }If the file has a time axis, scan() decodes it into ISO-8601 strings using
the file's CF units and calendar attributes:
const meta = await scan(file);
// If every multi-dim variable shares the same time axis, it's surfaced at
// the top level:
meta.times;
// {
// values: ['2024-01-01T00:00:00Z', '2024-01-01T03:00:00Z', '2024-01-01T06:00:00Z', ...],
// unitsRaw: 'hours since 2024-01-01',
// calendar: 'standard',
// }
// Otherwise each variable carries its own:
meta.variables[0].times;GRIB2 accumulation products are stamped at the END of their accumulation
window. Templates whose name ends "in a time interval" (product template 4.8 —
accumulations, averages, extremes) carry an explicit end of overall time
interval, and that is the valid time reported. Their forecast-time field marks
the start of the window and is usually 0.
So NCEP Stage IV st4_conus.2026080412.24h.grb2 reports
2026-08-04T12:00:00Z — the end of its 24-hour window — matching how NCEP names
and publishes the file, not the 2026-08-03T12:00:00Z reference time. Hourly
Stage IV behaves the same way, one hour later than its reference time.
Instantaneous products (template 4.0) are unchanged: reference time plus forecast offset. Templates 4.9–4.14 are also interval products, but they place the interval-end field at different offsets and are not yet decoded, so they still report the reference time.
Each time axis also carries convenience start / end (first and last
timestamp), and scan() adds a file-level timeRange spanning the whole
file — the earliest start and latest end across every axis:
meta.timeRange; // { start: '2026-04-14T06:00:00Z', end: '2026-04-14T12:00:00Z' }
meta.variables[0].times.start; // first timestep of that variabletimeRange is present whenever the file has any time axis (even when times are
per-variable rather than hoisted to meta.times).
Supported calendars: standard (a.k.a. gregorian, proleptic_gregorian),
noleap (a.k.a. 365_day), and 360_day. Unsupported calendars degrade
gracefully — times is omitted and a warning is added to the affected
variable's warnings[].
Per-format support today:
| Format | Time decoding |
|---|---|
| GRIB2 | Per-message valid_time already exposed by the WASM engine, grouped by variable |
| Zarr v2 | Reads the time coord array + .zattrs (units, calendar); Float64 precision |
| NetCDF4 | Reads the time variable's HDF5 attrs via h5wasm |
| NetCDF3 | Currently extract-only — a wp_nc3_get_time_units_json C accessor is needed to surface times in scan(). Tracked as a follow-up. |
| TIFF | No time axis. DateTime tag (306) surfacing is a follow-up. |
Decode one or more variables. Returns a structured JS object.
| Option | Type | Notes |
|---|---|---|
variable |
string | string[] |
Name(s) from scan. Omit → all supported vars. |
lat, lon |
number |
Nearest-grid-point lookup. Omit for whole grid. |
t1, t2 |
number |
Time-index range (inclusive). Default: all. |
date |
string | number | Date |
Single timestep by date (nearest match). Mutually exclusive with t1/t2. |
dateRange |
[start, end] |
Timestep range by date (nearest start/end). Mutually exclusive with t1/t2. |
// Single variable, single point
const result = await extract(file, {
variable: '2t',
lat: 52.52, lon: 13.40, // Berlin
});
// { variable: '2t', location: { lat, lon }, value: 271.4, time: 1734566400 }
// Multiple variables — returns { variables: [...] }
const multi = await extract(file, {
variable: ['TMP', 'UGRD'],
lat: 40.7, lon: -74.0,
t1: 0, t2: 5,
});Instead of integer t1/t2 (extract) or time (extractGrid), pass a date —
an ISO-8601 string, a Date, or epoch milliseconds:
// nearest timestep to a date
await extract(file, { variable: 'TMP', date: '2026-04-14T12:00:00Z' });
// a date range → nearest start/end indices
await extract(file, { variable: 'TMP', dateRange: ['2026-04-14T06:00:00Z', '2026-04-14T18:00:00Z'] });
// single timestep for a grid
await extractGrid(file, { variable: 'TMP', bbox, width: 256, height: 256, date: '2026-04-14T12:00:00Z' });Matching is nearest against the file's decoded time axis. A dateRange
bound given as a date only (YYYY-MM-DD, no time) expands to the whole UTC
day — start → 00:00:00.000, end → 23:59:59.999 — and every timestep inside
the window is kept. So dateRange: ['1990-01-01', '1990-01-01'] selects all
timesteps on that day, no need to spell out the time. Mixing a date option with
an integer index for the same axis throws HydroSliceError. For
Zarr arrays without CF time metadata the synthetic axis is step t = t days
(t·86400 s), so a date is matched against that. Files with a single timestep
(or no time axis) resolve any date to index 0. trim remains index-only.
Same as extract, but returns a serialised result instead of an object —
a string for the text formats, a Uint8Array for the binary ones.
import { writeFileSync } from 'node:fs';
// CSV
const csv = await extractOutput(file, { variable: '2t', lat: 52.52, lon: 13.40 }, 'csv');
writeFileSync('berlin.csv', csv);
// variable,time,value,lat,lon
// 2t,1734566400,271.4,52.52,13.4
// JSON string (pretty-printed)
const json = await extractOutput(file, { variable: '2t' });
writeFileSync('result.json', json);
// A NetCDF-3 file of the same time series
const nc = await extractOutput(file, { variable: '2t', lat: 52.52, lon: 13.40 }, 'netcdf3');
writeFileSync('berlin.nc3', nc);A point query returns a time series, so the array formats write it as
<var>(time, lat=1, lon=1) with time carrying CF units
(seconds since 1970-01-01T00:00:00Z, calendar = standard) and
featureType = 'timeSeries'. It opens in xarray as a time series with a
decoded time index, not as a stack of images. Missing values are written as
IEEE NaN.
extract has two other result shapes — the whole cube (no lat/lon) and
multi-variable (variable: [...]). Neither is encodable yet; both throw
UnsupportedExportError naming 'json' and 'csv', which do handle them.
Resample a variable over a bounding box into a dense, north-up Float32
grid. This is the heavy-lifting pathway: it runs across parallel workers, is
abortable, and reports progress. Use it whenever you want an area rather than
a single point — heat maps, raster export, tiles.
| Option | Type | Notes |
|---|---|---|
variable |
string |
Required. Name from scan().variable_names. |
bbox |
[minLon, minLat, maxLon, maxLat] |
Required. Geographic bounds, degrees. |
width |
number |
Required. Output columns. |
height |
number |
Required. Output rows. |
time |
number |
Time-axis index. Default 0. |
date |
string | number | Date |
Single timestep by date (nearest match). Mutually exclusive with time. |
workers |
number |
Parallel workers. Default 5; 0 forces inline (single-threaded). |
signal |
AbortSignal |
Abort the run; rejects with an AbortError. |
onProgress |
({done, total}) => void |
Called as output cells are filled. |
import { extractGrid } from 'hydroslice';
const controller = new AbortController();
const grid = await extractGrid(file, {
variable: 'TMP',
bbox: [-100, 30, -80, 45], // [minLon, minLat, maxLon, maxLat]
width: 256, height: 256,
workers: 5,
signal: controller.signal,
onProgress: ({ done, total }) => console.log(`${done}/${total}`),
});{
data: Float32Array, // length width*height, row-major, row 0 = maxLat (north-up)
width: number,
height: number,
bbox: [number, number, number, number],
variable: string,
units: string | undefined,
time: number | string | undefined,
}Cells are row-major with row 0 at maxLat (north-up), so data[y*width + x]
is the value at output pixel (x, y). Missing / masked points are NaN — the
render helpers paint those transparent.
Same shape, every format. GRIB2, NetCDF3/4, Zarr, and TIFF/COG all return this identical
ExtractGridResult, so the render and output helpers below work the same regardless of the input format. For Zarr without CF coordinates, the grid is indexed against the synthetic axes (see Zarr v2 notes).
Same as extractGrid, but returns a file-ready result instead of the raw
grid object — handy for one-shot "give me a blob to save/serve" callers.
format |
Returns | Use for |
|---|---|---|
'json' |
string |
Grid + metadata as JSON (pretty opt) |
'csv' |
string |
One row per cell: variable,lat,lon,value |
'geotiff' |
Uint8Array |
A WGS84 Float32 GeoTIFF |
'netcdf3' |
Uint8Array |
CF-1.8 NetCDF-3 classic, <var>(lat, lon) |
'zarr' |
Uint8Array |
A Zarr v2 store packed in a ZIP |
'netcdf4' |
Uint8Array |
NetCDF-4 / HDF5 (see the dimension note below) |
'grib2' |
Uint8Array |
One GRIB2 message, template 3.0 + simple packing |
'parquet' |
Uint8Array |
A table: one row per cell (lat,lon,<var>) |
'png' |
Promise<Uint8Array> |
Colored PNG (pass ramp, vmin, vmax) |
'imagedata' |
{ width, height, data } |
RGBA for a <canvas> / MapLibre |
Aliases: tif/tiff → geotiff; nc/nc3/netcdf → netcdf3;
nc4/hdf5/h5 → netcdf4; grib/grb/grb2 → grib2; pq/parq → parquet.
// Save a GeoTIFF straight from a bbox query
const tiff = await extractGridOutput(file, {
variable: 'TMP', bbox: [-100, 30, -80, 45], width: 512, height: 512,
}, 'geotiff');
writeFileSync('tmp.tif', tiff);The encoder layer on its own, for callers who already hold a result and do not want to extract it again:
import { extractGrid, encodeGrid, EXPORT_FORMATS } from 'hydroslice';
const grid = await extractGrid(file, { variable: 'TMP', bbox, width: 512, height: 512 });
const nc = await encodeGrid(grid, 'netcdf3'); // Uint8Array
const zarr = await encodeGrid(grid, 'zarr'); // Uint8Array (a .zip)
const json = await encodeGrid(grid, 'json'); // stringEXPORT_FORMATS is the registry both functions validate against, and it is
what a format chooser should be built from — it says which formats can
represent which kind of result:
| id | grid | point/series | ext | returns |
|---|---|---|---|---|
json |
yes | yes | .json |
string |
csv |
yes | yes | .csv |
string |
geotiff |
yes | no | .tif |
Uint8Array |
netcdf3 |
yes | yes | .nc3 |
Uint8Array |
zarr |
yes | yes | .zip |
Uint8Array |
netcdf4 |
yes | yes | .nc |
Uint8Array |
grib2 |
yes | no | .grb2 |
Uint8Array |
parquet |
yes | yes | .parquet |
Uint8Array |
// Build a chooser that can never offer a writer that does not exist
EXPORT_FORMATS.filter(f => f.series).map(f => f.label);A format that cannot represent the result throws UnsupportedExportError
(extends HydroSliceError) and the message names the formats that can. GeoTIFF
and GRIB2 refuse a point series rather than writing a misleading 1×1 raster.
GRIB2 has no free-text variable name — a field is identified by the WMO triple (discipline, category, number). Pass it when you know it:
// Total precipitation
await encodeGrid(grid, 'grib2', { grib2: { discipline: 0, category: 1, number: 8 } });Omit it and the parameter is written as missing (255/255) rather than mislabelled as some other quantity: the values, grid and time stay exact, only the identity is absent. GRIB2 also writes one field per message, so a multi-band grid is rejected rather than silently truncated.
A grid becomes one row per cell (lat, lon, <variable>), a series one row
per timestep (time, lat, lon, <variable>), with time as INT64 epoch
seconds — the same column names this library's Parquet reader looks for, so
what you write, it reads.
Writing Parquet means emitting thrift-compact metadata and page headers, which
is delegated to hyparquet-writer rather than hand-rolled. It is an optional
dependency, lazily imported, exactly like hyparquet for reading and
h5wasm for NetCDF-4: a caller who never exports Parquet never loads it, and
one who does gets a message naming the install rather than a module-not-found
stack.
npm i hyparquet-writer # only if you export ParquetnetCDF-C names a variable's dimensions from the HDF5 attribute
DIMENSION_LIST, whose type is H5T_VLEN{H5T_REFERENCE}. h5wasm cannot write
that type, and the library takes no dependency that could. So netCDF-C and
xarray open these files and read every value and attribute correctly, but
report the dimensions as phony_dim_0, phony_dim_1, … rather than
lat/lon/time; the coordinates are present as variables but not attached as
axes.
Writing a non-VLEN DIMENSION_LIST instead is not an option — it segfaults
netCDF-C on open — so the attribute is deliberately absent. For a fully
conformant file with named dimensions, export netcdf3.
For the in-memory grid, prefer extractGrid; the render helpers
(gridToImageData / gridToPNG)
and gridToGeoTIFF are documented under Map rendering below.
Zarr v2 files must be zipped (.zarr.zip or .zip) — directory-format Zarr is not supported.
The library reads the ZIP, finds all .zarray metadata entries, and treats each top-level array as a variable.
Supported compressors:
- Built-in (no extra dependency, via the platform
DecompressionStream):null(no compression),gzip,zlib. - Lazy-loaded via
numcodecs(fetched from npm in Node / jsdelivr in the browser on first use):blosc,zstd,lz4.
Any other compressor id throws a clear error. Filters other than shuffle (e.g. fixedscaleoffset, delta) are not yet supported.
Synthetic axes: Zarr v2 arrays don't carry CF coordinate metadata, so the library assigns synthetic axes for the query engine:
| Axis | Synthetic value |
|---|---|
| lat | lats[j] = j |
| lon | lons[i] = i |
| time | times[t] = t × 86400 |
This means lat: 0, lon: 0 lands on grid point (0, 0), and t1: 0, t2: 5 returns the first
six time steps. Real geographic queries require external coordinate data (planned for a future sprint).
import { scan, extract } from 'hydroslice';
import { readFileSync } from 'node:fs';
const file = new Uint8Array(readFileSync('data.zarr.zip'));
const meta = await scan(file);
// meta.format === 'zarr'
// meta.variables[0] → { name: 'temperature', shape: [12,721,1440], dtype: '<f4', compressor: 'zlib', supported: true }
const result = await extract(file, {
variable: 'temperature',
lat: 0, lon: 0, // synthetic: grid point (j=0, i=0)
t1: 0, t2: 11, // all 12 time steps
});Produce a smaller file in the same format as the input, containing only
the selected variables (and optionally a time-axis or spatial bbox slice). The
result is a Uint8Array plus a stats/warnings envelope.
Per-format strategy:
| Format | How it's trimmed | Decode? |
|---|---|---|
| GRIB2 | Filter messages by variable + valid time; concat | No |
| NetCDF3 | Rewrite header with kept vars; copy data spans | No |
| Zarr (zip) | Filter zip entries by var + chunk; re-zip | No |
| NetCDF4 | Open with h5wasm; copy selected datasets to new file | Partial (HDF5 re-frames B-trees) |
| TIFF | Copy or re-encode kept bands/blocks; update IFD tags | Partial |
NetCDF4 trim returns the input unchanged when the selection drops nothing —
every dataset in the file is kept and there is no t1/t2 or bbox. A rewrite
in that case could only lose fidelity: this path builds a fresh HDF5 file, so it
cannot reapply shuffle, drops the object-reference dimension-scale attributes,
and does not copy datasets inside groups. warnings says when the short-circuit
was taken. Note the test is against the datasets actually in the file, not
against scan()'s variable list — scan() omits variables it cannot decode, so
selecting every name it reports may still be a strict subset of the file.
When the file is rewritten, any group it contains is not copied (this version
walks top-level datasets only) and warnings now names the groups affected.
NetCDF4 trim preserves each dataset's compression: a variable the source stored
with deflate is written back with deflate at the same level, and one stored
uncompressed stays uncompressed. This matters for stats.outputSize — an
uncompressed rewrite of a compressed file can be larger than the input even
after dropping variables, because it is comparing raw bytes against packed ones.
h5wasm can write only the gzip filter, so HDF5's shuffle pre-filter is not
reapplied. Whether that costs or saves bytes depends on the data — shuffle helps
high-entropy float fields but hurts fields with many exactly-repeated values —
so a trim that keeps every variable will still not reproduce the input size.
On the idalia NLDAS-2 fixture plain deflate beats shuffle+deflate on 3 of 4
variables, and keeping all of them yields 86% of the source. warnings names the
affected datasets so the difference is explainable.
Zarr trim accepts both stored and DEFLATE-compressed .zip entries; the
trimmed output is itself a valid Zarr zip that scan/extract can read back.
Data chunks are passed through verbatim (no re-encode).
When trim() slices a Zarr store along time or a bbox, the matching 1-D
coordinate arrays (time/lat/lon) are decoded and re-sliced to the same
extent so the output's axes stay consistent and re-read correctly. Limitations:
coordinate arrays that are multi-chunk or zarr-compressed are kept at full
length (a warning is emitted); byte-cut slicing widens to chunk boundaries, so a
store whose spatial chunks span the whole dimension will not shrink along
lat/lon.
import { trim } from 'hydroslice';
const result = await trim(file, {
variables: ['2t', 'sp'], // names from scan().variable_names
t1: 0, t2: 23, // optional inclusive time-axis range
bbox: [-100, 30, -80, 45], // optional [minLon, minLat, maxLon, maxLat]
});
console.log(result.format); // 'grib2' | 'netcdf3' | 'netcdf4' | 'zarr' | 'tiff'
console.log(result.stats); // { inputSize, outputSize, variablesKept, variablesDropped }
console.log(result.warnings); // human-readable notes (e.g. Zarr boundary widening)
fs.writeFileSync('trim.grb2', result.bytes);| Option | Type | Notes |
|---|---|---|
variables |
string[] |
Required. Variable names to keep. Same naming as scan(). |
t1 |
number |
Inclusive lower time-axis index. Defaults to 0. |
t2 |
number |
Inclusive upper time-axis index. Defaults to the last one. |
bbox |
[number, number, number, number] |
Optional WGS84 spatial clip as [minLon, minLat, maxLon, maxLat]. |
t1/t2 semantics match extract() — they're indices into the variable's
time axis in the order scan() reports.
{
bytes: Uint8Array, // the trimmed file
format: 'grib2' | 'netcdf3' | 'netcdf4' | 'zarr' | 'tiff',
warnings: string[], // see below
stats: {
inputSize: number,
outputSize: number,
variablesKept: number,
variablesDropped: number,
},
}Zarr chunks are atomic — the whole chunk is either present or absent. If
the requested [t1, t2] range crosses chunk boundaries, the trim widens
to keep every chunk that touches the range. The actual time range that
ends up in the output is reported in warnings:
Zarr variable 'temperature': time range [3,7] widened to [0,7]
because chunk size along time axis is 8
GRIB2 (one timestep per message), NetCDF3 (records addressable individually), and NetCDF4 (h5wasm hyperslab) all give exact ranges.
| Thrown | When |
|---|---|
TrimError |
Invalid opts, out-of-range t1, format-specific failure |
VariableNotFoundError |
A requested variable isn't in the source |
UnsupportedFormatError |
The source isn't one of the supported formats |
- Spatial bbox is supported for Zarr, NetCDF4, and TIFF. GRIB2 and NetCDF3 bbox trimming are still tracked for a follow-up sprint.
- NetCDF4 writes via h5wasm into the WASM heap, so the practical output cap is ~1–2 GB.
- NetCDF4 v1 walks top-level datasets only — datasets nested inside HDF5 groups are not copied. Most NetCDF4 files in the wild use the root group.
- NetCDF4 dim-coord matching in
extract()(pre-existing) uses dim length; if a sliced time axis ends up with the same length as another coordinate (e.g.lat), the existing extract heuristic may mis-assign dims. The trimmed bytes are correct — verify with a direct h5wasm read.
scan, extract, and extractGrid all accept TIFF and GeoTIFF files.
| Aspect | What's covered |
|---|---|
| Byte order | Little-endian (II*\x00), big-endian (MM\x00*), and BigTIFF (magic === 43, 64-bit offsets) |
| Sample types | uint8, uint16, int16, float32 (chunky PlanarConfiguration=1 AND separate planes =2) |
| Compression | None, Deflate, LZW, PackBits, JPEG (jpeg-js optional dep), WebP (browser only) |
| Predictors | 1 (none), 2 (horizontal), 3 (floating-point — float32 only) |
| Layout | Strip and tile; extractGrid caches decoded blocks + nearest-neighbour resamples to the requested output size |
| CRS | Geographic EPSG:4326, UTM north/south, Sinusoidal (MODIS), Lambert Conformal Conic (NOAA HRRR/RAP/NAM), Polar Stereographic (NSIDC 3413/3031), Albers Equal Area (USDA NASS/USGS) |
| Multi-band | SamplesPerPixel ≥ 1; band names taken from GDAL_METADATA <Item name="DESCRIPTION" sample="N">…</Item> (fallback: band_1, band_2, …) |
| COG | Overview IFDs surfaced in scan().overviews; extractGrid auto-selects the smallest overview that meets the requested output size |
| COG over URL | scan and extract issue HTTP Range requests for the IFD + only the needed tile/strip — the whole file is never downloaded |
| trim() | Band selection + spatial bbox (snaps to block grid with a widening warning); PlanarConfiguration=2 trim is byte-copy (no decode) |
| Writer | gridToGeoTIFF(grid, opts) — multi-band, dtype (float32/uint8/uint16/int16), compression (none/deflate), predictor (1/2/3), CRS (any supported kind) |
const meta = await scan(geoTiffBuf);
meta.variable_names; // ['B04_red', 'B03_green', 'B02_blue'] (from GDAL_METADATA)
// or ['band_1', 'band_2', 'band_3'] if no GDAL_METADATA tag is present
await extract(geoTiffBuf, { variable: 'B04_red', lat: 23.5, lon: 10.5 });const meta = await scan('https://example.com/sentinel.tif');
// Server sees: 1 small Range request for the magic bytes (16 B) + 1 Range
// request for the IFD prefix (a few KB) — not a full-file GET.
const point = await extract('https://example.com/sentinel.tif',
{ variable: 'band_1', lat: 35.0, lon: -95.0 });
// One additional Range request for the tile/strip containing that pixel.The Range source falls back to a full-body GET if the server responds 200 to Range: (no Range support).
import { UnsupportedCRSError } from 'hydroslice';
try { await scan(polarStereoTiff); }
catch (e) {
if (e instanceof UnsupportedCRSError) {
console.log(e.epsg, e.crsName); // e.g. 3413, 'unknown projected'
}
}- Additional compression: JPEG 2000 (libopenjp2 in WASM, follow-up sprint)
- COG overview tile pyramid auto-build in
gridToGeoTIFF(today: single-IFD writer) - Cross-format
trim()bbox for GRIB2 + NetCDF3 (needs C-side accessor + WASM rebuild) - TIFF
DateTimetag (306) surfaced asmeta.times(single-snapshot timestamp) - Tiled GeoTIFF writer (today: single-strip)
- GRIB2 pre-defined / previously-defined Section-6 bitmaps (indicator 1–254); only an
included bitmap (indicator 0) and "no bitmap" (255) decode today. Fields with a
bitmap return masked grid points as
NaN.
Turn a Float32 grid from extractGrid into something a web map can draw: a
colored RGBA buffer or a PNG. Both helpers are zero-dependency and work in
Node and the browser. They are format-agnostic — every reader produces the same
ExtractGridResult, so the same render path covers GRIB2, NetCDF3/4, Zarr, and
TIFF/COG.
Built-in ramps: viridis, plasma, grayscale, and RdBu (diverging, for
anomalies / temperatures). Pass a built-in name or a custom array of
[t, [r, g, b]] stops (t ∈ [0, 1], RGB are 0..255). Interpolation is linear
in RGB.
import { RAMPS, resolveRamp, sampleRamp } from 'hydroslice';
sampleRamp(resolveRamp('viridis'), 0.5); // → [38, 130, 142]
const custom = [[0, [0, 0, 0]], [1, [255, 0, 0]]]; // black → red
sampleRamp(custom, 0.5); // → [128, 0, 0]Float32 grid → RGBA bytes ready for a <canvas> or a MapLibre ImageSource.
gridToImageData(grid, {
ramp: 'viridis', // built-in name or custom Ramp array (default 'viridis')
vmin, vmax, // optional — defaults to the grid's finite min/max
nodataColor: [0, 0, 0, 0],// RGBA for NaN cells (default: transparent)
});
// → { width, height, data: Uint8ClampedArray }The value range is auto-computed from the finite values (NaN/Infinity ignored).
NaN cells are painted with nodataColor. In a browser, wrap the result:
new ImageData(data, width, height).
Same options as gridToImageData, but returns a PNG (Promise<Uint8Array>) —
8-bit RGBA, no interlace. Uses node:zlib in Node and CompressionStream in
the browser, with no extra dependency.
import { extractGrid, gridToPNG } from 'hydroslice';
import { writeFileSync } from 'node:fs';
const grid = await extractGrid(file, { variable: '2t', bbox, width: 512, height: 512 });
writeFileSync('temp.png', await gridToPNG(grid, { ramp: 'RdBu' }));extractGridOutput(source, options, format) also accepts 'imagedata' and
'png' in addition to 'json' and 'geotiff'.
import { extractGrid, gridToImageData } from 'hydroslice';
const grid = await extractGrid(file, { variable, bbox, width: 1024, height: 1024 });
const img = gridToImageData(grid, { ramp: 'viridis' });
const canvas = document.createElement('canvas');
canvas.width = img.width; canvas.height = img.height;
canvas.getContext('2d').putImageData(new ImageData(img.data, img.width, img.height), 0, 0);
map.addSource('data', {
type: 'image',
url: canvas.toDataURL('image/png'),
coordinates: [[bbox[0], bbox[3]], [bbox[2], bbox[3]], [bbox[2], bbox[1]], [bbox[0], bbox[1]]],
});
map.addLayer({ id: 'data', type: 'raster', source: 'data', paint: { 'raster-opacity': 0.75 } });A complete drop-a-file demo (basemap, variable picker, click-to-query, Web
Worker offload) lives at examples/map-demo.html.
Run it with npm run demo:web.
For cases where you need to reuse a single loaded file across multiple queries:
import { HydroSlice } from 'hydroslice';
const parser = new HydroSlice();
await parser.read(fileBytes); // load once
const vars = parser.getvariables(); // same as scan().variables
const data = await parser.extract({ // same options as extract()
variable: '2t', lat: 52.52, lon: 13.40,
});
parser.close(); // always free WASM memory when doneAll errors extend HydroSliceError:
import {
HydroSliceError,
UnsupportedFormatError,
VariableNotFoundError,
SourceError,
ExtractError,
} from 'hydroslice';
try {
await scan(unknownBytes);
} catch (e) {
if (e instanceof UnsupportedFormatError) { /* format not recognised */ }
else if (e instanceof SourceError) { /* fetch / read failed */ }
else if (e instanceof VariableNotFoundError) { /* bad variable name */ }
else throw e;
}| Error | When thrown |
|---|---|
UnsupportedFormatError |
Magic bytes don't match any supported format |
UnsupportedCRSError |
TIFF/GeoTIFF uses a CRS outside the v1 set (epsg, crsName fields surfaced) |
VariableNotFoundError |
Named variable absent or not supported: true |
SourceError |
URL fetch failed, unsupported source type |
ExtractError |
Decoder failed for a variable the library knows about |
HydroSliceError |
Base class — catches anything thrown by this library |
The library ships as ESM and works in Node 18+ out of the box — no extra setup needed.
import { readFileSync } from 'node:fs';
import { scan, extract } from 'hydroslice';
const file = new Uint8Array(readFileSync('forecast.grb2'));
const meta = await scan(file);
console.log(meta.format, meta.variable_names);
const result = await extract(file, { variable: meta.variables[0].name, lat: 52.52, lon: 13.40 });
console.log(result);Run the included demos:
npm run demo:grib2
npm run demo:netcdf3
npm run demo:netcdf4Import via a bundler (Vite, webpack, etc.) or use an import map for bare module names.
<!-- Import map needed for browser bare-module resolution -->
<script type="importmap">
{
"imports": {
"hydroslice": "/node_modules/hydroslice/index.js",
"h5wasm": "/node_modules/h5wasm/dist/esm/hdf5_hl.js"
}
}
</script>
<script type="module">
import { scan } from 'hydroslice';
document.querySelector('#file').addEventListener('change', async (e) => {
const meta = await scan(e.target.files[0]);
console.log(meta);
});
</script>With a bundler (recommended for production), the import map is not needed — just:
import { scan } from 'hydroslice';