1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
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
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
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
119 parser = hlr_utils.BasicOptions("usage: %prog [options]", None, None,
120 hlr_utils.program_version(), 'error',
121 " ".join(description))
122
123
124 parser.add_option("", "--data1", dest="data1", help="Specify the files "\
125 +"of the first dataset")
126
127
128 parser.add_option("", "--data2", dest="data2", help="Specify the files "\
129 +"of the second dataset")
130
131
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
137 parser.add_option("-s", "--sub", dest="operation", action="store_const",
138 const="sub_ncerr", help="Perform data1 - data2")
139
140
141 parser.add_option("-m", "--mult", dest="operation", action="store_const",
142 const="mult_ncerr", help="Perform data1 * data2")
143
144
145 parser.add_option("-d", "--div", dest="operation", action="store_const",
146 const="div_ncerr", help="Perform data1 / data2")
147
148
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
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
166 parser.remove_option("--config")
167 parser.remove_option("--data")
168
169 (options, args) = parser.parse_args()
170
171
172 configure = hlr_utils.Configure()
173
174
175 configure.inst = None
176
177
178 configure.facility = None
179
180
181 configure.data = [""]
182 options.data = None
183
184
185 if options.output is not None:
186 filename = hlr_utils.fix_filename(options.output)
187 import os
188
189
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
201 old_verbosity = options.verbose
202 options.verbose = False
203
204
205 hlr_utils.BasicConfiguration(parser, configure, options, args)
206
207
208 configure.verbose = old_verbosity
209
210
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
221
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
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
235 else:
236 pass
237
238 if configure.verbose:
239 print "Using %s as output file" % configure.output
240
241
242 configure.data1 = hlr_utils.determine_files(options.data1,
243 configure.inst,
244 configure.facility,
245 stop_on_none=True)
246
247
248 configure.data2 = hlr_utils.determine_files(options.data2,
249 configure.inst,
250 configure.facility,
251 stop_on_none=True)
252
253
254 configure.operation = options.operation
255
256
257 try:
258 configure.rescale = float(options.rescale)
259 except TypeError:
260 configure.rescale = options.rescale
261
262
263 if options.timing:
264 import sns_timing
265 timer = sns_timing.DiffTime()
266 else:
267 timer = None
268
269
270 run(configure, timer)
271