Amit Kapadia

Citizen Science developer for the Zooniverse. Usually found in a planetarium or climbing a crag.

fitsjs 0.5.0

26 Jul 2013

The wildly popular astronomical data format – FITS – now has an upgraded javascript library!

fitsjs has been refactored to handle more real-world use cases. Originally the library was developed to read images for only Zooniverse use cases. This meant reading small SDSS and HST cutouts of around 500 pixels. Most users of FITS (pretty much just astronomers and a few folks at the Vatican Library) handle much larger files. With this refactoring, fitsjs handles files of around 1 gigabyte or more in size.

With the update, fitsjs has improved read speeds utilizing multi-threading (via Web Workers), and has a more javascript-esque API, heavily relying on callback functions for time intensive operations.

Find fitsjs and documentation on GitHub.

The library is easy to use, almost like PyFITS, but with callback functions. Here’s a snippet:

// Define a callback function for when the file is read
function readCallback(f) {
  
  // Get the first header
  var header1 = f.getHeader();  // or f.getHeader(0)
  
  // Get the second header if the file has multiple header-dataunits
  var header2 = f.getHeader(1);
  
  // Print a keyword from header
  console.log(header.get('BITPIX'));
  
  // Get the first dataunit
  var dataunit1 = f.getDataUnit();
  
  // Or how about the second dataunit
  var dataunit2 = f.getDataUnit(1);
  
  // Assuming this is an image, let's get the pixels (it's a asynchronous process)
  dataunit1.getFrame(0, function(arr) {
    console.log(arr);
  });
}

// Initialize
var f = new astro.FITS('/path/to/fits/file.fits', readCallback);