#!/usr/bin/perl

use lib 'd:/Programs/Perl/lib';

use strict;
use Utils;
use JFile;

my $InFile = "CharacterTable.html";

my %RotationMatrix = (
	'E'      => [ [ 1, 0, 0, 0, 1, 0, 0, 0, 1] ],
	'i'      => [ [-1, 0, 0, 0,-1, 0, 0, 0,-1] ],
	'C2'     => [ [-1, 0, 0, 0,-1, 0, 0, 0, 1] ],
# Temporary dead copy of C2
	'2C2'    => [ [-1, 0, 0, 0,-1, 0, 0, 0, 1] ],
	"C2'"    => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
# Temporary dead copy of C2'
	"2C2'"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"2C2''"  => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"3C2'"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"3C2''"  => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"4C2"    => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"4C2'"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"6C2"    => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	"6C2'"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
#	"C2'"    => [ [ 1, 0, 0, 0,-1, 0, 0, 0,-1],
#	              [-1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	"C2''"   => [ [ 0, 1, 0, 1, 0, 0, 0, 0,-1],
	              [ 1,-1, 0, 0,-1, 0, 0, 0,-1],
	              [-1, 0, 0,-1, 1, 0, 0, 0,-1] ],
#	'C2'''   => [ [ 0, 1, 0, 1, 0, 0, 0, 0,-1],
#	              [ 0,-1, 0,-1, 0, 0, 0, 0,-1] ],
	'C2x'    => [ [ 1, 0, 0, 0,-1, 0, 0, 0,-1] ],
	'C2y'    => [ [-1, 0, 0, 0, 1, 0, 0, 0,-1] ],
# Temporary dead copy of C2
	'3C2'    => [ [-1, 0, 0, 0,-1, 0, 0, 0, 1] ],
	'C3'     => [ [ 0,-1, 0, 1,-1, 0, 0, 0, 1],
	              [-1, 1, 0,-1, 0, 0, 0, 0, 1] ],
