~ubuntu-branches/debian/squeeze/stellarium/squeeze

« back to all changes in this revision

Viewing changes to util/generate_asteroid_ssystem.pl

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
#
 
3
# See POD docs at end of this file for genereal info
 
4
 
 
5
use strict;
 
6
use warnings;
 
7
use File::Basename;
 
8
use Getopt::Long;
 
9
 
 
10
 
 
11
use constant PROG_DESCRIPTION => "asteroid orbital elements converter";
 
12
use constant PROG_COPYRIGHT   => "(C) 2007; released under the GNU GPL version 2";
 
13
use constant PROG_VERSION     => "0.02";
 
14
use constant PROG_AUTHOR      => "Matthew Gates";
 
15
 
 
16
my $this_script         = basename($0);
 
17
 
 
18
# flags & param vars
 
19
my $gs_flg_all          = 0;
 
20
my $gs_flg_data_url     = "http://hamilton.dm.unipi.it/astdys/catalogs/allnum.cat";
 
21
my $gs_flg_name_url     = "http://cfa-www.harvard.edu/iau/lists/MPNames.html";
 
22
my $gs_flg_id_list      = "1-10";
 
23
my $gs_flg_data_age     = 3600 * 24 * 30; # only fetch data if current files older than 1 month
 
24
 
 
25
# process command line options.  Praise Bob, I love Getopt::Long!
 
26
GetOptions(
 
27
                'all'           => \$gs_flg_all,
 
28
                'data-url=s'    => \$gs_flg_data_url,
 
29
                'force-refresh' => sub { $gs_flg_data_age = 0; },
 
30
                'help'          => sub { usage(0,1) },
 
31
                'ids=s'         => \$gs_flg_id_list,
 
32
                'name-url=s'    => \$gs_flg_name_url,
 
33
                'version'       => sub { version_message(); exit 0; },
 
34
           )     or usage(1,0);
 
35
 
 
36
# some globals.  yeah yeah, so sue me.
 
37
my @ga_field_order = (qw(Epoch SemiMajorAxis Eccentricity Inclination AscendingNode ArgOfPericenter MeanAnomaly mag albedo));
 
38
my %gh_names;           # key is asteroid ID
 
39
my %gh_data;            # key is asteroid ID
 
40
my %gh_selection;       # list of IDs which we want
 
41
my $gs_selection_max = 0;
 
42
my $gs_data_filename = "../builds/asteroid_orbits.dat";
 
43
my $gs_name_filename = "../builds/asteroid_names.dat";
 
44
my $gs_unknown_name_counter = 0;
 
45
 
 
46
if ( ! $gs_flg_all ) {
 
47
        foreach my $i (split(",", $gs_flg_id_list)) {
 
48
                # handle ranges if necessary
 
49
                my ($l, $u); # lower and upper range values
 
50
                if ( $i =~ /^\d+$/ ) { $l = $i; $u = $i; } 
 
51
                elsif ( $i =~ /^(\d+)-(\d+)$/ ) { $l = $1; $u = $2; }
 
52
                else { next; }
 
53
                
 
54
                for(my $n=$l; $n<=$u; $n++) {
 
55
                        $gh_selection{$n} = 1;
 
56
                        if ( $n > $gs_selection_max ) { $gs_selection_max = $n; }
 
57
                }
 
58
        }
 
59
}
 
60
 
 
61
download_data_files();
 
62
load_data();
 
63
load_names();
 
64
dump_data();
 
65
 
 
66
###############################################################################
 
67
# Subroutines
 
68
###############################################################################
 
69
sub usage {
 
70
        my $level = shift || 0;
 
71
        my $verbose = shift || 0;
 
72
 
 
73
        if ( $level !~ /^\d+$/ ) { die "argument must be integer"; }
 
74
 
 
75
        my $cmd;
 
76
        if ( $verbose ) { $cmd = "pod2usage -verbose 1 $this_script"; }
 
77
        else { $cmd = "pod2usage $this_script"; }
 
78
 
 
79
        open(CMD, "$cmd|") || die "error executing $cmd: $!";
 
80
        while(<CMD>) { print; }
 
81
        close(CMD);
 
82
        exit($level);
 
83
}
 
84
 
 
85
sub version_message {
 
86
        if ( ! defined(&PROG_DESCRIPTION) ) {
 
87
                warn "You should define the constant PROG_DESCRIPTION in your program\n";
 
88
        } else {
 
89
                print &PROG_DESCRIPTION . " ";
 
90
        }
 
91
 
 
92
        if ( ! defined(&PROG_VERSION) ) {
 
93
                warn "You should define the constant PROG_VERSION in your program\n";
 
94
        } else {
 
95
                print &PROG_VERSION . "\n";
 
96
        }
 
97
 
 
98
        if ( ! defined(&PROG_COPYRIGHT) ) {
 
99
                warn "You should define the constant PROG_COPYRIGHT in your program\n";
 
100
        } else {
 
101
                print &PROG_COPYRIGHT . "\n";
 
102
        }
 
103
 
 
104
        if ( defined(&PROG_AUTHOR) ) {
 
105
                print "Written by " . &PROG_AUTHOR . "\n";
 
106
        }
 
107
}
 
