コード例 #1
0
    def makeFirstRefList(self, labelFile, scale=0.00995):
        """
        Read in a label.dat file specificed in <labelFile> and convert
        it into a starfinder *_rms.lis file. Coordinates in label.dat 
        are assumed to be in arcseconds and they will be converted to
        NIRC2 narrow pixels using <scale>. Sgr A* will be centered at
        x=2000, y=2000 in the output list. 

        labelFile - (string) The name of the label.dat file.
        scale - (float) The pixels scale in arcsec/pixel.
        """
        
        labels = starTables.Labels(labelFile=labelFile)
        starlist = starTables.StarfinderList(None, hasErrors=True)

        # We need to pull out the date of this epoch. Pull this from the
        # first image starlist in the mosaic.
        firstList = self.dataDir + self.images[0] + '_rms.lis'
        tmpList = starTables.StarfinderList(firstList)
        date = tmpList.epoch[0]

        # Trim label list to only stars with use? != 0
        idx = np.where(labels.useToAlign == 1)[0]
        labels.take(idx)

        # Convert label velocities to arcsec/yr instead of mas/yr
        labels.vx /= 1000.0
        labels.vy /= 1000.0
        labels.vxerr /= 1000.0
        labels.vyerr /= 1000.0

        starlist.name = labels.name
        starlist.mag = labels.mag
        starlist.epoch = np.zeros(len(labels.name), dtype=float)
        starlist.epoch += date
        
        dt = date - labels.t0
        starlist.x = ((labels.x + labels.vx * dt) / -scale) + 2000.0
        starlist.y = ((labels.y + labels.vy * dt) / scale) + 2000.0
        starlist.xerr = np.sqrt(labels.xerr**2 + (labels.vxerr * dt)**2)
        starlist.xerr /= scale
        starlist.yerr = np.sqrt(labels.yerr**2 + (labels.vyerr * dt)**2)
        starlist.yerr /= scale
        starlist.snr = np.ones(len(labels.name))
        starlist.corr = np.ones(len(labels.name))
        starlist.nframes = np.ones(len(labels.name))
        starlist.counts = np.ones(len(labels.name))

        outFile = self.lisDir + labelFile.split('/')[-1]
        outFile = outFile.replace('.dat', '_rms.lis')
        print 'Saving to %s' % outFile

        starlist.saveToFile(outFile)
コード例 #2
0
    def name_new_stars(self, oldNames):
        mosaicRoot = 'mag' + self.epoch +'_'+ self.mosaic +'_'+ self.filter
        starlist = self.tableDir + mosaicRoot + '_rms_named_abs_xwest.lis'
        labelNewFile = self.tableDir+'/label_new.dat'

        labels = starTables.Labels(labelFile=labelNewFile)
        list = starTables.StarfinderList(starlist, hasErrors=True)

        # Go through the existing named sources and figure out the highest
        # star index number.
        highestIndex = {}
        for ii in range(len(labels.name)):
            name = labels.name[ii]

            if name.startswith('S') and '-' in name:
                parts = name.split('-')

                radius = int(parts[0].replace('S', ''))
                index = int(parts[1])

                if highestIndex.has_key(radius):
                    if index > highestIndex[radius]:
                        highestIndex[radius] = index
                else:
                    highestIndex[radius] = index

        # Work with the starlist
        list.x *= -1.0
        list.r = np.hypot(list.x, list.y)

        for ii in range(len(oldNames)):
            idx = np.where(list.name == oldNames[ii])[0]

            rBin = int(math.floor( list.r[idx] ))

            highestIndex[rBin] += 1
            newName = 'S%d-%d' % (rBin, highestIndex[rBin])

            print '%-11s  %4.1f   %8.4f  %8.4f   %8.4f  %8.4f      0.000    0.000    0.000    0.000   %8.3f    0  %6.3f' % \
                (newName, list.mag[idx], list.x[idx], list.y[idx], 
                 list.xerr[idx], list.yerr[idx], list.epoch[idx], list.r[idx])
コード例 #3
0
    def convertToAbsolute(self):
        """
        Make a sham starlist in arcseconds with +x to the west. This is
        for Tuan's data analysis.
        """
        mosaicRoot = 'mag' + self.epoch +'_'+ self.mosaic +'_'+ self.filter
        starlist = self.tableDir + mosaicRoot + '_rms_named.lis'

        lis = starTables.StarfinderList(starlist, hasErrors=True)
        labels = starTables.Labels()

        # Convert the coordinates to arcseconds
        xpix = lis.x
        ypix = lis.y
        xpixerr = lis.xerr
        ypixerr = lis.yerr

        # Find 16C in both lists
        lis16C = np.where(lis.name == 'irs16C')
        lab16C = np.where(labels.name == 'irs16C')

        scale = 0.00995
        x = (xpix - xpix[lis16C]) * scale * -1.0
        x += labels.x[lab16C]
        x *= -1.0

        y = (ypix - ypix[lis16C]) * scale
        y += labels.y[lab16C]

        xe = xpixerr * scale
        ye = ypixerr * scale

        lis.x = x
        lis.y = y
        lis.xerr = xe
        lis.yerr = ye

        lis.saveToFile(starlist.replace('.lis', '_abs_xwest.lis'))

        shutil.copyfile(self.tableDir + mosaicRoot + '_rms_named_abs_xwest.lis', 
                        self.dataDir + mosaicRoot +  '_rms_named_abs_xwest.lis')
コード例 #4
0
def update_labels(labelFile):
    # Read in the label.dat file
    lab = starTables.Labels(labelFile=labelFile)

    # Create a connection to the database
    connection = sqlite.connect(dbfile)

    # Create a cursor object
    cur = connection.cursor()

    # Loop through all the stars and insert or replace them
    for ss in range(len(lab.name)):
        sql = 'insert or replace into stars '
        sql += '(name, kp, x, y, xerr, yerr, t0, '
        sql += 'vx, vy, vxerr, vyerr, useToAlign) '
        sql += 'values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'

        cur.execute(
            sql,
            (lab.name[ss], lab.mag[ss], lab.x[ss], lab.y[ss], lab.xerr[ss],
             lab.yerr[ss], lab.t0[ss], lab.vx[ss], lab.vy[ss], lab.vxerr[ss],
             lab.vyerr[ss], lab.useToAlign[ss]))

    connection.commit()
