c     mw.f Calculates average and frequency of soundings.
c     This program considers both Capital and noncapital
c     letters in filenames
      parameter (n=70)          !  n = rows in file unit 1
      character*3 date
      character*4 time
      character*5 label
      character*5 name
      character*4 ext(2)        !extension file: WIN or win
      character*1 fore(2)       !fore part of the file: P or p
      character*2 month         
      character*2 amopm
      character*2 stn
      dimension u(n),v(n),hm(n)
      integer j(n),tmax
      data ext/'.win','.WIN'/
      data fore/'p','P'/
c
c    Enter information from keyboard
      write(*,*)'input month (ex: 05 for May)'
          read(*,'(a2)')month
      write(*,*)'choose am, pm or total (am,pm or to ,respectively)'
          read(*,'(a2)')amopm
      write(*,*)'station number:'
          read(*,'(a2)')stn
c
c    Use information from keyboard to define "lim" (tot.number of days)
      lim=0
      if(month.eq.'01'.or.month.eq.'03'.or.month.eq.'05'.or.
     .   month.eq.'07'.or.month.eq.'09'.or.month.eq.'11')then
         lim=62
      else
         lim=60                 !lim=twice days of the month       
      endif
      if(month.eq.'02')lim=56   !change in case of lap year
      write(*,*)'This month has ',lim/2.,'days'
c
c    Initialize variables
      izero = 0
      tmax = 0
      ie = 0
      num = 0
      do 1 i=1,n
        j(i)=0
        hm(i)=0
        u(i)=0.
        v(i)=0.
1     continue
c
c    Opening files
      open(3,file='fechas.d',status='old')  !reading file
c
      name = fore(1)//stn//month
c
      open(2,file=name//amopm//'.s')        !output file
      open(4,file='comments')
      open(10,file='altura30')
c
      write(2,*)'    file           No.obs   MaxH(m)'
c
      do 5 ii=1,lim
       read(3,7)date
        if(amopm.ne.'to')then
          read(date,'(i3)')idate
          if(amopm.eq.'pm')then
             if(mod(idate,2).ne.0)goto 25    !afternoon
          endif
          if(amopm.eq.'am')then
             if(mod(idate,2).eq.0)goto 25    !morning
          endif
        endif
c
c      Open reading file
       open(1,file=name//date//ext(1),status='old',
     . iostat=ierr)                               !input file
c
      write(*,*)name//date//ext(1)
      if(ierr.ne.0)then
        close(1)
        open(1,file=fore(2)//stn//month//date//ext(2),status='old'
     .  ,iostat=ierr2)
c       write(*,*)'Capitals'
      endif
      if(ierr2.ne.0)goto 33
      read(1,*,end=30)
      read(1,'(a5)',end=30)label
      write(*,'(a5)')label

      do 10 k=1,n
        if(label.eq.' t[s]')then
          read(1,11,end=20)time,zt,x,x,x,x,x,u0,v0
        else
c         read(1,111,end=20,err=34)time,zt,x,x,x,x,x,u0,v0
          read(1,111,end=20)time,zt,x,x,x,x,x,u0,v0
        endif 
        read(10,*,end=20)time2,zt2
c
        if(zt.eq.zt2.and.iban.eq.1)then
          iban=0
          write(4,*)'gotcha again!',zt,zt2
        endif
      if(zt.ne.zt2.and.j(k).gt.1)then
c         ie=ie+1
          write(4,*)'error in Height ',name//date//ext(1)
          write(4,*)zt,zt2
          iban=1
          goto 10
      endif
      if(hm(k).eq.0)hm(k)=zt
               u(k)=u(k)+u0
               v(k)=v(k)+v0
               j(k)=j(k)+1
c
       maxz=zt
       count=k
10     continue
c
20     rewind(10)
      tmax=tmax+maxz
      write(2,6)name//date//ext(1),count,maxz
      goto 5
25    num=num+1
       rewind(10)
      goto 5
30    write(4,*)'No data found in file ',name//date//ext(1)
       rewind(10)
      goto 5
34    write(*,*)'messaje in file   ',name//date//ext(1)
       rewind(10)
      goto 5
33    write(2,*)'   ',name//date//ext(1),'      0'
        write(4,*)'file  ',name//date//ext(1),'   not found'
        izero = izero+1
5     continue
35    write(2,13)(lim-num)-izero,izero,(lim-num)
      write(2,*)
      write(2,*)'files with error in H = ',ie
      write(2,14)tmax/float((lim-num)-izero)
      write(2,*)
      write(2,*)' height   mag    dir      u       v    freq '
      do 40 k=1,n
         if(j(k).ne.0)then
            umean=u(k)/float(j(k))
            vmean=v(k)/float(j(k))
            xmag=sqrt(umean**2+vmean**2)
            dir=asin(-umean/xmag)*180/3.1416
            if(vmean.gt.0)dir=180-dir
            if(umean.gt.0.and.vmean.lt.0)dir=360+dir
            write(2,12)hm(k),xmag,dir,umean,vmean,j(k)
         endif
40    continue
c
7     format(1x,A3)
6     format(4x,A12,4x,f4.0,5x,i5)
11    format(x,f4.1,4(3x,f7.1),3x,f7.2,3(3x,f7.3))
111    format(f4.1,3x,4(x,f7.1),x,f6.2,x,f6.3,x,2(x,f7.3))
12    format(x,f6.0,2x,f5.2,2x,f5.1,2x,f6.2,2x,f6.2,4x,i2)
13    format(/,10hobserved= ,i2,3x,9hmissing= ,i2,3x,7htotal= ,i2)
14    format(/,47hAveraged height (excluding files with 0 obs.)= ,f6.0)
      stop
      end

