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 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
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
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
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
105 parser.remove_option("--config")
106
107 (options, args) = parser.parse_args()
108
109
110 configure = hlr_utils.Configure()
111
112
113 configure.inst = None
114
115
116 if options.output is not None:
117 filename = hlr_utils.fix_filename(options.output)
118 import os
119
120
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
132 old_verbosity = options.verbose
133 options.verbose = False
134
135
136 hlr_utils.BasicConfiguration(parser, configure, options, args)
137
138
139 configure.verbose = old_verbosity
140
141
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
152
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
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
166 else:
167 pass
168
169 if configure.verbose:
170 print "Using %s as output file" % configure.output
171
172
173 if options.timing:
174 import sns_timing
175 timer = sns_timing.DiffTime()
176 else:
177 timer = None
178
179
180 run(configure, timer)
181