1 |
#!/home/jsoc/bin/linux_x86_64/activeperl |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use JSON; |
6 |
use File::Basename; |
7 |
use FindBin qw($Bin); |
8 |
use lib "$Bin/../../../base/libs/perl"; |
9 |
use Data::Dumper; |
10 |
use drmsArgs; |
11 |
use drmsRunProg; |
12 |
|
13 |
# Required cmd-line arguments |
14 |
use constant kArgKeyNames => "keynames"; # Full path to file containing key names |
15 |
use constant kArgKeyVals => "keyvals"; # Full path to file containing key values |
16 |
use constant kArgBitMaps => "bmaps"; # Path to bit maps |
17 |
use constant kArgBitMapFname => "fname"; # Base file name of bit-map files |
18 |
use constant kArgSeries => "series"; # The series into which we are adding records |
19 |
use constant kArgSegment => "segment"; # The segment in series 'series' that will contain data files |
20 |
|
21 |
# Optional cmd-line arguments |
22 |
use constant kOptSetDate => "d"; # Pass to rawingest |
23 |
|
24 |
use constant kBatchSize => 128; |
25 |
|
26 |
my($argsinH); |
27 |
my(@args); |
28 |
my($optsinH); |
29 |
my($opts); |
30 |
my($kevals); |
31 |
my($keynames); |
32 |
my($keyvals); |
33 |
my($bmaps); |
34 |
my($fname); |
35 |
my($series); |
36 |
my($segment); |
37 |
my($setdate); |
38 |
my($dataH); |
39 |
my($cpkeyH); |
40 |
my($cpkeyParentH); |
41 |
my($fh); |
42 |
my($line); |
43 |
my($iline); |
44 |
my(@pkeys); # Prime-key keyword names. |
45 |
my($countpkeys); |
46 |
my($npkeys); |
47 |
my(@okeys); # Non-prime-key keyword names. |
48 |
my(@vals); # All vals |
49 |
my(@pkeyvals); # Prime-key keyword values. |
50 |
my($ival); |
51 |
my($hack); |
52 |
my($fpath); |
53 |
my($rv); |
54 |
|
55 |
$rv = 0; # success |
56 |
|
57 |
# Collect arguments |
58 |
$argsinH = |
59 |
{ |
60 |
&kArgKeyNames => 's', |
61 |
&kArgKeyVals => 's', |
62 |
&kArgBitMaps => 's', |
63 |
&kArgBitMapFname => 's', |
64 |
&kArgSeries => 's', |
65 |
&kArgSegment => 's' |
66 |
}; |
67 |
|
68 |
@args = GetArgs($argsinH); |
69 |
|
70 |
$optsinH = |
71 |
{ |
72 |
&kOptSetDate => 'noval' |
73 |
}; |
74 |
|
75 |
$opts = GetOpts($optsinH); |
76 |
|
77 |
if (@args) |
78 |
{ |
79 |
($keynames, $keyvals, $bmaps, $fname, $series, $segment) = @args; |
80 |
if (defined($opts)) |
81 |
{ |
82 |
$setdate = $opts->Get(&kOptSetDate); |
83 |
# $setdate will be undefined if there was no flag supplied on the cmd-line |
84 |
} |
85 |
|
86 |
# First put the keynames into an array. |
87 |
if (open($fh, "<$keynames")) |
88 |
{ |
89 |
$npkeys = 0; |
90 |
while (defined($line = <$fh>)) |
91 |
{ |
92 |
chomp($line); |
93 |
|
94 |
# skip blank/empty/ws lines |
95 |
if ($line =~ /^\s*$/) |
96 |
{ |
97 |
next; |
98 |
} |
99 |
|
100 |
# skip lines that have text enclosed by [] |
101 |
if ($line =~ /^\s*\[(.+)\]/) |
102 |
{ |
103 |
if ($1 =~ /prime keys/i) |
104 |
{ |
105 |
# Start $npkeys counter. |
106 |
$countpkeys = 1; |
107 |
} |
108 |
elsif ($1 =~ /keys to change/i) |
109 |
{ |
110 |
# Stop $npkeys counter. |
111 |
$countpkeys = 0; |
112 |
} |
113 |
next; |
114 |
} |
115 |
|
116 |
if ($line =~ /^\s*(\S+)\s*$/) |
117 |
{ |
118 |
if ($countpkeys) |
119 |
{ |
120 |
push(@pkeys, $1); |
121 |
$npkeys++; |
122 |
} |
123 |
else |
124 |
{ |
125 |
push(@okeys, $1); |
126 |
} |
127 |
} |
128 |
else |
129 |
{ |
130 |
print STDERR "Unexpected format of line in $keynames: $line.\n"; |
131 |
$rv = 1; |
132 |
last; |
133 |
} |
134 |
} |
135 |
|
136 |
$fh->close(); |
137 |
} |
138 |
else |
139 |
{ |
140 |
print STDERR "Unable to open $keynames for reading.\n"; |
141 |
$rv = 1; |
142 |
} |
143 |
|
144 |
if ($rv == 0 && $npkeys > 0) |
145 |
{ |
146 |
# loop through the data file identified by $keyvals |
147 |
if (open($fh, "<$keyvals")) |
148 |
{ |
149 |
$iline = 0; |
150 |
$dataH = {}; |
151 |
|
152 |
while (defined($line = <$fh>)) |
153 |
{ |
154 |
chomp($line); |
155 |
|
156 |
# Skip comment lines. |
157 |
if ($line =~ /^\#/) |
158 |
{ |
159 |
next; |
160 |
} |
161 |
|
162 |
# Skip blank/empty/ws lines |
163 |
if ($line =~ /^\s*$/) |
164 |
{ |
165 |
next; |
166 |
} |
167 |
|
168 |
@vals = split(/,\s*/, $line); |
169 |
|
170 |
# The first two columns in this file contain the values for the |
171 |
# prime-key keywords: HARPNUM and T_REC. Make a hash-array layer |
172 |
# for each of these keywords. |
173 |
$fpath = $bmaps; |
174 |
$ival = 0; |
175 |
foreach my $aval (@vals) |
176 |
{ |
177 |
if ($ival < $npkeys) |
178 |
{ |
179 |
# These are prime-key keyword values. |
180 |
|
181 |
# Must hack the HARPNUM keyword - the keys file does not contain the |
182 |
# exact HARPNUM directory name. For the HARPNUM, it contains an integer, |
183 |
# but the directory name is a 0-padded string representation of this integer |
184 |
# with a total of 5 digits. %05d |
185 |
if ($ival == 0) |
186 |
{ |
187 |
$hack = sprintf("%05d", $aval); |
188 |
push(@pkeyvals, $hack); |
189 |
$fpath = "$fpath/$hack"; |
190 |
} |
191 |
else |
192 |
{ |
193 |
push(@pkeyvals, $aval); |
194 |
$fpath = "$fpath/$aval"; |
195 |
} |
196 |
} |
197 |
else |
198 |
{ |
199 |
$cpkeyH = $dataH; |
200 |
|
201 |
foreach my $pkeyval (@pkeyvals) |
202 |
{ |
203 |
if (!exists($cpkeyH->{$pkeyval})) |
204 |
{ |
205 |
$cpkeyH->{$pkeyval} = {}; |
206 |
} |
207 |
|
208 |
$cpkeyH = $cpkeyH->{$pkeyval}; |
209 |
} |
210 |
|
211 |
if (!exists($cpkeyH->{"keys"})) |
212 |
{ |
213 |
$cpkeyH->{"keys"} = {}; |
214 |
$cpkeyParentH = $cpkeyH; |
215 |
} |
216 |
|
217 |
$cpkeyH = $cpkeyH->{"keys"}; |
218 |
|
219 |
# These are non-prime-key keyword values. |
220 |
if (!exists($cpkeyH->{$okeys[$ival - $npkeys]})) |
221 |
{ |
222 |
$cpkeyH->{$okeys[$ival - $npkeys]} = $aval; |
223 |
} |
224 |
else |
225 |
{ |
226 |
# Error - there should not be an element for this prime-key value already. |
227 |
$rv = 1; |
228 |
last; |
229 |
} |
230 |
} |
231 |
|
232 |
$ival++; |
233 |
} |
234 |
|
235 |
# Set the filename of the file to ingest. |
236 |
$cpkeyParentH->{"file"} = "$fpath/$fname"; |
237 |
|
238 |
@pkeyvals = (); |
239 |
$iline++; |
240 |
|
241 |
# Call ingestion module with batches of records. |
242 |
if ($iline % &kBatchSize == 0) |
243 |
{ |
244 |
$rv = Ingest($series, $segment, $setdate, $dataH); |
245 |
if ($rv != 0) |
246 |
{ |
247 |
last; |
248 |
} |
249 |
|
250 |
$dataH = {}; |
251 |
$cpkeyH = undef; |
252 |
$cpkeyParentH = undef; |
253 |
} |
254 |
} # End while |
255 |
|
256 |
if ($rv == 0) |
257 |
{ |
258 |
if (keys(%$dataH) > 0 && defined($cpkeyH) && keys(%$cpkeyH) > 0) |
259 |
{ |
260 |
$rv = Ingest($series, $segment, $setdate, $dataH); |
261 |
} |
262 |
} |
263 |
|
264 |
$fh->close(); |
265 |
} |
266 |
else |
267 |
{ |
268 |
print STDERR "Unable to open key-names file $keynames.\n"; |
269 |
$rv = 1; |
270 |
} |
271 |
} |
272 |
} |
273 |
else |
274 |
{ |
275 |
print STDERR "Invalid arguments.\n"; |
276 |
} |
277 |
|
278 |
exit($rv); |
279 |
|
280 |
sub GetArgs |
281 |
{ |
282 |
my($argsinH) = @_; |
283 |
my($args); |
284 |
my($arg); |
285 |
my(@rv); |
286 |
|
287 |
$args = new drmsArgs($argsinH, 1); |
288 |
|
289 |
if (defined($args)) |
290 |
{ |
291 |
$arg = $args->Get(&kArgKeyNames); |
292 |
if (defined($arg)) |
293 |
{ |
294 |
push(@rv, $arg); |
295 |
$arg = $args->Get(&kArgKeyVals); |
296 |
if (defined($arg)) |
297 |
{ |
298 |
push(@rv, $arg); |
299 |
$arg = $args->Get(&kArgBitMaps); |
300 |
if (defined($arg)) |
301 |
{ |
302 |
push(@rv, $arg); |
303 |
$arg = $args->Get(&kArgBitMapFname); |
304 |
if (defined($arg)) |
305 |
{ |
306 |
push(@rv, $arg); |
307 |
$arg = $args->Get(&kArgSeries); |
308 |
if (defined($arg)) |
309 |
{ |
310 |
push(@rv, $arg); |
311 |
$arg = $args->Get(&kArgSegment); |
312 |
if (defined($arg)) |
313 |
{ |
314 |
push(@rv, $arg); |
315 |
} |
316 |
} |
317 |
|
318 |
} |
319 |
} |
320 |
} |
321 |
} |
322 |
} |
323 |
|
324 |
return @rv; |
325 |
} |
326 |
|
327 |
sub GetOpts |
328 |
{ |
329 |
my($argsinH) = @_; |
330 |
my($rv); |
331 |
|
332 |
$rv = new drmsArgs($optsinH, 0); |
333 |
} |
334 |
|
335 |
sub Ingest |
336 |
{ |
337 |
my($series, $segment, $setdate, $dataH) = @_; |
338 |
my($cmd); |
339 |
my($json); |
340 |
my($pipe); |
341 |
my($rsp); |
342 |
my($childret); |
343 |
my($rv); |
344 |
|
345 |
$rv = 0; |
346 |
|
347 |
$json = to_json($dataH); |
348 |
# print "$json\n"; |
349 |
# return $rv; |
350 |
# exit; |
351 |
|
352 |
# Call the generic ingest module |
353 |
$cmd = "rawingest series=$series segment=$segment"; |
354 |
if (defined($setdate) && $setdate) |
355 |
{ |
356 |
$cmd = "$cmd -d"; |
357 |
} |
358 |
|
359 |
$pipe = new drmsPipeRun($cmd); |
360 |
|
361 |
if (defined($pipe)) |
362 |
{ |
363 |
$pipe->EnableAutoflush(); |
364 |
$pipe->WritePipe($json); |
365 |
$pipe->ClosePipe(1); # close write pipe |
366 |
$pipe->ReadPipe(\$rsp); |
367 |
|
368 |
if (defined($rsp) && length($rsp) > 0) |
369 |
{ |
370 |
print "rawingest returns:\n$rsp\n"; |
371 |
} |
372 |
|
373 |
# close both read and write pipes (but write was already closed) |
374 |
if ($pipe->ClosePipe()) |
375 |
{ |
376 |
print STDERR "Failure reading from pipe.\n"; |
377 |
$rv = 1; |
378 |
} |
379 |
else |
380 |
{ |
381 |
$childret = $pipe->GetStatus(); |
382 |
if ($childret != 0) |
383 |
{ |
384 |
print STDERR "Child process ran unsuccessfully, returned code $childret.\n"; |
385 |
$rv = 1; |
386 |
} |
387 |
} |
388 |
} |
389 |
else |
390 |
{ |
391 |
print STDERR "Unable to call geningest.\n"; |
392 |
$rv = 1; |
393 |
} |
394 |
|
395 |
return $rv; |
396 |
} |