M

Montage Montage is an astronomical image toolkit with components for reprojection, background matching, coaddition and visualization of FITS files. It can be used as a set of command-line tools (Linux, OS X and Windows), C library calls (Linux and OS X) and as Python binary extension modules.

The Montage source is written in ANSI-C and code can be downloaded from GitHub ( https://github.com/Caltech-IPAC/Montage ). The Python package can be installed from PyPI ("</i>pip install MontagePy"). The package has no external dependencies. See http://montage.ipac.caltech.edu/ for details on the design and applications of Montage.

MontagePy.main modules: mBgExec

The Montage modules are generally used as steps in a workflow to create a mosaic of a set of input images. These steps are: determine the geometry of the mosaic on the sky, reproject the images to a common frame and spatial sampling; rectify the backgrounds to a common level, and coadd the images into a mosaic. This page illustrates the use of one Montage module, mBgExec, which is used to modify the backgrounds for a set of images.

Visit Building a Mosaic with Montage to see how mBgExec is used as part of a workflow to creage a mosaic (or the one shot version if you just want to see the commands). See the complete list of Montage Notebooks here.

In [1]:
from MontagePy.main import mBgExec, mViewer

help(mBgExec)
Help on built-in function mBgExec in module MontagePy.main:

mBgExec(...)
    mBgExec takes the background correction for each image (usually from mBgModel) and subtracts it using mBackground.
    
    Parameters
    ----------
    path : str
        Path to input image directory.
    tblfile : str
        Table file list of images to correct.
    fitfile : str
        Table of background correction parameters.
    corrdir : str
        Directory for output corrected images.
    noAreas : bool, optional
        Flag indicating there are no area images.
    debug : int, optional
        Debugging output level.
    
    
    Returns
    -------
    status : int
        Return status (0: OK, 1:ERROR).
    msg : str
        Return message (for errors).
    count : int
        Total number of images.
    nocorrection : int
        Number of images with no correction parameters.
    failed : int
        Number of images where the correction failed.

mBgExec Example

If we coadd a set of reprojected 2MASS images after reprojection but without doing anything about the backgrounds, the differences in background levels are obvious:

In [3]:
from IPython.display import Image

mViewer('-color black -imginfo M17/rimages.tbl \
         -ct 1 -gray M17/uncorrected.fits -2s max gaussian-log \
         -out work/M17/uncorrected.png',
        '', mode=2)

Image(filename='work/M17/uncorrected.png')
Out[3]:

 

Through the background modeling, we can derive a set of corrections to the individual images (the "id" in this table is a reference to the images metadata table shown as an overlay above).

In [4]:
import pandas as pd
from astropy.io import ascii
ipactable = ascii.read('M17/corrections.tbl').to_pandas()
ipactable
Out[4]:
id a b c
0 0 0.000907 0.001942 -8.773330
1 8 0.000224 0.001593 -10.007300
2 9 0.001333 -0.000788 -13.112400
3 13 0.000368 0.000673 -9.036830
4 14 0.000188 0.001220 -8.722280
5 21 0.000840 0.001092 -8.212320
6 24 0.000589 0.003644 -5.954610
7 29 0.000048 0.000451 73.046300
8 31 -0.000693 0.001017 72.364000
9 1 0.000141 0.000352 -6.415340
10 6 -0.000007 0.000451 -2.633240
11 11 -0.001429 0.000924 -5.703410
12 20 0.000172 0.000463 -7.074830
13 27 0.000918 0.000861 -7.044280
14 34 0.000271 -0.000953 -3.343090
15 41 -0.000251 -0.000794 -5.701280
16 2 -0.000126 0.000716 -2.602070
17 5 -0.000621 -0.000537 -4.298660
18 18 0.001185 0.000606 -2.808010
19 25 -0.002151 -0.000163 -11.749000
20 26 -0.001034 -0.001586 -10.184300
21 28 0.000051 0.001949 0.313302
22 3 0.000102 0.001657 -8.210510
23 17 0.000139 0.002029 -7.026720
24 23 0.000299 -0.000114 -6.824260
25 38 -0.000346 -0.000761 -7.327550
26 4 -0.001596 -0.000706 -9.864560
27 15 -0.004903 0.000176 -15.105500
28 19 -0.001577 -0.002804 -7.426070
29 42 -0.006011 -0.000014 -16.471100
30 47 0.001447 0.000612 -4.191690
31 48 0.000177 0.001935 -5.128350
32 7 0.005118 -0.001551 67.571500
33 16 -0.000386 -0.005774 86.078200
34 30 0.006040 -0.000990 -19.543900
35 44 0.000025 -0.004062 -2.425410
36 45 0.001207 0.000318 -8.411490
37 10 -0.001965 0.001431 -6.796750
38 37 -0.004286 0.001074 -11.798200
39 12 -0.000845 -0.000515 -7.720550
40 46 -0.000094 0.000445 -8.694270
41 22 -0.003233 -0.002041 -10.677200
42 39 0.000799 -0.004311 -2.150530
43 40 0.001739 -0.000473 -5.173990
44 32 0.002344 -0.005776 -2.536600
45 35 -0.003949 -0.003464 -2.985680
46 36 -0.000999 -0.005700 0.289653
47 33 0.001042 -0.001365 -6.107730
48 43 0.000362 -0.002259 -1.917250

 

With mBgExec, we can apply these corrections to the original image:

In [5]:
rtn = mBgExec('M17/projected',
              'M17/pimages.tbl', 
              'M17/corrections.tbl', 
              'work/M17/corrected')
print(rtn)
{'status': '0', 'count': 49, 'nocorrection': 0, 'failed': 0}

 

and after coadding the corrected images together, we get:

In [6]:
mViewer('-color black -imginfo M17/rimages.tbl \
         -ct 1 -gray M17/mosaic.fits -2s max gaussian-log \
         -out work/mosaic_bgexec.png',
        '', mode=2)

Image(filename='work/mosaic_bgexec.png')
Out[6]:

Montage functions return JSON structures. They always include a status (0: success; 1: error) and a variable number of informational parameters.

 

mBgExec Error Handling

If mBgExec encounters an error, the return structure will just have two elements: a status of 1 ("error") and a message string that tries to diagnose the reason for the error.

For instance, if the user asks for an invalid file:

In [10]:
rtn = mBgExec('M17/projected', 
              'M17/unknown.tbl', 
              'M17/corrections.tbl', 
              "work/M17/corrected")
print(rtn)
{'status': '1', 'msg': b'Invalid image metadata file: M17/unknown.tbl'}

 

Classic Montage: mBgExec as a Stand-Alone Program

mBgExec Unix/Windows Command-line Arguments

mBgExec can also be run as a command-line tool in Linux, OS X, and Windows:

Usage: mBgExec [-p projdir] [-s statusfile] [-d] [-n(o-areas)] images.tbl corrections.tbl corrdir

 

If you are writing in C/C++, mBgExec can be accessed as a library function:

/*-*****************************************************************/
/*                                                                 */
/*  mBgExec                                                        */
/*                                                                 */
/*  Take the background correction determined for each image and   */
/*  subtract it using mBackground.                                 */
/*                                                                 */
/*   char *path       Path to images to be background-corrected    */
/*   char *tblfile    Table file list of images to correct         */
/*   char *fitfile    Table of background levels/slopes            */
/*   char *corrdir    Directory for corrected images               */
/*   int noAreas      Flag indicating there are no area images.    */
/*   int debug        Debug flag.                                  */
/*                                                                 */
/*******************************************************************/

struct mBgExecReturn *mBgExec(char *path, char *tblfile, char *fitfile, char *corrdir, int noAreas, int debug)

Return Structure

struct mBgExecReturn
{
   int    status;        // Return status (0: OK, 1:ERROR)
   char   msg [1024];    // Return message (for error return)
   char   json[4096];    // Return parameters as JSON string
   int    count;         // Number of images
   int    nocorrection;  // Number of images for which there was no correction 
   int    failed;        // Number of images where correction failed
};