.. C-Munipack - User's manual

   Copyright 2012 David Motl

   Permission is granted to copy, distribute and/or modify this document under the
   terms of the GNU Free Documentation License, Version 1.2 or any later version published
   by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and
   no Back-Cover Texts.

   $Id: photometry_files_format.rst,v 1.2 2015/07/12 07:44:57 dmotl Exp $

.. index:: 
   pair: photometry; files

.. _photometry-files-format:

Photometry files
================

The photometry files are produced by the photometry process. The file
consists of a table of detected objects and their properties. 

The photometry files are stored as binary streams in order to get 
optimum performace, because it does not require analyzing any kind of 
language and decoding the values.

The binary format of photometry files was introduced to latter versions of C-Munipack 1.2,
because it has turned out that the XML-based formats are too slow to parse.

Multi-byte data is stored in little-endian format (Intel). Real numbers are stored in the
*binary64* format of IEEE 754-1985.

.. rubric:: File header

The general structure of binary photometry file is given in table :ref:`1 <table:pht_bin_head>`.

.. _table:pht_bin_head:

Table 1

====== ====== ======== ===========
Offset Type   Length   Description
====== ====== ======== ===========
 0     char[] 28       Format identifier
28     long   4        Format revision number
32     long   4        Length of metadata in bytes
====== ====== ======== ===========

The format identifier must be the following text::

   C-Munipack photometry file\r\n

Current format revision number is **4**.

.. rubric:: Metadata

The parameters are stored on fixed offsets. The length of the header is 540 bytes.
See the table :ref:`2 <table:pht_bin_meta>` for description of header fields.

.. _table:pht_bin_meta:

Table 2

====== ============ ======== ===========
Offset Type         Length   Description
====== ============ ======== ===========
0      long         4        unused    
4      long         4        Frame width in pixels    
8      long         4        Frame height in pixels    
12     binary64     8        Julian date of observation 
20     char[]       70       Optical filter, right-padded with spaces 
90     binary64     8        Exposure duration in seconds 
98     binary64     8        CCD temperature in Centigrades 
106    char[]       70       Reduction software 
176    short        2        Date of creation (year) 
178    byte         1        Date of creation (month) 
179    byte         1        Date of creation (day) 
180    byte         1        Date of creation (hour) 
181    byte         1        Date of creation (minute) 
182    byte         1        Date of creation (second) 
183    byte         1        unused  
184    binary64     8        Lowest good pixel value 
192    binary64     8        Highest good pixel value 
200    binary64     8        Gain in electrons per ADU 
208    binary64     8        Readout noise in ADU 
216    binary64     8        Expected FWHM of objects 
224    binary64     8        Mean FWHM of objects 
232    binary64     8        Standard error of FWHM of objects 
240    binary64     8        Detection threshold 
248    binary64     8        Low sharpness cutoff 
256    binary64     8        High sharpness cutoff 
264    binary64     8        Low roundness cutoff 
272    binary64     8        High roundness cutoff       
280    long         4        Matching status: 0=not matched, 1=matched 
284    long         4        Number of stars used in matching 
288    long         4        Number of polygon vertices 
292    long         4        Number of matched stars   
296    binary64     8        Clipping threshold 
304    binary64     8        Offset in X axis to the ref. frame in pixels  
312    binary64     8        Offset in Y axis to the ref. frame in pixels  
320    char[]       70       Object's designation, right-padded with spaces 
390    binary64     8        Object's right ascension
398    binary64     8        Object's declination
406    char[]       70       Observer's location designation, right-padded with zeros 
476    binary64     8        Observer's longitude
484    binary64     8        Observer's latitude
492    binary64[]   48       Transformation matrix (xx, xy, x0, yx, yy, y0)
====== ============ ======== ===========

Object's right ascension - if a field value is outside range of 0 and 24, the R. A. is undefined

Object's declination - if a field value is outside range of -90 and 90, the declination is undefined 

Observer's longitude - if a field value is outside range of -360 and 360, the longitude is undefined 

Observer's latitude - if a field value is outside range of -360 and 360, the latitude is undefined 

.. rubric:: Transformation matrix

The transformation matrix holds an affine transformation, such as a scale, rotation, 
shear, or a combination of those. The transformation of a point (x, y) is given by:

.. math::
   :label: trafo

    x_new = x_x * x + x_y * y + x_0
    y_new = y_x * x + y_y * y + y_0

.. rubric:: WCS data
    
The table of apertures immediately follows the metadata block. The first four
bytes (long type) specify the length of the block in bytes, the data starts 
at offset 4. The WCS data are encoded as a FITS header. 
    
.. rubric:: Table of apertures

The table of apertures immediately follows the WCS data block. The first four
bytes (long type) specify a number of records in the table, the first record starts 
at offset 4, records have 12 bytes each. The description of fields in the table
of apertures is given in table :ref:`3 <table:pht_bin_aper>`.

.. _table:pht_bin_aper:

Table 3

====== ============ ======== ===========
Offset Type         Length   Description
====== ============ ======== ===========
0      long         4        Identifier
4      binary64     8        Radius in pixels
====== ============ ======== ===========

.. rubric:: Table of objects

The table of objects immediately follows the table of apertures. The first four
bytes (long type) specify a number of records in the table, the first record starts 
at offset 4, records have 48 bytes each. The description of fields in the table
of objects is given in table :ref:`4 <table:pht_bin_object>`.

.. _table:pht_bin_object:

Table 4

====== ============ ======== ===========
Offset Type         Length   Description
====== ============ ======== ===========
0      long         4        Identifier within a file 
4      long         4        Global identifier (assigned in matching), zero if the objects was not matched 
8      binary64     8        X-coordinate of position of the object 
16     binary64     8        Y-coordinate of position of the object
24     binary64     8        Local mean background level in ADU
32     binary64     8        Std. deviation of local background level
40     binary64     8        Full width at halt maximum (FWHM) in pixels
====== ============ ======== ===========

.. rubric:: Table of measurements

The table of measurements immediately follows the table of objects. The number
of records is not given explicitly, but it is always number of objects multiplied
by number of apertures. Objects and apertures are stored in the same order as they
appear in the tables of objects and table of apertures, respectively. The first
records that specify measurement data for the first object and aperture
stars at offset 0, the second record specify measurement data for the first object
and second aperture, etc. Each record has 8 bytes. Unlike previous fields, 
magnitude and its error is stored in 24.8 format with fixed decimal point. The
value ``0x7FFFFFFF`` means undefined value. The description of fields in the 
table of measurements is given in table :ref:`5 <table:pht_bin_body>`.

.. _table:pht_bin_body:

Table 5

====== ============ ======== ===========
Offset Type         Length   Description
====== ============ ======== ===========
0      long         4        Raw instrumental brightness in magnitudes, 8.24 fixed point format
4      long         4        Error estimation of brightness, 8.24 fixed point format
====== ============ ======== ===========