# Temporary dead copy of C3
	'C3^2'    => [ [ 0,-1, 0, 1,-1, 0, 0, 0, 1],
	              [-1, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'2C3'     => [ [ 0,-1, 0, 1,-1, 0, 0, 0, 1],
	              [-1, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'4C3'     => [ [ 0,-1, 0, 1,-1, 0, 0, 0, 1],
	              [-1, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'4C3^2'   => [ [ 0,-1, 0, 1,-1, 0, 0, 0, 1],
	              [-1, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'8C3'     => [ [ 0,-1, 0, 1,-1, 0, 0, 0, 1],
	              [-1, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'C4'     => [ [ 0,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 0, 0, 0, 0, 1] ],
# Temporary dead copy of C4
	'2C4'    => [ [ 0,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'6C4'    => [ [ 0,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 0, 0, 0, 0, 1] ],
	'C4^2'   => [ [-1, 0, 0, 0,-1, 0, 0, 0, 1],
	              [ 1, 0, 0, 0,-1, 0, 0, 0,-1],
	              [-1, 0, 0, 0, 1, 0, 0, 0,-1] ],
# Temprary dead copy of C4^2
	'C4^3'   => [ [-1, 0, 0, 0,-1, 0, 0, 0, 1],
	              [ 1, 0, 0, 0,-1, 0, 0, 0,-1],
	              [-1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	'C6'     => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
# Temprary dead copy of C6
	'2C6'    => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
	'2C6^2'  => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
	'C6^2'   => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
	'C6^3'   => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
	'C6^4'   => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
	'C6^5'   => [ [ 1,-1, 0, 1, 0, 0, 0, 0, 1],
	              [ 0, 1, 0,-1, 1, 0, 0, 0, 1] ],
# Temprary dead copy of C2
	"2S2"    => [ [ 0,-1, 0,-1, 0, 0, 0, 0,-1],
	              [-1, 1, 0, 0, 1, 0, 0, 0,-1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0,-1] ],
	'S3'     => [ [-1, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1,-1, 0, 0, 0,-1] ],
# Temprary dead copy of S3
	'2S3'    => [ [-1, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1,-1, 0, 0, 0,-1] ],
	'S3^5'   => [ [-1, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1,-1, 0, 0, 0,-1] ],
	'S3^6'   => [ [-1, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1,-1, 0, 0, 0,-1] ],
	'S4'     => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
# Temprary dead copy of S4
	'2S4'    => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'6S4'    => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'S4^2'   => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'S4^3'   => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'6S4^3'  => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'S6'     => [ [ 0, 1, 0,-1, 1, 0, 0, 0,-1],
	              [ 1,-1, 0, 1, 0, 0, 0, 0,-1] ],
# Temprary dead copy of S6
	'2S6'    => [ [ 0, 1, 0,-1, 1, 0, 0, 0,-1],
	              [ 1,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'4S6'    => [ [ 0, 1, 0,-1, 1, 0, 0, 0,-1],
	              [ 1,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'4S6^5'  => [ [ 0, 1, 0,-1, 1, 0, 0, 0,-1],
	              [ 1,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'8S6'    => [ [ 0, 1, 0,-1, 1, 0, 0, 0,-1],
	              [ 1,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'S6^5'   => [ [ 0, 1, 0,-1, 1, 0, 0, 0,-1],
	              [ 1,-1, 0, 1, 0, 0, 0, 0,-1] ],
# Temprary dead copy of S8
	'2S8'    => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'2S8^3'  => [ [ 0, 1, 0,-1, 0, 0, 0, 0,-1],
	              [ 0,-1, 0, 1, 0, 0, 0, 0,-1] ],
	'sgh'    => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
# Temprary dead copy of sgh
	'3sgh'   => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	'sgd'    => [ [ 0, 1, 0, 1, 0, 0, 0, 0, 1],
	              [ 1,-1, 0, 0,-1, 0, 0, 0, 1],
	              [-1, 0, 0,-1, 1, 0, 0, 0, 1] ],
# Temprary dead copy of sgd
	'2sgd'   => [ [ 0, 1, 0, 1, 0, 0, 0, 0, 1],
	              [ 1,-1, 0, 0,-1, 0, 0, 0, 1],
	              [-1, 0, 0,-1, 1, 0, 0, 0, 1] ],
	'3sgd'   => [ [ 0, 1, 0, 1, 0, 0, 0, 0, 1],
	              [ 1,-1, 0, 0,-1, 0, 0, 0, 1],
	              [-1, 0, 0,-1, 1, 0, 0, 0, 1] ],
	'4sgd'   => [ [ 0, 1, 0, 1, 0, 0, 0, 0, 1],
	              [ 1,-1, 0, 0,-1, 0, 0, 0, 1],
	              [-1, 0, 0,-1, 1, 0, 0, 0, 1] ],
	'6sgd'   => [ [ 0, 1, 0, 1, 0, 0, 0, 0, 1],
	              [ 1,-1, 0, 0,-1, 0, 0, 0, 1],
	              [-1, 0, 0,-1, 1, 0, 0, 0, 1] ],
#	'sgd'    => [ [ 0, 1, 0, 1, 0, 0, 0, 0, 1],
#	              [ 0,-1, 0,-1, 0, 0, 0, 0, 1] ],
	'sgv'    => [ [ 0,-1, 0,-1, 0, 0, 0, 0, 1],
	              [-1, 1, 0, 0, 1, 0, 0, 0, 1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0, 1] ],
# Temprary dead copy of sgv
	"2sgv"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0, 1],
	              [-1, 1, 0, 0, 1, 0, 0, 0, 1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0, 1] ],
	"3sgv"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0, 1],
	              [-1, 1, 0, 0, 1, 0, 0, 0, 1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0, 1] ],
	"sgv'"   => [ [ 0,-1, 0,-1, 0, 0, 0, 0, 1],
	              [-1, 1, 0, 0, 1, 0, 0, 0, 1],
	              [ 1, 0, 0, 1,-1, 0, 0, 0, 1] ],
#	'sgv'    => [ [-1, 0, 0, 0, 1, 0, 0, 0, 1],
#	              [ 1, 0, 0, 0,-1, 0, 0, 0, 1] ],
	'sgxy'     => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
# Temprary dead copy of sgxy
	'sg(xy)'   => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	"sg''(xy)" => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	"sg''(xz)" => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	"sg'(yz)"  => [ [ 1, 0, 0, 0, 1, 0, 0, 0,-1] ],
	'sgxz'     => [ [ 1, 0, 0, 0,-1, 0, 0, 0, 1] ],
	'sgyz'     => [ [-1, 0, 0, 0, 1, 0, 0, 0, 1] ],
	);

my $s = JFile->ReadFile($InFile) or die "$!: Can not read [$InFile].\n";
#print "s[$s]\n";

my @db;
my @tables = Utils::Split("\\<table[^\\>]*\\>", $s);
for(my $it = 1 ; $it < @tables ; $it++ ) {
	my @rows = Utils::Split("\\<tr[^\\>]*\\>", $tables[$it]);
	my $nrows = @rows;

	my @Characters;
	my ($PointGroup, @SymOp, @CharacterNames, @CharacterIndexes, @Representations);
	for(my $irow = 0 ; $irow < $nrows ; $irow++ ) {
#print "$irow: $rows[$irow]\n\n";
		my @cols = Utils::Split("\\<td[^\\>]*\\>", $rows[$irow]);
		my $ncols = @cols;
		for(my $icol = 0 ; $icol < $ncols ; $icol++ ) {
			$cols[$icol] =~ s/\<\/?p[^\>]*\>//ig;
			$cols[$icol] =~ s/\<\/?span[^\>]*\>//ig;
			$cols[$icol] =~ s/\<\/?td[^\>]*\>//ig;
			$cols[$icol] =~ s/\<\/?tr[^\>]*\>//ig;
			$cols[$icol] =~ s/\<\/?table[^\>]*\>//ig;
			$cols[$icol] =~ s/[;&]?nbsp;?//ig;
			$cols[$icol] =~ s/[\r\n]//g;
			$cols[$icol] =~ s/f/'/g;
#print "$irow:$icol: $cols[$icol]\n";

			$Characters[$irow][$icol] = Utils::DelSpace($cols[$icol]);
		}

		if($irow == 0) {
			$PointGroup = $Characters[0][0];
			for(my $icol = 1 ; $icol < @cols-1 ; $icol++ ) {
				$SymOp[$icol-1] = Utils::DelSpace($Characters[$irow][$icol]);
			}
#for(my $i = 0 ; $i < @SymOp; $i++) {
#print "Sym $i: $SymOp[$i]\n";
#}
#exit;
		}
		else {
			for(my $irow = 1 ; $irow < $nrows ; $irow++ ) {
				$CharacterNames[$irow-1] = $Characters[$irow][0];
				for(my $icol = 1 ; $icol < @cols ; $icol++) {
					$CharacterIndexes[$irow-1][$icol-1] = Utils::DelSpace($Characters[$irow][$icol]);
				}
			}
			$Representations[$irow-1] = Utils::DelSpace($Characters[$irow][$ncols-1]);
#print "R: ", $Representations[$irow-1], " (ncol=$ncols)\n";
		}
		$db[$it-1] = {
				PointGroup          => $PointGroup,
				pSymmetryOperations => \@SymOp, 
				pCharacterNames     => \@CharacterNames, 
				pRepresentations    => \@Representations,
				pCharacterIndexes   => \@CharacterIndexes,
			};
	}
}

my $DBFile = "CharacterTableDB.txt";
my $out = JFile->new($DBFile, "w") or die "$!: Can not write to [$DBFile].\n";

for(my $i = 0 ; $i < @db ; $i++) {
print "i=$i\n";
	my $p = $db[$i];
#print "pdb=$p\n";
	my $pSymOp  = $p->{pSymmetryOperations};
	my $pCName  = $p->{pCharacterNames};
	my $nC      = @$pCName;
#print "p=$pCName, n=", scalar $nC, "\n";
	my $pR      = $p->{pRepresentations};
	my $pCIndex = $p->{pCharacterIndexes};
	

	$out->print("Point Group ", $i+1, ": $p->{PointGroup}\n");
	for(my $is = 0 ; $is < $nC ; $is++) {
		$out->print("\t$pSymOp->[$is]");
	}
	$out->print("\n");

	for(my $iIndex = 0 ; $iIndex < @$pCName ; $iIndex++) {
		$out->print("$pCName->[$iIndex]");
		for(my $is = 0 ; $is < $nC ; $is++) {
			$out->print("\t$pCIndex->[$iIndex][$is]");
		}
		$out->print("\t$pR->[$iIndex]\n");
	}

	$out->print("\n");
}
$out->Close();

my $IrrepsPyFile = "PGArray.py";
my $out = JFile->new($IrrepsPyFile, "w") or die "$!: Can not write to [$IrrepsPyFile].\n";

$out->print("character_table = {\n");
for(my $i = 0 ; $i < @db ; $i++) {
print "i=$i\n";
	my $p = $db[$i];
	my $pSymOp  = $p->{pSymmetryOperations};
	my $nS      = @$pSymOp;
	my $pCName  = $p->{pCharacterNames};
	my $nC      = @$pCName;
	my $pR      = $p->{pRepresentations};
	my $pCIndex = $p->{pCharacterIndexes};

	my $PGName = $p->{PointGroup};
	$PGName =~ s/\<\/?su[bp]\>//gi;
	$PGName =~ s/'/\\'/gi;
	$out->print("    '$PGName':\n");

	$out->print("    { 'rotation_list':\n");
	$out->print("      [ ");
	
	for(my $is = 0 ; $is < $nS ; $is++) {
		my $sp = $pSymOp->[$is];
		$sp =~ s/\<sup\>/\^/gi;
		$sp =~ s/\<\/?su[bp]\>//gi;
		$sp =~ s/\<\/?h4\>//gi;
		$sp =~ s/\<\/?i\>//gi;
		$sp =~ s/sigma/sg/gi;
		$sp =~ s/'/\\'/gi;
		if($is == $nS-1) {
			$out->print("'$sp' ],\n");
		}
		else {
			$out->print("'$sp', ");
		}
	}

	$out->print("      'character_table'\n");
	for(my $ic = 0 ; $ic < $nC ; $ic++) {
		my $CName = $pCName->[$ic];
		$CName =~ s/\<\/?su[bp]\>//gi;
		$CName =~ s/'/\\'/gi;
		$out->print("      { '$CName': [ ");
		my $term = ($ic == $nC-1)? " },\n" : ",\n";
		for(my $is = 0 ; $is < $nS ; $is++) {
			if($is == $nS-1) {
				$out->print("$pCIndex->[$ic][$is] ]$term");
			}
			else {
				$out->print("$pCIndex->[$ic][$is],");
			}
		}
	}

#foreach my $key (sort keys %RotationMatrix) {
#print "$key: $RotationMatrix{$key}\n";
#}

	$out->print("      'mapping_table': [\n");
	$out->print("          { ");
	for(my $is = 0 ; $is < $nS ; $is++) {
		my $sp = $pSymOp->[$is];
		$sp =~ s/\<sup\>/\^/gi;
		$sp =~ s/\<\/?su[bp]\>//gi;
		$sp =~ s/\<\/?h4\>//gi;
		$sp =~ s/\<\/?i\>//gi;
		$sp =~ s/sigma/sg/gi;
		$sp =~ s/'/\\'/gi;

		my $spM = $sp;
		$spM =~ s/\\//g;
		$spM =~ s/\^'/'/g;
		my $pM = $RotationMatrix{$spM};
if(!$pM) {
	print "Can not find Rotation Matrixes for [$spM].\n";
	exit;
}
		my $pre = ($is == 0)? '' : '            ';
		for(my $im = 0 ; $im < @$pM ; $im++) {
			my $pre2 = ($im == 0)? sprintf("$pre%-8s: [ ( ", "'$sp'") :
								   '                          ';
			my $term = ($im == @$pM-1)? ") ) ],\n" : "),\n";
			$out->printf("$pre2(%2d,%2d,%2d)\n", $pM->[$im][0], $pM->[$im][1], $pM->[$im][2]);
			$out->printf("                          (%2d,%2d,%2d)\n", 
							$pM->[$im][3], $pM->[$im][4], $pM->[$im][5]);
			$out->printf("                          (%2d,%2d,%2d)$term", 
							$pM->[$im][6], $pM->[$im][7], $pM->[$im][8]);
		}
	}
	$out->print("      ]\n");
	$out->print("    },\n");
	$out->print("\n");
}
$out->print("}\n");

$out->Close();

exit;
