Package drivers :: Package GEN :: Module agg_dr_files
[hide private]
[frames] | no frames]

Source Code for Module drivers.GEN.agg_dr_files

  1  #                  High-Level Reduction Functions 
  2  #           A part of the SNS Analysis Software Suite. 
  3  # 
  4  #                  Spallation Neutron Source 
  5  #          Oak Ridge National Laboratory, Oak Ridge TN. 
  6  # 
  7  # 
  8  #                             NOTICE 
  9  # 
 10  # For this software and its associated documentation, permission is granted 
 11  # to reproduce, prepare derivative works, and distribute copies to the public 
 12  # for any purpose and without fee. 
 13  # 
 14  # This material was prepared as an account of work sponsored by an agency of 
 15  # the United States Government.  Neither the United States Government nor the 
 16  # United States Department of Energy, nor any of their employees, makes any 
 17  # warranty, express or implied, or assumes any legal liability or 
 18  # responsibility for the accuracy, completeness, or usefulness of any 
 19  # information, apparatus, product, or process disclosed, or represents that 
 20  # its use would not infringe privately owned rights. 
 21  # 
 22   
 23  # $Id: agg_dr_files.py 2350 2008-09-16 18:23:37Z 2zr $ 
 24   
 25  """ 
 26  This program reads in data reduction produced 3-column ASCII or DAVE 2D ASCII 
 27  files. 
 28  """ 
 29   
30 -def run(config, tim):
31 """ 32 This method is where the data reduction process gets done. 33 34 @param config: Object containing the data reduction configuration 35 information. 36 @type config: L{hlr_utils.Configure} 37 38 @param tim: Object that will allow the method to perform timing 39 evaluations. 40 @type tim: C{sns_time.DiffTime} 41 """ 42 import dr_lib 43 44 if tim is not None: 45 tim.getTime(False) 46 old_time = tim.getOldTime() 47 48 if config.data is None: 49 raise RuntimeError("Need to pass a data filename to the driver "\ 50 +"script.") 51 52 dst_type = hlr_utils.file_peeker(config.data[0]) 53 54 if config.verbose: 55 print "Initial file type:", dst_type 56 57 d_som1 = dr_lib.add_files(config.data, dst_type=dst_type, 58 Verbose=config.verbose, 59 Timer=tim) 60 61 hlr_utils.write_file(config.output, dst_type, d_som1, 62 verbose=config.verbose, 63 replace_ext=False, 64 path_replacement=config.path_replacement, 65 axis_ok=True, 66 message="combined file") 67 68 if tim is not None: 69 tim.setOldTime(old_time) 70 tim.getTime(msg="Total Running Time")
71 72 if __name__ == "__main__": 73 import hlr_utils 74 75 # Make description for driver 76 description = [] 77 description.append("This driver reads in data reduction produced 3-column") 78 description.append("ASCII or DAVE 2D ASCII files. The expected filenames") 79 description.append("should be of the form <inst_name>_<run_number>_") 80 description.append("<segment_number>[_dataset_type].<ext>. If they are") 81 description.append("not, the driver will still work, but it is possible") 82 description.append("that the first data file will be overwritten. In") 83 description.append("this case, it is best to provide an output filename") 84 description.append("via the command-line.") 85 86 # Set up the options available 87 parser = hlr_utils.BasicOptions("usage: %prog [options] <datafiles>", None, 88 None, hlr_utils.program_version(), 'error', 89 " ".join(description)) 90 91 parser.add_option("", "--timing", action="store_true", dest="timing", 92 help="Flag to turn on timing of code") 93 parser.set_defaults(timing=False) 94 95 # Change help message for output option 96 parser.get_option("-o").help = "Specify a new output file name, a new "\ 97 +"data directory or a new directory plus "\ 98 +"output file name. The new directory "\ 99 +"must exist. The default is to use the "\ 100 +"current working directory and the first "\ 101 +"data file as the basis for the output "\ 102 +"file name." 103 104 # Do not need to use config option 105 parser.remove_option("--config") 106 107 (options, args) = parser.parse_args() 108 109 # set up the configuration 110 configure = hlr_utils.Configure() 111 112 # Need to set the inst parameter to None to spoof data file finder 113 configure.inst = None 114 115 # Quick test on output 116 if options.output is not None: 117 filename = hlr_utils.fix_filename(options.output) 118 import os 119 # If this is a directory only, then we want to have the default 120 # created filename 121 if os.path.isdir(filename) and not os.path.isfile(filename): 122 have_output = False 123 is_dir = True 124 else: 125 have_output = True 126 is_dir = False 127 else: 128 have_output = False 129 is_dir = False 130 131 # Temporarily silence verbosity 132 old_verbosity = options.verbose 133 options.verbose = False 134 135 # Call the configuration setter for SNSOptions 136 hlr_utils.BasicConfiguration(parser, configure, options, args) 137 138 # Reset verbosity 139 configure.verbose = old_verbosity 140 141 # This is a standard file, but we need to remove the segment number 142 if not have_output: 143 if is_dir: 144 dired = os.path.dirname(configure.output) 145 dired += "/" 146 infile = os.path.basename(configure.output) 147 else: 148 dired = "" 149 infile = configure.output 150 151 # Check and remove extra extensions. This happens when original 152 # extension is not .txt 153 if infile.rfind('.') != infile.find('.'): 154 infile = infile[:infile.rfind('.')] 155 156 configure.path_replacement = dired 157 parts = infile.split('_') 158 outfile = dired + "_".join(parts[:2]) 159 # Have dataset tag 160 if len(parts) == 4: 161 configure.output = outfile + "_" + parts[-1] 162 else: 163 ext_parts = parts[-1].split('.') 164 configure.output = outfile + "." + ext_parts[-1] 165 # An output file has been provided so do nothing 166 else: 167 pass 168 169 if configure.verbose: 170 print "Using %s as output file" % configure.output 171 172 # Setup the timing object 173 if options.timing: 174 import sns_timing 175 timer = sns_timing.DiffTime() 176 else: 177 timer = None 178 179 # Run the program 180 run(configure, timer) 181