-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwkmerge
executable file
·197 lines (155 loc) · 3.5 KB
/
wkmerge
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
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/perl
#
# Copyright (c) 2008 Rainer Clasen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms described in the file LICENSE included in this
# distribution.
#
# example:
# convert srm to hrm
# ... merge gpx ele into hrm when available
# get summary info
# generate body
# TODO: pod
use strict;
use warnings;
use Getopt::Long;
use Workout;
use I18N::Langinfo qw(langinfo CODESET);
my $charset = langinfo(CODESET);
binmode STDIN, ":encoding($charset)";
binmode STDOUT, ":encoding($charset)";
binmode STDERR, ":encoding($charset)";
my $join;
my $skip;
my $recint;
my @itype;
my $otype;
my $delta = 0;
my @fields;
my $debug;
my $wanthelp;
my $needhelp;
if( ! GetOptions(
"debug!" => \$debug,
"delta|d=i" => \$delta,
"fields=s" => \@fields,
"help!" => \$wanthelp,
"itype=s" => \@itype,
"join!" => \$join,
"otype=s" => \$otype,
"recint=f" => \$recint,
"skip!" => \$skip,
)){
$needhelp++;
}
if( $wanthelp ){
print <<EOF;
$0 [opt] <fname1> <fname2> <dst-fname>
prepare workout data for mailing it to the trainer
Options:
--itype=<a>,<b> input file types
--delta=<d> time difference of fname2
--fields=<n,n..> fields to pick from fname2
--otype=<c> output file type
--recint=<int> output sampling interval (default=fname1's)
--join join blocks
--skip skip chunks where essential fields are missing
--debug enable debuging output
--help this cruft
EOF
exit 0;
}
my $sfname = shift;
if( ! $sfname ){
print STDERR "missing master file\n";
$needhelp++;
}
my $gfname = shift;
if( ! $gfname ){
print STDERR "missing secondary file\n";
$needhelp++;
}
my $ofname = shift;
if( ! $ofname ){
print STDERR "missing output file\n";
$needhelp++;
}
@itype = split(/,/,join(',',@itype)) if @itype;
if( @fields ){
@fields = split(/,/,join(',',@fields));
} else {
#@fields ||= (qw( time dur dist spd cad hr work pwr));
@fields = qw( ele );
}
foreach my $f ( @fields ){
grep /^$f$/, @Workout::Chunk::core_fields
and next;
print STDERR "field not supported: $f\n";
$needhelp++;
}
if( $needhelp ){
print STDERR "please use $0 --help for usage info\n";
exit 1;
}
$otype ||= Workout::file_type_name( $ofname );
my $dst = Workout::file_new( {
debug => $debug,
ftype => $otype,
} );
if( ! $join and $join = ! $dst->cap_block ){
$debug && print STDERR "auto-join\n";
}
# read wk1
my $wk1 = Workout::file_read( $sfname, {
ftype => $itype[0],
debug => $debug,
});
my $it1 = $wk1->iterate;
if( $recint ){
$dst->recint( $recint );
} elsif( $dst->recint ){
if( $wk1->recint_chunks ){
$dst->recint( $wk1->recint_chunks );
$recint = $dst->recint if $join;
} else {
$recint = $dst->recint;
$debug && print STDERR "auto-resampling to $recint\n";
}
}
$it1 = Workout::filter('Join', $it1,{
recint => $recint,
debug => $debug,
}) if $join;
$it1 = Workout::filter('Resample', $it1, {
recint => $recint,
debug => $debug,
}) if $recint;
# read wk2
my $wk2 = Workout::file_read( $gfname, {
ftype => $itype[1],
debug => $debug,
});
my $it2 = Workout::filter('Join', $wk2, {
debug => $debug,
});
$it2 = Workout::filter('Timeshift', $it2, {
delta => $delta,
debug => $debug,
}) if $delta;
my $iter = Workout::filter('Merge', $it2, {
master => $it1,
fields => \@fields,
debug => $debug,
});
$iter = Workout::filter( 'SkipUndef', $iter, {
debug => $debug,
fields => [ $dst->fields_essential ],
}) if $skip;
# fill dst
$dst->from( $iter );
# write dst
$dst->meta_prune_all;
$dst->write( $ofname );
1;