コード例 #5
0
def gcimf_completeness():
    """
    Make a completeness curve for every osiris spectroscopic field. Use
    the completeness analysis from mag06maylgs1_dp_msc.
    """
    # Keep a mapping of which OSIRIS field corresponds to which
    # NIRC2 field in the dp_msc.
    osiris2nirc2 = {
        'GC Central': 'C',
        'GC East': 'C',
        'GC South': 'C',
        'GC Southeast': 'C',
        'GC Southwest': 'C',
        'GC West': 'C',
        'GC Northwest': 'C',
        'GC North': 'C',
        'GC Northeast': 'C',
        'E2-1': 'C_NE',
        'E2-2': 'C_SE',
        'E2-3': 'C_SE',
        'E3-1': 'E',
        'E3-2': 'E',
        'E3-3': 'E',
        'E4-1': 'E',
        'E4-2': 'E',
        'E4-3': 'E'
    }

    # Load up the label.dat file and use the coordinates in there
    # to astrometrically calibrate each NIRC2 dp_msc field.
    label = starTables.Labels()

    # Load up the spectroscopic database to get the field-of-view definitions.
    dbfile = '/u/jlu/data/gc/database/stars.sqlite'

    connection = sqlite.connect(dbfile)
    cur = connection.cursor()
    cur.execute('select name, x_vertices, y_vertices from fields')
    rows = cur.fetchall()

    # Completeness Directory
    compDir = '/u/jlu/work/gc/dp_msc/2010_09_17/completeness'

    # NIRC2 Data directory
    nirc2Dir = '/u/ghezgroup/data/gc/06maylgs1/combo'

    for rr in range(len(rows)):
        fieldName = rows[rr][0]
        print 'Working on field %s' % fieldName

        xverts = np.array([float(ff) for ff in rows[rr][1].split(',')])
        yverts = np.array([float(ff) for ff in rows[rr][2].split(',')])

        xyverts = np.column_stack((xverts, yverts))

        # Load up the corresponding NIRC2 completeness starlists
        nircFieldName = osiris2nirc2[fieldName]
        alignDir = '%s/%s/kp/06maylgs1/align/' % \
            (compDir, nircFieldName)

        cData = starPlant.load_completeness(alignDir)

        # Load up information on the coo star for this field
        cooFile = '%s/mag06maylgs1_dp_msc_%s_kp.coo' % (nirc2Dir,
                                                        nircFieldName)
        cooTmp = open(cooFile).readline().split()
        cooPix = [float(cooTmp[0]), float(cooTmp[1])]

        idx = np.where(label.name == cooStarDict[nircFieldName])[0][0]
        cooAsec = [label.x[idx], label.y[idx]]

        scale = 0.00995

        # Convert the completeness results to absolute coordinates
        cData.x_in = ((cData.x_in - cooPix[0]) * scale * -1) + cooAsec[0]
        cData.y_in = ((cData.y_in - cooPix[1]) * scale) + cooAsec[1]
        cData.x_out = ((cData.x_out - cooPix[0]) * scale * -1) + cooAsec[0]
        cData.y_out = ((cData.y_out - cooPix[1]) * scale) + cooAsec[1]
        cData.x = ((cData.x - cooPix[0]) * scale * -1) + cooAsec[0]
        cData.y = ((cData.y - cooPix[0]) * scale * -1) + cooAsec[0]

        # Now trim down to just those pixels that are within this
        # OSIRIS field of view
        xypoints = np.column_stack((cData.x_in, cData.y_in))
        inside = nxutils.points_inside_poly(xypoints, xyverts)
        inside = np.where(inside == True)[0]

        # Measure the completeness for this OSIRIS field. Also print it
        # out into a file.
        outRoot = 'completeness_%s' % fieldName.replace(' ', '')
        completeness = np.zeros(len(cData.mag), dtype=float)
        _comp = open(outRoot + '.dat', 'w')
        for mm in range(len(cData.mag)):
            # All planted stars in this field
            planted = np.where(cData.m_in[inside] == cData.mag[mm])[0]

            # All detected stars in this field
            detected = np.where(cData.f_out[inside][planted] > 0)[0]

            completeness[mm] = float(len(detected)) / float(len(planted))

            _comp.write(
                '%5.2f  %6.3f  %3d  %3d\n' %
                (cData.mag[mm], completeness[mm], len(planted), len(detected)))

        py.clf()
        py.plot(cData.mag, completeness, 'k.-')
        py.xlabel('Kp Magnitude')
        py.ylabel('Completeness')
        py.title('OSIRIS Field %s, NIRC2 Field %s' %
                 (fieldName, nircFieldName))
        py.ylim(0, 1.1)
        py.xlim(9, 20)
        py.savefig(outRoot + '.png')
