Example #1
0
    def __init__(self,
                 data,
                 XWIN_IMAGE,
                 YWIN_IMAGE,
                 FLUX_AUTO,
                 FLUXERR_AUTO,
                 zscaleNsamp=200,
                 zscaleContrast=1.,
                 minGoodVal=None):
        self.XWIN_IMAGE = XWIN_IMAGE
        self.YWIN_IMAGE = YWIN_IMAGE
        self.FLUX_AUTO = FLUX_AUTO
        self.FLUXERR_AUTO = FLUXERR_AUTO
        self.data = data
        self.minGoodVal = minGoodVal

        mask = np.ones(self.data.shape, dtype=bool)
        if self.minGoodVal is not None:
            w = np.where(self.data < self.minGoodVal)
            mask[w] = 0
        zscale = ZScaleInterval(nsamples=zscaleNsamp, contrast=zscaleContrast)
        (self.z1, self.z2) = zscale.get_limits(self.data[mask])
        self.normer = interval.ManualInterval(self.z1, self.z2)

        self._increment = 0
Example #2
0
def plotCutoutsClass(pred,
                     pos_x,
                     pos_y,
                     data,
                     ind=0,
                     cutSize=25,
                     show=True,
                     title=None):

    ui = np.arange(len(pos_x))
    w = np.where(pred == ind)
    ui = ui[w]

    nsp = int(len(ui)**0.5)
    if nsp * nsp < len(ui):
        nsp += 1

    nsp = min(nsp, 10)
    print(nsp, len(ui))

    fig = pyl.figure('cutouts_' + str(ind))
    gs = gridspec.GridSpec(nsp, nsp)

    j = 0
    k = 0
    npl = 0
    for ii in range(len(ui)):
        x = int(pos_x[ui[ii]])
        y = int(pos_y[ui[ii]])
        cut = data[y - cutSize:y + cutSize + 1, x - cutSize:x + cutSize + 1]

        (z1, z2) = numdisplay.zscale.zscale(cut, contrast=0.5)
        normer = interval.ManualInterval(z1, z2)
        Cut = normer(cut)

        sp = pyl.subplot(gs[j, k])
        sp.imshow(Cut)

        if j == 0 and k == int(nsp / 2):
            sp.set_title(title)
        j += 1
        npl += 1
        if j == nsp:
            j = 0
            k += 1
        if npl == nsp * nsp:
            break
    if show:
        pyl.show()
Example #3
0
def plotCutoutsTree(resu,
                    stree,
                    pos_x,
                    pos_y,
                    data,
                    ind=7,
                    cutSize=25,
                    show=True):

    ui = []
    for j in range(len(resu[ind])):
        i0 = int(stree[resu[ind][j]][0])
        i1 = int(stree[resu[ind][j]][1])
        ui.append(i0)
        ui.append(i1)
    ui = np.unique(np.array(ui))
    nsp = int(len(ui)**0.5)
    if nsp * nsp < len(ui):
        nsp += 1

    fig = pyl.figure('cutouts_' + str(ind))
    gs = gridspec.GridSpec(nsp, nsp)

    j = 0
    k = 0
    npl = 0
    for ii in range(len(ui)):
        x = int(pos_x[ui[ii]])
        y = int(pos_y[ui[ii]])
        cut = data[y - cutSize:y + cutSize + 1, x - cutSize:x + cutSize + 1]

        (z1, z2) = numdisplay.zscale.zscale(cut, contrast=0.5)
        normer = interval.ManualInterval(z1, z2)
        Cut = normer(cut)

        sp = pyl.subplot(gs[j, k])
        sp.imshow(Cut)
        j += 1
        npl += 1
        if j == nsp:
            j = 0
            k += 1
        if npl == nsp * nsp:
            break
    if show:
        pyl.show()
Example #4
0
    def __init__(self,data,XWIN_IMAGE,YWIN_IMAGE,FLUX_AUTO,FLUXERR_AUTO,zscaleNsamp=200,zscaleContrast=1.,minGoodVal=None):
        self.XWIN_IMAGE=XWIN_IMAGE
        self.YWIN_IMAGE=YWIN_IMAGE
        self.FLUX_AUTO=FLUX_AUTO
        self.FLUXERR_AUTO=FLUXERR_AUTO
        self.data=data
        self.minGoodVal = minGoodVal

        if self.minGoodVal is not None:
            mask = np.zeros(self.data.shape)
            w = np.where(self.data<self.minGoodVal)
            mask[w] = 1
            (self.z1,self.z2)=numdisplay.zscale.zscale(self.data,nsamples=zscaleNsamp,contrast=zscaleContrast,bpmask=mask)
        else:
            (self.z1,self.z2)=numdisplay.zscale.zscale(self.data,nsamples=zscaleNsamp,contrast=zscaleContrast)
        self.normer=interval.ManualInterval(self.z1,self.z2)

        self._increment = 0
Example #5
0
    def __init__(self,
                 data,
                 XWIN_IMAGE,
                 YWIN_IMAGE,
                 FLUX_AUTO,
                 FLUXERR_AUTO,
                 zscaleNsamp=200,
                 zscaleContrast=1.):
        self.XWIN_IMAGE = XWIN_IMAGE
        self.YWIN_IMAGE = YWIN_IMAGE
        self.FLUX_AUTO = FLUX_AUTO
        self.FLUXERR_AUTO = FLUXERR_AUTO
        self.data = data
        (self.z1, self.z2) = numdisplay.zscale.zscale(self.data,
                                                      nsamples=zscaleNsamp,
                                                      contrast=zscaleContrast)
        self.normer = interval.ManualInterval(self.z1, self.z2)

        self._increment = 0
