lib/App/SeismicUnixGui/misc/count.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 125 CC 7 SCAR 17.3
CC
7
Cov
0%
get_histogram_aref
12.0
set_column
6.0
set_file_name_in
6.0
CRAP 5.6
1 package App::SeismicUnixGui::misc::count;
2  
3 =head1 DOCUMENTATION
4  
5 =head2 SYNOPSIS
6  
7 count
8  
9 PERL PROGRAM NAME: count.pm
10 AUTHOR: Juan Lorenzo
11 DATE: Feb. 2022
12  
13 DESCRIPTION
14 count occurrences of strings in a list
15  
16 BASED ON:
17  
18 =cut
19  
20 =head2 USE
21  
22 =head3 NOTES
23  
24  
25  
26 =head4 Examples
27  
28 =head3 SEISMIC UNIX NOTES
29  
30 =head2 CHANGES and their DATES
31  
32 =cut
33  
34 =head2 Notes from bash
35
36 =cut
37  
38 =head2 declare libraries
39  
40 =cut
41  
42 use Moose;
43 our $VERSION = '0.0.1';
44 use aliased 'App::SeismicUnixGui::misc::L_SU_global_constants';
45 use aliased 'App::SeismicUnixGui::misc::manage_files_by2';
46 use aliased 'App::SeismicUnixGui::configs::big_streams::Project_config';
47 use App::SeismicUnixGui::misc::SeismicUnix
48 qw($cdp $gx $in $out $on $go $to $txt $suffix_ascii $off $offset $su $sx $suffix_su $suffix_txt $tracl);
49  
50 =head2
51  
52 instantiate modules
53  
54 =cut
55  
56 my $Project = Project_config->new();
57 my $get = L_SU_global_constants->new();
58  
59 =head2
60  
61 define local variables
62  
63 =cut
64  
65 my $DATA_SEISMIC_TXT = $Project->DATA_SEISMIC_TXT();
66  
67 my $var = $get->var();
68 my $true = $var->{_true};
69 my $false = $var->{_false};
70  
71 =head2 define private hash
72 to share
73  
74 =cut
75  
76 my @array1;
77 my @array2;
78  
79 my $count = {
80 _column => '',
81 _file_name => '',
82 _suffix_type => '',
83 _file_format => '',
84  
85 # _aref4cc_pt3 => '',
86 # _aref4cc_pt4 => '',
87 # _aref4cc_pt5 => '',
88  
89 };
90  
91 =head2 sub clear
92 all memory
93  
94 =cut
95  
96 sub clear {
97 my $self = @_;
98 $count->{_column} = '';
99 $count->{_file_name} = '';
100 $count->{_suffix_type} = '';
101 $count->{_file_format} = '';
102  
103 # $count->{_aref4cc_pt3} = '';
104 # $count->{_aref4cc_pt4} = '';
105 }
106  
107 =head2 sub set_column
108  
109 =cut
110  
111 sub set_column {
112 my ( $self, $column ) = @_;
113  
114 if ( length $column ) {
115  
116 # print(
117 #"count, set_column, column = $column\n"
118 # );
119  
120 $count->{_column} = $column;
121 }
122 else {
123 print("count, set_column, missing variable\n");
124 }
125  
126 my $result;
127  
128 return ($result);
129  
130 }
131  
132 =head2 sub set_file_name_in
133  
134 =cut
135  
136 sub set_file_name_in {
137 my ( $self, $file_name_in ) = @_;
138  
139 if ( length $file_name_in ) {
140  
141 # print(
142 #"count, set_file_name_in, file_name_in = $file_name_in\n"
143 # );
144  
145 $count->{_file_name_in} = $file_name_in;
146 }
147 else {
148 print("count, set_file_name_in, missing variable\n");
149 }
150  
151 my $result;
152  
153 return ($result);
154  
155 }
156  
157 =head2 sub set_file_format
158  
159 geometry values
160  
161 =cut
162  
163 sub set_file_format {
164 my ( $self, $file_format ) = @_;
165  
166 my $result;
167  
168 if ( length $file_format ) {
169  
170 $count->{_file_format} = $file_format;
171  
172 }
173 else {
174 print("count, set_file_format, missing file_format=$file_format\n");
175 }
176  
177 return ($result);
178 }
179  
180 =head2 sub set_suffix_type
181  
182 geometry values
183  
184 =cut
185  
186 sub set_suffix_type {
187 my ( $self, $suffix_type ) = @_;
188  
189 my $result;
190  
191 if ( length $suffix_type ) {
192  
193 $count->{_suffix_type} = $suffix_type;
194  
195 }
196 else {
197 print("count, set_suffix_type, missing suffix_type=$suffix_type\n");
198 }
199  
200 return ($result);
201 }
202  
203 =head2 sub get_histogram_aref
204  
205 count unique elements
206 output the element value and the number of
207 occurrences
208  
209 =cut
210  
211 sub get_histogram_aref {
212 my ($self) = @_;
213  
214 my $result_aref =();
215  
216 if ( length $count->{_file_name_in}
217 and length $count->{_suffix_type}
218 and length $count->{_column} )
219 {
220  
221 my %hash;
222 my $inbound;
223  
224 my $name_in = $count->{_file_name_in};
225 my $file = manage_files_by2->new();
226 my $suffix_type = $count->{_suffix_type};
227 my $column = $count->{_column};
228 my $column_index = $column - 1;
229  
230 my @value;
231 my @name;
232 my @count;
233 my $i = 0;
234  
235 if ( $suffix_type eq $txt ) {
236  
237 $inbound = $DATA_SEISMIC_TXT . '/' . $name_in . $suffix_txt;
238  
239 }
240 else {
241 print(
242 "get_histogram_aref, count, suffix type is only for $txt\n"
243 );
244 }
245  
246 my $data_aref = $file->get_5cols_aref($inbound);
247  
248 my @data = @$data_aref;
249 my $l = scalar @data;
250 # print("$l, @data\n");
251  
252 # Convert individual array into a hash,
253 # for counting
254 my $array_ref = $data[$column_index];
255 my @array = @{$array_ref};
256  
257 $hash{$_}++ for @array;
258  
259 # sort keys numerically
260  
261 #tested agains groupcount in Matlab JML Feb 2022
262 foreach my $name ( sort { $a <=> $b } keys %hash ) {
263  
264 # print("cdp,fold pairs = $name,$hash{$name}\n");
265 $name[$i] = $name;
266 $count[$i] = $hash{$name};
267 $i++;
268  
269 }
270  
271 my @result = ( \@name, \@count );
272 $result_aref = \@result;
273  
274 }
275 else {
276 print("count, get_histogram_aref, missing value(s)\n");
277 }
278  
279 return($result_aref);
280 }
281  
282 1;