=head1 NAME Tektronix CSV Merge =head1 VERSION Version 0.03 =head1 SYNOPSIS perl gus_csv_merge.pl --csv_1 foo.csv --chans_1 0,1 --csv_2 bar.csv --chans_2 1 =head1 DESCRIPTION Reads in two Tektronix-generated C<*.csv> files, combines certain channels, writes one new, cobined C<*.csv> file and graphs the data. =head1 DEPENDENCIES This script has Perl module dependencies some of which is not yet on CPAN. Download them from here. http://starling.us/tet/gus_perl_modules/index.xml#Text-CSV-Munge http://starling.us/tet/gus_perl_modules/index.xml#Chart-EPS_graph =head1 AUTHOR Gan Uesli Starling > =head1 COPYRIGHT Copyright (c) 2006 - 2007 Gan Uesli Starling.. All rights reserved. This program is free software; you may redistribute and/or edit it under the same terms as Perl itself. =cut use strict; use warnings; use Cwd; use Text::CSV::Munge; use Chart::EPS_graph; use Getopt::Long; my ($VERSION) = '$Revision: 0.03 $' =~ m{ \$Revision: \s+ (\S+) }xm; my ($path_csv_1, $path_csv_2, $graph_label_top); my @chans_1; my @chans_2; # For when options are given at startup on the command line. &GetOptions( "csv_1=s" => \$path_csv_1, # CSV for o'scope channel 1 of 4. "chans_1=s" => \@chans_1, # Keep columns 0 and 1 for time and volts. "csv_2=s" => \$path_csv_2, # CSV for o'scope channel 2 of 4. "chans_2=s" => \@chans_2, # Keep column 1 for time and volts. "title=s" => \$graph_label_top, # What to say at top of graph. ); # Perlify paths. for ( $path_csv_1, $path_csv_2 ) { $_ =~ s{\\}{\/}g } # Arrayify CSV channels. @chans_1 = split(/,/, join(',', @chans_1)); @chans_2 = split(/,/, join(',', @chans_2)); my $munged = Text::CSV::Munge->new(); $munged->set_keys( 'Verbosity Level' => 2 ); $munged->merge_csvs( $path_csv_1, \@chans_1, # Usually TEK00000.CSV cols 0 and 1. $path_csv_2, \@chans_2, # Usually TEK00001.CSV col 1 only. ); $munged->set_col_key('Dimension', 'Time', 0); $munged->set_col_key('Unit', 'Seconds (S)', 0); $munged->set_col_key('Dimension', 'Amplitude', 1, 2); $munged->set_col_key('Unit', 'Volts (V)', 1, 2); print $munged->describe(0, 1, 2); # Significant digits per channel. $munged->round_cols( 4, 0 ); $munged->round_cols( 2, 1 .. 2 ); # Prettify column widths per channel. $munged->align_cols( 10, 0 ); $munged->align_cols( 8, 1 .. 2 ); # Save a copy of munged CVS data, including user's untidy info. $munged->set_keys( 'Info File Flag' => 1 ); $munged->write_csv( $munged->{file_path_base} . '.csv'); # Read it all back in, untidy data included, so as # to check on integrety print "\nDouble Check Description: \n=======\n"; my $dbl_ck = Text::CSV::Munge->new(); $dbl_ck->set_keys( 'Verbosity Level' => 2, 'Info File Flag' => 1, ); $dbl_ck->merge_csvs( $munged->{file_path_base} . '.csv'); print $dbl_ck->describe(); print "\n=======\n"; # Write a PostScript file of the graph. my $eps = Chart::EPS_graph->new(600, 400) or print "Oops! Can't create *.eps graph. \n"; # Give choices about EPS graph $eps->set( label_top => $graph_label_top, label_y1 => 'Amplitude (V)', label_y1_2 => '', label_y2 => '', label_x => 'Time (S)', label_x_2 => '', # The actual graph info and data. names => $munged->get_key('Column Names'), data => $munged->get_key('Column Data Arefs'), y1 => [1,2], y2 => [], ); # Create an EPS graph of the CSV data. $eps->write_eps( $munged->{file_path_base} . '.eps' ); # Display the EPS graph and convert to PNG. $eps->display('GS'); __END__