@rem = '--*-Perl-*--
@echo off
if "%OS%" == "Windows_NT" goto WinNT
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
:WinNT
perl -x -S %0 %*
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofperl
@rem ';
#!/usr/local/bin/perl
#line 15

#=========================================================
# Perl script
#=========================================================

use lib 'd:/Programs/Perl/lib';
#use lib '/home/tkamiya/bin/lib';

BEGIN {
#my $BaseDir = (defined $ENV{TkPerlDir})? "$ENV{TkPerlDir}/" : '';
#print "\nBaseDir: $BaseDir\n";
#@INC = ("${BaseDir}lib", "d:/Programs/Perl/lib", @INC);
}

use strict;
#use warnings;
#use CGI::Carp qw(fatalsToBrowser);
use Cwd;

use Deps;
use Utils;
use MyApplication;

BEGIN {
#	$| = 1;
	open (STDERR, ">&STDOUT");
}

#===============================================
# 大域変数
#===============================================
my $ScriptCharCode = 'utf8';
my $FileSystemCharCode = 'sjis';

my $nGauss = 5;

my $OutFile = "Summary.csv";
$OutFile = $ARGV[0] if(defined $ARGV[0]);
my $Target = ($ARGV[1] eq '')? '' : $ARGV[1];

#===============================================
# Applicationオブジェクト作成
#===============================================
my $App = new MyApplication;
my $ret = $App->Initialize();
exit(-1) if($ret < 0);

$App->SetOutputMode("console");

my $nLevel = 1;
my @files;
#my @Dirs = sort { $a cmp $b; } glob("*");
@files = Utils::SearchFilesRecursive2(".", "*.txt", $nLevel, \@files, 1,
			sub { 
				my ($path) = @_; 
#				print "dir: $path\n"; 
#				return 1 if(-d Deps::MakePath($path, "SCF", 0));
				return 1;
			},
			);
@files = sort @files;
#for(my $i= 0 ; $i < @files ; $i++) {
#	print "$i: $files[$i]\n";
#}
#exit;

my $out = new JFile;
if(!$out->Open($OutFile, "w")) {
	$App->print("Error: Can not write to [$OutFile].\n");
	exit;
}
$out->print("File name,Time,T(K),ID Gap(mm),TOA(deg),nSweep,"
		."Sample,Spectrum,"
		."ConductionType,EF,T,Wa,"
		."EBG00,BG,BGLeft0,WBG0,ECBM,EVBM,D0CB,D0VG,");
for(my $i = 0 ; $i < $nGauss ; $i++) {
$out->print("E$i,C$i,WL$i,GFrac$i,WRatio$i,Area$i,");
}
$out->print("S2,Comments\n");

print("\nMakeSummaryHTML\n");
for(my $i = 0 ; $i < @files ; $i++) {
	my ($drive, $directory, $filename, $ext1, $lastdir, $filebody) = Deps::SplitFilePath($files[$i]);
	my %hash = &ReadHXPESFiles($App, $files[$i]);
	$hash{Temperature} =~ s/,//g;
	$hash{Comments}    =~ s/,/ /g;
	$hash{IDGap} += 0.0;
	$hash{TOA}   += 0.0;
	$hash{Energy0}      = $hash{'Low Energy'};
	$hash{Energy1}      = $hash{'High Energy'};
	$hash{StepTime}     = $hash{'Step Time'};
	$hash{PassEnergy}   = $hash{'Pass Energy'};
	$hash{nSweep}       = $hash{'Number of Sweeps'};
	$hash{SpectrumName} = $hash{'Spectrum Name'};
	$hash{StepTime}     = $hash{'Step Time'};
	$hash{RegionName}   = $hash{'Region Name'};
	my @k = qw(EF T Wa EBG00 BG BGLeft0 WBG0 ECBM EVBM D0CB D0VG); # GLE00 GLC00 GLWL0 GLGFraction0 GLGWRatio0);
	for(my $i = 0 ; $i < @k ; $i++) {
		my $key = $k[$i];
		$hash{$key} =~ s/:.*$//;
	}
	for(my $i = 0 ; $i < $nGauss ; $i++) {
		my @k = qw(GLE0 GLC0 GLWL GLGFraction GLGWRatio);
		for(my $j = 0 ; $j < @k ; $j++) {
			my $key = $k[$j]. $i;
			$hash{$key} =~ s/:.*$//;
		}
	}

	$out->print("$filebody,$hash{Date} $hash{Time},$hash{Temperature},$hash{IDGap},$hash{TOA},$hash{nSweep},"
			."$hash{Sample},$hash{SpectrumName},"
			."$hash{ConductionType},$hash{EF},$hash{T},$hash{Wa},"
			."$hash{EBG00},$hash{BG},$hash{BGLeft0},$hash{WBG0},$hash{ECBM},$hash{EVBM},$hash{D0CB},$hash{D0VG},");
	for(my $i = 0 ; $i < $nGauss ; $i++) {
		my @k = qw(GLE0 GLC0 GLWL GLGFraction GLGWRatio);
		for(my $j = 0 ; $j < @k ; $j++) {
			my $key = $k[$j]. $i;
			$out->print("$hash{$key},");
		}
		my $C0 = $hash{"GLC0$i"};
		my $WL = $hash{"GLWL$i"};
		my $Gf = $hash{"GLGFraction$i"};
		my $WR = $hash{"GLGWRatio$i"};
		my $A = Sci::GaussLorentzArea($C0, $WL, $Gf, $WR);
		$out->print("$A,");
	}
	$out->print("$hash{LatestS2},$hash{Comments}\n");
}

