示例#1
0
def extract(hdu, ext=1, method='normal', section=[],
            minsize=3.0, thresh=3.0, convert=True):
    """For a given image, extract a 1D spectra from the image
       and write the spectra to the output file

    """

    ap_list = []
    i = ext
    if hdu[i].name == 'SCI':
        # set up the data, variance, and bad pixel frames
        # first step is to find the region to extract
        data_arr = hdu[i].data
        try:
            var_arr = hdu[hdu[i].header['VAREXT']].data
        except:
            var_arr = None
        try:
            bpm_arr = hdu[hdu[i].header['BPMEXT']].data
        except:
            bpm_arr = None
        var_arr = None
        bpm_arr = None

        xarr = np.arange(len(data_arr[0]))

        # convert using the WCS information
        try:
            w0 = hdu[i].header['CRVAL1']
            dw = hdu[i].header['CD1_1']
        except Exception as e:
            msg = 'Error on Ext %i: %s' % (i, e)
            raise Exception(msg)
        warr = w0 + dw * xarr

        # convert from air to vacuum
        if convert:
            warr = Spectrum.air2vac(warr)

        # set up the sections in case of findobj
        if section is None:
            section = findobj.findObjects(
                data_arr,
                method='median',
                specaxis=1,
                minsize=minsize,
                thresh=thresh,
                niter=5)

        # extract all of the  regions
        for sec in section:
            ap = apext.apext(warr, data_arr, ivar=var_arr)
            y1, y2 = sec
            ap.flatten(y1, y2)
            ap_list.append(ap)

    return ap_list
示例#2
0
文件: specsky.py 项目: dr-jpk/pysalt
def normalsky(xarr, data_arr, var_arr, section):
    """Determine the sky from a certain section of the image
       and subtract it from the data
  
       returns array
    """
    #create the variance 
    ap=apext.apext(xarr, data_arr, ivar=var_arr)
  
    #extract the regions for the sky
    y1,y2=section
    nrows=abs(y2-y1)
    ap.flatten(y1,y2)
    ap.ldata=ap.ldata/nrows
 
    return ap.ldata
示例#3
0
def normalsky(xarr, data_arr, var_arr, section):
    """Determine the sky from a certain section of the image
       and subtract it from the data

       returns array
    """
    # create the variance
    ap = apext.apext(xarr, data_arr, ivar=var_arr)

    # extract the regions for the sky
    y1, y2 = section

    nrows = abs(y2 - y1)
    ap.flatten(y1, y2)
    ap.ldata = ap.ldata / nrows

    return ap.ldata
示例#4
0
def extract(hdu,
            ext=1,
            method='normal',
            section=[],
            minsize=3.0,
            thresh=3.0,
            convert=True):
    """For a given image, extract a 1D spectra from the image
       and write the spectra to the output file

    """

    ap_list = []
    i = ext
    if hdu[i].name == 'SCI':
        # set up the data, variance, and bad pixel frames
        # first step is to find the region to extract
        data_arr = hdu[i].data
        try:
            var_arr = hdu[hdu[i].header['VAREXT']].data
        except:
            var_arr = None
        try:
            bpm_arr = hdu[hdu[i].header['BPMEXT']].data
        except:
            bpm_arr = None
        var_arr = None
        bpm_arr = None

        xarr = np.arange(len(data_arr[0]))

        # convert using the WCS information
        try:
            w0 = saltkey.get('CRVAL1', hdu[i])
            dw = saltkey.get('CD1_1', hdu[i])
        except Exception as e:
            msg = 'Error on Ext %i: %s' % (i, e)
            raise SALTSpecError(msg)
        warr = w0 + dw * xarr

        # convert from air to vacuum
        if convert:
            warr = Spectrum.air2vac(warr)

        # set up the sections in case of findobj
        if section is None:
            section = findobj.findObjects(data_arr,
                                          method='median',
                                          specaxis=1,
                                          minsize=minsize,
                                          thresh=thresh,
                                          niter=5)

        # extract all of the  regions
        for sec in section:
            ap = apext.apext(warr, data_arr, ivar=var_arr)
            y1, y2 = sec
            ap.flatten(y1, y2)
            ap_list.append(ap)

    return ap_list
示例#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
示例#6
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