#!/opt/local/bin/perl
#
#  Name:  pack_ph.pl -- Pack ph data
#
#  Purpose:
#	This script packs the ASCII file created by script get_ph.pl,
#	which contains mean ph data for each standard layer of each
#	mapunit, into a binary file in the INFO format defined by AML
#	script def_perm_items.aml.  The output file contains, in each
#	record, the mapunit ID followed by 4-byte real values for the
#	mean permeability for each layer.
#
#  Usage:
#	perl pack_ph.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_ph.pl and creates output compatible with the INFO table
#	definitions created by script def_perm_items.aml.
#
#  External files accessed:
#	<asciifile>	READ to get ASCII format data.
#	<packedfile>	CREATEd to receive packed data.
#
#  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.
#	help		Help text.
#	item		Value of current table item.
#	items		Array of item values for current table record.
#	opt		Name of current option.
#	usage		Usage text.
#
#  Script history:
#	2001-04		Initial version, R. A. White.
#
############   End of Prolog for Script pack_ph.pl   ############

#  Define usage and help text

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

$usage = <<"End of Usage";

Usage:  $cmd [<options>] <asciifile> <packedfile>
        $cmd -u|h
End of Usage

$help = <<"End of Help";

$cmd -- pack ph data into INFO-table format
$usage
Options and arguments are
     -h   	Write this help text to stdout and exit.
     -u   	Display terse usage message and exit.
  asciifile	Name of input ASCII format file.
  packedfile	Name of output packed binary file.
e.g. <example>.
End of Help

#  Get options

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

#  Preprocess arguments

(@ARGV != 2) && (print $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+\.\d+\s+\d+\.\d+/)
	{
	@items = split;
	print OUT pack ("A6", shift @items);
	print OUT pack ("f", $item)
	  while defined ($item = shift @items);
	}
    }
close IN;
close OUT;

exit 0;		# From Script pack_ph.pl
