#!/usr/npac/bin/perl
open(DIS,"</npac/home/webflow/WEBFLOW/JIGSAW/Jigsaw/WWW/GEM/calculator/Gem2Files/dislocOut.txt");
open(SIM,">/npac/home/webflow/WEBFLOW/JIGSAW/Jigsaw/WWW/GEM/calculator/Gem2Files/simplexInp.txt");
@line1 = split(/\s+/,<DIS>);
$xnum = $line1[0];
$ynum = $line1[1];
 
$latorig = $line1[2];
$lonorig = $line1[3];
@line2 = split(/\s+/,<DIS>);
$lat = $line2[1];
$lon = $line2[2];
$strike = $line2[3];
@line3 = split(/\s+/,<DIS>);
$d = $line3[1];
$dip = $line3[2];
$lambda = $line3[3];
$mu = $line3[4];
$u1 = $line3[5];
$u2 = $line3[6];
$u3 = $line3[7];
$length = $line3[8];
$width = $line3[9];

#next comes column labels, ignore.

@line4 = split(/\s+/,<DIS>);

# now for the data

$expected_lines = $xnum*$ynum;
$linenum = 0;
while(<DIS>){
   --$expected_lines < 0 && die "Too many lines! found ++$linenum, expected
$xnum*$ynum\n";
   @l = split(/\s+/);
   $x[$linenum] = $l[1];
   $y[$linenum] = $l[2];
   $ux[$linenum] = $l[3];
   $uy[$linenum] = $l[4];
   $uz[$linenum] = $l[5];
   $exx[$linenum] = $l[6];
   $exy[$linenum] = $l[7];
   $eyy[$linenum++] = $l[8];
}
$expected_lines == 0 || die "All lines read, but expect $expected_lines
more!\n";

# now write the simplex input file

#lets support one fault for the moment; user can always text-edit this part anyway

print SIM "1\n\n";

print SIM "1 1 $lat\n";
print SIM "2 1 $lon\n";
print SIM "3 1 $strike\n";
print SIM "4 1 $dip\n";
print SIM "5 1 $d\n";
print SIM "6 1 $width\n";
print SIM "7 1 $length\n";
print SIM "8 1 $u1\n";
print SIM "9 1 $u2\n";

$number_out_lines = 3*$xnum*$ynum;
$number_per_type = $xnum*$ynum;
print SIM "\n$number_out_lines\n";
print SIM "0.0 1000 10\n\n";


for($index = 0; $index < $number_per_type; $index++){
   print SIM "1\t$x[$index]\t$y[$index]\t$ux[$index]\t1.0e-4\n";
}
for($index = 0; $index < $number_per_type; $index++){
   print SIM "2\t$x[$index]\t$y[$index]\t$uy[$index]\t1.0e-4\n";
}
for($index = 0; $index < $number_per_type; $index++){
   print SIM "3\t$x[$index]\t$y[$index]\t$uz[$index]\t1.0e-4\n";
}







