def combine_all(infns, outfn, expected_nsubbands=None): """Given a list of ArchiveFile objects group them into sub-bands then remove the edges of each sub-band to remove the artifacts caused by aliasing. Finally, combine the sub-bands into a single output file. The combined sub-band files are not saved. Inputs: infns: A list of input ArchiveFile objects. outfn: The output file's name. expected_nsubbands: The expected number of subbands for each subintegration. Outputs: combinedfns: A list of output (combined) files. """ if expected_nsubbands is None: expected_nsubbands = config.cfg.expected_nsubbands infns = check_files(infns, expected_nsubbands=expected_nsubbands) groups = group_files(infns) combinedfiles = [] # Combine files from the same sub-band in the time direction for group in groups: subbands = [] for ctr_freq, to_combine in utils.group_by_ctr_freq(group).iteritems(): utils.print_info("Combining %d subints at ctr freq %d MHz" % \ (len(to_combine), ctr_freq), 3) # Combine sub-integrations for this sub-band subfn = utils.get_outfn(outfn + ".%(freq)dMHz", to_combine[0]) if subfn in [f.fn for f in subbands]: warnings.warn("'combined_all(...)' is overwritting files it " \ "previously created!") subband = combine_subints(to_combine, subfn) clean.trim_edge_channels(subband) subbands.append(subband) combinedfn = utils.get_outfn(outfn, subbands[0]) utils.print_info("Combining %d subbands into %s" % \ (len(subbands), combinedfn), 3) if combinedfn in [f.fn for f in combinedfiles]: warnings.warn("'combined_all(...)' is overwritting files it " \ "previously created!") combinedfile = combine_subbands(subbands, combinedfn) combinedfiles.append(combinedfile) if not config.debug.INTERMEDIATE: # Remove the temporary combined files for sub in subbands: os.remove(sub.fn) return combinedfiles
def clean_archive(inarf, outfn, clean_re=None, *args, **kwargs): import psrchive # Temporarily, because python bindings # are not available on all computers if clean_re is None: clean_re = config.cfg.clean_strategy try: outfn = utils.get_outfn(outfn, inarf) shutil.copy(inarf.fn, outfn) outarf = utils.ArchiveFile(outfn) trim_edge_channels(outarf) prune_band(outarf) remove_bad_channels(outarf) remove_bad_subints(outarf) matching_cleaners = [ clnr for clnr in cleaners if clean_re and re.search(clean_re, clnr) ] if len(matching_cleaners) == 1: ar = psrchive.Archive_load(outarf.fn) cleaner = eval(matching_cleaners[0]) utils.print_info( "Cleaning using '%s(...)'." % matching_cleaners[0], 2) cleaner(ar, *args, **kwargs) ar.unload(outfn) elif len(matching_cleaners) == 0: utils.print_info("No cleaning strategy selected. Skipping...", 2) else: raise errors.CleanError("Bad cleaner selection. " \ "'%s' has %d matches." % \ (clean_re, len(matching_cleaners))) except: # An error prevented cleaning from being successful # Remove the output file because it may confuse the user if os.path.exists(outfn): os.remove(outfn) raise return outarf
def main(): print "" print " clean.py" print " Patrick Lazarus" print "" file_list = args.files + args.from_glob to_exclude = args.excluded_files + args.excluded_by_glob to_clean = utils.exclude_files(file_list, to_exclude) print "Number of input files: %d" % len(to_clean) # Read configurations for infn in to_clean: inarf = utils.ArchiveFile(infn) config.cfg.load_configs_for_archive(inarf) outfn = utils.get_outfn(args.outfn, inarf) shutil.copy(inarf.fn, outfn) outarf = utils.ArchiveFile(outfn) ar = outarf.get_archive() try: for name, cfgstrs in args.cleaner_queue: # Set up the cleaner cleaner = cleaners.load_cleaner(name) for cfgstr in cfgstrs: cleaner.parse_config_string(cfgstr) cleaner.run(ar) except: # An error prevented cleaning from being successful # Remove the output file because it may confuse the user #if os.path.exists(outfn): # os.remove(outfn) raise finally: ar.unload(outfn) print "Cleaned archive: %s" % outfn
def run_coastguard(calname, cal_dz): #calname = sys.argv[1] #cal_dz = sys.argv[2] print calname inarf = utils.ArchiveFile(calname) config.cfg.load_configs_for_archive(inarf) outfn = utils.get_outfn(cal_dz, inarf) shutil.copy(inarf.fn, outfn) outarf = utils.ArchiveFile(outfn) ar = outarf.get_archive() cleaner = cleaners.load_cleaner('preclean') # hard coded, need to be fixed #for cfgstr in cfgstrs: # cleaner.parse_config_string(cfgstr) cleaner.run(ar) cleaner = cleaners.load_cleaner('surgical') # hard coded, need to be fixed cleaner.run(ar) print(type(outfn)) ar.unload(str(outfn)) print "Cleaned archive: %s" % outfn