      program readqnblendwind

c Fortran program to read binary QSCAT/NCEP blended wind data files.
c This routine can be adapted to read either the zonal and
c meridional wind data ("uv") or the windstress curl data ("curl").
c
c UV or Curl
c
c   Either read two arrays (u and v, in m/s), or one array (curl,
c   in N/m3).  Change record length for open statement, read
c   statements, and call to swap4 subroutine accordingly.
c
c Big-endian vs. little-endian
c
c   The data files were produced on a little-endian machine 
c   (a pentium PC).  Subroutine swap4 is required to read these files 
c   on big-endian machines (e.g. SGI).  The subroutine does not to be 
c   called from little-endian machines.  
c
c Geographic Grid
c
c   The data domain is a 0.5x0.5 degree grid, 720 x 353, starting at 
c   (0.5E, 88.0S) and ending at (360.0E, 88.0N).  There are data
c   values at all grid points, including land and ice points. 
c   Users who wish to examine ocean points only need to use 
c   appropriate land/ice masks.
c
c Time convention
c
c   Time is 6-hourly; the "day" variable is in days of year: 
c   e.g. January 1, 6hr = day 0.25. There is no February 29 in leap
c   years.  This data was produced for ocean models, that require
c   a constant number of days per year.  In all years days go from 
c   0.25 to 365.00.
c
c Data file types
c
c   bln : QSCAT scatterometer data blended with NCEP analyses.
c         The blended data has realistic highwavenumber variability
c         everywhere and preserves the satellite data where it occurs.
c         For each 6-hourly field, 12 hours of QSCAT satellite swaths
c         are overlayed on the global NCEP analysis map, centered on 
c         the analysis time.
c
c   low : NCEP analysis splined to the 0.5x0.5 degree grid.
c
c
c Version 3.0, June 11, 2002
c
c-----------------------------------------------------------------------

      parameter
     $( nlon = 720
     $, nlat = 353
     $)

      real
     $  dayin,day
     $, uin(nlon,nlat),u(nlon,nlat)
     $, vin(nlon,nlat),v(nlon,nlat)
     $, cin(nlon,nlat),c(nlon,nlat)

      character dfname*80
      integer idum1,idum2,idum3,idum4,idum5,idum6

      logical readuv     ! to read either uv or curl files
c
c pick datatype
c
      readuv = .true.
c
c open input
c
      if (readuv) then
         lrecl = (2*nlon*nlat + 7)*4
      else
         lrecl = (  nlon*nlat + 5)*4  ! for curl
      endif

      if (readuv) then
         dfname =   'uv.200001.bln'
      else
         dfname = 'curl.200012.bln'
      endif

      write(6,1010) dfname
 1010 format("c input= ",a80)
      open(11,file=dfname,form='unformatted',access='direct',
     $     recl=lrecl)
c
c read data
c
      nread = 0

      irec = 0
 11   irec = irec + 1
      if (readuv) then
         read(11,rec=irec,err=99) idum1,dayin,idum2,
     $                            idum3,uin  ,idum4,
     $                            idum5,vin  ,idum6
      else
         read(11,rec=irec,err=99) idum1,dayin,idum2, ! for curl
     $                            idum3,cin  ,idum4
      endif

      call swap4(dayin,day,4)
      if (readuv) then
         call swap4(uin  ,u  ,4*nlon*nlat)
         call swap4(vin  ,v  ,4*nlon*nlat)
      else
         call swap4(cin  ,c  ,4*nlon*nlat) ! for curl
      endif

      nread = nread + 1

      if (readuv) then ! print some u and v data
         ip1 =  21
         ip2 =  25
         jp  =  20
         write(6,1020) day,ip1,ip2,jp,
     $                 (u(i,jp),i=ip1,ip2),(v(i,jp),i=ip1,ip2)
         jp = 330
         write(6,1020) day,ip1,ip2,jp,
     $                 (u(i,jp),i=ip1,ip2),(v(i,jp),i=ip1,ip2)
 1020    format(/,"c day=",f12.7,
     $          " print data at i=",i4," to",i4,", j=",i4,
     $          /,"c   u=",5e16.7,/,"c   v=",5e16.7)
      endif

      if (.not.readuv) then ! print some curl data
         ip1 =  21
         ip2 =  25
         jp  =  20
         write(6,1030) day,ip1,ip2,jp,(c(i,jp),i=ip1,ip2)
         jp = 330
         write(6,1030) day,ip1,ip2,jp,(c(i,jp),i=ip1,ip2)
 1030    format(/,"c day=",f12.7,
     $          " print data at i=",i4," to",i4,", j=",i4,
     $          /,"c   c=",5e16.7)
      endif

      if (nread.gt.5) goto 99
      goto 11
c
c end of input file
c
 99   close(11)
      write(6,1040) nread
 1040 format(/,"c Nread=",i4)

      stop
      end
      subroutine swap4(in,io,nn)

c     swaps bytes in groups of 4 to compensate for byte swapping within
c     words which occurs on DEC (VAX) and PC machines.
c
c in - input array to be swapped
c io - ouput array with bytes swapped
c nn - number of bytes to be swapped
      logical*1 in(1),io(1),ih
c character*1 in(1),io(1),ih ! Cray CF90 (Version 3.0.1.3)
c Use character*1 instead of logical*1 when compiling on a Cray

        do 10 i=1,nn,4
           ih=in(i)
           io(i)=in(i+3)
           io(i+3)=ih
           ih=in(i+1)
           io(i+1)=in(i+2)
           io(i+2)=ih
   10   continue
        return
        end