コード例 #6
0
def plot_young_stars():
    # Load up the spectroscopic database to get the field-of-view definitions.
    dbfile = '/u/jlu/data/gc/database/stars.sqlite'

    connection = sqlite.connect(dbfile)
    cur = connection.cursor()
    cur.execute('select name, x, y, kp from stars where young="T"')
    yngRows = cur.fetchall()

    cur.execute('select name, x, y, kp from stars where old="T"')
    oldRows = cur.fetchall()

    cur.execute('select name, x_vertices, y_vertices from fields')
    regions = cur.fetchall()

    fieldNames = []
    xverts = []
    yverts = []
    for rr in range(len(regions)):
        fieldNames.append(regions[rr][0])
        xverts.append(np.array([float(ff)
                                for ff in regions[rr][1].split(',')]))
        yverts.append(np.array([float(ff)
                                for ff in regions[rr][2].split(',')]))

    # Load up the 06maylgs1 mosaic image
    imageRoot = '/u/jlu/work/gc/imf/gcows/'
    imageRoot += 'mag06maylgs1_dp_msc_kp'
    img = pyfits.getdata(imageRoot + '.fits')

    # Coo star is IRS 16C
    coords = open(imageRoot + '.coo').readline().split()
    cooPix = [float(coords[0]), float(coords[1])]

    # Load up the label.dat file to get the coordinates in arcsec
    label = starTables.Labels()
    idx = np.where(label.name == 'irs16C')[0]
    cooAsec = [label.x[idx[0]], label.y[idx[0]]]

    scale = 0.00995

    xmin = ((0 - cooPix[0]) * scale * -1.0) + cooAsec[0]
    xmax = ((img.shape[1] - cooPix[0]) * scale * -1.0) + cooAsec[0]
    ymin = ((0 - cooPix[1]) * scale) + cooAsec[1]
    ymax = ((img.shape[0] - cooPix[1]) * scale) + cooAsec[1]
    extent = [xmin, xmax, ymin, ymax]

    # Loop through the stars and make arrays
    numYng = len(yngRows)
    numOld = len(oldRows)

    yngName = np.zeros(numYng, dtype='S13')
    yngX = np.zeros(numYng, dtype=float)
    yngY = np.zeros(numYng, dtype=float)
    yngKp = np.zeros(numYng, dtype=float)

    oldName = np.zeros(numOld, dtype='S13')
    oldX = np.zeros(numOld, dtype=float)
    oldY = np.zeros(numOld, dtype=float)
    oldKp = np.zeros(numOld, dtype=float)

    for yy in range(len(yngRows)):
        yngName[yy] = yngRows[yy][0]
        yngX[yy] = yngRows[yy][1]
        yngY[yy] = yngRows[yy][2]
        yngKp[yy] = yngRows[yy][3]

    for oo in range(len(oldRows)):
        oldName[oo] = oldRows[oo][0]
        oldX[oo] = oldRows[oo][1]
        oldY[oo] = oldRows[oo][2]
        oldKp[oo] = oldRows[oo][3]

    print extent
    py.clf()
    py.imshow(img, extent=extent, cmap=py.cm.gray_r, vmin=0, vmax=10000)
    py.plot(yngX,
            yngY,
            'bo',
            markerfacecolor='none',
            markersize=10,
            markeredgecolor='blue',
            markeredgewidth=2)
    py.plot(oldX,
            oldY,
            'ro',
            markerfacecolor='none',
            markersize=10,
            markeredgecolor='red',
            markeredgewidth=2)
    for yy in range(numYng):
        txt = '%s, Kp=%5.2f' % (yngName[yy], yngKp[yy])
        #         txt = '%5.2f' % (yngKp[yy])
        py.text(yngX[yy], yngY[yy], txt, color='blue')
    py.axis('tight')
    py.axis('equal')

    for ff in range(len(fieldNames)):
        py.plot(np.append(xverts[ff], xverts[ff][0]),
                np.append(yverts[ff], yverts[ff][0]),
                color='black')
コード例 #7
0
def plotStarfinderList(
        starList,
        hasErrors=True,
        magCut=18,
        cooStarList='16C',
        cooStarLabels='irs16C',
        scaleList=0.00995,
        scaleImg=0.00995,
        image='/u/ghezgroup/data/gc/06maylgs1/combo/mag06maylgs1_dp_msc_kp.fits'
):
    """
    Plot the specified image and overlay the photo_calib.dat sources on top.
    Coordinates are converted from pixels to arcsec using the coo star and
    assuming that the angle of the image is 0.

    This code assumes coordinates are NIRC2 narrow pixels coordaintes. You can modify this by 
    changing the scale. But +x must be to the west in the starlist.
    """

    # Load up the photometric calibraters table.
    lis = starTables.StarfinderList(starList, hasErrors=hasErrors)
    labels = starTables.Labels()

    # Find the coo star in the starlist and in the labels
    ii1 = np.where(lis.name == cooStarList)[0]
    ii2 = np.where(labels.name == cooStarLabels)[0]

    dt = lis.epoch[0] - labels.t0
    labels.x += (labels.vx / 10**3) * dt
    labels.y += (labels.vy / 10**3) * dt

    # Convert the pixels in the starlist into arcsec
    x = ((lis.x - lis.x[ii1]) * -scaleList) + labels.x[ii2]
    y = ((lis.y - lis.y[ii1]) * scaleList) + labels.y[ii2]

    im = pyfits.getdata(image)
    imgsize = (im.shape)[0]

    # pixel position (0,0) is at upper left
    xpix = np.arange(0, im.shape[0], dtype=float)
    ypix = np.arange(0, im.shape[1], dtype=float)

    # Read in the image coo file
    # Coo star pixel coordinates
    _coo = open(image.replace('.fits', '.coo'), 'r')
    coordsTmp = _coo.readline().split()
    coords = [float(coo) for coo in coordsTmp]
    print 'Coordinates for %s:' % cooStarLabels
    print '  [%10.4f, %10.4f] pixels' % (coords[0], coords[1])
    print '  [%10.4f, %10.4f] arcsec' % (labels.x[ii2], labels.y[ii2])

    sgrax = coords[0] - (labels.x[ii2] / -scaleImg)
    sgray = coords[1] - (labels.y[ii2] / scaleImg)
    sgra = [sgrax, sgray]

    # Image coordinates (in arcsec)
    xim = (xpix - sgra[0]) * -scaleImg
    yim = (ypix - sgra[1]) * scaleImg

    py.clf()
    py.close(2)
    py.figure(2, figsize=(6, 4.5))
    py.grid(True)
    py.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
    py.imshow(np.log10(im),
              extent=[xim[0], xim[-1], yim[0], yim[-1]],
              aspect='equal',
              vmin=1.9,
              vmax=6.0,
              cmap=py.cm.gray)
    py.xlabel('X Offset from Sgr A* (arcsec)')
    py.ylabel('Y Offset from Sgr A* (arcsec)')
    py.title('UCLA/Keck Galactic Center Group', fontsize=20, fontweight='bold')
    thePlot = py.gca()

    py.axis([15, -15, -15, 15])

    idx = (np.where((lis.mag < magCut) & (x > xim.min()) & (x < xim.max())
                    & (y > yim.min()) & (y < yim.max())))[0]

    py.plot(x[idx], y[idx], 'r+', color='orange')
    for ii in idx:
        py.text(x[ii], y[ii], lis.name[ii], color='orange', fontsize=10)
