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: mBgModel

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, mBgModel, which is used to model the set of background corrections needed for a set of images.

Visit Building a Mosaic with Montage to see how mBgModel 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 mBgModel, mViewer

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

mBgModel(...)
    mBgModel is a modelling/fitting program. It uses the image-to-image difference parameter table created by mFitExec to interactively determine a set of corrections to apply to each image in order to achieve a 'best' global fit.
    
    Parameters
    ----------
    input_file : str
        iReprojected image metadata list.
    fit_file : str
        Set of image overlap difference fits.
    corr_file : str
        Output table of corrections for images in input list.
    noslope : bool, optional
        Only fit levels, not slopes.
    usall : bool, optional
        Use all the input differences (by default we exclude very small overlap areas).
    niteration : int, optional
        Number of iterations to run.
    debug : int, optional
        Debugging output level.
    
    
    Returns
    -------
    status : int
        Return status (0: OK, 1:ERROR).
    msg : str
        Return message (for errors).

 

mBgModel Example

Once we have determined and fit pairwise the overlaps between images in a set, we use mBgModel to determine the corrections we should make to minimize the overall background differences. The method used is a modified least-squares fitting for the set of correction coefficients using all the pixel-to-pixel differences in the overlap sets but with several approximations invoked to speed up the computational process.

We start with a set of pairwise differences, each fit with a plane and ignoring large excursions. The idea here is that in an overlap region two images (both flux-calibrated) will differ only by the levels (and maybe a little slope) in their backgrounds. Where the background varies more extremely, background matching is likely the be futile. We have seen this happen, for instance, in 2MASS images where airglow due to blobs of gas in the Earth's atmosphere sometimes contaminate images on an size scale similar to the images.

Normally, though, these fit background differences look like this ('plus' and 'minus' refer to row IDs in the input image metadata table):

