-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvWriter.pl
77 lines (62 loc) · 1.59 KB
/
csvWriter.pl
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
use strict;
use warnings;
use Parse::CSV;
#https://metacpan.org/pod/Parse::CSV
sub sortNumber {
my $arr_ref = shift;
my @sorted = sort {$a <=> $b} @$arr_ref;
return \@sorted;
}
sub getMedian {
my $arr_ref = shift;
my $sorted_ref = sortNumber($arr_ref);
my @sorted = @$sorted_ref;
my $len = $#sorted + 1;
return undef if ($len == 0);
my $center = $len / 2;
if ($len % 2 == 1) {
return $sorted[$center];
} else {
return ($sorted[$center - 1] + $sorted[$center]) / 2;
}
}
sub date_parser {
my $date_string = shift;
my %hash;
my @key = ('month', 'day', 'hour');
my @value = ($date_string =~ /2020.(\d+).(\d+) (\d+):00/);
@hash{@key} = @value;
return \%hash;
}
my @micro = [];
my $month = 4;
my $parser = Parse::CSV->new(
file => "./ripped_data/month$month.csv",
sep_char => ',',
names => 1,
filter => sub { ($_->{micro} eq '0') ? undef : $_ },
);
while (my $row = $parser->fetch ) {
my $date = date_parser($row->{date});
if (($row->{micro} eq 'NA') || ($date->{month} != $month)) {
next;
}
if(!defined $micro[$date->{day}]) {
$micro[$date->{day}] = [];
}
if(!defined $micro[$date->{day}][$date->{hour}]) {
$micro[$date->{day}][$date->{hour}] = [];
}
my $store_by_time_ref = $micro[$date->{day}][$date->{hour}];
push(@$store_by_time_ref, $row->{micro});
}
print "day,hour,data\n";
for(my $day = 1; $day < 32; ++$day) {
for(my $hour = 0; $hour < 24; ++$hour) {
my $ref = $micro[$day][$hour];
if(defined $ref) {
my $data = getMedian(sortNumber($ref));
print "$day,$hour,$data\n";
}
}
}