Exemple #1
0
def identify(img, oimg, slines, sfluxes, guesstype, guessfile, function, order,  
             rstep, interact, clobber, verbose):
   """For a given image, find the solution for each row in the file.  Use the appropriate first guess and 
      guess type along with the appropriate function and order for the fit.

      Write out the new image with the solution in the headers and/or as a table in the multi-extension
      fits file

       returns the status
   """
   status=0
   ImageSolution={}

   #Open up the image
   hdu=saltsafeio.openfits(img)   

   #Read in important keywords

   #determine the central row and read it in
   try:
       data=hdu[1].data
       midline=int(0.5*len(data))
       xarr=np.arange(len(data[midline]))
       specarr=st.flatspectrum(xarr, data[midline, :], mode='poly', order=2)
   except Exception, e:
       message = 'Unable to read in data array in %s because %s' % (img, e)
       raise SALTSpecError(message)
Exemple #2
0
 def regionChange(self, y1, y2):
     self.y1 = y1
     self.y2 = y2
     self.farr = apext.makeflat(self.specarr, self.y1, self.y2)
     self.farr = st.flatspectrum(self.xarr, self.farr, order=self.subback)
     # set up variables
     self.ws = self.newWS(0.5 * (self.y1 + self.y2))
     self.arcdisplay = ArcDisplay(
         self.xarr,
         self.farr,
         self.slines,
         self.sfluxes,
         self.ws,
         specarr=self.specarr,
         res=self.res,
         dres=self.dres,
         smooth=self.smooth,
         niter=self.niter,
         sigma=self.sigma,
         xp=[],
         wp=[],
         textcolor=self.textcolor,
         preprocess=self.preprocess, 
         log=self.log,
         verbose=self.verbose)
     self.arcPage = arcWidget(
         self.arcdisplay,
         hmin=self.hmin,
         wmin=self.wmin,
         y1=self.y1,
         y2=self.y2)
     self.connect(self.arcPage, QtCore.SIGNAL('savews()'), self.saveWS)
     # set up the residual page
     self.errPage = errWidget(
         self.arcdisplay,
         hmin=self.hmin,
         wmin=self.wmin)
     # reset the pages
     self.tabWidget.removeTab(2)
     self.tabWidget.removeTab(1)
     self.tabWidget.insertTab(1, self.arcPage, 'Arc')
     self.tabWidget.insertTab(2, self.errPage, 'Residual')
