      program calcmean
      implicit none

c -----
c number of data
      integer NDAT
      parameter (NDAT=121)

c missing value
      real    MISS
      parameter (MISS=-999.)

c number of bins for wind direction
      integer NB
      parameter (NB=9)

c IO
      integer IFILE
      parameter (IFILE=21)

c -----
c time
      character*8  HHMMSS

c number of data, histgram of wind direction
      integer nobs
      real    wdbin(NB)

c data
      real    t, ws
      integer wd

c local
      real    angle

c loop control
      integer i, j

c -----
c calculate mean temperature
c -initialization
      nobs = 0
      do 210 j = 1, NB
        wdbin(j) = 0.0
 210  continue
c -read data
      open(IFILE,file='datafile')
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      do 230 i = 1, NDAT
        read(IFILE,*) HHMMSS, t, wd, ws
        if ((wd.ne.MISS).and.(ws.ne.MISS)) then
            nobs = nobs + 1
            if (wd.eq.0) then
                wdbin(9) = wdbin(9) + 1
                go to 221
            end if
            do 220 j = 1, NB-1
              if (wd.eq.j) then
                  wdbin(j) = wdbin(j) + 1
                  go to 221
              end if
 220       continue
           write(*,*) 'wrong data'
           stop
 221       continue
        end if
 230  continue
      close(IFILE)
c normalization
      do 250 j = 1, NB
        wdbin(j) = wdbin(j) / nobs
 250  continue


c output
      j = 9
      angle = -(360.0/8.0)
      write(*,*) 'calm', angle, wdbin(9)
      j = 1
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'N',  angle, wdbin(j)
      j = 2
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'NE', angle, wdbin(j)
      j = 3
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'E',  angle, wdbin(j)
      j = 4
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'SE', angle, wdbin(j)
      j = 5
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'S',  angle, wdbin(j)
      j = 6
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'SW',  angle, wdbin(j)
      j = 7
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'W',  angle, wdbin(j)
      j = 8
      angle = (j-1)*(360.0/8.0)
      write(*,*) 'NW', angle, wdbin(j)


      stop
      end
