!-----------------------------------------------------------------------
!  write_grads_ctl:  generates GrADS ctl file
!-----------------------------------------------------------------------
subroutine write_grads_ctl(iu,ctl_file, &
                           ieee_file, &
                           data_options, &
                           undef, &
                           nx,ny,nz,nt, &
                           x0,y0,z0,t0, &
                           dx,dy,dz,dt, &
                           nv,var,var_units)
implicit none

!-----------------------------------------------------------------------
!  Fortran unit number for writing out the GrADS ctl file
!-----------------------------------------------------------------------
integer, intent(in) :: iu

!-----------------------------------------------------------------------
!  Output filename for constructing GrADS output file
!-----------------------------------------------------------------------
character(len=80), intent(in) :: ctl_file, ieee_file

!-----------------------------------------------------------------------
!  Output data characteristics
!-----------------------------------------------------------------------
character(len=30), intent(in) :: data_options

!-----------------------------------------------------------------------
!  GrADS undef value
!-----------------------------------------------------------------------
real, intent(in) :: undef

!-----------------------------------------------------------------------
!  Grid parameters...
!-----------------------------------------------------------------------
integer, intent(in) :: nx,ny,nz,nt      ! (x,y,z,t) dimensions
real,    intent(in) :: x0,y0,z0         ! (x,y,z) grid origin values
real,    intent(in) :: dx,dy,dz         ! (x,y,z) grid increments
character(len=30), intent(in) :: t0,dt  ! time origin, increment

!-----------------------------------------------------------------------
!  Number of GrADS variables, and their names and units
!-----------------------------------------------------------------------
integer, intent(in) :: nv
character(len=15), dimension(nv), intent(in) :: var, var_units

!-----------------------------------------------------------------------
!  Local variables
!-----------------------------------------------------------------------

integer :: iv

!-----------------------------------------------------------------------

      print *
      print *,'Writing GrADS ctl file for GrADS dataset: '
      print *,' ',TRIM(ieee_file)

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

      write(iu,10) TRIM(ieee_file)
      write(iu,11) 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,TRIM(t0),TRIM(dt)
      write(iu,17) nv
      do iv=1,nv
        write(iu,18) TRIM(var(iv)),nz,TRIM(var_units(iv))
      enddo
      write(iu,19)
      close(iu)

      print *,'Finished writing GrADS ctl file:'
      print *,' ',TRIM(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')

end subroutine write_grads_ctl
