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.csv");
77 print $out_fh join(",", map { m/\s/ ? qq{"$_"} : $_ } @a),"\n";
84 next if m/^\s*$/; # FIXME corrput data
85 my @v = split(/\t/, $_);
86 warn "#[ $_ ]\n" if $debug;
89 if ( ! defined $id ) {
90 warn "ERROR: ID col 0 null [$_]";
94 if ( ! $last_id || $last_id ne $v[0] ) {
95 my $patt = 'originals_2023-07-07/' . $v[0] . '*.csv';
97 $file2 ||= glob 'originals_2023-07-07/' . $v[0] . '*.csv';
98 $file2 ||= glob 'originals_2023-07-07/' . unac_string('utf-8', $v[0] ) . '*.csv';
99 $file2 ||= glob_id $v[0];
100 # try to find file with same numbers
103 $file2 ||= glob_id $id_nrs;
105 if ( ! defined $file2 ) {
106 warn "SKIP ERROR: $patt glob didn't find anything for v[0]=$v[0] [$_]";
107 print $skipped_input "$_\n";
112 warn "ERROR: $patt glob file $file2 not readable: $! SKIPPING [$_]";
113 print $skipped_input "$_\n";
117 warn "# file2 $file2 [$v[0]]";
118 open($fh2, '<', $file2);
125 my $i = $col2nr->{$name};
126 die "can't find $name" unless defined $i;
128 die "can't find $i : $name in ",dump( \@v ) unless defined $v;
129 warn "## col_v $name -> $i -> $v",dump( \@v ) if $debug > 1;
133 #my $start = $v[ $col2nr->{'Start (s)'} ];
134 #my $stop = $v[ $col2nr->{'Stop (s)'} ];
136 #my $start = col_v 'Start (s)';
137 #my $stop = col_v 'Stop (s)';
141 warn "# start: $start - stop: $stop\n" if $debug;
148 warn "## fh2 [ $_ ]\n" if $debug;
149 #my @v2 = map { s/^\s+//; $_ } split(/\t/,$_);
150 my @v2 = map { s/^\s+//; $_ } split(/[;,]\s*/,$_);
155 warn "# h2nr ",dump( $h2nr ) if $debug;
156 if ( ! @out_header ) {
157 push @out_header, @cols;
158 push @out_header, map { $h2[$_] }
159 ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} );
160 push @out_header, map { $h2[$_] }
161 ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} );
162 warn "# out_header ",dump( \@out_header );
170 my $i = $h2nr->{$name};
171 die "can't find $name in ",dump( $h2nr ) unless defined $i;
173 die "can't find $i : $name in ",dump( \@v2 ) unless defined $v;
174 return "### col_v2 $name $i = $v";
179 #my $timestamp = $v2[ $h2nr->{'timestamp'} ] || die;
180 #my $timestamp = col_v2 'timestamp';
181 my $timestamp = $v2[2];
183 #$timestamp /= 1000 if $timestamp =~ m/^\d+$/; # FIXME fix corrputed input data
184 #warn "XXX filter $start - $stop from ",dump( $timestamp ); #$h2nr, \@v2 );
186 # Argument "1.324.400" isn't numeric
187 $timestamp =~ s/^(\d+)\.(\d\d\d)\.(\d\d\d)$/$1$2.$3/;
189 if ( $timestamp !~ m/^\d+(\.\d+)$/ ) {
190 warn "timestamp [$timestamp] from $file2 not numeric [$_]";
193 if ( $timestamp < $start ) {
194 #warn "## $start > @v2";
195 } elsif ( $timestamp < $stop ) {
197 foreach my $n ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} ) {
199 #warn "sum $n $sum[$n] $v2[$n]\n";
202 #warn "XXX $count sum $h2nr->{'AU01_r'} .. $h2nr->{'AU45_c'} = ",dump( \@sum );
204 warn "# seek $tell2" if $debug;
205 seek $fh2, $tell2, 0;
207 die "count is 0" if $count == 0;
209 #warn "XXX $file2 count=$count XXX ", $h2nr->{'AU01_r'},' - ', $h2nr->{'AU45_r'} , " YYY ", $h2nr->{'AU01_c'}, ' - ', $h2nr->{'AU45_c'}, ' sum=', dump( \@sum );
211 push @add_cols, map { $sum[$_] / $count } ( $h2nr->{'AU01_r'} .. $h2nr->{'AU45_r'} );
212 push @add_cols, map { $sum[$_] } ( $h2nr->{'AU01_c'} .. $h2nr->{'AU45_c'} );
213 warn "# add_cols = ",dump( \@add_cols ) if $debug;
214 out_csv @v, @add_cols;
219 warn "# last" if $debug;
230 # prema filenamenu iz $v[0]