      character*120 text,text2,textm
      character*80 file,file_hyp,file_arv
      character*7 stnw(10000)
      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
	cret=char(10)	
         open(19,file='../arriv/event.dat',
     1  access='direct',form='formatted',recl=120,status='unknown')
          irecw=1
      open(12,file='print.dat')
c      open(9,file='hypotest.dat',
c     1 access='direct',form='formatted',recl=120,status='unknown')
c      open(10,file='hypo_izu_2011_2310.dat',
c     1 access='direct',form='formatted',recl=120,status='unknown')
      nc_iwaki=0
c        open(10,file='arrivtest.dat',
c     1 access='direct',recl=7700,status='unknown') 
       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
 9    continue
      if(text(1:2).ne.'J2') go to 20
cc      write(*,*) text
cc      read(text,100,err=13) iy,im,id,ih,imin,it,idt

cc      write(*,*) iy,im,id,ih,imin,it,idt
cc2011-09-03 00:00:31.21  0.05   37.134  0.18  140.464  0.21     8.1   1.3  1.3V        MID FUKUSHIMA PREF
cc                  J2011090300003121 005 370804 018 1402783 021  81213213V   511   2 55MID FUKUSHIMA PREF       20K        
      read(text,100,err=20) iy,im,id,ih,imin,it,idt,ip1,ip2,ip3,idp2,
     1 idp3,ir1,
     1 ir2,ir3,idr2,idr3,idep,iddep,ifmagc,a1826,nobsj,c_hypo_flag
      itemp=0
      c_temp=a1826(6:6)  !Template M
      if(a1826(6:6).eq.'M')  then
         itemp=1
         read(7,'(a120)',err=20) textm
cc         write(*,*) 'Template=', textm
         endif
         


cc      write(*,100) iy,im,id,ih,imin,it,idt,ip1,ip2,ip3,idp2,idp3,ir1,
cc     1 ir2,ir3,idr2,idr3,idep,iddep,ifmag
cc      read(text,100) iy,im,id,ih,imin,it,idt,ip1,ip2,ip3,idp2,idp3,ir1,
cc     1 ir2,ir3,idr2,idr3,idep,iddep,ifmag
 100  format(3x,5i2,2i4,1x,3i2,2i2,i4,2i2,2i2,i5,i3,a2,a9,29x,i3,a1)
      ifmag=0
      read(ifmagc,'(i2)',err=4) ifmag
 4    continue
cc      if(iy.le.0) go to 20
      t=float(it)/100.0
      dt=idt/100.0
      phi=float(ip1)+float(ip2)/60.0+float(ip3)/6000.0
      dphi=float(idp2)/60.0+float(idp3)/6000.0
      ram=float(ir1)+float(ir2)/60.0+float(ir3)/6000.0
      dram=float(idr2)/60.0+float(idr3)/6000.0
      dep=float(idep)/100.0
      ddep=iddep/100.0
      fmag=ifmag/10.0
      fmagj=fmag
      hm=float(imin)*60.0
      sec=0.0
      sd=0.0
           nobsp=0
            nobss=0
            n=0
ccc            iybef=0

 

c      if(id.eq.11.and.ih.ge.15) go to 18
c      if(id.eq.11.and.ih.eq.14.and.imin.ge.46) go to 18

  134 format(3i10,a1) 
  130 format(a1,i8,1x,5i2,f6.2,f8.2,f6.2,f8.4,f7.4,f9.4,f7.4,
	1 2f6.2,f5.2,3i4)

c            write(*,1311) irece,iy,im,id,ih,imin,sec,t,dt,
c	1 phi,dphi,ram,dram,dep,ddep,fmag
 1311       format(/i8,1x,5i2,f6.2,f8.2,f6.2,f8.4,f7.4,f9.4,f7.4,
	1 2f6.2,2f5.2,3i3)

          

            do k=1,10
               iduma(k)=0
               enddo
      nobsall=0
      NOBSP=0
      nobss=0
      k=0
      do 30 kw=1,10000      
      read(7,'(a120)',end=21) text2
cc      write(*,*) text2
      iend=0

      if(text2(1:1).eq.'E') go to 31
      if(text2(1:1).ne.'_'.and.text2(1:1).ne.'W') go to 30
      k=k+1
      nobsall=k
c      write(*,*) text2
cccc _YONAGU 686 N 1P   00002918ES  003660 2989 01  4 3155 03  9 3660 01  4J                1112TT  1            
      read(text2,110,err=35) stnw(k),nstn(k),rnp,ip1,ip2,ip3,rns,
     1 is2,is3,iamp_ns,iper_ns,it_ns_max,
     2 iamp_ew,iper_ew,it_ew_max,iamp_ud,iper_ud,it_ud_max,unit,
     3 pol_ud
      go to 36
 35   continue
      k=k-1
      go to 30
 36   continue

