C functions to compute rms values
C
C real function rms_2vec(u,v,n)
C real function rwms_2vec(u,v,w,n)
C real function rms_1vec(u,n)

C
C Root mean square of a vector with 2 components 
      real function rms_2vec(u,v,n)  
	 integer n
	 real    u(n), v(n)

	 integer i 
	 real    sum

c        write(*,*)'rms_2vec: n ',n
	 sum = 0.0
	 if(n.ge.1) then  
	   do i = 1, n
	    sum = sum + u(i)**2 + v(i)**2  
	   enddo
	   rms_2vec = sqrt(sum/float(n)) 
	 else
           rms_2vec = 0.0 
	 endif
      return
      end


C Root weighted mean square of a vector with 2 components 
C Assume that the weigth function contains the correct normalization
C
      real function rwms_2vec(u,v,w,n)  
	 integer n
	 real    u(n), v(n),w(n)

	 integer i 
	 real    sum

	 sum = 0.0

	 if(n.ge.1) then  
	   do i = 1, n
	    sum = sum + w(i)*(u(i)**2 + v(i)**2) 
c	    write(*,*)i,u(i),v(i),w(i)
	   enddo
	   rwms_2vec = sqrt(sum) 
	 else
           rwms_2vec = 0.0 
	 endif

      return
      end

C Root mean square of a vector with 1 component 
      real function rms_1vec(u,n)  
	 integer n
	 real    u(n)

	 integer i 
	 real    sum

c        write(*,*)'rms_1vec: n ',n
	 sum = 0.0
	 if(n.ge.1) then  
	    do i = 1, n
	       sum = sum + u(i)**2   
	    enddo
	    rms_1vec = sqrt(sum/float(n)) 
	 else
            rms_1vec = 0.0 
	 endif
      return
      end

c      program main 
c
c      integer nmax 
c      parameter (nmax = 5)
C
c      integer nobs 
c      real    u(nmax), v(nmax), w(nmax)
C      real    ercov(nmax), w_delta(nmax)
C
C      real    err_var 
C
c      real    rms_2vec, rwms_2vec
c      real    rms_1vec
C
C      integer i 
C      real    rms1, rms2 
C
C      err_var = 0.05**2
c      nobs = nmax - 1
C
C
C      do i = 1, nobs 
C         ercov(i) = err_var 
C         u(i) = 0.0
C         v(i) = 0.0
C      enddo
C 
C      do i = 1, nobs
C         W_delta(i) = 1.0/(float(nobs)*ercov(i))
C      enddo
C
c      u(1) = 1.
c      v(1) = 1.
c      u(2) = -1.
c      v(2) = 1.
c      u(3) = 1.
c      v(3) = 1.
c      u(4) = 1.
c      v(4) = -1.
C
c      rms1 = rms_2vec(u,v,nobs)
c      rms2 = rms_1vec(u,nobs)
C
c      write(*,*)'rms1', rms1
c       write(*,*)'rms2', rms2
c
c       v(1) = -10.
c	v(2) =   0.
c	v(3) =  10.
c	v(4) =   2 
c      rms2 = rms_1vec(v,nobs)
c       write(*,*)'rms2', rms2
c
c       rms2 = rms_1vec(v,0)
c        write(*,*)'rms2', rms2
c
C
c       stop
c       end
