def vanilla_pypeit_file(pypeit_file, root, spectrograph, extension='.fits'): """ Write a vanilla PypeIt file. Args: pypeit_file (str): Name of PypeIt file to be generated root (str): spectrograph (str): Name of spectrograph extension (str, optional): File extension Returns: """ # Generate dfname = os.path.join(root, '*{0}*'.format(extension)) \ if os.path.isdir(root) else '{0}*{1}*'.format(root, extension) # configuration lines cfg_lines = ['[rdx]'] cfg_lines += [' spectrograph = {0}'.format(spectrograph)] # cfg_lines += [' sortroot = {0}'.format(root)] make_pypeit_file(pypeit_file, spectrograph, [dfname], cfg_lines=cfg_lines, setup_mode=True)
def test_select_detectors(): # Generate a PYPIT file pypit_file = data_path('test.pypeit') make_pypeit_file(pypit_file, 'shane_kast_blue', [data_path('b*fits.gz')], setup_mode=True) # Perform the setup setup = pypeitsetup.PypeItSetup.from_pypeit_file(pypit_file) par, spectrograph, fitstbl = setup.run(sort_dir=data_path('')) assert PypeIt.select_detectors(detnum=par['rdx']['detnum'], ndet=spectrograph.ndet) == [1], \ 'Incorrect detectors selected.' # Clean-up os.remove(data_path('test.calib')) os.remove(data_path('test.pypeit')) assert np.array_equal(PypeIt.select_detectors(), [1]), 'Incorrect detectors selected.' assert np.array_equal(PypeIt.select_detectors(detnum=3, ndet=5), [3]), \ 'Incorrect detectors selected.' assert np.array_equal(PypeIt.select_detectors(ndet=5), [1,2,3,4,5]), \ 'Incorrect detectors selected.' assert np.array_equal(PypeIt.select_detectors(detnum=[1,3]), [1,3]), \ 'Incorrect detectors selected.'
def test_initialization(): """ Load input PYPIT file """ # Generate a PYPIT file pypit_file = data_path('test.pypeit') make_pypeit_file(pypit_file, 'shane_kast_blue', [data_path('b*fits.gz')], setup_mode=True) # Perform the setup setup = pypeitsetup.PypeItSetup.from_pypeit_file(pypit_file) par, spectrograph, fitstbl = setup.run(sort_dir=data_path('')) # Test assert spectrograph.spectrograph == 'shane_kast_blue' assert len(fitstbl) == 2 assert par['calibrations']['arcframe']['number'] == 1
def test_initialization(): """ Load input PypeIt file """ # Generate a PYPIT file pypit_file = data_path('test.pypeit') make_pypeit_file(pypit_file, 'shane_kast_blue', [data_path('b*fits.gz')], setup_mode=True) # Perform the setup setup = pypeitsetup.PypeItSetup.from_pypeit_file(pypit_file) par, spectrograph, fitstbl = setup.run(sort_dir=data_path('')) # Test assert spectrograph.name == 'shane_kast_blue' assert len(fitstbl) == 8 # Clean-up os.remove(data_path('test.calib')) os.remove(data_path('test.pypeit'))
def main(args): """ This setups PypeIt files for fluxing, coadding and telluric corrections. It will produce three files named as your_spectragraph.flux, your_spectragraph.coadd1d, and your_spectragraph.tell """ allfiles = os.listdir(args.sci_path) allfiles = np.sort(allfiles) spec1dfiles = [] spec2dfiles = [] spec1dinfos = [] for ifile in allfiles: if ('spec1d' in ifile) and ('.fits' in ifile): spec1dfiles.append(ifile) elif ('spec2d' in ifile) and ('.fits' in ifile): spec2dfiles.append(ifile) elif ('spec1d' in ifile) and ('.txt' in ifile): spec1dinfos.append(ifile) else: msgs.warn('{:} is not a standard PypeIt output.'.format(ifile)) if len(spec2dfiles) > len(spec1dfiles): msgs.warn('The following exposures do not have 1D extractions:') for ii in range(len(spec2dfiles)): if not os.path.exists( os.path.join(args.sci_path, spec2dfiles[ii].replace( 'spec2d', 'spec1d'))): msgs.info('\t {:}'.format(spec2dfiles[ii])) if len(spec1dfiles) > 0: par = io.fits_open(os.path.join(args.sci_path, spec1dfiles[0])) ## fluxing pypeit file spectrograph = par[0].header['PYP_SPEC'] pypeline = par[0].header['PYPELINE'] flux_file = '{:}.flux'.format(spectrograph) cfg_lines = ['[fluxcalib]'] cfg_lines += [ ' extinct_correct = False # Set to True if your SENSFUNC derived with the UVIS algorithm\n' ] cfg_lines += [ '# Please add your SENSFUNC file name below before running pypeit_flux_calib' ] make_pypeit_file(flux_file, spectrograph, spec1dfiles, cfg_lines=cfg_lines, setup_mode=True) fin = open(flux_file, "rt") data = fin.read() data = data.replace('spec1d_', os.path.join(args.sci_path, 'spec1d_')) data = data.replace('data', 'flux') fin.close() fin = open(flux_file, "wt") fin.write(data) fin.close() ## coadd1d pypeit file coadd1d_file = '{:}.coadd1d'.format(spectrograph) cfg_lines = ['[coadd1d]'] cfg_lines += [ ' coaddfile = YOUR_OUTPUT_FILE_NAME # Please set your output file name' ] cfg_lines += [ ' sensfuncfile = YOUR_SENSFUNC_FILE # Please set your SENSFUNC file name' ] if pypeline == 'Echelle': cfg_lines += [ ' wave_method = velocity # creates a uniformly space grid in log10(lambda)\n' ] else: cfg_lines += [ ' wave_method = linear # creates a uniformly space grid in lambda\n' ] cfg_lines += ['# This file includes all extracted objects. You need to figure out which object you want to \n'+\ '# coadd before running pypeit_coadd_1dspec!!!'] spec1d_info = [] for ii in range(len(spec1dfiles)): meta_tbl = Table.read(os.path.join(args.sci_path, spec1dfiles[ii]).replace( '.fits', '.txt'), format='ascii.fixed_width') _, indx = np.unique(meta_tbl['name'], return_index=True) objects = meta_tbl[indx] for jj in range(len(objects)): spec1d_info.append(spec1dfiles[ii] + ' ' + objects['name'][jj]) make_pypeit_file(coadd1d_file, spectrograph, spec1d_info, cfg_lines=cfg_lines, setup_mode=True) fin = open(coadd1d_file, "rt") data = fin.read() data = data.replace('spec1d_', os.path.join(args.sci_path, 'spec1d_')) data = data.replace('data', 'coadd1d') fin.close() fin = open(coadd1d_file, "wt") fin.write(data) fin.close() ## tellfit pypeit file tellfit_file = '{:}.tell'.format(spectrograph) cfg_lines = ['[tellfit]'] if args.objmodel == 'qso': cfg_lines += [' objmodel = qso'] cfg_lines += [' redshift = 0.0'] cfg_lines += [' bal_wv_min_max = 10000.,11000.'] elif args.objmodel == 'star': cfg_lines += [' objmodel = star'] cfg_lines += [' star_type = A0'] cfg_lines += [' star_mag = 0.0'] elif args.objmodel == 'poly': cfg_lines += [' objmodel = poly'] cfg_lines += [' polyorder = 5'] cfg_lines += [' fit_wv_min_max = 17000.0,22000.0'] with open(tellfit_file, 'w') as f: f.write('# Auto-generated PypeIt file\n') f.write('# {0}\n'.format( time.strftime("%a %d %b %Y %H:%M:%S", time.localtime()))) f.write("\n") f.write("# User-defined execution parameters\n") f.write( "# This is only an example. Make sure to change the following parameters accordingly.\n" ) f.write('\n'.join(cfg_lines)) f.write('\n') f.write('\n') msgs.info('PypeIt file written to: {0}'.format(tellfit_file))
def run_on_science(pargs, script_Utils, calib_pypeit_file, ps_sci): """ Process a science frame Args: pargs (argparse.ArgumentParser): Command line arguments script_Utils: calib_pypeit_file (str): ps_sci (:class:`pypeit.pypeitsetup.PypeItSetup`): """ # Load up and slurp PypeIt file ps = PypeItSetup.from_pypeit_file(calib_pypeit_file) # Parse science file info science_file = os.path.join(pargs.full_rawpath, pargs.science) science_pypeit = calib_pypeit_file.replace('calib', 'science') # Add to file list, it not present if science_file not in ps.file_list: ps.file_list += [science_file] # Add to usrdata new_row = {} for key in ps.usrdata.keys(): new_row[key] = ps_sci.fitstbl[key][0] ps.usrdata.add_row(new_row) # Build _ = ps.build_fitstbl() # Remove any extraneous science files in the folder gd_files = (ps.usrdata['filename'] == os.path.basename(science_file)) | ( ps.usrdata['frametype'] != 'science') # Update #ps.fitstbl.table = ps.fitstbl.table[gd_files] #ps.file_list = np.array(ps.file_list)[gd_files].tolist() ps.usrdata = ps.usrdata[gd_files] # Generate PypeIt file # TODO -- Generalize this with metadata.write_pypeit() # Write the file setup_lines = yaml.dump(utils.yamlify(ps.setup_dict)).split('\n')[:-1] with io.StringIO() as ff: ps.usrdata.write(ff, format='ascii.fixed_width') data_lines = ff.getvalue().split('\n')[:-1] paths = [os.path.dirname(item) for item in ps.file_list] paths = np.unique(paths) # Add to configs if pargs.slit_spat is not None: # Remove detnum for kk, item in enumerate(ps.user_cfg): if 'detnum' in item: ps.user_cfg.pop(kk) ridx = ps.user_cfg.index('[rdx]') ps.user_cfg.insert(ridx + 1, ' slitspatnum = {0}'.format(pargs.slit_spat)) # this is to avoid that the default detnum (which was introduced for mosaic) # will be passed to the reduction and crash it ps.user_cfg.insert(ridx + 2, ' detnum = None') else: raise NotImplementedError('NOT READY: 118 of ql_deimos') # Do it make_pypeit_file(science_pypeit, script_Utils.spectrograph.name, [], cfg_lines=ps.user_cfg, setup_lines=setup_lines, sorted_files=data_lines, paths=paths) # Run me! redux_path = os.path.dirname(science_pypeit) # Path to PypeIt file run_pargs = run_pypeit.RunPypeIt.parse_args([ science_pypeit, '-r={}'.format(redux_path), ]) run_pypeit.RunPypeIt.main(run_pargs)