      program nvap_2ASCII

C***********************************************************************
C
C  THIS PROGRAM PRINTS OUT A NVAP CCDA FORMATTED FIELD TO AN ASCCI TEXT FILE.
c  THIS VERSION WILL PRINT OUT ALL THE FIELDS IN A FILES.
C
C  WRITTEN BY DAVE RANDEL CIRA/CSU/METSAT
C          ON APRIL 2001.
C
C**********************************************************************
C
       character flname*40, flout*40, decout*1, deca*1
C
C--------COMMON BLOCK WHICH HOLDS THE DATA AND HEADER INFORMATION---------
C
       real nvdata(720,360), headr(4), zindef
cmy fix
       integer*2 headi(11), ifld
       integer*2 isfld, iefld, nflds
cen fix
       character label*40
       common /nvapdata/ nvdata, headr, zindef, headi, label
c
c-------------------------------------------------------------------------

c
c**read input filename**********
c
        write(6,*)
  5     write(6,'(A$)')'    Input ccda filename : '
        read(5,'(A)') flname
c
c**get output filename
c
        write(6,'(A$)')' output ascii filename [test.txt] : '
        read(5,'(A)') flout
        if(flout .eq. ' ') flout = 'test.txt'
c
c
        write(6,'(A$)')' do all fields in file? (y/n) [y] : '
        read(5,'(A)') decout
        if(decout .eq. ' ' .or. decout .eq. 'y') decout = 'y'
        if(decout .eq. 'y') then
              isfld = 1
cmy fix
              nflds = 1000
cend fix
        else
             write(6,'(A$)')'      start with field number [1] : '
             read(5,'(i3)') isfld
             if(isfld .eq. 0) isfld = 1
             write(6,'(A$)')'                number of fields? : '
             read(5,'(i3)') nflds
             if(nflds .le. 0) go to 5
        endif

c**open output file
c
c       open(unit=1,file=flout,form='formatted',status='replace',
        open(unit=1,file=flout,form='formatted',
     >      access='sequential',recl = 70)
c
c*** loop over all requested fields
c      
        iefld = isfld + nflds - 1
        do 100 ifld = isfld, iefld
          call nvap_read(flname,ifld,istat)
          if(istat .ne. 0) then
	        write(6,*)' may just be last grid in file'
	        stop 98
	    endif
          zindef1 = -1.1
                                               !write out 2 lines of header information
          write(1,10)(headi(j),j=1,2),(headi(m),m=4,11)
          write(6,10)(headi(j),j=1,2),(headi(m),m=4,11)
          write(1,12)(headr(j),j=1,4),zindef1
  10      format(1x,i3,1x,i3,2x,3(i4,1x),2x,3(i4,1x),2x,i4,1x,i4)
  12      format(1x,4(f10.2,1x),1x,f7.2)
c
                                               !write out data
          do j = 1,headi(11)            !nvap-ng data fields are (720,360)
            do i = 1,headi(10)
              if(nvdata(i,j) .eq. zindef) nvdata(i,j) = zindef1
            enddo
            write(1,16)(nvdata(m,j),m=1,headi(10))
          enddo
  16      format( 10(f5.2,1x))
100     continue
c
        stop
        end
