C   08/09/79 109150908  MEMBER NAME  KALADD   (JADESR)      FORTRAN
      SUBROUTINE KALADD
      IMPLICIT INTEGER*2 (H)
C---
C---     FOR ADDING ON AND MODIFYING CALIBRATION RECORDS.
C---                                   L.H. O'NEILL 08.09.79
C---     LAST MODIFICATION 09:00 07-10-79 Y.WATANABE
      DIMENSION JBUF(10000),IDAT(9991),HBUF(20000),ITIME(6),NAME( 7)
      DIMENSION HTIME(6),HDAT(3),HCAT(1)
      EQUIVALENCE(JBUF(1),HBUF(1))
      EQUIVALENCE(JBUF(10),IDAT(1))
      EQUIVALENCE(HDAT(1),IDAT(1))
      EQUIVALENCE(HCAT(1),HDAT(3))
      DATA NAME/'MUCA','LGMA','TAGS','JTPL','JTAB','TOFC','LGST'/
C---
      ITIME(1)=0
      ITIME(2)=0
      ITIME(3)=0
      ITIME(4)=1
      ITIME(5)=1
      ITIME(6)=1979
      LASTIM=0
      IDMTIM=KTMCON(ITIME,KTWRIT,LASTIM)
      NREC=0
C---
C---     LOOP OVER THE RECORDS ON THE EXISTING CALIBRATION FILE (UNIT 3)
C---     MAKE DELETIONS AND MODIFICATIONS AND WRITE TO SCRATCH FILE
C---     UNIT 4.
C---
      REWIND 3
    1 CONTINUE
      CALL EVREA1(3,NWORD,JBUF,IRET)
      IF(IRET.EQ.1) GO TO 1000
      IF(IRET.EQ.2) GO TO 2000
      NREC=NREC+1
C---
C---     DELETE UWANTED RECORDS. CHECK CAREFULLY.
C---
      CALL KALBNK(4,NWORD,JBUF)
      GO TO 1
 1000 CONTINUE
      WRITE(6,101) NREC
  101 FORMAT(' *** READ ERROR ENCOUNTERED AFTER ',I4,' RECORDS.')
      STOP
 2000 CONTINUE
      WRITE(6,102) NREC
  102 FORMAT('0NORMAL END OF FILE AFTER',I4,' RECORDS.')
C---
C---     ADD ON NEW RECORD(S) MADE FROM DATA ON UNIT 8 ETC.
C---     AND WRITE THESE TO UNIT 3 SCRATCH FILE.
C---
      JBUF(1)=NAME(7)
      JBUF(2)=1
      JBUF(3)=1
      JBUF(4)=0
      JBUF(5)=KTWRIT
      JBUF(6)=0
      JBUF(7)=1
      JBUF(8)=0
      JBUF(9)=0
      NREC=0
    4 CONTINUE
      CALL EVREAH(8,NWORDH,HCAT,IRET)
      IF(IRET.EQ.1) GO TO 1001
      IF(IRET.EQ.2) GO TO 2001
      NREC=NREC+1
C---
C---     SELECT RECORDS FROM INPUT UNIT(S).
C---
      IDAT(1)=NWORDH
         IF(IDAT(1).LE.10) GO TO 4
         IF(IDAT(2).LE.500) GO TO 4
C         IDAT(1)=LENG, IDAT(2)=#EV.  BETTER BE NO DATA THAN
C         HAVING SHORT DATA.
      NWORDH=NWORDH+2
      ITIME(1)=HCAT(5)
      ITIME(2)=HCAT(6)
      ITIME(3)=HCAT(7)
      ITIME(4)=HCAT(8)
      ITIME(5)=HCAT(9)
      ITIME(6)=HCAT(10)
      LASTIM=0
      JBUF(6)=KTMCON(ITIME,KTDUMM,LASTIM)
      IF(JBUF(6).EQ.0) GO TO 4
C---
C---     SUBTRACT 200 SECONDS FROM EFFECTIVE TIME TO MAKE SURECHANGE
C---     GOES INTO EFFECT AT START OF RUN.
C---
      JBUF(6)=JBUF(6)-200
C---     SET RUN #
      JBUF(8)=HCAT(4)
      NWORD=NWORDH/2
      IF((2*NWORD).NE.NWORDH) NWORD=NWORD+1
      NWORD=NWORD+9
      CALL KALBNK(4,NWORD,JBUF)
      GO TO 4
 1001 CONTINUE
      WRITE(6,201) NREC
  201 FORMAT(' *** READ ERROR ENCOUNTERED AFTER ',I4,' RECORDS. (TWO)')
      STOP
 2001 CONTINUE
      WRITE(6,202) NREC
  202 FORMAT('0NORMAL END OF FILE AFTER',I4,' RECORDS ON ADD ON FILE.')
C---
C---     WRITE BACK FROM THE SCRATCH FILE ONTO THE ORIGINAL INPUT FILE.
C---
      NREC=0
      REWIND 3
      REWIND 4
    2 CONTINUE
      CALL EVREA1(4,NWORD,JBUF,IRET)
      IF(IRET.EQ.1) GO TO 1002
      IF(IRET.EQ.2) GO TO 2002
      NREC=NREC+1
      CALL KALBNK(3,NWORD,JBUF)
      GO TO 2
 1002 CONTINUE
      WRITE(6,203) NREC
  203 FORMAT(' *** READ ERROR ENCOUNTERED AFTER ',I4,' RECORDS. (THR)')
      STOP
 2002 CONTINUE
      WRITE(6,204) NREC
  204 FORMAT('0NORMAL END OF FILE AFTER',I4,' RECORDS ON SCRATCH FILE.')
      RETURN
      END
