5 use Data::Dump qw(dump);
7 use POSIX qw(strftime);
9 my $filename = 'Intervali_Openface_novi_rad_Boris_export.txt';
11 my $debug = $ENV{DEBUG} || 0;
13 my $today = strftime("%Y-%m-%dT%H:%M:%S", localtime(time()));
14 warn "# today $today";
16 open(my $err_fh, '>', "out.$today.log");
17 local $SIG{__WARN__} = sub {
27 $out->{$_} = $i++ foreach @c;
28 #warn "## col2nr ", dump( $out );
32 open(my $fh, '<', $filename);
34 my $h = <$fh>; chomp $h;
35 my @cols = split(/\t/, $h);
36 my $col2nr = col2nr @cols;
46 my @glob = glob 'originals_2023-07-07/*';
49 my $un = lc( unac_string('utf-8', $_ ) );
50 $map_un_back->{ $un } = $_;
55 my $id_lc = lc( unac_string('utf-8', $id ) );
57 my @found = grep { $pos++; m/$id_lc/ } @glob_un;
60 warn "XXX patt=$id pos=$pos found=",dump(@found);
61 return $map_un_back->{$found[0]};
64 warn "ERROR: pattern $id found $#found results, ignoring";
73 open(my $skipped_input, '>',"out.$today.skip");
74 open(my $out_fh, '>', "out.$today");
79 next if m/^\s*$/; # FIXME corrput data
80 my @v = split(/\t/, $_);
81 warn "#[ $_ ]\n" if $debug;
84 if ( ! defined $id ) {
85 warn "ERROR: ID col 0 null [$_]";
89 if ( ! $last_id || $last_id ne $v[0] ) {
90 my $patt = 'originals_2023-07-07/' . $v[0] . '*.csv';
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
98 $file2 ||= glob_id $id_nrs;
100 if ( ! defined $file2 ) {
101 warn "SKIP ERROR: $patt glob didn't find anything for v[0]=$v[0] [$_]";
102 print $skipped_input "$_\n";
107 warn "ERROR: $patt glob file $file2 not readable: $! SKIPPING [$_]";
108 print $skipped_input "$_\n";
112 warn "# file2 $file2 [$v[0]]";
113 open($fh2, '<', $file2);
120 my $i = $col2nr->{$name};
121 die "can't find $name" unless defined $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;
128 #my $start = $v[ $col2nr->{'Start (s)'} ];
129 #my $stop = $v[ $col2nr->{'Stop (s)'} ];
131 #my $start = col_v 'Start (s)';
132 #my $stop = col_v 'Stop (s)';
136 warn "# start: $start - stop: $stop\n" if $debug;
143 warn "## fh2 [ $_ ]\n" if $debug;
144 #my @v2 = map { s/^\s+//; $_ } split(/\t/,$_);
145 my @v2 = map { s/^\s+//; $_ } split(/[;,]\s*/,$_);
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";
165 my $i = $h2nr->{$name};
166 die "can't find $name in ",dump( $h2nr ) unless defined $i;
168 die "can't find $i : $name in ",dump( \@v2 ) unless defined $v;
169 return "### col_v2 $name $i = $v";
174 #my $timestamp = $v2[ $h2nr->{'timestamp'} ] || die;
175 #my $timestamp = col_v2 'timestamp';
176 my $timestamp = $v2[2];
178 #$timestamp /= 1000 if $timestamp =~ m/^\d+$/; # FIXME fix corrputed input data
179 #warn "XXX filter $start - $stop from ",dump( $timestamp ); #$h2nr, \@v2 );
181 # Argument "1.324.400" isn't numeric
182 $timestamp =~ s/^(\d+)\.(\d\d\d)\.(\d\d\d)$/$1$2.$3/;
184 if ( $timestamp !~ m/^\d+(\.\d+)$/ ) {
185 warn "timestamp [$timestamp] from $file2 not numeric [$_]";
188 if ( $timestamp < $start ) {
189 #warn "## $start > @v2";
190 } elsif ( $timestamp < $stop ) {
192 foreach my $n ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} ) {
194 #warn "sum $n $sum[$n] $v2[$n]\n";
197 #warn "XXX $count sum $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} = ",dump( \@sum );
199 warn "# seek $tell2" if $debug;
200 seek $fh2, $tell2, 0;
202 die "count is 0" if $count == 0;
204 #warn "XXX $file2 count=$count XXX ", $h2nr->{'AU01_r'},' - ', $h2nr->{'AU45_r'} , " YYY ", $h2nr->{'AU01_c'}, ' - ', $h2nr->{'AU45_c'}, ' sum=', dump( \@sum );
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";
214 warn "# last" if $debug;
225 # prema filenamenu iz $v[0]