/*******************************************************************************
*
* McXtrace, x-ray tracing package
*         Copyright, All rights reserved
*         DTU Physics, Kgs. Lyngby, Denmark
*         Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: L_monitor
*
* %Identification
* Written by: Kristian Nielsen and Kim Lefmann
* Modified for X-ray use by Erik Knudsen
* Date: June 22, 2009
* Origin: Risoe
* Release: McXtrace 0.1
*
* Wavelength-sensitive monitor.
*
* %Description
* A square single monitor that measures the wavelength of the incoming
* xray.
*
* Example: L_monitor(xmin=-0.1, xwidth=0.1, yheight=0.1,
*           nL=20, filename="Output.L", Lmin=0.1, Lmax=1)
*
* %Parameters
* INPUT PARAMETERS:
*
* xwidth: [m]  Width of detector. 
* yheight: [m] Height of detector. 
* Lmin: [AA]   Minimum wavelength to detect.
* Lmax: [AA]   Maximum wavelength to detect.
* nL: [m]      Number of wavelength channels.
* filename: [str]  Name of file in which to store the detector image.
* restore_xray: [ ]If set, the monitor does not influence the x-ray state.
*
* CALCULATED PARAMETERS:
*
* L_N:      Array of x-ray counts
* L_p:      Array of x-ray weight counts
* L_p2:     Array of second moments
*
* %End
*******************************************************************************/

DEFINE COMPONENT L_monitor

SETTING PARAMETERS (int nL=20, string filename=0, 
  xwidth=0.1, yheight=0.1, Lmin, Lmax, restore_xray=0)

DECLARE
%{
  DArray1d L_N;
  DArray1d L_p;
  DArray1d L_p2;
  double xmin;
  double xmax;
  double ymin;
  double ymax;
%}

INITIALIZE
%{
    int i;

    xmax = xwidth/2;  xmin = -xmax;
    ymax = yheight/2; ymin = -ymax;

    if ((xmin >= xmax) || (ymin >= ymax)) {
            printf("L_monitor (%s): Null detection area !\n"
                   "ERROR      (xwidth,yheight,xmin,xmax,ymin,ymax). Exiting",
           NAME_CURRENT_COMP);
      exit(0);
    }

  L_N = create_darr1d(nL);
  L_p = create_darr1d(nL);
  L_p2 = create_darr1d(nL);

  // Use instance name for monitor output if no input was given
  if (!strcmp(filename,"\0")) sprintf(filename,"%s",NAME_CURRENT_COMP);
%}

TRACE
%{
    int i;
    double L;

    PROP_Z0;
    if (x>xmin && x<xmax && y>ymin && y<ymax)
    {
      L = 2*PI/sqrt(kx*kx + ky*ky + kz*kz);
      i = floor((L-Lmin)*nL/(Lmax-Lmin));
      if(i >= 0 && i < nL)
      {
#pragma acc atomic
        L_N[i] = L_N[i] + 1;
#pragma acc atomic
        L_p[i] = L_p[i] + p;
#pragma acc atomic
        L_p2[i] = L_p2[i] + p*p;
        SCATTER;
      }
    }
    if (restore_xray) {
      RESTORE_XRAY(INDEX_CURRENT_COMP, x, y, z, kx, ky, kz, phi, t, Ex, Ey, Ez, p);
    }
%}

SAVE
%{
    DETECTOR_OUT_1D(
        "Wavelength monitor",
        "Wavelength [AA]",
        "Intensity",
        "L", Lmin, Lmax, nL,
        &L_N[0],&L_p[0],&L_p2[0],
        filename);
%}

MCDISPLAY
%{
  multiline(5, (double)xmin, (double)ymin, 0.0,
               (double)xmax, (double)ymin, 0.0,
               (double)xmax, (double)ymax, 0.0,
               (double)xmin, (double)ymax, 0.0,
               (double)xmin, (double)ymin, 0.0);
%}

END