コード例 #8
0
def plotLabelAndAlign(alignRoot,
                      polyRoot,
                      magCut=25,
                      labelFile='label.dat',
                      showStars=True):

    label = starTables.Labels(labelFile)
    label.take(np.where(label.mag < magCut)[0])

    t = 2005.580
    x_lab = label.x + label.vx * (t - label.t0) / 10**3
    y_lab = label.y + label.vy * (t - label.t0) / 10**3
    n_lab = label.ourName
    use = label.useToAlign

    # Now read in the align stuff
    s = starset.StarSet(alignRoot)
    s.loadPolyfit(polyRoot, accel=0)
    mag = s.getArray('mag')
    idx = np.where(mag < magCut)[0]
    s.stars = [s.stars[ii] for ii in idx]

    n_aln = np.array(s.getArray('name'))
    mag = s.getArray('mag')
    x0 = s.getArray('fitXv.p') * -1.0
    y0 = s.getArray('fitYv.p')
    x0err = s.getArray('fitXv.perr')
    y0err = s.getArray('fitYv.perr')
    vx = s.getArray('fitXv.v') * -1.0
    vy = s.getArray('fitYv.v')
    vxerr = s.getArray('fitXv.verr')
    vyerr = s.getArray('fitYv.verr')
    t0x = s.getArray('fitXv.t0')
    t0y = s.getArray('fitYv.t0')
    r = np.hypot(x0, y0)

    x_aln = x0 + vx * (t - t0x)
    y_aln = y0 + vy * (t - t0y)

    # Fix x0err, y0err to some minimum value
    idx = np.where(x0err < 0.0001)[0]
    x0err[idx] = 0.0001
    idx = np.where(y0err < 0.0001)[0]
    y0err[idx] = 0.0001

    # First lets update the 16 sources since they are used
    # to align label.dat to the reference epoch (crude I know)
    names16 = [
        'irs16C', 'irs16NW', 'irs16CC', 'irs16NE', 'irs16SW', 'irs16SW-E',
        'irs33N', 'irs33E'
    ]
    for nn in range(len(names16)):
        idx = np.where(n_aln == names16[nn])[0]

        for rr in idx:
            print '%-11s  %4.1f    %6.3f  %6.3f  %7.4f %7.4f  %8.3f %8.3f  %7.3f  %7.3f   %8.3f    1   %5.3f' % \
                  (n_aln[rr], mag[rr], x0[rr], y0[rr], x0err[rr],
                   y0err[rr], vx[rr]*10**3, vy[rr]*10**3,
                   vxerr[rr]*10**3, vyerr[rr]*10**3, t0x[rr], r[rr])

    # Image
    imageFile = '/u/ghezgroup/data/gc/'
    imageFile += '06maylgs1/combo/mag06maylgs1_dp_msc_kp.fits'
    im = pyfits.getdata(imageFile)
    imgsize = (im.shape)[0]

    # pixel position (0,0) is at upper left
    xpix = np.arange(0, im.shape[0], dtype=float)
    ypix = np.arange(0, im.shape[1], dtype=float)

    sgra = [1422.6, 1543.8]
    scale_jpg = 0.00994
    xim = (xpix - sgra[0]) * scale_jpg * -1.0
    yim = (ypix - sgra[1]) * scale_jpg

    # Lets also make some coordinates for compass rose and scale bar
    xrose = np.array([-15.5, -15.5])
    yrose = np.array([15.5, 15.5])
    xroseLen = np.array([20.0, 0.0])
    yroseLen = np.array([0.0, 20.0])

    py.clf()
    py.close(1)
    py.close(2)

    def drawImage(xlo, xhi, ylo, yhi):
        py.figure(2, figsize=(9, 9))
        py.grid(True)
        py.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
        py.imshow(np.log10(im),
                  extent=[xim[0], xim[-1], yim[0], yim[-1]],
                  aspect='equal',
                  vmin=2.0,
                  vmax=4.0,
                  cmap=py.cm.gray)
        py.xlabel('X Offset from Sgr A* (arcsec)')
        py.ylabel('Y Offset from Sgr A* (arcsec)')
        py.title('UCLA/Keck Galactic Center Group',
                 fontsize=20,
                 fontweight='bold')
        thePlot = py.gca()

        # Plot label.dat points
        py.plot(x_lab, y_lab, 'rs')
        for ii in range(len(x_lab)):
            py.text(x_lab[ii], y_lab[ii], n_lab[ii], color='red', fontsize=10)

        # Plot align points
        py.plot(x_aln, y_aln, 'bo')
        for ii in range(len(x_aln)):
            py.text(x_aln[ii], y_aln[ii], n_aln[ii], color='blue', fontsize=10)

        py.axis([xlo, xhi, ylo, yhi])

    # We can do some matching here to help us out.
    drawImage(15, -15, -15, 15)

    if showStars:
        fmt = '%-11s   %4.1f   %9.5f %9.5f   %8.5f  %8.5f  %8.3f %8.3f  %7.3f  %7.3f   %8.3f    %1d   %5.3f\n'
        gcutil.rmall(['label_updated.dat'])
        out = open('label_updated.dat', 'w')

        out.write(
            '#Name           K         x       y           xerr      yerr       vx       vy    vxerr    vyerr         t0  use?    r2d\n'
        )
        out.write(
            '#()         (mag)      (asec)    (asec)     (asec)    (asec)  (mas/yr) (mas/yr) (mas/yr) (mas/yr)     (year)   ()  (asec)\n'
        )
        foo = raw_input('Continue?')
        for ss in range(len(x_lab)):
            update = 0

            idx = np.where(n_aln == n_lab[ss])[0]

            # The rest of the code allows us to find stars that were not matched
            # between the old label and new absolute alignment.
            # This will write out the new info from absolute alignment to a new
            # label_updated.dat file for just those stars that weren't matched properly,
            # but still need to run updateLabelInfoWithAbsRefs() to update the info
            # for stars that were matched.
            if len(idx) == 0:
                dr = np.hypot(x_aln - x_lab[ss], y_aln - y_lab[ss])

                rdx = np.where(dr < 0.2)[0]

                if len(rdx) > 0:
                    xlo = x_lab[ss] + 0.2
                    xhi = x_lab[ss] - 0.2
                    ylo = y_lab[ss] - 0.2
                    yhi = y_lab[ss] + 0.2
                    py.axis([xlo, xhi, ylo, yhi])

                    print 'Did not find a match for %s (K=%4.1f  x=%7.3f y = %7.3f):' % \
                          (n_lab[ss], label.mag[ss], x_lab[ss], y_lab[ss])

                    for rr in rdx:
                        print fmt % \
                              (n_aln[rr], mag[rr], x0[rr], y0[rr], x0err[rr],
                               y0err[rr], vx[rr]*10**3, vy[rr]*10**3,
                               vxerr[rr]*10**3, vyerr[rr]*10**3, t0x[rr], use[ss], r[rr])

                        # if the starname in align has the same ending as the name in
                        # label.dat, it's likely the same star (e.g. '1S9-87' and 'S9-87';
                        # Note that this is the naming used in mosaicked star list analysis)
                        if n_aln[rr].endswith(n_lab[ss]):
                            # do a check that these stars have similar magnitudes
                            if np.abs(label.mag[ss] - mag[rr]) <= 0.5:
                                update = 1
                                # replace the whole line in label_updated.dat
                                out.write(fmt % \
                                          (n_lab[ss], mag[rr], x0[rr], y0[rr], x0err[rr],
                                           y0err[rr], vx[rr]*10**3, vy[rr]*10**3,
                                           vxerr[rr]*10**3, vyerr[rr]*10**3, t0x[rr], use[ss], r[rr]))
                                continue
                            else:
                                foo = raw_input(
                                    'Similar names, but magnitudes are off. Check star %s manually and decide whether or not to replace it in label_updated.dat when this program completes. Hit enter to continue.'
                                    % n_aln[rr])

                        # Or the star just wasn't matched with any star in the old label.dat file
                        # and was given an arbitrary name
                        elif (('star' in n_aln[rr]) | ('ep' in n_aln[rr])):
                            print 'Manually check and update this star if needed:'
                            print 'Name in label: %s' % n_lab[ss]
                            print 'Name in new alignment: %s' % n_aln[rr]
                            if len(rdx) > 1:
                                print 'CAUTION: There are other nearby stars, including:'
                                for ii in range(len(rdx)):
                                    print n_aln[rdx[ii]]
                            update = raw_input(
                                'Update with %s velocity? (enter 1 for yes; 0 for no) '
                                % n_aln[rr])
                            update = int(update)
                            if update == 1:
                                out.write(fmt % \
                                          (n_lab[ss], mag[rr], x0[rr], y0[rr], x0err[rr],
                                           y0err[rr], vx[rr]*10**3, vy[rr]*10**3,
                                           vxerr[rr]*10**3, vyerr[rr]*10**3, t0x[rr], use[ss], r[rr]))
                            # check magnitude offset
                            #if np.abs(label.mag[ss] - mag[rr]) <= 0.5:
                            #    update = 1
                            #    # replace the whole line in label_updated.dat
                            #    out.write(fmt % \
                            #              (n_lab[ss], mag[rr], x0[rr], y0[rr], x0err[rr],
                            #               y0err[rr], vx[rr]*10**3, vy[rr]*10**3,
                            #               vxerr[rr]*10**3, vyerr[rr]*10**3, t0x[rr], 1, r[rr]))

            if update == 0:
                # just re-write what was in label.dat, and later run
                # updateLabelInfoWithAbsRefs() to update matched stars with the new
                # absolute alignment
                out.write(fmt % \
                          (label.ourName[ss], label.mag[ss], label.x[ss], label.y[ss],
                          label.xerr[ss], label.yerr[ss], label.vx[ss], label.vy[ss],
                          label.vxerr[ss], label.vyerr[ss], label.t0[ss], use[ss],
                          label.r[ss]))

        out.close()
