{01.08.01 : TDD : modif des procedures "xyGraphPhiMouseMove" et "xyGraphPhiMouseMove"
afin de pouvoir afficher la position sur la graphe avec des valeurs scientifiques
08.08.01 : TDD  : ajout des procedures Transfer2CurveS et TransferCurveS afin de pouvoir
afficher une serie de points dans 4 plots pour xygraph et 3 plots pour xygraphphi
               : dans FromCreate, internalzero fixé a zero pour toutes les series de courbes}
{ 23.08.01 : FO : touche arrow up et down pour ajuster les echelles y d'un facteur 10 }
{25.09.01 : TDD : valeurs scientifiques quand on utilise les touches arrow up et down}
{ 02.02.04 : FO : amélioration des séries et MAJ du boutton STORE               }
{ A FAIRE }
{ le autoscale ne marche pas correctement en echelle logarithmique      }
unit PilotageGraph2D;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  xyGraph, ExtCtrls, StdCtrls, Buttons, xyDatWiz, xyDesign, EditReal, MtxVec,
  ActnList, Menus, math387, Spin, dataReflec, JvComponent, JvFormPlace, MainForm, Editdata;

type
  TSRGraph1Dform = class(TForm)
    Panel1: TPanel;
    xyGraph: TxyGraph;
    Panel2: TPanel;
    xyGraphPhi: TxyGraph;
    Panel4: TPanel;
    PrintPhi: TSpeedButton;
    RedessinerPhi: TSpeedButton;
    EditGraphPhi: TSpeedButton;
    DataPhi: TSpeedButton;
    ZoomOutPhi: TSpeedButton;
    ZoomInPhi: TSpeedButton;
    YPhiAuto: TSpeedButton;
    LinLogPhi: TButton;
    Panel3: TPanel;
    EditGraph: TSpeedButton;
    Data: TSpeedButton;
    YAuto: TSpeedButton;
    LinLog: TButton;
    XAuto: TButton;
    XPhiAuto: TSpeedButton;
    xygraphRightPanel: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    XminBox: TEditReal;
    XmaxBox: TEditReal;
    YminBox: TEditReal;
    YmaxBox: TEditReal;
    xygraphphiRightPanel: TPanel;
    Label8: TLabel;
    YPhiMinBox: TEditReal;
    YPhiMaxBox: TEditReal;
    Label9: TLabel;
    Ypos: TEditReal;
    Xpos: TEditReal;
    Label2: TLabel;
    Label1: TLabel;
    Label10: TLabel;
    XphiMinBox: TEditReal;
    XPhiMaxBox: TEditReal;
    Label7: TLabel;
    Label3: TLabel;
    XPhiPos: TEditReal;
    Label4: TLabel;
    YPhiPos: TEditReal;
    Label11: TLabel;
    Label12: TLabel;
    UpdatePhi: TButton;
    ClearPhi: TButton;
    Splitter2: TSplitter;
    TfrCurves: TCheckBox;
    tfr2curves: TCheckBox;
    SpinEditPhi: TSpinEdit;
    StoreCurvesButton: TButton;
    StoreCurvesPhi: TButton;
    CurveSpinEdit: TSpinEdit;
    JvFormPlace1: TJvFormPlace;
    Label13: TLabel;
    MDSplotIdx: TSpinEdit;
    MainMenu1: TMainMenu;
    Graph1D1: TMenuItem;
    CopyToClipboard1: TMenuItem;
    Print1: TMenuItem;
    File1: TMenuItem;
    Edit1: TMenuItem;
    Updategraph1: TMenuItem;
    ClearAllCurves1: TMenuItem;
    Clearcalculatedcurves1: TMenuItem;
    Displayerrorbarslinscaleonly1: TMenuItem;
    Opendatafile1: TMenuItem;
    procedure PrintClick(Sender: TObject);
    procedure RedessinerClick(Sender: TObject);
    procedure EditGraphClick(Sender: TObject);
    procedure DataClick(Sender: TObject);
    procedure YAutoClick(Sender: TObject);
    procedure XAutoClick(Sender: TObject);
    procedure PrintPhiClick(Sender: TObject);
    procedure RedessinerPhiClick(Sender: TObject);
    procedure EditGraphPhiClick(Sender: TObject);
    procedure DataPhiClick(Sender: TObject);
    procedure YPhiAutoClick(Sender: TObject);
    procedure XPhiAutoClick(Sender: TObject);
    procedure LinLogClick(Sender: TObject);
    procedure xyGraphMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure xyGraphPhiMouseMove(Sender: TObject; Shift: TShiftState; XPhi,
      YPhi: Integer);
    procedure ClearButtonClick(Sender: TObject);
    procedure UpdateButtonClick(Sender: TObject);
    procedure ClearPhiButtonClick(Sender: TObject);
    procedure UpdatePhiButtonClick(Sender: TObject);
    procedure LinLogPhiClick(Sender: TObject);
    procedure XMinBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure XMaxBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure YMinBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure YMaxBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure XPhiMinBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure XPhiMaxBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure YPhiMinBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure YPhiMaxBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
    procedure XminBoxExit(Sender: TObject);
    procedure XmaxBoxExit(Sender: TObject);
    procedure YminBoxExit(Sender: TObject);
    procedure YmaxBoxExit(Sender: TObject);
    procedure XphiMinBoxExit(Sender: TObject);
    procedure XPhiMaxBoxExit(Sender: TObject);
    procedure YPhiMinBoxExit(Sender: TObject);
    procedure YPhiMaxBoxExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure StoreCurves;
    procedure StoreCurvesButtonClick(Sender: TObject);
    procedure StoreCurvesPhiClick(Sender: TObject);
    procedure ClearCalc;
    procedure ClearCalcButtonClick(Sender: TObject);
    procedure MDSplotIdxChange(Sender: TObject);
    procedure CopyToClipboard1Click(Sender: TObject);
    procedure OpenDataFile1Click(Sender: TObject);
    procedure Updategraph1Click(Sender: TObject);
    procedure ClearAllCurves1Click(Sender: TObject);
    procedure Clearcalculatedcurves1Click(Sender: TObject);
    procedure Displayerrorbarslinscaleonly1Click(Sender: TObject);
    procedure Print1Click(Sender: TObject);

  private
      { Déclarations privées}
  public
    procedure SetLegendXYGraphPhi(s0,s1,s2,s3,s4:string);  
    procedure upload1D(var x,y:Tvec;s:string);  
    procedure Transfer2CurveS(var Xcurve,Ycurve:Tvec;cindex:integer);
    procedure TransferCurveS(var Xcurve,Ycurve:Tvec;cindex:integer);
    procedure TransferCurve(var Xcurve,Ycurve:Tvec;index:integer);
    procedure TransferCurveWithError(var Xcurve,Ycurve,YError:Tvec;index:integer);
    procedure TransferCurve2(var Xcurve,Ycurve:Tvec;index:integer);
    procedure TransferCurvePhi(var Xcurve,Ycurve:Tvec;index:integer);    
    procedure PlotDifference(var MyDataExp,MyDataCalc:TDataReflectivity);
    procedure PlotDataReflectivity(var MyData:TDataReflectivity);
    procedure PlotDataReflectivityMDS(var MyData:TDataReflectivity;idx:integer);        
    { Déclarations publiques}
  end;

var
  SRGraph1Dform: TSRGraph1Dform;

implementation

uses SimulReflecForm;

{$R *.DFM}

procedure TSRGraph1Dform.SetLegendXYGraphPhi(s0,s1,s2,s3,s4:string);
begin
   SRGraph1DForm.xyGraphPhi[SeriePhi1].clear;SRGraph1DForm.xyGraphPhi[SeriePhi1].holdupdates:=true;
   SRGraph1DForm.xyGraphPhi[SeriePhi2].clear;SRGraph1DForm.xyGraphPhi[SeriePhi2].holdupdates:=true;
   SRGraph1DForm.xyGraphPhi[SeriePhi3].clear;SRGraph1DForm.xyGraphPhi[SeriePhi3].holdupdates:=true;
   SRGraph1DForm.xyGraphPhi[SeriePhi4].clear;SRGraph1DForm.xyGraphPhi[SeriePhi4].holdupdates:=true;
   xyGraphPhi.Appearance.graphTitle:=s0;
   xyGraphPhi.Series[SeriePhi1].SeriesName:=s1;
   xyGraphPhi.Series[SeriePhi2].SeriesName:=s2;
   xyGraphPhi.Series[SeriePhi3].SeriesName:=s3;
   xyGraphPhi.Series[SeriePhi4].SeriesName:=s4;
end;


{ transfert d'un spectre dans les listes de spectres und                        }
{ INPUT : x et u vecteurs des abscisses et des ordonnées                        }
{         s: le nom du spectre dans lequels on veut faire le tranfert           }
procedure TSRGraph1Dform.upload1D(var x,y:Tvec;s:string);
var i:integer;
begin
   und.addspectre(s,y.length,1,1);
   for i:=0 to y.length-1 do
   begin
       und.spect[und.indexof(s)].data[i,0,0].x:=x[i];
       und.spect[und.indexof(s)].data[i,0,0].z:=y[i];
   end;
end;


procedure TSRGraph1Dform.FormCreate(Sender: TObject);
var i:integer;
begin
windowState:=wsMinimized;
left:=0;top:=0;width:=700;height:=420;
panel1.height:=400;panel2.height:=120;
for i:=1 to xygraph.lastseriesindex do begin xygraph[i].internalzero:=0; end;
for i:=1 to xygraph.lastseriesindex do begin xygraphphi[i].internalzero:=0; end;
{ initialisation des echelles dans les boites echelles }
XminBox.value:=xygraph.XAxis.min;XmaxBox.value:=xygraph.XAxis.max;
YminBox.value:=xygraph.YAxis.min;YmaxBox.value:=xygraph.YAxis.max;
XPhiminBox.value:=xygraphPhi.XAxis.min;XPhimaxBox.value:=xygraphPhi.XAxis.max;
YPhiminBox.value:=xygraphPhi.YAxis.min;YPhimaxBox.value:=xygraphPhi.YAxis.max;
end;

procedure TSRGraph1Dform.TransferCurveS(var Xcurve,Ycurve:Tvec;cindex:integer);
var i,nbrPoints:integer;
begin
curvespinedit.maxvalue:=20;
if curvespinedit.value>xygraph.LastSeriesIndex then curvespinedit.value:=7;
nbrPoints:=Xcurve.length;
cindex:=curvespinedit.value;
xyGraphPhi[cindex].clear;xyGraph[cindex].holdupdates:=true;
for i:=0 to nbrPoints-1 do
  begin
      xyGraph[cindex][Xcurve[i]]:=Ycurve[i];
  end;
xyGraph[cindex].holdupdates:=false;xyGraph[cindex].active:=true;
curvespinedit.value:=curvespinedit.value+1;
end;

procedure TSRGraph1Dform.Transfer2CurveS(var Xcurve,Ycurve:Tvec;cindex:integer);
var i,nbrPoints:integer;
begin
spineditphi.maxvalue:=4;
if spineditphi.value>3 then spineditphi.value:=1;

nbrPoints:=Xcurve.length;
cindex:=spineditphi.value;
xyGraphPhi[cindex].clear;xyGraphphi[cindex].holdupdates:=true;
for i:=0 to nbrPoints-1 do
  begin
      xyGraphphi[cindex][Xcurve[i]]:=Ycurve[i];
  end;
  xyGraphphi[cindex].holdupdates:=false;
  xyGraphphi[cindex].active:=true;
spineditphi.value:=spineditphi.value+1;
end;

{ procédure de transfert d'une courbe stockée dans un vecteur Tvec }
{ dans une serie du graphe2D                                       }
{ INPUT: Xcurve,Ycurve, 2 vecteurs contenant les abscisses et les ordonnées de la courbe }
{        index, l'index de la série dans laquelle on veut tracer la courbe }
{ OUTPUT: la courbe est tracée sur le graphe 2D                            }
{ REMARK: pas défaut, ca clear la courbe existante                         }
procedure TSRGraph1Dform.TransferCurve(var Xcurve,Ycurve:Tvec;index:integer);
var i,nbrPoints:integer;
begin
  nbrPoints:=Xcurve.length;
  xyGraph[index].clear;
  xyGraph[index].holdupdates:=true;
  for i:=0 to nbrPoints-1 do
  begin
     { on verifie que les points expérimentaux ne sont pas nuls }
     if Ycurve[i]<>0 then xyGraph[index].add(Xcurve[i],Ycurve[i]);
{      xyGraph[index].AddWithError(Xcurve[i],Ycurve[i],sqrt(Xcurve[i]),sqrt(Ycurve[i])); }
{      xyGraph[index][Xcurve[i]]:=Ycurve[i]; }
  end;
  xyGraph[index].holdupdates:=false;
  xyGraph[index].active:=true;
end;

{ procédure de transfert d'une courbe stockée dans un vecteur Tvec }
{ dans une serie du graphe2D                                       }
{ INPUT: Xcurve,Ycurve, 2 vecteurs contenant les abscisses et les ordonnées de la courbe }
{        index, l'index de la série dans laquelle on veut tracer la courbe }
{ OUTPUT: la courbe est tracée sur le graphe 2D                            }
{ REMARK: pas défaut, ca clear la courbe existante                         }
procedure TSRGraph1Dform.TransferCurvePhi(var Xcurve,Ycurve:Tvec;index:integer);
var i,nbrPoints:integer;
begin
  nbrPoints:=Xcurve.length;
  xyGraphPhi[index].clear;
  xyGraphPhi[index].holdupdates:=true;
  for i:=0 to nbrPoints-1 do
  begin
     { on verifie que les points expérimentaux ne sont pas nuls }
     if Ycurve[i]<>0 then xyGraphPhi[index].add(Xcurve[i],Ycurve[i]);
{      xyGraph[index].AddWithError(Xcurve[i],Ycurve[i],sqrt(Xcurve[i]),sqrt(Ycurve[i])); }
{      xyGraph[index][Xcurve[i]]:=Ycurve[i]; }
  end;
  xyGraphPhi[index].holdupdates:=false;
  xyGraphPhi[index].active:=true;
end;

{ procédure de transfert d'une courbe stockée dans un vecteur Tvec }
{ dans une serie du graphe2D                                       }
{ INPUT: Xcurve,Ycurve, 2 vecteurs contenant les abscisses et les ordonnées de la courbe }
{        index, l'index de la série dans laquelle on veut tracer la courbe }
{ OUTPUT: la courbe est tracée sur le graphe 2D                            }
{ REMARK: pas défaut, ca clear la courbe existante                         }
procedure TSRGraph1Dform.TransferCurveWithError(var Xcurve,Ycurve,YError:Tvec;index:integer);
var i,nbrPoints:integer;
begin
  nbrPoints:=Xcurve.length;
  xyGraph[index].clear;
  xyGraph[index].holdupdates:=true;
  for i:=0 to nbrPoints-1 do
  begin
     if Ycurve[i]<>0 then xyGraph[index].AddWithError(Xcurve[i],Ycurve[i],0,Yerror[i]); 
{      xyGraph[index][Xcurve[i]]:=Ycurve[i]; }
  end;
  xyGraph[index].holdupdates:=false;
  xyGraph[index].active:=true;
end;

{ procédure de transfert d'une courbe stockée dans un vecteur Tvec                       }
{ dans une serie du graphe2Dphi  (deuxième fenetre graphique 2D                          }
{ INPUT: Xcurve,Ycurve, 2 vecteurs contenant les abscisses et les ordonnées de la courbe }
{        index, l'index de la série dans laquelle on veut tracer la courbe               }
{ OUTPUT: la courbe est tracée sur le graphe 2D                                          }
{ REMARK: pas défaut, ca clear la courbe existante                                       }
procedure TSRGraph1Dform.TransferCurve2(var Xcurve,Ycurve:Tvec;index:integer);
var i,nbrPoints:integer;
begin
  nbrPoints:=Xcurve.length;
  xyGraphPhi[index].clear;xyGraphPhi[index].holdupdates:=true;
  for i:=0 to nbrPoints-1 do
  begin
      xyGraphPhi[index][Xcurve[i]]:=Ycurve[i];
  end;
  xyGraphPhi[index].holdupdates:=false;xyGraphPhi[index].active:=true;
end;

procedure TSRGraph1Dform.LinLogClick(Sender: TObject);
begin
     If LinLog.caption='Lin'
     then begin
          xygraph.Yaxis.Logscale:=True;
          LinLog.caption:='Log'
          end
     else begin
          xyGraph.Yaxis.Logscale:=False;
          LinLog.caption:='Lin'
          end;
{     redessiner }
end;

procedure TSRGraph1Dform.DataClick(Sender: TObject);
begin
      ShowxyDataWizard(self, xyGraph);
end;

procedure TSRGraph1Dform.PrintClick(Sender: TObject);
begin
     xygraph.print;
end;

procedure TSRGraph1Dform.RedessinerClick(Sender: TObject);
begin
{     redessiner; }
end;

procedure TSRGraph1Dform.EditGraphClick(Sender: TObject);
begin
     editxyGraph(self, xyGraph);
end;

procedure TSRGraph1Dform.YAutoClick(Sender: TObject);
begin
     if xygraph.yaxis.autosizing
     then begin
          xygraph.yaxis.autosizing:=false;
          Yauto.caption:='Y Man';
          end
     else begin
          xygraph.yaxis.autosizing:=true;
          xygraph.YAxis.autostepping:=true;
          Yauto.caption:='Y Auto';
          end;
end;

procedure TSRGraph1Dform.XAutoClick(Sender: TObject);
begin
if xygraph.xaxis.autosizing
     then begin
          xygraph.xaxis.autosizing:=false;
          xauto.caption:='X Man';
          end
     else begin
          xygraph.xaxis.autosizing:=true;
          xygraph.xAxis.autostepping:=true;
          xauto.caption:='X Auto';
          end;
end;


procedure TSRGraph1Dform.xyGraphMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
     Xpos.text:=floattostrf(xygraph.GetMouseX(X),ffGeneral,3,2);
     Ypos.text:=floattostrf(xygraph.GetMouseY(Y),ffGeneral,3,2);
end;

procedure TSRGraph1Dform.ClearButtonClick(Sender: TObject);
var i:integer;
begin
     For i:=1 to xygraph.LastSeriesIndex do
     begin
          xyGraph[i].clear;
     end;
end;

{ mise a jour des echelles sur les deux graphes pour qu'elles se correspondent }
procedure TSRGraph1Dform.UpdateButtonClick(Sender: TObject);
begin
     xyGraphPhi.XAxis.Min:=xyGraph.xaxis.min;
     xyGraphPhi.XAxis.max:=xyGraph.xaxis.max;
     xyGraphPhi.XAxis.stepsize:=xyGraph.xaxis.stepsize;
end;

procedure TSRGraph1Dform.DataPhiClick(Sender: TObject);
begin
      ShowxyDataWizard(self, xyGraphPhi);
end;

procedure TSRGraph1Dform.PrintPhiClick(Sender: TObject);
begin
     xygraphphi.print;
end;

procedure TSRGraph1Dform.RedessinerPhiClick(Sender: TObject);
begin
xygraphphi.update;
end;

procedure TSRGraph1Dform.EditGraphPhiClick(Sender: TObject);
begin
     editxyGraph(self, xyGraphPhi);
end;

procedure TSRGraph1Dform.YPhiAutoClick(Sender: TObject);
begin
     if xygraphphi.yaxis.autosizing
     then begin
          xygraphphi.yaxis.autosizing:=false;
          yphiauto.caption:='YPhi Man';
          end
     else begin
          xygraphphi.yaxis.autosizing:=true;
          yphiauto.caption:='YPhi Auto';
          end;
end;

procedure TSRGraph1Dform.XPhiAutoClick(Sender: TObject);
begin
     if xygraphphi.xaxis.autosizing
     then begin
          xyGraphPhi.xaxis.autosizing:=false;
          xphiauto.caption:='XPhi Man';
          end
     else begin
          xygraphphi.xaxis.autosizing:=true;
          xphiauto.caption:='XPhi Auto';
          end;
end;

procedure TSRGraph1Dform.LinLogPhiClick(Sender: TObject);
begin
     If LinLogPhi.caption='Lin'
     then begin
          xygraphPhi.Yaxis.Logscale:=True;
          LinLogPhi.caption:='Log'
          end
     else begin
          xyGraphPhi.Yaxis.Logscale:=False;
          LinLogPhi.caption:='Lin'
          end;
end;


procedure TSRGraph1Dform.xyGraphPhiMouseMove(Sender: TObject;
  Shift: TShiftState; XPhi, YPhi: Integer);
begin
     XPhipos.text:=floattostrf(xygraphphi.GetMouseX(XPhi),ffGeneral,3,2);
     YPhipos.text:=floattostrf(xygraphphi.GetMouseY(YPhi),ffGeneral,3,2);
end;

procedure TSRGraph1Dform.ClearPhiButtonClick(Sender: TObject);
var i:integer;
begin
     For i:=1 to 10 do
     begin
          xyGraphPhi[i].clear;
     end;
end;

{ mise a jour des echelles sur les deux graphes pour qu'elles se correspondent }
procedure TSRGraph1Dform.UpdatePhiButtonClick(Sender: TObject);
begin
     xyGraphPhi.XAxis.Min:=xyGraph.xaxis.min;
     xyGraphPhi.XAxis.max:=xyGraph.xaxis.max;
     xyGraphPhi.XAxis.stepsize:=xyGraph.xaxis.stepsize;
end;


{controles des echelles de xygraph}
procedure TSRGraph1Dform.XminBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if key=13 then begin xygraph.XAxis.min:=XminBox.value;xygraph.update end;
end;

procedure TSRGraph1Dform.XmaxBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if key=13 then begin xygraph.XAxis.max:=XmaxBox.value;xygraph.update end;
end;

procedure TSRGraph1Dform.YminBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
begin
     if key=13 then begin xygraph.YAxis.min:=YminBox.value;xygraph.update end;
     if key=38 then begin YminBox.text:=FloatToStrf(YminBox.value*10,ffGeneral,4,2);YminBox.update;xygraph.YAxis.min:=YminBox.value;xygraph.update end;
     if key=40 then begin YminBox.text:=FloatToStrf(YminBox.value/10,ffGeneral,4,2);YminBox.update;xygraph.YAxis.min:=YminBox.value;xygraph.update end;
end;

procedure TSRGraph1Dform.YmaxBoxKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
begin
     if key=13 then begin xygraph.YAxis.max:=YmaxBox.value;xygraph.update end;
     if key=38 then begin YmaxBox.text:=FloatToStrf(YmaxBox.value*10,ffGeneral,4,2);YmaxBox.update;xygraph.YAxis.max:=YmaxBox.value;xygraph.update end;
     if key=40 then begin YmaxBox.text:=FloatToStrf(YmaxBox.value/10,ffGeneral,4,2);YmaxBox.update;xygraph.YAxis.max:=YmaxBox.value;xygraph.update end;
end;

procedure TSRGraph1Dform.XminBoxExit(Sender: TObject);
begin
xygraph.XAxis.min:=XminBox.value;
xygraph.update;
end;

procedure TSRGraph1Dform.XmaxBoxExit(Sender: TObject);
begin
xygraph.XAxis.max:=XmaxBox.value;
xygraph.update;
end;

procedure TSRGraph1Dform.YminBoxExit(Sender: TObject);
begin
xygraph.YAxis.min:=YminBox.value;
xygraph.update;
end;

procedure TSRGraph1Dform.YmaxBoxExit(Sender: TObject);
begin
xygraph.YAxis.max:=YmaxBox.value;
xygraph.update;
end;


{controles des echelles de xygraphphi}
procedure TSRGraph1Dform.XPhiminBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if key=13 then begin xygraphphi.XAxis.min:=XPhiminBox.value;xygraphphi.update end;
end;

procedure TSRGraph1Dform.XPhimaxBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if key=13 then begin xygraphphi.XAxis.max:=XPhimaxBox.value;xygraphphi.update end;
end;

procedure TSRGraph1Dform.YPhiminBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if key=13 then begin xygraphphi.YAxis.min:=YPhiminBox.value;xygraphphi.update end;
     if key=38 then begin YPhiminBox.text:=FloatToStrf(YPhiminBox.value*10,ffGeneral,4,2);YPhiminBox.update;xygraphPhi.YAxis.min:=YPhiminBox.value;xygraph.update end;
     if key=40 then begin YPhiminBox.text:=FloatToStrf(YPhiminBox.value/10,ffGeneral,4,2);YPhiminBox.update;xygraphPhi.YAxis.min:=YPhiminBox.value;xygraph.update end;
end;

procedure TSRGraph1Dform.YPhimaxBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
     if key=13 then begin xygraphphi.YAxis.max:=YPhimaxBox.value;xygraphphi.update end;
     if key=38 then begin YPhimaxBox.text:=FloatToStrf(YPhimaxBox.value*10,ffGeneral,4,2);YPhimaxBox.update;xygraphPhi.YAxis.max:=YPhimaxBox.value;xygraph.update end;
     if key=40 then begin YPhimaxBox.text:=FloatToStrf(YPhimaxBox.value/10,ffGeneral,4,2);YPhimaxBox.update;xygraphPhi.YAxis.max:=YPhimaxBox.value;xygraph.update end;
end;

procedure TSRGraph1Dform.XphiMinBoxExit(Sender: TObject);
begin
xygraphphi.XAxis.min:=XPhiminBox.value;
xygraphphi.update;
end;

procedure TSRGraph1Dform.XPhiMaxBoxExit(Sender: TObject);
begin
xygraphphi.XAxis.max:=XPhimaxBox.value;
xygraphphi.update;
end;

procedure TSRGraph1Dform.YPhiMinBoxExit(Sender: TObject);
begin
xygraphphi.YAxis.min:=YphiminBox.value;
xygraphphi.update;
end;

procedure TSRGraph1Dform.YPhiMaxBoxExit(Sender: TObject);
begin
xygraphphi.YAxis.max:=YphimaxBox.value;
xygraphphi.update;
end;

{ procedure to store the curves so as to be able to compare newly calculated curves with a set of reference curves }
procedure TSRGraph1Dform.StoreCurves;
var i:integer;
    x,y,rv:double;
    flag:boolean;
begin
xyGraph[Store1].clear;xyGraph[Store1].holdupdates:=true;
xyGraph[Store2].clear;xyGraph[Store2].holdupdates:=true;
xyGraph[Store3].clear;xyGraph[Store3].holdupdates:=true;
xyGraph[Store4].clear;xyGraph[Store4].holdupdates:=true;
flag:=xygraph.series[SerieRcalcUP].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraph.series[Store1].add(x,y);
      flag:=xygraph.series[SerieRcalcUP].GetNextPoint(x, y, rv,i); end;
flag:=xygraph.series[SerieRcalcDO].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraph.series[Store2].add(x,y);
      flag:=xygraph.series[SerieRcalcDO].GetNextPoint(x, y, rv,i); end;
flag:=xygraph.series[SerieRcalcSFUD].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraph.series[Store3].add(x,y);
      flag:=xygraph.series[SerieRcalcSFUD].GetNextPoint(x, y, rv,i); end;
flag:=xygraph.series[SerieRcalcSFDU].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraph.series[Store4].add(x,y);
      flag:=xygraph.series[SerieRcalcSFDU].GetNextPoint(x, y, rv,i); end;
xyGraph[Store1].holdupdates:=false;xyGraph[Store1].active:=true;
xyGraph[Store2].holdupdates:=false;xyGraph[Store2].active:=true;
xyGraph[Store3].holdupdates:=false;xyGraph[Store3].active:=true;
xyGraph[Store4].holdupdates:=false;xyGraph[Store4].active:=true;
{ sauvegarde de la spin asymétrie }
xyGraphPhi[2].clear;xyGraphPhi[2].holdupdates:=true;
flag:=xygraphPhi.series[1].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraphPhi.series[2].add(x,y);
      flag:=xygraphPhi.series[1].GetNextPoint(x, y, rv,i); end;
xyGraphPhi[2].holdupdates:=false;xyGraph[2].active:=true;
end;

{ procedure to store the curves so as to be able to compare newly calculated curves with a set of reference curves }
procedure TSRGraph1Dform.StoreCurvesButtonClick(Sender: TObject);
begin
   StoreCurves;
end;

procedure TSRGraph1Dform.StoreCurvesPhiClick(Sender: TObject);
var i:integer;
    x,y,rv:double;
    flag:boolean;
begin
{ sauvegarde de la spin asymmétrie }
xyGraphPhi[StorePhi1].clear;xyGraphPhi[StorePhi1].holdupdates:=true;
xyGraphPhi[StorePhi2].clear;xyGraphPhi[StorePhi2].holdupdates:=true;
flag:=xygraphPhi.series[SeriePhi1].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraphPhi.series[StorePhi1].add(x,y);
      flag:=xygraphPhi.series[SeriePhi1].GetNextPoint(x, y, rv,i); end;
flag:=xygraphPhi.series[SeriePhi2].GetFirstPoint(x, y, rv,i);
while flag do
begin xygraphPhi.series[StorePhi2].add(x,y);
      flag:=xygraphPhi.series[SeriePhi2].GetNextPoint(x, y, rv,i); end;
xyGraphPhi[StorePhi1].holdupdates:=false;xyGraphPhi[StorePhi1].active:=true;
xyGraphPhi[StorePhi2].holdupdates:=false;xyGraphPhi[StorePhi2].active:=true;
end;

procedure TSRGraph1Dform.ClearCalc;
var i:integer;
begin
     For i:=4 to xygraph.LastSeriesIndex do
     begin
          xyGraph[i].clear;
     end;
end;

procedure TSRGraph1Dform.ClearCalcButtonClick(Sender: TObject);
begin
   ClearCalc;
end;


{ affichage des data de la liste des MDS }
procedure TSRGraph1Dform.MDSplotIdxChange(Sender: TObject);
begin
   if MDSplotIdx.value<=(high(MyMDS)+1)
   then PlotDataReflectivityMDS(MyMDS[MDSplotIdx.value-1],MDSplotIdx.value)
   else MDSplotIdx.value:=high(MyMDS)+1;
end;

procedure TSRGraph1Dform.PlotDifference(var MyDataExp,MyDataCalc:TDataReflectivity);
var i:integer;
begin
   if MyDataExp.PolarisationType='non polarised'
   then begin
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.R,SerieRcalcUP);
        { plotting de la courbe de différence entre la courbe expérimentale et la courbe calculée }
        SetLegendXYGraphPhi('DIFFERENCE','diff. calc.-exp.','-','-','-');
        for i:=0 to MyDataExp.abscisses.length-1 do
        begin
         if MyDataExp.R.values[i]<>0   { au cas ou des data manquent }
         then
           with SimulReflecWindow do
           begin
           if ponderationTypeBox.text = 'linear' then
           xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.R.values[i]-MyDataExp.R.values[i])/(MyDataCalc.R.values[i]+MyDataExp.R.values[i]));
           if ponderationTypeBox.text = 'log' then
           xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.R.values[i])-log10(MyDataExp.R.values[i]));
           if ponderationTypeBox.text = 'log²' then
           xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.R.values[i])-log10(MyDataExp.R.values[i]));
//           if ponderationTypeBox.text = 'least square (linear)' then { least square (linear) }
//           xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=(MyDataCalc.R.values[i]-MyDataExp.R.values[i])/MyDataExp.Rs.values[i];
//           if ponderationTypeBox.text = 'least square (log)' then { least square (log) }
//           xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=MyDataCalc.R.values[i]*(log10(MyDataCalc.R.values[i])-log10(MyDataExp.R.values[i]))/(MyDataExp.Rs.values[i]);
           end;
        end;
        xyGraphPhi[SeriePhaseUP].holdupdates:=false;xyGraphPhi[SeriePhaseUP].active:=true;
        end;
   if MyDataExp.PolarisationType='polarised'
   then begin
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rup,SerieRcalcUP);
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rdo,SerieRcalcDO);
        { plotting de la courbe de différence entre la courbe expérimentale et la courbe calculée }
        SetLegendXYGraphPhi('DIFFERENCE','diff. UP calc.-exp.','diff DO calc.-exp.','-','-');
        for i:=0 to MyDataExp.abscisses.length-1 do
        with SimulReflecWindow do
        begin
           if ponderationTypeBox.text = 'linear' then
           begin if MyDataExp.Rup.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rup.values[i]-MyDataExp.Rup.values[i])/(MyDataCalc.Rup.values[i]+MyDataExp.Rup.values[i]));
                 if MyDataExp.Rdo.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rdo.values[i]-MyDataExp.Rdo.values[i])/(MyDataCalc.Rdo.values[i]+MyDataExp.Rdo.values[i])); end;
           if ponderationTypeBox.text = 'log' then
           begin if MyDataExp.Rup.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rup.values[i])-log10(MyDataExp.Rup.values[i]));
                 if MyDataExp.Rdo.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rdo.values[i])-log10(MyDataExp.Rdo.values[i])); end;
           if ponderationTypeBox.text = 'log²' then
           begin if MyDataExp.Rup.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rup.values[i])-log10(MyDataExp.Rup.values[i]));
                 if MyDataExp.Rdo.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rdo.values[i])-log10(MyDataExp.Rdo.values[i])); end;
{           if ponderationTypeBox.text = 'least square (linear)' then
           begin if MyDataExp.Rup.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=(MyDataCalc.Rup.values[i]-MyDataExp.Rup.values[i])/MyDataExp.Rups.values[i];
                 if MyDataExp.Rdo.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=(MyDataCalc.Rdo.values[i]-MyDataExp.Rdo.values[i])/MyDataExp.Rdos.values[i]; end;
           if ponderationTypeBox.text = 'least square (log)' then
           begin if MyDataExp.Rup.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=MyDataCalc.Rup.values[i]*(log10(MyDataCalc.Rup.values[i])-log10(MyDataExp.Rup.values[i]))/(MyDataExp.RupS.values[i]);
                 if MyDataExp.Rdo.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=MyDataCalc.Rdo.values[i]*(log10(MyDataCalc.Rdo.values[i])-log10(MyDataExp.Rdo.values[i]))/(MyDataExp.RdoS.values[i]); end; }
        end;
        xyGraphPhi[SeriePhaseUP].holdupdates:=false;xyGraphPhi[SeriePhaseUP].active:=true;
        xyGraphPhi[SeriePhaseDO].holdupdates:=false;xyGraphPhi[SeriePhaseDO].active:=true;
        end;
   if MyDataExp.PolarisationType='with spin-flip'
   then begin
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Ruu,SerieRcalcUP);
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rdd,SerieRcalcDO);
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rud,SerieRcalcSF);
        { plotting de la courbe de différence entre la courbe expérimentale et la courbe calculée }
        SetLegendXYGraphPhi('DIFFERENCE','diff. UP calc.-exp.','diff DO calc.-exp.','diff SF calc.-exp.','-');
        for i:=0 to MyDataExp.abscisses.length-1 do
        with SimulReflecWindow do
        begin
           if ponderationTypeBox.text = 'linear' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Ruu.values[i]-MyDataExp.Ruu.values[i])/(MyDataCalc.Ruu.values[i]+MyDataExp.Ruu.values[i]));
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rdd.values[i]-MyDataExp.Rdd.values[i])/(MyDataCalc.Rdd.values[i]+MyDataExp.Rdd.values[i]));
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSF][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rud.values[i]-MyDataExp.Rud.values[i])/(MyDataCalc.Rud.values[i]+MyDataExp.Rud.values[i])); end;
           if ponderationTypeBox.text = 'log' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Ruu.values[i])-log10(MyDataExp.Ruu.values[i]));
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rdd.values[i])-log10(MyDataExp.Rdd.values[i]));
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSF][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rud.values[i])-log10(MyDataExp.Rud.values[i])); end;
           if ponderationTypeBox.text = 'log²' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Ruu.values[i])-log10(MyDataExp.Ruu.values[i]));
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rdd.values[i])-log10(MyDataExp.Rdd.values[i]));
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSF][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rud.values[i])-log10(MyDataExp.Rud.values[i])); end;
{           if ponderationTypeBox.text = 'least square (linear)' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=(MyDataCalc.Ruu.values[i]-MyDataExp.Ruu.values[i])/MyDataExp.Ruus.values[i];
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=(MyDataCalc.Rdd.values[i]-MyDataExp.Rdd.values[i])/MyDataExp.Rdds.values[i];
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSF][MyDataExp.abscisses[i]]:=(MyDataCalc.Rud.values[i]-MyDataExp.Rud.values[i])/MyDataExp.Ruds.values[i]; end;
           if ponderationTypeBox.text = 'least square (log)' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=MyDataCalc.Ruu.values[i]*(log10(MyDataCalc.Ruu.values[i])-log10(MyDataExp.Ruu.values[i]))/(MyDataExp.RuuS.values[i]);
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=MyDataCalc.Rdd.values[i]*(log10(MyDataCalc.Rdd.values[i])-log10(MyDataExp.Rdd.values[i]))/(MyDataExp.RddS.values[i]);
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSF][MyDataExp.abscisses[i]]:=MyDataCalc.Rud.values[i]*(log10(MyDataCalc.Rud.values[i])-log10(MyDataExp.Rud.values[i]))/(MyDataExp.RudS.values[i]); end; }
        end;
        xyGraphPhi[SeriePhaseUP].holdupdates:=false;xyGraphPhi[SeriePhaseUP].active:=true;
        xyGraphPhi[SeriePhaseDO].holdupdates:=false;xyGraphPhi[SeriePhaseDO].active:=true;
        xyGraphPhi[SeriePhaseSF].holdupdates:=false;xyGraphPhi[SeriePhaseSF].active:=true;
        end;
   if MyDataExp.PolarisationType='with SF high field'
   then begin
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Ruu,SerieRcalcUP);
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rdd,SerieRcalcDO);
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rud,SerieRcalcSFUD);
        transferCurve(MyDataCalc.Abscisses,MyDataCalc.Rdu,SerieRcalcSFDU);
        { plotting de la courbe de différence entre la courbe expérimentale et la courbe calculée }
        SetLegendXYGraphPhi('DIFFERENCE','diff. UP calc.-exp.','diff DO calc.-exp.','diff UD calc.-exp.','diff DU calc.-exp.');
        for i:=0 to MyDataExp.abscisses.length-1 do
        with SimulReflecWindow do
        begin
           if ponderationTypeBox.text = 'linear' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Ruu.values[i]-MyDataExp.Ruu.values[i])/(MyDataCalc.Ruu.values[i]+MyDataExp.Ruu.values[i]));
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rdd.values[i]-MyDataExp.Rdd.values[i])/(MyDataCalc.Rdd.values[i]+MyDataExp.Rdd.values[i]));
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSFUD][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rud.values[i]-MyDataExp.Rud.values[i])/(MyDataCalc.Rud.values[i]+MyDataExp.Rud.values[i]));
                 if MyDataExp.Rdu.values[i]<>0 then xyGraphPhi[SeriePhaseSFDU][MyDataExp.abscisses[i]]:=sqr((MyDataCalc.Rdu.values[i]-MyDataExp.Rdu.values[i])/(MyDataCalc.Rdu.values[i]+MyDataExp.Rdu.values[i])); end;
           if ponderationTypeBox.text = 'log' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Ruu.values[i])-log10(MyDataExp.Ruu.values[i]));
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rdd.values[i])-log10(MyDataExp.Rdd.values[i]));
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSFUD][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rud.values[i])-log10(MyDataExp.Rud.values[i]));
                 if MyDataExp.Rdu.values[i]<>0 then xyGraphPhi[SeriePhaseSFDU][MyDataExp.abscisses[i]]:=abs(log10(MyDataCalc.Rdu.values[i])-log10(MyDataExp.Rdu.values[i])); end;
           if ponderationTypeBox.text = 'log²' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Ruu.values[i])-log10(MyDataExp.Ruu.values[i]));
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rdd.values[i])-log10(MyDataExp.Rdd.values[i]));
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSFUD][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rud.values[i])-log10(MyDataExp.Rud.values[i]));
                 if MyDataExp.Rdu.values[i]<>0 then xyGraphPhi[SeriePhaseSFDU][MyDataExp.abscisses[i]]:=sqr(log10(MyDataCalc.Rdu.values[i])-log10(MyDataExp.Rdu.values[i])); end;
{           if ponderationTypeBox.text = 'least square (linear)' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=(MyDataCalc.Ruu.values[i]-MyDataExp.Ruu.values[i])/MyDataExp.Ruus.values[i];
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=(MyDataCalc.Rdd.values[i]-MyDataExp.Rdd.values[i])/MyDataExp.Rdds.values[i];
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSFUD][MyDataExp.abscisses[i]]:=(MyDataCalc.Rud.values[i]-MyDataExp.Rud.values[i])/MyDataExp.Ruds.values[i];
                 if MyDataExp.Rdu.values[i]<>0 then xyGraphPhi[SeriePhaseSFDU][MyDataExp.abscisses[i]]:=(MyDataCalc.Rdu.values[i]-MyDataExp.Rdu.values[i])/MyDataExp.Rdus.values[i]; end;
           if ponderationTypeBox.text = 'least square (log)' then
           begin if MyDataExp.Ruu.values[i]<>0 then xyGraphPhi[SeriePhaseUP][MyDataExp.abscisses[i]]:=MyDataCalc.Ruu.values[i]*(log10(MyDataCalc.Ruu.values[i])-log10(MyDataExp.Ruu.values[i]))/(MyDataExp.RuuS.values[i]);
                 if MyDataExp.Rdd.values[i]<>0 then xyGraphPhi[SeriePhaseDO][MyDataExp.abscisses[i]]:=MyDataCalc.Rdd.values[i]*(log10(MyDataCalc.Rdd.values[i])-log10(MyDataExp.Rdd.values[i]))/(MyDataExp.RddS.values[i]);
                 if MyDataExp.Rud.values[i]<>0 then xyGraphPhi[SeriePhaseSFUD][MyDataExp.abscisses[i]]:=MyDataCalc.Rud.values[i]*(log10(MyDataCalc.Rud.values[i])-log10(MyDataExp.Rud.values[i]))/(MyDataExp.RudS.values[i]);
                 if MyDataExp.Rdu.values[i]<>0 then xyGraphPhi[SeriePhaseSFDU][MyDataExp.abscisses[i]]:=MyDataCalc.Rdu.values[i]*(log10(MyDataCalc.Rdu.values[i])-log10(MyDataExp.Rdu.values[i]))/(MyDataExp.RduS.values[i]); end; }
        end;
        xyGraphPhi[SeriePhaseUP].holdupdates:=false;xyGraphPhi[SeriePhaseUP].active:=true;
        xyGraphPhi[SeriePhaseDO].holdupdates:=false;xyGraphPhi[SeriePhaseDO].active:=true;
        xyGraphPhi[SeriePhaseSFUD].holdupdates:=false;xyGraphPhi[SeriePhaseSFUD].active:=true;
        xyGraphPhi[SeriePhaseSFDU].holdupdates:=false;xyGraphPhi[SeriePhaseSFDU].active:=true;
        end;
