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

filter: Statementstmt 0.0% 0 / 233 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 367 CC 25 SCAR 31.2
CC
25
Cov
0%
configs
72.0
cfg
30.0
cols_1p
12.0
CRAP 22.6
1 package App::SeismicUnixGui::misc::readfiles;
2  
3 use Moose;
4  
5 =head1 DOCUMENTATION
6  
7 =head2 SYNOPSIS
8  
9 PERL PROGRAM NAME: readfiles
10 AUTHOR: Juan Lorenzo
11 DATE: Oct 29 2012
12 DESCRIPTION read file operations
13 Version 1use aliased 'App::SeismicUnixGui::misc::control'; # '0.0.3'
14 2 May 31 2016, Mom's 83 birthday
15 2.1 June 29, 2016
16 Add reading configuration file hashes
17  
18 Notes:
19 Package name is the same as the file name
20 Moose is a package that allows an object-oriented
21 syntax to organizing your programs
22  
23 STEPS ARE:
24  
25 USAGE 1
26  
27 Read a file with one colume of text
28 Read each line
29  
30 Example
31 $readfiles->ref_file($list);
32 $readfiles-Step();
33 =cut
34  
35 use aliased 'App::SeismicUnixGui::misc::L_SU_global_constants';
36 use App::SeismicUnixGui::misc::control '0.0.3';
37 use aliased 'App::SeismicUnixGui::misc::control';
38  
39 my $get = L_SU_global_constants->new();
40  
41 # my $alias_superflow_name = $get->alias_superflow_names_h;
42 #print("readfiles, alias_superflow_name, fk is $alias_superflow_name->{fk}\n");
43 my $global_libs = $get->global_libs;
44  
45 # my $alias_PV = $alias_superflow_name->{_ProjectVariables};
46 # my $alias_superflow_config_name = $get->alias_superflow_config_names_aref();
47  
48 #WARNING---- watch out for missing underscore!!
49 # print("readfiles,fk alias_superflow_config_name : $$alias_superflow_config_name[0]\n");
50  
51 my $var = $get->var();
52 my $empty_string = $var->{_empty_string};
53  
54 my $readfiles = {
55 _note => '',
56 _ref_file => '',
57 _file_name => '',
58 _parameter => '',
59 _path => '',
60 _program_name => '',
61 _program_name_config => '',
62 _skip_lines => '',
63 _sunix_param_path => $global_libs->{_param},
64 _value => '',
65 _Step => '',
66 };
67  
68 =head2 sub clear:
69  
70 clean hash of its values
71  
72 =cut
73  
74 sub clear {
75 $readfiles->{_note} = '';
76 $readfiles->{_ref_file} = '';
77 $readfiles->{_file_name} = '';
78 $readfiles->{_parameter} = '';
79 $readfiles->{_path} = '';
80 $readfiles->{_program_name} = '';
81 $readfiles->{_program_name_config} = '';
82 $readfiles->{_skip_lines} = '';
83 $readfiles->{_program_name} = '';
84 $readfiles->{_value} = '';
85 $readfiles->{_Step} = '';
86 }
87  
88 =head2 sub cols_1
89  
90 read files cols 1 in a text file
91 open the file of interest
92 first item has index=1
93  
94 =cut
95  
96 sub cols_1 {
97  
98 my ( $variable, $ref_origin ) = @_;
99  
100 =pod
101  
102 declare local variables
103  
104 =cut
105  
106 my $line;
107  
108 =pod
109  
110 counter, a number, row number
111  
112 =cut
113  
114 my $i = 1;
115 my ( $x, $num_rows );
116 my (@OFFSET);
117  
118 if ($ref_origin) {
119 $readfiles->{_ref_file} = $ref_origin;
120  
121 # print ("\n readfiles, cols_1, The input file is called $ref_origin\n");
122 open( FILE, $readfiles->{_ref_file} )
123 || print("Can't open file_name, $!\n");
124  
125 =pod
126  
127 read contents of list, e.g. shotpoint geometry file, file names etc.
128
129 =cut
130  
131 while ( $line = <FILE> ) {
132  
133 # print("readfiles, col1,--$line--\n");
134 chomp($line);
135 my ($x) = $line;
136 $OFFSET[$i] = $x;
137  
138 # print("\n Reading 1 col file:$OFFSET[$i]\n");
139 $i = $i + 1;
140 }
141  
142 close(FILE);
143  
144 $num_rows = $i - 1;
145  
146 #print out the number of lines of data for the user
147 #print ("This file contains $num_rows rows of data\n\n\n");
148  
149 }
150 return ( \@OFFSET, $num_rows );
151  
152 }
153  
154 =head2 sub cols_1p
155  
156 read files cols 1 in a text file
157 open the file of interest
158 1st item has index=0
159 exclude empty lines
160  
161 =cut
162  
163 sub cols_1p {
164  
165 my ( $variable, $ref_origin ) = @_;
166  
167 =pod
168  
169 declare local variables
170  
171 =cut
172  
173 my $line;
174  
175 =pod
176  
177 counter, a number, row number
178  
179 =cut
180  
181 my $i = 0;
182 my ( $x, $num_rows );
183 my (@OFFSET);
184  
185 if ($ref_origin) {
186 $readfiles->{_ref_file} = $ref_origin;
187  
188 # print ("\n readfiles, cols_1, The input file is called $ref_origin\n");
189 open( FILE, $readfiles->{_ref_file} )
190 || print("Can't open file_name, $!\n");
191  
192 =pod
193  
194 read contents of list, e.g. shotpoint geometry file, file names etc.
195
196 =cut
197  
198 while ( $line = <FILE> ) {
199  
200 # print("readfiles, col1,--$line--\n");
201 chomp($line);
202  
203 if ( $line ne $empty_string ) {
204  
205 my ($x) = $line;
206 $OFFSET[$i] = $x;
207  
208 # print("Reading 1 col file:$OFFSET[$i]\n");
209 $i = $i + 1;
210 }
211  
212 }
213  
214 close(FILE);
215  
216 $num_rows = $i;
217  
218 # print out the number of lines of data for the user
219 # print ("This file contains $num_rows rows of data\n\n\n");
220  
221 }
222 return ( \@OFFSET, $num_rows );
223  
224 }
225  
226 =head2 sub cols_2p
227  
228 reads cols 1 and 2 in a text file
229  
230 =cut
231  
232 sub cols_2p {
233  
234 my ( $files, $ref_file ) = @_;
235  
236 $readfiles->{_ref_file} = $$ref_file if defined($ref_file);
237  
238 #print '$$ref_file= '.$$ref_file."\n\n";
239  
240 # open the file of interest
241 open( FILE, $$ref_file ) || print("Can't open file_name, $!\n");
242  
243 # set the counter
244 # uses the perl convention -- first index is 0
245 my $i = 0;
246 my ( @TIME, @OFFSET );
247  
248 # readfiles contents of shotpoint geometry file
249 while ( my $line = <FILE> ) {
250  
251 #print("line $i is $line");
252 chomp($line);
253 my ( $t, $x ) = split( " +", $line );
254 $TIME[$i] = $t;
255 $OFFSET[$i] = $x;
256 $i = $i + 1;
257 }
258 close(FILE);
259  
260 my $max_index = $i - 1;
261  
262 # print ("\nThis file contains ($max_index +1) row(s) of data\n");
263  
264 return ( \@TIME, \@OFFSET, $max_index );
265 }
266  
267 =head2 sub cols_2
268  
269 reads cols 1 and 2 in a text file
270
271 =cut
272  
273 sub cols_2 {
274  
275 my ( $files, $ref_file ) = @_;
276 $readfiles->{_ref_file} = $$ref_file if defined($ref_file);
277  
278 #print '$$ref_file= '.$$ref_file."\n\n";
279 # open the file of interest
280 open( FILE, $$ref_file ) || print("Can't open file_name, $!\n");
281  
282 #set the counter
283 my $i = 1;
284 my ( @TIME, @OFFSET );
285  
286 # readfiles contents of shotpoint geometry file
287 while ( my $line = <FILE> ) {
288  
289 #print("\n$line");
290 chomp($line);
291 my ( $t, $x ) = split( " +", $line );
292 $TIME[$i] = $t;
293 $OFFSET[$i] = $x;
294 $i = $i + 1;
295 }
296 close(FILE);
297  
298 my $num_rows = $i - 1;
299 my $rows = $num_rows;
300  
301 #print ("\nThis file contains $num_rows row(s) of data\n");
302  
303 # to prevent contaminating outside variables
304 my @TIME_OUT = @TIME;
305 my @OFFSET_OUT = @OFFSET;
306 return ( \@TIME_OUT, \@OFFSET_OUT, $rows );
307  
308 }
309  
310 #=head2 sub sunix_params
311 #
312 #sub sunix_params {
313 # my ($self,$hash_ref) = @_;
314 # my $prog_name = $hash_ref;
315 # $ref_cfg = defaults($prog_name);
316 # $size = ((scalar @$ref_cfg) )/2;
317 #
318 # for ($k=0,$i=0; $k < $size; $k++,$i=$i+2) {
319 # $param[$k] = @$ref_cfg[$i];
320 # $j = $i + 1;
321 # $values[$k] = @$ref_cfg[$j];
322 # if($values[$k] eq $nu) {
323 # $checkbutton_on_off[$k] = $off;
324 # }
325 # else {
326 # $checkbutton_on_off[$k] = $on;
327 # }
328 # }
329 #
330 # return(\@param,\@values,\@checkbutton_on_off);
331 #}
332 #
333 #=cut
334 #=head2 sub config
335 #deprecated in L_SU V0.3.6
336 #
337 # input is hash key and value pair
338 # Simple reads configuration file and
339 # cretes a hash with parameters (keys/names) and
340 # their values as assigned inside the configuration
341 # file
342 #
343 # Debug with
344 #
345 # print("1. program name is $program_name\n\n");
346 #
347 # local values of the programs have priority
348 #
349 # TODO: objectify name changes and
350 # variable setting too!
351 #
352 # Load variables from local configuration file
353 #
354 # tests: does this file Project_variables.config
355 # exist?
356 # from ./Project_variables.config
357 # my %values = %{$cfg->vars()};
358 # my $hash_size = keys %values;
359 # print ("hash size is $hash_size\n\n");
360 # print(" program name is $LSU->{_tool_name} \n\n");
361 # print(" program name is $choice \n\n");
362 #else { # all other normal programs whose configuration
363 # #file resides locally
364 # #$cfg = Config::Simple($readfiles->{_program_name_config});
365 # } #test for iVA2.config
366 #
367 #
368 # print("progr name is $program_name\n");
369 #
370 #=cut
371 #
372 #sub config {
373 #
374 # my ( $self, $program_name ) = @_;
375 # my @CFG;
376 # my $ref_CFG;
377 # my $cfg;
378 # use Moose;
379 our $VERSION = '0.0.1';
380  
381 # # use Config::Simple;
382 # use name;
383 #use App::SeismicUnixGui::misc::control '0.0.3';
384 #use aliased 'App::SeismicUnixGui::misc::control';
385 # my $name = name->new();
386 # my $control = control->new();
387 #
388 # if ( defined $program_name ) {
389 # $readfiles->{_program_name_config} =
390 # $name->change_config($program_name);
391 # if ( -e "./$readfiles->{_program_name_config}" ) {
392 #
393 #=pod
394 #
395 # Values taken from the simple,local
396 # file: iVA2.config called
397 # LOCAL VARIABLES FOR THIS PROJECT
398 # file_name = 'All_cmp'
399 # cdp_first = 15
400 # cdp_inc = 1
401 # cdp_last = 100
402 # data_scale = 1
403 # freq = '0,3,100,200'
404 # number_of_velocities = 300
405 # first_velocity = 3
406 # velocity_increment = 10
407 # min_semblance = .0
408 # max_semblance = .75
409 #
410 # print("home is $CFG[0]\n\n");
411 # print("Project home is $CFG[1]\n\n");
412 #
413 #=cut
414 #
415 # if ( $readfiles->{_program_name_config} eq 'iVA2.config' ) {
416 # $cfg = Config::Simple( $readfiles->{_program_name_config} );
417 # $CFG[0] = "file_name";
418 # $CFG[1] = $cfg->param("file_name");
419 # $CFG[2] = "cdp_first";
420 # $CFG[3] = $cfg->param("cdp_first");
421 # $CFG[4] = "cdp_inc";
422 # $CFG[5] = $cfg->param("cdp_inc");
423 # $CFG[6] = "cdp_last";
424 # $CFG[7] = $cfg->param("cdp_last");
425 # $CFG[8] = "data_scale";
426 # $CFG[9] = $cfg->param("data_scale");
427 # $CFG[10] = "freq";
428 # $CFG[11] = $cfg->param("freq");
429 # $CFG[12] = "number_of_velocities";
430 # $CFG[13] = $cfg->param("number_of_velocities");
431 # $CFG[14] = "first_velocity";
432 # $CFG[15] = $cfg->param("first_velocity");
433 # $CFG[16] = "velocity_increment";
434 # $CFG[17] = $cfg->param("velocity_increment");
435 # $CFG[18] = "min_semblance";
436 # $CFG[19] = $cfg->param("min_semblance");
437 # $CFG[20] = "max_semblance";
438 # $CFG[21] = $cfg->param("max_semblance");
439  
440 #=podfor iVA
441 #
442 # package control corrects for missing commas in number strings
443 # and file names with a suffix
444 #
445 #=cut
446 #
447 # #print(" 1. readfiles,config, for iVA2, freq is $CFG[11]\n\n");
448 # #print(" 1. readfiles,config, for iVA2, file_name is $CFG[1]\n\n");
449 # $CFG[11] = $control->commas( \$CFG[11] );
450 # $CFG[1] = $control->su_file_name( \$CFG[1] );
451 #
452 # #print(" 2. readfiles,config, for iVA2, freq is $CFG[11]\n\n");
453 # #print(" 1. readfiles,config, for iVA2, file_name is $CFG[1]\n\n");
454 #
455 # }
456 #
457 # if ( $readfiles->{_program_name_config} eq
458 # ( $$alias_superflow_config_name[0] . '.config' ) )
459 # {
460 #
461 ## print("readfiles,fk alias_superflow_config_name : $$alias_superflow_config_name[0]\n");
462 # $cfg = Config::Simple( $readfiles->{_program_name_config} );
463 # $CFG[0] = "file_name";
464 # $CFG[1] = $cfg->param("file_name");
465 # $CFG[2] = "sudipfilter_1_dt";
466 # $CFG[3] = $cfg->param("sudipfilter_1_dt");
467 # $CFG[4] = "sudipfilter_1_dx";
468 # $CFG[5] = $cfg->param("sudipfilter_1_dx");
469 # $CFG[6] = "sudipfilter_1_slopes";
470 # $CFG[7] = $cfg->param("sudipfilter_1_slopes");
471 # $CFG[8] = "sudipfilter_1_bias";
472 # $CFG[9] = $cfg->param("sudipfilter_1_bias");
473 # $CFG[10] = "sudipfilter_1_amps";
474 # $CFG[11] = $cfg->param("sudipfilter_1_amps");
475 # $CFG[12] = "sudipfilter_2_dt";
476 # $CFG[13] = $cfg->param("sudipfilter_2_dt");
477 # $CFG[14] = "sudipfilter_2_dx";
478 # $CFG[15] = $cfg->param("sudipfilter_2_dx");
479 # $CFG[16] = "sudipfilter_2_slopes";
480 # $CFG[17] = $cfg->param("sudipfilter_2_slopes");
481 # $CFG[18] = "sudipfilter_2_bias";
482 # $CFG[19] = $cfg->param("sudipfilter_2_bias");
483 # $CFG[20] = "sudipfilter_2_amps";
484 # $CFG[21] = $cfg->param("sudipfilter_2_amps");
485 # $CFG[22] = "sufilter_1_freq";
486 # $CFG[23] = $cfg->param("sufilter_1_freq");
487 # $CFG[24] = "sufilter_1_amplitude";
488 # $CFG[25] = $cfg->param("sufilter_1_amplitude");
489 # $CFG[26] = "suspecfk_1_dt";
490 # $CFG[27] = $cfg->param("suspecfk_1_dt");
491 # $CFG[28] = "suspecfk_1_dx";
492 # $CFG[29] = $cfg->param("suspecfk_1_dx");
493 # $CFG[30] = "suwind_1_tmin";
494 # $CFG[31] = $cfg->param("suwind_1_tmin");
495 # $CFG[32] = "suwind_1_tmax";
496 # $CFG[33] = $cfg->param("suwind_1_tmax");
497 # $CFG[34] = "suwind_2_key";
498 # $CFG[35] = $cfg->param("suwind_2_key");
499 # $CFG[36] = "suwind_2_min";
500 # $CFG[37] = $cfg->param("suwind_2_min");
501 # $CFG[38] = "suwind_2_max";
502 # $CFG[39] = $cfg->param("suwind_2_max");
503 # $CFG[40] = "TOP_LEFT_sugain_pbal_switch";
504 # $CFG[41] = $cfg->param("TOP_LEFT_sugain_pbal_switch");
505 # $CFG[42] = "TOP_LEFT_sugain_pbal_switch";
506 # $CFG[43] = $cfg->param("TOP_LEFT_sugain_pbal_switch");
507 # $CFG[44] = "TOP_LEFT_sugain_pbal_switch";
508 # $CFG[45] = $cfg->param("TOP_LEFT_sugain_pbal_switch");
509 # $CFG[46] = "TOP_LEFT_sugain_pbal_switch";
510 # $CFG[47] = $cfg->param("TOP_LEFT_sugain_pbal_switch");
511 #
512 #=pod
513 #
514 # Config::Simple interprets configuration values improperly
515 # for cases of numbers separated by commas
516 # Also, filename is file, without a suffix!
517 #
518 #=cut
519 #
520 # $CFG[1] = $control->su_file_name( \$CFG[1] );
521 # $CFG[7] = $control->commas( \$CFG[7] );
522 # $CFG[11] = $control->commas( \$CFG[11] );
523 # $CFG[17] = $control->commas( \$CFG[17] );
524 # $CFG[21] = $control->commas( \$CFG[21] );
525 # $CFG[23] = $control->commas( \$CFG[23] );
526 # $CFG[25] = $control->commas( \$CFG[25] );
527 # }
528 # if ( $readfiles->{_program_name_config} eq 'Project' . '.config' ) {
529 #
530 # }
531 #
532 # if ( $readfiles->{_program_name_config} eq $alias_PV . '.config' ) {
533 #
534 # $cfg = Config::Simple( $readfiles->{_program_name_config} );
535 #
536 #=head2
537 #
538 # contains all the configuration variables in
539 # perl script
540 # HOME ='/home/gom';
541 # PROJECT_HOME = '/FalseRiver';;
542 # site = 'Bueche';
543 # spare_dir = '';
544 # date = '051216';
545 # component = 'H';
546 # line = '1';
547 # subUser = 'gom';
548 # geomaps = 'no'
549 # print("home is $CFG[0]\n\n");
550 # print("Project home is $CFG[1]\n\n");
551 # $ref_CFG = default_Tkcfg($program_name);
552 # $cfg -> write($CFG);
553 # ($ref_labels_w,$ref_values_w) = disappear(\@param,\@values,$entries);
554 #
555 #=cut
556 #
557 # $CFG[0] = "HOME";
558 # $CFG[1] = $cfg->param("HOME");
559 # $CFG[2] = "PROJECT_HOME";
560 # $CFG[3] = $cfg->param("PROJECT_HOME");
561 # $CFG[4] = "site";
562 # $CFG[5] = $cfg->param("site");
563 # $CFG[6] = "spare_dir";
564 # $CFG[7] = $cfg->param("spare_dir");
565 # $CFG[8] = "date";
566 # $CFG[9] = $cfg->param("date");
567 # $CFG[10] = "component";
568 # $CFG[11] = $cfg->param("component");
569 # $CFG[12] = "line";
570 # $CFG[13] = $cfg->param("line");
571 # $CFG[14] = "subUser";
572 # $CFG[15] = $cfg->param("subUser");
573 # $CFG[14] = "geomaps";
574 # $CFG[15] = $cfg->param("geomaps");
575 #
576 #=pod
577 #
578 # package control corrects for empty string
579 #
580 #=cut
581 #
582 # $CFG[7] = $control->empty_string( \$CFG[7] );
583 #
584 # }
585 # else { # all other normal programs whose configuration
586 # #file resides locally
587 # #$cfg = Config::Simple($readfiles->{_program_name_config});
588 # } #test for Project_Variables.config
589 # }
590 # else {
591 # print(
592 # "file $readfiles->{_program_name} should exist in current directory\n"
593 # );
594 # print(
595 # "A new file:$readfiles->{_program_name}.config will be created in the local directory\n"
596 # );
597 #
598 # }
599 #
600 # return ( \@CFG );
601 #
602 # } # test entries
603 #} #sub config
604  
605 #=head2 sub defaults
606 #
607 # Read a default specification file
608 # Debug with
609 # print ("self is $self,program is $program_name\n");
610 # print("params are @$ref_CFG\n");
611 # program name is a hash
612 # print("params are @$ref_cfg\n");
613 # print ("self is $self,program is $program_name\n");
614 #
615 #=cut
616 #
617 # sub defaults {
618 # my ($self,$program_name) = @_ ;
619 # my ($ref_cfg,$size);
620 # use su_param;
621 # my $su_param = su_param->new();
622 # if (defined $program_name) {
623 # ($ref_cfg) = $su_param->get($program_name);
624 # $size = $su_param->size($program_name);
625 # return ($ref_cfg);
626 # }
627 # }
628 #
629  
630 =head2 sub cfg
631  
632 file is a character array scalar
633 actually a scalar reference to a string
634  
635 # my $file1 = '/usr/local/pl/System_Variables_config.pl';
636 # print("readfiles,cfg,HOME=$CFG->{Project_Variables}{1}{HOME}\n")
637 # print ("readfiles,cfg,Doing $file \n\n");
638 # print ("A hash: variables $variables \n\n");
639 # #print ("log is $CFG->{log}{level} \n\n");
640 # ## Check for errors
641 #my $file = '/usr/local/pl/System_Variables_config.pl';
642 # my $file1 = '/usr/local/pl/Sudipfilt_config.pl';
643  
644 =cut
645  
646 sub cfg {
647  
648 my ( $variables, $file_char ) = @_;
649  
650 #print("1. readfiles,cfg,variables=$variables,file_char=$file_char\n");
651 if ($file_char) {
652 my $file = $file_char;
653 our $err;
654 { # Put config file data into the namespace
655 # of this package
656  
657 package CFG;
658  
659 # Run $file
660 # $file internally shares
661 # global $CFG as a hash
662 our $CFG;
663 our $rc = do($file);
664  
665 # print("2. readfiles,cfg,file_name=$CFG->{file_name}\n");
666 if ($@) {
667 $::err = "ERROR: Failure compiling '$file' - $@";
668 }
669 elsif ( !defined($rc) ) {
670 $::err = "ERROR: Failure reading '$file' - $!";
671 }
672 elsif ( !$rc ) {
673 $::err = "ERROR: Failure processing '$file'";
674 }
675 return ( $err, $CFG );
676 }
677  
678 }
679 }
680  
681 =head2 sub configs
682  
683 read configuration parameter files
684 for seismic unix modules
685 and for tool (superflow) modules
686 (not for user-built flows)
687  
688 TODO: since sub configs= sub params
689 ONLY NEED TO USE ONE OF THEM
690 AMONG ANY PROGRAM
691  
692 =cut
693  
694 sub configs {
695 my ( $self, $program ) = @_;
696  
697 my @parameter;
698 my @value;
699  
700 if ( length $program ) {
701  
702 my $control = control->new();
703 my ( $this, $eq );
704  
705 $this = $program;
706  
707 # print("readfiles,configs,this program is:$this\n");
708 my ($t, $t_whole, $x, $line, $max_index );
709
710 my $index=0;
711  
712 open( my $IN, '<', $this )
713 or die "readfiles,configs: Can't open parameter file: '$this' $!";
714  
715 =pod
716  
717 set the counter
718 uses the perl convention -- first index is 0
719 readfiles contents of a file
720 print ("\nThis file contains ($max_index +1) row(s) of data\n");
721 print("line $i is $line");
722 print ("\nThis file contains $num_rows row(s) of data\n");
723 regex:
724 trim white spaces from both ends
725 split on '=' and its surrounding spaces
726
727 control does the following:
728 # 1. remove double quotes if they exist
729 # anywhere in the line
730 $x =~ tr/"//d;
731
732 # 2. remove extra single quotes if they exist at the start of the string
733 $x =~ s/^'//;
734
735 # 3. remove extra single quotes if they exist at the end of the string
736 $x =~ s/'$//;
737 # print("after removing only a last single quote: $x\n ");
738 # 4. determine whether we have a string or a number
739 my $fmt = 0;
740 $fmt = looks_like_number($x);
741
742 if ($fmt) {
743 # printf("$x looks like a number \n");
744 # do nothing
745
746 } else {
747 # printf("$x does not look like a number \n");
748 my $x_as_string = '\''.$x.'\'';
749 print (" Made $x into a string: $x_as_string\n");
750 $x = $x_as_string;
751 }
752  
753 =cut
754  
755 while ( $line = <$IN> ) {
756  
757 # print("1. readfiles,configs:raw line $index is $line\n");
758 chomp($line);
759  
760 # skip lines starting where first non-white character is
761 # modify 'm' starts ^ and ends $ as pertaining to each line and not each file
762 # print("2. readfiles,configs:chomped line num is $line\n");
763 next if $line =~ /^\s*#/m;
764  
765 # print("3.0 readfiles,configs:these lines have no starting '#' is $line\n");
766  
767 # trim white spaces from both ends
768 $line =~ s/^\s+|\s+$//g;
769  
770 # print("3-1. readfiles,configs white spaces removed from ends,line is $line\n");
771  
772 # split line using =
773 ( $t_whole, $x ) = split( /\s+=\s*/, $line );
774 $t = $t_whole; # redundant in long run unless the
775 # following changes
776  
777 # print("3-1b. readfiles,configs $t\t$x \n");
778  
779 $x = $control->get_no_quotes($x);
780  
781 # print("3-1b. readfiles,configs $t\t$x \n");
782  
783 $x = $control->get_string_or_number($x);
784  
785 # print("3-1c. readfiles,configs $t\t$x \n");
786  
787 # only print out lines that are not empty
788 if ($t) {
789  
790 # print("3-2. readfiles,configs, line: $line\n parameter name, value: $t,$x\n");
791  
792 # save value and increment index
793 # only if there is something to keep
794  
795 # test for "bad" x values
796 if ( not defined $x ) {
797  
798 # print( "readfiles,configs, x is undefined\n");
799 # print(" replace x=$x with ''\n");
800 $x = '';
801  
802 }
803 elsif ( $x eq "''" ) {
804  
805 # print( "readfiles,configs, x is --$x--i.e., an empty string\n");
806 # print(" replace x=$x with ''\n");
807 }
808 elsif ( $x =~ /^ *$/ ) {
809  
810 # print( "readfiles,configs, x contains 0 or more spaces\n");
811 # print(" replace x=$x with ''\n");
812 $x = '';
813 }
814 elsif ( $x eq "'nu'" ) {
815 $x = '';
816  
817 # print(" replace x=$x with ''\n");
818 }
819  
820 # assume ALL bad x values have been caught , including x=0
821  
822 # print("5-1. readfiles,configs:parameter name, value : $t,$x\n");
823 $parameter[$index] = $t;
824 $value[$index] = $x;
825  
826 # print(
827 # "5-2. index=$index; readfiles,configs: parameter,value :
828 # --$parameter[$index]--,--$value[$index]--\n"
829 # );
830 } # only non-empty lines
831
832 $index++;
833 } # end of while
834  
835 # my $long = scalar @parameter;
836 # print("L 837 long = $long\n");
837 # N.B. Read many times
838 close($IN);
839 $readfiles->{_parameter} = \@parameter;
840 $readfiles->{_value} = \@value;
841  
842 return ( \@parameter, \@value );
843  
844 } # end program
845  
846 } # end sub
847  
848 =head2 sub get_cols_3
849  
850 reads cols 1 through 3 in a text file
851
852 =cut
853  
854 sub get_cols_3 {
855  
856 my ($self) = @_;
857  
858 if ( $readfiles->{_skip_lines} ne $empty_string
859 && $readfiles->{_file_name} ne $empty_string
860 && $readfiles->{_path} ne $empty_string )
861 {
862  
863 my $num_lines2skip = $readfiles->{_skip_lines};
864 my $inbound_fh;
865 my $inbound = $readfiles->{_path} . '/' . $readfiles->{_file_name};
866  
867 print("readfiles,get_cols_3 inbound = $inbound\n");
868  
869 open( $inbound_fh, '<', $inbound )
870 || print(
871 "readfiles,cols_8,Can't open $readfiles->{_file_name}, $!\n");
872  
873 # set the counter
874 my $ip_counter = 1;
875 my $op_counter = 0;
876 my $lines;
877 my ( @A, @B, @C );
878  
879 # read contents of file
880 while ( $lines = <$inbound_fh> ) {
881  
882 if ( $ip_counter > $num_lines2skip ) {
883  
884 #print("$lines");
885 chomp($lines);
886 my ( $a, $b, $c ) = split( " ", $lines );
887  
888 # print("\n $a $b, $c \n");
889  
890 $A[$op_counter] = $a;
891 $B[$op_counter] = $b;
892 $C[$op_counter] = $c;
893  
894 $op_counter++;
895  
896 }
897  
898 $ip_counter++;
899 }
900  
901 # number of geophones stations in file
902 my $num_rows = $op_counter;
903  
904 print(
905 "readfiles, cols_3, The input file contains $num_rows row(s)\n\n");
906  
907 # close the file of interest
908 close($inbound_fh);
909  
910 my @array_refs = ( \@A, \@B, \@C );
911  
912 return ( \@array_refs );
913  
914 }
915 else {
916 print(
917 "readfiles,cols_3,missing either #skip_lines, path or file_name\n");
918 print("file_name = $readfiles->{_file_name}\n");
919 print("path = $readfiles->{_path}\n");
920 print("number of skipped lines = $readfiles->{_skip_lines}\n");
921 return ();
922 }
923  
924 }
925  
926 =head2 sub get_cols_8
927  
928 reads cols 1 through 7 in a text file
929
930 =cut
931  
932 sub get_cols_8 {
933  
934 my ($self) = @_;
935  
936 if ( $readfiles->{_skip_lines} ne $empty_string
937 && $readfiles->{_file_name} ne $empty_string
938 && $readfiles->{_path} ne $empty_string )
939 {
940  
941 my $num_lines2skip = $readfiles->{_skip_lines};
942 my $inbound_fh;
943 my $inbound = $readfiles->{_path} . '/' . $readfiles->{_file_name};
944  
945 print("readfiles,cols_8 inbound = $inbound\n");
946  
947 open( $inbound_fh, '<', $inbound )
948 || print(
949 "readfiles,cols_8,Can't open $readfiles->{_file_name}, $!\n");
950  
951 # set the counter
952 my $ip_counter = 1;
953 my $op_counter = 0;
954 my $lines;
955 my ( @A, @B, @C, @D, @E, @F, @G, @H );
956  
957 # read contents of file
958 while ( $lines = <$inbound_fh> ) {
959  
960 if ( $ip_counter > $num_lines2skip ) {
961  
962 #print("$lines");
963 chomp($lines);
964 my ( $a, $b, $c, $d, $e, $f, $g, $h ) = split( " ", $lines );
965  
966 # print("\n $a $b, $c, $d, $e, $f, $g,$h \n");
967  
968 $A[$op_counter] = $a;
969 $B[$op_counter] = $b;
970 $C[$op_counter] = $c;
971 $D[$op_counter] = $d;
972 $E[$op_counter] = $e;
973 $F[$op_counter] = $f;
974 $G[$op_counter] = $g;
975 $H[$op_counter] = $h;
976  
977 $op_counter++;
978  
979 }
980  
981 $ip_counter++;
982 }
983  
984 # number of geophones stations in file
985 my $num_rows = $op_counter;
986  
987 print(
988 "readfiles, cols_8, The input file contains $num_rows row(s)\n\n");
989  
990 # close the file of interest
991 close($inbound_fh);
992  
993 my @array_refs = ( \@A, \@B, \@C, \@D, \@E, \@F, \@G, \@H );
994  
995 return ( \@array_refs );
996  
997 }
998 else {
999 print(
1000 "readfiles,cols_8,missing either #skip_lines, path or file_name\n");
1001 print("file_name = $readfiles->{_file_name}\n");
1002 print("path = $readfiles->{_path}\n");
1003 print("number of skipped lines = $readfiles->{_skip_lines}\n");
1004 return ();
1005 }
1006  
1007 }
1008  
1009 =head2 sub params
1010  
1011 read parameter files
1012 for seismic unix modules
1013 print("readfiles,params,this:$this\n");
1014  
1015 =cut
1016  
1017 sub params {
1018 my ( $self, $program ) = @_;
1019 if ($program) {
1020 my ( @parameter, @value );
1021 my ( $this, $eq );
1022  
1023 $this = $program;
1024 my ( $i, $t, $x, $line, $max_index );
1025 open( FILE, $this ) || print(
1026 "readfiles,params: Can't open parameter file, $!\n
1027 "
1028 );
1029  
1030 =pod
1031  
1032 set the counter
1033 uses the perl convention -- first index is 0
1034  
1035 readfiles contents of shotpoint geometry file
1036 print ("\nThis file contains ($max_index +1) row(s) of data\n");
1037 print("line $i is $line");
1038 print ("\nThis file contains $num_rows row(s) of data\n");
1039  
1040 regex:
1041 trim white spaces from both ends
1042 split on '=' and its surrounding spaces
1043 =cut
1044  
1045 $i = 0;
1046 while ( $line = <FILE> ) {
1047 chomp($line);
1048  
1049 #print("line $i is $line\n");
1050 $line =~ s/^\s+|\s+$//g; # trim white spaces from both ends
1051 ( $t, $x ) = split( /\s+=\s+/, $line );
1052  
1053 #print("parameter value : $t,$x\n");
1054 $parameter[$i] = $t;
1055 $value[$i] = $x;
1056  
1057 #print("parameter,value : $parameter[$i],$value[$i]\n");
1058 $i++;
1059 }
1060 close(FILE);
1061 return ( \@parameter );
1062 }
1063 }
1064  
1065 =head2 sub set_file_name
1066  
1067 =cut
1068  
1069 sub set_file_name {
1070 my ( $self, $file_name ) = @_;
1071  
1072 if ( $file_name ne $empty_string ) {
1073  
1074 $readfiles->{_file_name} = $file_name;
1075 print("readfiles,set_file_name,path = $readfiles->{_file_name}\n");
1076  
1077 }
1078 else {
1079 print("readfiles,set_file_name,missing file_name\n");
1080 }
1081 }
1082  
1083 =head2 sub set_path
1084  
1085 =cut
1086  
1087 sub set_path {
1088 my ( $self, $path ) = @_;
1089  
1090 if ( $path ne $empty_string ) {
1091  
1092 $readfiles->{_path} = $path;
1093  
1094 # print("readfiles,set_path,path = $readfiles->{_path} \n");
1095  
1096 }
1097 else {
1098 print("readfiles,set_path,missing path\n");
1099 }
1100  
1101 }
1102  
1103 =head2 sub set_skip_lines
1104  
1105 =cut
1106  
1107 sub set_skip_lines {
1108 my ( $self, $skip_lines ) = @_;
1109  
1110 if ( $skip_lines ne $empty_string ) {
1111  
1112 $readfiles->{_skip_lines} = $skip_lines;
1113  
1114 # print("readfiles,set_skip_lines,skip_lines = $readfiles->{_skip_lines}\n");
1115  
1116 }
1117 else {
1118 print("readfiles,set_skip_lines,missing skip_lines\n");
1119 }
1120 }
1121  
1122 1;