Пример #1
0
 def set_imagepair(self, val):
     if not self.theapp.current_target:
         popup = WarningDialog(text='You need to select a target (on the Observing Screen) before proceeding!')
         popup.open()
         return
     self.pair_index = self.pairstrings.index(val)
     fitsfile = self.paths['out']+re.sub(' ','',re.sub('.fits','',val))+'.fits'
     if not path.isfile(fitsfile):
         popup = WarningDialog(text='You have to select an extraction'\
             'region for this image pair \nbefore you can move on to this step.')
         popup.open()
         return
     self.current_impair = FitsImage(fitsfile)
     self.region = self.current_impair.get_header_keyword(*('EXREG' + x for x in ['X1','Y1','X2','Y2']))
     if not any(self.region):
         popup = WarningDialog(text='You have to select an extraction'\
             'region for this image pair \nbefore you can move on to this step.')
         popup.open()
         return
     self.current_impair.load()
     idata = ''.join(map(chr,self.current_impair.scaled))
     self.itexture.blit_buffer(idata, colorfmt='luminance', bufferfmt='ubyte', \
         size = self.current_impair.dimensions)
     self.trace_axis = 0 if get_tracedir(self.current_target.instrument_id) == 'vertical' else 1
     #tmp = self.current_impair.data_array[self.region[1]:self.region[3]+1,self.region[0]:self.region[2]+1]
     #self.extractregion = med_normal(tmp)
     tmp = self.theapp.extract_pairs[self.pair_index]
     if self.trace_axis:
         #self.trace_axis = 1
         reg = [self.region[x] for x in [1, 0, 3, 2]]
         self.extractregion = make_region(tmp[0], tmp[1], reg, self.current_flats)#.transpose()
     else:
         self.extractregion = make_region(tmp[0], tmp[1], self.region, self.current_flats).transpose()
     reg = self.region[:]
     reg[2] = reg[2] - reg[0]
     reg[3] = reg[3] - reg[1]
     self.iregion = self.itexture.get_region(*reg)
     dims = [[0,0],list(self.extractregion.shape)]
     dims[0][self.trace_axis] = 0.4 * self.extractregion.shape[self.trace_axis]
     dims[1][self.trace_axis] = 0.6 * self.extractregion.shape[self.trace_axis]
     self.tracepoints = robm(self.extractregion[dims[0][0]:dims[1][0]+1,\
         dims[0][1]:dims[1][1]+1], axis = self.trace_axis)
     self.tplot.points = interp_nan(list(enumerate(self.tracepoints)))
     pxs, self.tracepoints = zip(*self.tplot.points)
     self.drange = minmax(self.tracepoints)
     self.ids.the_graph.add_plot(self.tplot)
Пример #2
0
def find_peaks(idata, npeak = 1, tracedir = None, pn = 'pos'):
    data = np.array(idata) #make sure we're dealing with an array
    if len(data.shape) > 1: #check for 2D array
        if traceder is None:
            tracedir = 1 #assume that the second axis is the right one...
            #if idata is 2D, compress along the trace using a robust mean
        data = robm(data, axis = tracedir)
    #since argrelextrema isn't working, just return argmax
    if pn == 'pos':
        return np.nanargmax(data)
    else:
        return np.nanargmin(data)
    
    ps = zip(range(data.size), data)
    junk, data = zip(*interp_nan(ps)) #remove nans via interpolation
    data = medfilt(data, 5) #a little smoothness never hurt
    #find /all/ rel extrema:
    maxima = argrelextrema(data, posneg[pn])
    max_val = data[maxima]
    
    priority = np.argsort(-np.fabs(max_val))
    return maxima[priority[:npeak]]