end;

{ procedure de tracé des courbes de réflectivité }
procedure TSRGraph1Dform.PlotDataReflectivity(var MyData:TDataReflectivity);
begin
  with MyData do
  begin
    if AbscissesUnits='deg' then SRGraph1DForm.xygraph.XAxis.title:='Theta (degrees)';
    if AbscissesUnits='nm-1' then SRGraph1DForm.xygraph.XAxis.title:='Qz (nm-1)';

    if PolarisationType='non polarised'
    then begin
         if weighting=false
         then SRGraph1DForm.transferCurve(Abscisses,R,SerieRexpUP)
         else SRGraph1DForm.transferCurveWithError(Abscisses,R,Rs,SerieRexpUP);
         upload1D(abscisses,R,'R');
         end;
    if PolarisationType='polarised'
    then begin
         if weighting=false
         then begin SRGraph1DForm.transferCurve(Abscisses,Rup,SerieRexpUP);
                    SRGraph1DForm.transferCurve(Abscisses,Rdo,SerieRexpDO); end
         else begin SRGraph1DForm.transferCurveWithError(Abscisses,Rup,RupS,SerieRexpUP);
                    SRGraph1DForm.transferCurveWithError(Abscisses,Rdo,RdoS,SerieRexpDO); end;
         upload1D(abscisses,Rup,'Rup');upload1D(abscisses,Rdo,'Rdo');
         end;
    if PolarisationType='with spin-flip'
    then begin
         if weighting=false
         then begin SRGraph1DForm.transferCurve(Abscisses,Ruu,SerieRexpUP);
                SRGraph1DForm.transferCurve(Abscisses,Rdd,SerieRexpDO);
                SRGraph1DForm.transferCurve(Abscisses,Rud,SerieRexpSF); end
         else begin SRGraph1DForm.transferCurveWithError(Abscisses,Ruu,RuuS,SerieRexpUP);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rdd,RddS,SerieRexpDO);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rud,RudS,SerieRexpSF); end;
         upload1D(abscisses,Ruu,'Ruu');upload1D(abscisses,Rdd,'Rdd');upload1D(abscisses,Rud,'Rud');
         end;
    if PolarisationType='with SF high field'
    then begin
         if weighting=false
         then begin SRGraph1DForm.transferCurve(Abscisses,Ruu,SerieRexpUP);
                SRGraph1DForm.transferCurve(Abscisses,Rdd,SerieRexpDO);
                SRGraph1DForm.transferCurve(Abscisses,Rud,SerieRexpSFUD);
                SRGraph1DForm.transferCurve(Abscisses,Rdu,SerieRexpSFDU); end
         else begin SRGraph1DForm.transferCurveWithError(Abscisses,Ruu,RuuS,SerieRexpUP);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rdd,RddS,SerieRexpDO);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rud,RudS,SerieRexpSFUD);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rdu,RduS,SerieRexpSFDU); end;
         upload1D(abscisses,Ruu,'Ruu');upload1D(abscisses,Rdd,'Rdd');upload1D(abscisses,Rud,'Rud');upload1D(abscisses,Rdu,'Rdu');
         end;
  end;
