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

Source Code for Module drivers.GEN.two_file_math

  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: two_file_math.py 2350 2008-09-16 18:23:37Z 2zr $ 
 24   
 25  """ 
 26  This program reads in two sets of one or more files and performs a specified 
 27  simple math execution between the two file sets. The program will also allow 
 28  the final spectrum to be rescaled before being written out to file. 
 29  """ 
 30   
31 -def run(config, tim):
32 """ 33 This method is where the data reduction process gets done. 34 35 @param config: Object containing the data reduction configuration 36 information. 37 @type config: L{hlr_utils.Configure} 38 39 @param tim: Object that will allow the method to perform timing 40 evaluations. 41 @type tim: C{sns_time.DiffTime} 42 """ 43 import common_lib 44 import dr_lib 45 46 if tim is not None: 47 tim.getTime(False) 48 old_time = tim.getOldTime() 49 50 if config.data1 is None or config.data2 is None: 51 raise RuntimeError("Need to pass a data filename(s) to the driver "\ 52 +"script.") 53 54 dst_type1 = hlr_utils.file_peeker(config.data1[0]) 55 56 if config.verbose: 57 print "Initial file type (data set 1):", dst_type1 58 59 d_som1 = dr_lib.add_files(config.data1, dst_type=dst_type1, 60 Verbose=config.verbose, 61 Timer=tim) 62 63 dst_type2 = hlr_utils.file_peeker(config.data2[0]) 64 65 if config.verbose: 66 print "Initial file type (data set 2):", dst_type2 67 68 d_som2 = dr_lib.add_files(config.data2, dst_type=dst_type2, 69 Verbose=config.verbose, 70 Timer=tim) 71 72 # Get requested simple math operation 73 func = common_lib.__getattribute__(config.operation) 74 75 d_som3 = func(d_som1, d_som2) 76 77 del d_som1, d_som2 78 79 # Rescale data if necessary 80 if config.rescale is not None: 81 d_som4 = common_lib.mult_ncerr(d_som3, (config.rescale, 0.0)) 82 else: 83 d_som4 = d_som3 84 85 del d_som3 86 87 # Write out file after simple math operation 88 hlr_utils.write_file(config.output, dst_type1, d_som4, 89 verbose=config.verbose, 90 replace_ext=False, 91 path_replacement=config.path_replacement, 92 axis_ok=True, 93 message="operated file") 94 95 if tim is not None: 96 tim.setOldTime(old_time) 97 tim.getTime(msg="Total Running Time")
98 99 if __name__ == "__main__": 100 import hlr_utils 101 102 # Make description for driver 103 description = [] 104 description.append("") 105 description.append("This driver reads in data reduction produced 3-column") 106 description.append("ASCII or DAVE 2D ASCII files. The expected filenames") 107 description.append("should be of the form <inst_name>_<run_number>_") 108 description.append("<segment_number>[_dataset_type].<ext>. If they are") 109 description.append("not, the driver will still work, but it is possible") 110 description.append("that the first data file will be overwritten. In") 111 description.append("this case, it is best to provide an output filename") 112 description.append("via the command-line. Once the files are read in,") 113 description.append("the reqested simple math operation is performed on") 114 description.append("the two sets of data: data1 op data2. The default") 115 description.append("operation is add. The final result can also be") 116 description.append("rescaled by providing the scaling constant.") 117 118 # Set up the options available 119 parser = hlr_utils.BasicOptions("usage: %prog [options]", None, None, 120 hlr_utils.program_version(), 'error', 121 " ".join(description)) 122 123 # Specify the first data set 124 parser.add_option("", "--data1", dest="data1", help="Specify the files "\ 125 +"of the first dataset") 126 127 # Specify the second data set 128 parser.add_option("", "--data2", dest="data2", help="Specify the files "\ 129 +"of the second dataset") 130 131 # Specify the operation as addition 132 parser.add_option("-a", "--add", dest="operation", action="store_const", 133 const="add_ncerr", help="Perform data1 + data2") 134 parser.set_defaults(operation="add_ncerr") 135 136 # Specify the operation as subtraction 137 parser.add_option("-s", "--sub", dest="operation", action="store_const", 138 const="sub_ncerr", help="Perform data1 - data2") 139 140 # Specify the operation as multiplication 141 parser.add_option("-m", "--mult", dest="operation", action="store_const", 142 const="mult_ncerr", help="Perform data1 * data2") 143 144 # Specify the operation as division 145 parser.add_option("-d", "--div", dest="operation", action="store_const", 146 const="div_ncerr", help="Perform data1 / data2") 147 148 # Specify the rescaling constant 149 parser.add_option("-r", "--rescale", dest="rescale", help="Specify a "\ 150 +"constant with which to rescale the final data.") 151 152 parser.add_option("", "--timing", action="store_true", dest="timing", 153 help="Flag to turn on timing of code") 154 parser.set_defaults(timing=False) 155 156 # Change help message for output option 157 parser.get_option("-o").help = "Specify a new output file name, a new "\ 158 +"data directory or a new directory plus "\ 159 +"output file name. The new directory "\ 160 +"must exist. The default is to use the "\ 161 +"current working directory and the first "\ 162 +"data file as the basis for the output "\ 163 +"file name." 164 165 # Do not need to use the following options 166 parser.remove_option("--config") 167 parser.remove_option("--data") 168 169 (options, args) = parser.parse_args() 170 171 # set up the configuration 172 configure = hlr_utils.Configure() 173 174 # Need to set the inst parameter to None to spoof data file finder 175 configure.inst = None 176 177 # Need to set the facility parameter to None to spoof data file finder 178 configure.facility = None 179 180 # Need to spoof the data check 181 configure.data = [""] 182 options.data = None 183 184 # Quick test on output 185 if options.output is not None: 186 filename = hlr_utils.fix_filename(options.output) 187 import os 188 # If this is a directory only, then we want to have the default 189 # created filename 190 if os.path.isdir(filename) and not os.path.isfile(filename): 191 have_output = False 192 is_dir = True 193 else: 194 have_output = True 195 is_dir = False 196 else: 197 have_output = False 198 is_dir = False 199 200 # Temporarily silence verbosity 201 old_verbosity = options.verbose 202 options.verbose = False 203 204 # Call the configuration setter for SNSOptions 205 hlr_utils.BasicConfiguration(parser, configure, options, args) 206 207 # Reset verbosity 208 configure.verbose = old_verbosity 209 210 # This is a standard file, but we need to remove the segment number 211 if not have_output: 212 if is_dir: 213 dired = os.path.dirname(configure.output) 214 dired += "/" 215 infile = os.path.basename(configure.output) 216 else: 217 dired = "" 218 infile = configure.output 219 220 # Check and remove extra extensions. This happens when original 221 # extension is not .txt 222 if infile.rfind('.') != infile.find('.'): 223 infile = infile[:infile.rfind('.')] 224 225 configure.path_replacement = dired 226 parts = infile.split('_') 227 outfile = dired + "_".join(parts[:2]) 228 # Have dataset tag 229 if len(parts) == 4: 230 configure.output = outfile + "_" + parts[-1] 231 else: 232 ext_parts = parts[-1].split('.') 233 configure.output = outfile + "." + ext_parts[-1] 234 # An output file has been provided so do nothing 235 else: 236 pass 237 238 if configure.verbose: 239 print "Using %s as output file" % configure.output 240 241 # Set the files from the first data set 242 configure.data1 = hlr_utils.determine_files(options.data1, 243 configure.inst, 244 configure.facility, 245 stop_on_none=True) 246 247 # Set the files from the second data set 248 configure.data2 = hlr_utils.determine_files(options.data2, 249 configure.inst, 250 configure.facility, 251 stop_on_none=True) 252 253 # Set the math operation 254 configure.operation = options.operation 255 256 # Set the rescaling constant 257 try: 258 configure.rescale = float(options.rescale) 259 except TypeError: 260 configure.rescale = options.rescale 261 262 # Setup the timing object 263 if options.timing: 264 import sns_timing 265 timer = sns_timing.DiffTime() 266 else: 267 timer = None 268 269 # Run the program 270 run(configure, timer) 271