astrojs / fitsjs

JavaScript library for reading the FITS astronomical format. Functionality includes reading of images, data cubes, compressed images, binary and ascii tables.
http://astrojs.github.io/fitsjs/
MIT License
85 stars 21 forks source link

Add support for 64bit Float #23

Open Samreay opened 9 years ago

Samreay commented 9 years ago

At the moment, all 64bit floating point data gets converted to 32bit. The Image.prototype._getFrame function should have the bitpix<0 if condition updated to differentiate between values of -64 and -32.

Specifically, modifying image.coffee such that from line 88 the code reads:

      arr = new Uint32Array(buffer)

      swapEndian = (value) ->
        return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value >> 8) & 0xFF00) | ((value >> 24) & 0xFF)

      while i--
        value = arr[i]
        arr[i] = swapEndian(value)

      if bitpix == -64
        i = arr.length
        while i-=2
          tmp = arr[i]
          arr[i] = arr[i + 1]
          arr[i + 1] = tmp
        arr = new Float64Array(buffer)
      else       
        # Initialize a Float32 array using the same buffer
        arr = new Float32Array(buffer)
kapadia commented 9 years ago

@Samreay It's been a while since I've thought about it, so my memory is a little fuzzy on this issue. I recall there being a limitation. I once tried to support 64 bit integers, but since javascript doesn't natively support that data type, it would require representing a Int64 array using 2 Uint32Arrays, along with some wacky endian flips.

As you point out, Float64 support should be easier. Do you mind submitting a PR? Adding a Float64 test would also be appreciated.

Samreay commented 9 years ago

Yeah, Javascript can only represent ints up to 53 bits as it stores everything as floats.

I'll figure out how to make a pull request, a test, and submit it. Cheers.

kapadia commented 9 years ago

Very true, but I believe the typed arrays are an exception to the 53 bit limitation.

Thanks!

Samreay commented 9 years ago

I've been having some major problems building the code (coffeescript on windows is apparently temperamental) and I lack the knowledge of node and coffeescript to be able to figure out what is actually the problem.

I've thrown what I had as a patch into a gisthub (https://gist.github.com/Samreay/f9c519dc9bebd8e6f1cf), so hopefully it comes in use if you have time to add it in properly at a later date.

kapadia commented 9 years ago

@Samreay Thanks for the gist. I should be able to incorporate it.