108
 
 
109
sub download_data_files {
 
110
        my $download = 0;
 
111
        my @statinfo = stat($gs_data_filename);
 
112
        if ( ! defined($statinfo[9]) ) { $download = 1; }
 
113
        elsif ( time - $statinfo[9] > $gs_flg_data_age ) { $download = 1; }
 
114
        if ( $download ) {
 
115
                system("curl '$gs_flg_data_url' > '$gs_data_filename'") && die "cannot download $gs_flg_data_url";
 
116
        }
 
117
        else {
 
118
                print STDERR "No need to download $gs_data_filename - we have a recent copy locally\n";
 
119
        }
 
120
 
 
121
        $download = 0;
 
122
        @statinfo = stat($gs_name_filename);
 
123
        if ( ! defined($statinfo[9]) ) { $download = 1; }
 
124
        elsif ( time - $statinfo[9] > $gs_flg_data_age ) { $download = 1; }
 
125
        if ( $download ) {
 
126
                system("curl '$gs_flg_name_url' > '$gs_name_filename'") && die "cannot download $gs_flg_name_url : $!";
 
127
        }
 
128
        else {
 
129
                print STDERR "No need to download $gs_name_filename - we have a recent copy locally\n";
 
130
        }
 
131
 
 
132
}
 
133
 
 
134
sub load_data {
 
135
        open(DATA, "<$gs_data_filename") || die "cannot open data file for reading : $!";
 
136
        my $header = 1;
 
137
        while(<DATA>) {
 
138
                if (/END_OF_HEADER/) {
 
139
                        $header = 0;
 
140
                        next;
 
141
                }
 
142
                if ($header) { next };
 
143
 
 
144
                # skip comments
 
145
                if (/^\s*\!/) { next };
 
146
 
 
147
                chomp;
 
148
                my($id, @data) = unpack("A10 A17 A25 A25 A25 A25 A25 A25 A6 A6", $_);
 
149
                $id =~ s/'//g;
 
150
                if ( $id > $gs_selection_max && ! $gs_flg_all ) { last; }
 
151
 
 
152
                if ( ! defined($gh_selection{$id}) && ! $gs_flg_all ) { next; }
 
153
 
 
154
                my %datahash;
 
155
                for(my $i=0; $i<=$#ga_field_order; $i++) {
 
156
                        $data[$i] =~ s/^\s+//;
 
157
                        $data[$i] =~ s/\s+$//;
 
158
                        $datahash{$ga_field_order[$i]} = $data[$i];
 
159
                }
 
160
                $gh_data{$id} = \%datahash;
 
161
        }
 
162
        close(DATA);
 
163
}
 
164
 
 
165
sub load_names {
 
166
        open(NAMES, "<$gs_name_filename") || die "cannot open name file for reading : $!";
 
167
        my $in_pre = 0;
 
168
        while(<NAMES>) {
 
169
                if ( /<pre>/i ) {
 
170
                        $in_pre = 1;
 
171
                        next;
 
172
                }
 
173
 
 
174
                if ( ! $in_pre ) { next; }
 
175
 
 
176
                if ( /<\/pre>/ ) { last; }
 
177
 
 
178
                chomp;
 
179
 
 
180
                s/<a name=[^>]+>//i;
 
181
                s/<\/a>//;
 
182
 
 
183
                my(undef, $id, $name) = split(/[\(\)]/, $_);
 
184
 
 
185
                if ( defined($gh_data{$id}) ) {
 
186
                        $name =~ s/^\s+//;
 
187
                        $name =~ s/\s+$//;
 
188
                        $gh_names{$id} = $name;
 
189
                }
 
190
        }
 
191
}
 
