-
Notifications
You must be signed in to change notification settings - Fork 2
/
bulk_igc_reader.pl
executable file
·187 lines (151 loc) · 4.07 KB
/
bulk_igc_reader.pl
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
182
183
184
185
186
#!/usr/bin/perl
#
# Reads in a zip file full of .igc files
# should be named as such: FAINO.igc or LASTNAME_FIRSTNAME.igc
#
# Geoff Wong 2007
#
require DBD::mysql;
use Data::Dumper;
use File::Temp;
use File::Copy;
use File::Basename;
use TrackLib qw(:all);
use Defines qw(:all);
local * DIR;
#
# Extract files and read them all into the database
#
sub extract_igcs
{
my ($f) = @_;
my @fields;
my @names;
my %tracks;
my $row;
my $tmpdir;
my $pilPk;
my $hgfa;
my $sth;
# clean up and extract files ..
$tmpdir = mkdtemp("/tmp/bulkXXXX");
#system("/usr/bin/rm -rf $tmpdir");
#mkdir($tmpdir, 0755);
system("yes | /usr/bin/unzip $f -d $tmpdir");
opendir(DIR, $tmpdir) or die "can't opendir $f: $!";
while (defined($file = readdir(DIR)))
{
# find the pilot ..
#print "got: $file\n";
if ($file eq "." or $file eq "..")
{
next;
}
@fields = split /\./, $file;
# check size = 2
@names = split /_/, $fields[0];
if (scalar @names > 0)
{
$pilPk = 0 + $names[1];
print "Supposed HGFA # as found in the file name $file: $pilPk\n";
}
if ($pilPk > 0)
{
$sth = $dbh->prepare("select pilHGFA from tblPilot where pilHGFA=$pilPk or pilCIVL=$pilPk" );
}
else
{
if (scalar @names > 1)
{
#print "two found: ", $names[0], " ", $names[1], "\n";
$sth = $dbh->prepare("select pilHGFA from tblPilot where pilLastName='" . $names[0] . "' and pilFirstName='$names[1]'");
}
else
{
$sth = $dbh->prepare("select pilHGFA from tblPilot where pilLastName='" . $names[0] . "'");
}
}
$sth->execute();
# add the pilot if they're not found?
$hgfa = $sth->fetchrow_array() + 0;
if ($hgfa == 0)
{
print "Can't find pilot $file in database\n";
}
else
{
print "Found: $file with hgfa=$hgfa\n";
$tracks{$hgfa} = $tmpdir . "/" .$file;
}
}
closedir(DIR);
return \%tracks;
}
sub read_all_tracks
{
my ($comPk,$tasPk,$tracks) = @_;
my $file;
my $nfile;
my $traPk;
my @fields;
my $out;
my $outo;
my $gliPk;
my $sth;
my $res;
my $name;
my $copyname;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
my $dte;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year = $year + 1900;
$dte = sprintf("%04d-%02d-%02d_%02d%02d%02d", $year, $mon+1, $mday, $hour, $min, $sec);
#print Dumper($tracks);
for my $pilPk ( keys %$tracks )
{
$file = $tracks->{$pilPk};
#print "Track read ($file) for pilot=$pilPk\n";
$nfile = dirname($file) . "\/$pilPk";
rename($file, $nfile);
print ("${BINDIR}add_track.pl $pilPk \"$nfile\" $comPk $tasPk\n");
$res = `${BINDIR}add_track.pl $pilPk \"$nfile\" $comPk $tasPk`;
print $res;
#print "Track read ($pilPk:$file) may have failed: $out\n";
$sth = $dbh->prepare("select pilLastName from tblPilot where pilHGFA='$pilPk'");
$sth->execute();
$name = lc($sth->fetchrow_array());
$copyname = $FILEDIR . $year . "/" . $name . "_" . $pilPk . "_" . $dte;
print "cp $nfile $copyname\n";
copy($file, $copyname);
chmod 0644, $copyname;
}
}
#
# Main program here ..
#
my $tracks;
my $tasPk;
my $comPk;
my $sth;
if (scalar @ARGV < 2)
{
print "bulk_igc_reader.pl tasPk zipfile\n";
exit 0;
}
$dbh = db_connect();
# Check the task stuff
$tasPk = $ARGV[0] + 0;
$sth = $dbh->prepare("select comPk from tblTask where tasPk=$tasPk");
$sth->execute();
$comPk = $sth->fetchrow_array() + 0;
if ($comPk == 0)
{
print "Can't find task $tasPk\n";
exit 1;
}
# Find all the tracks
$tracks = extract_igcs($ARGV[1]);
# Read each of the tracks
read_all_tracks($comPk,$tasPk,$tracks);
# Score the round ..
#system("task_score.pl $tasPk");