{ Data aquisition for TRES with 2 time gates
 check HTML Help file for this script for more informations }

{ block of variable definitions }
def
  StartWL : Float;
  StopWL : Float;
  DeltaWL : Float;
  i: Int;
  TimePerPoint : Float;
  Spectrum1: Curve; { Resulting time-gated Spectum}	
  Spectrum2: Curve; { Resulting time-gated Spectum}	
  TimeGate1First: Float;
  TimeGate1Last: Float;
  TimeGate2First: Float;
  TimeGate2Last: Float;
  ns: Float;
  us: Float;
  ms: Float;
  BaseName: String;
  CorrectionCurve: String;
  SaveTRES: Bool;
  SaveSpectrum: Bool;
{ program block }
exec
  { >> set the script aqusition parameters HERE << }
  Sample    = "TestSample";  // Sample name
  Solvent   = "undefined";   // Solvent name
  Container = "TestFile";    // Name of the file to create (get a _TRES / _Spectrum at end)
  //
  SaveTRES     = TRUE;  {save Decays + Formula to get the 2 Spectra in plot}
  SaveSpectrum = TRUE;  {Save the two spectra + correction, a valid correction curve must be given}
  CorrectionCurve = "Ref_DETECTOR_1_1_M"; {name of Correction Curve, leave empty "" if no correction needed}
  //
  // actual scan parameter
  StartWL      = 480; { in nm }
  StopWL       = 580;
  DeltaWL      =   5;
  TimePerPoint =   1; { in sec }
  // define time gate parameter
  ns = 1e-9;
  us = 1e-6;
  ms = 1e-3;
  TimeGate1First  =  5 * ns;
  TimeGate1Last   = 15 * ns;
  TimeGate2First  = 15 * ns;
  TimeGate2Last   = 25 * ns;
  { >> end set parameter, start of measurement << }
  DeleteAllCurves;

  DETECTOR_1.DETShutterPos=FALSE;
  // Setup spectrum curves
  Spectrum1.NumPoints = ((StopWL - StartWL) / DeltaWL) + 1;
  Spectrum2.NumPoints = ((StopWL - StartWL) / DeltaWL) + 1;
  for i = 0 to Spectrum1.NumPoints - 1 step 1
    Spectrum1.x[i] = (StartWL + DeltaWL * i) * 1e-9;
    Spectrum1.y[i] = 0;
    Spectrum1.Valid[i] = FALSE;

    Spectrum2.x[i] = (StartWL + DeltaWL * i) * 1e-9;
    Spectrum2.y[i] = 0;
    Spectrum2.Valid[i] = FALSE;
  end;
  // Main Loop
  for i = 0 to Spectrum1.NumPoints - 1 step 1
    DisplayStatus("Wavelength: " + (Spectrum1.x[i] * 1e9) + " nm (" + StartWL + " nm -> " + StopWL + " nm)");
    DETECTION_MONO.MCRGratingWavelength = Spectrum1.x[i]; { in meter }
    // Open Shutter and measure
    DETECTOR_1.DETShutterPos = TRUE;
    Histogram(TimePerPoint,-1);
    // Calculate the 2 timegates, via integration
    Spectrum1.y[i] = integral(crv[LastMeasCurveIndex] from TimeGate1First to TimeGate1Last);
    Spectrum1.Valid[i] = TRUE;
    Spectrum2.y[i] = integral(crv[LastMeasCurveIndex] from TimeGate2First to TimeGate2Last);
    Spectrum2.Valid[i] = TRUE;
    // remove normalization
    Spectrum1.y[i] = Spectrum1.y[i] / (crv[LastMeasCurveIndex].x[1] - crv[LastMeasCurveIndex].x[0] + 1e-30);
    Spectrum2.y[i] = Spectrum2.y[i] / (crv[LastMeasCurveIndex].x[1] - crv[LastMeasCurveIndex].x[0] + 1e-30);
    // plot result
    Plot(Spectrum1, 0); // second parameter = 0 for clearing the plot
    Plot(Spectrum2, -1); // second parameter < 0 for redrawing the plot
  end;
  DETECTOR_1.DETShutterPos=FALSE;
  { >> end of measurement, start of saving part << }
// #### The TRES saving Part
  if SaveTRES == TRUE then
    Formula = "def
  Spectrum1: Curve;
  Spectrum2: Curve;
  i: Int visible = FALSE;
  WL: Float visible = FALSE;
exec
  Spectrum1.NumPoints = NumCurves;
  Spectrum2.NumPoints = Spectrum1.NumPoints;
  for i = 0 to Spectrum1.NumPoints - 1 step 1
    // Change here the 2 time gates
    Spectrum1.y[i] = integral(crv[i] from " + TimeGate1First + " to " + TimeGate1Last + ");
    Spectrum2.y[i] = integral(crv[i] from " + TimeGate2First + " to " + TimeGate2Last + ");
    WL = i;
    GetCurveParam(crv[i], 'Det_WaveLength', WL);
    Spectrum1.x[i] = WL;
    Spectrum2.x[i] = Spectrum1.x[i];
    Spectrum1.Valid[i] = TRUE;
    Spectrum2.Valid[i] = TRUE;
    Spectrum1.y[i] = Spectrum1.y[i] / (crv[i].x[1] - crv[i].x[0] + 1e-30);
    Spectrum2.y[i] = Spectrum2.y[i] / (crv[i].x[1] - crv[i].x[0] + 1e-30);
  end;
end.
";
    SaveToWorkspace(Container + "_TRES");
  end;
  //
  DeleteAllCurves;
// #### The Spectrum saving part
  StoreAsSpectrum(Spectrum1);
  StoreAsSpectrum(Spectrum2);
  if SaveSpectrum == TRUE then
    if not (CorrectionCurve == "") then
      StoreRefCurve(CorrectionCurve);
      Formula = "Spectrum1 = crv[0] * (crv[2] / 10000) precision = 2;" /
                "Spectrum2 = crv[1] * (crv[2] / 10000) precision = 2;";
      SaveToWorkspace(Container + "_Spectrum");
    end;
  end;

end.