program write_grads

implicit none

!-----------------------------------------------------------------------
!  Diagnostic data dimensions
!-----------------------------------------------------------------------
!  (x,y,z,vars,t) grid dimensions
!     nx, ny = number of (x,y) grid points, respectively
!     nz     = number of vertical grid levels
!     nv     = number of variables
!     nt     = number of time-levels
!-----------------------------------------------------------------------
integer, parameter :: nx=10, ny=10, nz=10, nv=3, nt=3

!-----------------------------------------------------------------------
!  Diagnostic Data
!-----------------------------------------------------------------------
real, dimension(nx,ny,nz,nv,nt) :: dd

!-----------------------------------------------------------------------
!  Fortran unit number for writing out the GrADS data
!-----------------------------------------------------------------------
integer :: unit=10

!-----------------------------------------------------------------------
!  Output filename for constructing GrADS output files
!-----------------------------------------------------------------------
character(len=72) :: filename = 'grads_example'

!-----------------------------------------------------------------------
!  Output data characteristics
!-----------------------------------------------------------------------
character(len=30) :: data_options = 'little_endian'

!-----------------------------------------------------------------------
!  GrADS undef value
!-----------------------------------------------------------------------
real :: undef=-999.

!-----------------------------------------------------------------------
!  Grid parameters...
!-----------------------------------------------------------------------
!
! (x,y,z) grid origin values
!
real :: x0=0.,y0=0.,z0=0.
!
! (x,y,z) grid increments
!
real :: dx=10.,dy=10.,dz=1.
!
! time origin, increment
!
character(len=30) :: t0 = '00:00z1Jan2004', dt = '1mn'

!-----------------------------------------------------------------------
!  GrADS variables names and units
!-----------------------------------------------------------------------
character(len=15), dimension(nv) :: var, var_units

!-----------------------------------------------------------------------
!  Local variables
!-----------------------------------------------------------------------
integer :: i,j,k,iv,n
character(len=80) :: ctl_file, ieee_file

!-----------------------------------------------------------------------
!  Define GrADS ieee binary data and ctl files...
!-----------------------------------------------------------------------
      ctl_file = TRIM(filename) // '.ctl'
      ieee_file = TRIM(filename) // '.ieee'

!-----------------------------------------------------------------------
!  Define and write the diagnostic output into GrADS ieee binary data file...
!-----------------------------------------------------------------------

      do n = 1,nt
        do iv = 1,nv
          do k = 1,nz
            do j = 1,ny
            do i = 1,nx
              dd(i,j,k,iv,n) = REAL(i+j+k+iv+n)
            enddo
            enddo
          enddo
        enddo
      enddo

      call write_grads_ieee( unit, ieee_file, nx, ny, nz, nv, nt, dd )

      var = (/ 'a', &
               'b', &
               'c' /)
      var_units = (/ '(m)', &
                     '(K)', &
                     '(s)' /)

      call write_grads_ctl( unit, ctl_file, &
                            ieee_file, &
                            data_options, &
                            undef, &
                            nx, ny, nz, nt, &
                            x0, y0, z0, t0, &
                            dx, dy, dz, dt, &
                            nv, var, var_units)

end program write_grads
