#!/opt/local/bin/perl
#
#  Name:  pack_kfact.pl -- Pack soil erodibility factors
#
#  Purpose:
#	This script packs the ASCII file created by script get_kfact.pl,
#	which contains the soil erodibility factors KFACT (including
#	rock fragments) and KFFACT (excluding rock fragments) for the
#	top layer of each mapunit, into a binary file in the INFO format
#	defined by AML script def_kfact_items.aml.  The output file
#	contains, in each record, the mapunit ID followed by 4-byte
#	real values for KFACT and KFFACT.
#
#  Usage:
#	perl pack_kfact.pl infile outfile
#
#  Options and Arguments:
#	infile		Name of input file containing soil erodibility
#			factors in the format produced by script
#			get_kfact.pl.
#	outfile		Name of output file for packed erodibility.
#
#  External files accessed:
#	These files are described under agruments, above.
#
#  Internal variables:
#	IN		File handle for input fragment/texture data.
#	OUT		File handle for output packed porosity data.
#	cmd		Name of this script; used for help text.
#	fields		Array of fields from input record.
#	help		Help text.
#	muid		Mapunit ID for current record.
#	opt		Name of current option.
#	usage		Usage text.
#	val		Data value from input record.
#
#  Script history:
#	1998-05		Initial version, R. A. White
#
############   End of Prolog for Script pack_kfact.pl   ############
#	help		Help text.
#	opt		Name of current option.
#	usage		Usage text.
#
############   End of Prolog for Script <script-name>   ############

#  Define usage and help text

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

$usage = <<"End of Usage";

Usage:  $cmd infile outfile
        $cmd -u|h
End of Usage

$help = <<"End of Help";

$cmd -- pack soil erodibility factors
$usage
Options and arguments are
     -h   	Write this help text to stdout and exit.
     -u   	Display terse usage message and exit.
    infile	Name of input file containing soil erodibility factors
		in the format produced by script get_kfact.pl.
   outfile	Name of output file for packed erodibility.
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 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>)
    {
    print OUT pack ("A6 f f", split);
    }
close IN;
close OUT;

exit 0;		# From Script pack_kfact.pl
