Ejemplo n.º 1
0
def write_psffile(infile,
                  wcoeffs,
                  wcoeffs_wavemin,
                  wcoeffs_wavemax,
                  outfile,
                  wavestepsize=None):
    """
    extract psf file, add wcoeffs, and make a new psf file preserving the traces etc.
    psf module will load this
    """

    tset = read_xytraceset(infile)

    # convert wsigma to ysig ...
    nfiber = wcoeffs.shape[0]
    ncoef = wcoeffs.shape[1]
    nw = 100  # need a larger number than ncoef to get an accurate dydw from the gradients

    # wcoeffs and tset do not necessarily have the same wavelength range
    wave = np.linspace(tset.wavemin, tset.wavemax, nw)
    wsig_set = TraceSet(wcoeffs, [wcoeffs_wavemin, wcoeffs_wavemax])
    wsig_vals = np.zeros((nfiber, nw))
    for f in range(nfiber):
        y_vals = tset.y_vs_wave(f, wave)
        dydw = np.gradient(y_vals) / np.gradient(wave)
        wsig_vals[f] = wsig_set.eval(f, wave) * dydw
    tset.ysig_vs_wave_traceset = fit_traces(wave,
                                            wsig_vals,
                                            deg=ncoef - 1,
                                            domain=(tset.wavemin,
                                                    tset.wavemax))

    write_xytraceset(outfile, tset)
Ejemplo n.º 2
0
def write_psffile(infile,wcoeffs,wcoeffs_wavemin,wcoeffs_wavemax,outfile,wavestepsize=None):
    """
    extract psf file, add wcoeffs, and make a new psf file preserving the traces etc.
    psf module will load this
    """

    tset = read_xytraceset(infile)
    
    # convert wsigma to ysig ...
    nfiber    = wcoeffs.shape[0]
    ncoef     = wcoeffs.shape[1]
    nw        = 100 # need a larger number than ncoef to get an accurate dydw from the gradients
    
    # wcoeffs and tset do not necessarily have the same wavelength range
    wave      = np.linspace(tset.wavemin,tset.wavemax,nw)
    wsig_set  = TraceSet(wcoeffs,[wcoeffs_wavemin,wcoeffs_wavemax]) 
    wsig_vals = np.zeros((nfiber,nw))
    for f in range(nfiber) :
        y_vals = tset.y_vs_wave(f,wave)
        dydw   = np.gradient(y_vals)/np.gradient(wave)
        wsig_vals[f]=wsig_set.eval(f,wave)*dydw
    tset.ysig_vs_wave_traceset = fit_traces(wave, wsig_vals, deg=ncoef-1, domain=(tset.wavemin,tset.wavemax))
        
    write_xytraceset(outfile,tset)
