c (c) copyright 2003 by David G. Long, Brigham Young University
        program BYUavewr_reader
c
c	sample program to read BYU high resolution ocean sigma-0 data files
c
c	Written by DGL 21 Aug 2003 + with swap code to ensure bigending inputs
c
        character*250 fname
c
        integer ctgrid_max
        parameter (ctgrid_max=76*10)
c
        integer*2 buff(ctgrid_max)     ! input sigma-0 file read buffer
        integer recl
        parameter (recl=ctgrid_max*2)

        integer ngrid,wctgrid,watgrid,ctgrid,atgrid
        integer iwind_parms,natstart,natend,nctstart,nctend
        integer irev,iyear,iday,icnt_arr,inc_correct
        real sig(ctgrid_max,4),az(ctgrid_max,4),inc(ctgrid_max,4)
        real lat(ctgrid_max), lon(ctgrid_max)
        integer*2 land(ctgrid_max)

        integer argn,argc
c
c *************************************
c
c	begin program
c
	write (*,*) 'BYU High Resolution Ocean Sigma-0 File Read Sample 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
c
	if (argc.lt.argn) then
 1	   format(1x,a,1x,$)
           write (*,1) 'avewr file name?'
           read (*,'(a250)') 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=9,file=fname,status='old',form='unformatted',
     $       recl=recl,access='direct'
     $       ,CONVERT='BIG_ENDIAN' ! machine dependent option to read
     $       )                     ! big endian files on small endian machine
c
c     read input file header
c
        nrec=1
        read(unit=9,rec=nrec,err=99) buff
        nrec=nrec+1
c
	ctgrid=buff(1)            ! high res wvc's per record
	AVE_version=buff(2)/10.0  ! data version number code
        iwind_parms=buff(3)       ! flag for wind retrieval parameters
	ngrid=buff(4)             ! number of high res pixels per low res wvc
	wctgrid=buff(5)           ! low resolution wvc cross track grid dimension
	watgrid=buff(6)           ! low resolution wvc along track grid dimension
	ctgrid=buff(7)            ! high resolution wvc cross track grid dimension
	atgrid=buff(8)            ! high resolution wvc along track grid dimension
	natstart=buff(9)          ! start hr wvc along track index
	natend=buff(10)           ! end hr wvc along track index
        if (natend.lt.0)  natend=natend+65536  ! for long P1B files
	nctstart=buff(11)         ! start hr wvc cross track index
	nctend=buff(12)           ! end hr wvc cross track index
	irev=buff(13)             ! orbit rev number 
	iyear=buff(14)            ! year
	iday=buff(15)             ! day
	ascnode=buff(16)/180.0    ! orbit ascending node
        icnt_arr=buff(17)         ! flag for cnt array
        inc_correct=buff(18)      ! incidence angle correction flag
	angle_ref_1=buff(19)/200.0 
	angle_ref_2=buff(20)/200.0
	az_angle_exclude=buff(21)/200.0

        print *,ngrid,wctgrid,watgrid,ctgrid,atgrid
        print *,natstart,natend,nctstart,nctend
	print *,ascnode,irev,iyear,iday
c
        if (ctgrid.gt.ctgrid_max) then
           print *,'*** ctgrid_max too small ***',ctgrid_max,ctgrid
           call exit(-1)
        endif
c
c     read file contents
c
        do ia=natstart,natend
           print *,'along track index:',ia
           
           read(9,rec=nrec,err=99) buff ! latitude
           nrec=nrec+1
           do k=nctstart,nctend
              if (buff(k).lt.0) then
                 land(k)=1  ! land
              else
                 land(k)=0  ! ocean
              endif
              lat(k)=(iabs(buff(k))-16201)/180.0
           end do

           read(9,rec=nrec,err=99) buff ! longitude
           nrec=nrec+1
           do k=nctstart,nctend
              lon(k)=buff(k)/180.0
           end do

c     now read sigma0 values
           do ib=1,4
              read(9,rec=nrec) buff ! AVE sigma-0
              nrec=nrec+1
              do k=nctstart,nctend
                 sig(k,ib)=buff(k)/300.0
              end do

              if (iwind_parms.eq.1) then
                 if (icnt_arr.eq.1) then
                    read(9,rec=nrec) buff ! discard cnt array if present
                    nrec=nrec+1
                 endif
                 read(9,rec=nrec) buff ! az angle
                 nrec=nrec+1
                 do k=nctstart,nctend
                    az(k,ib)=buff(k)/180.0
                 end do
                 read(9,rec=nrec) buff ! inc angle
                 nrec=nrec+1
                 do k=nctstart,nctend
                    inc(k,ib)=buff(k)/200.0
                 end do
              endif

           end do
c
c     do something with the data
c
           write(*,500) ia,ia-natstart+1
 500       format('Along track row: ',i4,' (',I4,')')
           if (iwind_parms.eq.1) then
              do k=nctstart,nctend
                 write(*,505) k,land(k),lat(k),lon(k),(sig(k,ib),ib=1,4),
     $                (az(k,ib),ib=1,4),(inc(k,ib),ib=1,4)
 505             format(1x,i3,1x,i1,1x,f6.2,1x,f7.2,4(1x,f6.2),4(1x,f7.2),4(1x,f5.1))
              end do
           else
              do k=nctstart,nctend
                 write(*,510) k,land(k),lat(k),lon(k),(sig(k,ib),ib=1,4)
 510             format(1x,i3,1x,i1,1x,f6.3,1x,f7.2,4(1x,f6.2))
              end do
           endif

       end do
c
c     close output file
c
       close(1)
       print *,'Processing completed successfully'
c
 99    continue
       print *,'*** error reading input file at along track',ia
       close(1)
c
       call exit(0)
       stop
       end
