anchor.diff:                  NO = NO + 1
               END IF
           END DO
  -        
  +
           CLOSE(UNIT_NO)
       END IF
  -        
  +
       RETURN
  -        
  +
   END SUBROUTINE READ_SCATTER_FILE
  -      
  +
   SUBROUTINE QRANGE_MATCH(QOBS, QCALC, ICALC, XNO, CNO, IMATCH, MNO)
  - 
  -    ! Match modelled I values with experimental Q range 
  -        
  +
  +    ! Match modelled I values with experimental Q range
  +
       ! QOBS = Experimental Q values
       ! QCALC, ICALC = Modelled Q and I values
       ! XNO = no. experimental values, CNO = no. modelled values
       ! IMATCH = Modelled I values matched to experimental Q values
       ! MNO = no. matched values
  -    DOUBLE PRECISION, DIMENSION(*), INTENT(IN) :: QOBS, QCALC, ICALC    
  +    DOUBLE PRECISION, DIMENSION(*), INTENT(IN) :: QOBS, QCALC, ICALC
       INTEGER, INTENT(IN) :: XNO, CNO
       DOUBLE PRECISION, DIMENSION(*), INTENT(INOUT) :: IMATCH
       INTEGER, INTENT(OUT) :: MNO
  -        
  +
       INTEGER :: I, J
       DOUBLE PRECISION :: QMINDIFF, DELTAQ
   
  
positive.code:     ELSE
          OPEN(UNIT=UNIT_NO, FILE=FILENAME, STATUS='OLD', ACTION='READ')
          NO = 1
          DO 
              READ(UNIT_NO, *, IOSTAT=IERR) TMPQ, TMPI
              IF (IERR .LT. 0) THEN
                  NO = NO - 1
  
negative.code:     AVERAGE_DATA = TOTAL / N
  
  END FUNCTION AVERAGE_DATA
  
  DOUBLE PRECISION FUNCTION CALC_RFACTOR (QOBS, IOBS, ICALC, N, QMIN, QMAX, CON, VERBOSE)
  
      ! Calculate the R factor comparing IOBS to IMATCH
      ! Rfactor is used by analogy with crystallography where:
      ! R = sum (abs(F_expt - F_calc)) / sum (abs(F_expt))
  
      DOUBLE PRECISION, DIMENSION(*), INTENT(IN) :: QOBS, IOBS, ICALC
      INTEGER, INTENT(IN) :: N
      DOUBLE PRECISION, INTENT(IN) :: QMIN, QMAX
      DOUBLE PRECISION, INTENT(INOUT) :: CON
      LOGICAL, INTENT(IN) :: VERBOSE
  
      DOUBLE PRECISION :: DELTAC, ORFAC, RFNUM, RFDEN, RFACTOR
      INTEGER :: NDX
  
      ! Initialize the update and R factor
      DELTAC = CON / 10.
      RFACTOR = 1000000.
  
      DO WHILE ( ( ABS(DELTAC) ) .GT. ( CON / 10000. ) )
  
          ORFAC = RFACTOR
          RFACTOR = 0.
          RFNUM = 0.
          RFDEN = 0.
  
          DO NDX = 1, N
              IF ( ( QOBS(NDX) .LE. QMAX ) .AND. ( QOBS(NDX) .GT. QMIN ) ) THEN
                  RFNUM = RFNUM + ABS( ( IOBS(NDX) / CON ) - ICALC(NDX) )
                  RFDEN = RFDEN + ABS( IOBS(NDX) / CON )
              END IF
          END DO
  
          RFACTOR = RFNUM / RFDEN
  
          IF (VERBOSE) THEN
              WRITE(*,*) 'BEST YET:', DELTAC, CON, RFACTOR
          END IF
  
          IF (RFACTOR .LT. ORFAC) THEN
              CON = CON + DELTAC
          ELSE
              DELTAC = DELTAC * (-0.5)
              CON = CON + DELTAC
          ENDIF
  
      END DO
  
      IF (VERBOSE) THEN
          WRITE(*,*) 'FINISHED WITH:', DELTAC, CON, RFACTOR
      END IF
  
      CALC_RFACTOR = RFACTOR * 100
  
  END FUNCTION CALC_RFACTOR
  