Example #6
0
    def __call__(self,
                 xi,
                 yi,
                 radius=4.,
                 l=5.,
                 a=0.01,
                 width=20.,
                 skyRadius=8.,
                 zpt=27.0,
                 exptime=1.,
                 enableBGSelection=False,
                 display=False,
                 verbose=False,
                 backupMode='fraserMode',
                 forceBackupMode=False,
                 trimBGHighPix=False,
                 zscale=True,
                 zoomRegion=None):
        """
        Perform the actual photometry.

        Important note: the background is taken as the output from bgfinder.smartBackground.
            First a Guassian is fit to the data.
            If the gaussian standard deviation / gaussian peak is larger than the variable gaussSTDlimit, or if
            forceBackupMode is set to true, the backup mode background value is returned. Otherwise, the peak of the
            gaussian fit is returned.

        angle in degrees clockwise +-90 degrees from +x
        Length in pixels.
        Coordinates are in iraf coordinates, not numpy.

        -width is the outer dimension of the image region considered.
        That is, 2*width*2*width is the image subsection size.

        set trimBGHighPix to some value ,x, to trim the background
        -this is done by first estimating the background. Then, it trims
        all pixels with value v>bg+x*bgstd. That is, it provides a rough
        sigma cut to get rid of glaringly bright sources that might affect
        the bg estimate.
        -the bg is then restimated.

        -when zoomRegion is set [x_low,x_high,y_low,y_high], a 4 int/float array, and enableBGSelection = True, the
        aperture panel will automatically zoom to the region of the image specified by the box coordinates in zoomRegion
        """
        #Single-letter variables = super painful debugging
        #I'll leave them in the function call for backwards compatibility,
        #but will remove below.
        angle = a
        length = l
        del (a, l)

        #Set up some of the True/False statements that gets used repeatedly:
        singleAperture = isinstance(radius, (float, num.float64))
        multipleApertures = isinstance(radius, num.ndarray)
        if not singleAperture | multipleApertures:
            raise Exception(
                'Aperture size not understood. ' +
                'It seems to be neither an array or a single value')
        if enableBGSelection:
            if zoomRegion is not None:
                if isinstance(zoomRegion, (list, num.float64)):
                    if not len(zoomRegion) == 4:
                        raise Exception(
                            'Need four values to specify the corners of the zoom box.'
                        )
                    elif len(zoomRegion) == 4 and not isinstance(
                            zoomRegion[0], (float, int)):
                        raise Exception(
                            'zoomRegion takes floats or ints only.')
                else:
                    raise Exception(
                        'zoomRegion is a 4 float/int array or list ')

        #Check whether aperture(s) is integer: if True convert to float.
        integerAperture = isinstance(radius, (int, num.integer))
        if multipleApertures:
            integerAperture = issubclass(radius.dtype.type, num.integer)
        if integerAperture:
            radius *= 1.
            integerAperture = False

        if display:
            #setup the necessary subplots

            self.dispFig = pyl.figure('Image Display')
            if enableBGSelection:
                #dispFig.subplots_adjust(wspace = 0.0,hspace = 0.0)
                self.dispGS = gridspec.GridSpec(
                    1, 2)  #, height_ratios = [3.5,1], width_ratios = [3.5,1])
                self.dispAx = pyl.subplot(self.dispGS[0])
            else:

                self.dispAx = self.dispFig.add_subplot(111)

        x = xi - 0.5
        y = yi - 0.5

        #if l+radius<width or l+skyRadius<width:
        #    raise Exception("Width must be large enough to include both the full aperture, and the sky radius.")

        #        if angle > 90 or angle < -90 or length < 0 or num.min(radius) < 0:
        if angle > 90 or angle < -90:
            angle = angle % 180.
            if verbose:
                print("Warning! You gave a bad angle. I'll fix it for you.")
        if length < 0:
            length = -length
            if verbose:
                print("Warning! You gave a bad length. I'll fix it for you.")
        if num.min(radius) < 0:
            raise Exception('Aperture radius must be positive!')

        if singleAperture:
            image = self.__lp__(x=x + 1.,
                                y=y + 1.,
                                radius=radius,
                                l=length,
                                a=angle,
                                w=int(width))
            mask = self.mask
        elif multipleApertures:
            image = []
            mask = []
            for jj in range(len(radius)):
                image.append(
                    self.__lp__(x=x + 1.,
                                y=y + 1.,
                                radius=radius[jj],
                                l=length,
                                a=angle,
                                w=int(width)))
                mask.append(self.mask)

        if display and self.l0 <> None:
            l0 = self.l0
            l1 = self.l1
            l2 = self.l2
            l3 = self.l3

        bgstd = -1.

        if skyRadius == None:
            if singleAperture:
                skyImage = image * 0.0
            elif multipleApertures:
                skyImage = image[0] * 0.0
            bg = 0.0
        else:
            skyImage = self.__lp__(x=x + 1.,
                                   y=y + 1.,
                                   radius=skyRadius,
                                   l=length,
                                   a=angle,
                                   w=int(width),
                                   retObj=False)
            bgmask = self.bgmask

            rebinnedSkyImage = num.zeros(
                num.array(skyImage.shape) / self.repFact)
            (aa, bb) = skyImage.shape
            for ii in range(0, aa, self.repFact):
                for jj in range(0, bb, self.repFact):
                    n = num.sum(bgmask[ii:ii + self.repFact,
                                       jj:jj + self.repFact])
                    if n == self.repFact * self.repFact:
                        rebinnedSkyImage[ii / self.repFact,
                                         jj / self.repFact] = num.sum(
                                             skyImage[ii:ii + self.repFact,
                                                      jj:jj + self.repFact])

            w = num.where(rebinnedSkyImage <> 0.0)
            bgf = bgFinder.bgFinder(rebinnedSkyImage[w])
            if display and enableBGSelection:
                bgf.plotAxis = self.dispFig.add_subplot(self.dispGS[1])

            if not trimBGHighPix:
                bg = bgf.smartBackground(display=display,
                                         backupMode=backupMode,
                                         forceBackupMode=forceBackupMode)
                bgstd = num.std(rebinnedSkyImage[w])
            else:
                #trimming BG high pix
                bg = bgf.smartBackground(backupMode=backupMode,
                                         forceBackupMode=forceBackupMode)
                bgstd = num.std(rebinnedSkyImage[w])

                W = num.where(rebinnedSkyImage[w] < bg + trimBGHighPix * bgstd)
                bgf = bgFinder.bgFinder(rebinnedSkyImage[w][W])
                if display and enableBGSelection:
                    bgf.plotAxis = self.dispFig.add_subplot(self.dispGS[1])
                bg = bgf.smartBackground(display=display,
                                         backupMode=backupMode,
                                         forceBackupMode=forceBackupMode)
                bgstd = num.std(rebinnedSkyImage[w][W])

        if singleAperture:
            W = num.where(mask != 0.0)
            flux = num.sum(image) - len(W[0]) * bg / (self.repFact**2)
        elif multipleApertures:
            flux = []
            for jj in range(len(radius)):
                W = num.where(mask[jj] != 0.0)
                flux.append(
                    num.sum(image[jj]) - len(W[0]) * bg / (self.repFact**2))
            flux = num.array(flux)

        self.nPix = num.sum(mask) / (self.repFact**2)
        self.sourceFlux = flux
        self.bg = bg
        self.bgstd = bgstd
        self.exptime = exptime
        self.magnitude = zpt - 2.5 * num.log10(self.sourceFlux / self.exptime)

        if display:
            if trimBGHighPix:
                w = num.where(
                    skyImage *
                    (self.repFact * self.repFact) > (bg +
                                                     trimBGHighPix * bgstd))
                skyImage[w] = 0

            if multipleApertures:
                im = skyImage + image[-1]
            elif singleAperture:
                im = skyImage + image

            if self.zscale:
                (z1, z2) = numdisplay.zscale.zscale(im)
                norm = interval.ManualInterval(z1, z2)
                self.dispAx.imshow(norm(im),
                                   interpolation='nearest',
                                   origin='lower')
            else:
                w = num.where(im == 0.0)
                im[w] += self.bg * 0.7 / (self.repFact * self.repFact)
                im = num.clip(im, num.min(im), num.max(image))
                self.dispAx.imshow(im, interpolation='nearest', origin='lower')
            if self.l0 is not None:
                self.dispAx.plot(num.linspace(l0.xlim[0], l0.xlim[1], 100),
                                 l0(num.linspace(l0.xlim[0], l0.xlim[1], 100)),
                                 'w-',
                                 lw=2.)
                self.dispAx.plot(num.linspace(l2.xlim[0], l2.xlim[1], 100),
                                 l2(num.linspace(l2.xlim[0], l2.xlim[1], 100)),
                                 'w-',
                                 lw=2.)
                mx0 = (l0.xlim[0] + l2.xlim[0]) / 2
                my0 = (l0.ylim[0] + l2.ylim[0]) / 2
                a0 = num.arctan2(l0.ylim[0] - my0, l0.xlim[0] - mx0)
                a1 = num.arctan2(l2.ylim[0] - my0, l2.xlim[0] - mx0)
                a01 = num.linspace(a0, a1, 25)
                outerRadius = radius if singleAperture else radius[-1]
                semiCircX = num.cos(a01) * outerRadius * self.repFact
                semiCircY = num.sin(a01) * outerRadius * self.repFact
                mx1 = (l0.xlim[1] + l2.xlim[1]) / 2
                my1 = (l0.ylim[1] + l2.ylim[1]) / 2
                my0, my1 = ((my1, my0) if ((angle < 0) & (angle > -90)) else
                            (my0, my1))
                self.dispAx.plot(mx0 - semiCircX, my0 - semiCircY, 'w-', lw=2)
                self.dispAx.plot(mx1 + semiCircX, my1 + semiCircY, 'w-', lw=2)

            if enableBGSelection:
                print 'Current background value: %.3f' % (self.bg)
                self.dispAx.set_title(
                    'To improve background measurement, zoom on\na good background region, then close.'
                )

                (ox0, ox1) = self.dispAx.get_xlim()
                (oy0, oy1) = self.dispAx.get_ylim()

                (A, B) = im.shape

                #all of these are needed in _on_lims_change
                self._bgFind = bgf
                self._rbsi = rebinnedSkyImage
                self._AB = im.shape
                self._bgm = backupMode
                self._fbgm = forceBackupMode

                if zoomRegion is not None:
                    # Broad steps to zoom to a specific region
                    self.dispAx.set_xlim(zoomRegion[0] * self.repFact,
                                         zoomRegion[1] * self.repFact)
                    self.dispAx.set_ylim(zoomRegion[2] * self.repFact,
                                         zoomRegion[3] * self.repFact)
                    self._on_xlims_change(self.dispAx)
                    self._on_ylims_change(self.dispAx)

                self.dispAx.callbacks.connect('xlim_changed',
                                              self._on_xlims_change)
                self.dispAx.callbacks.connect('ylim_changed',
                                              self._on_ylims_change)

                pyl.show()

                (x0, x1) = self.dispAx.get_xlim()
                (y0, y1) = self.dispAx.get_ylim()

                x0 = max(0, x0) / self.repFact
                y0 = max(0, y0) / self.repFact
                x1 = min(B, x1) / self.repFact
                y1 = min(A, y1) / self.repFact
                self.bgSamplingRegion = [x0, x1, y0, y1]

                #need to clear the histogram first
                #need to update the positions so that the code below this ifstatement actualy fires
                if ox0 == x0 and ox1 == x1 and oy0 == y0 and oy1 == y1: return

                rebinnedSkyImage = rebinnedSkyImage[int(y0):int(y1),
                                                    int(x0):int(x1)]
                w = num.where(rebinnedSkyImage <> 0.0)
                W = num.where(rebinnedSkyImage[w] < bg + trimBGHighPix * bgstd)
                bgf = bgFinder.bgFinder(rebinnedSkyImage[w][W])
                bg = bgf.smartBackground(display=False,
                                         backupMode=backupMode,
                                         forceBackupMode=forceBackupMode,
                                         verbose=verbose)
                bgstd = num.std(rebinnedSkyImage[w])

                if singleAperture:
                    W = num.where(mask != 0.0)
                    flux = num.sum(image) - len(
                        W[0]) * bg / (self.repFact * self.repFact)
                elif multipleApertures:
                    flux = []
                    for jj in range(len(radius)):
                        W = num.where(mask[jj] != 0.0)
                        flux.append(
                            num.sum(image[jj]) - len(W[0]) * bg /
                            (self.repFact * self.repFact))
                    flux = num.array(flux)

                self.nPix = num.sum(mask) / (self.repFact * self.repFact)
                self.sourceFlux = flux
                self.bg = bg
                self.bgstd = bgstd
                self.exptime = exptime
                self.magnitude = zpt - 2.5 * num.log10(
                    self.sourceFlux / self.exptime)

            else:
                pyl.show()
        if verbose:
            print num.sum(
                image), self.sourceFlux, self.bg, zpt - 2.5 * num.log10(flux)
