#!/usr/bin/perl
# This code is designed to read the P++ output information from version 0.7.6b
# and create a the param-tmp.inc and ion-param-tmp.inc files necessary for
# compiling the MSPHERE code

$num = scalar(@ARGV);
if ($num != 1) {
  die "Usage: create-param.pl <MSETUP INFO> \n";
}

print "Creating param-tmp.inc and ion-param-tmp.inc....\n";

open(IN,"$ARGV[0]")            || die "Unable to open $ARGV[0] \n";
open(HDR,"param.h")            || die "Unable to open param.h \n";
open(PARAM,">param-tmp.inc")   || die "Unable to open param-tmp.inc \n";
open(ION,">ion-param-tmp.inc") || die "Unable to open ion-param-tmp.inc \n";

$nilocal = 1;
$njlocal = 1;
$nklocal = 1;


while (<HDR>) {
  if (/#define\s*NUMBER_OF_PROCESSORS\s+(\S+)/) { $nproc = $1; }
  if (/#define\s*NUMBER_OF_MHD\s+(\S+)/) { $mproc = $1; }
  if (/#define\s*NUMBER_OF_ION\s+(\S+)/) { $iproc = $1; }
}

while(<IN>) {
#while(<>) {
  # first we get ni,nj,nk,no
  if (/inside the parallel code/) {
     s/inside the parallel code//;
     ($ni,$nj,$nk,$no) = split;
  }
  $no2 = $no/2;
  # now we get the distribution of information across the processors
  if ((/On processor  ([0-9]+) :: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/)) {
     $proc = $1; $nilow = $2; $nihigh = $3; $njlow = $4; $njhigh = $5;
     $nklow = $6; $nkhigh = $7;
     $off = -$no + 1;
     if ( $nilow == 1 ) {$off += $no2;}
     if ( $nihigh == $ni+1) {$off += $no2;}
     $localspan = $nihigh - $nilow + $off;
     $nilocal = ($nilocal < $localspan) ? $localspan :
                                                     $nilocal; 
      print " nilocal stuff: prcoessor- $proc offset- $off local span- $localspan  nilocal- $nilocal\n";
     $off = -$no + 1;
     if ( $njlow == 1 ) {$off += $no2;}
     if ( $njhigh == $nj+1) {$off += $no2;}
     $localspan = $njhigh - $njlow + $off;
     $njlocal = ($njlocal < $localspan) ? $localspan :
                                                     $njlocal; 
      print " njlocal stuff: prcoessor- $proc offset- $off local span- $localspan  njlocal- $njlocal\n";
     $off = -$no + 1;
     if ( $nklow == 1 ) {$off += $no2;}
     if ( $nkhigh == $nk+1) {$off += $no2;}
     $localspan = $nkhigh - $nklow + $off;
     $nklocal = ($nklocal < $localspan) ? $localspan :
                                                     $nklocal; 
      print " nklocal stuff: prcoessor- $proc offset- $off local span- $localspan  nklocal- $nklocal\n";
   }

    
}

$li = ($nilocal < $njlocal) ? $njlocal : $nilocal;
$li = ($li < $nklocal) ? $nklocal : $li;
$llow = -$no/2+1;
$lihigh = $li + $no/2;
$ljhigh = $li + $no/2;

# then we can create the param-tmp and ion-param-tmp files
      print $nilocal, $njlocal, $nklocal;

print PARAM "c param.inc\n";
print PARAM "       PARAMETER ( NI = $nilocal, NJ = $njlocal, NK = $nklocal, \n";
print PARAM "     \$ no = $no,\n";
print PARAM "     \$ li = $li, lj = $li, lk = $li,\n";
print PARAM "     \$ llow = $llow, lihigh =$lihigh, ljhigh = $ljhigh,\n";
print PARAM "     \$ num_procs = $nproc, num_mhd = $mproc, num_ion = $iproc)\n";
print PARAM "#include \"rest_of_param.inc\"\n";
close(PARAM);

      $nii = $no2-1 < 2 ? 2 : $no2-1;
print ION "c ion-param.inc\n";
print ION "       PARAMETER (  NI = $nii, NJ = $nj, NK = $nk )\n";
print ION "#include \"ion-rest-of-param.inc\"\n";
close(ION);

print "Done!\n";

