      SUBROUTINE HYOPEN
C--CALLED BY HYPOINV TO OPEN FILES
      INCLUDE 'common.inc'
C--ISTAT2 IS USED BY HYOPEN TO TELL HYP WHETHER FILES EXIST.

      CHARACTER CHEAD*8,CHEADC*8
      DIMENSION CHEAD(5),CHEADC(5)
      DATA CHEAD/'DUR-MAG1','DUR-MAG2','AMP-MAG1','AMP-MAG2','EXT-MAG '/
      DATA CHEADC/'DUR,MAG1','DUR,MAG2','AMP,MAG1','AMP,MAG2',
     2 'EXT,MAG '/

C--OPEN PHASE FILE
      IF (JCP.LT.6) CALL OPENR (14,PHSFIL,'F',IOS)
      IF (IOS.NE.0) THEN
C--HERE IS THE ERROR MESSAGE FOR NON-EXISTENT PHASE FILES
        WRITE (6,1010)
1010    FORMAT (' *** ERROR - PHASE FILE DOES NOT EXIST ***')
        ISTAT2=0
        IRES=-61
        RETURN
      END IF

C--OPEN CUSP-ID FILE
C      IF (JCP.EQ.7) CALL OPENR (14,PHSFIL,'F',IOS)
C      IF (IOS.NE.0) THEN
CC--HERE IS THE ERROR MESSAGE FOR NON-EXISTENT CUSP-ID FILE
C        WRITE (6,1011)
C1011    FORMAT (' *** ERROR - CUSP-ID FILE DOES NOT EXIST ***')
C        ISTAT2=0
C        IRES=-62
C        RETURN
C      END IF
      ISTAT2=1

C--OPEN OUTPUT FILES
      IF (LSUM .AND. LAPP(2)) CALL OPENW (12,SUMFIL,'F',IOS,'A')
      IF (LARC .AND. LAPP(3)) CALL OPENW (7,ARCFIL,'F',IOS,'A')
      IF (LPRT .AND. LAPP(1)) CALL OPENW (15,PRTFIL,'F',IOS,'A')

      IF (LSUM .AND. .NOT.LAPP(2)) THEN
        CALL OPENW (12,SUMFIL,'F',IOS,'S')

C--WRITE HEADER LINE IN READABLE SUMMARY FILES
        IF (IH71S.EQ.3) WRITE (12,1003) (CHEAD(MRDMAG(I)),I=1,NRDMAG)
1003    FORMAT ('   DATE     TIME  SEC     LAT      LON    ',
     2 ' DEPTH PREF-MAG NM NUM GAP  DMIN  RMS    ERH   ERZ QASR ',4X,
     3 'ID-NUM LOC',5(' ',A8))

        IF (IH71S.EQ.4) WRITE (12,1004) (CHEADC(MRDMAG(I)),I=1,NRDMAG)
1004    FORMAT ('   DATE     TIME, SEC ,   LAT  ,   LON   ,',
     2 ' DEPTH,PREF,MAG,NM,NUM,GAP, DMIN, RMS ,  ERH,  ERZ,QASR,',4X,
     3 'ID-NUM,LOC',5(',',A8))
      END IF

      IF (LARC .AND. .NOT.LAPP(3)) CALL OPENW (7,ARCFIL,'F',IOS,'S')
      IF (LPRT .AND. .NOT.LAPP(1)) CALL OPENW (15,PRTFIL,'F',IOS,'S')

      IF (LMAG) CALL OPENW (16,MAGFIL,'F',IOS,'S')
      RETURN

      END