end;

{ procedure de tracé des courbes de réflectivité }
{ INPUT : index de la courbe à rajouter }
procedure TSRGraph1Dform.PlotDataReflectivityMDS(var MyData:TDataReflectivity;idx:integer);
var s:string;
begin
  s:=intToStr(idx);
  with MyData do
  begin
    if AbscissesUnits='deg' then SRGraph1DForm.xygraph.XAxis.title:='Theta (degrees)';
    if AbscissesUnits='nm-1' then SRGraph1DForm.xygraph.XAxis.title:='Qz (nm-1)';

    if PolarisationType='non polarised'
    then begin
         if weighting=false
         then SRGraph1DForm.transferCurve(Abscisses,R,SerieRexpUP)
         else SRGraph1DForm.transferCurveWithError(Abscisses,R,Rs,SerieRexpUP);
         upload1D(abscisses,R,'MDS'+s);
         end;
    if PolarisationType='polarised'
    then begin
         if weighting=false
         then begin SRGraph1DForm.transferCurve(Abscisses,Rup,SerieRexpUP);
                    SRGraph1DForm.transferCurve(Abscisses,Rdo,SerieRexpDO); end
         else begin SRGraph1DForm.transferCurveWithError(Abscisses,Rup,RupS,SerieRexpUP);
                    SRGraph1DForm.transferCurveWithError(Abscisses,Rdo,RdoS,SerieRexpDO); end;
         upload1D(abscisses,Rup,'MDS'+s+'up');upload1D(abscisses,Rdo,'MDS'+s+'do');
         end;
    if PolarisationType='with spin-flip'
    then begin
         if weighting=false
         then begin SRGraph1DForm.transferCurve(Abscisses,Ruu,SerieRexpUP);
                SRGraph1DForm.transferCurve(Abscisses,Rdd,SerieRexpDO);
                SRGraph1DForm.transferCurve(Abscisses,Rud,SerieRexpSF); end
         else begin SRGraph1DForm.transferCurveWithError(Abscisses,Ruu,RuuS,SerieRexpUP);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rdd,RddS,SerieRexpDO);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rud,RudS,SerieRexpSF); end;
         upload1D(abscisses,Ruu,'MDS'+s+'uu');upload1D(abscisses,Rdd,'MDS'+s+'dd');upload1D(abscisses,Rud,'MDS'+s+'ud');
         end;
    if PolarisationType='with SF high field'
    then begin
         if weighting=false
         then begin SRGraph1DForm.transferCurve(Abscisses,Ruu,SerieRexpUP);
                SRGraph1DForm.transferCurve(Abscisses,Rdd,SerieRexpDO);
                SRGraph1DForm.transferCurve(Abscisses,Rud,SerieRexpSFUD);
                SRGraph1DForm.transferCurve(Abscisses,Rdu,SerieRexpSFDU); end
         else begin SRGraph1DForm.transferCurveWithError(Abscisses,Ruu,RuuS,SerieRexpUP);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rdd,RddS,SerieRexpDO);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rud,RudS,SerieRexpSFUD);
                SRGraph1DForm.transferCurveWithError(Abscisses,Rdu,RduS,SerieRexpSFDU); end;
         upload1D(abscisses,Ruu,'MDS'+s+'uu');upload1D(abscisses,Rdd,'MDS'+s+'dd');upload1D(abscisses,Rud,'MDS'+s+'ud');upload1D(abscisses,Rdu,'MDS'+s+'du');
         end;
  end;
