Astro-Coords

 view release on metacpan or  search on metacpan

t/basic.t  view on Meta::CPAN

#!perl
use strict;
use Test::More tests => 105;
use Test::Number::Delta within => 1e-9;
use Scalar::Util qw/ looks_like_number /;

require_ok('Astro::Coords');
require_ok('Astro::Telescope');
use Time::Piece ':override';

# Force stringification to use colons
Astro::Coords::Angle->DELIM(':');
Astro::Coords::Angle::Hour->DELIM(':');
Astro::Coords::Angle::Hour->NDP(2);

# Simultaneously test negative zero dec and B1950 to J2000 conversion
my $c = new Astro::Coords( ra => "15:22:33.3",
                           dec => "-0:13:4.5",
                           type => "B1950");

ok($c, "create object");

is($c->native, 'radec1950', 'check native type');
my ($r1950, $d1950) = $c->radec1950;
is( $r1950->string, "15:22:33.30", "compare B1950 RA");
is( $d1950->string, "-00:13:04.50", "compare B1950 dec");

($r1950, $d1950) = $c->radec1950();
is( $r1950->string, "15:22:33.30", "compare B1950 RA");
is( $d1950->string, "-00:13:04.50", "compare B1950 dec");

print "#J2000: $c\n";
# Compare with J2000 values
is( $c->ra(format=>'s'), "15:25:07.35","compare J2000 RA");
is( $c->dec(format=>'s'), "-00:23:35.76","compare J2000 Dec");

# Calculate distance
my $c2 = new Astro::Coords(ra => "15:22:33.3",
                           dec => "-0:14:4.5",
                           type => "B1950");

delta_within(scalar($c->distance($c2))*&Astro::PAL::DR2AS, '60.0',
  1.0e-4, "calculate distance");

# Set telescope
my $tel = new Astro::Telescope('JCMT');
my $ukirt = new Astro::Telescope('UKIRT');

# Date/Time
# Something we know
# Approx Fri Sep 14 02:57 2001
my $date = gmtime(1000436215);

# Configure the object
$c->telescope( $tel );
$c->datetime( $date);

# Test Az/El
is( int($c->el(format=>"d")), 67.0, "compare El");
is( int($c->az(format=>"d")), 208.0, "compare Az" );

# Get the summary
my @result = ("RADEC",4.03660853577072,-0.00686380910209873,undef,
              undef,undef,undef,undef,undef,undef,undef);
my @summary = $c->array;

t/basic.t  view on Meta::CPAN

is(sprintf("%.1f",$c->el(format => 'd')), '22.1',"Hale-Bopp elevation");

# Limit resolution for comparison
my ($ra_bopp, $dec_bopp) = $c->radec();
$ra_bopp->str_ndp( 0 );
$dec_bopp->str_ndp( 1 );
is($ra_bopp->string,"08:09:08","Hale-Bopp RA (J2000)");
is($dec_bopp->string,"-47:25:27.5","Hale-Bopp Dec (J2000)");

my $s = $c->status;
my @s = split /\n/,$s;
print join("\n", map { "# $_" } @s),"\n";

# and create a new elements object from the HaleBopp version
my $newbopp = new Astro::Coords( elements => [ $c->array ] );
if ($newbopp) {
  $newbopp->datetime( $c->datetime );
  $newbopp->telescope( $c->telescope );
  is( $c->az, $newbopp->az,
      "compare Azimuth from two (supposedly) identical elements");
} else {
  ok(0, "Failed to create clone of elements object");
}

# Test parsing of date strings in for elements EPOCH.
$c = new Astro::Coords(
    elements => {EPOCH => '2013 Dec 04.0',
                 ORBINC => 62.40397752235779 * &Astro::PAL::DD2R,
                 ANODE => 295.65203155196 * &Astro::PAL::DD2R,
                 PERIH => 345.5312406205832 * &Astro::PAL::DD2R,
                 AORQ => 0.01245259242960607,
                 E => 1.000201003833968,
                 EPOCHPERIH => '2013 Nov 28.7786582736',
                },
    name => 'ISON');
ok($c, 'Instantiate element object for ISON');
my %c_elements = $c->elements();
delta_ok($c_elements{'EPOCH'}, 56630.0,
         'Compare parsed elements EPOCH');
delta_ok($c_elements{'EPOCHPERIH'}, 56624.7786582736,
         'Compare parsed elements EPOCHPERIH');

# Make sure we can get a list of planet names
my @planets = Astro::Coords::Planet->planets();
my %pl = map { lc($_) => undef } @planets;
ok( exists $pl{jupiter}, "Jupiter in list of planets" );
ok( exists $pl{sun}, "Sun in list of planets" );
ok( exists $pl{moon}, "Moon in list of planets" );
is( scalar @planets, 9, "Count the number of planets" );

exit;

sub test_array_elem {
  my $ansref  = shift;  # The answer you got
  my $testref = shift;  # The answer you should have got

  # Compare sizes
  is($#$ansref, $#$testref, "Compare number of elements in array");

  for my $i (0..$#$testref) {
    if (defined $ansref->[$i] && looks_like_number($ansref->[$i])) {
      delta_ok($ansref->[$i], $testref->[$i], "Compare array element $i");
    } else {
      is($ansref->[$i], $testref->[$i], "Compare array element $i");
    }
  }

}

sub _gmstrptime {
  # parse ISO date as UT
  my $input = shift;
  my $isoformat = "%Y-%m-%dT%T";
  my $time = Time::Piece->strptime($input, $isoformat);

  # At some point Time::Piece started assuming UT from strptime
  # rather than localtime! Only add on the offset if we have a local
  # time - look inside!
  if ($time->[Time::Piece::c_islocal]) {
    my $tzoffset = $time->tzoffset;
    $time = gmtime($time->epoch() + $tzoffset->seconds);
  }
  return $time;
}



( run in 0.591 second using v1.01-cache-2.11-cpan-39bf76dae61 )