annotate tools/rdock/bin/sdsplit @ 2:0faa03a92843 draft default tip

Uploaded
author dzesikah
date Fri, 26 Aug 2016 10:19:49 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
1 #!/usr/bin/perl
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
2 # Splits SD records into multiple files of equal no. of records
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
3 use lib "$ENV{'RBT_ROOT'}/lib";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
4
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
5 use FileHandle;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
6 use SDRecord;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
7
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
8 # Record size to split into
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
9 my $recSize = 1000;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
10
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
11 #output root
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
12 my $outRoot = "tmp";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
13
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
14 #Print help if no command line arguments
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
15 printHelpAndExit() if (scalar(@ARGV) == 0);
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
16
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
17 #Parse command line arguments
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
18 my @files;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
19 while (scalar(@ARGV)) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
20 my $arg = shift @ARGV;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
21 printHelpAndExit() if ($arg eq '-h');
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
22 if (index($arg,'-o')==0) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
23 $outRoot = substr($arg,2);
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
24 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
25 elsif (index($arg,'-')==0) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
26 $recSize = substr($arg,1);
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
27 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
28 else {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
29 push @files,$arg;#must be a filename
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
30 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
31 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
32 push @ARGV,@files;#put the filenames back in the arg list
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
33
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
34 my $sdRec = new SDRecord;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
35 my $nRec=0;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
36 my $nFile=0;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
37 my $sdfh;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
38
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
39 #read records
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
40 while ($sdRec->readRec('LINES'=>1)) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
41 #check if we need to start a new output file
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
42 if ($nRec % $recSize == 0) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
43 $nFile++;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
44 my $outFile = $outRoot . $nFile . ".sd";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
45 if (defined $sdfh) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
46 undef $sdfh;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
47 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
48 $sdfh = new FileHandle ">$outFile";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
49 if (!defined $sdfh) {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
50 die "Can't open $outFile";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
51 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
52 else {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
53 print STDOUT "Opening $outFile\n";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
54 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
55 $sdfh->autoflush(1);
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
56 select($sdfh);
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
57 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
58 $nRec++;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
59 $sdRec->writeRec();
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
60 }
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
61 undef $sdfh;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
62 select(STDOUT);#reselect STDOUT as default
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
63
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
64 #######################################################################
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
65 sub printHelpAndExit {
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
66 print "\Splits SD records into multiple files of equal size\n";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
67 print "\nUsage:\tsdsplit [-<RecSize>] [-o<OutputRoot>] [sdFiles]\n\n";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
68 print "\t-<RecSize>\trecord size to split into (default = 1000 records)\n";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
69 print "\t-o<OutputRoot>\tRoot name for output files (default = tmp)\n";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
70 print "\n\tIf SD file list not given, reads from standard input\n";
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
71 exit;
0faa03a92843 Uploaded
dzesikah
parents:
diff changeset
72 }