#!/usr/bin/env perl # -*- perl -*- # Concatenate CRaTER data files and gzip the result # Version: $Id: c_zip,v 1.4 2008/10/02 17:40:28 goeke Exp goeke $ ######################################################################## # initial constants ######################################################################## ######################################################################## # Main program ######################################################################## &Setup; &Zippy; exit 0; ######################################################################## # Subroutines follow ######################################################################## ######################################################################## # Give Help ######################################################################## sub Help { print "Usage: $0 [-h] [-v] [file1] [file2] ... [filen]\n"; print " flags: -h gets full help message -v makes the output verbose (to STDERR) Concatenates all CRaTER raw data files with a common base name and compresses the result in gzip format. If only one name given, include files with -1.bin, etc. extensions. " if ($Verbose); } ######################################################################## # Do the setup ######################################################################## sub Setup { my ($file,$foo); my $kk = 0; while ($foo = shift(@ARGV)) { if ($foo =~ /^-[hH]/) { $Verbose++; &Help; exit 0; } if ($foo =~ /^-[vV]/) { $Verbose++; next; } if ($foo =~ /^-/) { print "Bag flag: $foo\n"; &Help; exit 1; } if ($foo =~ /(\.gz)|(\.zip)$/) { print "Failed to open for input: $foo\n only binary input files are allowed\n"; exit 1; } else { $Input_list[$kk++] = $foo; } } if (!$kk) { print "At least one file name needs to be supplied\n"; &Help; exit 1; } my $zipname = $Input_list[0]; # First file becomes default $zipname =~ s/^([^.]+).*/$1.bin.gz/; # use basename plus .bin if ($Verbose) { print "Using $Input_list[0] as basename\n" if ($kk<2); } else { print "Using $Input_list[0] as first input file\n"; } print "Using $zipname to record output\n" if ($Verbose); open(ZIP,"|gzip >$zipname") || die "Could not open gzip pipe"; } ######################################################################## # Do the setup # If we were only provided one file name, it must (?) be a set of # .bin, -1.bin, -2.bin files # If multiple file names given, simply suck them in ######################################################################## sub Zippy { my ($buffer,$ii,$name); $name = $Input_list[0]; $name = $Input_list[0] . ".bin" if (!$#Input_list); open (FIRST,"$name") || die "Could not open $name to read"; print "Reading $name\n" if ($Verbose); ### NB that this file has the header which we pass through while (read(FIRST,$buffer,2**14)) { print ZIP $buffer; } close FIRST; for ($ii=1; ;$ii++) { if ($#Input_list) { $name = defined($Input_list[$ii]) ? $Input_list[$ii] : "NO_SUCH_FILE.$$"; } else { $name = $Input_list[0] . "-$ii" . '.bin'; } last if (! -r $name); open (NEXT,"$name") || die "Could not open $name to read"; print STDERR "Reading $name\n" if ($Verbose); defined(sysread(NEXT,$buffer,64)) || die $!; # skip next header while (read(NEXT,$buffer,2**14)) { print ZIP $buffer; } close NEXT; } } ######################################################################## # Pod follows ######################################################################## =for html