//
// Simple implementation of the Sherpa gauss2D model as a user model
//
// Unlike the "ascfit" usermodel version we just need to provide a
// routine that will evaluate the model. The user_model.c file
// contains routines that handle the S-Lang interface.
//
// Both *.cc files need editing if you want to add your own model(s).
//

#include <math.h>

#define FACTOR (-2.0 * log(4.0))

// prototype
void evaluate_gauss2d(double fwhm,
		      double xpos,
		      double ypos,
		      double ellip,
		      double theta,
		      double ampl,
		      double *x0,
		      double *x1,
		      double *y,
		      int nelem);

// This routine evaluates the model and fills in the output array
//
void evaluate_gauss2d(double fwhm,
		      double xpos,
		      double ypos,
		      double ellip,
		      double theta,
		      double ampl,
		      double *x0,
		      double *x1,
		      double *y,
		      int nelem)
{
  // See 'ahelp gauss2D'. No significant optimisation has
  // been attempted here.
  //
  double eterm = 1.0 - ellip;

  int i;
  for(i = 0; i < nelem; i++)
  {
    double dx = x0[i] - xpos;
    double dy = x1[i] - ypos;

    double radius;
    if (ellip > 0.0)
    {
      double xnew = dx * cos (theta) + dy * sin (theta);
      double ynew = dy * cos (theta) - dx * sin (theta);
      radius = sqrt (xnew*xnew*eterm*eterm + ynew*ynew) / eterm; 
    }
    else
    {
      radius = sqrt (dx*dx + dy*dy);
    }

    y[i] = ampl * exp ((radius/fwhm) * (radius/fwhm) * FACTOR);
  }
}

// End