c      write(*,110) stnw(k),nstn(k),rnp,ip1,ip2,ip3,rns,
c     1 is2,is3,iamp_ns,iper_ns,it_ns_max,
c     2 iamp_ew,iper_ew,it_ew_max,iamp_ud,iper_ud,it_ud_max,unit,
c     3 pol_ud

 110  format(1x,a6,i4,4x,a2,i4,i2,i4,a2,2x,i2,i4,3(i5,2i3),a1,
     1     8x,a1)
      is1=ip1
      pq=float(ip2*60)+float(ip3)/100.0-hm
      sq=float(is2*60)+float(is3)/100.0-hm
      if(pq.gt.1800.0) pq=pq-3600.0
      if(pq.lt.-1800.0) pq=pq+3600.0
      if(sq.gt.1800.0) sq=sq-3600.0
      if(sq.lt.-1800.0) sq=sq+3600.0
c      write(*,*) hm,' p=',ip1,ip2,ip3,pq,sq,rnp,rns
      if(itemp.eq.1) then
      if(text2(93:93).eq.'P') rnp='P'
       if(text2(93:93).eq.'S') then
          rns='S'
          sq=pq
          pq=-99.9
          endif
   
          endif

c          write(*,*) stnw(k),' pq,sq=',pq,sq,rnp,rns


c
c1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
cWN.MASH2536 h01X   05032025  40    11 10              32      16      J20121123231015842001%S  
cc      write(*,*) 'text2 90-95=',text2(90:95)


      po(k)=-99.9
      so(k)=-99.9
      wp(k)=0.0
      ws(k)=0.0
      rankp(k)=''
      ranks(k)=''
      fp(k)=1.0
      w=1.0
       if(rnp(2:2).eq.' ')  w=0.7
       if(rnp(1:1).eq.'E')  w=0.5
      if(rnp(1:1).eq.'P'.or.rnp(2:2).eq.'P') then
       po(k)=pq
       wp(k)=w
       rankp(k)=rnp
       endif
       w=1.0
       if(rns(2:2).eq.' ')  w=0.7
       if(rns(1:1).eq.'E')  w=0.5
      if(rns(1:1).eq.'S'.or.rns(2:2).eq.'S') then
       so(k)=sq
       ws(k)=w
       ranks(k)=rns
       endif




       ipola(k)=''
cc       write(*,120) stnw(k),nstn(k),po(k),wp(k),so(k),ws(k)
 120   format(a7,i5,4f8.2)
          u=0.0
          if(unit.eq.'J'.or.unit.eq.'K') u=0.1
           if(unit.eq.'1'.or.unit.eq.'A') u=1.0
             if(unit.eq.'4'.or.unit.eq.'D') u=10.0         
               if(unit.eq.'7'.or.unit.eq.'G') u=100.0                    
               amps(k)=0.0
         if(u.gt.0.001) amps(k)=float(iamp_ud)*u/1000.0  !Unit:mkine
         q=po(k)
         if(po(k).lt.-90.0) q=so(k)
         ts_amax(k)=float(it_ud_max)+q
         perioda(k)=float(iper_ud)/10.0
         ampp(k)=0.01
         period(k,1)=perioda(k)
         period(k,2)=perioda(k)
         sp(k)=so(k)-po(k)
         resoc(k,1)=0.1
         resoc(k,2)=0.1
         if(wp(k).gt.0.01) nobsp=nobsp+1
         if(ws(k).gt.0.01) nobss=nobss+1
c         write(*,122) stnw(k),nstn(k),po(k),wp(k),rankp(k),so(k),ws(k),
c     2 ranks(k),amps(k),ts_amax(k),perioda(k),rnp
 122     format(a7,i5,2(f7.2,f5.1,a3),f9.4,2f7.2,a3)
         
         if(so(k).gt.990.0.or.so(k).lt.-990.0) then
cc            write(*,*) pq,sq,po(k),so(k)

cc         write(*,*) text2
c         read(*,*) iok
         endif
 30      continue
 31      continue
       if(nobsall.gt.120) nobsall=120
cc       write(*,*) 'nobsall=',nobsall
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_20',i2.2,'.dat')
           write(file_arv,166) iy
 166       format('../arriv/arv_20',i2.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

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
       sd=0.0
        write(9,1303,rec=irece) cret,iy,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
       write(10,rec=irece) iy,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

        irecw=irecw+1
        write(19,1303,rec=irecw) cret,iy,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




 20    continue
 21    continue
       close(7)
 10    continue
 99    continue
       close(9)
       close(10)
       close(13)
       stop
       end
