Move use strict before BEGIN
[gpstools.git] / vg
blobac92e327f3b55275e869d6d6d879c0a9a99c2630
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # vg
5 # File ID: 58ece940-08f3-11de-ab14-000475e441b9
6 # View GPX files in gnuplot(1)
8 # Character set: UTF-8
9 # ©opyleft 2009– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 3 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 use strict;
15 use Getopt::Long;
17 $| = 1;
19 our $Debug = 0;
21 our %Std = (
23 'label-file' => "$ENV{'HOME'}/gps/poi/labels.gnuplot",
24 'with' => 'l',
28 our %Opt = (
30 '2d' => 0,
31 'debug' => 0,
32 'help' => 0,
33 'keep-files' => 0,
34 'label-file' => "$Std{'label-file'}",
35 'time' => 0,
36 'verbose' => 0,
37 'version' => 0,
38 'with' => $Std{'with'},
42 our $progname = $0;
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = "0.00";
46 my @cmdl = @ARGV;
48 Getopt::Long::Configure("bundling");
49 GetOptions(
51 "2d|2" => \$Opt{'2d'},
52 "debug" => \$Opt{'debug'},
53 "help|h" => \$Opt{'help'},
54 "keep-files|k" => \$Opt{'keep-files'},
55 "label-file|l=s" => \$Opt{'label-file'},
56 "time|t" => \$Opt{'time'},
57 "verbose|v+" => \$Opt{'verbose'},
58 "version" => \$Opt{'version'},
59 "with|w=s" => \$Opt{'with'},
61 ) || die("$progname: Option error. Use -h for help.\n");
63 $Opt{'debug'} && ($Debug = 1);
64 $Opt{'help'} && usage(0);
65 if ($Opt{'version'}) {
66 print_version();
67 exit(0);
70 ($Opt{'2d'} && $Opt{'time'}) && die("$progname: Cannot mix --2d and --time options\n");
71 ($Opt{'with'} =~ /^(d|l|lp|p)$/) || die("$progname: $Opt{'with'}: Invalid value of --with option, see $progname -h for help\n");
73 my $cmdl_str = join(" ", @cmdl);
74 my ($dat_file, $cmd_file);
75 my $fp;
77 if ($Opt{'time'}) {
78 $dat_file = sprintf("/tmp/vg-t.%u.%u.tmp", time, $$);
79 open($fp, ">$dat_file") || die("$progname: $dat_file: Cannot open file for write: $!\n");
80 my @gpst_array = ("gpst", "-o", "csv", "-rt", "-e", "-d", "-t", @ARGV, "|", "rmspcall", "|", "uniq");
81 print($fp `@gpst_array`);
82 close($fp);
83 $cmd_file = sprintf("/tmp/vg-t-cmd.%u.%u.tmp", time, $$);
84 open($fp, ">$cmd_file") || die("$progname: $cmd_file: Cannot open file for write: $!\n");
85 print($fp <<END);
86 set mouse format "%.6f"
87 load "$Opt{'label-file'}"
88 set zdata time
89 # set timefmt "%Y-%m-%dT%H:%M:%SZ"
90 set timefmt "%s"
91 splot "$dat_file" using 2:3:1 w $Opt{'with'} palette
92 pause -1 "Trykk Enter..."
93 END
94 close($fp);
95 system("gnuplot -persist $cmd_file");
96 } elsif ($Opt{'2d'}) {
97 $dat_file = sprintf("/tmp/vg-2.%u.%u.tmp", time, $$);
98 open($fp, ">$dat_file") || die("$progname: $dat_file: Cannot open file for write: $!\n");
99 my @gpst_array = ("gpst", "-o", "clean", "-rp", "-d", "-t", @ARGV, "|", "rmspcall", "|", "uniq");
100 print($fp `@gpst_array`);
101 close($fp);
102 $cmd_file = sprintf("/tmp/vg-2-cmd.%u.%u.tmp", time, $$);
103 open($fp, ">$cmd_file") || die("$progname: $cmd_file: Cannot open file for write: $!\n");
104 print($fp <<END);
105 set mouse format "%.6f"
106 load "$Opt{'label-file'}"
107 plot "$dat_file" using 1:2 w $Opt{'with'}
108 pause -1 "Trykk Enter..."
110 close($fp);
111 system("gnuplot -persist $cmd_file");
112 } else {
113 $dat_file = sprintf("/tmp/vg.%u.%u.tmp", time, $$);
114 open($fp, ">$dat_file") || die("$progname: $dat_file: Cannot open file for write: $!\n");
115 my @gpst_array = ("gpst", "-o", "clean", "-ret", "-d", "-t", @ARGV, "|", "rmspcall", "|", "uniq");
116 print($fp `@gpst_array`);
117 close($fp);
118 $cmd_file = sprintf("/tmp/vg-cmd.%u.%u.tmp", time, $$);
119 open($fp, ">$cmd_file") || die("$progname: $cmd_file: Cannot open file for write: $!\n");
120 print($fp <<END);
121 set mouse format "%.6f"
122 load "$Opt{'label-file'}"
123 splot "$dat_file" w $Opt{'with'} palette
124 pause -1 "Trykk Enter..."
126 close($fp);
127 system("gnuplot -persist $cmd_file");
129 $Opt{'keep-files'} || unlink($dat_file, $cmd_file);
131 sub print_version {
132 # Print program version {{{
133 print("$progname v$VERSION\n");
134 # }}}
135 } # print_version()
137 sub usage {
138 # Send the help message to stdout {{{
139 my $Retval = shift;
141 if ($Opt{'verbose'}) {
142 print("\n");
143 print_version();
145 print(<<END);
147 Usage: $progname [options] [file [files [...]]]
149 Options:
151 -2, --2d
152 Create 2D plot instead of 3D. Bigger window by default in gnuplot,
153 and it’s faster when plotting lots of data.
154 -h, --help
155 Show this help.
156 -k, --keep-files
157 Don’t remove temporary files after program execution.
158 -l X, --label-file X
159 Use X as label file.
160 Default: $Std{'label-file'}
161 -t, --time
162 Use time as Z axis instead of elevation.
163 -v, --verbose
164 Increase level of verbosity. Can be repeated.
165 -w X, --with X
166 Use line type X:
167 d - dots
168 l - lines
169 p - points
170 lp - lines with points
171 Default: $Std{'with'}
172 --version
173 Print version information.
174 --debug
175 Print debugging messages.
178 exit($Retval);
179 # }}}
180 } # usage()
182 sub msg {
183 # Print a status message to stderr based on verbosity level {{{
184 my ($verbose_level, $Txt) = @_;
186 if ($Opt{'verbose'} >= $verbose_level) {
187 print(STDERR "$progname: $Txt\n");
189 # }}}
190 } # msg()
192 sub D {
193 # Print a debugging message {{{
194 $Debug || return;
195 my @call_info = caller;
196 chomp(my $Txt = shift);
197 my $File = $call_info[1];
198 $File =~ s#\\#/#g;
199 $File =~ s#^.*/(.*?)$#$1#;
200 print(STDERR "$File:$call_info[2] $$ $Txt\n");
201 return("");
202 # }}}
203 } # D()
205 __END__
207 # Plain Old Documentation (POD) {{{
209 =pod
211 =head1 NAME
215 =head1 SYNOPSIS
217 [options] [file [files [...]]]
219 =head1 DESCRIPTION
223 =head1 OPTIONS
225 =over 4
227 =item B<-h>, B<--help>
229 Print a brief help summary.
231 =item B<-v>, B<--verbose>
233 Increase level of verbosity. Can be repeated.
235 =item B<--version>
237 Print version information.
239 =item B<--debug>
241 Print debugging messages.
243 =back
245 =head1 BUGS
249 =head1 AUTHOR
251 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
253 =head1 COPYRIGHT
255 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
256 This is free software; see the file F<COPYING> for legalese stuff.
258 =head1 LICENCE
260 This program is free software: you can redistribute it and/or modify it
261 under the terms of the GNU General Public License as published by the
262 Free Software Foundation, either version 3 of the License, or (at your
263 option) any later version.
265 This program is distributed in the hope that it will be useful, but
266 WITHOUT ANY WARRANTY; without even the implied warranty of
267 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
268 See the GNU General Public License for more details.
270 You should have received a copy of the GNU General Public License along
271 with this program.
272 If not, see L<http://www.gnu.org/licenses/>.
274 =head1 SEE ALSO
276 =cut
278 # }}}
280 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :