#!/perl
# dms2ll.pl - Oct 2005 - geoff mclane
# AIM: To accept an input DEGREES MINUTES (SECONDS DECIMAL) [NSEW]
# and convert to a single decimal degrees ...
# 20061206 - Later added conversion back to DEG MIN SECS as a check ...

print "Hello, World ... Arguments are ...\n";
my $cnt = 0;
my $arg = '';
my $arg2 = '';
my $args = '';
my $degs = 0;
my $deg1 = 0;
my $quad = '';
foreach $arg (@ARGV) {
	if ($cnt) {
		$args .= ' ';
	}
	$args .= $arg;
	$cnt++;
	if ($cnt == 1) {
		$deg1 = $arg;
		$degs += $arg;
		print "Arg 1 Degrees = $arg ($deg1) = $degs\n";
	} elsif ($cnt == 2) {
		$arg2 = $arg;
		$arg2 =~ s/\'//;
		$degs += ($arg2 / 60);
		$deg1 = ($arg2 / 60);
		print "Arg 2 Minutes = $arg ($deg1) = $degs\n";
	} elsif ($cnt == 3) {
		$arg2 = $arg;
		$arg2 =~ s/\"//;
		$degs += ($arg2 / (60*60));
		$deg1 = ($arg2 / (60*60));
		print "Arg 3 Seconds = $arg ($deg1) = $degs\n";
	} else {
		$deg1 = 0;
		print "Arg 4+ = $arg ($deg1) = $degs\n";
	}
}

print "Found $cnt arguments ... [$args]\n";

if ($args =~ /(N|E)/i) {
	# nothing
	$quad = $1;
	print "Quadrant: $quad ...\n";
} elsif ($args =~ /(S|W)/i) {
	$degs *= -1;
	$quad = $1;
	print "Quadrant: $quad ...(negative)\n";
} else {
	print "No quadrant NSEW found ... assumed positive ...\n";
}
print "Degrees = $degs\n";
# now to convert is back
$quad = 'N|E';
if ($degs < 0) {
	$quad = 'S|W';
	$degs *= -1;
}
my $idegs = int($degs);
my $rmins = ($degs - $idegs) * 60;
my $imins = int($rmins);
my $rsecs = ($rmins - $imins) * 60;
print "dms = [$idegs $imins $rsecs $quad] ... from [$args]\n";
exit(0);

# eof = dms2ll.pl