Example #7
0
    #        bads[i][j] = False

    for i in range(h):
        for j in range(w):
            if n+j==len(unique_ims):
                break

            im = prefix+'-'+unique_ims[n+j]+'-'+chips[i]+postfix
            names[i][j] = im

            try:
                with fits.open(im) as han:
                    lu = han[0].data

                    (z1,z2) = tzscale.zscale(lu,contrast = 0.5)
                    normer = interval.ManualInterval(z1,z2)
                    d = normer(lu)
                    sps[i][j].imshow(d,interpolation = 'nearest')
            except:
                sps[i][j].imshow(np.zeros((30,30)),interpolation = 'nearest')

    n+=w
    if n==len(unique_ims):
        n = len(unique_ims)
        break
    print(n,len(unique_ims))
    pyl.show()

    for i in range(len(bads)):
        for j in range(len(bads[i])):
            if bads[i][j]:
Example #8
0
def blinky(event):
    global subplots, normed, patches, counter, nsp, ims, xs, ys, ras, decs, jds, X, Y, dist_low, dist_high, distantCandidate, corners, cut_widthx, cut_widthy, possCounter, possIms, possDrawing, fieldCuts, contrast

    if event.key == 'q':
        exit()

    if event.key in ['a', 'tab', 'd']:
        possIms = []
        possDrawing = False
        possCounter = 0
        for ii in range(len(patches)):
            if len(subplots) > 1:
                subplots[ii + 1].patches[-1].set_edgecolor('r')

    if event.key in ['a', 'tab']:
        counter += 1
        counter = counter % len(normed)
        if len(subplots) > 1:
            subplots[counter + 1].patches[-1].set_edgecolor('y')
    elif event.key in ['d']:
        counter -= 1
        counter = counter % len(normed)
        if len(subplots) > 1:
            subplots[counter + 1].patches[-1].set_edgecolor('y')

    if event.key in ['v']:
        comm = 'ds9 '
        for ii in range(len(ims)):
            comm += '{} -pan to {} {} -regions command "circle({},{},10)" '.format(
                ims[ii], xs[ii], ys[ii], xs[ii], ys[ii])
        print(comm)

    if event.key in ['a', 'tab', 'd']:
        draw(subplots, normed, counter, xs, ys, X, Y, cut_widthx, cut_widthy)
        pyl.draw()

    if event.key == 'g':
        print('Distant Object!', dist_low, dist_high)
        distantCandidate = True
    if event.key == 'c':
        print('Bad Object.')

    if event.key in ['g', 'c']:
        pyl.close()

    if event.key in ['b']:
        if len(possIms) == 0:
            print("Getting field image of ", ims[counter])
            possImss = findBGImage([ras[counter], decs[counter]], jds[counter],
                                   corners)
            possIms = []
            for k in range(len(possImss)):
                if possImss[k] not in ims:
                    possIms.append(possImss[k])
            print('    Have', len(possIms), 'images to choose from.\n')
            possDrawing = True

        if possDrawing and len(possIms) > 0:

            print('Loading', possIms[possCounter])

            if len(fieldCuts) >= possCounter - 1:

                with fits.open(possIms[possCounter]) as han:
                    fieldData = han[1].data
                    fieldHeader = han[1].header
                fWCS = wcs.WCS(fieldHeader)

                (xx, yy) = fWCS.all_world2pix(ras[0], decs[0], 0)
                #print(fWCS.all_pix2world(xx,yy,0),possCounter,'&&')
                (A, B) = fieldData.shape

                bg = 0.0
                big = np.zeros((A + cut_widthy * 4 + 1,
                                B + cut_widthx * 4 + 1)).astype('float64') + bg
                big[cut_widthy * 2:A + cut_widthy * 2,
                    cut_widthx * 2:cut_widthx * 2 + B] = fieldData
                cut = big[int(yy) + cut_widthy:int(yy) + 3 * cut_widthy,
                          int(xx) + cut_widthx:int(xx) + 3 * cut_widthx]

                (z1, z2) = numdisplay.zscale.zscale(fieldData,
                                                    contrast=contrast)

                normer = interval.ManualInterval(z1, z2)
                fieldCut = normer(cut)
                fieldCut[:5, :5] = 1.0
                fieldCut[-5:, :5] = 1.0
                fieldCut[-5:, -5:] = 1.0
                fieldCut[:5, -5:] = 1.0
                fieldCuts.append(np.copy(fieldCut))
            else:
                fieldCut = fieldCuts[possCounter]
            draw(subplots,
                 normed,
                 counter,
                 xs,
                 ys,
                 X,
                 Y,
                 cut_widthx,
                 cut_widthy,
                 fieldCut=fieldCut,
                 fmt='w-')

            possCounter = (possCounter + 1) % len(possIms)

            pyl.draw()

        else:
            print("...No background images available!\n")
    else:
        possDrawing = False
        possIms = []
        possCounter = 0

    if event.key == 'h':
        print('    g - real object')
        print('    c - bad  object')
        print('    a - blink backwards')
        print('d/tab - blink forwards')
        print('    q - quit without saving a candidates file.')
