XPRESS/Data/XPRESS_DataFile.pm untested

filter: Statementstmt 0.0% 0 / 352 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 26 totaltotal 0.0% 0 / 513 CC 45 SCAR 28.3
CC
45
Cov
0%
timestamp
42.0
create_folder
30.0
save_plot
30.0
CRAP 16.9
1 package Lab::XPRESS::Data::XPRESS_DataFile;
2 $Lab::XPRESS::Data::XPRESS_DataFile::VERSION = '3.899';
3 #ABSTRACT: XPRESS data file module
4  
5 use v5.20;
6  
7 use strict;
8 use Time::HiRes qw/usleep/, qw/time/;
9 use Storable qw(dclone);
10 use File::Copy;
11 use Lab::XPRESS::Data::XPRESS_logger;
12 use Lab::XPRESS::Sweep;
13  
14 our $counter = 0;
15 our $GLOBAL_PATH = "./";
16 our $GLOBAL_FOLDER = undef;
17 our $DEFAULT_FOLDER = "MEAS";
18 our $DEFAULT_HEADER = "#";
19  
20 sub new {
21 my $proto = shift;
22 my $class = ref($proto) || $proto;
23 my $self = {};
24 bless( $self, $class );
25 $self->{COLUMN_NAMES};
26 $self->{NUMBER_OF_COLUMNS} = 0;
27 $self->{COLUMNS} = ();
28 $self->{BLOCK_NUM} = 0;
29 $self->{LOG_STARTED} = 0;
30 $self->{loop}->{interval} = 1;
31 $self->{loop}->{overtime} = 0;
32  
33 $self->{autolog} = 1;
34 $self->{skiplog} = 0;
35  
36 my $filenamebase = shift;
37  
38 if ( not $filenamebase =~ /(.+)(\..+)\b/ ) {
39 $filenamebase .= ".dat";
40 }
41  
42 my $foldername = $DEFAULT_FOLDER;
43 $foldername = shift if @_;
44  
45 $self->{filenamebase} = $filenamebase;
46  
47 my @plots = @_;
48 $self->{plots} = [];
49 foreach my $plot (@plots) {
50 $self->add_plot($plot);
51 }
52  
53 $self->{plot_count} = @plots;
54  
55 # create file-handle:
56 $self->{filenamebase}
57 = $self->create_folder( $self->{filenamebase}, $foldername );
58  
59 $self->open_logger( $self->{filenamebase}, $self->{plots} );
60 $self->{file} = $self->{filenamebase};
61  
62 return $self;
63  
64 }
65  
66 sub create_folder {
67  
68 my $self = shift;
69 my $filenamebase = shift;
70 my $foldername = shift || $DEFAULT_FOLDER;
71  
72 $filenamebase =~ s/\\/\//g;
73 $filenamebase =~ s/\.\///g;
74 $filenamebase =~ s/\.\.\///g;
75 $filenamebase =~ s/[a-zA-Z]\:\///g;
76  
77 my @filename = split( /\//, $filenamebase );
78 my $filename = pop(@filename);
79  
80 my $folder = join( '/', @filename );
81  
82 if ( not defined $GLOBAL_FOLDER ) {
83 if ( not -d $GLOBAL_PATH ) {
84 mkdir $GLOBAL_PATH;
85 }
86  
87 # look for existing files:
88 opendir( DIR, $GLOBAL_PATH );
89 my @files = readdir(DIR);
90  
91 my $max_index = 0;
92 foreach my $file (@files) {
93  
94 if ( $file =~ /($foldername)_([0-9]+)\b/ ) {
95 if ( $2 > $max_index ) {
96 $max_index = $2;
97 }
98 $max_index++;
99 }
100 }
101  
102 closedir(DIR);
103  
104 $GLOBAL_PATH =~ s/\/$//;
105 $GLOBAL_FOLDER
106 = sprintf( "%s/%s_%03d", $GLOBAL_PATH, $foldername, $max_index );
107  
108 mkdir($GLOBAL_FOLDER);
109  
110 copy( $0, $GLOBAL_FOLDER );
111  
112 $self->create_InfoFile();
113 }
114  
115 my $folder = $GLOBAL_FOLDER . "/" . $folder;
116  
117 return $folder . "/" . $filename;
118  
119 }
120  
121 sub create_InfoFile {
122 my $self = shift;
123  
124 open( my $LOG, ">" . $GLOBAL_FOLDER . "/Config.txt" )
125 or die "cannot open Config.txt";
126 print $LOG "Instrument Configuration\n";
127 print $LOG "-" x 100, "\n\n";
128 print $LOG $self->timestamp(), "\n";
129 print $LOG "-" x 100, "\n\n";
130  
131 foreach my $instrument ( @{Lab::Instrument::REGISTERED_INSTRUMENTS} ) {
132 print $LOG $instrument->get_id() . " ( "
133 . $instrument->get_name()
134 . " )", "\n\n";
135 print $LOG $instrument->sprint_config(), "\n";
136 print $LOG "-" x 100, "\n\n";
137 }
138  
139 }
140  
141 # ------------------------------- CONFIG ---------------------------------------------------------
142 sub add_measurement {
143 my $self = shift;
144 my $methode = shift;
145  
146 my $name = "measurement";
147 $self->{measurement} = $methode;
148  
149 my $package = ref($self) . "_" . $counter++;
150  
151 no strict 'refs';
152 *{ $package . "::" . $name } = $methode;
153 @{ $package . '::ISA' } = ( ref($self) );
154 bless $self, $package;
155  
156 return $self;
157 }
158  
159 sub add_header {
160 my $self = shift;
161 my $header = shift;
162  
163 if ( not defined $header ) {
164 return $self->{HEADER};
165 }
166  
167 my @header = split( /\n/, $header );
168 foreach (@header) {
169 $self->{HEADER} .= "#HEADER#\t" . $_ . "\n";
170 }
171  
172 return $self;
173 }
174  
175 sub add_config {
176 my $self = shift;
177 my $config = shift;
178  
179 if ( not defined $config ) {
180 return $self->{CONFIG};
181 }
182  
183 my @config = split( /\n/, $config );
184 foreach (@config) {
185 $self->{HEADER} .= "#CONFIG#\t" . $_ . "\n";
186 }
187  
188 return $self;
189 }
190  
191 sub add_column {
192 my $self = shift;
193 my $col = shift;
194  
195 if ( eval "return exists &Lab::XPRESS::Sweep::$col;" ) {
196 Lab::Exception::Warning->throw(
197 "$col is not an alowed column name. Sorry. \n");
198 }
199  
200 if ( not defined $col ) {
201 return $self->{COLUMNS};
202 }
203  
204 $self->{COLUMN_NAMES}{$col}
205 = scalar( keys %{ $self->{COLUMN_NAMES} } ) + 1;
206 push( @{ $self->{COLUMNS} }, $col );
207 $self->{NUMBER_OF_COLUMNS} += 1;
208 $self->{logger}->{COLUMN_NAMES} = $self->{COLUMN_NAMES};
209 $self->{logger}->{NUMBER_OF_COLUMNS} = $self->{NUMBER_OF_COLUMNS};
210 return $self;
211 }
212  
213 sub add_plot {
214 my $self = shift;
215 my $plot;
216  
217 if ( ref( @_[0] ) eq 'HASH' ) {
218 $plot = @_[0];
219 }
220 else {
221 $plot = shift;
222 }
223  
224 if ( not defined $plot->{'autosave'} ) {
225 $plot->{'autosave'} = 'last';
226 }
227 push( @{ $self->{plots} }, $plot );
228 $self->{logger}->{COLUMN_NAMES}
229 = $self->{COLUMN_NAMES}; # refresh logger->column_names
230 my $plot_copy = dclone( \%{$plot} );
231 $self->{logger}->add_plots($plot_copy);
232 $self->{plot_count}++;
233  
234 return $self;
235 }
236  
237 sub open_logger {
238 my $self = shift;
239 my $filenamebase = shift;
240 my $plots = shift;
241  
242 my $plots_copy = [];
243  
244 if ( defined $plots ) {
245 my $plots_copy = dclone($plots);
246 }
247  
248 $self->{logger}
249 = new Lab::XPRESS::Data::XPRESS_logger( $filenamebase, $plots_copy );
250 $self->{logger}->{COLUMN_NAMES} = $self->{COLUMN_NAMES};
251 }
252  
253 sub change_filenamebase {
254 my $self = shift;
255 my $filenamebase = shift;
256 if ( not $filenamebase =~ /\// ) {
257 $filenamebase = "./" . $filenamebase;
258 }
259  
260 #$self->{filenamebase} = $filenamebase;
261  
262 my $old_file = $self->{logger}->{filename};
263 my $old_directory = $self->{logger}->{directory};
264  
265 delete $self->{logger};
266 $self->{LOG_STARTED} = 0;
267  
268 if ( -z $old_file ) {
269 unlink $old_file;
270 }
271  
272 $self->open_logger($filenamebase);
273 $self->{file} = $self->{logger}->{filename};
274  
275 $self->{logger}->{COLUMN_NAMES} = $self->{COLUMN_NAMES};
276 $self->{logger}->{NUMBER_OF_COLUMNS} = $self->{NUMBER_OF_COLUMNS};
277  
278 my $plots_copy = dclone( $self->{plots} );
279 $self->{logger}->add_plots($plots_copy);
280  
281 }
282  
283 sub start_log {
284 my $self = shift;
285  
286 if ( defined $self->{HEADER} ) {
287 chomp $self->{HEADER};
288 $self->{logger}->LOG( $self->{HEADER} );
289 }
290 if ( defined $self->{CONFIG} ) {
291 $self->{logger}->LOG( $self->{CONFIG} );
292 }
293 if ( defined @{ $self->{COLUMNS} }[0] ) {
294 my $columns = $DEFAULT_HEADER;
295 $columns .= join( "\t", @{ $self->{COLUMNS} } );
296  
297 $self->{logger}->LOG($columns);
298 }
299 $self->{LOG_STARTED} = 1;
300 return $self;
301 }
302  
303 sub start_block {
304 my $self = shift;
305  
306 if ( not $self->{LOG_STARTED} ) {
307 $self->start_log();
308 }
309  
310 $self->{BLOCK_NUM} = $self->{logger}->_log_start_block();
311 print "Data block $self->{BLOCK_NUM}\n";
312  
313 $self->{loop}->{overtime} = 0;
314 undef $self->{loop}->{t0};
315  
316 return 1;
317  
318 }
319  
320 sub set_loop_interval {
321 my $self = shift;
322 my $interval = shift;
323  
324 $self->{loop}->{interval} = $interval;
325  
326 return $self;
327  
328 }
329  
330 sub end_loop {
331  
332 my $self = shift;
333  
334 $self->{loop}->{t1} = time();
335  
336 if ( not defined $self->{loop}->{t0} ) {
337 $self->{loop}->{t0} = time();
338 return 0;
339 }
340  
341 my $delta_time = ( $self->{loop}->{t1} - $self->{loop}->{t0} )
342 ; # + $self->{loop}->{overtime};
343 if ( $delta_time > $self->{loop}->{interval} ) {
344 $self->{loop}->{overtime} = $delta_time - $self->{loop}->{interval};
345 $delta_time = $self->{loop}->{interval};
346 warn
347 "WARNING: Measurement Loop takes more time ($self->{loop}->{overtime}) than specified by measurement interval ($self->{loop}->{interval}).\n";
348 }
349 else {
350 $self->{loop}->{overtime} = 0;
351 }
352 usleep( ( $self->{loop}->{interval} - $delta_time ) * 1e6 );
353 $self->{loop}->{t0} = time();
354 return $delta_time;
355  
356 }
357  
358 sub timestamp {
359  
360 my $self = shift;
361 my (
362 $Sekunden, $Minuten, $Stunden, $Monatstag, $Monat,
363 $Jahr, $Wochentag, $Jahrestag, $Sommerzeit
364 ) = localtime(time);
365  
366 $Monat += 1;
367 $Jahrestag += 1;
368 $Monat = $Monat < 10 ? $Monat = "0" . $Monat : $Monat;
369 $Monatstag = $Monatstag < 10 ? $Monatstag = "0" . $Monatstag : $Monatstag;
370 $Stunden = $Stunden < 10 ? $Stunden = "0" . $Stunden : $Stunden;
371 $Minuten = $Minuten < 10 ? $Minuten = "0" . $Minuten : $Minuten;
372 $Sekunden = $Sekunden < 10 ? $Sekunden = "0" . $Sekunden : $Sekunden;
373 $Jahr += 1900;
374  
375 return "$Stunden:$Minuten:$Sekunden $Monatstag.$Monat.$Jahr\n";
376  
377 }
378  
379 sub finish_measurement {
380 my $self = shift;
381  
382 my $num_of_plots = @{ $self->{logger}->{plots} };
383 for ( my $i = 0; $i < $num_of_plots; $i++ ) {
384  
385 # close gnuplot-pipe:
386 $self->{logger}->{plots}->[$i]->{plotter}->_stop_live_plot();
387 }
388  
389 $self = $self->{logger}->close_file();
390  
391 delete $self->{logger};
392 return $self;
393 }
394  
395 sub save_plot {
396 my $self = shift;
397 my $plot_index = shift;
398 my $type = shift;
399 my $filename = shift;
400  
401 if ( not defined $plot_index ) {
402 $plot_index = 0;
403 }
404 elsif ( $plot_index > ( my $num_plots = @{ $self->{logger}->{plots} } ) )
405 {
406 warn "defined plotnumber $plot_index doesn't exist.";
407 }
408  
409 if ( not defined $filename ) {
410  
411 # create filename for saving plot as eps-file:
412 $filename = $self->{logger}->{filename};
413 $filename =~ /(.+)\.(.+)\b/;
414 $filename = sprintf( "%s_%02d", $1, $plot_index + 1 );
415 }
416  
417 if ( not defined $type ) {
418 $type = 'png';
419 }
420  
421 $self->{logger}->{plots}->[$plot_index]->{plotter}->replot();
422 $self->{logger}->{plots}->[$plot_index]->{plotter}
423 ->save_plot( $type, $filename );
424  
425 return $self;
426  
427 }
428  
429 sub LOG {
430 my $self = shift;
431  
432 if ( $self->{skiplog} == 1 ) {
433 $self->{skiplog} = 0;
434 return $self;
435 }
436  
437 if ( not $self->{LOG_STARTED} ) {
438 $self->start_log();
439 }
440  
441 if ( ref( $_[0] ) eq "HASH" ) {
442 $self->{logger}->LOG(@_);
443 }
444 else {
445 $self->{logger}->LOG( \@_ );
446 }
447  
448 return $self;
449 }
450  
451 sub set_autolog {
452 my $self = shift;
453 my $value = shift;
454  
455 $self->{autolog} = $value;
456  
457 return $self;
458 }
459  
460 sub skiplog {
461 my $self = shift;
462  
463 $self->{skiplog} = 1;
464  
465 return $self;
466 }
467  
468 sub gnuplot_cmd {
469 my $self = shift;
470 my $plot_index = shift;
471 my $cmd = shift;
472  
473 if ( not defined $cmd ) {
474 $cmd = $plot_index;
475 $plot_index = 0;
476 }
477  
478 if ( $plot_index > ( my $num_plots = @{ $self->{logger}->{plots} } ) ) {
479 warn "defined plotnumber $plot_index doesn't exist.";
480 }
481  
482 $self->{logger}->{plots}->[$plot_index]->{plotter}->gnuplot_cmd($cmd);
483  
484 return $self;
485  
486 }
487  
488 sub gnuplot_init_bindings {
489 my $self = shift;
490  
491 foreach my $plot ( @{ $self->{logger}->{plots} } ) {
492 $plot->{plotter}->init_gnuplot_bindings();
493 }
494  
495 return 1;
496 }
497  
498 sub gnuplot_restart {
499 my $self = shift;
500  
501 my $i = 0;
502 foreach my $plot ( @{ $self->{logger}->{plots} } ) {
503 if ( $plot->{plotter}->{PAUSE} < 0 ) {
504 $self->gnuplot_pause();
505 }
506  
507 my $gpipe = $plot->{plotter}->{gpipe};
508 if ( not defined $gpipe ) {
509 $plot->{plotter} = new Lab::XPRESS::Data::XPRESS_plotter(
510 $self->{logger}->{filename}, $plot );
511 $plot->{plotter}->{ID} = $i;
512 $plot->{plotter}->{FILENAME} = $self->{logger}->{filename};
513 $plot->{plotter}->{COLUMN_NAMES}
514 = $self->{logger}->{COLUMN_NAMES};
515 }
516 elsif ( not( my $result = print $gpipe "" ) ) {
517 $plot->{plotter} = new Lab::XPRESS::Data::XPRESS_plotter(
518 $self->{logger}->{filename}, $plot );
519 $plot->{plotter}->{ID} = $i;
520 $plot->{plotter}->{FILENAME} = $self->{logger}->{filename};
521 $plot->{plotter}->{COLUMN_NAMES}
522 = $self->{logger}->{COLUMN_NAMES};
523 }
524  
525 $plot->{plotter}->init_gnuplot();
526 $plot->{plotter}->init_gnuplot_bindings();
527 $plot->{plotter}->start_plot( $self->{BLOCK_NUM} );
528  
529 if ( $plot->{plotter}->{PAUSE} > 0 ) {
530 $self->gnuplot_pause();
531 }
532  
533 $i++;
534 }
535  
536 return 1;
537 }
538  
539 sub gnuplot_pause {
540 my $self = shift;
541  
542 foreach my $plot ( @{ $self->{logger}->{plots} } ) {
543 $plot->{plotter}->toggle_pause();
544 }
545  
546 return 1;
547 }
548  
549 sub datazone {
550 my $self = shift;
551 my $plot_index = shift;
552 my $left = shift;
553 my $center = shift;
554 my $right = shift;
555  
556 my $x_min;
557 my $x_max;
558 my $y_min;
559 my $y_max;
560  
561 my %plot
562 = %{ $self->{logger}->{plots}->[$plot_index]->{plotter}->{plot} };
563 if ( not defined $plot{'x-min'} ) {
564 $x_min = $self->{logger}->{DATA}[ $plot{'x-axis'} - 1 ][0];
565 }
566 else {
567 $x_min = $plot{'x-min'};
568 }
569 if ( not defined $plot{'x-max'} ) {
570 $x_max = $self->{logger}->{DATA}[ $plot{'x-axis'} - 1 ][1];
571 }
572 else {
573 $x_max = $plot{'x-max'};
574 }
575 if ( not defined $plot{'y-min'} ) {
576 $y_min = $self->{logger}->{DATA}[ $plot{'y-axis'} - 1 ][0];
577 }
578 else {
579 $y_min = $plot{'y-min'};
580 }
581 if ( not defined $plot{'y-max'} ) {
582 $y_max = $self->{logger}->{DATA}[ $plot{'y-axis'} - 1 ][1];
583 }
584 else {
585 $y_max = $plot{'y-max'};
586 }
587 $self->{logger}->{plots}->[$plot_index]->{plotter}
588 ->datazone( $x_min, $x_max, $y_min, $y_max, $left, $center, $right );
589  
590 return $self;
591  
592 }
593  
594 sub DESTROY {
595 my $self = shift;
596 if ( $self->{writer} ) {
597 $self->finish_measurement();
598 }
599 }
600  
601 1;
602  
603 __END__