#!/opt/local/bin/perl
#
#  Name:  pack_awc.pl -- Pack AWC values
#
#  Purpose:
#	This script packs available water capacity (AWC) values from the
#	ASCII format output by Perl script get_awc.pl to the binary
#	format defined by the aml script def_awc_items.aml.
#
#  Usage:
#	perl pack_awc.pl asciifile binfile
#
#  Options and Arguments:
#	asciifile	ASCII file created by get_awc.pl.
#	binfile		Binary file to receive packed data.
#
#  External files accessed:
#	These files are described under agruments, above.
#
#  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 binary file.
#	cmd		Name of this script; used for help text.
#	fields		Array of fields from input record.
#	opt		Name of current option.
#	val		Data value from input record.
#
#  Script history:
#	12-96	Initial Version, R. A. White.
#
############   End of Prolog for Script pack_awc.pl   ############

#  Define usage and help text

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

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

sub help
    {
    print "\n";
    print $cmd, " -- pack AWC values\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	ASCII file created by get_awc.pl.\n";
    print "   binfile	Binary file to receive packed data.\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 < 2)
    {
    &usage;
    exit 1;
    }

#  Pack the data

open (IN, "$ARGV[0]") || die "Cannot open $ARGV[0] for input";
open (OUT, "> $ARGV[1]") || die "Cannot open $ARGV[1] for output";
while (<IN>)
    {
    @fields = split;
    print OUT pack ("A6", shift (@fields));
    print OUT pack ("s", $val) while defined ($val = shift (@fields));
    }
close IN;
close OUT;

exit 0;		# From Script pack_awc.pl
