summaryrefslogtreecommitdiffstats
path: root/perl/gpsp2igcfile.pl
blob: 06efe1d0dd395bad0546727d846a24ad55259c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/perl -w

# $Id: gpsp2igcfile.pl 3 2014-07-31 09:59:20Z kruegerh $
# (c) 2002-2016 Hannes Krueger
# This file is part of the GPLIGC/ogie package
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

#  This little script converts a tracklist file obtained from a Garmin GPS using
#  the gpspoint program into something resembling an IGC file for use with other programs.
#  Script by Jan M. W. Krueger, 1st of March 2004

#  put into the GPLIGC/perl CVS repository
#  $Id: gpsp2igcfile.pl 3 2014-07-31 09:59:20Z kruegerh $
#  revised by Hannes Kruger

#  usage:
#  gpsp2igc.pl  <inputfile.gpspoint

# this will write igcfiles for each day!
# with more or less igc conform filenames!
# see also gpsp2igc

# new segment on new day -> new igc file! (well, that may be problematic for australian files...)

# igc naming

$device_id = "000";
$manufacturer_short="X";
$manufacturer_long="GAR";
$filenr=1;


@lines = <STDIN>;


# flag for new track segment ( 1 is new segment)
$first = 0;
$oday ="000000"; # old day!

for($tmp=0; $tmp<=$#lines; $tmp++){
	if($lines[$tmp] =~ /trackpoint/){
		if($lines[$tmp] =~ /newsegment=\"yes\"/){
			$lines[$tmp] =~ s/\ \ newsegment=\"yes\"\ //g;
			#push(@outlines, "\n");
			#push(@igclines, "\n");
			$first = 1;
		}

		$lines[$tmp] =~ s/type\=\"trackpoint\"\ \ //g;
		$lines[$tmp] =~ s/\"/\ /g;
		$lines[$tmp] =~ s/altitude\=\ //g;
		$lines[$tmp] =~ s/latitude\=\ //g;
		$lines[$tmp] =~ s/longitude\=\ //g;
		$lines[$tmp] =~ s/unixtime\=\ //g;



		$lines[$tmp] =~ /([\d.-]+)\s*([\d.-]+)\s*([\d.-]+)\s*([\d.-]+)/;
		$alt = $1;
		$latitude = $2;
		$longitude = $3;
		$unixtime = $4;

		# Put altitude in IGC line format:
		if($alt<0){
			$altnew = -$alt;
			$altnew = sprintf "%5d", $altnew;
			$altnew =~ s/\ /0/g;
			$altnew =~ s/0/-/;
		} else {
			$altnew = sprintf "%5d", $alt;
			$altnew =~ s/\ /0/g;
		}


		# Put latitude in IGC line format:
		if($latitude < 0){
			$latitudenew = -$latitude;
			$ns = "S";
		} else {
			$latitudenew = $latitude;
			$ns = "N";
		}
		$latint = sprintf "%.0f", $latitudenew-0.5;
		$latmins = 100000* ($latitudenew - $latint)*60/100;
		$latint = sprintf "%2.0f", $latint;
		$latint =~ s/\ /0/g;
		$latmins = sprintf "%5.0f", $latmins;
		$latmins =~ s/\ /0/g;

		$latitudestring = $latint.$latmins.$ns;


		# Put longitude in IGC line format:
		if($longitude < 0){
			$longitudenew = -$longitude;
			$ew = "W";
		} else {
			$longitudenew = $longitude;
			$ew = "E";
		}
		$lonint = sprintf "%.0f", $longitudenew-0.5;
		$lonmins = 100000* ($longitudenew - $lonint)*60/100;
		$lonint = sprintf "%3.0f", $lonint;
		$lonint =~ s/\ /0/g;
		$lonmins = sprintf "%5.0f", $lonmins;
		$lonmins =~ s/\ /0/g;

		$longitudestring = $lonint.$lonmins.$ew;


		# use gmtime to get GMT from unix-epoch-seconds
		($ss, $mm, $hh, $dd, $mon, $yyyy) = gmtime ($unixtime);
		$mon++; # (dd: 1-31, mon: 0-11, y: yyyy -1900)
		$yyyy+=1900; $yy=substr($yyyy,2);


		# detect new segment and mark with HFDTEddmmyy
		if($first == 1){
			$first = 0;
				if (sprintf("%02d%02d%02d",$dd,$mon,$yy) ne $oday) {
					newfile($yyyy,$mon,$dd);
					$oday = sprintf("%02d%02d%02d",$dd,$mon,$yy);
				}
		}

		print FN "B".sprintf("%02d%02d%02d", $hh,$mm,$ss).$latitudestring.$longitudestring."A".$altnew.$altnew."\n";

	}
}


sub newfile {

	my $y=shift;
	my $m=shift;
	my $d=shift;

	my ($igcm, $igcd);
	my $igcy=substr($y,-1);

	if ($m < 10) {$igcm=$m}
	if ($m == 10) {$igcm="a"}
	if ($m == 11) {$igcm="b"}
	if ($m == 12) {$igcm="c"}

	if ($d < 10) {$igcd=$d} else {
		$igcd= chr(97+($d-10));
	}

	filenames:
 	my $fn = "$igcy$igcm$igcd$manufacturer_short${device_id}$filenr.igc";
	my $lfn= sprintf("%4d-%02d-%02d-$manufacturer_long-$device_id-%02d.igc", $y,$m,$d,$filenr);

	print "IGC file: $lfn ($fn)\n";

	if (-e $lfn) {print "$lfn ($fn) exists!\n"; $filenr++; goto filenames;}

	close FN;

	open(FN, ">$lfn") || die "cannot open $lfn for writing";

	print FN "A$manufacturer_long$device_id  ".'$Id: gpsp2igcfile.pl 3 2014-07-31 09:59:20Z kruegerh $'."\n";
	print FN "HFFTY FR Type:  unknown device\n";
	print FN "HFFSW Software:  gpspoint / gpsp2igcfile.pl\n\n";

	my $yy=substr($y,2);
	print FN "HFDTE".sprintf("%02d%02d%02d",$d,$m,$yy)."\n\n";
}