Ejemplo n.º 1
0
def arcstraight(data, xarr, istart, ws=None, function='poly', order=3,
                rstep=1, nrows=1, dcoef=None, y1=None, y2=None, log=None, verbose=True):
    """For a given image, assume that the line given by istart is the fiducial and then calculate
       the transformation between each line and that line in order to straighten the arc

       returns Wavlenght solution
    """
    ImageSolution = {}

    #set up the edges 
    if y1 is None: y1=0
    if y2 is None: y2=data.shape[0]

    # extract the central row
    oxarr = xarr.copy()
    ofarr = data[istart]
    ws = WavelengthSolution.WavelengthSolution(xarr, xarr, function, order)
    ws.fit()
    ImageSolution[istart] = ws
    if isinstance(dcoef, str): 
       if dcoef=='':
          dcoef = None
       else:
          try:
              dcoef = [float(w) for w in dcoef.replace('[','').replace(']','').split()]
          except:
              raise SaltError('dcoef is not the right format')
      
    if dcoef is not None: ws.coef = dcoef

    data = nd.gaussian_filter(data, 3)

    # now step around the central row
    for i in range(rstep, int(0.5 * len(data)), rstep):
        for k in [istart - i, istart + i]:
            if k < y1 or k > y2: continue
            lws = getwsfromIS(k, ImageSolution)
            xarr = np.arange(len(data[k]))
            farr = apext.makeflat(data, k, k + nrows)
            nws = st.fitxcor(
                xarr,
                farr,
                oxarr,
                ofarr,
                lws,
                interptype='interp')
            ImageSolution[k] = nws

    return ImageSolution
Ejemplo n.º 2
0
 def regionChange(self, y1, y2):
     self.y1=y1
     self.y2=y2
     self.farr=apext.makeflat(self.specarr, self.y1, self.y2)
     #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, niter=self.niter, sigma=self.sigma, xp=[], wp=[], 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')
Ejemplo n.º 3
0
def arcstraight(data, xarr, istart, ws=None, function='poly', order=3,
                rstep=1, nrows=1, dcoef=None, ndstep=50, log=None, verbose=True):
    """For a given image, assume that the line given by istart is the fiducial and then calculate
       the transformation between each line and that line in order to straighten the arc

       returns Wavlenght solution
    """
    ImageSolution = {}

    # extract the central row
    oxarr = xarr.copy()
    ofarr = data[istart]
    print function, order
    ws = WavelengthSolution.WavelengthSolution(xarr, xarr, function, order)
    ws.fit()
    print ws.coef
    ImageSolution[istart] = ws
    if dcoef is None:
        docef = ws.coef * 0.0
        dcoef[0] = 10.0
    else:
        dcoef = np.array(dcoef)
    print dcoef

    data = nd.gaussian_filter(data, 3)

    # now step around the central row
    for i in range(rstep, int(0.5 * len(data)), rstep):
        for k in [istart - i, istart + i]:
            lws = getwsfromIS(k, ImageSolution)
            xarr = np.arange(len(data[k]))
            farr = apext.makeflat(data, k, k + nrows)
            nws = st.findxcor(
                xarr,
                farr,
                oxarr,
                ofarr,
                lws,
                dcoef=dcoef,
                ndstep=ndstep,
                best=True,
                inttype='interp',
                debug=False)
            ImageSolution[k] = nws
            print k, nws.coef

    return ImageSolution
Ejemplo n.º 4
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
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def arcstraight(data,
                xarr,
                istart,
                ws=None,
                function='poly',
                order=3,
                rstep=1,
                nrows=1,
                dcoef=None,
                y1=None,
                y2=None,
                log=None,
                verbose=True):
    """For a given image, assume that the line given by istart is the fiducial and then calculate
       the transformation between each line and that line in order to straighten the arc

       returns Wavlenght solution
    """
    ImageSolution = {}

    #set up the edges
    if y1 is None: y1 = 0
    if y2 is None: y2 = data.shape[0]

    # extract the central row
    oxarr = xarr.copy()
    ofarr = data[istart]
    ws = WavelengthSolution.WavelengthSolution(xarr, xarr, function, order)
    ws.fit()
    ImageSolution[istart] = ws
    if isinstance(dcoef, str):
        if dcoef == '':
            dcoef = None
        else:
            try:
                dcoef = [
                    float(w)
                    for w in dcoef.replace('[', '').replace(']', '').split()
                ]
            except:
                raise SaltError('dcoef is not the right format')

    if dcoef is not None: ws.coef = dcoef

    data = nd.gaussian_filter(data, 3)

    # now step around the central row
    for i in range(rstep, int(0.5 * len(data)), rstep):
        for k in [istart - i, istart + i]:
            if k < y1 or k > y2: continue
            lws = getwsfromIS(k, ImageSolution)
            xarr = np.arange(len(data[k]))
            farr = apext.makeflat(data, k, k + nrows)
            nws = st.fitxcor(xarr,
                             farr,
                             oxarr,
                             ofarr,
                             lws,
                             interptype='interp')
            ImageSolution[k] = nws

    return ImageSolution
Ejemplo n.º 7
0
   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, niter=5, istart=None,
                nrows=1, rstep=100, method='Zeropoint', ivar=None, cmap='gray', scale='zscale', contrast=1.0,
                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.filename=filename
        self.ImageSolution={}
        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)

        #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,niter=self.niter, mdiff=self.mdiff, sigma=self.sigma, 
                                   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)