      character*120 text,text2,textm
      character*80 file,file_hyp,file_arv
      character*7 stnw(10000),stn
      character*1 psc
      character*2 rankp(10000),ranks(10000),ipola(10000),rnp,rns
      dimension po(1000),so(10000),nstn(10000),wp(10000),ws(10000),
     1 amps(10000),ts_amax(10000),perioda(10000),ampp(10000),
     1 period(10000,2),fp(10000),sp(10000),resoc(10000,2),iduma(10)
	character*1 cret,unit,pol_ud,c_hypo_flag,c_temp
        character*2 ifmagc
        character*9 a1826
          open(19,file='../arriv/event.dat',
     1  access='direct',form='formatted',recl=120,status='unknown')
          irecw=1
        pideg=3.141592/180.0
	cret=char(10)	
      open(12,file='print.dat')
      iybef=0
       irecw=0
       irece=1
       iybef=0
      do 10 kmkm=1,10000
         read(12,'(a80)',end=99) file
cc         if(kmkm.le.250) go to 10
         write(*,*) file
         iend=0
      open(7,file=file)
      do 20 kkk=1,10000000
      read(7,'(a120)',end=21) text
      write(*,*) text
 9    continue
      if(text(1:1).eq.'#') then
         text2=text(2:110)
         
      read(text2,*,err=20) iy,im,id,ih,imin,t,phi,ram,dep,fmag,dh,
     1 ddep,sd,n
       write(*,121) iy,im,id,ih,imin,t,phi,ram,dep,fmag,dh,
     1 ddep,sd,nev
 121   format(i4,4i3,f7.1,2f10.4,4f6.1,f7.2,i8)

      dphi=dh/1.414/111.18
      dram=dphi/cos(phi*pideg)
      fmagj=fmag
      sec=0.0
      sd=0.0
           nobsp=0
            nobss=0
            nc=1
            do k=1,120
               po(k)=999.9
               so(k)=999.9
               wp(k)=0.0
               ws(k)=0.0
               enddo
               endif
      nobsall=0
      do 30 kw=1,10000 
      read(7,'(a120)',end=21) text
      if(text(1:1).eq.'#') go to 31
      read(text,*) stn,pq,wq,psc
      write(*,'(a7,2f7.2,2x,a1)')  stn,pq,wq,psc
      kps=2
      if(psc.eq.'P'.or.psc.eq.'p') kps=1
      n=1
      if(nobsall.eq.0) go to 2
      do k=1,nobsall
         if(stn.eq.stnw(k)) then
            n=k
            go to 3
            endif
         enddo
 2       continue
         nobsall=nobsall+1
         n=nobsall
 3       continue
         stnw(n)=stn
         if(kps.eq.1) then
            po(n)=pq
            wp(n)=wq
            else
               so(n)=pq
               ws(n)=wq
               endif
 30      continue
 31      continue
       if(nobsall.gt.120) nobsall=120
       write(*,*) 'nobsall=',nobsall
       nobsp=0
       nobss=0
       do k=1,nobsall
        write(*,'(a7,4f8.2)') stnw(k),po(k),wp(k),so(k),ws(k) 
        if(wp(k).gt.0.001) nobsp=nobsp+1
        if(ws(k).gt.0.001) nobss=nobss+1
        rankp(k)=''
        ranks(k)=''
        ipola(k)=''
        resoc(k,1)=0.0
        resoc(k,2)=0.0
        fp(k)=1.0
        amps(k)=0.0
        period(k,1)=0.0
        period(k,2)=0.0
        enddo
cc       write(13,rec=irece) iy,im,id,ih,imin,sec,t,dt,
c	1 phi,dphi,ram,dram,dep,ddep,fmag,sd,nobsall,
c     1 (stnw(k),nstn(k),po(k),wp(k),rankp(k),so(k),ws(k),
c     2 ranks(k),amps(k),ts_amax(k),period(k),k=1,nobsall)
         if(iybef.ne.iy) then
            write(*,*) 'iy,iybef=',iy,iybef
            iybef=iy
            close(9)
            close(10) 
            write(file_hyp,165) iy
 165        format('../arriv/hypo_',i4.2,'.dat')
           write(file_arv,166) iy
 166       format('../arriv/arv_',i4.2,'.dat')
           write(*,*) 'file_hypo=',file_hyp
           write(*,*) 'file_arv=',file_arv
           open(9,file=file_hyp,
     1  access='direct',form='formatted',recl=120,status='unknown')
      open(10,file=file_arv,access='direct',recl=7700,status='unknown') 
      irece=1
      read(9,134,rec=1,err=17) irece
 17    continue
cc       write(*,*) 'irece=',irece
       endif
  134 format(3i10,a1) 
c          write(*,1302) nobsall,iy,im,id,ih,imin,sec,t,dt,
c	1 phi,dphi,ram,dram,dep,ddep,fmag,nobsp,nobss,a1826,c_hypo_flag
 1302  format(i8,1x,5i2,f6.2,f8.2,f6.2,f8.4,f7.4,f9.4,f7.4,
	1 2f6.2,f5.2,2i4,1x,a1,a1)
cc       if(itemp.eq.1)  read(*,*) iok
       irece=irece+1
       c_temp=''
       c_hypo_flag=''
       iyq=iy/100
       iyw=iy-iyq*100
        write(9,1303,rec=irece) cret,iyw,im,id,ih,imin,sec,t,dt,
	1 phi,dphi,ram,dram,dep,ddep,fmag,nobsp,nobss,nobsall,
     1  c_temp,c_hypo_flag,irece
 1303   format(a1,1x,5i2,f6.2,f8.2,f6.2,f8.4,f7.4,f9.4,f7.4,
	1 2f7.2,f5.2,3i4,1x,2a1,i9,f6.2)
cc        write(*,*) 'irece=',irece,nobsall
	write(9,134,rec=1) irece
        irecw=irecw+1
        write(19,1303,rec=irecw) cret,iyw,im,id,ih,imin,sec,t,dt,
	1 phi,dphi,ram,dram,dep,ddep,fmag,nobsp,nobss,nobsall,
     1  c_temp,c_hypo_flag,irece
	write(19,134,rec=1) irecw

       write(10,rec=irece) iyw,im,id,ih,imin,sec,t,dt,
     1 phi,dphi,ram,dram,dep,ddep,fmagj,sd,nobsp,nobss,
     1  nobsall,fmag,c_temp,c_hypo_flag,
     2 (stnw(k),nstn(k),po(k),wp(k),rankp(k),
     3 ipola(k),so(k),ws(k),ranks(k),(resoc(k,kps),kps=1,2),
     5  fp(k),amps(k),period(k,1),period(k,2),k=1,nobsall)
          write(10,rec=1) irece
          go to 9
 20    continue
 21    continue
       close(7)
 10    continue
 99    continue
       close(9)
       close(10)
       close(13)
       close(19)
       stop
       end
