c$debug
C***************************************************************
C PROGRAM TO CONVERT HYPOINVERSE
C TO NORDIC STANDARD FORMAT.
    
c Originally by M. Villagran for converting H71 to Nordic
C By R. Arvidsson, 1992-01-28. Format on output scs in header
c  header line.

c  modified by Barry Lienert 11/94

c   dec 94    by jh   **************** version 5.0 ************** no change
c   feb 99    by jh   -----------------version 7.0 check ------------------
c                     add question about century, no 5 char stations
c   18 mar 99    lo   define cero
c
C character variables definition
c
        CHARACTER*80 HEADERINFO
	CHARACTER*30 INFILE,outfile
        character*5   sec
        CHARACTER*4 CODA 
        CHARACTER*3 AMP1,TODO
        character*4 sta
	CHARACTER*2 PHP
        character*2 cen ! century
        character*1 type,siete,udp,wtp,decision,sp,qi
        character*5 cero

C integer variables definition 
        integer yr,mth,dy,escribs,jj,hrmn,error



c
c print version
c
      include 'version.inc'
      out_version_date='July 23, 2001'
      if (version_new) out_version_date=version_date
      call print_ver

C Formats
C 100 is to read HYPOINV phases format
C 200 is to write the headline of identification of the quake in NF
c NF means Nordic Format.
C 300 is to write P or S Phases format in NF

 102    format(a)
	            
C Program presentation and
C asks for input and output file
C F1FILE is the input file
        HEADERINFO(1:40)= ' STAT SP IPHASW D HRMN SECON CODA AMPLIT ' 
c        HEADERINFO(41:80)=' PERI AZIMU VELO SNR AR TRES  W DIS CAZ7'
        HEADERINFO(41:80)=' PERI AZIMU VELO AIN AR TRES  W DIS CAZ7'
        WRITE(*,*)'  '
        WRITE(*,*)' This program converts HYPOINVERSE',
     &   ' phase format to'
        WRITE(*,*)' NORDIC phase format for use with HYPOCENTER'

C open file to convert
1098    WRITE(*,'(/'' HYPOINVERSE Phase file ? '',$)')
        read(*,'(a)')infile
 11     continue
        write(6,*)' Give century, 19 or 20'
        read(5,'(a2)') cen
        if(cen.ne.'19'.and.cen.ne.'20') goto 11
        open(1,file=infile,err=1099,status='old')

        write(*,'(/'' Output Filename ? '',$)')
        read(*,'(a)')outfile   

C all quakes will be cataloged like local 
C it has to be updated
        TYPE='L'
 
C 7 is a number that will be puted in the 80 column
C of the Nordic Format just in the earthquake identification line
        SIETE='7'

C all phases will be considerated like Z component 
C because H71pc and Hinvpc formats don't have
C notation of that.
        SP='SZ'

C to be used in P phase if Weigth option in pc format is 9.
        CERO='     '

C TODO is a variable to write blank lines.
	TODO='   '

C JJ is the quakes counter
	JJ=0

C open the nordic format file to create
C write the head information in output file
10      OPEN(2,FILE=outfile,status='new',err=70)

C start the process to convert format
C at first the program writes P phase
C that is because the NF uses one line for each phase
C after the program change the variable values
C to write the S phase

C YES is an variable that the program loks to know
C if has to write P or S Wave.
20	YES=0

C earthquakes counter
	JJ=JJ+1
c       WRITE(2,*)TODO

      read(1,101,end=90) yr, mth, dy, hrmn 
101    FORMAT(3i2,i4)

21    read(1,100,end=90)sta,qi,php,wtp,udp,sec,coda
100    format(a4,4a1,a5,a4,a4) 

c      write(sec,'(a2,a1,a2)') ss,'.',hhscs
 
c Fix weight as blank when zero
      if ( WTP .eq. '0' ) then
         WTP= ' '
      endif
c ---
25      continue

c        if(jj.ne.1) write(2,*)'                  '

c -------- Fix format of seconds, r.n.a
c        write(*,*)sec
c        read(ss,'(f5.2)') scs     

       if(yes.eq.0)then
        write(2,200)cen,yr,mth,dy,hrmn,'L'	
 200    format(1x,a2,i2,1x,2i2,1x,i4,6x,a1,57(' '),'1')
        write(2,'(A79,A1)')HEADERINFO(1:79), '7' 
	yes=1
       endif

C if the programs H71pc and Hinvpc reads coda values of "0"
C accept that like not a coda value, for that case
C in NF coda has to be blank
      if(coda.eq.'   0')then
       coda='    '
      endif

C For an H71pc format the program will change the way of notacion
C of the arrival up will be C and Down D
      if(udp.eq.'U')then
       udp='C' 
      endif
      amp1='   '
       escribs=0
       if(sta.eq.'    ')then
        write(2,*)
        goto 20
       endif

C Writes P in the NF file and make variable values to
C can write S phase
50    write(2,300)sta,sp,qi,php,udp,wtp,hrmn,sec,coda,amp1
300    format(1x,a4,1x,a2,1x,2a1,3x,a1,1x,a1,1x,i4,1x,a5,1x,a4,1x,a3)
      goto 21

C error display
60    write(*,*)'  '
      error=1
      write(*,*)'  Error found after ...'
      write(2,300)sta,sp,qi,php,udp,wtp,hrmn,sec,coda,amp1
      goto 21
70    WRITE(*,*)'  '
      WRITE(*,*)' This file already exists:'
      WRITE(*,'(/'' Do you want to overwrite it (Y/N) ? '',$)')
      read(*,'(a1)')decision

      if((decision.eq.'y').or.(decision.eq.'Y'))then
        open(2,file=outfile,status='old')
        close(2,status='delete')
        goto 10
      else
        goto 99
      endif

1099  write(*,'(/'' File not found '')') 
      go to 1098

c finish
90      IF(error.EQ.1)THEN
	WRITE(*,*)' Output file was not created'
	WRITE(*,*)' Correct the file and try again'
	GOTO 98
	ENDIF
        write(2,*)'    '
        CLOSE(2)
        write(6,*)
	WRITE(*,'(10X,I5,1X,A22)')JJ-1,'Quakes were converted '
98      CLOSE(1)
99      continue
        END
