#! /usr/bin/perl
####################################
#This program, given grid parameters and filenames via user input,
#will parse a 3-column tab-delimited file, with the columns
#being north, east and hectares, and output the results in a similar
#3-column format. No headers in original file, please, and I'm not sure
#what'll happen if it's fed a file not of this strict format, so don't do it.
#####################################
print "Name or path of input file: ";
$infile=<STDIN>;
chop $infile;
print "Name or path of output file: ";
$outfile=<STDIN>;
chop $outfile;
$delim="\t";
print "\nSelect an area for which to calculate occupation density:\n";
print "\t1: Taraco Peninsula\n";
print "\t2: Lower Tiwanaku Valley\n";
print "\t3: Middle Tiwanaku Valley\n";
print "\t4: Taraco Peninsula and Lower Tiwanaku Valley\n";
print "\t5: Entire Tiwanaku Valley\n";
print "\t6: Taraco Peninsula and Entire Tiwanaku Valley\n";
print "Selection: ";
$area=<STDIN>;
chop $area;
if ($area==1) {
$emin=505000;
$nmin=8179000;
$emax=526500;
$nmax=8190500;
} elsif ($area==2) {
$emin=514500;
$nmin=8162000;
$emax=528500;
$nmax=8180500;
} elsif ($area==3) {
$emin=527500;
$nmin=8160500;
$emax=540000;
$nmax=8177500;
} elsif ($area==4) {
$emin=505000;
$nmin=8162000;
$emax=528500;
$nmax=8190500;
} elsif ($area==5) {
$emin=514500;
$nmin=8160500;
$emax=540000;
$nmax=8180500;
} elsif ($area==6) {
$emin=505000;
$nmin=8160500;
$emax=540000;
$nmax=8190500;
} else {
&warn_and_exit ("Area not defined.\n");
}
print "\nGrid dimensions (meters): ";
$grid=<STDIN>;
chop $grid;
print "\nWorking...\n\n";
open (INFILE, "< $infile") || &warn_and_exit ("File $infile could not be opened. Aborted.");
#First initialize the HoH data structure to zero for all grid points...
$e_extent=($emax-$emin)/$grid;
$n_extent=($nmax-$nmin)/$grid;
for $i (0..$n_extent) {
for $j (0..$e_extent) {
$DATA{$i * $grid + $nmin}{$j * $grid + $emin}=0;
}
}
#Then read in $infile and augument the HoH appropriately...
while ($line = <INFILE>) {
chop $line;
@data = split /$delim/, $line;
$data[2] = s/,/\./;
$rem=mod($data[0],$grid);
if ($rem > $grid/2) {
$data[0]=$data[0]-$rem+$grid;
} else {
$data[0]=$data[0]-$rem;
}
$rem=mod($data[1],$grid);
if ($rem > $grid/2) {
$data[1]=$data[1]-$rem+$grid;
} else {
$data[1]=$data[1]-$rem;
}
if (($data[0] < $nmin)||($data[0] > $nmax)||($data[1] < $emin)||($data[1] > $emax)) {
&warn_and_exit ("Data out of grid boundaries. Aborted.\nNorth:
$data[0]\tEast: $data[1]\n");
}
$temp=$DATA{$data[0]}{$data[1]};
$temp=$temp+$data[2];
$DATA{$data[0]}{$data[1]}=$temp;
}
close INFILE;
# Now all we have to do is print the output file...
open (OUTFILE, "> $outfile") || &warn_and_exit ("File $outfile could not be
opened. Aborted.");
for $i (0..$n_extent) {
for $j (0..$e_extent) {
$north=$i * $grid + $nmin;
$east=$j * $grid + $emin;
$DATA{$north}{$east} = s/\./,/;
print OUTFILE "$north\t$east\t$DATA{$north}{$east}\n";
}
}
close OUTFILE;
&warn_and_exit ("Okay. All done.\n");
#########################################
#Passed a number and a divisor, gives the remainder. Just don't pass it a
#zero divisor...
#########################################
sub mod {
my $num = $_[0];
my $div = $_[1];
my $times = int $num/$div;
return $num-$times*$div;
}
sub warn_and_exit {
print "$_[0]\n";
print "Press Enter to Exit\n";
$trash=<STDIN>;
exit (0);
}