caed5e9aab9c2ca767f29eccfabca5833dc1b598
[openface-maja] / p.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use Data::Dump qw(dump);
6 use Text::Unaccent;
7 use POSIX qw(strftime);
8
9 my $filename = 'Intervali_Openface_novi_rad_Boris_export.txt';
10
11 my $debug = $ENV{DEBUG} || 0;
12
13 my $today = strftime("%Y-%m-%dT%H:%M:%S", localtime(time()));
14 warn "# today $today";
15
16 open(my $err_fh, '>', "out.$today.log");
17 local $SIG{__WARN__} = sub {
18         print STDERR @_;
19         print $err_fh @_;
20 };
21
22
23 sub col2nr {
24         my @c = @_;
25         my $i = 0;
26         my $out;
27         $out->{$_} = $i++ foreach @c;
28         #warn "## col2nr ", dump( $out );
29         return $out;
30 }
31
32 open(my $fh, '<', $filename);
33
34 my $h = <$fh>; chomp $h;
35 my @cols = split(/\t/, $h);
36 my $col2nr = col2nr @cols;
37
38 my $file2;
39 my $fh2;
40 my $last_id;
41
42 my @h2;
43 my $h2nr;
44 my $tell2;
45
46 my @glob = glob 'originals_2023-07-07/*';
47 my $map_un_back;
48 my @glob_un = map {
49         my $un = lc( unac_string('utf-8', $_ ) );
50         $map_un_back->{ $un } = $_;
51         $un;
52 } @glob;
53 sub glob_id {
54         my $id = shift;
55         my $id_lc = lc( unac_string('utf-8', $id ) );
56         my $pos = -1;
57         my @found = grep { $pos++; m/$id_lc/ } @glob_un;
58         if ( @found ) {
59                 if ( $#found == 0 ) {
60                         warn "XXX patt=$id pos=$pos found=",dump(@found);
61                         return $map_un_back->{$found[0]};
62                         #return $glob[$pos];
63                 } else {
64                         warn "ERROR: pattern $id found $#found results, ignoring";
65                         return;
66                 }
67         } else {
68                 warn "not found $id";
69                 return;
70         }
71 }
72
73 open(my $skipped_input, '>',"out.$today.skip");
74 open(my $out_fh, '>', "out.$today");
75 my @out_header;
76
77 while(<$fh>) {
78         chomp;
79         next if m/^\s*$/;       # FIXME corrput data
80         my @v = split(/\t/, $_);
81         warn "#[ $_ ]\n" if $debug;
82
83         my $id = $v[0];
84         if ( ! defined $id ) {
85                 warn "ERROR: ID col 0 null [$_]";
86                 next;
87         }
88
89         if ( ! $last_id || $last_id ne $v[0] ) {
90                 my $patt = 'originals_2023-07-07/' . $v[0] . '*.csv';
91                 $file2 = glob $patt;
92                 $file2 ||= glob 'originals_2023-07-07/' . $v[0] . '*.csv';
93                 $file2 ||= glob 'originals_2023-07-07/' . unac_string('utf-8', $v[0] ) . '*.csv';
94                 $file2 ||= glob_id $v[0];
95                 # try to find file with same numbers
96                 my $id_nrs = $v[0];
97                 $id_nrs =~ s/\D//g;
98                 $file2 ||= glob_id $id_nrs;
99
100                 if ( ! defined $file2 ) {
101                         warn "SKIP ERROR: $patt glob didn't find anything for v[0]=$v[0] [$_]";
102                         print $skipped_input "$_\n";
103                         next;
104                 }
105
106                 if ( ! -r $file2 ) {
107                         warn "ERROR: $patt glob file $file2 not readable: $! SKIPPING [$_]";
108                         print $skipped_input "$_\n";
109                         next;
110                 }
111
112                 warn "# file2 $file2 [$v[0]]";
113                 open($fh2, '<', $file2);
114                 $last_id = $v[0];
115                 @h2 = ();
116         }
117
118         sub col_v {
119                 my $name = shift;
120                 my $i = $col2nr->{$name};
121                 die "can't find $name" unless defined $i;
122                 my $v = $v[$i];
123                 die "can't find $i : $name in ",dump( \@v ) unless defined $v;
124                 warn "## col_v $name -> $i -> $v",dump( \@v ) if $debug > 1;
125                 return $v;
126         }
127
128         #my $start = $v[ $col2nr->{'Start (s)'} ];
129         #my $stop  = $v[ $col2nr->{'Stop (s)'} ];
130
131         #my $start = col_v 'Start (s)';
132         #my $stop  = col_v 'Stop (s)';
133
134         my $start = $v[10];
135         my $stop  = $v[11];
136         warn "# start: $start - stop: $stop\n" if $debug;
137
138         my @sum;
139         my $count = 0;
140
141         while(<$fh2>) {
142                 chomp; s/\r+$//;
143                 warn "## fh2 [ $_ ]\n" if $debug;
144                 #my @v2 = map { s/^\s+//; $_ } split(/\t/,$_);
145                 my @v2 = map { s/^\s+//; $_ } split(/[;,]\s*/,$_);
146                 if ( ! @h2 ) {
147                         @h2 = @v2;
148                         $h2nr = col2nr @h2;
149                         $tell2 = tell($fh2);
150                         warn "# h2nr ",dump( $h2nr ) if $debug;
151                         if ( ! @out_header ) {
152                                 push @out_header, @cols;
153                                 push @out_header, map { $h2[$_] }
154                                         ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} );
155                                 push @out_header, map { $h2[$_] }
156                                         ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} );
157                                 warn "# out_header ",dump( \@out_header );
158                                 print $out_fh join("\t", @out_header),"\n";
159                         }
160                         next;
161                 }
162
163                 sub col_v2 {
164                         my $name = shift;
165                         my $i = $h2nr->{$name};
166                         die "can't find $name in ",dump( $h2nr ) unless defined $i;
167                         my $v = $v2[$i];
168                         die "can't find $i : $name in ",dump( \@v2 ) unless defined $v;
169                         return "### col_v2 $name $i = $v";
170                         return $v;
171                 }
172
173
174                 #my $timestamp = $v2[ $h2nr->{'timestamp'} ] || die;
175                 #my $timestamp = col_v2 'timestamp';
176                 my $timestamp = $v2[2];
177
178                 #$timestamp /= 1000 if $timestamp =~ m/^\d+$/;  # FIXME fix corrputed input data
179                 #warn "XXX filter $start - $stop from ",dump( $timestamp ); #$h2nr, \@v2 );
180
181                 # Argument "1.324.400" isn't numeric
182                 $timestamp =~ s/^(\d+)\.(\d\d\d)\.(\d\d\d)$/$1$2.$3/;
183
184                 if ( $timestamp !~ m/^\d+(\.\d+)$/ ) {
185                         warn "timestamp [$timestamp] from $file2 not numeric [$_]";
186                 }
187
188                 if ( $timestamp < $start ) {
189                         #warn "## $start > @v2";
190                 } elsif ( $timestamp < $stop ) {
191                         $tell2 = tell($fh2);
192                         foreach my $n ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} ) {
193                                 $sum[$n] += $v2[$n];
194                                 #warn "sum $n $sum[$n] $v2[$n]\n";
195                         }
196                         $count++;
197                         #warn "XXX $count sum $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} = ",dump( \@sum );
198                 } else {
199                         warn "# seek $tell2" if $debug;
200                         seek $fh2, $tell2, 0;
201
202                         die "count is 0" if $count == 0;
203
204                         #warn "XXX $file2 count=$count XXX ", $h2nr->{'AU01_r'},' - ', $h2nr->{'AU45_r'} , " YYY ", $h2nr->{'AU01_c'}, ' - ', $h2nr->{'AU45_c'}, ' sum=', dump( \@sum );
205                         my @add_cols;
206                         push @add_cols, map { $sum[$_] / $count } ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} );
207                         push @add_cols, map { $sum[$_] }          ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} );
208                         warn "# add_cols = ",dump( \@add_cols ) if $debug;
209                         print $out_fh join("\t", @v, @add_cols ), "\n";
210
211                         @sum = ();
212                         $count = 0;
213
214                         warn "# last" if $debug;
215                         last;
216                 }
217         }
218 }
219
220 __END__
221
222
223 my $col2nr;
224
225 # prema filenamenu iz $v[0]
226 # Start (s)       Stop (s)
227
228 my $lookup_file = '
229
230 # col timestamp
231