      program tseries

      implicit none

      integer iu
      parameter ( iu = 1 )      ! Fortran unit number; use for GrADS I/O
      integer nx, ny, nz, nt, nv
      real x0, y0, z0
      real dx, dy, dz
      parameter ( nx = 1, x0 = 0., dx = 1. )
      parameter ( ny = 1, y0 = 0., dy = 1. )
      parameter ( nz = 1, z0 = 0., dz = 1. )
      parameter ( nt = 100 )
      parameter ( nv = 2 )
      real undef
      parameter ( undef = -999. )
      character (len=15) ieee_file, ctl_file, data_options, t0, dt
      character (len=15) var(nv), var_units(nv)
      character (len=1) char_iv

      integer n, iv
      real ts(nv,nt)

      do iv=1,nv
        write(char_iv,'(i1)') iv
        var(iv) = 'var' // char_iv(1:LEN_TRIM(char_iv))
        var_units(iv) = var(iv)
        do n=1,nt
          ts(iv,n) = REAL(iv+n)
        enddo
      enddo

      ieee_file = 'tseries.ieee'
      ctl_file = 'tseries.ctl'
      data_options = 'little_endian'
      t0 = '00:00z1Jan2005'
      dt = '1hr'

      print *,' '
      print *,'Writing GrADS ieee file for GrADS dataset'
      print *,' ',ieee_file
      print *,' '

      open(iu,file=ieee_file,form="unformatted",access="direct",
     &     recl=4*nv*nt,status="unknown")

      write(iu,rec=1) ts

      close(iu)

      print *,' '
      print *,'Writing GrADS ctl file for GrADS dataset'
      print *,' '

      open(iu,file=ctl_file,status="unknown")
      rewind(iu)

      write(iu,10) ieee_file(1:LEN_TRIM(ieee_file))
      write(iu,11) data_options(1:LEN_TRIM(data_options))
      write(iu,12) undef
      write(iu,13) nx,x0,dx
      write(iu,14) ny,y0,dy
      write(iu,15) nz,z0,dz
      write(iu,16) nt,t0(1:LEN_TRIM(t0)),dt(1:LEN_TRIM(dt))
      write(iu,17) nv
      do iv=1,nv
        write(iu,18) var(iv)(1:LEN_TRIM(var(iv))),nz,
     &               var_units(iv)(1:LEN_TRIM(var_units(iv)))
      enddo
      write(iu,19)
      close(iu)

      print *,'Finished writing GrADS ctl file:'
      print *,' ',ctl_file
      print *,' '

10    format('dset ^',a)
11    format('options ',a)
12    format('undef ',e12.5)
13    format('xdef ',i4,' linear ',e12.5,' ',e12.5)
14    format('ydef ',i4,' linear ',e12.5,' ',e12.5)
15    format('zdef ',i4,' linear ',e12.5,' ',e12.5)
16    format('tdef ',i5,' linear ',a,' ',a)
17    format('vars ',i3)
18    format(a,' ',i3,' 99 ',a)
19    format('endvars')

      stop
      end
