#!/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 CRaTER -- c_zip =head2 NAME B -- Generate a zipped archive of packet files =head2 USAGE c_zip [-h] [-v] [file] [file2] ... [filen] =head2 FLAGS -h gets full help message -v makes the output verbose (to STDERR) =head2 DESCRIPTION If a single file name is given, that is taken to represent the basename of a contiguous set of archive files labeled archive-xxyyaa.bin archive-xxyyaa-1.bin archive-xxyyaa-2.bin Alternatively, if more than one input file is given as argument (such as in a wildcard, viz: CRAT*) the files are simply accepted as given. In either event, the files are concatenated into a single, compressed datafile using the gzip format. The file name will be based upon the basename of the first file in the argument list. The LRO-standard header for the resultant file will be copied from the first file read; all other headers will be deleted. The original files B be deleted. =head2 ENVIRONMENT perl5.002 Minimum version of Perl interpreter required =head2 BUGS The output file name of "name.bin.gz", when decompressed to "name.bin" will, in some cases, be the same as the first of the input files, yet contain much more data. Unlike some other programs in this series, only non-compressed files are valid inputs. This is considered a feature to keep from concatenating previously compressed archives. =head2 SEE ALSO rtlm =head2 AUTHOR Bob Goeke =head2 RCS Information $Id: c_zip,v 1.4 2008/10/02 17:40:28 goeke Exp goeke $ =cut ######################################################################## # history follows ######################################################################## # $Log: c_zip,v $ # Revision 1.4 2008/10/02 17:40:28 goeke # Add check to avoid compressing *.gz files # Changed output file to be .bin.gz # # Revision 1.3 2008/09/29 18:26:06 goeke # Accept wildcard lists of input files. # # Revision 1.2 2008/06/30 14:23:58 goeke # Fix typos in documentation # # Revision 1.1 2007/09/29 17:55:17 goeke # Initial revision # # Revision 2.1 2007/05/04 13:47:26 goeke # Name change and associated POD updates # # Revision 1.4 2007/04/26 19:54:10 goeke # Enable reading data on STDIN # # Revision 1.3 2007/04/26 15:43:56 goeke # Insert header line with s/n and date from rtlm stream # # Revision 1.2 2007/04/24 17:44:16 goeke # update POD # # Revision 1.1 2007/04/19 17:41:27 goeke # Initial revision #