Exemple #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
Exemple #2
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
Exemple #3
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
Exemple #4
0
def extract_spectra(img, yc=None, oy=10, dy=50, minsize=5, thresh=3, findobject=False, 
                    niter=5, calfile=None, smooth=False, maskzeros=False, 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

    #skynormalize the data
    #specslitnormalize(img, 'n'+img, '', response=None, response_output=None, order=3, conv=1e-2, niter=20,
    #                 startext=0, clobber=False,logfile='salt.log',verbose=True)

    print 'Extract Spectra from ', img
    hdu=pyfits.open(img)
    target=hdu[0].header['OBJECT'].replace(' ', '')
    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=mean  np.random.normal(mean, std, size=data.shape)
       print mean, std
       data[data<=0]=mean #rdata[data<=0]

    #use manual intervention to get section
    if findobject:
       section=findobj.findObjects(data, method='median', specaxis=1, minsize=minsize, thresh=thresh, niter=niter)
         
       if yc is None and len(section)>0:
          yc = np.mean(section[0])
       elif len(section)>0:
          diff = 1e6
          for i in range(len(section)):
              y = np.mean(section[i])
              if abs(y-yc) < diff: 
                 bestyc = y
                 diff = abs(y-yc)
          yc = bestyc
              

    if yc is None:
        os.system('ds9 %s &' % img)
    print len(hdu)
    if len(hdu)==2: 
        print 'Using basic extraction'
        if yc is None:
           y1=int(raw_input('y1:'))
           y2=int(raw_input('y2:'))
           sy1=int(raw_input('sky y1:'))
           sy2=int(raw_input('sky y2:'))
        ap_list=extract(hdu, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=True)
        sk_list=extract(hdu, method='normal', section=[(sy1,sy2)], minsize=minsize, thresh=thresh, convert=True)
        
        ap_list[0].ldata=ap_list[0].ldata-float(y2-y1)/(sy2-sy1)*sk_list[0].ldata
    
        ofile='%s.%s_%i.txt' % (target, extract_date(img), extract_number(img))
        write_extract(ofile, [ap_list[0]], outformat='ascii', clobber=clobber)

        w, f, e = np.loadtxt(ofile, usecols=(0,1,2), unpack=True)
        w, f, e=cleanspectra(w, f, e, neg=True) 
        m = (w>3900)*(w<8100)
        write_spectra(ofile, w[m], f[m], e[m])
  
    else: 
        print 'Using advanced extraction'

        if yc is None: yc=int(raw_input('yc:'))
        

        w0=hdu[1].header['CRVAL1']
        dw=hdu[1].header['CD1_1']
        xarr = np.arange(len(hdu[1].data[0]))
        warr=w0+dw*xarr
        print warr.min(), warr.max()
       
        print hdu[1].data[yc, 1462], hdu[2].data[yc,1462]
        warr, madata, var = skysub_region(warr, hdu[1].data, hdu[2].data, hdu[3].data, yc, oy, dy)
        print warr.min(), warr.max()
        w, f, e = masked_extract(warr, madata[yc-oy:yc+oy, :], var[yc-oy:yc+oy, :])
        print yc
        ofile='%s.%s_%i_%i.txt' % (target, extract_date(img), extract_number(img), yc)
        write_spectra(ofile, w, f, e)
        

    if calfile is not None: 
       extfile=iraf.osfn("pysalt$data/site/suth_extinct.dat")
       speccal(ofile, ofile.replace("txt", "spec"), calfile, extfile, airmass, exptime, clobber=True, logfile='salt.log', verbose=True)
    
    spec_list=[ofile, airmass, exptime, propcode]

    return spec_list
Exemple #5
0
def extract_spectra(img,
                    yc=None,
                    oy=10,
                    dy=50,
                    minsize=5,
                    thresh=3,
                    findobject=False,
                    niter=5,
                    calfile=None,
                    smooth=False,
                    maskzeros=False,
                    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

    #skynormalize the data
    #specslitnormalize(img, 'n'+img, '', response=None, response_output=None, order=3, conv=1e-2, niter=20,
    #                 startext=0, clobber=False,logfile='salt.log',verbose=True)

    print 'Extract Spectra from ', img
    hdu = pyfits.open(img)
    target = hdu[0].header['OBJECT'].replace(' ', '')
    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=mean  np.random.normal(mean, std, size=data.shape)
        print mean, std
        data[data <= 0] = mean  #rdata[data<=0]

    #use manual intervention to get section
    if findobject:
        section = findobj.findObjects(data,
                                      method='median',
                                      specaxis=1,
                                      minsize=minsize,
                                      thresh=thresh,
                                      niter=niter)

        if yc is None and len(section) > 0:
            yc = np.mean(section[0])
        elif len(section) > 0:
            diff = 1e6
            for i in range(len(section)):
                y = np.mean(section[i])
                if abs(y - yc) < diff:
                    bestyc = y
                    diff = abs(y - yc)
            yc = bestyc

    if yc is None:
        os.system('ds9 %s &' % img)
    print len(hdu)
    if len(hdu) == 2:
        print 'Using basic extraction'
        if yc is None:
            y1 = int(raw_input('y1:'))
            y2 = int(raw_input('y2:'))
            sy1 = int(raw_input('sky y1:'))
            sy2 = int(raw_input('sky y2:'))
        ap_list = extract(hdu,
                          method='normal',
                          section=[(y1, y2)],
                          minsize=minsize,
                          thresh=thresh,
                          convert=True)
        sk_list = extract(hdu,
                          method='normal',
                          section=[(sy1, sy2)],
                          minsize=minsize,
                          thresh=thresh,
                          convert=True)

        ap_list[0].ldata = ap_list[0].ldata - float(y2 - y1) / (
            sy2 - sy1) * sk_list[0].ldata

        ofile = '%s.%s_%i.txt' % (target, extract_date(img),
                                  extract_number(img))
        write_extract(ofile, [ap_list[0]], outformat='ascii', clobber=clobber)

        w, f, e = np.loadtxt(ofile, usecols=(0, 1, 2), unpack=True)
        w, f, e = cleanspectra(w, f, e, neg=True)
        m = (w > 3900) * (w < 8100)
        write_spectra(ofile, w[m], f[m], e[m])

    else:
        print 'Using advanced extraction'

        if yc is None: yc = int(raw_input('yc:'))

        w0 = hdu[1].header['CRVAL1']
        dw = hdu[1].header['CD1_1']
        xarr = np.arange(len(hdu[1].data[0]))
        warr = w0 + dw * xarr
        print warr.min(), warr.max()

        print hdu[1].data[yc, 1462], hdu[2].data[yc, 1462]
        warr, madata, var = skysub_region(warr, hdu[1].data, hdu[2].data,
                                          hdu[3].data, yc, oy, dy)
        print warr.min(), warr.max()
        w, f, e = masked_extract(warr, madata[yc - oy:yc + oy, :],
                                 var[yc - oy:yc + oy, :])
        print yc
        ofile = '%s.%s_%i_%i.txt' % (target, extract_date(img),
                                     extract_number(img), yc)
        write_spectra(ofile, w, f, e)

    if calfile is not None:
        extfile = iraf.osfn("pysalt$data/site/suth_extinct.dat")
        speccal(ofile,
                ofile.replace("txt", "spec"),
                calfile,
                extfile,
                airmass,
                exptime,
                clobber=True,
                logfile='salt.log',
                verbose=True)

    spec_list = [ofile, airmass, exptime, propcode]

    return spec_list