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

C
C   Evaluate the actual model
C   
      subroutine evaluate_gauss2d(fwhm, xpos, ypos, ellip, theta, ampl,
     :     x0, x1, y, nelem)

      implicit none

      integer nelem

      double precision fwhm, xpos, ypos, ellip, theta, ampl

      double precision x0(nelem), x1(nelem), y(nelem)

      double precision dx, dy, xnew, ynew, radius, eterm
      double precision factor

      integer i;

C
C   See 'ahelp gauss2D'. No significant optimisation has
C   been attempted here.
C
      eterm = 1.0d0 - ellip
      factor = -2.0d0 * log(4.0d0)
      do i = 1, nelem, 1

         dx = x0(i) - xpos
         dy = x1(i) - ypos

         if (ellip .gt. 0.0d0) then
            xnew = dx * dcos(theta) + dy*dsin(theta)
            ynew = dy * dcos(theta) - dx*dsin(theta)
            radius = sqrt(xnew*xnew*eterm*eterm + ynew*ynew) / eterm
         else
            radius = sqrt(dx*dx + dy*dy)
         endif

         y(i) = ampl * exp( (radius/fwhm)*(radius/fwhm)*FACTOR )

      end do
      end
