      program calcmean
      implicit none

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

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

c IO
      integer IFILE
      parameter (IFILE=21)

c maximum number of observer
      integer NPMAX
      parameter (NPMAX=32)

c -----
c time
      character*8  HHMMSS(NDAT)

c number of data, temperature
      integer nobs(NDAT)
      real    ta(NDAT), tsd(NDAT)
      real    wa(NDAT), wsd(NDAT)

c instrumental error
      real    tcorrect(NPMAX)

c data
      real    t, wd, ws

c loop control
      integer i, j

c -----
c read instrumental error
      open(IFILE,file='instrument.dat')
 110  continue
      read(IFILE,*,end=120) i, t
      if (i.gt.NPMAX) then
          write(*,*) 'data length is larger than NPMAX'
          stop
      end if
      tcorrect(i) = t
      go to 110
 120  continue
      close(IFILE)


c calculate mean temperature
c -initialization
      do 210 i = 1, NDAT
        nobs(i) = 0
        ta(i)   = 0.0
 210  continue
c -read data
      j = 0
      open(IFILE,file='datafile')
 220  continue
      j = j + 1
      read(IFILE,*,end=240)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      do 230 i = 1, NDAT
        read(IFILE,*) HHMMSS(i), t, wd, ws
        if (t.ne.MISS) then
            nobs(i) = nobs(i) + 1
            ta(i)   = ta(i)   + ( t - tcorrect(j) )
        end if
 230  continue
      go to 220
 240  continue
      close(IFILE)
c -calculate mean
      do 250 i = 1, NDAT
        ta(i) = ta(i) / nobs(i)
 250  continue

c calculate standard deviation of temperature
c -initialization
      do 260 i = 1, NDAT
        nobs(i) = 0
        tsd(i)  = 0.0
 260  continue
c -read data
      j = 0
      open(IFILE,file='datafile')
 270  continue
      j = j + 1
      read(IFILE,*,end=290)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      do 280 i = 1, NDAT
        read(IFILE,*) HHMMSS(i), t, wd, ws
        if (t.ne.MISS) then
            nobs(i) = nobs(i) + 1
            tsd(i)  = tsd(i)  + ( ( t - tcorrect(j) ) - ta(i) )**2
        end if
 280  continue
      go to 270
 290  continue
      close(IFILE)
c -calculate standard deviation
      do 300 i = 1, NDAT
        tsd(i) = ( tsd(i) / nobs(i) )**(0.5)
 300  continue



c calculate mean wind speed
c -initialization
      do 310 i = 1, NDAT
        nobs(i) = 0
        wa(i)   = 0.0
 310  continue
c -read data
      open(IFILE,file='datafile')
 320  continue
      read(IFILE,*,end=340)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      do 330 i = 1, NDAT
        read(IFILE,*) HHMMSS(i), t, wd, ws
        if (ws.ne.MISS) then
            nobs(i) = nobs(i) + 1
            wa(i)   = wa(i)   + ws
        end if
 330  continue
      go to 320
 340  continue
      close(IFILE)
c -calculate mean
      do 350 i = 1, NDAT
        wa(i) = wa(i) / nobs(i)
 350  continue

c calculate standard deviation
c -initialization
      do 360 i = 1, NDAT
        nobs(i) = 0
        wsd(i)  = 0.0
 360  continue
c -read data
      open(IFILE,file='datafile')
 370  continue
      read(IFILE,*,end=390)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      read(IFILE,*)
      do 380 i = 1, NDAT
        read(IFILE,*) HHMMSS(i), t, wd, ws
        if (ws.ne.MISS) then
            nobs(i) = nobs(i) + 1
            wsd(i)  = wsd(i)  + (ws-wa(i))**2
        end if
 380  continue
      go to 370
 390  continue
      close(IFILE)
c -calculate standard deviation
      do 400 i = 1, NDAT
        wsd(i) = ( wsd(i) / nobs(i) )**(0.5)
 400  continue


c output
      do 410 i = 1, NDAT
        write(*,*) HHMMSS(i), ',', nobs(i), ',', 
     &    ta(i), ',', tsd(i), ',', wa(i), ',', wsd(i)
c        write(*,*) HHMMSS(i), ta(i), tsd(i), nobs(i)
 410  continue


      stop
      end