end;


procedure TSRGraph1Dform.CopyToClipboard1Click(Sender: TObject);
begin
   XYgraph.copyToClipboard
end;

procedure TSRGraph1Dform.OpenDataFile1Click(Sender: TObject);
var CurrentDir:string;
begin
  with SimulReflecWindow do
  begin
  if OpenDialogDataFile.Execute then
  begin
     UpdateVarExperimentalParameters(MyExperimentalParameters);
     GetDir(0,CurrentDir);SaveDialogCourbeReflec.InitialDir:=CurrentDir;
     MydataExp.ReadFromFile(MyExperimentalParameters,OpenDialogDataFile.FileName);
     SRGraph1Dform.PlotDataReflectivity(MyDataExp);
     UpdateSimulReflecWindowParameters(MyExperimentalParameters);
     DataFileName.text:=OpenDialogDataFile.FileName;
  end;
  end;
end;

procedure TSRGraph1Dform.Updategraph1Click(Sender: TObject);
begin
     xyGraphPhi.XAxis.Min:=xyGraph.xaxis.min;
     xyGraphPhi.XAxis.max:=xyGraph.xaxis.max;
     xyGraphPhi.XAxis.stepsize:=xyGraph.xaxis.stepsize;
end;

procedure TSRGraph1Dform.ClearAllCurves1Click(Sender: TObject);
var i:integer;
begin
     For i:=1 to xygraph.LastSeriesIndex do
     begin
          xyGraph[i].clear;
     end;
end;

procedure TSRGraph1Dform.Clearcalculatedcurves1Click(Sender: TObject);
begin
   ClearCalc;
end;

procedure TSRGraph1Dform.Displayerrorbarslinscaleonly1Click(Sender: TObject);
begin
     if Displayerrorbarslinscaleonly1.checked=true
     then begin Displayerrorbarslinscaleonly1.checked:=false;
                xygraph.series[SerieRexpUP].drawErrors:=false;
                xygraph.series[SerieRexpDO].drawErrors:=false;
                xygraph.series[SerieRexpSFUD].drawErrors:=false;
                xygraph.series[SerieRexpSFDU].drawErrors:=false end
     else begin Displayerrorbarslinscaleonly1.checked:=true;
                xygraph.series[SerieRexpUP].drawErrors:=true;
                xygraph.series[SerieRexpDO].drawErrors:=true;
                xygraph.series[SerieRexpSFUD].drawErrors:=true;
                xygraph.series[SerieRexpSFDU].drawErrors:=true end;
end;

procedure TSRGraph1Dform.Print1Click(Sender: TObject);
begin
     xygraph.print;
end;

end.
