{ Measure polarization dependent spectra
 check HTML Help file for this script for more informations }

{ block of variable definitions }
def
  SpectrumH: Curve;
  SpectrumM: Curve;
  SpectrumV: Curve;
  StartWL: Float;
  StopWL: Float;
  DeltaWL: Float;
  MeasTime: Float;
  Intensity: Int;
  TimePerPoint: Float;
  WithMagic: Bool;
  i: Int;
exec
  { >> set the script acquisition parameters HERE << }
  Sample    = "TestSample";  // Sample name
  Solvent   = "undefined";   // Solvent name
  Container = "PolarizationSpectra";    // Name of the file to create
  //
  // actual scan parameter
  StartWL      = 480; { in nm }
  StopWL       = 580;
  DeltaWL      =   5;
  TimePerPoint = 1.0; { per point and polarization in sec }
  WithMagic    = TRUE;
  { >> end set parameter, start of measurement << }

  SpectrumH.NumPoints = ((StopWL - StartWL) / DeltaWL) + 1;
  SpectrumM.NumPoints = SpectrumH.NumPoints;
  SpectrumV.NumPoints = SpectrumH.NumPoints;
  for i = 0 to SpectrumH.NumPoints - 1 step 1
    SpectrumH.x[i] = (StartWL + DeltaWL * i) * 1e-9;
    SpectrumH.y[i] = 0;
    SpectrumH.Valid[i] = FALSE;

    SpectrumM.x[i] = (StartWL + DeltaWL * i) * 1e-9;
    SpectrumM.y[i] = 0;
    SpectrumM.Valid[i] = FALSE;

    SpectrumV.x[i] = (StartWL + DeltaWL * i) * 1e-9;
    SpectrumV.y[i] = 0;
    SpectrumV.Valid[i] = FALSE;
  end;
  //
  DETECTOR_1.DETShutterPos = FALSE;
  DETECTION_MONO.MCRGratingWavelength = StartWL * 1e-9;
  DETECTOR_1.DETShutterPos = TRUE;
  for i = 0 to SpectrumH.NumPoints - 1 step 1
    DisplayStatus("Wavelength: " + (SpectrumH.x[i] * 1e9) + " nm (" + StartWL + " nm -> " + StopWL + " nm)");
    DETECTION_MONO.MCRGratingWavelength = SpectrumH.x[i];
    // Vertical
    DETECTION_POLARIZER.ACTPos = 0;
    Intensity = 0;
    MeasIntensity(TimePerPoint, Intensity);
    SpectrumV.y[i] = Intensity;
    SpectrumV.Valid[i] = TRUE;
    Plot(SpectrumV, 0);
    // Magic
    if WithMagic == TRUE then
      DETECTION_POLARIZER.ACTPos = 54.7;
      Intensity = 0;
      MeasIntensity(TimePerPoint, Intensity);
      SpectrumM.y[i] = Intensity;
      SpectrumM.Valid[i] = TRUE;
      Plot(SpectrumM, 1);
    end;
    // Horizontal
    DETECTION_POLARIZER.ACTPos = 90;
    Intensity = 0;
    MeasIntensity(TimePerPoint, Intensity);
    SpectrumH.y[i] = Intensity;
    SpectrumH.Valid[i] = TRUE;
    Plot(SpectrumH, -2);
  end;
  DETECTOR_1.DETShutterPos = FALSE;
  Comment = "Vertical spectrum";
  StoreAsSpectrum(SpectrumV);
  if WithMagic == TRUE then
    Comment = "Magic spectrum";
    StoreAsSpectrum(SpectrumM);
  end;
  Comment = "Horizontal spectrum";
  StoreAsSpectrum(SpectrumH);
  SaveToWorkspace(Container);
end.