#!/opt/local/bin/perl
#
#  Name:  pack_bd_poros.pl -- Pack bulk density and porosity
#
#  Purpose:
#	This script packs the ASCII file created by script
#	get_bd_poros.pl, which contains bulk density and porosity values
#	fro each standard layer of each mapunit, into two binary files
#	in the INFO format defined by AML scripts def_bd_items.aml and
#	def_poros_items.aml.  Each of the output files contains, in each
#	record, the mapunit ID followed by 4-byte real values for the BD
#	or porosity for each layer.
#
#  Usage:
#	perl pack_bd_poros.pl infile outbd outporos
#
#  Options and Arguments:
#	infile		Name of input file containing bulk density and
#			porosity values in the format produced by script
#			get_bd_poros.pl.
#	outbd		Name of output file for packed bulk density.
#	outporos	Name of output file for packed porosity.
#
#  External files accessed:
#	These files are described under agruments, above.
#
#  Internal subroutines defined:
#	help		Output help text.
#	usage		Output terse usage text.
#
#  Internal variables:
#	BD		File handle for output packed BD data.
#	IN		File handle for input fragment/texture data.
#	POROS		File handle for output packed porosity data.
#	cmd		Name of this script; used for help text.
#	fields		Array of fields from input record.
#	muid		Mapunit ID for current record.
#	opt		Name of current option.
#	poros		Array of porosity values.
#	val		Data value from input record.
#
#  Script history:
#	12-96	Initial version, R. A. White
#
############   End of Prolog for Script pack_bd_poros.pl   ############

#  Define usage and help text

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

sub usage
    {
    print "\n";
    print "Usage:  perl $cmd infile outbd outporos\n";
    print "        perl $cmd -u|h\n";
    }

sub help
    {
    print "\n";
    print $cmd, " -- pack bulk density and porosity\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 "   infile	Name of input file containing bulk density",
			" and\n",
	  "		porosity values in the format produced by",
			" script\n",
	  "		get_bd_poros.pl.\n";
    print "    outbd	Name of output file for packed bulk density.\n";
    print "  outporos	Name of output file for packed porosity.\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 < 3)
    {
    &usage;
    exit 1;
    }


#  Pack the data

open (IN, $ARGV[0]) || die "Cannot open $ARGV[0] for input";
open (BD, "> $ARGV[1]") || die "Cannot open $ARGV[1] for output";
open (POROS, "> $ARGV[2]") || die "Cannot open $ARGV[2] for output";
while (<IN>)
    {
    @fields = split;
    print BD pack ("A6", $fields[0]);
    print POROS pack ("A6", shift (@fields));
    @poros = splice (@fields, @fields / 2);
    print BD pack ("f", $val) while defined ($val = shift (@fields));
    print POROS pack ("f", $val) while defined ($val = shift (@poros));
    }
close IN;
close BD;
close POROS;

exit 0;		# From Script pack_bd_poros.pl
