1package main;
2
3use 5.006002;
4
5use strict;
6use warnings;
7
8use Astro::Coord::ECI::TLE;
9use Test::More 0.88;	# Because of done_testing();
10
11my @rslt;
12
13note <<'EOD';
14This test is of the proper population of the magnitude table only.
15Actual computation tests are done elsewhere.
16EOD
17
18succeeds( clear => 'Clearing the magnitude table' );
19
20succeeds( show => 'Retrieving the empty magnitude table' );
21
22is_deeply \@rslt, [], 'There are no magnitudes in the table';
23
24succeeds( magnitude => { 25544 => -1.0 },
25    'Setting the magnitude table directly' );
26
27succeeds( show => 'Retrieving the magnitude table' );
28
29is_deeply \@rslt, [ 25544 => -1.0 ],
30    'Contents of magnitude table are as expected';
31
32succeeds( show => 5,
33    'Retrieving a magnitude which is not in the table' );
34
35is_deeply \@rslt, [], 'The retrieval of 00005 returned nothing';
36
37succeeds( add => 00005 => 11, 'Adding OID 5' );
38
39succeeds( show => 5, 'Retrieving an added magnitude' );
40
41is_deeply \@rslt, [ '00005' => 11 ],
42    'Got back correct magnitude for OID 5';
43
44succeeds( show => 25544, 'Retrieving originally-loaded magnitude' );
45
46is_deeply \@rslt, [ 25544, -1.0 ],
47    'Got back correct originally-loaded magnitude';
48
49succeeds( show => 'Retrieving all loaded magnitudes' );
50
51is_deeply { @rslt }, {
52    '00005'	=> 11,
53    '25544'	=> -1,
54}, 'The contents of the magnitude table are as expected';
55
56succeeds( drop => 5, 'Dropping OID 5' );
57
58succeeds( show => 'Retrieving modified magnitudes' );
59
60is_deeply \@rslt, [ 25544 => -1 ],
61    'Dropped body is gone from table';
62
63succeeds( molczan => 't/mcnames.mag',
64    'Loading a Molczan-format magnitude file' );
65
66succeeds( show => 'Retrieving Molczan-format magnitudes' );
67
68my $want = {
69    '20580'	=> 3.0,
70    '25544'	=> -0.5,
71    '37820'	=> 4.0,
72};
73
74is_deeply { @rslt }, $want, 'Got the expected data from the load';
75
76if ( open my $fh, '<', 't/mcnames.mag' ) {
77    clear(  );
78    succeeds( molczan => $fh, 'Loading a Molczan-format file handle' );
79    close $fh;
80    succeeds( show => 'Retrieve data loaded from file handle' );
81    is_deeply { @rslt }, $want,
82	'Got the expected data from the handle';
83} else {
84    note <<"EOD";
85Skipping load of Molczan-format data from file handle. Failed to open
86t/mcnames.mag: $!
87EOD
88}
89
90clear(  );
91
92succeeds( quicksat => 't/quicksat.mag',
93    'Loading a Quicksat-format magnitude file' );
94
95succeeds( show => 'Retrieving Quicksat-format magnitudes' );
96
97$want = {
98    '20580'	=> 2.2,
99    '25544'	=> -1.3,
100    '37820'	=> 3.2,
101};
102
103is_deeply { @rslt }, $want, 'Got the expected data from the load';
104
105if ( open my $fh, '<', 't/quicksat.mag' ) {
106    clear(  );
107    succeeds( quicksat => $fh, 'Loading a Quicksat-format file handle' );
108    close $fh;
109    succeeds( show => 'Retrieve data loaded from file handle' );
110    is_deeply { @rslt }, $want,
111	'Expected data is 0.7 mag dimmer than file contents';
112} else {
113    note <<"EOD";
114Skipping load of Quicksat-format data from file handle. Failed to open
115t/quicksat.mag: $!
116EOD
117}
118
119succeeds( adjust => 'Getting the magnitude adjustment' );
120
121cmp_ok $rslt[0], '==', 0, 'The default magnitude adjustment is zero';
122
123# Except for the OID (which was changed from 88888) the following OID is
124# test data distributed with Space Track Report Number 3, obtained from
125# the Celestrak web site.
126
127my $tle;
128ok eval {
129    ( $tle ) = Astro::Coord::ECI::TLE->parse( <<'EOD' );
1301 25544U          80275.98708465  .00073094  13844-3  66816-4 0    8
1312 25544  72.8435 115.9689 0086731  52.6988 110.5714 16.05824518  105
132EOD
133    1;
134}, 'Parse test TLE';
135
136cmp_ok $tle->get( 'intrinsic_magnitude' ), '==', -1.3,
137    'The intrinsic magnitude got set to -1.3';
138
139succeeds( adjust => 0.7, 'Setting the magnitude adjustment to 0.7' );
140
141succeeds( adjust => 'Retrieving the new magnitude adjustment' );
142
143cmp_ok $rslt[0], '==', 0.7, 'The magnitude adjustment is now 0.7';
144
145succeeds( show => 25544, 'Retrieving the magnitude table entry for our OID' );
146
147cmp_ok { @rslt }->{'25544'}, '==', -1.3,
148    'Magnitude table still has -1.3';
149
150ok eval {
151    ( $tle ) = Astro::Coord::ECI::TLE->parse( <<'EOD' );
1521 25544U          80275.98708465  .00073094  13844-3  66816-4 0    8
1532 25544  72.8435 115.9689 0086731  52.6988 110.5714 16.05824518  105
154EOD
155    1;
156}, 'Re-parse the test TLE';
157
158cmp_ok 0 + sprintf( '%.1f', $tle->get( 'intrinsic_magnitude' ) ), '==', -0.6,
159    'The intrinsic magnitude got set to -0.6';
160
161
162done_testing;
163
164sub clear {
165    my ( $name ) = @_;
166    defined $name
167	or $name = 'Clear magnitude table';
168    eval {
169	Astro::Coord::ECI::TLE->magnitude_table( 'clear' );
170	@rslt = Astro::Coord::ECI::TLE->magnitude_table( 'show' );
171	@rslt
172	    and die "Magnitude table still occupied\n";
173	1;
174    } or do {
175	@_ = "$name failed: $@";
176	goto &fail;
177    };
178    @_ = ( "$name succeeded" );
179    goto \&pass;
180}
181
182sub succeeds {
183    my ( @args ) = @_;
184    my $title = pop @args;
185    eval {
186	@rslt = Astro::Coord::ECI::TLE->magnitude_table( @args );
187	1;
188    } or do {
189	@_ = ( "$title failed: $@" );
190	goto &fail;
191    };
192    @_ = ( "$title succeeded" );
193    goto &pass;
194}
195
1961;
197
198# ex: set filetype=perl textwidth=72 :
199