def _compute(self, slitx, slity, m, waves, returnx, returny): """Wrapper for cfunctions.compute""" nw = waves.size ns = slitx.size ng = n_sell(self.ARM, waves) cfunctions.compute( self.ARM_FLAG, 0, # blaze flag 1, # location flag self.GAP_FLAG, nw, ns, m, self.gap, self.gapoffset, self.XD_0, self.YD_0, np.ascontiguousarray(ng, dtype=np.float64), np.ascontiguousarray(slitx, dtype=np.float64), np.ascontiguousarray(slity, dtype=np.float64), np.ascontiguousarray(waves, dtype=np.float64), np.empty(0), # weights np.empty((0,0)), # ccd np.empty((0,0), dtype=np.uint), # counts np.empty((0,0), dtype=np.uint), # order list np.ascontiguousarray(returnx, dtype=np.float64), np.ascontiguousarray(returny, dtype=np.float64)) return returnx, returny
def compute(arm, orders, wavelengths, slit=0): """Compute wrapper for debug.""" norders = orders.size nw = wavelengths.shape[0] if slit is 0: slitfunc = point_source else: slitfunc = half_moon_even slitx, slity = slitfunc(arm, offset=0.0) print "xd_0 = %.2f mm" % arm.XD_0 print "yd_0 = %.2f mm" % arm.YD_0 xd = np.empty(wavelengths.shape) yd = np.empty(wavelengths.shape) ng = np.empty(wavelengths.shape) returnx = np.empty(wavelengths[0].size) returny = np.empty(wavelengths[0].size) for i in xrange(norders): lamb = wavelengths[i] ng = n_sell(arm.ARM, lamb) print "n_sell = ", ng cfunctions.compute( arm.ARM_FLAG, 0, # blaze flag 1, # location flag nw, slitx.size, orders[i], arm.XD_0, arm.YD_0, np.ascontiguousarray(ng, dtype=np.float64), np.ascontiguousarray(slitx, dtype=np.float64), np.ascontiguousarray(slity, dtype=np.float64), np.ascontiguousarray(lamb, dtype=np.float64), np.empty(0), # weights np.empty((0, 0)), # ccd np.empty((0, 0), dtype=np.uint), # counts np.empty((0, 0), dtype=np.uint), # order list np.ascontiguousarray(returnx, dtype=np.float64), np.ascontiguousarray(returny, dtype=np.float64), ) xd[i] = returnx # mm yd[i] = returny # mm xd = xd.flatten() / arm.DPIX yd = yd.flatten() / arm.DPIX xdmin = xd.min() xdmax = xd.max() xdrange = np.absolute(xdmax - xdmin) ydmin = yd.min() ydmax = yd.max() ydrange = np.absolute(ydmax - ydmin) return wavelengths.flatten(), xd, yd, xdrange, ydrange
def _solve_wavelengths(arm, fiber, wave_trace=True, write=True): """ Finds wavelength solutions using a point source slit function. """ solution_file = 'DATA/%s_wavelength_solutions_fiber_%s.txt' % (arm.ARM.lower(), arm.fib_char[fiber].lower()) if arm.fib_char[fiber] in ['A', 'B']: #@MZ offset = arm.fib_OFFSET[fiber] # CREATE POINT SOURCE SLIT FUNCTION slitx, slity = point_source(arm, offset=offset) nslit = slitx.size wavelengths = wavefuncs.calculate_wavelengths(arm, mode="wavetrace", nwaves=arm.dwt) # reminder: wavelengths is 2d array in this case intensities = np.ones(wavelengths.shape) # SET ALL INTENSITIES TO 1 orders = arm.OSET norders = arm.NOSET # INITIALIZE IMAGE ARRAYS image = np.zeros(arm.CCD_DIMS) counts = np.zeros(arm.CCD_DIMS, dtype=np.uint) m_list = np.zeros(arm.CCD_DIMS, dtype=np.uint) # records order of each pixel # OTHER INITIALIZATIONS returnx = np.empty(0) returny = np.empty(0) LOC_FLAG = 0 BLAZE_FLAG = 0 for i,m in enumerate(orders): waves, _weights = wavefuncs.feed_wavelengths(arm, m, wavelengths=wavelengths, intensities=intensities) nwaves = waves.size n_g_sell = n_sell(arm.ARM, waves) cfunctions.compute(arm.ARM_FLAG, BLAZE_FLAG, LOC_FLAG, nwaves, nslit, m, arm.XD_0, arm.YD_0, np.ascontiguousarray(n_g_sell, dtype=np.float64), np.ascontiguousarray(slitx, dtype=np.float64), np.ascontiguousarray(slity, dtype=np.float64), np.ascontiguousarray(waves, dtype=np.float64), np.ascontiguousarray(waves, dtype=np.float64), image, np.ascontiguousarray(counts), np.ascontiguousarray(m_list), returnx, returny) output = ' * Wavetrace order %i (%i of %i), %.2f%% complete.' % (m, i+1, norders, ((i+1.) * 100.0 / norders)) Printer(output) print '\n' inds = np.where(counts != 0) image[inds] *= 1.0e7 / counts[inds] # average each pixel, convert mm to Angstrom # saves solutions to disk if write: newimage = image[inds] m_list = m_list[inds] y = inds[0] x = inds[1] n = x.size print " * Saving wavelength solutions to '%s'" % solution_file filename = open(solution_file, 'w') for i,lamb in enumerate(newimage): filename.write( '%s %s %s %s\n' % (m_list[i], lamb, x[i], y[i]) ) output1 = ' * %0.2f%% Complete. point %s of %s.' % \ (100.0 * float(i) / n, i+1, n) Printer(output1) print "\n" filename.close() return m_list, newimage, x, y return 0
def _write_region_solve(arm, fiber, m_solutions=None, lamb_solutions=None, x=None, y=None): """ Writes .reg file using regions from a pre-written wavelength solution file. lamb_solutions are in Ang NOTE possibly other functions in this module can be deprecated in favor of this one. """ solution_file = 'DATA/%s_wavelength_solutions_fiber_%s.txt' % \ (arm.ARM.lower(), arm.fib_char[fiber].lower()) print " * Writing region file to '%s.reg'" % arm.outfile dlamb = arm.dwt # Ang wtlist = arm.wtlist # Ang if arm.WT_FLAG: outfile = open('%s.reg' % arm.outfile, 'a') elif not arm.WT_FLAG: outfile = open('%s.reg' % arm.outfile, 'w') outfile.write('# Region file format: DS9 version 4.0\n') outfile.write('# Filename: %s.fits\n' % arm.outfile.replace('FITS/', '')) outfile.write('global color=green font="helvetica 10 normal" select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 source\n') outfile.write('physical\n') # INITIALIZATIONS if arm.fib_char[fiber] in ['A', 'B']: #@MZ offset = arm.fib_OFFSET[fiber] slitx, slity = point_source(arm, offset=offset) nslit = slitx.size # import wavelist, or create a wavelist using the ccd limits, ang -> mm if wtlist: input_waves = np.loadtxt(wtlist, unpack=True) * 1.e-7 else: input_waves = np.arange(arm.wmin*1.e7, arm.wmax*1.e7, dlamb) * 1.e-7# step size of dlamb in Angstrom _weights = np.zeros(input_waves.size) # dummy variable for feed_wavelengths() orders = arm.OSET norders = arm.NOSET # INITIALIZE IMAGE ARRAYS image = np.zeros(arm.CCD_DIMS) counts = np.zeros(arm.CCD_DIMS, dtype=np.uint) m_list = np.zeros(arm.CCD_DIMS, dtype=np.uint) # records order of each pixel # OTHER INITIALIZATIONS LOC_FLAG = 2 BLAZE_FLAG = 0 # find wavelengths for each order for i,m in enumerate(orders): waves, _intensities = wavefuncs.feed_wavelengths(arm, m, wavelengths=input_waves, intensities=_weights) nwaves = waves.size n_g_sell = n_sell(arm.ARM, waves) returnx = np.empty(nwaves) returny = np.empty(nwaves) cfunctions.compute(arm.ARM_FLAG, BLAZE_FLAG, LOC_FLAG, nwaves, nslit, m, arm.XD_0, arm.YD_0, np.ascontiguousarray(n_g_sell, dtype=np.float64), np.ascontiguousarray(slitx, dtype=np.float64), np.ascontiguousarray(slity, dtype=np.float64), np.ascontiguousarray(waves, dtype=np.float64), np.ascontiguousarray(waves, dtype=np.float64), image, np.ascontiguousarray(counts), np.ascontiguousarray(m_list), returnx, returny) x = returnx y = returny print x print y # FITS indices start at 1, not 0 outfile.write("".join(['point(%i,%i) # point=cross text={%.0f}\n' % \ tup for tup in zip(x+1, y+1, waves*1.e7)])) # ds9 has 1-based indexing ? @CM yes, I believe so output = ' * Region order %s (%s of %s), %.2f%% complete.' % \ (m, i+1, norders, ((i+1.) * 100.0 / norders)) Printer(output) print "\n" outfile.close() arm.set_wt_flag(True) # set flag to enable appending for region file return 0