Exemple #3
0
def runsolution(xarr,
                specarr,
                slines,
                sfluxes,
                ws,
                func,
                ivar=None,
                fline=True,
                oneline=False,
                farr=None,
                rstep=20,
                istart=None,
                nrows=1,
                dsigma=5,
                dniter=5,
                subback=0,
                smooth=0,
                res=2.0,
                dres=0.1,
                log=None,
                verbose=True,
                **kwargs):
    """Starting in the middle of the image, it will determine the solution
       by working its way out to either edge and compiling all the results into
       ImageSolution.  The image solution is only saved if the sigma is less than dres.

       xarr--Full range in x of pixels to solve for

       specarr--Input 2D flux

       func--function to use for the solution

       fline--True if spectral lines are in array format.  If False, spectral
              lines are assumed to be in line format

       oneline--whether to measure one line or all lines


    """
    # set up the variables
    ImageSolution = {}

    # Setup the central line if it isn't specified
    if istart is None:
        istart = int(0.5 * len(specarr))

    # set up the flux from the central line (or the line specified by the user
    # in istart)
    if farr is None:
        specext = apext.apext(xarr, specarr, ivar=ivar)
        farr = apext.makeflat(specarr, istart, istart + nrows)
        farr = st.flatspectrum(xarr, farr, mode='poly', order=subback)

    # smooth the data
    if smooth > 0:
        farr = st.smooth_spectra(xarr, farr, sigma=smooth)

    # detect the lines
    cxp = st.detect_lines(xarr, farr, dsigma, dniter)
    nlines = len(cxp)

    # first set up the artificial spectrum
    if fline:
        swarr = slines
        sfarr = sfluxes
    else:
        swarr, sfarr = st.makeartificial(slines, sfluxes, farr.max(), res,
                                         dres)

    # find the solution for the central wavelegnth
    k = istart
    min_lines = 0.1 * len(cxp)
    if oneline:
        mws = solution(xarr,
                       farr,
                       swarr,
                       sfarr,
                       ws,
                       func,
                       min_lines=min_lines,
                       dsigma=dsigma,
                       dniter=dniter,
                       **kwargs)
        for i in range(dniter - 1):
            mws = solution(xarr,
                           farr,
                           swarr,
                           sfarr,
                           mws,
                           func,
                           min_lines=min_lines,
                           dsigma=dsigma,
                           dniter=dniter,
                           **kwargs)

        if verbose and mws is not None:
            msg = "%5i %3i %3.2f" % (k, mws.func.mask.sum(),
                                     mws.sigma(mws.func.x, mws.func.y))
            if log is not None:
                log.message(msg)
        return mws

    # now loop through each step, and calculate the wavelengths for the given
    if log is not None:
        log.message('%5s %3s %4s' % ('Line', 'N', 'RMS'), with_header=False)

    for i in range(0, int(0.5 * len(specarr)), rstep):
        for k in [istart - i, istart + i]:

            if k in ImageSolution.keys():
                continue

            lws = getwsfromIS(k, ImageSolution, default_ws=ws)

            # set up the flux from the set of lines
            farr = apext.makeflat(specarr, k, k + nrows)

            if smooth > 0:
                farr = st.smooth_spectra(xarr, farr, sigma=smooth)

            # continuum correct the spectrum if possible
            try:
                farr = st.flatspectrum(xarr, farr, mode='poly', order=subback)
            except:
                continue

            # find the solution to the lines
            fws = solution(xarr,
                           farr,
                           swarr,
                           sfarr,
                           lws,
                           func,
                           min_lines=min_lines,
                           dsigma=dsigma,
                           dniter=dniter,
                           **kwargs)
            if fws is not None:
                if fws.sigma(fws.func.x, fws.func.y) < dres:
                    ImageSolution[k] = fws

                if verbose:
                    p_new = i * 100.0 / (0.5 * len(specarr))
                    # ctext='Percentage Complete: %d %d %f\r' % (i,p_new, time.clock()) #p_new
                    # sys.stdout.write(ctext)#
                    # sys.stdout.flush()
                    msg = "%5i %3i %3.2f" % (k, fws.func.mask.sum(),
                                             fws.sigma(fws.func.x, fws.func.y))
                    if log is not None:
                        log.message(msg, with_header=False)

    return ImageSolution
Exemple #4
0
                             interact=interact, verbose=verbose)
   ImageSolution[midline]=ws

   #now using that information, find the fit to all other data
   #repeat in the same way if interactive is selected
   #start in the middle and then go either way
   uws=WavelengthSolution.WavelengthSolution(ws.x_arr, ws.w_arr, order=order, function=function)
   uws.fit()
   lws=WavelengthSolution.WavelengthSolution(ws.x_arr, ws.w_arr, order=order, function=function)
   lws.fit()
   zpfind=True 
   dcstep=3
   nstep=50 
   for i in range(rstep,int(0.5*len(data)), rstep):
   #for i in range(1,10, rstep):
           specarr=st.flatspectrum(xarr, data[midline-i, :], mode='poly', order=2)
           lws=findwavelengthsolution(xarr, specarr, slines, sfluxes, lws, xdiff, 
                                      function, order, zeropoint=zpfind, dc=dcstep, nstep=nstep,
                                      interact=interact, verbose=verbose)
           ImageSolution[midline-i]=lws
           specarr=st.flatspectrum(xarr, data[i+midline, :], mode='poly', order=2)
           uws=findwavelengthsolution(xarr, specarr, slines, sfluxes, uws, xdiff, 
                                      function, order, zeropoint=zpfind, dc=dcstep, nstep=nstep,
                                      interact=interact, verbose=verbose)
           ImageSolution[midline+i]=uws
           if verbose:
              p_new=i*100.0/(0.5*len(data))
              ctext='Percentage Complete: %d %d %f\r' % (i,p_new, time.clock()) #p_new
              sys.stdout.write(ctext)
              sys.stdout.flush()
