c (c) copyright 2003 by David G. Long, Brigham Young University
      program WRave_reader
c
c     Example program to read BYU high resolution wind field files
c     Written: D. Long    21 Aug 2003
c
      character*120 fname
c
      integer wct,ctgrid_max
      parameter (wct=76)
      parameter (ctgrid_max=wct*10)
      integer ngrid,wctgrid,watgrid,ctgrid,atgrid
c
      integer*2 wsp(ctgrid_max,4),wd(ctgrid_max,4)
      integer*2 nchoice(ctgrid_max)
      integer*2 lat(ctgrid_max),lon(ctgrid_max)

      real spds(ctgrid_max,4),dirs(ctgrid_max,4)
      real selspd(ctgrid_max),seldir(ctgrid_max)
      real lats(ctgrid_max),lons(ctgrid_max)
      integer*2 land(ctgrid_max)

      integer fa,la,fc,lc
      integer argc, argn
c
c *************************************
c
c	begin program
c
      write (*,*) 'BYU High Resolution Wind Fields File Read Program'
      write (*,*)
c
c     get input file name
c     note: this section of code may be machine dependent
c     key idea: set fname to name of file to process
c
      argc = iargc()		! number of command line arguments
      argn = 1                  ! command line argument counter
      if (argc.lt.argn) then
 1       format(1x,a,1x,$)
         write (*,1) 'Input file name?'
         read (*,'(a120)') fname
      else
         call getarg(argn,fname)
         argn = argn + 1
      endif
c
c     open input file
c     note:high resolution files are produced in big endian byte
c     order.  Fortran on some little endian machines can have trouble
c     reading the files if there is not automatic byte order conversion
c     such as the "CONVERT" option for the LF95 compiler
c
      print *,'Reading from file ',fname(1:length(fname))
      open(unit=1,file=fname,status='unknown',form='unformatted'
     $     ,CONVERT='BIG_ENDIAN' ! machine dependent option to read
     $     )                    ! big endian files on small endian machine
c
c     read input file header
c
      read(1) ngrid,wctgrid,watgrid,ctgrid,atgrid
      read(1) natstart,natend,nctstart,nctend
      read(1) iastart,iaend,icstart,icend
      print *,'File header: ',ngrid,wctgrid,watgrid,ctgrid,atgrid
      read(1) ascnode,irev,iyears,idays
      print *,ascnode,irev,iyears,idays
c
c     read input file
c
      do ia=iastart,iaend
         read(1) (nchoice(k),(wsp(k,j),wd(k,j),j=1,4),k=icstart,icend),
     $            (lat(k),lon(k),k=icstart,icend)
         do k=icstart,icend
c     decode all ambiguities
            do j=1,4
               spds(k,j)=wsp(k,j)/200.0
               dirs(k,j)=wd(k,j)/180.0
            end do
c     ambiguity selection in nchoice
            j=nchoice(k)
            if (j.lt.0) j=1
            if (j.gt.5) j=1
c     selected ambiguity
            selspd(k)=spds(k,j)
            seldir(k)=dirs(k,j)
c     set land flag
            if (lat(k).lt.0.0) then
               land(k)=1   ! land
            else
               land(k)=0   ! ocean
            endif
c     decode lat,lon
            lats(k)=(iabs(lat(k))-16201)/180.0
            lons(k)=lon(k)/180.0
         end do
c
c     now do something with the data
c
         ia1=ia-iastart+1
         do k=icstart,icend

            write(*,500) ia1,k,land(k),lats(k),lons(k),nchoice(k),
     $           selspd(k),seldir(k)
 500        format(1x,i4,1x,i3,1x,i1,1x,f6.2,1x,f7.2,1x,i1,f6.2,1x,f7.2)

         end do


      end do
      close(1)
      print *,'Input file read'
      print *
      stop
      end
