The FITS static class

in the astronomy & astrophysics toolbox for MATLAB

Description:

The FITS is a static class that supports read, write and manipulation of Flexible Image Transport Files (FITS). FITS are commonly used astronomical image file format, and FITS utilities and standards are being developed by the FITS support office.

MATLAB has a full support of high level and low level FITS functions and can call the FITS/IO library. This static class provide additional high level functions.

This file is accessible through the manual package (i.e. manual.FITS).

We note that reading and writing FITS files is slow relative to simple arithmatic operations. This is one of the reasons why the philosophy of the image reduction tools in the astronomy and astrophysics toolbox is to use the in-memory SIM object. SIM object is simply a container for astronomical images and it can be associated with additional meta data like, header, WCS, catalogs, PSF, background image, noise image, weight image and mask image. The FITS static class support also reading FITS files into SIM objects.

We note that reading and writing HDF5 files is an order of magnitude faster than reading and writing FITS files, and it is useful to inspect this alternative. Support for this option is TBD.

Credit

If you are using this code or products in your scientific publication please give a reference to Ofek (2014; ascl.soft 07005).

License

Unless specified otherwise this code and products are released under the GNU general public license version 3.

Installation

See http://weizmann.ac.il/home/eofek/matlab/doc/install.html for installation instruction and additional documentation.

Methods and examples

Reading FITS images into SIM

The most basic operation is reading a FITS file:

The FITS.read2sim method can be used to read FITS files into SIM objects:

S=FITS.read2sim('test_PTF*.fits')

Various file input methods are supported. The first argument (the list of file names) is being parsed using the Util.files.create_list function (see help Util.files.create_list for details). For example, one can read file names listed in a file:

%S=FITS.read2sim('@file');

or use wild cards and number ranges (note that more than one digit range is supported):

% doen't work - need to fix bug!

S=FITS.read2sim('test_PTF_20[12-16]*.fits')

S =
  SIM with properties:

               Im: []
    ImageFileName: ''
           BackIm: []
            ErrIm: []
         WeightIm: []
              Cat: []
              Col: []
          ColCell: []
         ColUnits: []
         SortedBy: []
      SortedByCol: []
             Name: []
           Source: []
        Reference: []
          Version: []
           Header: {0×3 cell}
              WCS: []
         UserData: []
             Mask: []
          MaskDic: []
              PSF: []
           ErrPSF: []
           ParPSF: {}
           CooPSF: []

or a cell array of files:

cd ~/matlab/images

[~,Cell] = Util.files.create_list('test_PTF*.fits',NaN)

Cell =
    'test_PTF_201205233056_i_p_scie_t072001_u013342353_f02_p004162_c08.fits'    'test_PTF_201606123141_i_p_scie_t073220_u027914930_f02_p004162_c08.fits'

S=FITS.read2sim(Cell);

One can also read FITS image sections:

S=FITS.read2sim('test_PTF*.fits','CCDSEC',[1 100 1 100]);

surface(S(1).Im)

For details regarding additional options see help FITS.read2sim.

Reading FITS images into an array

The following example read a section of a FITS file into an array (the default is to read the entire image):

cd ~/matlab/images

[~,Cell] = Util.files.create_list('test_PTF*.fits',NaN)

Cell =
    'test_PTF_201205233056_i_p_scie_t072001_u013342353_f02_p004162_c08.fits'    'test_PTF_201606123141_i_p_scie_t073220_u027914930_f02_p004162_c08.fits'

Im=FITS.read(Cell{1},[],[11 11],[70 70]);

surface(Im)

or you can read a list of FITS images (with the same size) into a cube, where the first dimension is the image index.

S=FITS.read2cube('test_PTF*.fits');

size(S)

ans =
           2        4096        2048

The function FITS.fitsread call the internal matlab FITS file reader.

Read FITS tables

You can use FITS.read_table to read FITS tables.

Writing FITS images

You can use FITS.fitswrite, FITS.write and FITS.write_old to write FITS images. For example:

Flag=FITS.write(rand(100,100),'TryTry.fits');

!rm TryTry.fits

The header

There are several functions for reading, editing and manipulating FITS headers and keywords. To read the FITS header (only) into an HEAD object (see separate documentation):

H=FITS.get_head('test_PTF*.fits')

H =
  1×2 HEAD array with properties:

    Header
    WCS
    UserData

The FITS.head command can be used to display the FITS header of a single file (either a file or an HEAD object):