Example #9
0
        big = np.zeros((A + cut_widthy * 4 + 1,
                        B + cut_widthx * 4 + 1)).astype('float64') + bg
        big[cut_widthy * 2:A + cut_widthy * 2,
            cut_widthx * 2:cut_widthx * 2 + B] = datas[i]
        cut = big[int(Y[i][0]) + cut_widthy:int(Y[i][0]) + 3 * cut_widthy,
                  int(X[i][0]) + cut_widthx:int(X[i][0]) + 3 * cut_widthx]
        #cut = big[int(meanPos[1])+cut_width:int(meanPos[1])+3*cut_width,int(meanPos[0])+cut_width:int(meanPos[0])+3*cut_width]
        cutouts.append(np.copy(cut))

        if len(cut) == 0:
            normers.append([])
            normed.append(np.zeros((2 * cut_widthy + 1, 2 * cut_widthx + 1)))
        else:
            (z1, z2) = numdisplay.zscale.zscale(cutouts[-1], contrast=contrast)
            normers.append(interval.ManualInterval(z1, z2))
            normed.append(normers[-1](cutouts[-1]))

    #findBGImage(np.array([ras[1],decs[1]]),jds[1],corners)
    #exit()

    #subplots[0].imshow(normed[0])
    #subplots[0].plot([cut_width-20,cut_width-5],[cut_width,cut_width],'r-')
    #subplots[0].plot([cut_width+5,cut_width+20],[cut_width,cut_width],'r-')
    #ubplots[0].plot([cut_width,cut_width],[cut_width-20,cut_width-5],'r-')
    #subplots[0].plot([cut_width,cut_width],[cut_width+5,cut_width+20],'r-')
    #subplots[0].set_ylim(subplots[0].get_ylim()[::-1])

    distantCandidate = False
    counter = 0
    draw(subplots, normed, counter, xs, ys, X, Y, cut_widthx, cut_widthy)