def runsolution(xarr, specarr, slines, sfluxes, ws, func, ivar=None,
                fline=True, oneline=False, farr=None, rstep=20,
                istart=None, nrows=1, dsigma=5, dniter=5, subback=0, smooth=0, res=2.0,
                dres=0.1, log=None, verbose=True, **kwargs):
    """Starting in the middle of the image, it will determine the solution
       by working its way out to either edge and compiling all the results into
       ImageSolution.  The image solution is only saved if the sigma is less than dres.

       xarr--Full range in x of pixels to solve for

       specarr--Input 2D flux

       func--function to use for the solution

       fline--True if spectral lines are in array format.  If False, spectral
              lines are assumed to be in line format

       oneline--whether to measure one line or all lines


    """
    # set up the variables
    ImageSolution = {}

    # Setup the central line if it isn't specified
    if istart is None:
        istart = int(0.5 * len(specarr))

    # set up the flux from the central line (or the line specified by the user
    # in istart)
    if farr is None:
        specext = apext.apext(xarr, specarr, ivar=ivar)
        farr = apext.makeflat(specarr, istart, istart + nrows)
        farr = st.flatspectrum(xarr, farr, mode='poly', order=subback)

    # smooth the data
    if smooth > 0:
        farr = st.smooth_spectra(xarr, farr, sigma=smooth)

    # detect the lines
    cxp = st.detect_lines(xarr, farr, dsigma, dniter)
    nlines = len(cxp)

    # first set up the artificial spectrum
    if fline:
        swarr = slines
        sfarr = sfluxes
    else:
        swarr, sfarr = st.makeartificial(
            slines, sfluxes, farr.max(), res, dres)

    # find the solution for the central wavelegnth
    k = istart
    min_lines = 0.1 * len(cxp)
    if oneline:
        mws = solution(xarr, farr, swarr, sfarr, ws, func,
                       min_lines=min_lines, dsigma=dsigma, dniter=dniter, **kwargs)
        for i in range(dniter - 1):
            mws = solution(xarr, farr, swarr, sfarr, mws, func,
                           min_lines=min_lines, dsigma=dsigma, dniter=dniter, **kwargs)

        if verbose and mws is not None:
            msg = "%5i %3i %3.2f" % (k,
                                     mws.func.mask.sum(),
                                     mws.sigma(
                                         mws.func.x,
                                         mws.func.y))
            if log is not None:
                log.message(msg)
        return mws

    # now loop through each step, and calculate the wavelengths for the given
    if log is not None:
        log.message('%5s %3s %4s' % ('Line', 'N', 'RMS'), with_header=False)

    for i in range(0, int(0.5 * len(specarr)), rstep):
        for k in [istart - i, istart + i]:

            if k in ImageSolution.keys():
                continue

            lws = getwsfromIS(k, ImageSolution, default_ws=ws)

            # set up the flux from the set of lines
            farr = apext.makeflat(specarr, k, k + nrows)

            if smooth > 0:
                farr = st.smooth_spectra(xarr, farr, sigma=smooth)

            # continuum correct the spectrum if possible
            try:
                farr = st.flatspectrum(xarr, farr, mode='poly', order=subback)
            except:
                continue

            # find the solution to the lines
            fws = solution(xarr, farr, swarr, sfarr, lws, func,
                           min_lines=min_lines, dsigma=dsigma, dniter=dniter, **kwargs)
            if fws is not None:
                if fws.sigma(fws.func.x, fws.func.y) < dres:
                    ImageSolution[k] = fws

                if verbose:
                    p_new = i * 100.0 / (0.5 * len(specarr))
                    # ctext='Percentage Complete: %d %d %f\r' % (i,p_new, time.clock()) #p_new
                    # sys.stdout.write(ctext)#
                    # sys.stdout.flush()
                    msg = "%5i %3i %3.2f" % (k,
                                             fws.func.mask.sum(),
                                             fws.sigma(
                                                 fws.func.x,
                                                 fws.func.y))
                    if log is not None:
                        log.message(msg, with_header=False)

    return ImageSolution
    def __init__(self, xarr, specarr, slines, sfluxes, ws, hmin=150, wmin=400, mdiff=20,
                 filename=None, res=2.0, dres=0.1, dc=20, ndstep=20, sigma=5, smooth=0, niter=5, istart=None,
                 nrows=1, rstep=100, method='Zeropoint', ivar=None, cmap='gray', scale='zscale', contrast=1.0,
                 subback=0, textcolor='green', log=None, verbose=True):
        """Default constructor."""

        # set up the variables
        if istart is None:
            self.y1 = int(0.5 * len(specarr))
        else:
            self.y1 = istart
        self.y2 = self.y1 + nrows
        self.specarr = specarr
        self.xarr = xarr
        self.ivar = ivar
        self.slines = slines
        self.sfluxes = sfluxes
        self.hmin = hmin
        self.wmin = wmin
        self.ws = ws
        self.res = res
        self.dres = dres
        self.mdiff = mdiff
        self.sigma = sigma
        self.niter = int(niter)
        self.nrows = nrows
        self.rstep = rstep
        self.dc = dc
        self.ndstep = ndstep
        self.method = method
        self.cmap = cmap
        self.scale = scale
        self.contrast = contrast
        self.smooth = smooth
        self.subback = subback
        self.filename = filename
        self.ImageSolution = {}
        self.textcolor = textcolor
        self.log = log
        self.verbose = verbose

        # Setup widget
        QtGui.QMainWindow.__init__(self)

        # Set main widget
        self.main = QtGui.QWidget(self)

        # Set window title
        self.setWindowTitle("InterIdentify")

        # create the Image page
        self.imagePage = imageWidget(self.specarr, y1=self.y1, y2=self.y2, hmin=self.hmin, wmin=self.wmin, cmap=self.cmap,
                                     rstep=self.rstep, name=self.filename, scale=self.scale, contrast=self.contrast, log=self.log)

        # set up the arc page
        self.farr = apext.makeflat(self.specarr, self.y1, self.y2)
        self.farr = st.flatspectrum(self.xarr, self.farr, order=self.subback)

        # set up variables
        self.arcdisplay = ArcDisplay(xarr, self.farr, slines, sfluxes, self.ws, specarr=self.specarr,
                                     res=self.res, dres=self.dres, dc=self.dc, ndstep=self.ndstep, xp=[], wp=[],
                                     method=self.method, smooth=self.smooth, niter=self.niter, mdiff=self.mdiff,
                                     sigma=self.sigma, textcolor=self.textcolor, log=self.log, verbose=self.verbose)
        self.arcPage = arcWidget(
            self.arcdisplay,
            hmin=hmin,
            wmin=wmin,
            y1=self.y1,
            y2=self.y2,
            name=self.filename)
        # set up the residual page
        self.errPage = errWidget(self.arcdisplay, hmin=hmin, wmin=wmin)

        # create the tabs
        self.tabWidget = QtGui.QTabWidget()
        self.tabWidget.addTab(self.imagePage, 'Image')
        self.tabWidget.addTab(self.arcPage, 'Arc')
        self.tabWidget.addTab(self.errPage, 'Residual')

        # layout the widgets
        mainLayout = QtGui.QVBoxLayout(self.main)
        mainLayout.addWidget(self.tabWidget)
        # self.setLayout(mainLayout)

        # Set focus to main widget
        # self.main.setFocus()

        # Set the main widget as the central widget
        self.setCentralWidget(self.main)

        # Destroy widget on close
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

        # Close when config dialog is closed
        # self.connect(self.conf, QtCore.SIGNAL('destroyed()'),
        #              self, QtCore.SLOT('close()'))
        self.connect(self.tabWidget, QtCore.SIGNAL('currentChanged(int)'),
                     self.currentChanged)
        self.connect(self.imagePage, QtCore.SIGNAL('regionChange(int,int)'),
                     self.regionChange)
        self.connect(self.imagePage, QtCore.SIGNAL('runauto(int, int, int)'),
                     self.runauto)
        self.connect(self.arcPage, QtCore.SIGNAL('savews()'), self.saveWS)
        self.connect(self.arcdisplay, QtCore.SIGNAL('quit()'), self.close)