FITS.head(H(1))

ans =
  SIMPLE                 NaN Fits standard
  BITPIX      -32.0000000000 FOUR-BYTE SINGLE PRECISION FLOATING POINT
   NAXIS        2.0000000000 STANDARD FITS FORMAT
  NAXIS1     2048.0000000000 STANDARD FITS FORMAT
  NAXIS2     4096.0000000000 STANDARD FITS FORMAT
  ORIGINPalomar Transient Factory Origin of these image data
CREATORInfrared Processing and Analysis Center Creator of this FITS file
TELESCOP            P48      Name of telescope
INSTRUME          PTF/MOSAIC Instrument name
OBSERVER         KulkarniPTF Observer name and project
   CCDID            8        CCD number (0..11)
DATE-OBS2012-05-23T07:20:01.937 UTC shutter time YYYY-MM-DDTHH:MM:SS.SSS
    DATE 2012-05-23T09:28:46 File creation date (YYYY-MM-DDThh:mm:ss UT)
REFERENChttp://www.astro.caltech.edu/ptf' / URL of PTF websiteptf' / URL of PTF website
PTFPRPI            Kulkarni PTF Project PI
  PTFPID            30011    Project type: 00000-49999
  OBJECT          PTF_survey Fields object
PTFFIELD            4162     PTF unique field ID
PTFFLAG            1        1 = PTF; 0 = non-PTF category
  FILTER            R        Filter name
FILTERID            2        Filter ID
FILTERSL            1        Filter changer slot position
EXPTIME       60.0000000000 [s] Requested exposure time
AEXPTIME       60.0000000000 actual exposure time (sec)
UTC-OBS2012-05-23T07:20:01.937 UTC time shutter open YYYY-MM-DDTHH:MM:SS.
   OBSJD  2456070.8055699999 [day] Julian day corresponds to UTC-OBS
  OBSMJD    56070.3055700000 MJD corresponds to UTC-OBS (day)
  OBSLST         15:37:49.68 Mean LST corresponds to UTC-OBS 'HH:MM:SS.S'
HOURANG         -0:57:41.00 Mean HA (sHH:MM:SS.S) based on LMST at UTC-OBS
     HJD  2456070.8089700001 [day] Heliocentric Julian Day
OBSTYPE            object   Image type (dark,science,bias,focus)
  IMGTYP            object   Image type (dark,science,bias,focus)
  MOONRA       87.2731850000 [deg] Moon J2000.0 R.A.
MOONDEC       20.6650400000 [deg] Moon J2000.0 Dec.
MOONILLF        0.0486000000 [frac] Moon illuminated fraction
MOONPHAS      154.5286000000 [deg] Moon phase angle
MOONESB       -0.0000000000 Moon excess in sky brightness V-band
MOONALT      -27.5645200000 [deg] Moon altitude
   SUNAZ      353.0420000000 [deg] Sun azimuth
  SUNALT      -35.6648600000 [deg] Sun altitude
   BUNIT            DN       Data number (analog-to-digital units or ADU)
PHTCALEX        1.0000000000 Was phot.-cal. module executed?
PHTCALFL        1.0000000000 Flag for image is photometric (0=N, 1=Y)
PCALRMSE        0.0263410000 RMSE from (zeropoint, extinction) data fit
IMAGEZPT       23.3086200000 Image magnitude zeropoint
COLORTRM        0.1377820000 Image color term (g-r)
ZPTSIGMA        0.0181340000 Robust dispersion of SEx-SDSS magnitudes
IZPORIG            SDSS     Photometric-calibration origin
  ZPRULE            DIRECT   Photometric-calibration method
  MAGZPT       23.3268400000 Magnitude zeropoint at airmass=1
EXTINCT        0.1358640000 Extinction
APSFILT            r        SDSS filter used in abs phot cal
  APSCOL            r-i      SDSS color used in abs phot cal
   APRMS        0.0417657000 RMS in mag of final abs phot cal
APBSRMS        0.0234658700 RMS in mag of final abs phot cal for bright sta
APNSTDI1   163870.0000000000 Number of standard stars in first iteration
APNSTDIF   153726.0000000000 Number of standard stars in final iteration
  APCHI2   409231.2817779100 Chi2 of final abs phot cal
   APDOF   153715.0000000000 Dof of chi2 of final abs phot cal
