#!/opt/local/bin/perl
#
#  Name:  pack_rockvol.pl -- pack rock volume table for INFO
#
#  Purpose:
#	This script packs the ASCII table of percent rock by volume for
#	each standard soil layer, output by script get_rockvol.pl, into
#	the binary format defined by def_rockvol_items.aml, so that they
#	can then be copied into an INFO table using the INFO GET
#	command.
#
#  Usage:
#	perl pack_rockvol.pl <asciifile> <packedfile>
#
#  Options and Arguments:
#	<asciifile>	Name of input ASCII format file.
#	<packedfile>	Name of output packed binary file.
#
#  External associations:
#	This script is intended for use with output from script
#	get_rockvol.pl and creates output compatible with the INFO
#	table definitions created by script def_rockvol_items.aml.
#
#  External files accessed:
#	<asciifile>	READ to get ASCII format data.
#	<packedfile>	CREATEd to receive packed data.
#
#  Internal subroutines defined:
#	help	Output help text.
#	usage	Output terse usage text.
#
#  Internal variables:
#	IN		File handle for input ASCII file.
#	OUT		File handle for output packed file.
#	cmd		Name of this script; used for help text.
#	item		Value of current table item.
#	items		Array of item values for current table record.
#	opt		Name of current option.
#
#  Script history:
#	10-95	Initial version, R. A. White
#
############   End of Prolog for Script pack_rockvol.pl   ############

#  Define usage and help text

@cmd =  (split ('/', $0));
$cmd = pop (@cmd);

sub usage
    {
    print "\n";
    print "Usage:  perl $cmd <asciifile> <packedfile>\n";
    print "        perl $cmd -u|h\n";
    }

sub help
    {
    print "\n";
    print "$cmd -- pack ASCII rock volume table to INFO binary",
      " format\n"; 
    &usage;
    print "\n";
    print "Options and arguments are\n";
    print "     -h   	Write this help text to stdout and exit.\n";
    print "     -u   	Display terse usage message and exit.\n";
    print "  asciifile	Name of input ASCII format file.\n",
	  " packedfile	Name of output packed binary file.\n";
    }

#  Get options

while ($ARGV[0] =~ /-(\w+)(\s+|$)/)
    {
    push (@opt, $1);
    shift;
    }	
if ($opt[0] eq 'u')
    {
    &usage;
    exit;
    }
if ($opt[0] eq 'h')
    {
    &help;
    exit;
    }

#  Preprocess arguments

if  ($#ARGV < 1)
    {
    &usage;
    exit 1;
    }

#  Pack input one record at a time into output

open (IN, $ARGV[0]) || die "Cannot open input file $ARGV[0]";
open (OUT, ">$ARGV[1]") || die "Cannot open output file $ARGV[1]";
while (<IN>)
    {
    if (/\S+\s+\d+\s+\d+/)
	{
	@items = split;
	print OUT pack ("A6", shift @items);
	print OUT pack ("s", $item)
	  while defined ($item = shift @items);
	}
    }
close IN;
close OUT;

exit 0;		# From Script pack_rockvol.pl