Пример #3
0
def draw_trace(idata, x_val, pfit, nfit, fixdistort = False, fitdegree = 2, ptype = 'Gaussian'):
    '''move along the trace axis, fitting each position with a model of the PSF'''
    #pdb.set_trace()
    ns = idata.shape[1]
    midpoint = ns/2
    tc1, tc2 = midpoint, midpoint + 1
    fitp = deconstruct_composite(pfit)
    fitn = deconstruct_composite(nfit)
    p_amp, p_mean, p_sig = get_individual_params(*fitp)
    n_amp, n_mean, n_sig = get_individual_params(*fitn)
    n_p = len(p_mean)
    n_n = len(n_mean)
    
    #back-convert the custom model into a composite model
    #fitp = build_composite(pfit, ptype)
    #fitn = build_composite(nfit, ptype)

    #trace = {'pos':[np.zeros(idata.shape) for _ in fitp._transforms], \
    #    'neg':[np.zeros(idata.shape) for _ in fitn._transforms]}
    #apertures = {'pos':[range(ns) for _ in fitp._transforms], \
    #    'neg':[range(ns) for _ in fitn._transforms]}
    trace = {'pos':[np.zeros(idata.shape) for _ in p_mean], \
        'neg':[np.zeros(idata.shape) for _ in n_mean]}
    apertures = {'pos':[range(ns) for _ in p_mean], \
        'neg':[range(ns) for _ in n_mean]}

    pcur1, ncur1 = deepcopy(fitp), deepcopy(fitn)
    pcur2, ncur2 = deepcopy(fitp), deepcopy(fitn)
    pmodel, nmodel = multi_peak_model(ptype, n_p), multi_peak_model(ptype, n_n)
    #pcur1, ncur1 = deepcopy(pfit), deepcopy(nfit)
    #pcur2, ncur2 = deepcopy(pfit), deepcopy(nfit)
    down, up = True, True
    
    #set up initial data for use with cross-correlation
    piece0 = robm(idata[:, (max(tc1 - 20, 0), min(tc1 + 20, ns - 1))], axis=1)
    junk, piece0 = zip(*interp_nan(list(enumerate(piece0))))
    med = np.median(piece0)
    p0 = np.clip(piece0, a_min = med, a_max = np.nanmax(piece0))
    n0 = np.clip(piece0, a_min = np.nanmin(piece0), a_max = med)
    
    while down or up:
        #work in both directions from the middle
        if tc1 >= 0:
            lb = max(tc1 - 20, 0)
            ub = min(tc1 + 20, ns-1)
            piece = robm(idata[:,(lb,ub)], axis=1)
            junk, piece = zip(*interp_nan(list(enumerate(piece))))
            med = np.median(piece)
            pdata = np.clip(piece,a_min=med,a_max=np.nanmax(piece))
            ndata = np.clip(piece,a_min=np.nanmin(piece),a_max=med)
            if pcur1 is not None:
                offset = offset1d(p0, pdata)
                pcur1 = [np.array([interp1d(x_val, pdata, kind='cubic', bounds_error=False)(f[1] + \
                    offset), f[1] + offset, f[2]]) for f in fitp]
                #pnew1 = fitmethod(pcur1, x_val, pdata)
                pnew1, psig1 = curve_fit(pmodel, x_val, pdata, pcur1)
                pnmodel = build_composite(pnew1, ptype)
                #for i, transform in enumerate(pnew1._transforms):
                for i, transform in enumerate(pnmodel._transforms):
                    trace['pos'][i][:,tc1] = transform(x_val)
                    apertures['pos'][i][tc1] = transform.mean
                pcur1 = pnew1
                #print tc1, pcur1
            if ncur1 is not None:
                offset = offset1d(n0, ndata)
                ncur1 = [np.array([interp1d(x_val, ndata, kind='cubic', bounds_error=False)(f[1] + \
                    offset), f[1] + offset, f[2]]) for f in fitn]
                #nnew1 = fitmethod(ncur1, x_val, ndata)
                nnew1, nsig1 = curve_fit(nmodel, x_val, ndata, ncur1)
                nnmodel = build_composite(nnew1, ptype)
                #for i, transform in enumerate(nnew1._transforms):
                for i, transform in enumerate(nnmodel._transforms):
                    trace['neg'][i][:,tc1] = transform(x_val)
                    apertures['neg'][i][tc1] = transform.mean
                ncur1 = nnew1
                #print tc1, ncur1
            tc1 -= 1
        else:
            down = False
        if tc2 < ns:
            lb = max(tc2 - 20, 0)
            ub = min(tc2 + 20, ns-1)
            piece = robm(idata[:,(lb,ub)], axis=1)
            junk, piece = zip(*interp_nan(list(enumerate(piece))))
            med = np.median(piece)
            pdata = np.clip(piece,a_min=med,a_max=np.nanmax(piece))
            ndata = np.clip(piece,a_min=np.nanmin(piece),a_max=med)
            if pcur2 is not None:
                offset = offset1d(p0, pdata)
                pcur2 = [np.array([interp1d(x_val, pdata, kind='cubic', bounds_error=False)(f[1] + \
                    offset), f[1] + offset, f[2]]) for f in fitp]
                #pnew2 = fitmethod(pcur2, x_val, pdata)
                pnew2, psig2 = curve_fit(pmodel, x_val, pdata, pcur2)
                pnmodel = build_composite(pnew2, ptype)
                #for i, transform in enumerate(pnew2._transforms):
                for i, transform in enumerate(pnmodel._transforms):
                    trace['pos'][i][:,tc2] = transform(x_val)
                    apertures['pos'][i][tc2] = transform.mean
                pcur2 = pnew2
                #print tc2, pcur2
            if ncur2 is not None:
                offset = offset1d(n0, ndata)
                ncur2 = [np.array([interp1d(x_val, ndata, kind='cubic', bounds_error=False)(f[1] + \
                    offset), f[1] + offset, f[2]]) for f in fitn]
                #nnew2 = fitmethod(ncur2, x_val, ndata)
                nnew2, nsig2 = curve_fit(nmodel, x_val, ndata, ncur2)
                nnmodel = build_composite(nnew2, ptype)
                #for i, transform in enumerate(nnew2._transforms):
                for i, transform in enumerate(nnmodel._transforms):
                    trace['neg'][i][:,tc2] = transform(x_val)
                    apertures['neg'][i][tc2] = transform.mean
                ncur2 = nnew2
                #print tc2, ncur2
            tc2 += 1
        else:
            up = False
    
    #import shelve
    #f = shelve.open('/Users/gray/Desktop/trace-shelve')
    #f['pos'] = trace['pos']
    #f['neg'] = trace['neg']
    
    if not fixdistort:
        return trace
        
    #pdb.set_trace()
    
    if pcur1 is not None:
        #identify the various aperture traces, subtract off the 
        #median x-position of each one, determine the median offset
        #across apertures, and fit with a polynomial
        if len(apertures['pos']) > 1:
            ap = np.array(zip(*apertures['pos'])).squeeze()
            ns, nap = ap.shape
            meds = np.median(ap, axis=1)
            meds = np.repeat(meds.reshape(ns, 1), nap, axis=1)
        else:
            ap = np.array(apertures['pos'][0]).squeeze()
            ns, nap = ap.size, 1
            meds = np.median(ap)
        ap -= meds
        off_x = np.median(ap, axis=0) if nap > 1 else ap
        pinit = poly.Polynomial1D(fitdegree)
        x_trace = np.arange(ns)
        posfit = fitmethod(pinit, x_trace, off_x)
        posfit
    else: posfit = None
    
    if ncur1 is not None:
        if len(apertures['neg']) > 1:
            ap = np.array(zip(*apertures['neg'])).squeeze()
            ns, nap = ap.shape
            meds = np.median(ap, axis=1)
            meds = np.repeat(meds.reshape(ns, 1), nap, axis=1)
        else:
            ap = np.array(apertures['neg'][0]).squeeze()
            ns, nap = ap.size, 1
            meds = np.median(ap)
        ap -= meds
        off_x = np.median(ap, axis=0) if nap > 1 else ap
        pinit = poly.Polynomial1D(fitdegree)
        x_trace = np.arange(ns)
        negfit = fitmethod(pinit, x_trace, off_x)
    else: negfit = None
    
    return posfit, negfit