コード例 #9
0
def arches_figure():
    """
    Plot a 3 panel figure showing seeing-limited, HST, and AO data on the
    Arches cluster to illustrate the power of AO.
    """
    # ----------
    # NIRC2
    # ----------

    hroot = 'mag06maylgs2_arch_f1_h'
    kroot = 'mag06maylgs2_arch_f1_kp'
    lroot = 'mag06maylgs2_arch_f1_lp'

    cooStar = 'f1_psf0'

    scaleMinH = 1000
    scaleMinK = 800
    scaleMinL = 600

    scaleMaxH = 4500
    scaleMaxK = 8000
    scaleMaxL = 10000

    img = np.zeros((1500, 1500, 3), dtype=float)
    origin = np.array([750.0, 750.0])

    labelFile = '/u/ghezgroup/data/gc/source_list/label_arch.dat'
    labels = starTables.Labels(labelFile=labelFile)

    dataDir = '/u/ghezgroup/data/gc/06maylgs2/combo/'

    # Load up the images
    h = pyfits.getdata(dataDir + hroot + '.fits')
    k = pyfits.getdata(dataDir + kroot + '.fits')
    l = pyfits.getdata(dataDir + lroot + '.fits')

    # Make the arrays into the largest size.
    h_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)
    k_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)
    l_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)

    h_new[0:h.shape[0], 0:h.shape[1]] = h
    k_new[0:k.shape[0], 0:k.shape[1]] = k
    l_new[0:l.shape[0], 0:l.shape[1]] = l

    # Load up the coo stars
    tmpH = open(dataDir + hroot + '.coo').readline().split()
    cooH = np.array([float(tmpH[0]), float(tmpH[1])])

    tmpK = open(dataDir + kroot + '.coo').readline().split()
    cooK = np.array([float(tmpK[0]), float(tmpK[1])])

    tmpL = open(dataDir + lroot + '.coo').readline().split()
    cooL = np.array([float(tmpL[0]), float(tmpL[1])])

    # Get the coordinates of each coo star in arcsec.
    idxH = np.where(labels.name == cooStar)[0][0]
    idxK = np.where(labels.name == cooStar)[0][0]
    idxL = np.where(labels.name == cooStar)[0][0]

    asecH = np.array([labels.x[idxH], labels.y[idxH]])
    asecK = np.array([labels.x[idxK], labels.y[idxK]])
    asecL = np.array([labels.x[idxL], labels.y[idxL]])

    scale = np.array([-0.00995, 0.00995])

    # Now figure out the necessary shifts
    originH = cooH - asecH / scale
    originK = cooK - asecK / scale
    originL = cooL - asecL / scale

    # Shift the J and H images to be lined up with K-band
    shiftL = origin - originL
    shiftK = origin - originK
    shiftH = origin - originH
    l = interp.shift(l_new, shiftL[::-1])
    k = interp.shift(k_new, shiftK[::-1])
    h = interp.shift(h_new, shiftH[::-1])
    print shiftH
    print shiftL

    xx, yy = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1]))

    idx = np.where((h >= 1) & (k >= 1) & (l >= 1))

    # Trim off the bottom 10 rows where there is data
    ymin = yy[idx[0], idx[1]].min()
    ydx = np.where(yy[idx[0], idx[1]] > (ymin + 10))[0]
    idx = (idx[0][ydx], idx[1][ydx])

    #     gcutil.rmall(['arches_f1_h.fits', 'arches_f1_kp.fits', 'arches_f1_lp.fits'])
    #     pyfits.writeto('arches_f1_h.fits', h)
    #     pyfits.writeto('arches_f1_kp.fits', k)
    #     pyfits.writeto('arches_f1_lp.fits', l)

    img[idx[0], idx[1], 0] = img_scale.sqrt(l[idx[0], idx[1]],
                                            scale_min=scaleMinL,
                                            scale_max=scaleMaxL)
    img[idx[0], idx[1], 1] = img_scale.sqrt(k[idx[0], idx[1]],
                                            scale_min=scaleMinK,
                                            scale_max=scaleMaxK)
    img[idx[0], idx[1], 2] = img_scale.sqrt(h[idx[0], idx[1]],
                                            scale_min=scaleMinH,
                                            scale_max=scaleMaxH)

    # Define the axes
    xaxis = np.arange(-0.5, img.shape[1] + 0.5, 1)
    xaxis = ((xaxis - origin[0]) * scale[0])
    yaxis = np.arange(-0.5, img.shape[0] + 0.5, 1)
    yaxis = ((yaxis - origin[1]) * scale[1])
    extent = [xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]]

    img_nirc2 = img
    ext_nirc2 = extent

    # ----------
    # UKIDSS
    # ----------
    scaleMinJ = 20
    scaleMinH = 400
    scaleMinK = 1000

    scaleMaxJ = 5000
    scaleMaxH = 35000
    scaleMaxK = 90000

    dataDir = '/u/jlu/data/arches/ukidss/'

    # Load up the images
    j = pyfits.getdata(dataDir + 'ukidss_arches_j.fits')
    h = pyfits.getdata(dataDir + 'ukidss_arches_h.fits')
    k = pyfits.getdata(dataDir + 'ukidss_arches_k.fits')

    img = np.zeros((j.shape[0], j.shape[1], 3), dtype=float)
    origin = [173, 198]
    scale = [-0.2, 0.2]

    xx, yy = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1]))

    img[:, :, 0] = img_scale.sqrt(k, scale_min=scaleMinK, scale_max=scaleMaxK)
    img[:, :, 1] = img_scale.sqrt(h, scale_min=scaleMinH, scale_max=scaleMaxH)
    img[:, :, 2] = img_scale.sqrt(j, scale_min=scaleMinJ, scale_max=scaleMaxJ)
    # Define the axes
    xaxis = np.arange(-0.5, img.shape[1] + 0.5, 1)
    xaxis = ((xaxis - origin[0]) * scale[0])
    yaxis = np.arange(-0.5, img.shape[0] + 0.5, 1)
    yaxis = ((yaxis - origin[1]) * scale[1])
    extent = [xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]]

    img_ukidss = img
    ext_ukidss = extent

    py.figure(2, figsize=(6, 12))
    py.clf()
    py.subplots_adjust(bottom=0.05, top=0.95, hspace=0.25)

    py.subplot(2, 1, 1)
    py.imshow(img_ukidss, extent=ext_ukidss)
    py.axis('equal')
    py.axis([4.5, -6.5, -6.5, 4.5])
    py.title('UKIDSS JHK')
    py.xlabel('R.A. Offset (arcsec)')
    py.ylabel('Dec. Offset (arcsec)')

    py.subplot(2, 1, 2)
    py.imshow(img_nirc2, extent=ext_nirc2)
    py.axis('equal')
    py.axis([4.5, -6.5, -6.5, 4.5])
    py.title("Keck AO HK'L'")
    py.xlabel('R.A. Offset (arcsec)')
    py.ylabel('Dec. Offset (arcsec)')

    py.savefig('arches_see_vs_ao.png')
