#!/usr/bin/perl

use lib 'c:/Programs/Perl/lib';
use lib 'd:/Programs/Perl/lib';

use Utils;
use JFile;

my $InFile  = 'TC-Chino.csv';

#my $iStartLine = 4;
#my $OutFile = 'TC-B.csv';
#my $iStartLine = 40;
#my $OutFile = 'TC-R.csv';
#my $iStartLine = 76;
#my $OutFile = 'TC-K.csv';
#my $iStartLine = 94;
#my $OutFile = 'TC-Kminus.csv';
#my $iStartLine = 130;
#my $OutFile = 'TC-J.csv';
#my $iStartLine = 148;
#my $OutFile = 'TC-Jminus.csv';
#my $iStartLine = 184;
#my $OutFile = 'TC-CR-AuFe.csv';
my $iStartLine = 272;
my $OutFile = 'TC-PtRh40-PtRh20.csv';

print "Read from [$InFile].\n";
print "Save to [$OutFile].\n";

my $in  = JFile->new($InFile,  'r') or die "$!: Can not read [$InFile].\n";
my $out = JFile->new($OutFile, 'w') or die "$!: Can not write to [$OutFile].\n";

for(my $i = 0 ; $i < $iStartLine-1 ; $i++) {
	$in->ReadLine();
}
my $line = $in->ReadLine();
$line =~ s/\"1,(.*?)\"/1$1/g;
#$line =~ s/[^\d,]//g;
#print "line: $line";
my ($aa, @Tbase) = split(/,/, $line);
pop @Tbase;
print join(', ', @Tbase), "\n";

$line = $in->ReadLine();

my %Vdata;
my $c = 0;
for(my $i = 0 ; ; $i++) {
	$line = $in->ReadLine();
print "line: $line\n";
	my ($dt, @V) = split(/\s*,\s*/, $line);
	last if($dt !~ /^[+-\d\.]+$/);

	pop @V;
print "dt=$dt\n";
	for(my $j = 0 ; $j < @V ; $j++) {
		next if($V[$j] !~ /^[+-\d\.]+$/);

		my $T = $Tbase[$j] + $dt;
		$Vdata{$T} = $V[$j];
print "  $T: V=$Vdata{$T}\n";
		$c++;
	}
}

my @T = sort { $a <=> $b; } (keys %Vdata);

print "\n";

$out->print("T,V\n");
for(my $i = 0 ; $i < @T ; $i++) {
	my $T = $T[$i];
#	print "$T: $Vdata{$T}\n";
	$out->print("$T,$Vdata{$T}\n");
}
$out->Close();
$in->Close();
exit;
