#! /usr/bin/env perl

# $Id: util.pm 173 2007-03-08 03:53:40Z yamada $

use warnings;
use strict;
package PS::MOPS::DET::util;

#
# Convert an RA in Decimal degrees to Hour:Minute:Seconds.SSS format.  This
# function is used to meet ICD requirements, so its output format must
# not be changed.
#

sub DDtoHMS {
	my ($dd) = @_;

	if ($dd < 0.0 || $dd > 360.0) {
		die "DD must be in [0.0, 360.0]";
	}

	#
	# Convert decimal degrees into decimal hours.
	#

	my $hour = 24.0 * $dd / 360.0;

	#
	# Now split the hour into an integer hour + decimal minutes.
	#

	my $min = ($hour - int($hour)) * 60.0;
	$hour = int($hour);

	#
	# Now split the minutes into an integer minutes + decimal seconds.
	#

	my $sec = ($min - int($min)) * 60.0;
	$min = int($min);

	return sprintf("%02d:%02d:%06.3f", $hour, $min, $sec);
}

#
# Convert a Dec in Decimal degrees to Degrees:Minute:Seconds format.  This
# function is used to meet ICD requirements, so its output format must
# not be changed.
#

sub DDtoDMS {
	my ($dd) = @_;

	if ($dd < -90.0 || $dd > 90.0) {
		die "DD must be in [-90.0, 90.0]";
	}

	#
	# Split off sign.
	#

	my $sign;
	my $degrees;

	if ($dd < 0.0) {
		$degrees = -$dd;
		$sign = '-';
	} else {
		$degrees = $dd;
		$sign = '+';
	}

	#
	# Now split the degrees into an integer degrees + decimal minutes.
	#

	my $min = ($degrees - int($degrees)) * 60.0;
	$degrees = int($degrees);

	#
	# Now split the minutes into an integer minutes + decimal seconds.
	#

	my $sec = ($min - int($min)) * 60.0;
	$min = int($min);

	return sprintf("%s%02d:%02d:%02.0f", $sign, $degrees, $min, $sec);
}

1;

__END__

=head1 NAME

PS::MOPS::DET::util.pm - utility routines

=head1 SYNOPSIS

use PS::MOPS::DET::util.pm;

=head1 DESCRIPTION

Some common utility functions

=head1 PUBLIC FUNCTIONS

=head2 DDtoHMS

Decimal degrees to hour minute seconds format.

=head2 DDtoDMS

Decimal degrees to degree minute second format.

=cut