コード例 #10
0
def mosaic3color():
    """
    Make a 3 color mosaic of our NIRC2 data on W51.
    """
    hepochs = ['09jun26', '09jun26', '09jun26', '09jun26']
    kepochs = ['09jun10', '09jun10', '09jun10', '09jun26']
    lepochs = ['09jun26', '09jun26', '09jun26', '09jun26']

    cooStarsH = ['f1_psf0', 'f2_psf0', 'f3_psf0', 'f4_psf0']
    cooStarsK = ['f1_psf0', 'f2_psf0', 'f3_psf0', 'f4_psf0']
    cooStarsL = ['f1_psf1', 'f2_psf0', 'f3_psf2', 'f4_psf1']

    cooStarsH = ['E4-1', 'E8-1', 'N5-1', 'W6-2']
    cooStarsK = ['E4-1', 'E8-1', 'N5-1', 'W6-2']
    cooStarsL = ['S0-1', 'E8-1', 'W7-1', 'W9-1']

    scaleMinH = [0, 0, 0, 0]
    scaleMinK = [0, 0, 0, 0]
    scaleMinL = [1000, 1100, 1200, 1250]

    scaleMaxH = [6000, 6000, 5000, 6000]
    scaleMaxK = [5500, 5500, 5500, 4500]
    #     scaleMaxL = [1600, 1300, 1400, 1600]
    scaleMaxL = [2000, 2000, 2000, 2000]

    img = np.zeros((2400, 2400, 3), dtype=float)
    origin = np.array([1200.0, 1200.0])

    labelFile = '/u/jlu/data/w51/source_list/w51a_label.dat'
    labels = starTables.Labels(labelFile=labelFile)

    dataRoot = '/u/jlu/data/w51/'

    py.clf()
    foo = range(len(hepochs))
    for ii in foo[::-1]:
        #     for ii in range(1):
        rootH = '%s/%s/combo/mag%s_w51a_f%d_h' % \
            (dataRoot, hepochs[ii], hepochs[ii], ii+1)
        rootK = '%s/%s/combo/mag%s_w51a_f%d_kp' % \
            (dataRoot, kepochs[ii], kepochs[ii], ii+1)
        rootL = '%s/%s/combo/mag%s_w51a_f%d_lp' % \
            (dataRoot, lepochs[ii], lepochs[ii], ii+1)

        # Load up the images
        h = pyfits.getdata(rootH + '.fits')
        k = pyfits.getdata(rootK + '.fits')
        l = pyfits.getdata(rootL + '.fits')

        # Make the arrays into the largest size.
        h_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)
        k_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)
        l_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)

        h_new[0:h.shape[0], 0:h.shape[1]] = h
        k_new[0:k.shape[0], 0:k.shape[1]] = k
        l_new[0:l.shape[0], 0:l.shape[1]] = l

        # Load up the coo stars
        tmpH = open(rootH + '.coo').readline().split()
        cooH = np.array([float(tmpH[0]), float(tmpH[1])])

        tmpK = open(rootK + '.coo').readline().split()
        cooK = np.array([float(tmpK[0]), float(tmpK[1])])

        tmpL = open(rootL + '.coo').readline().split()
        cooL = np.array([float(tmpL[0]), float(tmpL[1])])

        # Get the coordinates of each coo star in arcsec.
        idxH = np.where(labels.name == cooStarsH[ii])[0][0]
        idxK = np.where(labels.name == cooStarsK[ii])[0][0]
        idxL = np.where(labels.name == cooStarsL[ii])[0][0]

        asecH = np.array([labels.x[idxH], labels.y[idxH]])
        asecK = np.array([labels.x[idxK], labels.y[idxK]])
        asecL = np.array([labels.x[idxL], labels.y[idxL]])

        scale = np.array([-0.00995, 0.00995])

        # Now figure out the necessary shifts
        originH = cooH - asecH / scale
        originK = cooK - asecK / scale
        originL = cooL - asecL / scale

        # Shift the J and H images to be lined up with K-band
        shiftL = origin - originL
        shiftK = origin - originK
        shiftH = origin - originH
        l = interp.shift(l_new, shiftL[::-1])
        k = interp.shift(k_new, shiftK[::-1])
        h = interp.shift(h_new, shiftH[::-1])
        print shiftH
        print shiftL

        xx, yy = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1]))

        idx = np.where((h >= 1) & (k >= 1) & (l >= 1))

        # Trim off the bottom 10 rows where there is data
        ymin = yy[idx[0], idx[1]].min()
        ydx = np.where(yy[idx[0], idx[1]] > (ymin + 10))[0]
        idx = (idx[0][ydx], idx[1][ydx])

        img[idx[0], idx[1], 0] = img_scale.sqrt(l[idx[0], idx[1]],
                                                scale_min=scaleMinL[ii],
                                                scale_max=scaleMaxL[ii])
        img[idx[0], idx[1], 1] = img_scale.sqrt(k[idx[0], idx[1]],
                                                scale_min=scaleMinK[ii],
                                                scale_max=scaleMaxK[ii])
        img[idx[0], idx[1], 2] = img_scale.sqrt(h[idx[0], idx[1]],
                                                scale_min=scaleMinH[ii],
                                                scale_max=scaleMaxH[ii])

        # Defin the axes
        xaxis = np.arange(-0.5, img.shape[1] + 0.5, 1)
        xaxis = ((xaxis - origin[0]) * scale[0])
        yaxis = np.arange(-0.5, img.shape[0] + 0.5, 1)
        yaxis = ((yaxis - origin[1]) * scale[1])
        extent = [xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]]

        py.imshow(img, extent=extent)
        py.axis('equal')
        foo = raw_input('Continue?')

    py.axis([7, -7, -7, 7])
    py.savefig('/u/jlu/work/w51/maps/w51a_3color.png')
