Scientific raster format

Read FITS images and data cubes in JavaScript

The Flexible Image Transport System, or FITS, stores a sequence of Header/Data Units. A FITS file may contain a primary array, IMAGE extensions, tables, or other extensions. PureJsImage exposes the HDU document first so an application can choose a FITS image or FITS data cube explicitly.

Import

import { openFits, renderScientificPlane } from 'purejsimage/scientific'

Minimal Node.js example

import { FileSource } from 'purejsimage'
import { openFits, renderScientificPlane } from 'purejsimage/scientific'

const document = await openFits(await FileSource.open('observation.fits'))
console.log(document.hdus)
const dataset = await document.openImage(0)
const display = await renderScientificPlane(dataset, {
  plane: { z: 0, c: 0, t: 0 },
  range: { mode: 'percentile', low: 1, high: 99 },
  palette: 'magma',
})
for await (const pixels of display.pixels) console.log(pixels.data)

Minimal browser example

import { openFits } from 'purejsimage/scientific'

const input = document.querySelector<HTMLInputElement>('#fits')
const file = input?.files?.[0]
if (!file) throw new Error('Choose a FITS file')
const document = await openFits(file, { maxInputBytes: file.size })
const imageHdu = document.hdus.find((hdu) => hdu.canOpenRaster)
if (!imageHdu) throw new Error('No supported FITS image array')
const dataset = await document.openImage(imageHdu.index)
for await (const block of dataset.readPlane({
  z: 2, c: 0, t: 0, x: 100, y: 100, width: 256, height: 256,
})) console.log(block.data)

Supported features

FeatureSupport
HDU inspectionPrimary and extensions
Image arraysPrimary arrays and XTENSION='IMAGE'
AxesNAXIS 1, 2, and 3
BITPIX8, 16, 32, -32, -64
Physical valuesBSCALE, BZERO, integer BLANK
HeadersTyped cards, repeated COMMENT and HISTORY, unknown cards

Dataset mapping

NAXIS1 maps to X, NAXIS2 maps to Y, and NAXIS3 maps to Z. A one-dimensional array has height 1. C and T are 1. Identity scaling preserves the stored sample type. Standard unsigned 16-bit and 32-bit BZERO conventions use uint16 and uint32 output. Other scaling and integer BLANK conversion use float64 physical values.

Bounded reads

openFits() reads aligned 2880-byte header blocks for every HDU, then leaves image data lazy. A selected Z plane uses its direct array offset. A spatial ROI reads the selected contiguous row ranges. Blob, file, memory, HTTP range, and custom ImageSource inputs use the same reader.

Display rendering

renderScientificPlane() maps one selected quantitative plane into display RGB. Percentile calculation is approximate when bounded sampling is used. Display transfers are applied after mapping the selected range to 0 through 1. They do not compute a logarithm, square root, or asinh of the original measurement before range selection.

Unsupported features

ASCII tables, binary tables, random groups, tile-compressed images, Rice compression, gzip wrappers, FITS writing, celestial WCS projection math, SIP distortion, spectral-coordinate interpretation, NAXIS greater than 3, and BITPIX=64 raster samples are not supported. Unsupported HDUs remain inspectable but cannot be opened as rasters.

Official specification

NASA/GSFC FITS Standard · FITS Primer · Official FITS samples

Scientific Raster Explorer · ENVI guide · GSF guide · API · TIFF and OME-TIFF · Whole-slide viewer