Example #1
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
Example #2
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
Example #3
0
    def __init__(self, xarr, farr, slines, sfluxes, ws, xp=[], wp=[], mdiff=20, specarr=None,
                 res=2.0, dres=0.1, dc=20, ndstep=20, sigma=5, smooth=0, niter=5, method='MatchZero',
                 textcolor='green', log=None, verbose=True):
        """Default constructor."""
        QtGui.QWidget.__init__(self)

        # Initialize base class
        self.arcfigure = MplCanvas()
        self.errfigure = MplCanvas()

        # Add central axes instance
        self.axes = self.arcfigure.figure.add_subplot(111)
        self.erraxes = self.errfigure.figure.add_subplot(111)

        # Connect mouse events
        self.arcfigure.connectMatplotlibMouseMotion()
        self.arcfigure.mpl_connect('button_press_event', self.onButtonPress)
        self.arcfigure.mpl_connect('key_press_event', self.onKeyPress)

        self.errfigure.connectMatplotlibMouseMotion()
        self.errfigure.mpl_connect('button_press_event', self.onButtonPress)
        self.errfigure.mpl_connect('key_press_event', self.onKeyPress)

        # load the data
        self.xarr = xarr
        self.farr = farr
        self.slines = slines
        self.sfluxes = sfluxes
        self.ws = ws
        self.orig_ws = copy.deepcopy(ws)
        self.specarr = specarr
        self.mdiff = mdiff
        self.sigma = sigma
        self.niter = int(niter)
        self.smooth = int(smooth)
        self.res = res
        self.dres = dres
        self.dc = dc
        self.sections = 6
        self.ndstep = ndstep
        self.method = method
        self.textcolor = textcolor
        self.log = log
        self.verbose = True

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

        self.xp = xp
        self.wp = wp

        self.rms = res

        # set up the artificial spectra
        self.spectrum = Spectrum.Spectrum(
            self.slines,
            self.sfluxes,
            dw=self.dres,
            stype='line',
            sigma=self.res)
        self.swarr = self.spectrum.wavelength
        self.sfarr = self.spectrum.flux * \
            self.farr.max() / self.spectrum.flux.max()

        # set up the wavelength solution
        if self.ws.function == 'line':
            self.ws.set_xarr(self.xarr)
            self.ws.farr = self.farr
            self.ws.spectrum = self.spectrum

        # set up the list of deleted points
        self.dxp = []
        self.dwp = []

        # set up other variables
        self.isArt = False
        self.isFeature = False

        # Set display parameters
        self.xmin = self.xarr.min()
        self.xmax = self.xarr.max()
        self.ymin = self.farr.min()
        self.ymax = self.farr.max()