コード例 #11
0
def plot_wide_labeled(labels='/u/jlu/data/w51/source_list/w51a_label.dat',
                      markCoordRefStars=False):
    lab = starTables.Labels(labelFile=labels)

    t = 2009.580

    xarc = lab.x + lab.vx * (t - lab.t0) / 10**3
    yarc = lab.y + lab.vy * (t - lab.t0) / 10**3

    vxarc = lab.vx
    vyarc = lab.vy

    angle = math.radians(0.0)
    cosa = math.cos(angle)
    sina = math.sin(angle)

    x = xarc * cosa + yarc * sina
    y = -xarc * sina + yarc * cosa

    vx = vxarc * cosa + vyarc * sina
    vy = vxarc * sina - vyarc * cosa

    r = np.hypot(x, y)
    v = np.hypot(vx, vy)

    # Lets also make some coordinates for compass rose and scale bar
    xrose = np.array([-15.5, -15.5])
    yrose = np.array([15.5, 15.5])
    xroseLen = np.array([20.0, 0.0])
    yroseLen = np.array([0.0, 20.0])

    xr = xrose
    yr = yrose
    xrlen = xroseLen * cosa + yroseLen * sina
    yrlen = xroseLen * sina - yroseLen * cosa

    # Image
    im = pyfits.getdata(
        '/u/jlu/data/w51/09jun26/combo/mag09jun26_w51a_wide_h.fits')
    imgsize = (im.shape)[0]

    # pixel position (0,0) is at upper left
    xpix = np.arange(0, im.shape[0], dtype=float)
    ypix = np.arange(0, im.shape[1], dtype=float)

    origin = [607.0, 749.0]
    scale_jpg = 0.04
    xim = (xpix - origin[0]) * scale_jpg * -1.0
    yim = (ypix - origin[1]) * scale_jpg

    py.clf()
    py.grid(True)
    py.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
    py.imshow(np.log10(im),
              extent=[xim[0], xim[-1], yim[0], yim[-1]],
              aspect='equal',
              vmin=0.5,
              vmax=5,
              cmap=py.cm.gray)
    py.xlabel('X Offset (arcsec)')
    py.ylabel('Y Offset (arcsec)')
    py.title('J. R. Lu', fontsize=20, fontweight='bold')
    thePlot = py.gca()

    idx = (
        np.where(  #(mag < 18) &
            (x > xim.min()) & (x < xim.max()) & (y > yim.min()) &
            (y < yim.max())))[0]

    py.plot(x[idx],
            y[idx],
            'ro',
            color='orange',
            markerfacecolor='None',
            markeredgecolor='orange')

    if markCoordRefStars:
        mdx = (np.where((x > xim.min()) & (x < xim.max()) & (y > yim.min())
                        & (y < yim.max()) & (lab.useToAlign != '0')))[0]
        py.plot(x[mdx],
                y[mdx],
                'ro',
                color='cyan',
                markerfacecolor='None',
                markeredgecolor='green')

    for ii in idx:
        if markCoordRefStars and ii in mdx:
            color = 'cyan'
        else:
            color = 'orange'
        py.text(x[ii], y[ii], lab.name[ii], color=color, fontsize=10)

    py.axis([15, -15, -15, 15])
    py.savefig('w51a_wide_labeled.png', dpi=300)