APMEDJD  2456070.8333872198 Median JD used in abs phot cal
  APPN01           ZeroPoint Name of parameter abs phot cal 01
APPAR01       23.4267993700 Value of parameter abs phot cal 01
APPARE01        0.0010640700 Error of parameter abs phot cal 01
  APPN02           ColorTerm Name of parameter abs phot cal 02
APPAR02        0.2125329500 Value of parameter abs phot cal 02
APPARE02        0.0015520600 Error of parameter abs phot cal 02
  APPN03         AirMassTerm Name of parameter abs phot cal 03
APPAR03       -0.1109834800 Value of parameter abs phot cal 03
APPARE03        0.0008787200 Error of parameter abs phot cal 03
  APPN04    AirMassColorTerm Name of parameter abs phot cal 04
APPAR04       -0.0019008300 Value of parameter abs phot cal 04
APPARE04        0.0012602100 Error of parameter abs phot cal 04
  APPN05            TimeTerm Name of parameter abs phot cal 05
APPAR05        0.0244465000 Value of parameter abs phot cal 05
APPARE05        0.0015265100 Error of parameter abs phot cal 05
  APPN06           Time2Term Name of parameter abs phot cal 06
APPAR06        0.4440270000 Value of parameter abs phot cal 06
APPARE06        0.0169121700 Error of parameter abs phot cal 06
  APPN07            XTerm    Name of parameter abs phot cal 07
APPAR07        0.0105750300 Value of parameter abs phot cal 07
APPARE07        0.0004082700 Error of parameter abs phot cal 07
  APPN08            YTerm    Name of parameter abs phot cal 08
APPAR08       -0.0031507100 Value of parameter abs phot cal 08
APPARE08        0.0010232400 Error of parameter abs phot cal 08
  APPN09            Y2Term   Name of parameter abs phot cal 09
APPAR09        0.0193487100 Value of parameter abs phot cal 09
APPARE09        0.0016131000 Error of parameter abs phot cal 09
  APPN10            Y3Term   Name of parameter abs phot cal 10
APPAR10        0.0284405200 Value of parameter abs phot cal 10
APPARE10        0.0064104400 Error of parameter abs phot cal 10
  APPN11            XYTerm   Name of parameter abs phot cal 11
APPAR11        0.0140945700 Value of parameter abs phot cal 11
APPARE11        0.0014344400 Error of parameter abs phot cal 11
  CRVAL1      248.4820365961 [deg] RA of reference point
  CRVAL2       32.2364556726 [deg] DEC of reference point
  CRPIX1     1243.6350000000 [pix] Image reference point
  CRPIX2     1251.8710000000 [pix] Image reference point
  CTYPE1        RA---TAN-SIP TAN (gnomic) projection + SIP distortions
  CTYPE2        DEC--TAN-SIP TAN (gnomic) projection + SIP distortions
  CUNIT1            deg      Image axis-1 celestial-coordinate units
  CUNIT2            deg      Image axis-2 celestial-coordinate units
CRTYPE1            deg      Data units of CRVAL1
CRTYPE2            deg      Data units of CRVAL2
   CD1_1        0.0002812418 Transformation matrix
   CD1_2        0.0000004875
   CD2_1        0.0000004612
   CD2_2       -0.0002812495
   OBJRA        16:35:03.370 Requested field J2000.0 Ra.
  OBJDEC        +32:37:30.00 Requested field J2000.0 Dec.
  OBJRAD      248.7640400000 [deg] Requested field RA (J2000.0)
OBJDECD       32.6250000000 [deg] Requested field Dec (J2000.0)
PIXSCALE        1.0100000000 [arcsec/pix] Pixel scale
WCSAXES        2.0000000000
EQUINOX     2000.0000000000 [yr] Equatorial coordinates definition
LONPOLE      180.0000000000
LATPOLE        0.0000000000
  SEEING        2.1100000000 [pix] Seeing FWHM
PEAKDIST        0.3736161131 [pix] Mean dist brightest pixel-centroid pixel
   ELLIP        0.0630000000 Mean image ellipticity A/B
ELLIPPA      -44.3300000000 [deg] Mean image ellipticity PA
   FBIAS      669.1448000000 [DN] Floating bias of the image
SATURVAL    53000.0000000000 [DN] Saturation value of the CCD array
FWHMSEX        2.1500000000 [arcsec] SExtractor SEEING estimate
MDSKYMAG       20.8787100000 [mag/s-arcsec^2] Median sky obsolete
MSMAPCZP       20.8832200000 [mag/s-arcsec^2] Median sky abs. phot. cal.
LIMITMAG       21.5707600000 [mag/s-arcsec^2] Limiting magnitude obsolete
LMGAPCZP       21.5752700000 [mag/s-arcsec^2] Limiting mag. abs. phot. cal.
MEDFWHM        2.6830190000 [arcsecond] Median FWHM
MEDELONG        1.1475960000 [dimensionless] Median elongation
STDELONG        0.4866248000 [dimensionless] Std. dev. of elongation
MEDTHETA       -1.1546510000 [deg] Atan(median sin(theta)/median cos(theta))
STDTHETA       63.1680600000 [deg] Atan(stddev sin(theta)/stddev cos(theta))
MEDDLMAG       29.8947600000 [mag/s-arcsec^2] Median (MU_MAX-MAG_AUTO)
STDDLMAG        0.4480669000 [mag/s-arcsec^2] Stddev of (MU_MAX-MAG_AUTO)
OCS_TIME2012-05-23T07:20:01.845 UTC Date for OCS calc time-dep params
OPERMODE            OCS      Mode of operation: OCS | Manual | N/A
SOFTVER            1.1.1.1  Softwere version (TCS.Camera.OCS.Sched)
OCS_VER            1        OCS software version and date
TCS_VER            1        TCS software version and date
SCH_VER            1        OCS-Scheduler software version and date
MAT_VER           7.7.0.471 Matlab version
HDR_VER            1        Header version
TRIGGER            N/A      trigger ID for TOO, e.g. VOEVENT-Nr
TCSMODE            Star     TCS fundamental mode
TCSSMODE            Active   TCS fundamental submode
TCSFMODE            Pos      TCS focus mode
TCSFSMOD           On-Target TCS focus submode
TCSDMODE            Stop     TCS dome mode
TCSDSMOD            N/A      TCS dome submode
TCSWMODE            Slave    TCS windscreen mode
TCSWSMOD            N/A      TCS windscreen submode
  OBSLAT       33.3574000000 [deg] Telescope geodetic latitude in WGS84
  OBSLON     -116.8599000000 [deg] Telescope geodetic longitude in WGS84
  OBSALT     1703.2000000000 [m] Telescope geodetic altitude in WGS84
DEFOCUS        0.0000000000 [mm] Focus position - nominal focus
FOCUSPOS        1.3620000000 [mm] Exposures focusPos
DOMESTAT            open     Dome status at begining of exposure
TRACKRA       10.5000000000 [arcsec/hr] Track speed RA rel to sidereal
TRACKDEC       -1.5000000000 [arcsec/hr] Track speed Dec rel to sidereal
AZIMUTH       89.5647900000 [deg] Telescope Azimuth
ALTITUDE       77.9868300000 [deg] Telescope altitude
AIRMASS        1.0223480000 Telescope airmass
   TELRA      248.8778000000 [deg] Telescope ap equinox of date RA
  TELDEC       32.6000000000 [deg] Telescope ap equinox of date Dec
   TELHA      345.5788000000 [deg] Telescope ap equinox of date HA
  DOMEAZ       89.8835000000 [deg] Dome azimuth

We note that the HEAD class provide many more powerfull functions to work with header information.

The function FITS.num_hdu return the number of header data units (HDU) available ina FITS file.

Keywords manipulation

To read specific keywords directly from the FITS header:

[KeysVal,KeysComment,Struct]=FITS.get_keys('test_PTF_201205233056_i_p_scie_t072001_u013342353_f02_p004162_c08.fits',{'NAXIS1','NAXIS2'})

KeysVal =
    '2048'    '4096'

KeysComment =
    'STANDARD FITS FORMAT'    'STANDARD FITS FORMAT'

Struct =
    NAXIS1: '2048'
    NAXIS2: '4096'

The function FITS.mget_keys can be used to read FITS header keywords from multiple FITS files:

[KeysVal,KeysComment]=FITS.mget_keys('_test_PTF*.fits',{'NAXIS1','NAXIS2'});

You can also write new keywords to a FITS header using FITS.write_keys, and the functions FITS.get_sip and FITS.get_tpv can be used to read the specific WCS keywords from a FITS header.

Additional functions

There are several other functions FITS.cellhead_* which can be used to manipulate the 3 column cell array in which the FITS header is stored.