Esempio n. 1
0
def createspectra(img, obsdate, minsize=5, thresh=3, skysection=[800,1000], smooth=False, maskzeros=True, clobber=True):
    """Create a list of spectra for each of the objects in the images"""
    #okay we need to identify the objects for extraction and identify the regions for sky extraction
    #first find the objects in the image
    hdu=pyfits.open(img)
    target=hdu[0].header['OBJECT']
    propcode=hdu[0].header['PROPID']
    airmass=hdu[0].header['AIRMASS']
    exptime=hdu[0].header['EXPTIME']

    if smooth:
       data=smooth_data(hdu[1].data)
    else:
       data=hdu[1].data

    #replace the zeros with the average from the frame
    if maskzeros:
       mean,std=iterstat(data[data>0])
       rdata=np.random.normal(mean, std, size=data.shape)
       print mean, std
       data[data<=0]=rdata[data<=0]

    #find the sections in the images
    section=findobj.findObjects(data, method='median', specaxis=1, minsize=minsize, thresh=thresh, niter=5)
    print section

    #use a region near the center to create they sky
    skysection=findskysection(section, skysection)
    print skysection
 
    #sky subtract the frames
    shdu=skysubtract(hdu, method='normal', section=skysection)
    if os.path.isfile('s'+img): os.remove('s'+img)
    shdu.writeto('s'+img)
 
    spec_list=[]
    #extract the spectra
    #extract the comparison spectrum
    section=findobj.findObjects(shdu[1].data, method='median', specaxis=1, minsize=minsize, thresh=thresh, niter=5)
    print section
    for j in range(len(section)):
        ap_list=extract(shdu, method='normal', section=[section[j]], minsize=minsize, thresh=thresh, convert=True)
        ofile='%s.%s_%i_%i.txt' % (target, obsdate, extract_number(img), j)
        write_extract(ofile, [ap_list[0]], outformat='ascii', clobber=clobber)
        spec_list.append([ofile, airmass, exptime, propcode])

    return spec_list
Esempio n. 2
0
def specreduce(images, badpixelimage=None, caltype='rss',
               function='polynomial', order=3,
               skysub=True, skysection=None, findobj=False,
               objsection=None, thresh=3.0,
               clobber=True, logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # open the badpixelstruct
        if saltio.checkfornone(badpixelimage):
            badpixelstruct = saltio.openfits(badpixelimage)
        else:
            badpixelstruct = None

        # set up the section for sky estimate
        if skysection is not None:
            skysection = makesection(skysection)
        else:
            skysub = False

        # set up the section for sky estimate
        section = saltio.checkfornone(objsection)
        if section is not None:
            sections = saltio.getSection(section, iraf_format=False)
            objsection = []
            for i in range(0, len(sections), 2):
                objsection.append((sections[i], sections[i + 1]))

        # determine the wavelength solutions
        if caltype == 'line':
            calc_wavesol(infiles)

        # correct the images
        for img in infiles:
            # open the fits file
            struct = saltio.openfits(img)

            # prepare filep
            log.message('Preparing %s' % img)
            struct = prepare(struct, badpixelstruct)

            # rectify the spectrum
            log.message('Rectifying %s using %s' % (img, caltype))
            struct = rectify(
                struct,
                None,
                caltype=caltype,
                function=function,
                order=order)

            # sky subtract the spectrum
            # assumes the data is long slit and in the middle of the field
            if skysub:
                log.message('Subtracting the sky from %s' % img)

                struct = skysubtract(
                    struct,
                    method='normal',
                    section=skysection)

            # extract the spectrum
            log.message('Extracting the spectrum from %s' % img)
            print objsection
            aplist = extract(
                struct,
                method='normal',
                section=objsection,
                thresh=thresh)
            oimg = os.path.dirname(
                os.path.abspath(img)) + '/s' + os.path.basename(img.strip())
            ofile = oimg[:-5] + '.txt'
            write_extract(ofile, aplist, clobber=clobber)

            # write FITS file
            log.message('Writing 2-D corrected image as %s' % oimg)
            saltio.writefits(struct, oimg, clobber=clobber)
            saltio.closefits(struct)
Esempio n. 3
0
def specreduce(
    images,
    badpixelimage=None,
    caltype="rss",
    function="polynomial",
    order=3,
    skysub=True,
    skysection=None,
    findobj=False,
    objsection=None,
    thresh=3.0,
    clobber=True,
    logfile="salt.log",
    verbose=True,
):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack("Input", images)

        # open the badpixelstruct
        if saltio.checkfornone(badpixelimage):
            badpixelstruct = saltio.openfits(badpixelimage)
        else:
            badpixelstruct = None

        # set up the section for sky estimate
        if skysection is not None:
            skysection = makesection(skysection)
        else:
            skysub = False

        # set up the section for sky estimate
        section = saltio.checkfornone(objsection)
        if section is not None:
            sections = saltio.getSection(section, iraf_format=False)
            objsection = []
            for i in range(0, len(sections), 2):
                objsection.append((sections[i], sections[i + 1]))

        # determine the wavelength solutions
        if caltype == "line":
            calc_wavesol(infiles)

        # correct the images
        for img in infiles:
            # open the fits file
            struct = saltio.openfits(img)

            # prepare filep
            log.message("Preparing %s" % img)
            struct = prepare(struct, badpixelstruct)

            # rectify the spectrum
            log.message("Rectifying %s using %s" % (img, caltype))
            struct = rectify(struct, None, caltype=caltype, function=function, order=order)

            # sky subtract the spectrum
            # assumes the data is long slit and in the middle of the field
            if skysub:
                log.message("Subtracting the sky from %s" % img)

                struct = skysubtract(struct, method="normal", section=skysection)

            # extract the spectrum
            log.message("Extracting the spectrum from %s" % img)
            print objsection
            aplist = extract(struct, method="normal", section=objsection, thresh=thresh)
            oimg = os.path.dirname(os.path.abspath(img)) + "/s" + os.path.basename(img.strip())
            ofile = oimg[:-5] + ".txt"
            write_extract(ofile, aplist, clobber=clobber)

            # write FITS file
            log.message("Writing 2-D corrected image as %s" % oimg)
            saltio.writefits(struct, oimg, clobber=clobber)
            saltio.closefits(struct)