
      program  Binning_Orbit_L2_to_1deg_L3_Data
      implicit none
      integer*4 k,kk,kkk,kk1,kk2,kk3,irec,nnn,nvars,
     &          i,j,lons,lats,ii,jj
      parameter (nnn=1720*152,nvars=3,lons=360,lats=180)
      real*4    wspd(nvars,nnn),lon(nnn),lat(nnn),missing,
     &          grid(lons,lats,nvars),obs(lons,lats,nvars)

c     **************************************************************************
      open(8, file='data/scatsat_wind_binned.dat',form='unformatted',
     & status='unknown', access='direct',recl=lons*lats*4)

      open(21,file='data/scatsat_wind_dump.dat', form='formatted')
c     **************************************************************************
      data num_obs/nvars*0./
      data irec/0/,missing/-9999.0/

c     **************************************************************************

 666  continue
      irec=irec+1 

      read(21,*,end=888) wspd(1,irec),wspd(2,irec),lon(irec),
     &  lat(irec)

      write(97,*) lon(irec)
      if(lon(irec).gt.180.) lon(irec)=lon(irec)-360
      write(97,*) lon(irec)

      goto 666
 888  continue
c     **************************************************************************

      do k=1,nvars
      do i=1,lons
      do j=1,lats
      grid(i,j,k)=0.
      obs(i,j,k)=0.
      enddo
      enddo
      enddo
c     **************************************************************************
      kkk=irec-1

      do 315 kk=1, kkk
      xx=lon(kk)
      yy=lat(kk)

      ii=int(xx+180.)+1
      jj=int(yy+ 90.)+1
      if(xx.eq.180.)  ii=360
      if(yy.eq. 90.)  jj=180

      do k=1,nvars
      grid(ii,jj,k)=grid(ii,jj,k)+wspd(k,kk)
      obs(ii,jj,k)=obs(ii,jj,k)+1
      enddo
 315  continue
c     **************************************************************************

      do i=1,lons
      do j=1,lats
      if(obs(i,j,k).ne.0.) grid(i,j,k)=grid(i,j,k)/obs(i,j,k)
      if(obs(i,j,k).eq.0.) grid(i,j,k)=missing1

      if(obs(i,j,k).ne.0.) grid2(i,j,k)=grid2(i,j,k)/obs(i,j,k)
      if(obs(i,j,k).eq.0.) grid2(i,j,k)=missing1

      if(obs(i,j,k).eq.0.) obs(i,j,k)=missing1
      enddo
      enddo

 310  continue
c     **************************************************************************


       irec=1
       do 145 k=1,nvars
       write(8,rec=irec) ((grid(i,j,k), i=1,lons),j=1,lats)
       irec=irec+1
       write(8,rec=irec) ((obs(i,j,k),  i=1,lons),j=1,lats)
       irec=irec+1
 145   continue
c     **************************************************************************
 103   format(10i8)
      stop
      end
c     **************************************************************************
      subroutine grid_1deg
      integer*8   k,kk,kkk,irec,nnn,mmm,nvars,i,j,ii,jj,lons,lats
      parameter (nnn=180000,nvars=3,lons=360,lats=180)
      real*4    wspd(nvars,nnn),lon(nvars,nnn),lat(nvars,nnn),
     &          qai(nvars,nnn),missing1,missing2,xx,yy,
     &          grid(lons,lats,nvars),obs(lons,lats,nvars),
     &          grid2(lons,lats,nvars),num_obs(nvars)
 
      common /stat/wspd,qai,lon,lat,num_obs,grid,grid2,obs,missing1
c     **************************************************************************

      do k=1,nvars
      do i=1,lons
      do j=1,lats
      grid(i,j,k)=0.
      grid2(i,j,k)=0.
      obs(i,j,k)=0.
      enddo
      enddo
      enddo
c     **************************************************************************

      do 310 k=1,nvars
      kkk=int(num_obs(k))
      if(kkk.ne.0) then

      do 315 kk=1, kkk
      xx=lon(k,kk)
      yy=lat(k,kk)

      ii=int(xx+180.)+1
      jj=int(yy+ 90.)+1
      if(xx.eq.180.)  ii=360
      if(yy.eq. 90.)   jj=180
      
      grid(ii,jj,k)=grid(ii,jj,k)+wspd(k,kk)
      grid2(ii,jj,k)=grid2(ii,jj,k)+qai(k,kk)
      obs(ii,jj,k)=obs(ii,jj,k)+1
 315  continue


      do i=1,lons
      do j=1,lats
      if(obs(i,j,k).ne.0.) grid(i,j,k)=grid(i,j,k)/obs(i,j,k)
      if(obs(i,j,k).eq.0.) grid(i,j,k)=missing1

      if(obs(i,j,k).ne.0.) grid2(i,j,k)=grid2(i,j,k)/obs(i,j,k)
      if(obs(i,j,k).eq.0.) grid2(i,j,k)=missing1

      if(obs(i,j,k).eq.0.) obs(i,j,k)=missing1
      enddo
      enddo

      endif
c     **************************************************************************
      print *, kkk
      if(kkk.eq.0) then
      write(6,*) "observation number is zero"

      do i=1,lons
      do j=1,lats
      grid(i,j,k)=missing1
      grid2(i,j,k)=missing1
      obs(i,j,k)=missing1
      enddo
      enddo
      endif

 310  continue
c     **************************************************************************
      return
      end
c     **************************************************************************

