def arces() : ered=imred.Reducer(inst='ARCES',dir='UT191020/ARCES/') ecomb=imred.Combiner(reducer=ered) flat=ecomb.sum([11,15]) thar=ecomb.sum([19,20]) t=tv.TV() apertures=np.loadtxt('newap')[:,1] #traces=trace(flat,apertures/4) traces=Trace() traces.trace(flat,apertures/4,sc0=1024,thresh=1000,plot=t) ec=traces.extract(thar,scat=True) wcal=spectra.WaveCal(type='chebyshev2D',orders=54+np.arange(107),spectrum=ec) wav=readmultispec.readmultispec('w131102.0004.ec.fits')['wavelen'] new=ec.data*0. new[:,204:1855]=wav wcal.identify(ec,wav=new,file=os.environ['PYVISTA_DIR']+'/data/lamps/thar_arces',xmin=201,xmax=1849,thresh=200,rad=3,plot=t) wcal.fit() wcal.identify(ec,file=os.environ['PYVISTA_DIR']+'/data/lamps/thar_arces',xmin=150,xmax=1900,thresh=200,rad=3,plot=t) wcal.fit() w=wcal.wave(image=np.array(ec.data.shape)) hr7950=ered.reduce(1,superflat=flat) hr7950ec=traces.extract(hr7950,scat=True) return flat,thar,traces,ec,wcal,w
def read_multispec_file(parameters): image_name = parameters['MS_FITS_IMAGE'] wavelength_template = parameters['WAVE_TEMPLATE'] wavelength_grid = np.loadtxt(wavelength_template)[:, 0] image = rmspec.readmultispec(image_name) wavelengths = image['wavelen'] flux = image['flux'] # for now just make ids a np arange until I figure out how to hook the WOCS IDs num_stars = len(flux) ids = np.arange(num_stars) ids = np.reshape(ids, (num_stars, 1)) output_array = np.zeros((num_stars, np.size(wavelength_grid)), dtype=np.float32) for entry in range(num_stars): current_fluxes = flux[entry] current_wavelengths = wavelengths[entry] function = ip.interp1d(current_wavelengths, current_fluxes, kind='linear') interpolated_spectrum = function(wavelength_grid) output_array[entry, :] = interpolated_spectrum return ids, output_array
def read_multispec_file(parameters): image_name = parameters['MS_FITS_IMAGE'] wavelength_template = parameters['WAVE_TEMPLATE'] wavelength_grid = np.loadtxt(wavelength_template)[:, 0] image = rmspec.readmultispec(image_name) wavelengths = image['wavelen'] flux = image['flux'] # for now just make ids a np arange until I figure out how to hook the WOCS IDs num_stars = len(flux) ids = np.arange(num_stars) ids = np.reshape(ids, (num_stars, 1)) output_array = np.zeros((num_stars, np.size(wavelength_grid)), dtype=np.float32) for entry in range(num_stars): current_fluxes = flux[entry] current_wavelengths = wavelengths[entry] function = ip.interp1d(current_wavelengths, current_fluxes, kind='linear') interpolated_spectrum = function(wavelength_grid) output_array[entry, :] = interpolated_spectrum if '/' in image_name: image_name = image_name.split('/')[-1] return ids, output_array, image_name
import sys if __name__ == '__main__': try: if sys.argv[1] == 'long1d': test_multi_spec = False elif sys.argv[1] == 'multi': test_multi_spec = True else: raise ValueError('python test.py [long1d/multi]\n'+\ 'multi will be tested this time') except: test_multi_spec = True app = QApplication(sys.argv) window = MainWindow(dwvl_knots=10.0) if test_multi_spec: HD122563R = readmultispec.readmultispec('HD122563R.fits') window.input_multi_data(\ HD122563R['wavelen'][0:5],HD122563R['flux'][0:5],\ output='HD122563Rn_testmulti.csv', output_multi_head='HD122563_multi/') else: HD122563UVES = Table.read('./HD122563_UVES.fits') test_region = (6400 < HD122563UVES['WAVE'][0]) & ( HD122563UVES['WAVE'][0] < 6800) window.input_long1d(\ HD122563UVES['WAVE'][0][test_region],HD122563UVES['FLUX'][0][test_region],\ output='HD122563_UVESn_testlong1d.csv') window.show() sys.exit(app.exec_())
def ReadFits(datafile, errors=False, extensions=False, x=None, y=None, cont=None, return_aps=False, debug=False): """ Read a fits file. If extensions=False, it assumes IRAF's multispec format. Otherwise, it assumes the file consists of several fits extensions with binary tables, with the table names given by the x,y,cont, and errors keywords. See ReadExtensionFits for a convenience function that assumes my standard names """ if debug: print "Reading in file %s: " % datafile if extensions: # This means the data is in fits extensions, with one order per extension # At least x and y should be given (and should be strings to identify the field in the table record array) if type(x) != str: x = raw_input("Give name of the field which contains the x array: ") if type(y) != str: y = raw_input("Give name of the field which contains the y array: ") orders = [] hdulist = pyfits.open(datafile) if cont == None: if not errors: for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y)) orders.append(xypt) else: if type(errors) != str: errors = raw_input("Give name of the field which contains the errors/sigma array: ") for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y), err=data.field(errors)) orders.append(xypt) elif type(cont) == str: if not errors: for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y), cont=data.field(cont)) orders.append(xypt) else: if type(errors) != str: errors = raw_input("Give name of the field which contains the errors/sigma array: ") for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y), cont=data.field(cont), err=data.field(errors)) orders.append(xypt) else: # Data is in multispec format rather than in fits extensions # Call Rick White's script try: retdict = multispec.readmultispec(datafile, quiet=not debug) except ValueError: warnings.warn("Wavelength not found in file %s. Using a pixel grid instead!" % datafile) hdulist = pyfits.open(datafile) data = hdulist[0].data hdulist.close() numpixels = data.shape[-1] numorders = data.shape[-2] wave = np.array([np.arange(numpixels) for i in range(numorders)]) retdict = {'flux': data, 'wavelen': wave, 'wavefields': np.zeros(data.shape)} # Check if wavelength units are in angstroms (common, but I like nm) hdulist = pyfits.open(datafile) header = hdulist[0].header hdulist.close() wave_factor = 1.0 #factor to multiply wavelengths by to get them in nanometers for key in sorted(header.keys()): if "WAT1" in key: if "label=Wavelength" in header[key] and "units" in header[key]: waveunits = header[key].split("units=")[-1] if waveunits == "angstroms" or waveunits == "Angstroms": # wave_factor = u.nm/u.angstrom wave_factor = u.angstrom.to(u.nm) if debug: print "Wavelength units are Angstroms. Scaling wavelength by ", wave_factor if errors == False: numorders = retdict['flux'].shape[0] else: numorders = retdict['flux'].shape[1] orders = [] for i in range(numorders): wave = retdict['wavelen'][i] * wave_factor if errors == False: flux = retdict['flux'][i] err = np.ones(flux.size) * 1e9 err[flux > 0] = np.sqrt(flux[flux > 0]) else: if type(errors) != int: errors = int(raw_input("Enter the band number (in C-numbering) of the error/sigma band: ")) flux = retdict['flux'][0][i] err = retdict['flux'][errors][i] cont = FittingUtilities.Continuum(wave, flux, lowreject=2, highreject=4) orders.append(DataStructures.xypoint(x=wave, y=flux, err=err, cont=cont)) if return_aps: # Return the aperture wavefields too orders = [orders, retdict['wavefields']] return orders
def MakeXYpoints(datafile, errors=False, extensions=False, x=None, y=None, cont=None): print "Reading in file %s: " % datafile if extensions: #This means the data is in fits extensions, with one order per extension #At least x and y should be given (and should be strings to identify the field in the table record array) if type(x) != str: x = raw_input("Give name of the field which contains the x array: ") if type(y) != str: y = raw_input("Give name of the field which contains the y array: ") orders = [] hdulist = pyfits.open(datafile) if cont == None: if not errors: for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y)) orders.append(xypt) else: if type(errors) != str: errors = raw_input("Give name of the field which contains the errors/sigma array: ") for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y), err=data.field(errors)) orders.append(xypt) elif type(cont) == str: if not errors: for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y), cont=data.field(cont)) orders.append(xypt) else: if type(errors) != str: errors = raw_input("Give name of the field which contains the errors/sigma array: ") for i in range(1, len(hdulist)): data = hdulist[i].data xypt = DataStructures.xypoint(x=data.field(x), y=data.field(y), cont=data.field(cont), err=data.field(errors)) orders.append(xypt) else: #Data is in multispec format rather than in fits extensions #Call Rick White's script retdict = multispec.readmultispec(datafile) #Check if wavelength units are in angstroms (common, but I like nm) header = pyfits.getheader(datafile) wave_factor = 1.0 #factor to multiply wavelengths by to get them in nanometers for key in sorted(header.keys()): if "WAT1" in key: if "label=Wavelength" in header[key] and "units" in header[key]: units = header[key].split("units=")[-1] if units == "angstroms" or units == "Angstroms": wave_factor = units.angstroms.to(units.nm) print "Wavelength units are Angstroms. Scaling wavelength by ", wave_factor if errors == False: numorders = retdict['flux'].shape[0] else: numorders = retdict['flux'].shape[1] print "There are %i orders" % numorders orders = [] for i in range(numorders): wave = retdict['wavelen'][i] * wave_factor if errors == False: flux = retdict['flux'][i] err = np.ones(flux.size) * 1e9 err[flux > 0] = np.sqrt(flux[flux > 0]) else: if type(errors) != int: errors = int(raw_input("Enter the band number (in C-numbering) of the error/sigma band: ")) flux = retdict['flux'][0][i] err = retdict['flux'][errors][i] cont = FittingUtilities.Continuum(wave, flux, lowreject=2, highreject=4) orders.append(DataStructures.xypoint(x=wave, y=flux, err=err, cont=cont)) return orders