// Implementation of the routines for a virtual detector. The object
// 'detector' adds up the Stokes vectors of the detected photons
// according to their wavelength. It then can save the stored data to
// a disk.
//
// STOKES, version 1.0, Nov 2004


#include "detector-stokes-v1.0.h"


// initialize detector by setting all channels to zero
Screen::Screen()
{
  int i;

  for(i = 0; i < ResMax; i++)
  {
    I[i] = 0;
    Q[i] = 0;
    U[i] = 0;
    V[i] = 0;
  }
}


// register a photon according to its wavelength and add its
// Stokes parameters
void Screen::RegisterPhoton(Photon p, Model M)
{
  int i;
  double step;
  bool flag;

  // compute the wavelength space covered by one channel
  step = double(M.LambdaRange())/M.res();

  // search the correct channel and add the Stokes vector
  if ((p.GetLambda()>=M.LambdaMin()))
  {
    i = 0;
    flag = false;
    while ((i < M.res()) && !flag)
    {
      if (p.GetLambda() < M.LambdaMin() + (i+1) * step)
      {
        flag = true;
        I[i] = I[i]+p.GetI()*p.GetW();
        Q[i] = Q[i]+p.GetQ()*p.GetW();
        U[i] = U[i]+p.GetU()*p.GetW();
        V[i] = V[i]+p.GetV()*p.GetW();
      }
      i++;
    }
  }
}


// save the data of the detector to the disk
void Screen::SaveCurve(ofstream out_stream, int n, Model M)
{
  int i;

  for (i = 0; i < M.res(); i++)
  {

    // the Stokes parameter to be saved is denoted by n
    switch(n)
    {
      case 0:
        out_stream << I[i] << " ";
        break;
      case 1:
        out_stream << Q[i] << " ";
        break;
      case 2:
        out_stream << U[i] << " ";
        break;
      case 3:
        out_stream << V[i] << " ";
        break;
    }
  }
}
