Skip to content

Instantly share code, notes, and snippets.

@rirze
Created June 9, 2016 17:50
Show Gist options
  • Save rirze/ae53416244861f10632bab2df87194b2 to your computer and use it in GitHub Desktop.
Save rirze/ae53416244861f10632bab2df87194b2 to your computer and use it in GitHub Desktop.
PyFits vs FitsIO
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import fitsio\n",
"from astropy.io import fits as pyfits\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"files = ['iris.fits', 'new3d.fits', 'new4d.fits', 'new5d.fits']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def read_test_pyfits(filename):\n",
" with pyfits.open(filename) as hdul:\n",
" data = hdul[0].data\n",
" c = data.copy()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def read_test_fitsio(filename):\n",
" with fitsio.FITS(filename) as f:\n",
" data = f[0].read()\n",
" c = data.copy()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def read_test_fitsio_buffered(filename, n=1000):\n",
" data =[]\n",
" fitsio.read_header(files[0], ext=1)\n",
" with fitsio.FITS(filename, iter_row_buffer=n) as f:\n",
" for hdu in f:\n",
" data.append(hdu.read())\n",
" return data\n",
" #c = np.vstack(data).copy()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.14 s, sys: 1.81 s, total: 3.96 s\n",
"Wall time: 5.13 s\n"
]
},
{
"data": {
"text/plain": [
"[None, None, None, None]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time [read_test_fitsio(f) for f in files]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.36 s, sys: 1.83 s, total: 4.2 s\n",
"Wall time: 7.78 s\n"
]
},
{
"data": {
"text/plain": [
"[None, None, None, None]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time [read_test_pyfits(f) for f in files] "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.98 s, sys: 1.6 s, total: 3.58 s\n",
"Wall time: 4.48 s\n",
"CPU times: user 4 ms, sys: 0 ns, total: 4 ms\n",
"Wall time: 227 ms\n",
"CPU times: user 48 ms, sys: 56 ms, total: 104 ms\n",
"Wall time: 674 ms\n",
"CPU times: user 144 ms, sys: 172 ms, total: 316 ms\n",
"Wall time: 756 ms\n"
]
}
],
"source": [
"for f in files:\n",
" %time read_test_fitsio(f)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.24 s, sys: 1.56 s, total: 3.8 s\n",
"Wall time: 4.54 s\n",
"CPU times: user 4 ms, sys: 4 ms, total: 8 ms\n",
"Wall time: 59.7 ms\n",
"CPU times: user 28 ms, sys: 36 ms, total: 64 ms\n",
"Wall time: 422 ms\n",
"CPU times: user 84 ms, sys: 88 ms, total: 172 ms\n",
"Wall time: 329 ms\n"
]
}
],
"source": [
"for f in files:\n",
" %time read_test_pyfits(f)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"firis = pyfits.open(files[0])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"finfo, hdu, tab = firis"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[-200., -200., -200., ..., -200., -200., -200.],\n",
" [-200., -200., -200., ..., -200., -200., -200.],\n",
" [-200., -200., -200., ..., -200., -200., -200.],\n",
" ..., \n",
" [-200., -200., -200., ..., -200., -200., -200.],\n",
" [-200., -200., -200., ..., -200., -200., -200.],\n",
" [-200., -200., -200., ..., -200., -200., -200.]], dtype=float32)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"finfo.section[199]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6.32100000e+01, -6.94129944e+01, 2.66883105e-01,\n",
" 2.99996529e+01, 5.13198437e+02, 5.09000000e+02,\n",
" 1.00000000e+00, 1.00000000e+00, 1.50999999e+00,\n",
" 4.00000000e+00, -2.43545162e+01, 3.19467302e+02,\n",
" 5.78302185e+02, 1.34987861e-01, 9.99936581e-01,\n",
" 1.12623181e-02, -1.12623181e-02, 9.99936581e-01,\n",
" 0.00000000e+00, 0.00000000e+00, 4.43127251e+01,\n",
" 2.56499996e+01, 1.90163631e+01, 2.22063637e+01,\n",
" 1.90263634e+01, 2.14627266e+01, 2.49172726e+01,\n",
" 6.66636324e+00, -1.57600002e+01, -1.41199999e+01,\n",
" 5.10000000e+01])"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hdu.section[1]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(firis[0].data)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"SIMPLE = T / Written by IDL: Thu Oct 22 01:49:21 2015 \n",
"BITPIX = 16 / Number of bits per data pixel \n",
"NAXIS = 3 / Number of data axes \n",
"NAXIS1 = 1850 / \n",
"NAXIS2 = 1092 / \n",
"NAXIS3 = 200 / \n",
"EXTEND = T / FITS data may contain extensions \n",
"DATE = '2015-10-22' / Creation UTC (CCCC-MM-DD) date of FITS header \n",
"CDELT1 = 0.166350 / \n",
"CDELT2 = 0.166350 / \n",
"CDELT3 = 63.1250 / \n",
"CRPIX1 = 925.500 / \n",
"CRPIX2 = 546.500 / \n",
"CRPIX3 = 100.000 / \n",
"CRVAL1 = -24.3494 / \n",
"CRVAL2 = 319.476 / \n",
"CRVAL3 = 6249.52 / \n",
"CTYPE1 = 'HPLN-TAN' / \n",
"CTYPE2 = 'HPLT-TAN' / \n",
"CTYPE3 = 'Time ' / \n",
"CUNIT1 = 'arcsec ' / \n",
"CUNIT2 = 'arcsec ' / \n",
"CUNIT3 = 'seconds ' / \n",
"COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy \n",
"COMMENT and Astrophysics', volume 376, page 359; bibcode 2001A&A...376..359H \n",
"TELESCOP= 'IRIS ' / \n",
"INSTRUME= 'SJI ' / \n",
"DATA_LEV= 2.00000 / \n",
"LVL_NUM = 2.00000 / \n",
"VER_RF2 = 'L12-2015-08-14' / \n",
"DATE_RF2= '2015-10-22T07:15:15.766' / \n",
"DATA_SRC= 1.51000 / \n",
"ORIGIN = 'SDO ' / \n",
"BLD_VERS= 'V8R10X ' / \n",
"LUTID = 4.00000 / \n",
"OBSID = '3620263378' / \n",
"OBS_DESC= 'Very large dense 400-step raster 131.7x175 400s C II Mg II h/k De'\n",
"OBSLABEL= ' ' / \n",
"OBSTITLE= ' ' / \n",
"DATE_OBS= '2015-10-09T04:14:33.710' / \n",
"DATE_END= '2015-10-09T07:43:55.580' / \n",
"STARTOBS= '2015-10-09T04:14:33.610' / \n",
"ENDOBS = '2015-10-09T07:44:58.778' / \n",
"OBSREP = 1 / \n",
"CAMERA = 2 / \n",
"STATUS = 'Quicklook' / \n",
"BTYPE = 'Intensity' / \n",
"BUNIT = 'Corrected DN' / \n",
"BSCALE = 0.25 / \n",
"BZERO = 7992 / \n",
"HLZ = 0 / \n",
"SAA = ' 0' / \n",
"SAT_ROT = -6.77902E-05 / \n",
"AECNOBS = 0 / \n",
"AECNRAS = 0 / \n",
"DSUN_OBS= 1.49452E+11 / \n",
"IAECEVFL= 'NO ' / \n",
"IAECFLAG= 'NO ' / \n",
"IAECFLFL= 'YES ' / \n",
"TR_MODE = ' ' / \n",
"FOVY = 181.654 / \n",
"FOVX = 307.748 / \n",
"XCEN = -24.3494 / \n",
"YCEN = 319.476 / \n",
"SUMSPTRL= 1 / \n",
"SUMSPAT = 1 / \n",
"EXPTIME = 29.9997 / \n",
"EXPMIN = 29.9996 / \n",
"EXPMAX = 29.9997 / \n",
"DATAMEAN= 36.5037 / \n",
"DATARMS = 28.6593 / \n",
"DATAMEDN= 29.0919 / \n",
"DATAMIN = -2726.66 / \n",
"DATAMAX = 26851.0 / \n",
"DATAVALS= 203736243 / \n",
"MISSVALS= 200303757 / \n",
"NSATPIX = 0 / \n",
"NSPIKES = 0 / \n",
"TOTVALS = 404040000 / \n",
"PERCENTD= 50.4248 / \n",
"DATASKEW= 10.1806 / \n",
"DATAKURT= 502.324 / \n",
"DATAP01 = 10.5103 / \n",
"DATAP10 = 15.7991 / \n",
"DATAP25 = 20.7684 / \n",
"DATAP75 = 43.1863 / \n",
"DATAP90 = 64.8415 / \n",
"DATAP95 = 84.5688 / \n",
"DATAP98 = 114.404 / \n",
"DATAP99 = 139.341 / \n",
"NEXP_PRP= 1.00000 / \n",
"NEXP = 200 / \n",
"NEXPOBS = 1200 / \n",
"NRASTERP= 200 / \n",
"RASTYPDX= 1 / \n",
"RASTYPNX= 1 / \n",
"RASRPT = 1 / \n",
"RASNRPT = 1 / \n",
"CADPL_AV= 63.1270 / \n",
"CADPL_DV= 0.00000 / \n",
"CADEX_AV= 63.1250 / \n",
"CADEX_DV= 0.0479676 / \n",
"MISSOBS = 4 / \n",
"MISSRAS = 0 / \n",
"IPRPVER = 1.42999994755 / \n",
"IPRPPDBV= 8.00000000000 / \n",
"IPRPDVER= 20130925 / \n",
"IPRPBVER= 20150927 / \n",
"PC1_1 = 0.999995231628 / \n",
"PC1_2 = 0.0112744029611 / \n",
"PC2_1 = -0.0112744029611 / \n",
"PC2_2 = 0.999995231628 / \n",
"PC3_1 = 0.00000000000 / \n",
"PC3_2 = 0.00000000000 / \n",
"NWIN = 1 / \n",
"TDET1 = 'SJI ' / \n",
"TDESC1 = 'SJI_1330' / \n",
"TWAVE1 = 1330.00 / \n",
"TWMIN1 = 1310.00 / \n",
"TWMAX1 = 1350.00 / \n",
"TDMEAN1 = 36.5037 / \n",
"TDRMS1 = 28.6593 / \n",
"TDMEDN1 = 29.0919 / \n",
"TDMIN1 = -2726.66 / \n",
"TDMAX1 = 26851.0 / \n",
"TDVALS1 = 203736243 / \n",
"TMISSV1 = 200303757 / \n",
"TSATPX1 = 0 / \n",
"TSPIKE1 = 0 / \n",
"TTOTV1 = 404040000 / \n",
"TPCTD1 = 50.4248 / \n",
"TDSKEW1 = 10.1806 / \n",
"TDKURT1 = 502.324 / \n",
"TDP01_1 = 10.5103 / \n",
"TDP10_1 = 15.7991 / \n",
"TDP25_1 = 20.7684 / \n",
"TDP75_1 = 43.1863 / \n",
"TDP90_1 = 64.8415 / \n",
"TDP95_1 = 84.5688 / \n",
"TDP98_1 = 114.404 / \n",
"TDP99_1 = 139.341 / \n",
"TSR1 = 1 / \n",
"TER1 = 1092 / \n",
"TSC1 = 13 / \n",
"TEC1 = 1026 / \n",
"IPRPFV1 = 213 / \n",
"IPRPGV1 = 4 / \n",
"IPRPPV1 = 213 / \n",
"KEYWDDOC= 'http://www.lmsal.com/iris_science/irisfitskeywords.pdf' / \n",
"HISTORY iris_prep Set 3 saturated pixels to Inf \n",
"HISTORY iris_prep Dark v20130925; T=[-62.0,-62.0,-61.9,-62.4,0.2,-0.2,-0.0,-0.2\n",
"HISTORY iris_prep Flat fielded with recnum 213 \n",
"HISTORY iris_prep Set permanently bad pixels to 0 prior to warping \n",
"HISTORY iris_prep_geowave_roi ran with rec_num 4 \n",
"HISTORY iris_prep_geowave_roi boxwarp set to 1 \n",
"HISTORY iris_prep_geowave_roi updated WCS parameters with iris_isp2wcs \n",
"HISTORY iris_prep Used iris_mk_poindtb ver 8 \n",
"HISTORY iris_prep Fiducial midpoint shift X,Y [pix]: -15.77 -14.15 \n",
"HISTORY iris_prep Used INF_POLY_2D for warping \n",
"HISTORY iris_prep VERSION: 1.43 \n",
"HISTORY iris_prep ran on 20151022_084442 \n",
"HISTORY level2 Version L12-2015-08-14 "
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"finfo.header"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"XTENSION= 'IMAGE ' / IMAGE extension \n",
"BITPIX = -64 / Number of bits per data pixel \n",
"NAXIS = 2 / Number of data axes \n",
"NAXIS1 = 31 / \n",
"NAXIS2 = 200 / \n",
"PCOUNT = 0 / No Group Parameters \n",
"GCOUNT = 1 / One Data Group \n",
"TIME = 0 /time of each exposure in s after start of OBS (r\n",
"PZTX = 1 /PZTX of each exposure in arcsec (rowindex) \n",
"PZTY = 2 /PZTY of each exposure in arcsec (rowindex) \n",
"EXPTIMES= 3 /SJI Exposure duration of each exposure in s (row\n",
"SLTPX1IX= 4 /Slit center in X of each exposure in window-pixe\n",
"SLTPX2IX= 5 /Slit center in Y of each exposure in window-pixe\n",
"SUMSPTRS= 6 /SJI spectral summing (rowindex) \n",
"SUMSPATS= 7 /SJI spatial summing (rowindex) \n",
"DSRCSIX = 8 /SJI data source level (rowindex) \n",
"LUTIDS = 9 /SJI LUT ID (rowindex) \n",
"XCENIX = 10 /XCEN (rowindex) \n",
"YCENIX = 11 /YCEN (rowindex) \n",
"OBS_VRIX= 12 /Speed of observer in radial direction (rowindex)\n",
"OPHASEIX= 13 /Orbital phase (rowindex) \n",
"PC1_1IX = 14 /PC1_1 (rowindex) \n",
"PC1_2IX = 15 /PC1_2 (rowindex) \n",
"PC2_1IX = 16 /PC2_1 (rowindex) \n",
"PC2_2IX = 17 /PC2_2 (rowindex) \n",
"PC3_1IX = 18 /PC3_1 (rowindex) \n",
"PC3_2IX = 19 /PC3_2 (rowindex) \n",
"IT01PSJI= 20 /SJI PM Temperature (for pointing) (rowindex) \n",
"IT06TSJI= 21 /SJI MidTel Temperature (for pointing) (rowindex)\n",
"IT14SSJI= 22 /SJI Spectrograph +X Temperature (for wavelength)\n",
"IT15SSJI= 23 /SJI Spectrograph +Y Temperature (for wavelength)\n",
"IT16SSJI= 24 /SJI Spectrograph -X Temperature (for wavelength)\n",
"IT17SSJI= 25 /SJI Spectrograph -Y Temperature (for wavelength)\n",
"IT18SSJI= 26 /SJI Spectrograph +Z Temperature (for wavelength)\n",
"IT19SSJI= 27 /SJI Spectrograph -Z Temperature (for wavelength)\n",
"POFFXSJI= 28 /SJI X (spectral direction) shift in pixels (rowi\n",
"POFFYSJI= 29 /SJI Y (spatial direction) shift in pixels (rowin\n",
"POFFFSJI= 30 /SJI Flag indicating source of X and Y offsets (r"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hdu.header"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 7970299.50 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1 loop, best of 3: 954 ns per loop\n"
]
}
],
"source": [
"%timeit x = finfo.data[2]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 6.37 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"100 loops, best of 3: 4.44 ms per loop\n"
]
}
],
"source": [
"%timeit x = finfo.section[2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment