Example #1
0
def extract_spectra(img,
                    minsize=5,
                    thresh=3,
                    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
    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]

    #use manual intervention to get section
    os.system('ds9 %s &' % img)

    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)

    cleanspectra(ofile)

    spec_list = [ofile, airmass, exptime, propcode]

    return spec_list
Example #2
0
def extract_spectra(hdu, yc, dy, outfile, minsize=5, thresh=3, grow=0, smooth=False, maskzeros=False, convert=True,  cleanspectra=True, clobber=True, specformat='ascii'):
    """From an image, extract a spectra.   

    """
    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)
       data[data<=0]=mean #rdata[data<=0]

    y1=yc-dy
    y2=yc+dy

    #sy1=y2-2*dy
    #sy2=y2+2*dy

    #sdata = 1.0 * data 
    #y,x = np.indices(sdata.shape)
    #for i in range(sdata.shape[1]):
    #   mask=(hdu[3].data[:,i]==0)
    #   mask[sy1:sy2] = 0
    #   if mask.sum()>0:
    #     sdata[y1:y2,i] = np.interp(y[y1:y2,i], y[:,i][mask], data[:,i][mask])  
    #hdu[1].data = sdata
    #sk_list=extract(hdu, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=True)
    #ap_list[0].ldata=ap_list[0].ldata-sk_list[0].ldata
    #ap_list[0].ldata=ap_list[0].ldata-float(y2-y1)/(sy2-sy1)*sk_list[0].ldata

    convert=True 
    ap_list=extract(hdu, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=convert)
    sy1a=y2
    sy2a=sy1a+2.0*dy
    ska_list=extract(hdu, method='normal', section=[(sy1a,sy2a)], minsize=minsize, thresh=thresh, convert=convert)
    sy2b=y1-dy
    sy1b=sy2b-2.0*dy
    skb_list=extract(hdu, method='normal', section=[(sy1b,sy2b)], minsize=minsize, thresh=thresh, convert=convert)
    print sy1b, sy2b

    sdata = 0.5*(ska_list[0].ldata/(sy2a-sy1a) + skb_list[0].ldata/(sy2b-sy1b))
    #sdata = ska_list[0].ldata/(sy2a-sy1a)
    #sdata = skb_list[0].ldata/(sy2b-sy1b)
    
    ap_list[0].ldata=ap_list[0].ldata-float(y2-y1) * sdata

    if cleanspectra:
       clean_spectra(ap_list[0], grow=grow)
    
    if specformat == 'ascii':
        write_extract(outfile, [ap_list[0]], outformat='ascii', clobber=clobber)
    elif specformat == 'lcogt':
        write_lcogt(outfile, ap_list[0], hdu, sky=float(y2-y1) * sdata, clobber=clobber)
Example #3
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
Example #4
0
def extract_spectra(img, minsize=5, thresh=3, 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
    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]

    #use manual intervention to get section
    os.system('ds9 %s &' % img)

    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)

    cleanspectra(ofile)
    
    spec_list=[ofile, airmass, exptime, propcode]

    return spec_list
Example #5
0
def quickap(profile, lampid=None, findobj=False, objsection=None, skysection=None, clobber=False, logfile='saltclean.log', verbose=True):
   profile = os.path.basename(profile)
   outfile = profile.replace('fits', 'txt')
   #open the file
   struct=pyfits.open(profile)
   data=struct[1].data
   xlen=len(struct[1].data[0])
   ylen=len(struct[1].data)

   #determine the resolution element
   dres = 1.0*calc_resolution(struct)

   #correct for the response function
   response = create_response(data, spatial_axis=1, order=3, conv=1e-2, niter=10)
   data = data / response


   if objsection is None:
      y1=0.5*ylen-0.05*ylen
      y2=0.5*ylen+0.05*ylen
      objsection='[%i:%i]' % (y1,y2)
   else:
      y1=int(objsection[1:].split(':')[0])
      y2=int(objsection[:-1].split(':')[1])
 

   #extract object
   minsize=5
   thresh=5
   ap_spec=extract(struct, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=True)[0]

   #if it is a lamp, do not sky subtract it
   if saltsafeio.checkfornone(lampid):
        write_extract(outfile, [ap_spec], outformat='ascii', clobber=clobber)
        return y1,y2

   if skysection is None:
      skysection='%i:%i' % (y2+0.1*ylen,y2+0.2*ylen)
   #sky spectrum
   sy1, sy2 = skysection.split(':')
   sy1=int(sy1)
   sy2=int(sy2)

   sk_spec=extract(struct, method='normal', section=[(sy1,sy2)], minsize=minsize, thresh=thresh, convert=True)[0] 
   sk_spec.ldata - sk_spec.ldata
   
   w1=ap_spec.wave.min()
   w2=ap_spec.wave.max()
   nsect=10
   dw=(w2-w1)/nsect
   for w in np.arange(w1,w2,dw):
      mask=(ap_spec.wave>w)*(ap_spec.wave<w+dw)
      sk_spec=quickcross(ap_spec, sk_spec, mask=mask, dx=0.02, nstep=500)
      ap_spec.ldata[mask]=ap_spec.ldata[mask]-float(y2-y1)/(sy2-sy1)*sk_spec.ldata[mask]

   #set up masks for sky subtraction
   amask = (ap_spec.ldata==0)
   smask = (sk_spec.ldata==0)


   #find offset between two spectra
   d1 = ap_spec.ldata
   d2 = sk_spec.ldata*float(y2-y1)/(sy2-sy1) 
   y=d1[d2>0]/d2[d2>0]
   y=np.median(y)

   #subtract the skyspectrum

   clean_spectra(ap_spec, dres=2*dres, grow=dres)
   #pl.plot(ap_spec.wave, ap_spec.ldata, ls='', marker='o', ms=2)
   #pl.show()
   #write it out and return
   write_extract(outfile, [ap_spec], outformat='ascii', clobber=clobber)

   #clean up the data 
   if clobber: 
      print 'Replacing pixels'
      os.remove(profile)
      struct[1].data[data==0]=np.median(data)
      struct.writeto(profile)

   return  y1,y2
Example #6
0
def extract_spectra(hdu,
                    yc,
                    dy,
                    outfile,
                    minsize=5,
                    thresh=3,
                    grow=0,
                    smooth=False,
                    maskzeros=False,
                    convert=True,
                    cleanspectra=True,
                    clobber=True,
                    specformat='ascii'):
    """From an image, extract a spectra.   

    """
    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)
        data[data <= 0] = mean  #rdata[data<=0]

    y1 = yc - dy
    y2 = yc + dy

    #sy1=y2-2*dy
    #sy2=y2+2*dy

    #sdata = 1.0 * data
    #y,x = np.indices(sdata.shape)
    #for i in range(sdata.shape[1]):
    #   mask=(hdu[3].data[:,i]==0)
    #   mask[sy1:sy2] = 0
    #   if mask.sum()>0:
    #     sdata[y1:y2,i] = np.interp(y[y1:y2,i], y[:,i][mask], data[:,i][mask])
    #hdu[1].data = sdata
    #sk_list=extract(hdu, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=True)
    #ap_list[0].ldata=ap_list[0].ldata-sk_list[0].ldata
    #ap_list[0].ldata=ap_list[0].ldata-float(y2-y1)/(sy2-sy1)*sk_list[0].ldata

    convert = True
    ap_list = extract(hdu,
                      method='normal',
                      section=[(y1, y2)],
                      minsize=minsize,
                      thresh=thresh,
                      convert=convert)
    sy1a = y2
    sy2a = sy1a + 2.0 * dy
    ska_list = extract(hdu,
                       method='normal',
                       section=[(sy1a, sy2a)],
                       minsize=minsize,
                       thresh=thresh,
                       convert=convert)
    sy2b = y1 - dy
    sy1b = sy2b - 2.0 * dy
    skb_list = extract(hdu,
                       method='normal',
                       section=[(sy1b, sy2b)],
                       minsize=minsize,
                       thresh=thresh,
                       convert=convert)
    print sy1b, sy2b

    sdata = 0.5 * (ska_list[0].ldata / (sy2a - sy1a) + skb_list[0].ldata /
                   (sy2b - sy1b))
    #sdata = ska_list[0].ldata/(sy2a-sy1a)
    #sdata = skb_list[0].ldata/(sy2b-sy1b)

    ap_list[0].ldata = ap_list[0].ldata - float(y2 - y1) * sdata

    if cleanspectra:
        clean_spectra(ap_list[0], grow=grow)

    if specformat == 'ascii':
        write_extract(outfile, [ap_list[0]],
                      outformat='ascii',
                      clobber=clobber)
    elif specformat == 'lcogt':
        write_lcogt(outfile,
                    ap_list[0],
                    hdu,
                    sky=float(y2 - y1) * sdata,
                    clobber=clobber)
Example #7
0
def quickap(profile, lampid=None, findobj=False, objsection=None, skysection=None, clobber=False, logfile='saltclean.log', verbose=True):
   profile = os.path.basename(profile)
   outfile = profile.replace('fits', 'txt')
   #open the file
   struct=pyfits.open(profile)
   data=struct[1].data
   xlen=len(struct[1].data[0])
   ylen=len(struct[1].data)

   #determine the resolution element
   dres = 1.0*calc_resolution(struct)

   #correct for the response function
   response = create_response(data, spatial_axis=1, order=3, conv=1e-2, niter=10)
   data = data / response


   if objsection is None:
      y1=0.5*ylen-0.05*ylen
      y2=0.5*ylen+0.05*ylen
      objsection='[%i:%i]' % (y1,y2)
   else:
      y1=int(objsection[1:].split(':')[0])
      y2=int(objsection[:-1].split(':')[1])
 

   #extract object
   minsize=5
   thresh=5
   ap_spec=extract(struct, method='normal', section=[(y1,y2)], minsize=minsize, thresh=thresh, convert=True)[0]

   #if it is a lamp, do not sky subtract it
   if saltsafeio.checkfornone(lampid):
        write_extract(outfile, [ap_spec], outformat='ascii', clobber=clobber)
        return y1,y2

   if skysection is None:
      skysection='%i:%i' % (y2+0.1*ylen,y2+0.2*ylen)
   #sky spectrum
   sy1, sy2 = skysection.split(':')
   sy1=int(sy1)
   sy2=int(sy2)

   sk_spec=extract(struct, method='normal', section=[(sy1,sy2)], minsize=minsize, thresh=thresh, convert=True)[0] 
   sk_spec.ldata - sk_spec.ldata
   
   w1=ap_spec.wave.min()
   w2=ap_spec.wave.max()
   nsect=10
   dw=(w2-w1)/nsect
   for w in np.arange(w1,w2,dw):
      mask=(ap_spec.wave>w)*(ap_spec.wave<w+dw)
      sk_spec=quickcross(ap_spec, sk_spec, mask=mask, dx=0.02, nstep=500)
      ap_spec.ldata[mask]=ap_spec.ldata[mask]-float(y2-y1)/(sy2-sy1)*sk_spec.ldata[mask]

   #set up masks for sky subtraction
   amask = (ap_spec.ldata==0)
   smask = (sk_spec.ldata==0)


   #find offset between two spectra
   d1 = ap_spec.ldata
   d2 = sk_spec.ldata*float(y2-y1)/(sy2-sy1) 
   y=d1[d2>0]/d2[d2>0]
   y=np.median(y)

   #subtract the skyspectrum

   clean_spectra(ap_spec, dres=2*dres, grow=dres)
   #pl.plot(ap_spec.wave, ap_spec.ldata, ls='', marker='o', ms=2)
   #pl.show()
   #write it out and return
   write_extract(outfile, [ap_spec], outformat='ascii', clobber=clobber)

   #clean up the data 
   if clobber: 
      print 'Replacing pixels'
      os.remove(profile)
      struct[1].data[data==0]=np.median(data)
      struct.writeto(profile)

   return  y1,y2
Example #8
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
Example #9
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)
Example #10
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)
Example #11
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