-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-r.pl
executable file
·143 lines (119 loc) · 3.79 KB
/
fix-r.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl -w
########################################
#
# Automatically distribute finds on each grid when granularity of data is too high.
#
# Same as fix-f.pl just for a different grid naming schema (i.e. I12V).
#
# Author: Arjuna Del Toso (http://arjuna.deltoso.net)
#
########################################
use strict;
use warnings;
use List::MoreUtils 'pairwise';
use Text::CSV;
# WINDOWS TO UNIX
# cat -v file.csv
# tr -d '\r' < file.csv > file_unix.csv
my @alphabet = qw(I II III IV V VI VII VIII IX X XI XII XIII XIV XV);
my $csv = Text::CSV->new({ sep_char => ';', binary => 1 });
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";
open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$file' $!\n";
my $num_grids = $ARGV[1] or die "Set the number of grids.\n";
die 'Up to '.scalar @alphabet.' grids are supported' if $num_grids > scalar @alphabet;
my @grid_letters = @alphabet[0 .. $num_grids-1];
my $results = {};
my $linenum = 1;
my $header = '';
while (my $line = <$data>) {
chomp $line;
if ($linenum eq 1) {
$header = $line;
$linenum = $linenum + 1;
next;
}
if ($csv->parse($line)) {
my @fields = $csv->fields();
@fields = map { $_ =~ s/^$/0/g; $_ } @fields;
my $grid = $fields[0];
$grid =~ s/^\s+|\s+$//g;
shift @fields;
if ($grid =~ /^[a-zA-Z]{1}\d+[IV]+$/) {
# Single grid item (examples: I13II or F16V)
&add($grid, $results, @fields);
} else {
# Complex grid item (examples: 127a+b+d or 56+57)
if ($grid =~ /^[a-zA-Z]{1}[0-9]+$/) {
# Grid is the entire macro-grid: H13
# Each value needs to be divided by the cardinality
# of grids and added back to each single grid.
my @divided_fields = ();
foreach my $value (@fields) {
$value = 0 unless $value;
push(@divided_fields, $value/$num_grids);
}
foreach my $letter (@grid_letters) {
&add($grid.$letter, $results, @divided_fields);
}
} elsif ($grid =~ /^([a-zA-Z]{1}[0-9]+)([IVX+]+)$/) {
# Grid is multiple sub-grids: I16II+III+IV
# Each value needs to be divided by the number of
# sub-grids and added back to the proper sub-grids.
my @sub_grid_letters = split /\+/, $2;
my $divisor = scalar @sub_grid_letters || 1;
my @divided_fields = ();
foreach my $value (@fields) {
$value = 0 unless $value;
push(@divided_fields, $value/$divisor);
}
foreach my $letter (@sub_grid_letters) {
&add($1.$letter, $results, @divided_fields);
}
} elsif ($grid =~ /^([a-zA-Z]{1}[0-9+]+)+?$/) {
# Grid is multiple grids: H72+F43
# Each value needs to be divided by the cardinality
# of grids x number of grids in this entry and added
# back to all the grids.
my @single_grids = split /\+/, $grid;
my $divisor = scalar @single_grids || 1;
$divisor = $divisor * $num_grids;
my @divided_fields = ();
foreach my $value (@fields) {
$value = 0 unless $value;
push(@divided_fields, $value/$divisor);
}
foreach my $grid (@single_grids) {
foreach my $letter (@grid_letters) {
&add($grid.$letter, $results, @divided_fields);
}
}
} else {
# Print unexpected grid notation.
print STDERR '>>>>> ', $grid, "\n";
}
}
} else {
warn "Line could not be parsed: $line\n";
}
}
# Print to STDOUT the results
print $header,"\n";
my @keys = sort { lc($a) cmp lc($b) } keys %$results;
foreach my $key (@keys) {
print "$key;".join(';', @{$results->{$key}}),"\n";
}
sub add {
my $grid = shift(@_);
my $results = shift(@_);
my @new_values = @_;
if ($results->{$grid}) {
# grid already in memory, add every value to current ones
my @current_values = @{$results->{$grid}};
no warnings qw(once);
my @sum = pairwise { $a + $b } @current_values, @new_values;
$results->{$grid} = \@sum;
} else {
# new grid, store the values
$results->{$grid} = \@new_values;
}
}