c=======================================================================
      subroutine write_rec(ou, fmt_vers, nobs, itime, sec, msec, flag,
     +                     numsat, prn, char, clockerr, obs, lli, snr,
     +                     eventrecs)
c=======================================================================
c
c     ... Write a record to a RINEX file, either RINEX version 1 or 2.
c
      implicit none
c
      integer         ou, nobs, itime(5), prn(99), numsat, flag, sec,
     +                msec, fmt_vers
      character*80    outline, eventrecs(64), dynfmt, dynfmt2
      character*80    dynfmt3
      character*1     char(99), lli(27,99), snr(27,99)
      real*8          obs(27,99), clockerr
      
      integer         i, i1, i2, itrack, j, iLine
c
c
      outline = ' '
      
c     Write as many lines as needed depending on the number of satellites
      do iLine = 1, (numsat-1)/12 + 1
        if (iLine .eq. 1) then
          write(outline(1:32), fmt='(5I3,X,I2,''.'',I7.7,2I3)')
     +         (itime(i), i=1,5), sec, msec, flag, numsat
        else
          write(outline(1:32), fmt='(32X)')
        endif
      
c     ... Write the satellite ID numbers if this is a normal
c         observation record, a record indicating a power
c         failure since the previous epoch, or a cycle slip
c         record.
c
c       Write all the satellites for this line
        do j=1+12*(iLine-1), 12*iLine
          if (flag.le.1 .or. flag.eq.6) then
            i1 = 30 + 3*(j-12*(iLine-1))
            i2 = 30 + 3*(j-12*(iLine-1)) + 2
            if ( j .le. numsat) then
              write(outline(i1:i2), fmt='(A1,I2)') char(j),prn(j)
            else
              write(outline(i1:i2), fmt='(A3)') '   '
            endif
          endif
        enddo
c
c       fix fmt error: 12.7 should be 12.9 ... JimR 30Apr99
        if (fmt_vers.gt.1 .and. clockerr.ne.0.d0) then
          write(outline(69:80), fmt='(F12.9)') clockerr
        end if
        call writeline(ou, outline, 80)
      enddo
        
      if (flag .le. 1) then
c
c        ... Write a normal observation record
c
         do itrack = 1, numsat
            if (nobs .le. 5) then
               outline = ' '
               write(dynfmt, fmt='(A, I3.3, A)')
     +             "(", nobs, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt)
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=1,nobs)
               call writeline(ou, outline, 80)

            elseif (nobs .le. 10) then
c    write first line of obs
               outline = ' '
               write(outline, fmt='( 5(F14.3, 2A1) )')
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=1,5)
               call writeline(ou, outline, 80)
c    write second line of obs
               outline = ' '
               write(dynfmt2, fmt='(A, I3.3, A)')
     +           "(", nobs-5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt2)
     +          (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=6,nobs)
               call writeline(ou, outline, 80)

            elseif (nobs .le. 15) then
c    write first line of obs
               outline = ' '
               write(outline, fmt='( 5(F14.3, 2A1) )')
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=1,5)
               call writeline(ou, outline, 80)
c    write second line of obs
               outline = ' '
               write(dynfmt2, fmt='(A, I3.3, A)')
     +             "(", nobs-5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt2)
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=6,10)
               call writeline(ou, outline, 80)
c    write third line of obs
               outline = ' '
               write(dynfmt3, fmt='(A, I3.3, A)')
     +           "(", nobs-10, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt3)
     +          (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=11,nobs)
               call writeline(ou, outline, 80)

            elseif (nobs .le. 20) then
c    write first line of obs
               outline = ' '
               write(outline, fmt='( 5(F14.3, 2A1) )')
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=1,5)
               call writeline(ou, outline, 80)

c    write second line of obs
               outline = ' '
               write(dynfmt2, fmt='(A, I3.3, A)')
     +             "(", 5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt2)
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=6,10)
               call writeline(ou, outline, 80)

c    write third line of obs
               outline = ' '
               write(dynfmt3, fmt='(A, I3.3, A)')
     +           "(", 5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt3)
     +          (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=11,15)
               call writeline(ou, outline, 80)

c    write fourth line of obs
               outline = ' '
               write(dynfmt3, fmt='(A, I3.3, A)')
     +           "(", nobs-15, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt3)
     +          (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=16,nobs)
               call writeline(ou, outline, 80)

            elseif (nobs .le. 25) then
c    write first line of obs
               outline = ' '
               write(outline, fmt='( 5(F14.3, 2A1) )')
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=1,5)
               call writeline(ou, outline, 80)
c    write second line of obs
               outline = ' '
               write(dynfmt2, fmt='(A, I3.3, A)')
     +             "(", 5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt2)
     +            (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=6,10)
               call writeline(ou, outline, 80)
c    write third line of obs
               outline = ' '
               write(dynfmt3, fmt='(A, I3.3, A)')
     +           "(", 5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt3)
     +          (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=11,15)
               call writeline(ou, outline, 80)
c    write fourth line of obs
               outline = ' '
               write(dynfmt3, fmt='(A, I3.3, A)')
     +           "(", 5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt3)
     +          (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=16,20)
               call writeline(ou, outline, 80)
c    write fifth line of obs
               outline = ' '
               write(dynfmt3, fmt='(A, I3.3, A)')
     +           "(", 5, "(F14.3, 2A1))"
               write(outline, fmt=dynfmt3)
     +        (obs(i,itrack),lli(i,itrack),snr(i,itrack), i=21,nobs)
               call writeline(ou, outline, 80)
            endif

         enddo
      else
c
c        ... Write the (uninterpreted) event records
c
         do itrack = 1, numsat
            call writeline(ou, eventrecs(itrack), 80)
         enddo
      endif
c      
      return
      end
c      
