C
C   Quick implementation of the Sherpa beta1D model as a user model
C
C   See c_version/beta1d_model_c.c for comments on the required
C   routines and the parameter values
C

C   Initialize the model. As we do not need to allocate anything
C   here we just return 0 (success).
C
      integer function ascfitUserModelInit()
      implicit none
      ascfitUserModelInit = 0
      end

C   Set up the parameter values to their initial values
C
      integer function ascfitUserModelSetParamValues(i,value, min, max,
     :     name,  descr, units, state, nameLen, descLen, unitsLen)
	
      implicit none

      integer i, state, nameLen, descLen, unitsLen
      double precision value, min, max
      character*(*) name, descr, units

      character*4  r0, beta, xpos, ampl
      parameter(r0 = 'r0', beta = 'beta', xpos = 'xpos', ampl = 'ampl')

      ascfitUserModelSetParamValues = 0

C
C note: use 110 rather than 120 for exponents of min/max values
C       as 120 seems to cause problems
C
      if (i.eq.1) then
         value = 1.0
         min = 1.0d-110
         max = 1.0d110
         name(1:2) = r0
         state = 1
      else if (i.eq.2) then
         value = 1.0d-5
         min = 1.0d-5
         max = 10.0
         name(1:4) = beta
         state = 1
      else if (i.eq.3) then
         value = 0.0
         min = 0.0
         max = 1.0d110
         name(1:4) =  xpos
         state = 0
      else if (i.eq.4) then
         value = 1.0
         min = 0.0
         max = 1.0d110
         name(1:4) = ampl
         state = 1
      else
         ascfitUserModelSetParamValues = 1
      end if
      end

C   Evaluate the model and fill in the outValues array
C
      integer function ascfitUserModelFunction(parameterList,
     :     numberOfDimensions, numberOfData, outValues,
     :     data1, data2, data3, data4, data5, data6, data7)

      implicit none

      integer numberOfDimensions
      integer numberOfData

      double precision parameterList(numberOfData)
      double precision data1(numberOfData), data2(numberOfData),
     :     data3(numberOfData), data4(numberOfData),
     :     data5(numberOfData), data6(numberOfData),
     :     data7(numberOfData)
      double precision outValues(numberOfData)

      double precision r0, beta, xpos, ampl;
      double precision powterm, r02;
      integer i;

C   Sanity checks
C
      if ((4.ne.parameterList(1)) .or.
     :     (1.ne.numberOfDimensions)) then
         ascfitUserModelFunction = 1
      else

         r0 = parameterList(2)
         beta = parameterList(3)
         xpos = parameterList(4)
         ampl = parameterList(5)

C
C Evaluates:
C   f(x) = A (1+[(x-xpos)/r_o]^2)^(-3*beta+1/2)
C
         powterm = 0.5 - 3.0 * beta
         r02 = r0 * r0

         do i = 1, numberOfData, 1

            outValues(i) = ampl *
     :           (1.0 + (data1(i)-xpos)*(data1(i)-xpos)/r02)**powterm

         end do

         ascfitUserModelFunction = 0
      end if
      end

C   De-allocate any resources allocated by ascfitUserModelInit
C   
      subroutine ascfitUserModelCleanup()
      return
      end
  
