 

C++*********************************************************************
C
C NORMASS.F
C                     SQRT NEGATIVE TRAP          AUG 2005 ArDean Leith
C
C **********************************************************************
C=* FROM: SPIDER - MODULAR IMAGE PROCESSING SYSTEM.   AUTHOR: J.FRANK  *
C=* Copyright (C) 1985-2005  Health Research Inc.                      *
C=*                                                                    *
C=* HEALTH RESEARCH INCORPORATED (HRI),                                *   
C=* ONE UNIVERSITY PLACE, RENSSELAER, NY 12144-3455.                   *
C=*                                                                    *
C=* Email:  spider@wadsworth.org                                       *
C=*                                                                    *
C=* This program is free software; you can redistribute it and/or      *
C=* modify it under the terms of the GNU General Public License as     *
C=* published by the Free Software Foundation; either version 2 of the *
C=* License, or (at your option) any later version.                    *
C=*                                                                    *
C=* This program is distributed in the hope that it will be useful,    *
C=* but WITHOUT ANY WARRANTY; without even the implied warranty of     *
C=* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
C=* General Public License for more details.                           *
C=*                                                                    *
C=* You should have received a copy of the GNU General Public License  *
C=* along with this program; if not, write to the                      *
C=* Free Software Foundation, Inc.,                                    *
C=* 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.      *
C=*                                                                    *
C **********************************************************************
C
C NORMASS(X,NS1,NS2,NR1,NR2,IR1,IR2)
C
C SERIALLY NORMALIZES X BY VARIANCE
C
C  NOTE   :    FOR PARALLEL USE NORMAS INSTEAD al Sept 01
C              DIFFICULT TO ADD ERROR FLAG DUE TO USE INSIDE
C              PARRALLEL REGION aug 05 al
C
C23456789012345678901234567890123456789012345678901234567890123456789012
C--*********************************************************************

        SUBROUTINE  NORMASS(X,NS1,NS2,NR1,NR2,IR1,IR2)

        DIMENSION  X(NS1:NS2,NR1:NR2)
        REAL*8     AV,VR,VRINV,DTEMP

        I1SQ = IR1 * IR1
        I2SQ = IR2 * IR2
        AV   = 0.0
        VR   = 0.0
        N    = 0

        DO J=NR1,NR2
           JSQ = J * J
           DO I=NS1,NS2
              IR = JSQ + I * I
              IF (IR .GE. I1SQ .AND. IR .LE. I2SQ)  THEN
	         N  = N  + 1
                 AV = AV + X(I,J)
                 VR = VR + X(I,J)*X(I,J)
              ENDIF
           ENDDO
        ENDDO

        AV   = AV / N
        DTEMP = (VR - N * AV * AV)
        IF (DTEMP .GT. 0) THEN
           
           VR    = DSQRT(DTEMP / (N-1))
           VRINV = 1.0 / VR

C          ARRAY OPERATION ON X
           X = (X - AV) * VRINV
        ELSE
C          TRAP FOR BLANK IMAGE AREA

C          ARRAY OPERATION ON X
           X = 0
        ENDIF

        END