Example #10
0
def showSources(pred,catalog,data,c=8,returnCuts = False,snr=None,appended=''):
    X = catalog['X_IMAGE']
    Y = catalog['Y_IMAGE']
    (A,B) = data.shape

    (z1,z2) = numdisplay.zscale.zscale(data,contrast = 0.5)
    normer = interval.ManualInterval(z1,z2)

    w = np.where(pred == 1)
    nsp = int(len(w[0])**0.5)
    if len(w[0])>nsp*nsp:
        nsp += 1
    if not returnCuts:
        fig = pyl.figure('good'+appended,figsize=(15,15))
        fig.subplots_adjust(hspace=0,wspace=0)
        gs = gridspec.GridSpec(nsp,nsp)

    else:
        cuts = []
        snrs = []
    for ii in range(nsp):
        for jj in range(nsp):
            if ii*nsp+jj<len(w[0]):
                x,y = int(X[w[0]][ii*nsp+jj])-1,int(Y[w[0]][ii*nsp+jj])-1
                cut = data[y-c:y+c+1,x-c:x+c+1]
                if np.min(cut.shape)<3:
                    continue
                if cut.shape[0]!=2*c+1 or cut.shape[1]!=2*c+1:
                    cut = cutter(x,y,cut,A,B,c,returnCuts)
                if returnCuts:
                    cuts.append(cut)
                    snrs.append(snr[w[0]][ii*nsp+jj])
                else:
                    try:
                        (z1,z2) = numdisplay.zscale.zscale(cut,contrast = 0.5)
                        normer = interval.ManualInterval(z1,z2)
                        Cut = normer(cut)
                        sp = pyl.subplot(gs[ii,jj])
                        pyl.imshow(Cut)
                    except:
                        pyl.imshow(cut)

    w = np.where(pred == -1)
    #print(len(w[0]))

    nsp = int(len(w[0])**0.5)
    if len(w[0])>nsp*nsp:
        nsp += 1

    fig = pyl.figure('bad'+appended,figsize=(15,15))
    fig.subplots_adjust(hspace=0,wspace=0)
    gs = gridspec.GridSpec(nsp,nsp)
    for ii in range(nsp):
        for jj in range(nsp):
            if ii*nsp+jj<len(w[0]):
                x,y = int(X[w[0]][ii*nsp+jj])-1,int(Y[w[0]][ii*nsp+jj])-1
                cut = data[y-c:y+c+1,x-c:x+c+1]
                if np.min(cut.shape)<3:
                    continue
                if cut.shape[0]!=2*c+1 or cut.shape[1]!=2*c+1:
                    cut = cutter(x,y,cut,A,B,c,returnCuts)

                if returnCuts:
                    cuts.append(cut)
                    snrs.append(-snr[w[0]][ii*nsp+jj]) #negative for bad sources
                else:
                    try:
                        (z1,z2) = numdisplay.zscale.zscale(cut,contrast = 0.5)
                        normer = interval.ManualInterval(z1,z2)
                        Cut = normer(cut)
                        sp = pyl.subplot(gs[ii,jj])
                        pyl.imshow(Cut)
                    except:
                        pyl.imshow(cut)

    if returnCuts:
        return (cuts,snrs)
    def __init__(self,
                 imagedata,
                 header,
                 imageSources,
                 refSources,
                 xo=512.0,
                 yo=512.0,
                 windowSize=13):

        if 'B_DEC_7' in header:
            self.b_ra = []
            self.b_dec = []
            for i in range(8):
                self.b_ra.append(header['B_RA_{}'.format(i)])
                self.b_dec.append(header['B_DEC_{}'.format(i)])
            self.b_ra = np.array(self.b_ra)
            self.b_dec = np.array(self.b_dec)
        if 'B_DEC_L5' in header:
            self.b_ra_low = []
            self.b_dec_low = []
            for i in range(6):
                self.b_ra_low.append(header['B_RA_L{}'.format(i)])
                self.b_dec_low.append(header['B_DEC_L{}'.format(i)])
            self.b_ra_low = np.array(self.b_ra_low)
            self.b_dec_low = np.array(self.b_dec_low)

        self.imageSources = imageSources
        self.refSources = refSources
        self._xo = xo
        self._yo = yo
        self.header = header
        self.header.set('RADESYSa', 'FK5')
        self.imagedata = np.copy(imagedata)

        #scratch crap pixel value fix
        mean = np.median(self.imagedata[::3, ::3])
        w = np.where(self.imagedata < 0)
        self.imagedata[w] = mean

        (self._z1, self._z2) = numdisplay.zscale.zscale(self.imagedata,
                                                        contrast=0.4)
        self._normer = interval.ManualInterval(self._z1, self._z2)

        self._wcs = WCS.WCS(header)
        if self.refSources is not None:
            self._refSourcePix = self._wcs.wcs_world2pix(
                self.refSources[:, :2], 0)

            (a, b) = self.imagedata.shape
            w = np.where((self._refSourcePix[:, 0] > -100)
                         & (self._refSourcePix[:, 0] < b + 100)
                         & (self._refSourcePix[:, 1] > -100)
                         & (self._refSourcePix[:, 1] < a + 100))
            self._refSourcePix = self._refSourcePix[w]
            self.refSources = self.refSources[w]

        self._initSourceSelection = None
        self._initRefSelection = None
        self.matches = []

        self.windowSize = windowSize
        self._lastKilled = []
