      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)
      INTEGER*4 PSRATIO,PRATIO,SRATIO

      INCLUDE 'UNPACK.INC'

      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 ( IPD.EQ.1 ) THEN
            TIME(1) = YEAR3
            CALL CONHOUR(SCETHR,TIME)
            CALL CONSEC(SCETSC,TIME)
            TIME(6) = SCETML
            WRITE(80,'('' RECORD NUMBER: '',I5.5,''   TIME: '',
     &       I2,1X,I3,3(1X,I2.2),1X,I3.3)') RECNUM,TIME
         END IF
         IF ( NSDIFF.NE.0 ) THEN
            WRITE(80,'(24(1X,I4))')
     &      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,'(20(1X,I4))')
     &      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, SREC(1,IS), SREC(2,IS), SREC(3,IS)
         END IF
      END DO

      RETURN
      END