192
 
 
193
sub dump_data {
 
194
        foreach my $id (sort {$a <=> $b} keys %gh_data) {
 
195
                my $name;
 
196
                if (defined($gh_names{$id})) {
 
197
                        $name = $gh_names{$id};
 
198
                }
 
199
                else {
 
200
                        $name = "unknown_name_$gs_unknown_name_counter";
 
201
                        $gs_unknown_name_counter++;
 
202
                }
 
203
                printf "[%s]\n", lc($name);
 
204
                print "name = $name\n";
 
205
                print "parent = Sun\n";
 
206
                print "oblateness = 0.0\n";
 
207
                print "color = 1.0,1.0,1.0\n";
 
208
                print "coord_func = comet_orbit\n";
 
209
                print "halo = false\n";
 
210
                print "lighting = false\n";
 
211
                print "tex_halo = star16x16.png\n";
 
212
                print "radius = 25\n";
 
213
                print "tex_map = nomap.png\n";
 
214
                my $sp = (($gh_data{$id}{"SemiMajorAxis"} ** 3) ** 0.5) * 365.256363051;
 
215
                print "sidereal_period = $sp\n";
 
216
                foreach my $field (reverse sort @ga_field_order) {
 
217
                        my $prefix = "";
 
218
                        if ($field eq "mag") { next; }
 
219
                        elsif ($field ne "albedo") { $prefix = "orbit_"; }
 
220
 
 
221
                        my $val = sprintf("%.15f", $gh_data{$id}{$field});
 
222
                        $val =~ s/0+$//;
 
223
                        printf "%s%s = %s\n", $prefix, $field, $val; 
 
224
                }
 
225
                print "\n";
 
226
        }
 
227
}
 
228
 
 
229
 
 
230
__END__
 
231
 
 
232
=head1 NAME 
 
233
 
 
234
generate_asteroid_ssystem.pl - generate asteroid orbital data for ssystem.ini
 
235
 
 
236
=head1 SYNOPSIS
 
237
 
 
238
generate_asteroid_ssystem.pl [options]
 
239
 
 
240
=head1 DESCRIPTION
 
241
 
 
242
Downloads asteroid data and outputs sections intended for use in the Stellarium data file, ssystem.ini.
 
243
 
 
244
=head1 OPTIONS
 
245
 
 
246
=over
 
247
 
 
248
=item B<--all>
 
249
 
 
250
Select all asteroids for output.  Note this is a lot and will 
 
251
take some time.
 
252
 
 
253
=item B<--data-url>=I<u>
 
254
 
 
255
Fetch orbital data from url I<u>.  The default is 
 
256
http://hamilton.dm.unipi.it/astdys/catalogs/allnum.cat
 
257
 
 
258
=item B<--force-refresh>
 
259
 
 
260
Fetch orbital and name data even if the files are less than a month old.
 
261
 
 
262
=item B<--help>
 
263
 
 
264
Print the command line syntax an option details.
 
265
 
 
266
=item B<--ids>=I<i>
 
267
 
 
268
Specify the list of asteroid IDs to output.  This is a comma separated list of numbers or ranges of numbers, e.g.
 
269
 
 
270
1,3-6,12
 
271
 
 
272
Would select asteroids with numbers 1, 3, 4, 5, 6, and 12.  The default is 1-10.
 
273
 
 
274
=item B<--name-url>=I<u>
 
275
 
 
276
Fetch name data from url I<u>.  The default is 
 
277
http://cfa-www.harvard.edu/iau/lists/MPNames.html
 
278
 
 
279
=item B<--version>
 
280
 
 
281
Print the program description and version.
 
282
 
 
283
=back
 
284
 
 
285
=head1 ENVIRONMENT
 
286
 
 
287
=over
 
288
 
 
289
=item STUFF_?_DBLEVEL
 
290
 
 
291
Sets debugging levels.  The ? can be D for database, M for module,
 
292
or S for script debugging messages.  Generally only S and D are
 
293
interesting for users, M is mostly just used during development.
 
294
 
 
295
=back
 
296
 
 
297
=head1 FILES
 
298
 
 
299
=over
 
300
 
 
301
=item filename
 
302
 
 
303
desc
 
304
 
 
305
=back
 
306
 
 
307
=head1 LICENSE
 
308
 
 
309
generate_asteroid_ssystem.pl is released under the GNU GPL (version 2, June 1991).  A
 
310
copy of the license should have been provided in the distribution of
 
311
the software in a file called "LICENSE".  If you can't find this, then
 
312
try here: http://www.gnu.org/copyleft/gpl.html
 
313
 
 
314
=head1 AUTHOR
 
315
 
 
316
Matthew Gates <matthew@porpoisehead.net>
 
317
 
 
318
http://porpoisehead.net/
 
319
 
 
320
=head1 CHANGELOG
 
321
 
 
322
=over
 
323
 
 
324
=item Date:2007-05-29 Created, Author MNG
 
325
 
 
326
Original version.
 
327
 
 
328
=item Date:2007-11-06 Bug fixes, Author MNG
 
329
 
 
330
Download error status fixed.  Fixed --all option.  Fixed undefined name error.
 
331
 
 
332
=back
 
333
 
 
334
=head1 BUGS
 
335
 
 
336
Please report bugs to the author.
 
337
 
 
338
=head1 SEE ALSO
 
339
 
 
340
stellarium(1)
 
341
 
 
342
=cut
 
343