/*
 * 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 *.c 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)
{
  double dx, dy, xnew, ynew, radius, eterm;
  int i;

  /*
   * See 'ahelp gauss2D'. No significant optimisation has
   * been attempted here.
   */
  eterm = 1.0 - ellip;
  for(i = 0; i < nelem; i++)
  {
    dx = x0[i] - xpos;
    dy = x1[i] - ypos;

    if (ellip > 0.0)
    {
      xnew = dx * cos (theta) + dy * sin (theta);
      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 */
