      SUBROUTINE WRITE_DIFF_COUNTS(TIME,NPREF,NPDIFF,NSREF,NSDIFF)

c GLOBAL VARIABLES

c    PREF - primary reference word
c    PDIFF - primary difference word
c    SREF - secondary reference word
c    SDIFF - secondary difference word

c INPUT VARIABLES

c    NPREF - number of reference words
c    NPDIFF - number of primary difference words
c    NSREF - number of secondary reference words
c    NSDIFF - number of secondary difference words

c LOCAL VARIABLES

c    PSRATIO - number of secondary difference words per primary difference word
c    PRATIO - number of primary reference words per primary difference word
c    SRATIO - number of secondary reference words per primary difference word

      INTEGER*2 TIME(6),CURRTIME(6),DELTA(6)/6*0/
      INTEGER*4 PSRATIO,PRATIO,SRATIO

      INCLUDE 'UNPACK.INC'

      IF ( MODE().EQ.0 ) THEN
         DELTA(6) = 60
      ELSE IF ( MODE().EQ.4 ) THEN
         DELTA(6) = 300
      ELSE IF ( MODE().EQ.13 ) THEN
         DELTA(6) = 480
      ELSE
         DELTA(6) = 0
         WRITE(6,'(1X,''BAD MODE VALUE: '',I5,3X,I5)') MODE(),DATMOD
         STOP
      END IF

      TIME(1) = YEAR3
      CALL CONHOUR(SCETHR,TIME)
      CALL CONSEC(SCETSC,TIME)
      TIME(6) = SCETML

      CURRTIME(1) = TIME(1)
      CURRTIME(2) = TIME(2)
      CURRTIME(3) = TIME(3)
      CURRTIME(4) = TIME(4)
      CURRTIME(5) = TIME(5)
      CURRTIME(6) = TIME(6)

      IF ( NSDIFF.NE.0 ) THEN
         PSRATIO = NPDIFF/NSDIFF
      ELSE
         PSRATIO = -1 ! accommodate VIM-5 mode, no secondary differences
      END IF
      PRATIO = NPDIFF/NPREF
      SRATIO = NPDIFF/NSREF
      DO IPD = 1,NPDIFF
         ISD = (IPD-1)/PSRATIO + 1
         IP = (IPD-1)/PRATIO + 1
         IS = (IPD-1)/SRATIO + 1
         IF ( NSDIFF.NE.0 ) THEN
            WRITE(80,'(1X,I2,1X,I3,3(1X,I2.2),1X,I3.3,24(1X,I4))')
     &      CURRTIME, IP, PREF(1,IP), PREF(2,IP), PREF(3,IP),
     &      IPD, PDIFF(1,IPD), PDIFF(2,IPD), PDIFF(3,IPD),
     &      IPD, PREC(1,IPD), PREC(2,IPD), PREC(3,IPD),
     &      IS, SREF(1,IS), SREF(2,IS), SREF(3,IS),
     &      ISD, SDIFF(1,ISD), SDIFF(2,ISD), SDIFF(3,ISD),
     &      ISD, SREC(1,ISD), SREC(2,ISD), SREC(3,ISD)
         ELSE
            WRITE(80,'(1X,I2,1X,I3,3(1X,I2.2),1X,I3.3,24(1X,I4))')
     &      CURRTIME, IP, PREF(1,IP), PREF(2,IP), PREF(3,IP),
     &      IPD, PDIFF(1,IPD), PDIFF(2,IPD), PDIFF(3,IPD),
     &      IPD, PREC(1,IPD), PREC(2,IPD), PREC(3,IPD),
     &      IS, SREF(1,IS), SREF(2,IS), SREF(3,IS),
     &      IS, SDIFF(1,IS), SDIFF(2,IS), SDIFF(3,IS),
     &      IS, SREC(1,IS), SREC(2,IS), SREC(3,IS)
         END IF
         CALL INC_TIME(CURRTIME,DELTA)
      END DO

      RETURN
      END
