def fitplane(afile1, afile2, xfile1, xfile2, options): xy, alpha1, sx, sy = readalpha(afile1) xy, alpha2, sx, sy = readalpha(afile2) alpha2 -= alpha1 alpha2 *= options.gain if options.removespikes: for m in range(ch): # that's what you would like, but medfilt2d returns bad data # for the edge (i.e., zero or random). Use a wrapper # alpha2[:,:,m]= medfilt2d(alpha2[:,:,m], kernel_size=ks) alpha2[:, :, m] = median_2D(alpha2[:, :, m], kernel_size=options.median) clip = options.clip xd, yd, ch = shape(alpha2) nr = (xd - 2 * clip) * (yd - 2 * clip) x = xy[clip:xd - clip, clip:yd - clip, 0].reshape(nr) y = xy[clip:xd - clip, clip:yd - clip, 1].reshape(nr) shx = alpha2[clip:xd - clip, clip:yd - clip, 0].reshape(nr) shy = alpha2[clip:xd - clip, clip:yd - clip, 1].reshape(nr) if ch == 3: defoc = alpha2[clip:xd - clip, clip:yd - clip, 2].mean() else: defoc = 0.0 ut = zeros((4, nr), float64) k = 0 #for i in range(2): # for j in range(2): # ut[k,:] = x**i * y**j # k += 1 ut[0, :] = ones(nr) ut[1, :] = y ut[2, :] = x ut[3, :] = x * y kk = inner(linalg.inv(inner(ut, ut)), ut.transpose()) kxx = inner(kk, shx) kxy = inner(kk, shy) X, Y = mgrid[0:sy, 0:sx] fitx = int16( (100 * (kxx[1] * X + kxx[2] * Y + kxx[3] * Y * X + kxx[0])).round()) fity = int16( (100 * (kxy[1] * X + kxy[2] * Y + kxy[3] * Y * X + kxy[0])).round()) mm = max(abs(array([fitx.min(), fitx.max(), fity.min(), fity.max()]))) pyana.writeto(xfile1, pyana.getdata(xfile1) + fitx) pyana.writeto(xfile2, pyana.getdata(xfile2) + fity) # pyana.writeto(xfile1, pyana.getdata(xfile1)+fitx, comments=' ') # pyana.writeto(xfile2, pyana.getdata(xfile2)+fity, comments=' ') return mm, defoc
def _anaload(path): """ Load ana file using pyana. @param path File to load @return Data if successful, False otherwise. """ import pyana try: data = pyana.getdata(path) except: return False return data
def interpolate_fringe(stacked, imean, wav_path='wav.8542.f0', do=1, pca=[0], edge=[0, -1]): ''' Interpolate fringe from wing points onto entire line. INPUT: stacked : stacked cube from stack_cube() imean : a 3d pol cube. Probably output of stack_cube() wav : name of wav file ''' print(wav_path) wav = pa.getdata(wav_path) we = (1.0 - wav / wav.max()) / 2 if pca[-1] <> 0: #pick 2 first and last frames and do PCA on them. stacked_shape = stacked.shape print(stacked_shape) print('Using PCA to interpolate fringes...') p0, p1, p2, p3 = pca if pca[-1] == 99: #use last frame #we only apply this to S2 and S3 s2 = np.stack([ PCA(stacked[1, p0:p1], PCA_N=1), PCA(stacked[1, p2:], PCA_N=1) ], axis=0) s3 = np.stack([ PCA(stacked[2, p0:p1], PCA_N=1), PCA(stacked[2, p2:], PCA_N=1) ], axis=0) s0 = np.zeros_like(s2) stot = np.stack([s0, s2, s3, s0], axis=0) else: s1 = np.stack( [PCA(stacked[0, p0:p1], 0), PCA(stacked[0, p2:p3], 0)], axis=0) s2 = np.stack( [PCA(stacked[1, p0:p1], 0), PCA(stacked[1, p2:p3], 0)], axis=0) s3 = np.stack( [PCA(stacked[2, p0:p1], 0), PCA(stacked[2, p2:p3], 0)], axis=0) s4 = np.stack( [PCA(stacked[3, p0:p1], 0), PCA(stacked[3, p2:p3], 0)], axis=0) stot = np.stack([s1, s2, s3, s4], axis=0) fringe = np.zeros(stacked.shape) for ii in range(wav.size): fringe[1, ii, :, :] = ( stot[1, edge[0], :, :] * we[ii] / imean[0, edge[0]] + (1.0 - we[ii]) * stot[1, edge[1], :, :] / imean[0, edge[1]]) * imean[0, ii] fringe[2, ii, :, :] = ( stot[2, edge[0], :, :] * we[ii] / imean[0, edge[0]] + (1.0 - we[ii]) * stot[2, edge[1], :, :] / imean[0, edge[1]]) * imean[0, ii] #Take outer two frames (asssume to be flat save for fringes.) and then make a interpolated mean fringe as function of the wavelength. else: fringe = stacked * 0 for ii in range(wav.size): fringe[1, ii, :, :] = ( stacked[1, edge[0], :, :] * we[ii] / imean[0, edge[0]] + (1.0 - we[ii]) * stacked[1, edge[1], :, :] / imean[0, edge[1]]) * imean[0, ii] fringe[2, ii, :, :] = ( stacked[2, edge[0], :, :] * we[ii] / imean[0, edge[0]] + (1.0 - we[ii]) * stacked[2, edge[1], :, :] / imean[0, edge[1]]) * imean[0, ii] print(fringe.shape) if do: stacked[1:3] = stacked[1:3] - fringe[1:3] result = stacked result_shape = result.shape result_shape = np.append(1, result_shape) result = result.reshape(result_shape) return result