
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C***************************
C**     LSM_CALFUN        **
C***************************
C
C **** LSM SERVICE ROUTINE ONLY ****
C
      SUBROUTINE LSM_CALFUN(M,N,FVECC,X)
C
C
      INCLUDE 'implicit_none.finc'
      INCLUDE 'limits.finc'
C
C Purpose: Calculate functions for least squares refinement (for LSM_REFINE)
C
C Author:  Steffi Arzt, John Campbell, July 1997
C
C Arguments:
C
      INTEGER M, N
      DOUBLE PRECISION FVECC(*)   
      DOUBLE PRECISION X(MXPAR) 

C Parameters:
C  M       i  (R)   Number of functions (= no. of equivalent reflections)
C  N       i  (R)   Number of paramters to be refined
C  FVECC   dp (W)   An array, dimensioned to at least M, returning
C                   the M values of f(j), j=1...M calculated using
C                   the refined parameter values.
C  X       dp (R)   Array containing the parameters to be refined
C
C
C====== PARAMETERS:
C

C
C====== GLOBALS:
C
      INCLUDE 'next_refobs.finc'
      INCLUDE 'scp.finc'
      INCLUDE 'scps_counters.finc'
      INCLUDE 'other_scale.finc'
      INCLUDE 'x_array.finc'
      INCLUDE 'ref_flags.finc'
      INCLUDE 'use_reflns.finc'
      INCLUDE 'refine_lsm.finc'
C
C====== FUNCTION TYPES:
C
      INTEGER LSM_NEXT_REFOBS
C
C====== LOCALS:
C
      INTEGER I, MM
      INTEGER NR
      INTEGER IERR
C     INTEGER I, MM, NM     ! Temp/loop variables 
C     INTEGER NR            ! No. of reflections in set of equivalents
C     INTEGER IERR          ! Error flag
C     LOGICAL REFERENCE     ! Flag = .true.  return reference intensities
C                           ! = .false. return weighted mean scaled intensities



C
C----------------------------------------------------------------------------
C
      CALL LSM_TOP_LIRL(LINDX_USE, IERR)

      CALL LSM_SCP_X (2, X)

      CALL LSM_CALC_PRESETS 

      MM = 0

 10   NR = LSM_NEXT_REFOBS(REFSETFLAG)       
      IF (NR.LE.-1) THEN
         IF (MM.NE.M) THEN
            WRITE(6,1000)
            WRITE(6,2000)
            STOP
         END IF
         RETURN
      END IF

      DO 20 I = 1, NR
         MM = MM + 1
         FVECC(MM) = (SCALINT(I)-REFMEAN(I))/SCALSIG(I)
 20   CONTINUE

      GO TO 10

 1000 FORMAT (/,' ***  ERROR  *** IN LSM_CALFUN',/,
     +        ' (MM .NE. M)' )
 2000 FORMAT (//,' ******** PROCESSING STOPPED *******')

      END