In [2]:
import pandas as pd
from astropy.io import ascii
ipactable = ascii.read('M17/fits.tbl').to_pandas()
ipactable
Out[2]:
plus minus a b c crpix1 crpix2 xmin xmax ymin ymax xcenter ycenter npixel rms boxx boxy boxwidth boxheight boxang
0 0 8 1.268790e-03 4.644410e-04 0.410733 -1711.0 1756.5 1711 1789 -1756 -935 1750.15 -1343.07 52864 7.362550e-01 1749.9 -1346.0 821.2 77.3 -90.1
1 0 9 -2.523660e-03 1.653820e-03 6.969540 -1713.0 988.5 1713 1789 -988 -733 1751.01 -863.07 16232 7.125360e-01 1751.1 -861.0 255.3 76.9 -90.2
2 0 13 2.453210e-03 3.935060e-03 0.277418 -1286.0 986.5 1286 1361 -986 -732 1323.43 -862.04 16218 7.174900e-01 1323.5 -859.5 254.0 75.0 90.0
3 0 14 -2.490360e-03 8.991680e-04 4.436860 -1283.0 1755.5 1283 1361 -1755 -933 1322.70 -1342.62 52649 7.105830e-01 1322.3 -1344.5 822.2 77.3 89.9
4 0 21 -3.303240e-10 5.376120e-09 -1.130130 -1286.0 786.5 1286 1790 -786 -732 1537.86 -759.37 27003 7.587320e-06 1538.0 -759.5 54.0 504.0 90.0
5 0 24 -1.489270e-10 -5.884170e-09 0.561656 -1284.0 1756.5 1284 1787 -1756 -1702 1535.59 -1729.23 27090 7.108770e-06 1535.5 -1729.5 54.0 503.0 90.0
6 0 29 -1.230960e-03 5.115690e-04 -79.055300 -1710.0 994.5 1710 1790 -994 -733 1749.78 -865.65 16584 7.530540e-01 1749.9 -864.0 261.3 79.3 -90.2
7 0 31 2.329150e-03 1.100920e-03 -82.140500 -1708.0 1756.5 1708 1789 -1756 -941 1748.71 -1348.03 53097 7.707710e-01 1748.8 -1349.0 815.2 79.9 89.8
8 1 6 1.756510e-04 -2.708390e-04 -3.833850 426.0 789.5 -426 -351 -789 32 -387.36 -347.45 42063 9.129390e-01 -388.5 -378.5 822.0 75.0 90.0
9 1 11 -2.720230e-03 -5.054520e-04 -0.537130 -2.0 789.5 2 78 -789 31 40.09 -390.72 47921 8.727160e-01 40.0 -379.0 821.0 76.0 90.0
10 1 20 -5.109730e-19 -2.983720e-16 0.763428 425.0 789.5 -425 78 -789 -736 -172.39 -763.09 22413 4.641070e-15 -173.5 -763.0 53.0 503.0 90.0
11 1 27 1.084200e-19 -5.074070e-17 0.691162 426.0 -180.5 -426 77 180 233 -174.68 207.06 23318 7.921560e-16 -174.5 207.0 53.0 503.0 90.0
12 1 34 -4.281630e-03 6.650880e-04 -4.570030 426.0 20.5 -426 -351 -20 233 -388.07 106.65 14652 8.112380e-01 -388.5 106.5 254.0 75.0 90.0
13 1 41 -9.965350e-03 3.869090e-03 -0.521526 -2.0 21.5 2 77 -21 233 39.46 110.87 15314 9.693480e-01 39.5 106.0 255.0 75.0 90.0
14 2 5 5.910760e-03 9.426410e-04 5.666030 854.0 1757.5 -854 -777 -1757 -937 -815.22 -1354.16 49825 7.697600e-01 -815.4 -1347.5 820.1 76.4 90.1
15 2 6 -7.671010e-04 1.611060e-03 0.543132 854.0 990.5 -854 -778 -990 -734 -815.57 -864.23 14285 9.272300e-01 -816.0 -862.5 256.0 76.0 90.0
16 2 18 4.852270e-10 3.118610e-09 1.600290 1282.0 788.5 -1282 -778 -788 -734 -1029.37 -761.30 26871 7.403790e-06 -1030.0 -761.5 54.0 504.0 90.0
17 2 25 -3.896340e-04 9.108010e-04 6.162890 1281.0 1758.5 -1281 -1238 -1758 -933 -1260.25 -1335.75 24848 8.814390e-01 -1260.0 -1346.0 825.1 42.7 -89.9
18 2 26 6.081620e-03 1.740740e-03 13.644000 1282.0 986.5 -1282 -1240 -986 -735 -1260.26 -864.96 7125 1.006160e+00 -1261.0 -861.0 251.0 42.0 90.0
19 2 28 -3.959880e-20 -1.304840e-16 -0.597885 1280.0 1758.5 -1280 -777 -1758 -1704 -1028.23 -1731.35 24404 2.032750e-15 -1028.5 -1731.5 54.0 503.0 90.0
20 3 11 2.111740e-03 8.402230e-04 -2.689850 -431.0 784.5 431 505 -784 31 468.00 -378.34 50863 7.444600e-01 468.0 -376.5 816.0 74.0 90.0
21 3 13 3.627310e-03 1.314690e-03 -2.519670 -858.0 784.5 858 935 -784 37 896.00 -382.34 50308 7.361090e-01 896.5 -373.5 822.1 76.1 -90.1
22 3 17 2.483370e-10 -1.152390e-09 -0.965677 -431.0 784.5 431 934 -784 -731 682.61 -757.99 27199 7.623260e-06 682.5 -758.0 53.0 503.0 90.0
23 3 23 -3.036630e-11 1.690620e-09 -1.258960 -432.0 -185.5 432 935 185 238 683.50 212.00 27216 6.063610e-06 683.5 212.0 53.0 503.0 90.0
24 3 38 3.505620e-03 1.811340e-04 -3.260770 -860.0 15.5 860 935 -15 238 897.12 111.32 15496 7.787990e-01 897.5 111.5 254.0 75.0 90.0
25 3 41 5.486890e-03 1.928290e-03 -4.866100 -431.0 21.5 431 505 -21 238 468.22 108.40 15892 7.351890e-01 468.0 108.5 260.0 74.0 90.0
26 4 15 4.008290e-03 -9.678930e-04 6.493410 1747.0 -183.5 -1747 -1672 183 1005 -1708.71 591.93 43190 9.386600e-01 -1709.7 594.6 822.0 73.7 -89.9
27 4 18 -3.352750e-04 -2.270560e-03 -3.924300 1283.0 16.5 -1283 -1242 -16 234 -1262.86 106.53 7062 1.012440e+00 -1262.5 109.0 251.0 41.0 90.0
28 4 19 1.435180e-16 -1.116540e-14 -0.190674 1747.0 -952.5 -1747 -1244 974 983 -1493.10 978.90 47 3.698180e-16 -1495.5 979.5 54.0 503.0 90.0
29 4 26 2.168400e-18 4.445230e-18 1.108510 1745.0 17.5 -1745 -1242 -17 36 -1494.00 9.26 24954 3.432580e-16 -1493.5 9.5 54.0 503.0 90.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
98 25 37 2.993400e-03 -9.639940e-04 1.930760 1743.0 1756.5 -1743 -1667 -1756 -934 -1704.44 -1368.87 43558 9.570520e-01 -1705.1 -1345.5 822.1 75.0 90.1
99 26 37 2.132510e-03 -7.878530e-04 1.137900 1743.0 987.5 -1743 -1669 -987 -732 -1704.94 -856.26 13175 8.965520e-01 -1706.0 -860.0 255.0 74.0 90.0
100 26 42 4.095660e-03 -1.870220e-03 4.653600 1745.0 786.5 -1745 -1670 -786 35 -1705.76 -365.01 44054 8.743800e-01 -1707.4 -375.5 822.0 74.3 90.1
101 27 33 2.527970e-18 -4.167670e-16 1.673900 426.0 -1150.5 -426 77 1150 1203 -173.40 1177.04 21881 6.513750e-15 -174.5 1177.0 53.0 503.0 90.0
102 27 34 3.071140e-03 1.595790e-03 -2.654820 426.0 -180.5 -426 -351 180 1002 -389.66 570.28 44329 9.364340e-01 -388.5 591.5 822.0 75.0 90.0
103 27 35 1.628030e-03 1.117010e-03 -0.391749 -3.0 -948.5 3 77 948 1203 40.62 1076.31 12992 1.003060e+00 40.0 1076.0 255.0 74.0 90.0
104 27 41 2.676220e-03 1.519470e-03 -1.326430 -2.0 -180.5 2 77 180 1001 40.10 570.35 46199 8.684200e-01 39.5 591.0 821.0 75.0 90.0
105 27 43 6.693140e-03 3.815910e-03 -3.391830 426.0 -949.5 -426 -352 949 1203 -390.89 1078.05 12730 1.083480e+00 -389.0 1076.5 254.0 74.0 90.0
106 29 30 -6.003610e-03 7.217340e-03 92.591200 -1716.0 18.5 1716 1798 -18 28 1757.75 4.26 3000 9.457370e-01 1757.0 5.0 47.0 82.0 90.0
107 29 31 3.228370e-09 3.008890e-09 2.299610 -1710.0 994.5 1710 1798 -994 -941 1754.05 -968.01 4791 7.628790e-06 1754.0 -968.0 53.0 88.0 90.0
108 30 44 1.178090e-15 1.691360e-17 -3.227810 -1718.0 -951.5 1718 1798 951 1004 1757.89 978.04 3747 2.755180e-14 1758.0 978.0 53.0 80.0 90.0
109 30 45 5.933540e-03 -1.243340e-03 -13.183500 -1716.0 -183.5 1716 1793 183 1004 1754.93 607.41 46514 8.592740e-01 1754.9 594.0 821.2 76.3 -90.2
110 32 35 1.212370e-03 -2.488770e-03 2.879520 -433.0 -1155.5 433 506 1155 1797 468.94 1573.64 27217 9.886340e-01 469.5 1476.5 642.0 73.0 90.0
111 32 36 -4.557880e-03 2.532880e-03 0.350670 -862.0 -1155.5 862 936 1155 1797 899.31 1465.39 37138 8.199690e-01 899.0 1476.5 642.0 74.0 90.0
112 33 35 5.023290e-03 2.110940e-03 -3.150380 -3.0 -1150.5 3 77 1150 1797 40.41 1487.34 35991 8.733230e-01 40.0 1474.0 647.0 74.0 90.0
113 33 43 3.743590e-03 1.023310e-03 -3.199060 426.0 -1150.5 -426 -352 1150 1797 -389.43 1485.53 33026 9.699490e-01 -389.0 1474.0 647.0 74.0 90.0
114 34 43 9.529120e-20 -1.970540e-17 -0.217102 855.0 -949.5 -855 -352 949 1002 -603.82 976.05 24455 3.088130e-16 -603.5 976.0 53.0 503.0 90.0
115 34 47 -5.801320e-04 -1.266650e-03 1.172310 855.0 -182.5 -855 -780 182 1002 -817.62 588.56 45873 8.761660e-01 -817.5 592.5 820.0 75.0 90.0
116 35 41 4.690440e-19 1.317850e-16 -0.366928 -3.0 -948.5 3 506 948 1001 254.91 974.98 26986 2.055550e-15 254.5 975.0 53.0 503.0 90.0
117 36 38 2.612920e-10 -1.019570e-09 1.721730 -861.0 -953.5 861 1364 953 1007 1111.91 980.63 26937 7.228180e-06 1112.5 980.5 54.0 503.0 90.0
118 36 39 -5.956260e-04 -7.872360e-04 -0.036234 -1291.0 -1154.5 1291 1366 1154 1797 1327.21 1483.97 36016 9.384890e-01 1328.3 1476.0 643.1 74.5 -90.1
119 36 45 -5.363510e-03 -4.439150e-03 11.325300 -1290.0 -953.5 1290 1365 953 1207 1327.66 1080.66 13662 9.161630e-01 1327.6 1080.5 254.4 74.4 89.7
120 37 42 1.569380e-17 9.454240e-17 0.765747 1801.0 786.5 -1801 -1670 -786 -732 -1735.80 -759.64 5746 1.590380e-15 -1735.5 -759.5 54.0 131.0 90.0
121 38 45 -3.689510e-03 -1.696570e-03 4.205390 -1289.0 -184.5 1289 1364 184 1006 1326.44 596.48 48646 8.512290e-01 1326.5 595.5 822.0 75.0 90.0
122 39 44 2.348420e-03 4.838670e-04 -3.615190 -1718.0 -1153.5 1718 1795 1153 1797 1756.99 1475.15 39354 7.699550e-01 1756.8 1475.5 644.3 76.8 -90.2
123 39 45 3.354750e-10 2.823240e-09 0.253280 -1291.0 -1153.5 1291 1794 1153 1207 1542.22 1180.83 27010 6.472080e-06 1542.5 1180.5 54.0 503.0 90.0
124 40 43 -1.941730e-03 1.361570e-03 -5.333800 856.0 -1152.5 -856 -781 1152 1797 -817.98 1464.43 34937 9.498680e-01 -818.3 1475.0 645.2 74.7 90.1
125 40 47 -7.749280e-11 2.229940e-09 -2.566560 1284.0 -1151.5 -1284 -781 1151 1205 -1032.04 1178.82 26883 7.354740e-06 -1032.5 1178.5 54.0 503.0 90.0
126 43 47 -8.581890e-04 -3.095250e-03 2.637200 855.0 -949.5 -855 -781 949 1205 -818.16 1078.88 14308 8.889450e-01 -818.0 1077.5 256.0 74.0 90.0
127 44 45 1.303560e-03 -1.999610e-03 -0.969453 -1718.0 -951.5 1718 1794 951 1206 1756.03 1080.04 14963 7.546190e-01 1756.0 1079.0 255.0 76.0 90.0