$out->Close();

exit;

#===============================================
# Subroutines
#===============================================
sub ReadHXPESFiles
{
	my ($App, $filename) = @_;

print "Read [$filename]\n";
	my %hash = &GetHashFromHXPESDataFile($App, $filename);
	my $ParamFile = Deps::ReplaceExtension($filename, '.prm');
	my %hash2 = &GetHashFromParameterFile($App, $ParamFile);
	my $pHash = Utils::MergeHash(\%hash, \%hash2);

foreach my $key (sort keys %$pHash) {
print "  $key: $pHash->{$key}\n";
}

	return %$pHash;
}

sub GetHashFromParameterFile
{
	my ($App, $filename) = @_;

	my $ini = new IniFile($filename, 0);
	if(!$ini) {
		print "Error: Can not read [$filename]\n";
		return 0;
	}
	$ini->ReadAll($filename, '=', 1);
	return %$ini;
}

sub GetHashFromHXPESDataFile
{
	my ($App, $filename) = @_;

	my %hash;
	my $in = new JFile($filename, "r");
	if(!$in) {
		print "Error: Can not read [$filename]\n";
		return 0;
	}
	
	$in->SkipTo("\\[Info 1\]");
	while(1) {
		my $line = $in->ReadLine();
		last if(!defined $line);
		last if($line =~ /^\[/);

		my ($key, $val) = ($line =~ /^\s*(.*?)=(.*)\s*$/);
		next if($key eq '' or !defined $val);

		$hash{$key} = $val;
#print "  $key: $val\n";
	}
	$in->Close();

print "  C: [$hash{Comments}]\n";
	my ($toa)    = ($hash{Comments} =~ /[\s,]TOA\s*=\s*(\S+)/);
	$hash{TOA}   = $toa   if(defined $toa);
	my ($IDGap)  = ($hash{Comments} =~ /[\s,]IDG?\s*=\s*(\S+)/);
	$hash{IDGap} = $IDGap if(defined $IDGap);
	my ($T)            = ($hash{Comments} =~ /[\s,]T?\s*=\s*(\S+)/);
	$hash{Temperature} = $T if(defined $T);

#foreach my $key (sort keys %hash) {
#print "  $key: $hash{$key}\n";
#}

	return %hash;
}

=head1 NAME

widget - Demonstration of Perl/Tk widgets

=head1 SYNOPSYS

  widget [ directory ]

=head1 DESCRIPTION

This script demonstrates the various widgets provided by Tk, along with
many of the features of the Tk toolkit.  This file only contains code to
generate the main window for the application, which invokes individual
demonstrations.  The code for the actual demonstrations is contained in
separate ".pl" files in the "widget_lib" directory, which are autoloaded
by this script as needed.

widget looks in the directory specified on the command line to load user
contributed demonstrations.  If no directory name is specified when widget is
invoked and the environment variable WIDTRIB is defined then demonstrations
are loaded from the WIDTRIB directory. If WIDTRIB is undefined then widget
defaults to the released user contributed directory, "widtrib".

=head2 History

 #
 # Stephen O. Lidie, LUCC, 96/03/11.  lusol@Lehigh.EDU
 # Stephen O. Lidie, LUCC, 97/01/01.  lusol@Lehigh.EDU
 # Stephen O. Lidie, LUCC, 97/02/11.  lusol@Lehigh.EDU
 # Stephen O. Lidie, LUCC, 97/06/07.  lusol@Lehigh.EDU
 #     Update for Tk402.00x.  Total revamp:  WidgetDemo, Scrolled, released
 #     composites, -menuitems, qw//, etcetera.  Perl 5.004 required.
 # Stephen O. Lidie, LUCC, 98/03/10.  lusol@Lehigh.EDU
 #     Update for Tk8.
 # Stephen O. Lidie, LUCC, 98/06/26.  Stephen.O.Lidie@Lehigh.EDU
 #     Add Common Dialogs for Tk800.007.
 # Stephen.O.Lidie@Lehigh.EDU, 1999/11/29, Lehigh University.
 #     Demo some "dash patch" changes.
 # Stephen.O.Lidie@Lehigh.EDU, 2000/01/11, Lehigh University.
 #     Update menubar to Tk 8, fix color palette Menubutton demo.
 # Stephen.O.Lidie@Lehigh.EDU, 2000/07/06, Lehigh University.
 #     Remove inswt() from widget and styles.pl to show the proper Perl/Tk
 #     idiom for inserting Text tags.  Various and sundry cleanups.
 # sol0@lehigh.edu, 2003/07/29, Lehigh University Computing Center.
 #     Update for Tk 8.4.4.

=head1 AUTHOR

Steve Lidie <sol0@Lehigh.EDU>

=cut

__END__

:endofperl
