File: sweepsplit.in

package info (click to toggle)
gwave 20060606-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,716 kB
  • ctags: 1,136
  • sloc: ansic: 8,839; lisp: 1,665; sh: 1,202; makefile: 170; perl: 91
file content (121 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download | duplicates (4)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!@PERL@
#
# split the output of "sp2sp -s head" into multiple files, one per sweep.
#

use FileHandle;
use Getopt::Long;

sub usage {
	print STDERR "usage: sweepsplit [options] [file  [name]]
or:    sp2sp -s head | sweepsplit [options]
Options:
     -a Len         Use Len digits when constructing numered output file names
     -s N           Dump only sweep number N to stdout
     -t Type        Assume input is of type T (passed to sp2sp)
";

}


Getopt::Long::config('no_auto_abbrev',
                     'no_ignore_case','no_ignore_case_always');

$dump_sweepno = -1;
$suffixlength = 2;
$verbose = 0;
$fnbase = '';
$nsweeps = 0;

%optctl = ("s=i"               => \$dump_sweepno,
	   "a|suffixlength=i"  => \$suffixlength,
	   "t=s"               => \$infile_type,
           "v|verbose!"        => \$verbose
);

if(!GetOptions(%optctl)) {
	&usage();
        exit 1;
}

if($#ARGV >= 0) {  # input filename provided
	$infname = shift(@ARGV);
}
if($#ARGV >= 0) {  # base part of output filenames specified
	$fnbase = shift(@ARGV);
}

if(!$fnbase) { # If no output filename specified, construct one.
	if($infname) {
		$fnbase = $infname;
		$fnbase =~ s|^.*/([^/]+)$|$1|;   # remove leading directory names
		$fnbase =~ s|\.[^.]*$||;   # remove trailing .suffix
	} else {
		$fnbase = 'sp';
	}
}

$infp = new FileHandle;
if($infname) {
	if($infile_type) {
		$topt = "-t $infile_type";
	}
	$infp->open("sp2sp -s head $topt $infname|") || die "pipe from sp2sp $infname: $!";
} else {
	$infp->fdopen(STDIN, "r");
}


$heads = $infp->getline;

while($_ = $infp->getline) {

	if($_ =~ m/^\#\s*sweep\s*(\d+);/) {
		$sweepno = $1;
		open_outfile($sweepno);
		$nsweeps++;
	} else {
		if(!$fp) {
			open_outfile(0);
			$nsweeps++;
		}
		print $fp $_;
	}
}

$infp->close || "pipe from sp2sp $infname: $!";

if($fp) {
	$fp->close;
} else {
	print STDERR "no sweeps found\n";
	exit 1;
}

printf "%d sweeps\n", $nsweeps;

exit 0;
#############################################################################

sub open_outfile
{
	my($sweepno) = @_;

	if($fp) {
		$fp->close;
	}
	if($dump_sweepno == -1 || $dump_sweepno == $sweepno) {
		$fname = sprintf "%s%0*d.asc", $fnbase, $suffixlength, $sweepno;
	} else {
		$fname = '/dev/null';
	}
	$fp = new FileHandle $fname,"w";
	if(!defined($fp)) {
		die "$fname: $!";
	}
	print $fp $heads;
	
	# would like to include sweep number as comment - but gwave
	# can't deal with this.  to be fixed.
	# print $fp $_;
}