Skip to content

Instantly share code, notes, and snippets.

@calvin-brizzi
Created August 17, 2016 14:53
Show Gist options
  • Save calvin-brizzi/973e16c9aa7f31d3b79d338fa5ee13da to your computer and use it in GitHub Desktop.
Save calvin-brizzi/973e16c9aa7f31d3b79d338fa5ee13da to your computer and use it in GitHub Desktop.
Convert FITS data cube to JSON
# Requires astropy ("pip install astropy")
# Creates HUGE files 42MB > 430MB in my test
from astropy.io import fits
import json
# Change to your fits file
hdulist = fits.open("in.fits")
# My fits file only had one data cube in it
scidata = hdulist[0].data
z, x, y = scidata.shape
mn = scidata.min()
mx = scidata.max()
scidata -= mn
scidata /= (mx - mn)
datal = scidata.tolist()
exdata = {"x": x, "y":y, "z":z, "data": datal}
with open ('data.json', 'w') as fp:
json.dump(exdata, fp, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment