<jupyter_start><jupyter_text>Alogrithm to adjust trace [v1.0]<jupyter_code># imports
import pdb
from pypit import armsgs
from pypit import pyputils
msgs = pyputils.get_dummy_logger()
armsgs.pypit_logger = msgs
from pypit import ginga
from pypit import arutils as arut
#from xastropy.xutils import xdebug as xdb<jupyter_output><empty_output><jupyter_text>Gaussian profile<jupyter_code>def trace_gweight(fimage, xcen, ycen, sigma, invvar=None):
    from scipy.special import erf
    # Setup
    nx = fimage.shape[1]
    ny = fimage.shape[0]
    ncen = xcen.size
    xnew = xcen.copy()
    xerr = 0.0 * xnew # Allocate memory

    if invvar is None:
        invvar = np.ones_like(fimage)

    # More setting up
    #lower = xcen - 3.0*sigma
    #upper = xcen + 3.0*sigma
    x_int = np.round(xcen).astype(int)
    nstep = 2*int(3.0*sigma) - 1
    #i1 = x_int - nstep/2
    #i2 = i1 + nstep - 1

    weight = np.zeros_like(xcen)
    numer  = np.zeros_like(xcen)
    meanvar = np.zeros_like(xcen)
    bad = np.zeros_like(xcen).astype(bool)
    #xtemp = (np.arange(nstep)-nstep/2)/5.  * sigma

    for i in range(nstep): #=0, nstep-1 do begin
        xh = x_int - nstep/2 + i
        xtemp = (xh - xcen - 0.5)/sigma/np.sqrt(2.0)
        g_int = (erf(xtemp+1./sigma/np.sqrt(2.0)) - erf(xtemp))/2.
        try:
            xs = np.minimum(np.maximum(xh,0),(nx-1))
        except:           
            pdb.set_trace()

        cur_weight = fimage[ycen, xs] * (invvar[ycen, xs] > 0) * g_int * ((xh >= 0) & (xh < nx))
        weight += cur_weight
        numer += cur_weight * xh
        meanvar = meanvar + cur_weight * cur_weight * (xcen-xh)**2 / (
                            invvar[ycen, xs] + (invvar[ycen, xs] == 0))
        #pdb.set_trace()
        bad = np.any([bad, xh < 0, xh >= nx], axis=0)
        
    xerr[:] = 999.0
    #pdb.set_trace()
    good = (~bad) & (weight > 0)
    if np.sum(good) > 0:
        xnew[good] = numer[good]/weight[good]
        xerr[good] = np.sqrt(meanvar[good])/weight[good]
    # Return
    return xnew, xerr<jupyter_output><empty_output><jupyter_text>Load image + trace<jupyter_code>img_file = '/Users/xavier/Keck/LRIS/data/2016apr06/red_A/Science/spec2d_J0817+1351_LRISr_2016Apr06T060223.fits'
hdu = fits.open(img_file)
names = [hdu[i].name for i in range(len(hdu))]
exten = names.index('DET2-SKYSUB')
skysub = hdu[exten].data
skysub.shape
spec_file = '/Users/xavier/Keck/LRIS/data/2016apr06/red_A/Science/spec1d_J0817+1351_LRISr_2016Apr06T060223.fits'
hdu1 = fits.open(spec_file)
tbl = Table(hdu1[5].data)
trace = tbl['obj_trace']<jupyter_output><empty_output><jupyter_text>Refine<jupyter_code>reload(arut)
xnew, xerr = arut.trace_gweight(skysub, trace.data, np.arange(skysub.shape[0]).astype(int), 3.)
xnew2, xerr2 = arutils.trace_gweight(skysub, xnew, np.arange(skysub.shape[0]).astype(int), 3.)
plt.clf()
plt.plot(np.arange(xnew.size), xnew2-trace.data)
plt.show()<jupyter_output><empty_output><jupyter_text>Display<jupyter_code>viewer, ch = ginga.show_image(skysub)
ginga.show_trace(viewer, ch, trace.data, 'orig', clear=True, color='blue')
ginga.show_trace(viewer, ch, xnew2, 'x2', color='red')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.150853889943074, 'fraction_numerical': 0.03573687539531942, 'mean_word_length': 3.3869625520110955, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '240504', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1328, 'n_words': 227, 'path': 'doc/nb/Adjust_trace.ipynb', 'repo_name': 'afloers/PypeIt', 'size': '3162'}