Example #12
0
def removeTSF(data,
              xt,
              yt,
              bg,
              goodPSF,
              NAXIS1,
              NAXIS2,
              header,
              inputName,
              outfile=None,
              repfact=10,
              remove=True,
              verbose=False):
    '''Remove a TSF.
  If remove=False, will not remove, just saves postage-stamp around xt, yt.'''
    Data = (data[np.max([0, int(yt) -
                         200]):np.min([NAXIS2 - 1, int(yt) + 200]),
                 np.max([0, int(xt) -
                         200]):np.min([NAXIS1 - 1, int(xt) + 200])] - bg)
    dtransy = int(yt) - np.max([0, int(yt) - 200]) - 1
    dtransx = int(xt) - np.max([0, int(xt) - 200]) - 1
    m_obj = np.max(
        data[np.max([0, int(yt) - 5]):np.min([NAXIS2 -
                                              1, int(yt) + 5]),
             np.max([0, int(xt) - 5]):np.min([NAXIS1 -
                                              1, int(xt) + 5])])
    if remove:
        print("Should I be doing this?")
        fitter = MCMCfit.MCMCfitter(goodPSF, Data)
        fitter.fitWithModelPSF(dtransx + xt - int(xt),
                               dtransy + yt - int(yt),
                               m_in=m_obj / repfact**2.,
                               fitWidth=2,
                               nWalkers=10,
                               nBurn=10,
                               nStep=10,
                               bg=bg,
                               useLinePSF=True,
                               verbose=True,
                               useErrorMap=False)
        (fitPars, fitRange) = fitter.fitResults(0.67)
        print("\nfitPars = ", fitPars, "\n")
        print("\nfitRange = ", fitRange, "\n")
        if outfile is not None:
            outfile.write("\nfitPars={}".format(fitPars))
            outfile.write("\nfitRange={}".format(fitRange))
        removed = goodPSF.remove(fitPars[0],
                                 fitPars[1],
                                 fitPars[2],
                                 Data,
                                 useLinePSF=True)
        (z1, z2) = numdisplay.zscale.zscale(removed)
        normer = interval.ManualInterval(z1, z2)
        modelImage = goodPSF.plant(fitPars[0],
                                   fitPars[1],
                                   fitPars[2],
                                   Data,
                                   addNoise=False,
                                   useLinePSF=True,
                                   returnModel=True)
        if verbose:
            pyl.imshow(normer(goodPSF.lookupTable), origin='lower')
            pyl.show()
            pyl.imshow(normer(modelImage), origin='lower')
            pyl.show()
            pyl.imshow(normer(Data), origin='lower')
            pyl.show()
            pyl.imshow(normer(removed), origin='lower')
            pyl.show()
        hdu = pyf.PrimaryHDU(modelImage, header=header)
        list = pyf.HDUList([hdu])
        list.writeto(inputName + '_modelImage.fits', overwrite=True)
        hdu = pyf.PrimaryHDU(removed, header=header)
        list = pyf.HDUList([hdu])
        list.writeto(inputName + '_removed.fits', overwrite=True)
    else:
        (z1, z2) = numdisplay.zscale.zscale(Data)
        normer = interval.ManualInterval(z1, z2)
        if verbose:
            pyl.imshow(normer(goodPSF.lookupTable), origin='lower')
            pyl.show()
            pyl.imshow(normer(Data), origin='lower')
            pyl.show()
    hdu = pyf.PrimaryHDU(goodPSF.lookupTable, header=header)
    list = pyf.HDUList([hdu])
    list.writeto(inputName + '_lookupTable.fits', overwrite=True)
    hdu = pyf.PrimaryHDU(Data, header=header)
    list = pyf.HDUList([hdu])
    list.writeto(inputName + '_Data.fits', overwrite=True)
    return
