      subroutine write_sounding(irec_snd,odir,ofile_snd,varo_snd,
     &                          po,zo,uo,vo,tho,qvo,wspdo,wdiro,to,tdo)
      implicit none

      !-----------------------------------------------------------------------
      ! Subroutine "write_sounding"
      !
      ! Writes sounding data to GrADS output file...
      !-----------------------------------------------------------------------

      !-----------------------------------------------------------------------
      ! Subroutine arguments...
      !-----------------------------------------------------------------------
      include 'sounding_params'

      include 'sounding_variables'

      !-----------------------------------------------------------------------
      ! Local parameters and arrays...
      !-----------------------------------------------------------------------
      integer ounit     ! output Fortran unit
      parameter( ounit=2 )

      integer k, iv
      character*4   char_num
      character*144 output_file
      logical do_printouts

      !-----------------------------------------------------------------------
      ! External function...
      !-----------------------------------------------------------------------
      integer lensub

      do_printouts = .true.

      if( do_printouts )then
        print *, ' '
        print *, '----------------------------------------------'
        print *, 'Subroutine "write_sounding":'
        print *, '----------------------------------------------'
        print *, ' '
        print *, ' '
        print *, 'Open GrADS output unit...'
      endif
      !
      output_file = odir (1:lensub(odir)) // '/' //
     &              ofile_snd (1:lensub(ofile_snd))
      open(ounit,file=output_file,status='unknown',access='direct',
     &     form='unformatted',recl=4*kdo)
      if( do_printouts )then
        print *, ' '
        print *, 'Opened sounding output file:'
        print *, '  ',output_file
        print *, ' '
        print *, 'Write out sounding data to GrADS output file...'
        print *, ' '
      endif

      do iv=1,nvaro_snd
        if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'p' )then
          write(ounit,rec=irec_snd) po
            if( do_printouts ) print *, '...pressure'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'z' )then
          write(ounit,rec=irec_snd) zo
            if( do_printouts ) print *, '...geopotential height'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'u' )then
          write(ounit,rec=irec_snd) uo
            if( do_printouts ) print *, '...zonal wind'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'v' )then
          write(ounit,rec=irec_snd) vo
            if( do_printouts ) print *, '...meridional wind'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'th' )then
          write(ounit,rec=irec_snd) tho
            if( do_printouts ) print *, '...potential temperature'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'qv' )then
          write(ounit,rec=irec_snd) qvo
            if( do_printouts ) print *, '...water vapor'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'wspd' )then
          write(ounit,rec=irec_snd) wspdo
            if( do_printouts ) print *, '...wind speed'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'wdir' )then
          write(ounit,rec=irec_snd) wdiro
            if( do_printouts ) print *, '...wind direction'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 't' )then
          write(ounit,rec=irec_snd) to
            if( do_printouts ) print *, '...air temperature'
            irec_snd = irec_snd+1
        else if( varo_snd(iv) (1:lensub(varo_snd(iv))) == 'td' )then
          write(ounit,rec=irec_snd) tdo
            if( do_printouts ) print *, '...dewpoint temperature'
            irec_snd = irec_snd+1
        else
          print *, 'Output variable: '
          print *, ' ',varo_snd(iv) (1:lensub(varo_snd(iv)))
          print *, 'is not available for output...'
          print *, 'Stopping program.'
          stop
        endif
      enddo

      if( do_printouts )then
        print *, ' '
        print *, '...finished writing sounding data.'
      endif

      close(ounit)

      if( do_printouts )then
        print  *, ' '
        print  *, '----------------------------------------------'
        print  *, '...leaving subroutine "write_sounding".'
        print  *, '----------------------------------------------'
        print  *, ' '
      endif

      return
      end