Ejemplo n.º 3
0
def read_xytraceset(filename) :
    """
    Reads traces in PSF fits file
    
    Args:
        filename : Path to input fits file which has to contain XTRACE and YTRACE HDUs
    Returns:
         XYTraceSet object
    
    """

    log=get_logger()

    
    xcoef=None
    ycoef=None
    xsigcoef=None
    ysigcoef=None
    wsigmacoef=None
    wavemin=None
    wavemax=None
     
    log.info("reading traces in '%s'"%filename)
    
    fits_file = fits.open(filename)
    
    
    # npix_y, needed for boxcar extractions
    npix_y=0
    for hdu in [0,"XTRACE","PSF"] :
        if npix_y > 0 : break
        if hdu in fits_file : 
            head = fits_file[hdu].header
            if "NPIX_Y" in head :
                npix_y=int(head["NPIX_Y"])
    if npix_y == 0 :
        raise KeyError("Didn't find head entry NPIX_Y in hdu 0, XTRACE or PSF")
    log.info("npix_y={}".format(npix_y))
    
    try :
        psftype=fits_file[0].header["PSFTYPE"]
    except KeyError :
        psftype=""
    
    # now read trace coefficients
    log.info("psf is a '%s'"%psftype)
    if psftype == "bootcalib" :
        xcoef,wavemin,wavemax =_traceset_from_image(wavemin,wavemax,fits_file[0],"xcoef")
        ycoef,wavemin,wavemax =_traceset_from_image(wavemin,wavemax,fits_file[1],"ycoef")
    else :
        for k in ["XTRACE","XCOEF","XCOEFF"] :
            if k in fits_file :
                xcoef,wavemin,wavemax =_traceset_from_image(wavemin,wavemax,fits_file[k],"xcoef")
        for k in ["YTRACE","YCOEF","YCOEFF"] :
            if k in fits_file :
                ycoef,wavemin,wavemax =_traceset_from_image(wavemin,wavemax,fits_file[k],"ycoef")
        for k in ["XSIG"] :
            if k in fits_file :
                xsigcoef,wavemin,wavemax =_traceset_from_image(wavemin,wavemax,fits_file[k],"xsigcoef")
        for k in ["YSIG"] :
            if k in fits_file :
                ysigcoef,wavemin,wavemax =_traceset_from_image(wavemin,wavemax,fits_file[k],"ysigcoef")
        if "WSIGMA" in fits_file :
            wsigmacoef = fits_file["WSIGMA"].data
                
    if psftype == "GAUSS-HERMITE" : # older version where XTRACE and YTRACE are not saved in separate HDUs
        hdu=fits_file["PSF"]
        if xcoef is None    : xcoef,wavemin,wavemax =_traceset_from_table(wavemin,wavemax,hdu,"X")
        if ycoef is None    : ycoef,wavemin,wavemax =_traceset_from_table(wavemin,wavemax,hdu,"Y")
        if xsigcoef is None : xsigcoef,wavemin,wavemax =_traceset_from_table(wavemin,wavemax,hdu,"GHSIGX")
        if ysigcoef is None : ysigcoef,wavemin,wavemax =_traceset_from_table(wavemin,wavemax,hdu,"GHSIGY")
    
    log.info("wavemin={} wavemax={}".format(wavemin,wavemax))
    
    if xcoef is None or ycoef is None :
        raise ValueError("could not find xcoef and ycoef in psf file %s"%filename)
    
    if xcoef.shape[0] != ycoef.shape[0] :
        raise ValueError("XCOEF and YCOEF don't have same number of fibers %d %d"%(xcoef.shape[0],ycoef.shape[0]))
    
    fits_file.close()
    
    if wsigmacoef is not None :
        log.warning("Converting deprecated WSIGMA coefficents (in Ang.) into YSIG (in CCD pixels)")
        nfiber    = wsigmacoef.shape[0]
        ncoef     = wsigmacoef.shape[1]
        nw = 100 # to get accurate dydw
        wave      = np.linspace(wavemin,wavemax,nw)
        wsig_set  = TraceSet(wsigmacoef,[wavemin,wavemax])
        y_set  = TraceSet(ycoef,[wavemin,wavemax])
        wsig_vals = np.zeros((nfiber,nw))
        for f in range(nfiber) :
            y_vals = y_set.eval(f,wave)
            dydw   = np.gradient(y_vals)/np.gradient(wave)
            wsig_vals[f]=wsig_set.eval(f,wave)*dydw
        tset = fit_traces(wave, wsig_vals, deg=ncoef-1, domain=(wavemin,wavemax))
        ysigcoef = tset._coeff
        
    return XYTraceSet(xcoef,ycoef,wavemin,wavemax,npix_y,xsigcoef=xsigcoef,ysigcoef=ysigcoef)
Ejemplo n.º 4
0
def read_xytraceset(filename):
    """
    Reads traces in PSF fits file
    
    Args:
        filename : Path to input fits file which has to contain XTRACE and YTRACE HDUs
    Returns:
         XYTraceSet object
    
    """
    #- specter import isolated within function so specter only loaded if
    #- really needed
    from specter.util.traceset import TraceSet, fit_traces

    log = get_logger()

    xcoef = None
    ycoef = None
    xsigcoef = None
    ysigcoef = None
    wsigmacoef = None
    wavemin = None
    wavemax = None

    log.info("reading traces in '%s'" % filename)

    fits_file = fits.open(filename)

    # npix_y, needed for boxcar extractions
    npix_y = 0
    for hdu in [0, "XTRACE", "PSF"]:
        if npix_y > 0: break
        if hdu in fits_file:
            head = fits_file[hdu].header
            if "NPIX_Y" in head:
                npix_y = int(head["NPIX_Y"])
    if npix_y == 0:
        raise KeyError("Didn't find head entry NPIX_Y in hdu 0, XTRACE or PSF")
    log.debug("npix_y={}".format(npix_y))

    try:
        psftype = fits_file[0].header["PSFTYPE"]
    except KeyError:
        psftype = ""

    # now read trace coefficients
    log.debug("psf is a '%s'" % psftype)
    if psftype == "bootcalib":
        xcoef, wavemin, wavemax = _traceset_from_image(wavemin, wavemax,
                                                       fits_file[0], "xcoef")
        ycoef, wavemin, wavemax = _traceset_from_image(wavemin, wavemax,
                                                       fits_file[1], "ycoef")
    else:
        for k in ["XTRACE", "XCOEF", "XCOEFF"]:
            if k in fits_file:
                xcoef, wavemin, wavemax = _traceset_from_image(
                    wavemin, wavemax, fits_file[k], "xcoef")
        for k in ["YTRACE", "YCOEF", "YCOEFF"]:
            if k in fits_file:
                ycoef, wavemin, wavemax = _traceset_from_image(
                    wavemin, wavemax, fits_file[k], "ycoef")
        for k in ["XSIG"]:
            if k in fits_file:
                xsigcoef, wavemin, wavemax = _traceset_from_image(
                    wavemin, wavemax, fits_file[k], "xsigcoef")
        for k in ["YSIG"]:
            if k in fits_file:
                ysigcoef, wavemin, wavemax = _traceset_from_image(
                    wavemin, wavemax, fits_file[k], "ysigcoef")
        if "WSIGMA" in fits_file:
            wsigmacoef = fits_file["WSIGMA"].data

    if psftype == "GAUSS-HERMITE":  # older version where XTRACE and YTRACE are not saved in separate HDUs
        hdu = fits_file["PSF"]
        if xcoef is None:
            xcoef, wavemin, wavemax = _traceset_from_table(
                wavemin, wavemax, hdu, "X")
        if ycoef is None:
            ycoef, wavemin, wavemax = _traceset_from_table(
                wavemin, wavemax, hdu, "Y")
        if xsigcoef is None:
            xsigcoef, wavemin, wavemax = _traceset_from_table(
                wavemin, wavemax, hdu, "GHSIGX")
        if ysigcoef is None:
            ysigcoef, wavemin, wavemax = _traceset_from_table(
                wavemin, wavemax, hdu, "GHSIGY")

    log.debug("wavemin={} wavemax={}".format(wavemin, wavemax))

    if xcoef is None or ycoef is None:
        raise ValueError("could not find xcoef and ycoef in psf file %s" %
                         filename)

    if xcoef.shape[0] != ycoef.shape[0]:
        raise ValueError(
            "XCOEF and YCOEF don't have same number of fibers %d %d" %
            (xcoef.shape[0], ycoef.shape[0]))

    fits_file.close()

    if wsigmacoef is not None:
        log.warning(
            "Converting deprecated WSIGMA coefficents (in Ang.) into YSIG (in CCD pixels)"
        )
        nfiber = wsigmacoef.shape[0]
        ncoef = wsigmacoef.shape[1]
        nw = 100  # to get accurate dydw
        wave = np.linspace(wavemin, wavemax, nw)
        wsig_set = TraceSet(wsigmacoef, [wavemin, wavemax])
        y_set = TraceSet(ycoef, [wavemin, wavemax])
        wsig_vals = np.zeros((nfiber, nw))
        for f in range(nfiber):
            y_vals = y_set.eval(f, wave)
            dydw = np.gradient(y_vals) / np.gradient(wave)
            wsig_vals[f] = wsig_set.eval(f, wave) * dydw
        tset = fit_traces(wave,
                          wsig_vals,
                          deg=ncoef - 1,
                          domain=(wavemin, wavemax))
        ysigcoef = tset._coeff

    return XYTraceSet(xcoef,
                      ycoef,
                      wavemin,
                      wavemax,
                      npix_y,
                      xsigcoef=xsigcoef,
                      ysigcoef=ysigcoef)