#!/usr/bin/env perl

## Noise Figure and Temperature Conversion

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

sub Print {
$db = 10 * (log(1 + ($k / 290)) * 0.43429);

printf "\n\n	         dB : %.5f\n", $db;
printf "             Kelvin : %.5f\n", $k;
printf "    degrees Celsius : %.5f\n", $k - 273.16;
printf " degrees Fahrenheit : %.5f\n\n", (1.8 * $c) + 32;
}

while (!$ans || $ans > 4 || $ans =~ m/[a-z]/i) {
	print "\33[H\33[J".
	      "\tNoise Figure and Temperature Conversion\n\n".
	      "1) dB to Kelvin, Celsius, and Fahrenheit\n".
      	      "2) Kelvin to dB, Celsius, and Fahrenheit\n".
	      "3) Celsius to dB, Kelvin, and Fahrenheit\n".
	      "4) Fahrenheit to dB, Kelvin, and Celsius\n\n".
              "Enter your selection: ";
	chomp($ans = <STDIN>);
}

if ($ans == 1) {

	print "\n";

	while (!$db) {
		print "Enter noise figure (in dB): ";
		chomp($db = <STDIN>);
		$db =~ tr/0-9.//csd;
	}

	$k = 290 * (10 ** ($db / 10) - 1);
	&Print;
}

elsif ($ans == 2) {

	print "\n";

	while (!$k) {
		print "Enter noise temperature in K: ";
		chomp($k = <STDIN>);
	        $k =~ tr/0-9.//csd;
	}
	
	&Print;
}

elsif ($ans == 3) {

	print "\n";

	while (!$c) {
        	print "Enter degrees Celsius: ";
		chomp($c = <STDIN>);
                $c =~ tr/0-9.//csd;
        }

	&Print($k = $c + 273.16);
}

elsif ($ans == 4) { 

	print "\n";

	 while (!$f) {
	 	print "Enter degrees Fahrenheit: ";
		chomp($f = <STDIN>);
		$f =~ tr/0-9.//csd;
	}

	$c = (5 / 9) * ($f - 32);
	&Print($k = $c + 273.16);
}