Example #13
0
def chooseCentroid(data,
                   xt,
                   yt,
                   x0,
                   y0,
                   bg,
                   goodPSF,
                   NAXIS1,
                   NAXIS2,
                   repfact=10,
                   outfile=None,
                   centroid=False,
                   remove=False):
    ''' Choose between SExtractor position and predicted position.
  If desirable, use MCMC to fit the TSF to the object, thus centroiding on it.
  This is often NOT better than the SExtractor location, especially when the
  object is only barely trailed or when the sky has a gradient
  (near something bright).
  This fit is also used to remove the object from the image, later.
  fit takes time proportional to nWalkers*(2+nBurn+nStep).
  '''
    if (x0 == xt) & (y0 == yt):  # if SExtractor not find TNO, run centroid
        centroid = True
        SExFoundIt = False
    else:
        SExFoundIt = True
    Data = (data[np.max([0, int(yt) -
                         200]):np.min([NAXIS2 - 1, int(yt) + 200]),
                 np.max([0, int(xt) -
                         200]):np.min([NAXIS1 - 1, int(xt) + 200])] - bg)
    dtransy, dtransx = (int(yt) - np.max([0, int(yt) - 200]) - 1,
                        int(xt) - np.max([0, int(xt) - 200]) - 1)
    Zoom = (data[np.max([0, int(yt) - 15]):np.min([NAXIS2 -
                                                   1, int(yt) + 15]),
                 np.max([0, int(xt) - 15]):np.min([NAXIS1 -
                                                   1, int(xt) + 15])] - bg)
    zy, zx = (int(yt) - np.max([0, int(yt) - 15]) - 1,
              int(xt) - np.max([0, int(xt) - 15]) - 1)
    m_obj = np.max(
        data[np.max([0, int(yt) - 5]):np.min([NAXIS2 -
                                              1, int(yt) + 5]),
             np.max([0, int(xt) - 5]):np.min([NAXIS1 -
                                              1, int(xt) + 5])])
    xt0, yt0 = xt, yt
    while True:  # Breaks once a centroid has been selected.
        (z1, z2) = numdisplay.zscale.zscale(Zoom)
        normer = interval.ManualInterval(z1, z2)
        pyl.imshow(normer(Zoom), origin='lower')
        pyl.plot([zx + x0 - int(xt0)], [zy + y0 - int(yt0)], 'k*', ms=10)
        if SExFoundIt:
            pyl.plot([zx + xt0 - int(xt0)], [zy + yt0 - int(yt0)],
                     'w+',
                     ms=10,
                     mew=2)
        if centroid or remove:
            print("Should I be doing this?")
            xcent, ycent, fitPars, fitRange = runMCMCCentroid(
                goodPSF, Data, x0, y0, m_obj, bg, dtransx, dtransy, repfact)
            print("\nfitPars = ", fitPars, "\nfitRange = ", fitRange, "\n")
            if outfile is not None:
                outfile.write("\nfitPars={}".format(fitPars) +
                              "\nfitRange={}".format(fitRange))
            pyl.plot([zx + xcent - int(xt0)], [zy + ycent - int(yt0)],
                     'gx',
                     ms=10,
                     mew=2)
            print("\n")
            print("MCMCcentroid (green)  x,y = ", xcent, ycent)
            if SExFoundIt:
                print("SExtractor   (white)  x,y = ", xt, yt)
            print("Estimated    (black)  x,y = ", x0, y0)
            pyl.show()
            if SExFoundIt:
                yn = input('Accept MCMC centroid (m or c), ' +
                           'SExtractor centroid (S), or estimate (e)? ')
            else:
                yn = input('Accept MCMC centroid (M or c), ' +
                           'SExtractor centroid (s), or estimate (e)? ')
            if ('e' in yn) or ('E' in yn):  # if press e/E use estimate
                xt, yt = x0, y0
                break
            elif ('s' in yn) or ('S' in yn):
                break
            elif ('m' in yn) or ('M' in yn) or ('c' in yn) or ('C' in yn):
                xt, yt = xcent, ycent
                break
            else:
                if SExFoundIt:
                    yn = 'S'  # else do nothing, use SExtractor co-ordinates.
                else:
                    yn = 'M'  # else do nothing, use MCMC co-ordinates.
                    xt, yt = xcent, ycent
                break
        else:  # if not previously centroided, check whether needed
            if (x0 == xt) & (
                    y0 == yt):  # if SExtractor not find TNO, run centroid
                centroid = True
                SExFoundIt = False
            else:  # else pick between estimate, SExtractor and recentroiding
                print("SExtractor   (white)  x,y = ", xt, yt)
                print("Estimated    (black)  x,y = ", x0, y0)
                pyl.show()
                yn = input('Accept ' +
                           'SExtractor centroid (S), or estimate (e), ' +
                           ' or recentroid using MCMC (m or c)? ')
                if ('e' in yn) or ('E' in yn):  # if press e/E use estimate
                    xt, yt = x0, y0
                    break
                elif ('m' in yn) or ('M' in yn) or ('c' in yn) or ('C' in yn):
                    centroid = True
                    print("Setting centroid={}, will re-centroid".format(
                        centroid))
                else:
                    yn = 'S'  # else do nothing, use SExtractor co-ordinates.
                    break
    print("Coordinates chosen from this centroid: {}".format(yn))
    if outfile is not None:
        outfile.write("\nCoordinates chosen from this centroid: {}".format(yn))
    return xt, yt, yn
