def selfid(data, sdata, ystart='middlerow', rstep=3): """Create a wavelength identification and rectification based on the data itself. The first step is to extract the line itself and then run specidentify on the data """ # setup the fiducial line if ystart == 'middlerow': ystart = int(0.5 * len(data)) else: ystart = int(ystart) sfluxes = sdata[ystart, :] xarr = np.arange(len(data[0])) function = 'polynomial' order = 2 blank = 0 inttype = 'interp' ws = WavelengthSolution.WavelengthSolution(xarr, xarr, function=function, order=order) ws.fit() ndstep = 50 nw_arr = xarr for j in range(0, ystart): dcoef = [2.0, 0.0, 0.0] nws = st.findxcor(xarr, sdata[j, :], xarr, sfluxes, ws, dcoef=dcoef, ndstep=ndstep, best=False, inttype=inttype, debug=False) data[j, :] = zeroshift(data[j, :], xarr, nws, blank=blank, inttype=inttype) for j in range(ystart, len(data) - 1): dcoef = [2.0, 0.0, 0.0] nws = st.findxcor(xarr, sdata[j, :], xarr, sfluxes, ws, dcoef=dcoef, ndstep=ndstep, best=False, inttype=inttype, debug=False) data[j, :] = zeroshift(data[j, :], xarr, nws, blank=blank, inttype=inttype) return data
def test_LineSolution(): ws = WS.WavelengthSolution(xp, wp, function='poly') ws.fit() print ws.func.coef print ws.value(2000) print ws.sigma(ws.func.x, ws.func.y) print ws.chisq(ws.func.x, ws.func.y, ws.func.yerr) pl.figure() pl.plot(xp, wp - ws.value(xp), ls='', marker='o') #pl.plot(ls.x, ls.y-ls(ls.x), ls='', marker='o') pl.show() return
def arcstraight(data, xarr, istart, function='poly', order=3, rstep=1, y1=None, y2=None, sigma=5, sections=3, niter=5, log=None, verbose=True): """For a given image, assume that the line given by istart is the fiducial and then calculate the transformation between each line and that line in order to straighten the arc returns Wavlenght solution """ #set up the edges if y1 is None: y1=0 if y2 is None: y2=data.shape[0] # extract the central row ys, xs = data.shape farr = data[istart,:] xp, xf = st.findpoints(xarr, farr, sigma, niter, sections=sections) #short function to return closest peak def get_closest_x(y_arr, i): y = np.arange(len(y_arr)) j = (abs(y[y_arr >0] - i)).argmin() return y_arr[y[y_arr >0][j]] #loop through all lines and rows to trace the lines line_dict = {} yc = int(ys/2.0) xdiff = 5 for x in xp: y_arr = np.zeros(ys) y_arr[yc] = x for i in range(1, yc): if yc+i < y2: y_arr[yc+i] = st.mcentroid(xarr, data[yc+i,:], kern=st.default_kernal, xdiff=xdiff, xc=get_closest_x(y_arr, yc+i)) if yc+i > y1: y_arr[yc-i] = st.mcentroid(xarr, data[yc-i,:], kern=st.default_kernal, xdiff=xdiff, xc=get_closest_x(y_arr, yc-i)) line_dict[x] = y_arr #find the solution for each row iws={} xp = np.array(line_dict.keys()) for i in range(y1, y2): wp = [line_dict[x][i] for x in xp] ws = WavelengthSolution.WavelengthSolution(np.array(wp), xp, order=3, function='polynomial') ws.fit() exit() iws[i] = ws return iws
def arcstraight(data, xarr, istart, ws=None, function='poly', order=3, rstep=1, nrows=1, dcoef=None, y1=None, y2=None, log=None, verbose=True): """For a given image, assume that the line given by istart is the fiducial and then calculate the transformation between each line and that line in order to straighten the arc returns Wavlenght solution """ ImageSolution = {} #set up the edges if y1 is None: y1 = 0 if y2 is None: y2 = data.shape[0] # extract the central row oxarr = xarr.copy() ofarr = data[istart] ws = WavelengthSolution.WavelengthSolution(xarr, xarr, function, order) ws.fit() ImageSolution[istart] = ws if isinstance(dcoef, str): if dcoef == '': dcoef = None else: try: dcoef = [ float(w) for w in dcoef.replace('[', '').replace(']', '').split() ] except: raise SaltError('dcoef is not the right format') if dcoef is not None: ws.coef = dcoef data = nd.gaussian_filter(data, 3) # now step around the central row for i in range(rstep, int(0.5 * len(data)), rstep): for k in [istart - i, istart + i]: if k < y1 or k > y2: continue lws = getwsfromIS(k, ImageSolution) xarr = np.arange(len(data[k])) farr = apext.makeflat(data, k, k + nrows) nws = st.fitxcor(xarr, farr, oxarr, ofarr, lws, interptype='interp') ImageSolution[k] = nws return ImageSolution
def test_ModelSolution(): hdu = pyfits.open(inimage) # create the data arra data = hdu[1].data # create the header information instrume = hdu[1].header['INSTRUME'].strip() grating = hdu[1].header['GRATING'].strip() grang = hdu[1].header['GR-ANGLE'] arang = hdu[1].header['AR-ANGLE'] filter = hdu[1].header['FILTER'].strip() slit = float(hdu[1].header['MASKID']) xbin, ybin = hdu[1].header['CCDSUM'].strip().split() # print instrume, grating, grang, arang, filter # print xbin, ybin # print len(data), len(data[0]) # create the RSS Model rssmodel = RSSModel.RSSModel(grating_name=grating, gratang=grang, camang=arang, slit=slit, xbin=int(xbin), ybin=int(ybin)) xarr = np.arange(len(data[0]), dtype='int64') rss = rssmodel.rss alpha = rss.gratang beta = rss.gratang - rss.camang d = rss.detector.xbin * rss.detector.pix_size * (xarr - 0.5 * len(xarr)) dbeta = np.degrees(np.arctan(d / rss.camera.focallength)) y = 1e7 * rss.calc_wavelength(alpha, beta - dbeta) #ws=WS.WavelengthSolution(xp, wp, function='model', sgraph=rssmodel.rss, xlen=len(data[0]), order=4, cfit='all') ws = WS.WavelengthSolution(xarr, y, function='model', sgraph=rssmodel.rss, xlen=len(data[0]), order=4, cfit='ndcoef') #ws=WS.WavelengthSolution(xarr, y, function='poly', order=3) #ws=WS.WavelengthSolution(xp, wp, function='poly', order=3) ws.fit() print ws.coef print ws.func.result for c in ws.func.spcoef: print c() for c in ws.func.ndcoef: print c() print ws.value(2000) print ws.sigma(xp, wp) print ws.chisq(xp, wp, ep) pl.figure() # pl.plot(xarr,y) #pl.plot(xarr, ws.value(xarr)) #pl.plot(xp, wp-ws.value(xp), ls='', marker='o') pl.plot(xarr, y - ws.value(xarr)) #pl.plot(ls.x, ls.y-ls(ls.x), ls='', marker='o') pl.show()