dimecres, 21 de maig de 2008

Ampero-voltimetrias

Script en Perl per processar la sortida del hardware per fer ampero-voltimetrias de CH-Instruments
#!/usr/bin/perl -w
#use strict;
#
# This code is licensed under a BSD License
# http://creativecommons.org/licenses/BSD/
#
# By Marck Collado (marck@silmak.com)
# $Id: segments.pl,v 0.0 2008-05-14 11:00:21 Marck  $
#use File::Path;

# future:
#   use File::Temp http://perldoc.perl.org/File/Temp.html
#   use of threads for more speed.
#   put gnuplot pref somewhere more easy to modify.


my ($dtmp,$gp_prefs,$dirname,$datafile,$ln,$line,$seg,$seg2,$segnum,$id,$plot,$title);

$dtmp= "temp";
$seg = $ARGV[1]+1;
$seg2 = $seg + 2;

print "\nLoading preferences...\n";
{ local $/ = undef; local *FILE; open FILE, ""; $gp_prefs = ; close FILE };

if ($#ARGV > -1) { $dirname = $ARGV[0]; }
else { die "No directory specified.\n"; };

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($datafile = readdir(DIR))) {
next if $datafile =~ /^\.\.?$/;
next if $datafile !~ /\.txt$/;
$datafile =~ s#^(\s)#./$1#;
open (INP, "< $dirname/$datafile\0") || die "couldn't open $datafile for reading.\n";
mkdir $dtmp || die "Couldn't create temp directory.";
open (GPT, ">$dtmp/plot.gp") ||
 die "couldn't open temporary files for writing.\n";
#print "$seg2\n";
print "Extracting cycle ". ($seg - 1) ." from file: $datafile\n";

# initialize graph command for gnuplot
#print GPT "set xlabel \"Potential/V\"\n";
#print GPT "set ylabel \"Current/A\"\n";
#print GPT "set xtics out autofreq\n";
#print GPT "set ytics out autofreq\n";
#print GPT "set grid\n";
#print GPT "set style increment\n";
#print GPT "set key noautotitle\n";
print GPT $gp_prefs;
print GPT "\nplot ";

# initialize temp data file

while () {
    if (/^Segment $seg:/ ... /^Segment $seg2:/) {
            open (DAT, ">&gt; $dtmp/$datafile-c$seg.dat");
            print DAT "$_";
            close (DAT);
        };
};

close (INP);
$title = $datafile;
$title !~ s/\.txt//;
$plot.="\'$dtmp/$datafile-c$seg.dat\' using 1:2:3 title \"$title\" w l,";
}
closedir(DIR);

print GPT substr($plot, 0, -1);
close (GPT);

print "Creating graph...\n";
@args = ("gnuplot", "$dtmp/plot.gp");
system(@args) == 0
or die "system @args failed: $?";

print "Cleaning scratch files and directories\n";
`rm -fr $dtmp`;