#!/usr/bin/env perl

## VHF Line of Sight Distance / Isotropic Radiation Field Strength Calculator

## This file Copyright 2000 <contact@gbppr.org> under the GPL
## NO WARRANTY. Please send bug reports / patches / reports.

sub Power {
print "\n";
while (!$pwr) {
	print "Enter antenna input power (in watts): ";
        chomp($pwr = <STDIN>);
        $pwr =~ tr/0-9.//csd;
 }        
}

sub Fs {
$ss = ((30 * $pwr) ** .5) / $dism;
$ssu = $ss * 1000;
}


sub Print {
print  "\n\n                     Transmitter power : $pwr Watts\n";
printf "     Antenna Height Above Ground Level : %.2f m (%.2f ft)\n", $heightm, $heightf;
printf " Approximate distance to radio horizon : %.2f km (%.2f mi)\n", $diskm, $dismi;
printf " Approximate field strength at horizon : %.5f mV/m (%.5f uV/m)\n\n", $ss, $ssu;
}

sub Print1 {
print  "\n\n";
if ($unit eq "feet" && $dis == 1) {
	$unit = "foot";
}

elsif ($unit eq "feet" && $dis > 1) {
	printf " Approximate field strength of %s Watts at %.2f feet:\n\n", $pwr, $dis;
	printf " millivolts per meter : %.5f mV/m\n".
	       " microvolts per meter : %.5f uV/m\n\n", $ss, $ssu;
	exit 0;
}

printf " Approximate field strength of %s Watts at %.2f %s%s:\n\n", $pwr, $dis, $unit, ($dis == 1) ? "" : "s";
printf " millivolts per meter : %.5f mV/m\n".
       " microvolts per meter : %.5f uV/m\n\n", $ss, $ssu;
exit 0;
}

while (!$ans || $ans > 2 || $ans =~ m/[a-z]/i) {
	print "\33[H\33[J".
	      "VHF Line of Sight / Isotropic Radiation Field Strength Calculator\n\n\n".
      	      "1) Calculate VHF Line of Sight Distance\n".
              "2) Calculate Isotropic Radiation Field Strength\n\n".
              "Enter your selection: ";
	chomp($ans = <STDIN>);
}

if ($ans == 1) {

	while (!m/^([0-9.]+)(ft|FT|m|M+)$/) {
		undef $_; undef $height; undef $unit;
		print "\nft = feet\n".
      	              "m  = meters\n\n".
                      "Enter antenna height above average terrain  [height][ft,m]: ";
		chomp($_ = <STDIN>);
		}

		if (m/^([0-9.]+)(ft|FT|m|M+)$/) {
       			$height = $1;
			$unit = $2;
		}              

		if ($unit =~ /ft/i) {
			$dismi = sqrt (2 * $height);
			$diskm = $dismi * 1.609344;
			$dism = $diskm * 1000;
			$heightf = $height;
			$heightm = $heightf * 0.3048;
		}

		elsif ($unit =~ /m/i) {
			$diskm = sqrt (17 * $height);
			$dism = $diskm * 1000;
			$dismi = $diskm / 1.609344;
			$heightm = $height;
			$heightf = $height / 0.3048;
		}
	
		&Power;
		&Fs;
		&Print;
}

elsif ($ans == 2) {

	while (!m/^([0-9.]+)(mi|MI|km|KM|m|M|ft|FT+)$/) {
		undef $_; undef $dis; undef $unit;
		print "\nmi = miles\n".
      		      "km = kilometers\n".
       	              "m  = meters\n".
                      "ft = feet\n\n".
                      "Enter distance from antenna  [distance][mi,km,m,ft]: ";
        	chomp($_ = <STDIN>);
		}

		if (m/^([0-9.]+)(mi|MI|km|KM|m|M|ft|FT+)$/) {
       			$dis = $1;
        		$unit = $2;
		}

		if ($unit =~ /mi/i) {
			$unit = "mile";
			$dism = $dis * 1609.344;
		}

		elsif ($unit =~ /km/i) {
			$unit = "kilometer";
			$dism = $dis * 1000;
		}

		elsif ($unit =~ /m/i) {
			$unit = "meter";
			$dism = $dis;
		}

		elsif ($unit =~ /ft/i) {
			$unit = "feet";
			$dism = $dis * 0.3048;
		}
	
		&Power;
		&Fs;
		&Print1;
}
