      program nvap_glbave
C***********************************************************************
C
C  THIS PROGRAM PRODUCES A LIST OF THE GLOBAL AND HEMISPHERIC AVERAGES
C  FROM THE NVAP FORMATTED FIELDS.
c
C  WRITTEN BY DAVE RANDEL CIRA/CSU/METSAT
C          ON JULY 6, 1992  modified 8/2003
C
C**********************************************************************
C	
       real glb(3)
	 character sg*1, CGOOD(2)*5
	 character*40  flname
	 DATA CGOOD/'%GOOD','#GOOD'/
	 
c--------common block which holds the data and header information---------
c
       real nvdata(720,360), headr(4), zindef
       integer*2 headi(11)
       character label*40
       common /nvapdata/ nvdata, headr, zindef, headi, label
c
c-------------------------------------------------------------------------
c       
       write(*,*) ' '
       write(6,'(a,$)')'  Input filename: '
	 read(5,'(a)') flname
c
  1    format(A,$)
c
	 do igrid=1,1000    !read all grids in file
	   call nvap_read(flname, igrid, istat)
	   if(istat .ne. 0) then
	        write(6,*)' may just be last grid in file'
	        stop 99
	   endif
c
         call nvap_globalave(glb)    !call global average routine
c
         rngp = 0.0
         do j = 1,headi(11)                        !calc number of good pts
           do i = 1,headi(10)
             if(nvdata(i,j) .ne. zindef) rngp = rngp + 1.0
           enddo
         enddo
         gpercnt = rngp*100. / ( float(headi(10))*float(headi(11)) )

         ioutls = 1
         if(gpercnt .lt. 10.0) then
             gpercnt = rngp
         ioutls = 2
         endif
c
         if(igrid .eq. 1) then
              write(*,*)'input filename: ',flname(1:40)
              write(*,*)'fld# source param  strt date    end date   ',
     >                  '  n.hemis   s.hemis    global  ',cgood(ioutls)
         endif
     
	   if(abs(glb(1)) .gt. 0.1 .or. abs(glb(2)).gt.0.1 .or.
     >      abs(glb(3)) .gt. 0.1) then
               write(*,50)igrid,headi(1),headi(2),(headi(j),j=4,9),
     >              (glb(m),m=1,3), gpercnt
         else  
		     write(*,51)igrid,headi(1),headi(2),(headi(j),j=4,9),
     >              (glb(m),m=1,3), gpercnt
         endif

 50      format(' ',i3,2x,i4,3x,i3,3x,i4,'/',i3.3,1x,i2.2,2x,i4,'/',
     >                 i3.3,1x,i2.2,3(f10.2),1x,f8.1)
 51      format(' ',i3,2x,i4,3x,i3,3x,i4,'/',i3.3,1x,i2.2,2x,i4,'/',
     >                 i3.3,1x,i2.2,3(f10.6),1x,f8.1)
       enddo   !finish looping all grids in file
	 stop
       end
