better error messages
[openface-maja] / p.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5 use Data::Dump qw(dump);
6
7 my $filename = 'Intervali_Openface_novi_rad_Boris_export.txt';
8
9 my $debug = $ENV{DEBUG} || 0;
10
11 sub col2nr {
12         my @c = @_;
13         my $i = 0;
14         my $out;
15         $out->{$_} = $i++ foreach @c;
16         #warn "## col2nr ", dump( $out );
17         return $out;
18 }
19
20 open(my $fh, '<', $filename);
21
22 my $h = <$fh>; chomp $h;
23 my @cols = split(/\t/, $h);
24 my $col2nr = col2nr @cols;
25
26 my $file2;
27 my $fh2;
28 my $last_id;
29
30 my @h2;
31 my $h2nr;
32 my $tell2;
33
34
35 while(<$fh>) {
36         chomp;
37         next if m/^\s*$/;       # FIXME corrput data
38         my @v = split(/\t/, $_);
39         warn "#[ $_ ]\n" if $debug;
40
41         if ( ! $last_id || $last_id ne $v[0] ) {
42                 my $patt = 'originals_2023-07-07/' . $v[0] . '*.csv';
43                 $file2 = glob $patt;
44                 $file2 ||= glob 'originals_2023-07-07/' . $v[0] . '*.csv';
45
46                 if ( ! defined $file2 ) {
47                         warn "SKIP ERROR: $patt glob didn't find anything for v[0]=$v[0] [$_]";
48                         next;
49                 }
50
51                 if ( ! -r $file2 ) {
52                         warn "ERROR: $patt glob file $file2 not readable: $! SKIPPING [$_]";
53                         next;
54                 }
55
56                 warn "# file2 $file2 [$v[0]]";
57                 open($fh2, '<', $file2);
58                 $last_id = $v[0];
59                 @h2 = ();
60         }
61
62         sub col_v {
63                 my $name = shift;
64                 my $i = $col2nr->{$name};
65                 die "can't find $name" unless defined $i;
66                 my $v = $v[$i];
67                 die "can't find $i : $name in ",dump( \@v ) unless defined $v;
68                 warn "## col_v $name -> $i -> $v",dump( \@v ) if $debug > 1;
69                 return $v;
70         }
71
72         #my $start = $v[ $col2nr->{'Start (s)'} ];
73         #my $stop  = $v[ $col2nr->{'Stop (s)'} ];
74
75         #my $start = col_v 'Start (s)';
76         #my $stop  = col_v 'Stop (s)';
77
78         my $start = $v[10];
79         my $stop  = $v[11];
80         warn "# start: $start - stop: $stop\n" if $debug;
81
82         my @sum;
83         my $count = 0;
84
85         while(<$fh2>) {
86                 chomp; s/\r+$//;
87                 warn "## fh2 [ $_ ]\n" if $debug;
88                 #my @v2 = map { s/^\s+//; $_ } split(/\t/,$_);
89                 my @v2 = map { s/^\s+//; $_ } split(/[;,]\s*/,$_);
90                 if ( ! @h2 ) {
91                         @h2 = @v2;
92                         $h2nr = col2nr @h2;
93                         $tell2 = tell($fh2);
94                         warn "# h2nr ",dump( $h2nr ) if $debug;
95                         next;
96                 }
97
98                 sub col_v2 {
99                         my $name = shift;
100                         my $i = $h2nr->{$name};
101                         die "can't find $name in ",dump( $h2nr ) unless defined $i;
102                         my $v = $v2[$i];
103                         die "can't find $i : $name in ",dump( \@v2 ) unless defined $v;
104                         return "### col_v2 $name $i = $v";
105                         return $v;
106                 }
107
108
109                 #my $timestamp = $v2[ $h2nr->{'timestamp'} ] || die;
110                 #my $timestamp = col_v2 'timestamp';
111                 my $timestamp = $v2[2];
112
113                 #$timestamp /= 1000 if $timestamp =~ m/^\d+$/;  # FIXME fix corrputed input data
114                 #warn "XXX filter $start - $stop from ",dump( $timestamp ); #$h2nr, \@v2 );
115
116                 # Argument "1.324.400" isn't numeric
117                 $timestamp =~ s/^(\d+)\.(\d\d\d)\.(\d\d\d)$/$1$2.$3/;
118
119                 if ( $timestamp !~ m/^\d+(\.\d+)$/ ) {
120                         warn "timestamp [$timestamp] from $file2 not numeric [$_]";
121                 }
122
123                 if ( $timestamp < $start ) {
124                         #warn "## $start > @v2";
125                 } elsif ( $timestamp < $stop ) {
126                         $tell2 = tell($fh2);
127                         foreach my $n ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} ) {
128                                 $sum[$n] += $v2[$n];
129                                 #warn "sum $n $sum[$n] $v2[$n]\n";
130                         }
131                         $count++;
132                         #warn "XXX $count sum $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} = ",dump( \@sum );
133                 } else {
134                         warn "# seek $tell2" if $debug;
135                         seek $fh2, $tell2, 0;
136
137                         die "count is 0" if $count == 0;
138
139                         #warn "XXX $file2 count=$count XXX ", $h2nr->{'AU01_r'},' - ', $h2nr->{'AU45_r'} , " YYY ", $h2nr->{'AU01_c'}, ' - ', $h2nr->{'AU45_c'}, ' sum=', dump( \@sum );
140                         my @add_cols;
141                         push @add_cols, map { $sum[$_] / $count } ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} );
142                         push @add_cols, map { $sum[$_] }          ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} );
143                         warn "# add_cols = ",dump( \@add_cols ) if $debug;
144                         print join("\t", @v, @add_cols ), "\n";
145
146                         @sum = ();
147                         $count = 0;
148
149                         warn "# last" if $debug;
150                         last;
151                 }
152         }
153 }
154
155 __END__
156
157
158 my $col2nr;
159
160 # prema filenamenu iz $v[0]
161 # Start (s)       Stop (s)
162
163 my $lookup_file = '
164
165 # col timestamp
166