      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 Version 4.0, February 26, 2003: the sample output is from version 2.0
c                                 blended winds
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.200012.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,end=99) idum1,dayin,idum2,
     $                            idum3,uin  ,idum4,
     $                            idum5,vin  ,idum6
      else
         read(11,rec=irec,end=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

c-----------------------------------------------------------------------

      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

c-------------------------------------------------------------------------------------
c sample uv output
c-------------------------------------------------------------------------------------

c input= uv.200012.bln

c day= 334.2500000 print data at i=  21 to  25, j=  20
c   u=  -0.6261527E+01  -0.6391816E+01  -0.6628040E+01  -0.6944738E+01  -0.7281798E+01
c   v=  -0.3976547E+01  -0.4061101E+01  -0.4198959E+01  -0.4371038E+01  -0.4548159E+01

c day= 334.2500000 print data at i=  21 to  25, j= 330
c   u=  -0.8690641E+00   0.1493269E+01  -0.1314949E+00   0.9986894E+00   0.8866047E+00
c   v=  -0.1781888E+00   0.2572129E+01  -0.1234992E+00  -0.3034407E+01  -0.3978628E+00

c day= 334.5000000 print data at i=  21 to  25, j=  20
c   u=  -0.1635436E+01  -0.1657879E+01  -0.1645074E+01  -0.1670709E+01  -0.1702627E+01
c   v=  -0.7123935E+01  -0.7367778E+01  -0.7504821E+01  -0.7516336E+01  -0.7387633E+01

c day= 334.5000000 print data at i=  21 to  25, j= 330
c   u=   0.5737838E+00   0.5515110E+00   0.3158028E+00   0.1466654E-01  -0.3804827E+00
c   v=  -0.4946753E+01  -0.5067935E+01  -0.5138824E+01  -0.5100097E+01  -0.4899036E+01

c day= 334.7500000 print data at i=  21 to  25, j=  20
c   u=  -0.4167575E+01  -0.4295633E+01  -0.4373021E+01  -0.4431966E+01  -0.4432241E+01
c   v=  -0.3774283E+01  -0.3661986E+01  -0.3605659E+01  -0.3613662E+01  -0.3690331E+01

c day= 334.7500000 print data at i=  21 to  25, j= 330
c   u=  -0.4194596E+01  -0.3793672E+01  -0.4610442E+01  -0.3819447E+01  -0.3499499E+01
c   v=  -0.1312240E+01  -0.1140066E+01  -0.1961668E+01  -0.2026333E+01  -0.1965222E+01

c day= 335.0000000 print data at i=  21 to  25, j=  20
c   u=  -0.3480863E+01  -0.3413593E+01  -0.3413718E+01  -0.3427837E+01  -0.3452338E+01
c   v=  -0.5508242E+01  -0.5592972E+01  -0.5631907E+01  -0.5615794E+01  -0.5599281E+01

c day= 335.0000000 print data at i=  21 to  25, j= 330
c   u=  -0.4194983E+01  -0.3794250E+01  -0.4610260E+01  -0.3818083E+01  -0.3496243E+01
c   v=  -0.1311776E+01  -0.1139917E+01  -0.1962011E+01  -0.2027861E+01  -0.1968535E+01

c day= 335.2500000 print data at i=  21 to  25, j=  20
c   u=  -0.5316371E+01  -0.5104924E+01  -0.4972486E+01  -0.4906077E+01  -0.4860748E+01
c   v=  -0.7137482E+01  -0.7277713E+01  -0.7392037E+01  -0.7460644E+01  -0.7484635E+01

c day= 335.2500000 print data at i=  21 to  25, j= 330
c   u=  -0.2166968E+01  -0.2223098E+01  -0.2352385E+01  -0.2373809E+01  -0.2560740E+01
c   v=  -0.2293304E+01  -0.2104609E+01  -0.1950466E+01  -0.2450455E+01  -0.1572295E+01

c day= 335.5000000 print data at i=  21 to  25, j=  20
c   u=  -0.2019876E+01  -0.1988412E+01  -0.2005970E+01  -0.2049413E+01  -0.2104703E+01
c   v=  -0.7446113E+01  -0.7553129E+01  -0.7662796E+01  -0.7779762E+01  -0.7880020E+01

c day= 335.5000000 print data at i=  21 to  25, j= 330
c   u=   0.1194302E+00  -0.1297664E+01  -0.1272057E+01  -0.1518909E+01  -0.1520632E+01
c   v=  -0.9302226E+00  -0.6681840E+00  -0.7768011E+00  -0.8876370E+00  -0.4797976E+00

c Nread=  6


c-------------------------------------------------------------------------------------
c sample curl output
c-------------------------------------------------------------------------------------

c input= curl.200012.bln

c day= 334.2500000 print data at i=  21 to  25, j=  20
c   c=   0.4700762E-07   0.2483775E-07  -0.8615757E-07  -0.1829566E-06  -0.2690410E-06

c day= 334.2500000 print data at i=  21 to  25, j= 330
c   c=  -0.1618842E-06   0.1679314E-06  -0.1806514E-06  -0.6358338E-06   0.3288401E-06

c day= 334.5000000 print data at i=  21 to  25, j=  20
c   c=  -0.2110851E-06  -0.1910313E-06  -0.2146487E-07   0.1978483E-06   0.4042045E-06

c day= 334.5000000 print data at i=  21 to  25, j= 330
c   c=  -0.1844057E-06  -0.1604944E-06  -0.8700629E-07   0.3533102E-07   0.2207377E-06

c day= 334.7500000 print data at i=  21 to  25, j=  20
c   c=   0.1221521E-06   0.6417191E-07  -0.5203414E-07  -0.1433947E-06  -0.2213669E-06

c day= 334.7500000 print data at i=  21 to  25, j= 330
c   c=   0.1946323E-07  -0.3245196E-06  -0.4222066E-06   0.4014844E-06  -0.1779394E-06

c day= 335.0000000 print data at i=  21 to  25, j=  20
c   c=  -0.9305947E-07  -0.8726962E-08   0.7547640E-07   0.9576736E-07   0.7005239E-07

c day= 335.0000000 print data at i=  21 to  25, j= 330
c   c=   0.1932649E-07  -0.3248668E-06  -0.4227005E-06   0.3999208E-06  -0.1796330E-06

c day= 335.2500000 print data at i=  21 to  25, j=  20
c   c=  -0.1000767E-06  -0.1217519E-06  -0.1495817E-06  -0.1701649E-06  -0.2047416E-06

c day= 335.2500000 print data at i=  21 to  25, j= 330
c   c=  -0.1533412E-09   0.4047371E-07  -0.2203242E-06   0.2544934E-08   0.7261985E-06

c day= 335.5000000 print data at i=  21 to  25, j=  20
c   c=   0.4861004E-07   0.6683826E-07   0.5546768E-07   0.4942357E-07   0.1069590E-06

c day= 335.5000000 print data at i=  21 to  25, j= 330
c   c=  -0.8947296E-07  -0.1086711E-06  -0.1468849E-06  -0.1264342E-06  -0.1422434E-06

c Nread=  6