Example #14
0
def runSex_sep(file, fn, chip, mask_file, showProgress=False):

    if path.isfile(mask_file):
        with fits.open(mask_file) as han:
            mask = han[0].data == 0
    else:
        mask = None

    with fits.open(file) as han:
        mask_data = han[2].data.astype('float64')
        header = han[0].header
        header1 = han[1].header

    WCS = wcs.WCS(header1)

    (A, B) = data.shape
    if A == 4716:
        bh = 72
        bw = 64

    else:
        bh = 64
        bw = 72

    if showProgress:
        pyl.imshow(data, interpolation='nearest', cmap='gray', origin='lower')
        pyl.colorbar()
        pyl.show()

    #t1 = time.time()
    bg = sep.Background(data, mask=mask, bw=bw, bh=bh)

    bg_im = bg.back()

    if showProgress:
        pyl.imshow(bg_im, interpolation='nearest', cmap='gray', origin='lower')
        pyl.colorbar()
        pyl.show()

    if showProgress:
        data_sub = np.copy(data) - bg_im
        (z1, z2) = numdisplay.zscale.zscale(data_sub)
        normer = interval.ManualInterval(z1, z2)
        pyl.imshow(normer(data_sub),
                   interpolation='nearest',
                   cmap='gray',
                   origin='lower')
        pyl.colorbar()
        pyl.show()

    #print(bg.globalrms)
    obj = sep.extract(np.copy(data) - bg_im,
                      1.2,
                      minarea=3,
                      mask=mask,
                      err=bg.globalrms)
    np.save(savesPath + fn.replace('.fits', '.sex_save'), obj)

    #t2 = time.time()

    #print(chip,t2-t1,len(obj),bg.globalrms)
    #print

    (ra, dec) = WCS.all_pix2world(obj['x'], obj['y'], 0)

    arr = np.zeros(len(obj['x']),
                   dtype=[('x', 'float64'), ('y', 'float64'),
                          ('ra', 'float64'), ('dec', 'float64'),
                          ('flux', 'float64'), ('snr', 'float64'),
                          ('fwhm', 'float64')])
    arr['x'] = obj['x']
    arr['y'] = obj['y']
    arr['ra'] = ra
    arr['dec'] = dec
    arr['flux'] = obj['flux']
    arr['snr'] = obj['flux']
    #arr[:,1] = obj['y']
    print(arr[0])
    print(arr[0].shape)
    exit()

    exit()
    return obj