C   08/09/79 002222002  MEMBER NAME  KORDER   (JADESR)      FORTRAN
      SUBROUTINE KORDER
      IMPLICIT INTEGER*2 (H)
C---
C---     PUTS CALIBRATION UPDATE RECORDS INTO INCREASING TIME ORDER.
C---                                   L.H. O'NEILL 08.09.79
C---
      DIMENSION JBUF(10000),IDAT(9991),HBUF(20000),ITIME(6)
      DIMENSION LIST(2,10000)
      EQUIVALENCE(JBUF(1),HBUF(1))
      EQUIVALENCE(JBUF(10),IDAT(1))
C---
      IWFLAG=1
      ITIME(1)=0
      ITIME(2)=0
      ITIME(3)=0
      ITIME(4)=1
      ITIME(5)=1
      ITIME(6)=1979
      LASTIM=0
      NDUMMY=KTMCON(ITIME,KTWRIT,LASTIM)
      REWIND 3
      REWIND 4
      READ(3) IDMMY,KSTIME
      WRITE(4) IDMMY,KSTIME
      WRITE(6,500) KSTIME
  500 FORMAT(' *** START TIME = ',I10,/)
      NREC=0
    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
      IF(NREC.GT.10000) WRITE(6,201)
  201 FORMAT(' **** ERROR. MORE THEN 10000 RECORDS. PROGRAMMED STOP.')
      IF(NREC.GT.10000) STOP
      JBUF(5)=KTWRIT
      LIST(1,NREC)=NREC
      LIST(2,NREC)=JBUF(6)
      CALL KALBNK(4,NWORD,JBUF)
      GO TO 1
 1000 CONTINUE
      WRITE(6,101) NREC
  101 FORMAT(' *** READ ERROR ENCOUNTERED AFTER ',I4,' RECORDS.')
      RETURN
 2000 CONTINUE
      WRITE(6,102) NREC
  102 FORMAT('0NORMAL END OF FILE AFTER',I4,' RECORDS.')
C---
C---     FIND TIME ORDER OF RECORDS.
C---
C        WRITE(6,1991) ((LIST(KLM,LHO),KLM=1,2),LHO=1,NREC)
 1991    FORMAT(1X,2I10)
      DO 2 I=1,NREC
      LOWEST=2000000001
      DO 3 J=I,NREC
      IF(LIST(2,J).GE.LOWEST) GO TO 3
      K=J
      LOWEST=LIST(2,J)
    3 CONTINUE
      IADDR=LIST(1,I)
      IVALU=LIST(2,I)
      LIST(1,I)=LIST(1,K)
      LIST(2,I)=LOWEST
      LIST(1,K)=IADDR
      LIST(2,K)=IVALU
C        WRITE(6,1998)
 1998    FORMAT(' ')
C        WRITE(6,1991) ((LIST(KLM,LHO),KLM=1,2),LHO=1,NREC)
    2 CONTINUE
C---
C---     NOW OVERWRITE FILE ON UNIT 3 WITH TIME ORDERED RECORDS.
C---
      REWIND 3
      REWIND 4
      READ(4) IDMMY,KSTIME
      IF(IWFLAG.EQ.1) WRITE(3) IDMMY,KSTIME
C---
      NCURNT=0
      LASTIW=0
      DO 10 I=1,NREC
C         WRITE(6,1995) I,NCURNT,LIST(1,I),LIST(2,I)
 1995     FORMAT(' I,NCURNT,LIST(1,I),LIST(2,I)= ',4I10)
      NWISH=LIST(1,I)
      IF(NWISH.GT.NCURNT) GO TO 4
      REWIND 4
      READ(4) IDMMY,KSTIME
      DO 5 J=1,NWISH
      CALL EVREA1(4,NWORD,JBUF,IRET)
      IF(IRET.EQ.0) GO TO 5
      WRITE(6,202) IRET
  202 FORMAT(' *** ILLEGAL IRET = ',I2,' IN 5 LOOP. STOP.')
      STOP
    5 CONTINUE
      GO TO 6
    4 CONTINUE
      LOOP=NWISH-NCURNT
      DO 7 J=1,LOOP
      CALL EVREA1(4,NWORD,JBUF,IRET)
      IF(IRET.EQ.0) GO TO 7
      WRITE(6,203) IRET
  203 FORMAT(' *** ILLEGAL IRET = ',I2,' IN 7 LOOP. STOP.')
      STOP
    7 CONTINUE
    6 CONTINUE
      NCURNT=NWISH
      IF(JBUF(6).EQ.LIST(2,I)) GO TO 8
      WRITE(6,204) I,JBUF(6),LIST(1,I),LIST(2,I)
  204 FORMAT(' JBUF(6) AND LIST(2,I) DISAGREE. DUMP = ',4I10)
      WRITE(6,303)
  303 FORMAT(' *** PROGRAMMED STOP *** ')
      STOP
    8 CONTINUE
      IF(JBUF(6).GE.LASTIW) GO TO 9
      WRITE(6,205) I,JBUF(6),LASTIW,LIST(1,I),LIST(2,I)
  205 FORMAT(' DECREASING TIME. DUMP = ',5I10)
      WRITE(6,304)
  304 FORMAT(' *** PROGRAMMED STOP *** ')
      STOP
    9 CONTINUE
      LISTIW=JBUF(6)
      IF(IWFLAG.EQ.1) CALL KALBNK(3,NWORD,JBUF)
   10 CONTINUE
      RETURN
      END
