      program nvap_sample
c	 
c-----------------------------------------------------------------------------------
c
c      Link this program with the nvap_subroutines.for file.  Used for an 
c      example for reading, averaging, and writing nvap files.  Header and variable
c      information is at the end of nvap_subroutines.for file.
c
c      written by  Dave Randel  CIRA/CSU/METSAT
c      August 2003
c
c-----------------------------------------------------------------------------------
	 character*40  flname
	 real sum, rcnt, average
	 
C--------COMMON BLOCK WHICH HOLDS THE GRIDDED 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(6,'(a,$)')' input filename: '
	 read(5,'(a)') flname
c
       do igrid = 1,1000                       !loop over all grids in file
c
c-------test read_nvap----------------------------------------------------
c
	   call nvap_read(flname, igrid, istat)
	   if(istat .ne. 0) then
	        write(6,*)' may be last grid in file'
	        stop 98
	   endif
	   if(igrid.eq.1) write(6,*)' grid size = ',headi(10), headi(11)
c
c-------simple average test-----------------------------------------------
c
	   sum = 0.0
	   rcnt = 0.0
	   do i = 1, headi(10)                   !do a simple average of good pnts
	     do j = 1, headi(11)
	       if(nvdata(i,j) .ne. zindef) then
	           sum = sum + nvdata(i,j)
	           rcnt = rcnt + 1
	       endif
	     enddo
	   enddo
	   if(rcnt .ne. 0) then
	        average = sum / rcnt
	   else
	        average = -999.9
	   endif
	   write(6,*)' simple (no weighting) grid ave = ', igrid, average
c
c--------test write_nvap--------------------------------------------------
c
	   call nvap_write('test.std', igrid, istat)
	   if(istat .ne. 0) then
	        write(6,*)' ERROR writing file'
	        stop 99
	   endif
c
	 enddo
      stop
	end
