package inversionsrechnung_Phillips_Twomey;

# Everything related to the inversion of the kernel matrix
# Start Phillips-Twomey algorithm

use strict;
use warnings;
use vars qw(@ISA);
#use Carp;

use PDL;
use PDL::Core;
use PDL::Math;
use PDL::Primitive;
use PDL::Complex;
#use Proc::Background; # for running background processes (in this case: inversion)
use File::Spec; # for OS-agnostic file and path names
use Wx;

# This is the script doing the inversion
my $inversion_script = File::Spec->rel2abs(File::Spec->catfile(File::Spec->curdir(),'alpha_inversion_Faran.pl'));
# Results of the inversion in this file
my $result_file =File::Spec->rel2abs('result.txt');

use attenuation;
use material;

use inversionsrechnung;

use lib qw( Module );
use Helos;
use Faran_lsg_pdl;

@ISA=qw(inversionsrechnung);

my $xm = pdl [0];

my $c_L;

my $h_L;
my $h_T;
my $g;

# Umrechnung Np/m/MHz -> dB/cm/MHz
my $dB = 20/log(10) / 100;

sub new {
  my( $this ) = shift;
  my $class = ref($this) || $this;
  my $newInst={};
  bless($newInst, $class);

  #printf "new inversionsrechnung\n";
  $newInst->Initialize;
  return $newInst;
}

sub Initialize {
  my ($this) = @_;
  #printf "Initialize $this\n";
  $this->inversionsrechnung::Initialize; # call parent
}

sub start_calculation {
  my ($this, $process) = @_;
  # Process is a pointer to a process variable
  # initiate calculation by calling the relevant script
  my $fit_process_id =Wx::ExecuteCommand("perl ". $inversion_script . " -s 1",# -r $result_file",
  #my $fit_process_id =Wx::ExecuteCommand("ls -la",
                     0,  # asynchronous execution

                     $process);
  $this->{attn}->output_alpha($process);
  $this->{fluid_properties}->output_property_data($process,"fluid");
  $this->{particle_properties}->output_property_data($process, "particle");
  $this->output_initial_data($process);
  $process->CloseOutput();
  return($fit_process_id);
}

# Get the results from the output of the inversion script
# stored in results.txt

sub read_results {
  my ($this,$linebuf) = @_;
  my $ret_val=0;
  # search for phi value
  if ($linebuf =~ "phi ") {
    my @fields= split(" ", $linebuf);
    $this->c($fields[1]);
  }
  # search for x values in line
  if ($linebuf =~ m/x \[/i) {
    $linebuf =~ s/\[//g;
    $linebuf =~ s/\]//g;
    $linebuf =~ s/x //g;
    my @fields = split(" ", $linebuf);
    $this->xm(pdl @fields);
  }
#  # search for dQ values in line
  if ($linebuf =~ m/dQ \[/i) {
    $linebuf =~ s/\[//g;
    $linebuf =~ s/\]//g;
    $linebuf =~ s/dQ //g;
    my @fields = split(" ", $linebuf);
    $this->dQ(pdl @fields);
  }
#  $this->x50($x50_fields[1]);
#  $this->sigma_ln($sigmaln_fields[1]);
#  $this->c($phi_fields[1]);
  $ret_val=1;
  return($ret_val);
}

sub output_initial_data {
  my ($this, $process) = @_;
  my $a =$process->GetOutputStream;
  # Header
  print $a "Begin of initial data\n";
  # Data
  my $x50=$this->{X_50_INI};
  print $a "x_50 $x50\n";
  my $phi =$this->{PHI_INI}/100;
  print $a "phi $phi\n";
  # "Footer"
  print $a "End of initial data\n";
  return 1;
}

1;
