      CHARACTER*32 LAB

      PARAMETER(LEVMAX=100)
      parameter (ijgrid=26*22, LEVL=18, LEVL1=LEVL+1)

      dimension idate4(4)
c      INTEGER*4 IDATE4(4)
c      REAL*4 SI4( LEVL1 ),SL4( LEVL ),FHOUR4,ARR4( ijgrid )
      REAL*4 SI4( LEVL1 ),SL4( LEVL ),ARR4( ijgrid )
      REAL*4 DUMMY4(2*LEVMAX+1- LEVL1 - LEVL )
     1         ,EXT4(512-(6+2*LEVMAX))
      REAL*4 FLAT4( ijgrid ), FLON4( ijgrid ),
     1       FM24( ijgrid ), FM2X4( ijgrid ), FM2Y4( ijgrid )


      INTEGER*8 IDATE8(4)
      REAL*8 SI8( LEVL1 ),SL8( LEVL ),FHOUR8,ARR8( ijgrid )
      REAL*8 DUMMY8(2*LEVMAX+1- LEVL1 - LEVL )
     1         ,EXT8(512-(6+2*LEVMAX))
      REAL*8 FLAT8( ijgrid ), FLON8( ijgrid ),
     1       FM28( ijgrid ), FM2X8( ijgrid ), FM2Y8( ijgrid )





c
      pi=4*ATAN(1.0)
      iverbose=5

      ND=30
      N=60

      OPEN(ND,form='FORMATTED',status='OLD',READONLY)
      OPEN(N,form='UNFORMATTED',status='UNKNOWN')


      READ(ND,'(a32)') LAB
         if (iverbose .gt. 3 )
     *   print*,"In Lab=",LAB
      WRITE(N) LAB
C
      READ(ND,66) FHOUR4,IDATE4,(SI4(K),K=1, LEVL1 ),(SL4(K),K=1, LEVL )
c    1         ,DUMMY4,EXT4
   66 format(e12.6,4i5,19e12.6,18e12.6)
         if (iverbose .gt. 3 ) then
          print*,'-SI-SL----------------------------'
          print*,si4
          print*,' '
          print*,sl4
          print*,'----------------------------------'
         endif
      fhour8=fhour4
      idate8=idate4
         si8=si4
         sl8=sl4
      print *,'8and4',fhour8,idate8,fhour4,idate4
c      WRITE(N) FHOUR8,IDATE8,(SI8(K),K=1, LEVL1 ),(SL8(K),K=1, LEVL )
      WRITE(N) FHOUR4,IDATE4,(SI8(K),K=1, LEVL1 ),(SL8(K),K=1, LEVL )
     1         ,DUMMY8,EXT8

         if (iverbose .ge. 1 )
     *    write(6,'(a11,f10.1,4i7)')'converting ',fhour4,idate4

C GZ
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8
         if (iverbose .gt. 1 )
     *   call stats4(ARR4,ijgrid,1,'OROG')


C PRESS
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8
         if (iverbose .gt. 1 )
     *   call stats4(ARR4,ijgrid,1,'PRSS')

C TE
      DO 300 K=1, LEVL
         READ(ND,'(6e12.6)') ARR4
         arr8=arr4
         WRITE(N) ARR8
         if (iverbose .gt. 1 )
     *   call stats4(ARR4,ijgrid,1,'VTMP')
300   CONTINUE

C UU VV
      DO 310 K=1, LEVL
         READ(ND,'(6e12.6)') ARR4
         arr8=arr4
         WRITE(N) ARR8
         READ(ND,'(6e12.6)') ARR4
         arr8=arr4
         WRITE(N) ARR8
310   CONTINUE

C RQ
      DO 320 K=1, LEVL
         READ(ND,'(6e12.6)') ARR4
         arr8=arr4
         WRITE(N) ARR8
         if (iverbose .gt. 1 )
     *   call stats4(ARR4,ijgrid,1,'SPHM')
320   CONTINUE

C FM2 
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8

C FM2X
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8

C FM2Y
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8

C FLAT
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8
         if (iverbose .gt. 3 )
     *   write(*,*)(arr4(i)*180./pi,i=1,ijgrid,21)

C FLON
      READ(ND,'(6e12.6)') ARR4
      arr8=arr4
      WRITE(N) ARR8
         if (iverbose .gt. 3 )
     *   write(*,*)(arr4(i)*180./pi,i=1,21)

C GESHEM
      READ(ND,'(6e12.6)',END=99) ARR4
      arr8=arr4
      WRITE(N) ARR8
*     write(77,*)arr4

      ipoint=0
      do i=1,ijgrid
         if(ARR4(i).ne.0.)ipoint=ipoint+1
      enddo

         if (iverbose .gt. 1 )
     *   write(6,*)'wet points=',ipoint

C BENGSH
      READ(ND,'(6e12.6)',END=88) ARR4
      arr8=arr4
      WRITE(N) ARR8
c     write(66,*)arr4


      STOP
c
   99 continue
      write(6,*)'***** no precip record ******'
      stop
   88 continue
         if (iverbose .gt. 2 )
     *   write(6,*)'***** no BENGSH record ******'
      stop
      END


      subroutine stats4(grd,nx,ny,clab)

      character*4 clab
      integer*4   nx,ny
      real*4      rmax,rmin,rbar,rdev
      real*4      grd(nx,ny)

      rmax=maxval(grd)
      rmin=minval(grd)
      rbar=sum(grd)/size(grd)

      if (rbar .eq. 0.0 ) then
        print*,clab//" WARNING STATS: RBAR RMIN RMAX",rbar,rmin,rmax
        return
      endif

      rdev=sqrt(sum( (grd - rbar)**2)/(size(grd)-1))

      write(*,'(a22,4(1x,f16.7))')
     * clab//" MAX MIN AVG VRC ",rmax,rmin,rbar,rdev

      return
      end