128 rows × 20 columns

mBgModel uses this data to model the corrections for the individual IDs:

In [3]:
rtn = mBgModel('M17/pimages.tbl', 'M17/fits.tbl', 'work/M17/corrections.tbl')

print(rtn)
{'status': '0'}
In [4]:
ipactable = ascii.read('work/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

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

mBgModel Error Handling

If mBgModel 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 table file:

In [5]:
rtn = mBgModel('M17/unknown.tbl', 'M17/fits.tbl', 'work/M17/corrections.tbl')

print(rtn)
{'status': '1', 'msg': b'Invalid image metadata file: M17/unknown.tbl'}

 

Classic Montage: mBgExec as a Stand-Alone Program

mBgModel Unix/Windows Command-line Arguments

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

Usage: mBgModel [-i niter] [-l(evel-only)] [-d level] [-a(ll-overlaps)] [-s statusfile] images.tbl fits.tbl corrections.tbl

 

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

/*-***********************************************************************/
/*                                                                       */
/*  mBModel                                                              */
/*                                                                       */
/*  Given a set of image overlap difference fits (parameters on the      */
/*  planes fit to pairwise difference images between adjacent images)    */
/*  interatively determine the 'best' background adjustment for each     */
/*  image (assuming each image is changed to best match its neighbors    */
/*  with them remaining unchanged) uses these adjustments to modify      */
/*  each set of difference parameters, and iterate until the changes     */
/*  modifications become small enough.                                   */
/*                                                                       */
/*   char  *imgfile        Reprojected image metadata list               */
/*   char  *fitfile        Set of image overlap difference fits          */
/*   char  *corrtbl        Output table of corrections for images        */
/*                         in input list                                 */
/*   int    noslope        Only fit levels, not slopes                   */
/*   int    useall         Use all the input differences (by default     */
/*                         we exclude very small overlap areas)          */
/*   int    niteration     Number of iterations to run                   */
/*   int    debug          Debugging output level                        */
/*                                                                       */
/*************************************************************************/

struct mBgModelReturn *mBgModel(char *imgfile, char *fitfile, char *corrtbl, int noslope, int useall, int niter, int debug)

Return Structure

struct mBgModelReturn
{
   int    status;        // Return status (0: OK, 1:ERROR)
   char   msg [1024];    // Return message (for error return)
   char   json[4096];    // Return parameters as JSON string
};