{ A FAIRE }
{ * dans le cas des xrays, lre le fichier de f et f' à parir de l'energie ou de la longueur d'onde }
unit dataBaseReading;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EditReal, ComCtrls, ExtCtrls, Constants, DataReflec,
  JvComponent, JvFormPlace;

type
  TdataBaseReadingForm = class(TForm)
    MaterialName: TEdit;
    Label1: TLabel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Label2: TLabel;
    densityBox: TEditReal;
    Label4: TLabel;
    bnBox: TEditReal;
    sigmaBox: TEditReal;
    Label6: TLabel;
    Label7: TLabel;
    bniBox: TEditReal;
    Label9: TLabel;
    Label22: TLabel;
    Label3: TLabel;
    bmBox: TEditReal;
    Label23: TLabel;
    deltaMbox: TEditReal;
    deltaBox: TEditReal;
    Label25: TLabel;
    betaBox: TEditReal;
    Label27: TLabel;
    Label5: TLabel;
    lambdaBox: TEditReal;
    Label8: TLabel;
    AimBox: TEditReal;
    Label28: TLabel;
    DescriptionBox: TEdit;
    Panel2: TPanel;
    Label14: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    fBox: TEditReal;
    fibox: TEditReal;
    densityBox2: TEditReal;
    Panel1: TPanel;
    deltaBox2: TEditReal;
    betaBox2: TEditReal;
    JvFormPlace1: TJvFormPlace;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label24: TLabel;
    Label29: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label30: TLabel;
    Panel3: TPanel;
    Label36: TLabel;
    lambdaBox2: TEditReal;
    Label15: TLabel;
    Label26: TLabel;
    EnergyBox2: TEditReal;
    Label31: TLabel;
    Label32: TLabel;
    EnergyBox: TEditReal;
    Label33: TLabel;
    Label34: TLabel;
    procedure MaterialNameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure lambdaBoxKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EnergyBoxKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure lambdaBox2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EnergyBox2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
    procedure GetF(material:string;E:double;var f,f1:double);    
  end;

var
  dataBaseReadingForm: TdataBaseReadingForm;

implementation

{$R *.dfm}

procedure TdataBaseReadingForm.MaterialNameKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var Filename:string;
    f:textFile;
    description,dataS:string;
    data,energy:double;
    errorCode:integer;
    lambda,lambda2,density:double;
begin
 if key=13
 then begin
      densityBox.font.color:=clWhite;
      bnBox.font.color:=clWhite;bniBox.font.color:=clWhite;bmBox.font.color:=clWhite;
      fBox.font.color:=clWhite;fiBox.font.color:=clWhite;
      AimBox.font.color:=clWhite;
      deltaBox.font.color:=clWhite;deltaMBox.font.color:=clWhite;betaBox.font.color:=clWhite;
      { check if the files actually exists }
      fileName:=SourceDirectory+'dataBase\'+MaterialName.Text+'.txt';
      if FileExists(fileName)
      then begin
           assignFile(f,fileName);
           Reset(f);
           readln(f,description);DescriptionBox.Text:=description;
           lambda:=lambdaBox.value*1e-9;lambda2:=lambdaBox2.value*1e-9;
           { rho }readln(f,dataS);Val(dataS, data, ErrorCode);
           if ErrorCode=0
           then begin
                densityBox.font.color:=clBlack;densityBox.Value:=data;
                densityBox2.font.color:=clBlack;densityBox2.Value:=data;
                end
           else begin densityBox.font.color:=clWhite;densityBox.font.color:=clWhite; end;
           density:=densityBox.value*1e28;
           { bn } readln(f,dataS);Val(dataS, data, ErrorCode);
           if ErrorCode=0
           then begin
                bnBox.font.color:=clBlack;bnBox.Value:=data;
                deltaBox.font.color:=clBlack;deltaBox.value := lambda * lambda  / (2*pi) * density * bnBox.value * 1e-15 * 1e6;
                end
           else begin bnBox.font.color:=clWhite;deltaBox.font.color:=clWhite; end;
           { sigma }
           readln(f,dataS);
{           try begin
           readln(f,data,energy);
           if data<>0
           then begin
                sigmaBox.font.color:=clBlack;sigmaBox.Value:=data;
                BniBox.font.color:=clBlack;BniBox.Value:=data;
                betaBox.font.color:=clBlack;betaBox.Value:=data;
                energyBox.value:=energy;lambdaBox.Value:=sqrt(hb2over2mn*4*pi*pi/(EnergyBox.value*eV*1e-3))*1e9;
                bnibox.value:=1e15 * sigmaBox.value * 1e-28 * (2*pi) / (lambdaBox.value*1e-9) / (4*pi);
                betaBox.value := lambdaBox.value * lambdaBox.value  / (2*pi) * density * bniBox.value *1e-15 * 1e6; { signe? }
{                end
                end
           except begin sigmaBox.font.color:=clWhite;bniBox.font.color:=clWhite;betaBox.font.color:=clWhite; end; end; }

           { f } { readln(f,dataS);Val(dataS, data, ErrorCode);
           if ErrorCode=0 then begin fBox.font.color:=clBlack;fBox.Value:=data end else fBox.font.color:=clWhite; }
           try begin
           readln(f,data,energy);
           if data<>0
           then begin fBox.font.color:=clBlack;fBox.Value:=data;deltaBox2.font.color:=clBlack;
                      energyBox2.value:=energy;lambdaBox2.Value:=hp*c0/(EnergyBox2.value*eV)*1e9;
                      deltaBox2.value := re * lambda2 * lambda2  / (2*pi) * density * fBox.value * 1e6; end
               end
           except begin fBox.font.color:=clWhite;deltaBox2.font.color:=clWhite end end;
           { f'} { readln(f,dataS);Val(dataS, data, ErrorCode);
           if ErrorCode=0 then begin fiBox.font.color:=clBlack;fiBox.Value:=data end else fiBox.font.color:=clWhite; }
           try begin
           readln(f,data,energy);
           if data<>0
           then begin fiBox.font.color:=clBlack;fiBox.Value:=data;betaBox2.font.color:=clBlack;
                      energyBox2.value:=energy;lambdaBox2.Value:=hp*c0/(EnergyBox2.value*eV)*1e9;
                      betaBox2.value := re * lambda2 * lambda2  / (2*pi) * density * fiBox.value * 1e6; end
           end
           except begin fiBox.font.color:=clWhite;fiBox.Value:=data;betaBox2.font.color:=clWhite; end end;

           { M }  readln(f,dataS);Val(dataS, data, ErrorCode);
           if ErrorCode=0
           then begin
                AimBox.font.color:=clBlack;AimBox.Value:=data;
                bmBox.font.color:=clBlack;bMBox.value := 1e15 * mn / (2*pi*hb2) * abs(mun) * mu0 * Aimbox.value * muB;
                deltaMBox.font.color:=clBlack;deltaMBox.value := lambda * lambda  / (2*pi) * density * bMBox.value *1e-15 * 1e6;
                end
           else begin AimBox.font.color:=clWhite;bmBox.font.color:=clWhite;deltaMBox.font.color:=clWhite end;
           closeFile(f);
           end
      else MessageDlg('This material is not in your database', mtWarning, [mbOk], 0);
      end;

end;

procedure TdataBaseReadingForm.FormCreate(Sender: TObject);
begin
  WindowState:=wsMinimized;
end;

procedure TdataBaseReadingForm.lambdaBoxKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
   if key=13
   then EnergyBox.value:=hb2over2mn*sqr(2*pi/(lambdaBox.Value*1e-9))/eV*1e3;
end;

procedure TdataBaseReadingForm.EnergyBoxKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
   if key=13
   then lambdaBox.Value:=sqrt(hb2over2mn*4*pi*pi/(EnergyBox.value*eV*1e-3))*1e9;
end;

procedure TdataBaseReadingForm.lambdaBox2KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var f,fi:double;
    lambda,density:double;
begin
   if key=13
   then begin
        EnergyBox2.value:=hp*c0/(lambdaBox2.Value*1e-9)/eV;
        GetF(MaterialName.text,EnergyBox2.value,f,fi);
        fBox.font.color:=clBlack;fiBox.font.color:=clBlack;
        fbox.value:=f;fibox.value:=fi;
        lambda:=lambdaBox2.value*1e-9;density:=densityBox.value*1e28;
        deltaBox2.value := re * lambda * lambda  / (2*pi) * density * fBox.value * 1e6;
        betaBox2.value := re * lambda * lambda  / (2*pi) * density * fiBox.value * 1e6;
        end;
end;

procedure TdataBaseReadingForm.EnergyBox2KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
var f,fi:double;
    lambda,density:double;
begin
   if key=13
   then begin
        lambdaBox2.Value:=hp*c0/(EnergyBox2.value*eV)*1e9;
        GetF(MaterialName.text,EnergyBox2.value,f,fi);
        fBox.font.color:=clBlack;fiBox.font.color:=clBlack;
        fbox.value:=f;fibox.value:=fi;
        lambda:=lambdaBox2.value*1e-9;density:=densityBox.value*1e28;
        deltaBox2.value := re * lambda * lambda  / (2*pi) * density * fBox.value * 1e6;
        betaBox2.value := re * lambda * lambda  / (2*pi) * density * fiBox.value * 1e6;
        end;
end;

{ INPUT : material : material name (corresponding to the file name) }
{         E, x-rays energy in eV                                    }
{ OUTPUT : scattering factors F and F'                              }
procedure TdataBaseReadingForm.GetF(material:string;E:double;var f,f1:double);
var fileName,s:string;
    fichier:textfile;
    a0,a1,b0,b1,c0,c1:double;
begin
   { check if the files actually exists }
   fileName:=SourceDirectory+'dataBase\formFactors\'+material+'.nff';
   if FileExists(fileName)
   then begin
           assignFile(fichier,fileName);
           Reset(fichier);
           readln(fichier,s); { on saute la première ligen }
           a1:=-1;
           while not(eof(fichier)) and (a1<E) do
           begin
             readln(fichier,a1,b1,c1);
             { readln(fichier,a1);         }
             if (a1>= E) { on a trouvé l'énergie demandée }
             then begin
                  f:=b0+(b1-b0)*(E-a0)/(a1-a0);
                  f1:=c0+(c1-c0)*(E-a0)/(a1-a0);
                  end
             else begin a0:=a1;b0:=b1;c0:=c1; end;
             { extraction des valeurs numériques de la ligne }
           end;
           closefile(fichier);
        end
   else MessageDlg('This material is not in your database', mtWarning, [mbOk], 0);
end;

end.