コード例 #12
0
def mosaic_kp():
    """
    Make a mosaic of our NIRC2 data on W51, one per filter.
    """
    hepochs = ['09jun26', '09jun26', '09jun26', '09jun26']
    kepochs = ['09jun10', '09jun10', '09jun10', '09jun26']
    lepochs = ['09jun26', '09jun26', '09jun26', '09jun26']

    cooStarsH = ['f1_psf0', 'f2_psf0', 'f3_psf0', 'f4_psf0']
    cooStarsK = ['f1_psf0', 'f2_psf0', 'f3_psf0', 'f4_psf0']
    cooStarsL = ['f1_psf1', 'f2_psf0', 'f3_psf2', 'f4_psf1']

    cooStarsH = ['E4-1', 'E8-1', 'N5-1', 'W6-2']
    cooStarsK = ['E4-1', 'E8-1', 'N5-1', 'W6-2']
    cooStarsL = ['S0-1', 'E8-1', 'W7-1', 'W9-1']

    scaleMinH = [0, 0, 0, 0]
    scaleMinK = [0, 0, 0, 0]
    scaleMinL = [1000, 1100, 1200, 1250]

    scaleMaxH = [6000, 6000, 5000, 6000]
    scaleMaxK = [6500, 6500, 6500, 5500]
    scaleMaxL = [1600, 1300, 1400, 1600]

    img = np.zeros((2400, 2400, 3), dtype=float)
    imgH = np.zeros((2400, 2400), dtype=float)
    imgK = np.zeros((2400, 2400), dtype=float)
    imgL = np.zeros((2400, 2400), dtype=float)
    origin = np.array([1200.0, 1200.0])

    labelFile = '/u/jlu/data/w51/source_list/w51a_label.dat'
    labels = starTables.Labels(labelFile=labelFile)

    dataRoot = '/u/jlu/data/w51/'

    py.clf()
    foo = range(len(hepochs))
    for ii in foo[::-1]:
        #     for ii in range(1):
        rootH = '%s/%s/combo/mag%s_w51a_f%d_h' % \
            (dataRoot, hepochs[ii], hepochs[ii], ii+1)
        rootK = '%s/%s/combo/mag%s_w51a_f%d_kp' % \
            (dataRoot, kepochs[ii], kepochs[ii], ii+1)
        rootL = '%s/%s/combo/mag%s_w51a_f%d_lp' % \
            (dataRoot, lepochs[ii], lepochs[ii], ii+1)

        # Load up the images
        h, hhdr = pyfits.getdata(rootH + '.fits', header=True)
        k, khdr = pyfits.getdata(rootK + '.fits', header=True)
        l, lhdr = pyfits.getdata(rootL + '.fits', header=True)

        hint = hhdr['ITIME'] * hhdr['COADDS']
        kint = khdr['ITIME'] * khdr['COADDS']
        lint = lhdr['ITIME'] * lhdr['COADDS']

        # Make the arrays into the largest size.
        h_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)
        k_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)
        l_new = np.zeros((img.shape[0], img.shape[1]), dtype=float)

        h_new[0:h.shape[0], 0:h.shape[1]] = h
        k_new[0:k.shape[0], 0:k.shape[1]] = k
        l_new[0:l.shape[0], 0:l.shape[1]] = l

        # Load up the coo stars
        tmpH = open(rootH + '.coo').readline().split()
        cooH = np.array([float(tmpH[0]), float(tmpH[1])])

        tmpK = open(rootK + '.coo').readline().split()
        cooK = np.array([float(tmpK[0]), float(tmpK[1])])

        tmpL = open(rootL + '.coo').readline().split()
        cooL = np.array([float(tmpL[0]), float(tmpL[1])])

        # Get the coordinates of each coo star in arcsec.
        idxH = np.where(labels.name == cooStarsH[ii])[0][0]
        idxK = np.where(labels.name == cooStarsK[ii])[0][0]
        idxL = np.where(labels.name == cooStarsL[ii])[0][0]

        asecH = np.array([labels.x[idxH], labels.y[idxH]])
        asecK = np.array([labels.x[idxK], labels.y[idxK]])
        asecL = np.array([labels.x[idxL], labels.y[idxL]])

        scale = np.array([-0.00995, 0.00995])

        # Now figure out the necessary shifts
        originH = cooH - asecH / scale
        originK = cooK - asecK / scale
        originL = cooL - asecL / scale

        # Shift the J and H images to be lined up with K-band
        shiftL = origin - originL
        shiftK = origin - originK
        shiftH = origin - originH
        l = interp.shift(l_new, shiftL[::-1])
        k = interp.shift(k_new, shiftK[::-1])
        h = interp.shift(h_new, shiftH[::-1])
        print shiftH
        print shiftL

        xx, yy = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1]))

        idx = np.where((h >= 1) & (k >= 1) & (l >= 1))

        # Trim off the bottom 10 rows where there is data
        ymin = yy[idx[0], idx[1]].min()
        ydx = np.where(yy[idx[0], idx[1]] > (ymin + 10))[0]
        idx = (idx[0][ydx], idx[1][ydx])

        img[idx[0], idx[1], 0] = img_scale.log(l[idx[0], idx[1]],
                                               scale_min=scaleMinL[ii],
                                               scale_max=scaleMaxL[ii])
        img[idx[0], idx[1], 1] = img_scale.log(k[idx[0], idx[1]],
                                               scale_min=scaleMinK[ii],
                                               scale_max=scaleMaxK[ii])
        img[idx[0], idx[1], 2] = img_scale.log(h[idx[0], idx[1]],
                                               scale_min=scaleMinH[ii],
                                               scale_max=scaleMaxH[ii])

        imgH[idx[0], idx[1]] = h[idx[0], idx[1]] / hint
        imgK[idx[0], idx[1]] = k[idx[0], idx[1]] / kint
        imgL[idx[0], idx[1]] = l[idx[0], idx[1]] / lint

        # Fix scaling of first image.
        if ii == 0:
            imgK[idx[0], idx[1]] -= 0.4

        # Save on memory
        l = None
        k = None
        h = None
        l_new = None
        k_new = None
        h_new = None

        # Define the axes
        xaxis = np.arange(-0.5, img.shape[1] + 0.5, 1)
        xaxis = ((xaxis - origin[0]) * scale[0])
        yaxis = np.arange(-0.5, img.shape[0] + 0.5, 1)
        yaxis = ((yaxis - origin[1]) * scale[1])
        extent = [xaxis[0], xaxis[-1], yaxis[0], yaxis[-1]]

        #         py.figure(1)
        #         py.imshow(img[:,:,0], extent=extent, cmap=py.cm.gray)
        #         py.axis('equal')

        py.figure(2)
        py.imshow(img[:, :, 1], extent=extent, cmap=py.cm.gray)
        py.axis('equal')

        #         py.figure(3)
        #         py.imshow(img[:,:,2], extent=extent, cmap=py.cm.gray)
        #         py.axis('equal')

        foo = raw_input('Continue?')

    gcutil.rmall(
        ['w51a_h_mosaic.fits', 'w51a_k_mosaic.fits', 'w51a_l_mosaic.fits'])

    pyfits.writeto('w51a_h_mosaic.fits', imgH)
    pyfits.writeto('w51a_k_mosaic.fits', imgK)
    pyfits.writeto('w51a_l_mosaic.fits', imgL)