Skip to content

Instantly share code, notes, and snippets.

@kbarbary
Created May 25, 2014 20:08
Show Gist options
  • Save kbarbary/c7eaa69aa1d7c38b81de to your computer and use it in GitHub Desktop.
Save kbarbary/c7eaa69aa1d7c38b81de to your computer and use it in GitHub Desktop.
FITSIO HDU types demo
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"language": "Julia",
"name": "",
"signature": "sha256:cecac9bd2bf10399ce57d7b5689a4b9bc13dc79ad4193687854aedc6babd9661"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"using FITSIO"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Writing an image to a file"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Open the file for write\n",
"f = FITS(\"test.fits\", \"w\"); # \"r\" (read-only), \"r+\" (read-write), \"w\" (clobber)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"file: test.fits\n",
"mode: w\n",
"extnum exttype extname\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# suppose we have some image data\n",
"data = rand(5,3);"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Write the image to the fits file (creates a new image extension)\n",
"write(f, data)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# extension has been added\n",
"f"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"file: test.fits\n",
"mode: w\n",
"extnum exttype extname\n",
"1 image_hdu \n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"close(f)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Reading from a file"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Open the file in read-only mode\n",
"f = FITS(\"test.fits\", \"r\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"file: test.fits\n",
"mode: r\n",
"extnum exttype extname\n",
"1 image_hdu \n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"length(f)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 19,
"text": [
"1"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"file: test.fits\n",
"extension: 1\n",
"type: IMAGE\n",
"image info:\n",
" bitpix: -64\n",
" size: (5,3)"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# read the data\n",
"data = read(f[1])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"5x3 Array{Float64,2}:\n",
" 0.325731 0.630665 0.113141\n",
" 0.904424 0.277356 0.75392 \n",
" 0.310158 0.871586 0.999169\n",
" 0.862666 0.719114 0.750297\n",
" 0.321728 0.868971 0.869869"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Alternative read syntax\n",
"f[1][:, :]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"5x3 Array{Float64,2}:\n",
" 0.325731 0.630665 0.113141\n",
" 0.904424 0.277356 0.75392 \n",
" 0.310158 0.871586 0.999169\n",
" 0.862666 0.719114 0.750297\n",
" 0.321728 0.868971 0.869869"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Read an image subset\n",
"f[1][2:3, 2:end]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 23,
"text": [
"2x2 Array{Float64,2}:\n",
" 0.277356 0.75392 \n",
" 0.871586 0.999169"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# You can get an HDU object to simplify notation\n",
"hdu = f[1]\n",
"hdu[2:3, 2:end]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"2x2 Array{Float64,2}:\n",
" 0.277356 0.75392 \n",
" 0.871586 0.999169"
]
}
],
"prompt_number": 24
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Access after closing is \"safe\" (no segfault)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"close(f)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "ErrorException",
"evalue": "attempt to access closed FITS file",
"output_type": "pyerr",
"traceback": [
"attempt to access closed FITS file",
" in fits_assert_open at /home/kyle/.julia/v0.3/FITSIO/src/FITSIO.jl:70"
]
}
],
"prompt_number": 26
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment