Munipack's logo

Munipack

An astronomical image processing software

📘
Guide
📁
Documents

FITS file utility

An utility manipulating with files in FITS format.

Command

munipack fits [.. parameters ..] file(s)[,file(s)]

Description

FITS format is a native format of Munipack as well as rest of astronomical softwares. Munipack stores all data as FITS images. Moreover related processing products, like tables with aperture photometry or calibration data are also included in the files.

Following operations on FITS file(s) are available:

Important part of functionality is just a shell wrapper of (c)fitsio library functions.

Input and output

On input, the only one or more files is expected depending on operation.

Results depends on selected operation.

Many utilities prints results to standard output (usually connected to terminal). It is desired for (short) informative messages. Large blocks of data or results intended to be kept can be redirected to a text file specifying of > operator.

Parameters

-lh, --header
list header
-lt, --table
list table
-li, --image
list image
-K, --keys key[,key,..]
Print header values by the keyword(s). Multiple keywords can be specified. Values are printed in FITS header order.
--shell
shell-friendly output format for keyword prints, only usefull with -K option
--value
print only values (without keywords and comments) for the keyword print option
--update
Indicates to add or to update operation on FITS header. Usefull with --key, --val, --com options. Only single record can be add or update during a single run.
--key keyword
specify keyword of updated record
--val value
specify value of updated record
--com comment
specify comment of updated record
--templ file
update records in header by this file. The option in mutual to --key. The file must confirm rules of FITS templates.
--remove-keys key[,key,..]
remove header's records by keyword(s)
--remove-extension extension[,extension,..]
remove the specified extension(s)
--dump
dump FITS to plain text
--restore
restore FITS from plain text
--cat
cat (copy) input on output.

Also see Common options

Any listing invoked by -lt and -li will print full data. One can be limited by using of extended filename syntax.

List structure of a FITS file

Any FITS file may by composed from multiple parts (HDUs — header data units) of three types: dummy, image or table. Every HDU must contain header with proper parameters (dimensions, bitpix, …). Multiple HDUs would be used for storing of related information. For example, Munipack adds a table with photometry results to processed images.

The structure of a FITS file is listed when no arguments are used:

$ munipack fits spln.fits
#    EXTNAME       TYPE BITPIX SIZE
1                 IMAGE    -32 200x200x4
2 'u5ct.c0h.tab'  ASCII_TBL    4x49

In this example, the FITS file consist from two parts (HDU). The unnamed first is an 3D image 200 × 200 × 4 pixels and the second is an table. The image can be also accessed as spln.fits[0] and the table as spln.fits[1].

List of header: -lh

Every HDU contains parameters carrying additional information's (meta-info) about included data. The mandatory parameters are specifying dimensions of an image or a table, data representation (and some technical records) and must be included in any valid HDU. Other parameters are optional and may give a time, exposure conditions or band specifications for images (and many related info). Header of a table must include dimensions and the standardized descriptions of columns. Headers frequently contains some calibration data as the astrometry data. FITS keyword dictionaries summarizes commonly used optional parameters as are used by various astronomical communities.

The argument -lh (or --header) invokes listing of full header.

$ munipack fits -lh spln.fits
SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                  -32 / number of bits per data pixel
NAXIS   =                    3 / number of data axes
NAXIS1  =                  200 / length of data axis 1
...

These records are directly copied-out from FITS header without any formatting.

List of header using keywords: -K

Only selected records may be listed by using of specified keywords with -K (or --print-keys) argument.

$ munipack fits -K NAXIS1,NAXIS2 spln.fits
NAXIS1  =                  200 / length of data axis 1
NAXIS2  =                  300 / length of data axis 2

There is also possibility to print a record in machine-oriented (for further processing) format KEYWORD=VALUE. To activate the mode, use the switch --shell:

$ munipack fits -K NAXIS spln.fits
NAXIS   =                    3 / number of data axes
$ munipack fits -K NAXIS --shell spln.fits
NAXIS=3

Tip. Shell scripts can use the output to set variables by using of eval function:

$ A=$(munipack fits -K NAXIS --shell spln.fits)
$ echo $A
NAXIS=3
$ eval "$A"
$ echo $NAXIS
3

The value itself can be extracted from a header by passing option --value:

A=$(munipack fits -K NAXIS --value spln.fits)
echo $A
3

Remove in header using keywords: --remove-keys

Any keyword can be removed from header. For illustration, the command removes an object identifier (with keyword OBJECT):

$ munipack fits --remove-keys OBJECT spln.fits

Add or update in header using keyword: --update, --key, --val, --com

A new record as well as already existing record can be added or modified as

$ munipack fits --update --key OBJECT --val 'Spln' spln.fits

The main purpose of this option is adding new keywords and values to FITS headers or modification of already presented records.

This option has been designed for the properties:

Examples (get look to distinguish between integer and real numbers):

$ munipack fits --update --key EXPTIME --val 30 spln.fits             # integer
$ munipack fits -K EXPTIME spln.fits
EXPTIME =                   30

$ munipack fits --update --key EXPTIME --val 30.0 spln.fits           # real
$ munipack fits -K EXPTIME spln.fits
EXPTIME =                  30.

$ munipack fits --update --key EXPTIME --val "'30'" spln.fits         # string
$ munipack fits -K EXPTIME spln.fits
EXPTIME = '30      '

$ munipack fits --update --key EXPTIME --val F spln.fits              # logical
$ munipack fits -K EXPTIME spln.fits
EXPTIME =                    F

$ munipack fits --update --key EXPTIME --val 30.0 --com "[s] exposure time" spln.fits
$ munipack fits -K EXPTIME spln.fits
EXPTIME =                  30. / [s] exposure time

The last one demonstrates a bit complex, but preferred format.

Add or update header by a template file: --update, --templ

A header can be modified also with a template file. The approach can provide way for more flexible updates of header or update of multiple records during one run.

A new record as well as already existing record can be added or modified as

$ munipack fits --update --templ templ spln.fits

where an auxliary template file templ can contain:

EXPTIME = 666 /[s] exposure time
OBJECT = 'Fool Moon' / object name

List of image values: -li

The argument -li (or --image) switch-on listing of intensity values contained in an image.

The output listing is in the format: indexes in all axes followed by a value itself:

i1 i2 i3 ... value

Use of extended filename syntax is recommended. See the example:

$ munipack -li fits spln.fits
1 1 321
2 1 310
...

List of table values: -lt

The argument -lt (or --table) switch-on listing of values contained in a table.

Use of extended filename syntax is recommended. See the example:

$ munipack -lt fits spln.fits
B        330.460300000000        42.0238720000000 S
...

Extended FITS file syntax

The syntax is commonly used for selecting of an embedded part of FITS file or a certain part of an image or a table. The extended filenames contains brace index selector in addition to usual file names. Ones considerably benefits from FITS file extended syntax.

Important note: The extended syntax infers with shell syntax. Therefore it is recommended to enclose extended filenames in apostrophes.

The second part of a FITS file (indexed from zero) will be selected as

$ munipack fits -lh 'spln.fits[1]'
XTENSION= 'TABLE   '           / Ascii table extension
BITPIX  =                    8 / 8-bits per 'pixels'
NAXIS   =                    2 / Simple 2-D matrix
NAXIS1  =                  796 / Number of characters per
...

A sub-image of a whole image can be accessed as

$ munipack fits -li 'spln.fits[666:676,100:103]'
1 1 321
2 1 310
...

Analogically we selected objects with positive fluxes in a table with extension MUNIPACK:

$ munipack fits -lt 'spln.fits[MUNIPACK][APER1>=0]'
12.335
12.339
...

Remove any FITS extension

Any FITS extension can be removed like the example:

$ munipack fits --remove-extensions MUNIPACK,XXX spln.fits

FITS file dump/restore

The content of a FITS file should be dumped to a plain text file. Also, a FITS file can be created from a plain text file. The functionality can be important when above options are not satisfying needs, or to create a new FITS file or when we need a text version of FITS file.

The basic usage of the command for dump is

$ munipack fits --dump file.fits

The output is stored to file.lst file. The file can be directly inspected by any text tool (editor). There is no way to dump on the standard output (terminal) which is incompatible with processing of more files together.

An inverse operation, creating of FITS file from a text template, is similar:

$ munipack fits --restore file.lst

FITS file and text names are derived mutually with rule: *.fits -> *.lst (and vice verso). The behaviour can be changed using Advanced Output Filenames. For example:

munipack fits -O --pattern '(.+)\.fits' --mask '\1.list' --dump file.fits

The contents of a FITS file is generally accessible from many other tools. Munipack is using directly cfitsio library. One is also accessible from Python Similar functionality is offered by ftools.

Format of dump/restore file

The dump/restore format for FITS file must comply these rules:

The template is:

# BEGIN HDU 0
 ... ( header ) ....
END
 ... ( data - optional ) ...
# END HDU 0
# BEGIN HDU 1
 ... ( header ) ...
END
 ... ( data - optional ) ...
# END HDU 1
 ...

Example:

# BEGIN HDU 0
SIMPLE  =                    T / file does conform to FITS standard
BITPIX  =                   16 / number of bits per data pixel
NAXIS   =                    0 / number of data axes
EXTEND  =                    T / FITS dataset may contain extensions
END
# END HDU 0
# BEGIN HDU 1
XTENSION= 'BINTABLE'           / binary table extension
BITPIX  =                    8 / 8-bit bytes
NAXIS   =                    2 / 2-dimensional binary table
NAXIS1  =                   28 / width of table in bytes
NAXIS2  =                    4 / number of rows in table
PCOUNT  =                    0 / size of special data area
GCOUNT  =                    1 / one data group (required keyword)
TFIELDS =                    5 / number of fields in each row
TTYPE1  = 'name    '           / label for field   1
TFORM1  = '12A     '           / data format of field: ASCII Character
TTYPE2  = 'RA      '           / label for field   2
TFORM2  = 'E       '           / data format of field: 4-byte REAL
TUNIT2  = 'deg     '           / physical unit of field
TTYPE3  = 'DEC     '           / label for field   3
TFORM3  = 'E       '           / data format of field: 4-byte REAL
TUNIT3  = 'deg     '           / physical unit of field
TTYPE4  = 'MAG     '           / label for field   4
TFORM4  = 'E       '           / data format of field: 4-byte REAL
TTYPE5  = 'MAGERR  '           / label for field   5
TFORM5  = 'E       '           / data format of field: 4-byte REAL
HISTORY This FITS file was created by the FCREATE task.
HISTORY fcreate3.2e at 2012-12-10T00:00:51
DATE    = '2012-12-10T00:00:51' / file creation date (YYYY-MM-DDThh:mm:ss UT)
END
'B'        330.689        42.2765        11.9300      0.0500000
'C'        330.667        42.2860        13.6900      0.0300000
'H'        330.636        42.2798        13.6000      0.0300000
'K'        330.650        42.2816        14.8800      0.0500000
# END HDU 1

When the structure of a table is changed (count of rows or columns), it is important to change NAXIS2 (count of rows), TFIELDS (count of columns) and NAXIS1 for which total length of a row must be computed as sum: length of character (12 bytes in our example), 4 bytes per integer value with I flag in TFORM., 4 bytes per single floating value with E flag (4*4 bytes) and 8 bytes for double floating value with D flag (sum is 12+4*4 = 28 in NAXIS1).

The text can be directly pasted to a file blcal.lst and output FITS file blcal.fits should be restored:

$ munipack fits --restore blcal.lst

It is recommended to test of a restored FITS by using of FITS File Verifier.

Cat (copy) FITS input on FITS output

This option looks useless but it has very powerfull capability. The common usage is filtering of images end tables.

$ munipack fits --cat 'in.fits[1:10,1:10]',out.fits

Cat is clone of fitscopy utility by cFITSIO. FITS file extended syntax and FITS tools gives more examples.

Advanced usage

The fits action gives a good support for usage in shell scripts and other machine processing tools.

The simple usage of the action for listing of some parameters from all FITS files in a directory shows following example.

$ for A in *.fits; do
    B=$(munipack fits --shell -K DATE-OBS,FILTER  "$A[0]" | \
     awk -F "=" '{ printf "%s ",$2; }');
     echo $A $B;
  done
...
hp29716_48R.fits '2011-02-07T20:50:18.822' 'R'
hp29716_49R.fits '2011-02-07T20:51:36.610' 'R'
hp29716_50R.fits '2011-02-07T20:52:54.345' 'R'
hp29716_51R.fits '2011-02-07T20:54:11.962' 'R'
hp29716_52R.fits '2011-02-07T20:55:29.588' 'R'
...

A similar effect can be implemented with the code:

$ for A in *.fits; do
    B=$(munipack fits --shell -K DATE-OBS,FILTER  "$A[0]" | sed 's/^DATE\-OBS/DATEOBS/');
    for C in $B; do eval export "$C"; done
    echo $A $DATEOBS $FILTER;
  done
...
hp29716_48R.fits 2011-02-07T20:50:18.822 R
hp29716_49R.fits 2011-02-07T20:51:36.610 R
hp29716_50R.fits 2011-02-07T20:52:54.345 R
hp29716_51R.fits 2011-02-07T20:54:11.962 R
hp29716_52R.fits 2011-02-07T20:55:29.588 R
...

The code for C in $B; do eval export "$C"; done split array of variables and sets their values. One is equivalent to:

$ munipack fits --shell -K NAXIS file.fits
NAXIS=2
$ export NAXIS=2
$ echo $NAXIS
2

Important for use in a shell is the code sed 's/^DATE\-OBS/DATEOBS/' which transforms invalid variable DATE-OBS (meaning a difference) to an ordinary variable identifier. When DATE-OBS is not required, the calling of the piece of code can be omitted.

See also

Common options, astropy.io.fits (for Python, very general, flexible and ideal for scripting), ftools (futils are more general and matured).