      	include 'common_arriv.inc'
        character*80 dir,file,text,file_hyp,file_arv,pick_file,file_wv
	character*1 cret,unit,pol_ud,c_hypo_flag,c_temp
        character*2 ifmagc
        character*9 a1826
	cret=char(10)	
        open(12,file='print.dat')
         open(19,file='../arriv/event.dat',
     1  access='direct',form='formatted',recl=120,status='unknown')
          irecev=1
       irecw=0
       irece=1
       iybef=0
       do 10 kmkm=1,10000
         read(12,'(a80)',end=99) dir
         do j=2,89
            if(dir(j:j).eq.' ') then
               j1=j-1
               if(dir(j1:j1).ne.'/') then
                  j1=j
                  dir(j1:j1)='/'
                  go to 11
                  endif
                  endif
                  enddo
 11               continue

         text='ls '//dir(1:j1)//'* >print2.dat'
         write(*,*) 'text=',text
         call system(text)
         open(13,file='print2.dat') 
         do 20 kkk=1,1000000
          read(13,'(a80)',end=21) pick_file
          write(*,*) pick_file
         call  dkread_win_pick(pick_file,file_wv)
       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_temp=''
       c_hypo_flag=''
       irece=irece+1
       sd=0.0
       write(*,1302) 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
 1302  format(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)


        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
  134 format(3i10,a1) 
       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)

       do k=1,nobsall
          write(*,100) 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)
 100   format(a7,i7,2f7.2,1x,a2,1x,a2,1x,2f7.2,1x,a2,3f7.2,
     1 e10.3,2f6.1)
       enddo
          write(10,rec=1) irece
cc        
          irecev=irecev+1
        write(19,1303,rec=irecev) 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) irecev


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



        subroutine dkread_win_pick(pick_file,file_wv)
	include 'common_arriv.inc'
        dimension ptime_err(na200),stime_err(na200)
        character*80 pick_file,file_wv
        character*100 text
	character*8 text8
        character*7 stnq
        character*2 polq
	nobsall=0
	do k=1,na200
	   wp(k)=0.0
	   ws(k)=0.0
	   po(k)=-99.9
	   so(k)=-99.9
	   ipola(k)=''
	   enddo
	   phi=-99.9
	   ram=-99.9
	   dep=0.0
	   sd=9.9
	   write(*,*) 'dkread_win_pick pick_file=',pick_file
        open(23,file=pick_file,err=2)
        go to 3
 2      continue
        write(*,*) 'No pick file pick_file=',pick_file
	write(*,*) 'Check pick file !!!!!!!!!!!!!!'
	read(*,'(i3)') iok
	return
 3	continue

	read(23,135,end=5) file_wv
 135	format(3x,a20)
        do j=2,20
           if(file_wv(j:j).eq.' ') then
             file_wv=file_wv(1:j-1)
	go to 6
        endif
        enddo
 5	continue
	close(23)
	go to 2
 6	continue
        write(*,*) 'read date'
	read(23,'(2x,a50)',end=99,err=99) text
        read(text,*) iy,im,id,ih,imin,isec

cc	write(*,*) iy,im,id,ih,imin,isec
  137   format(2x,6(1x,i3.2))
cc 1370   format(1x,'#p',6(1x,i2.2))
	secmin=sec
 100	format(a80)
	nobsall=0
	do 26 k=1,10000
	   read(23,100,end=99) text
c           write(*,*) text
	   if(text(1:2).ne.'#p') go to 27
 26	   continue
 27	   continue
cc        write(*,110) iy,im,id,ih,imin,iy,im,id,ih,imin
 110    format(3x,2(i2,1x,i2.2,1x,i2.2,1x,i2.2,1x,i2.2,19x))
       read(text,110) iy,im,id,ih,imin
      read(text,110) iy_sakai,im_sakai,id_sakai,ih_sakai,imin_sakai
      write(*,*) 'man pick date=',iy,im,id,ih,imin
	nobsp=0
	nobss=0
        k=0
	do 28 kkk=1,na200
	   read(23,100,end=29) text
cc           write(*,*) kkk,text
	   if(text(1:2).ne.'#s') go to 29
       read(text,111,err=28) stnq,polq,pq,per,sq,ser,fpq,ampq,
     1 phiobsq,ramobsq,iheitq
 111    format(3x,a7,4x,a2,f7.3,f6.3,1x,f7.3,f6.3,f6.1,e9.2,2x,
     1 f9.5,f11.5,i7)
       
	   if(stnq.eq.'       ') go to 28
           k=k+1
           stnw(k)=stnq
	   nobsall=k
	  ipola(k)=polq
	  po(k)=pq
	  so(k)=sq
        ptime_err(k)=per
        stime_err(k)=ser
	amps(k)=ampq*100000.0
	perr=ptime_err(k)*100.0
	aicsn(k,1)=40.0/(perr+0.00001)
	serr=stime_err(k)*100.0
	aicsn(k,2)=40.0/(serr+0.00001)
	wp(k)=aicsn(k,1)/10.0
	ws(k)=aicsn(k,2)/10.0
	if(wp(k).gt.1.0) wp(k)=0.0
	if(ws(k).gt.1.0) ws(k)=1.0
	if(pq.gt.0.0001.and.wp(k).lt.0.5) wp(k)=0.5

	if(sq.gt.0.0001.and.ws(k).lt.0.1) ws(k)=0.1
        
cc        if(ws(k).gt.0.1) ws(k)=0.1


	if(po(k).lt.0.001) then
	   wp(k)=0.0
	   po(k)=-99.9
	   endif
	   if(so(k).lt.0.001) then
	      ws(k)=0.0
	      so(k)=-99.9
	      endif
	      if(wp(k).gt.0.01) nobsp=nobsp+1
	      if(ws(k).gt.0.01) nobss=nobss+1
 28	      continue
 29	      continue
cc	      write(*,*) text
       read(text,112,err=99) iy,im,id,ih,imin,t,phi,ram,dep,fmag
 112   format(3x,3i3,3x,2i3,f8.3,2x,f9.5,f11.5,f8.3,f6.1)
 99	continue
	close(23)
	write(*,112)iy,im,id,ih,imin,t,phi,ram,dep,fmag

cc	   write(*,*) 'file_wave=',file_wv
	  
cc           write(*,*) 'after store_ps'
cc           read(*,*) iok
	return
	end
      
