c
c     ##################################################################
c     ##################################################################
c     ######                                                      ######
c     ######                SUBROUTINE PAKDUMP                    ######
c     ######                                                      ######
c     ######                Copyright (c) 1992                    ######
c     ######  Center for the Analysis and Prediction of Storms    ######
c     ######    University of Oklahoma.  All rights reserved.     ######
c     ######                                                      ######
c     ##################################################################
c     ##################################################################
c
      SUBROUTINE PAKDUMP(nx,ny,nz, nchanl, grdbas, stagrd,
     :           u,v,w,ptprt,pprt,qv,qc,qr,qi,qs,qh,km,
     :           ubar,vbar,ptbar,pbar,rhobar,qvbar,
     :           x,y,z,zp,hterain, j1,j2,j3, tem1, intem)
c
c#######################################################################
c
c     IMPORTANT NOTICE:
c
c     This version is designed for the IBM RISC/6000 and VAX computers
c     (that write IEEE standard integer*4 binary files).  
c     The files are generally not portable between different machine types.
c
c     PURPOSE:
c
c     Write history data into direct access channel nchanl,
c     as packed integers.
c
c     All output data are located at the grid cell centers, therefore
c     u, v and w are averaged to the cell center before being dumped. 
c
c     Note: coordinate fields are dumped as 3 dimensional fields which
c     have been converted from meters to kilometers.  This is for the
c     convenience of the plotting applications.
c
c#######################################################################
c
c     AUTHOR: Keith Brewster
c     6/25/92.    
c
c     MODIFICATION HISTORY:
c
c     7/14/92 (K. Brewster)
c     Added runname, comment and version number writing
c
c     7/25/92 (K. Brewster)
c     Replaced use of integer*2 with packing into integer*4's
c
c     8/23/92 (M. Xue)
c     Modify to perform the dumping of both base and t-dependent arrays
c     and added control on grid staggering.
c
c#######################################################################
c
c     INPUT:
c
c       nx       Number of grid points in the x-direction (east/west)
c       ny       Number of grid points in the y-direction (north/south)
c       nz       Number of grid points in the vertical
c
c       nchanl   Fortran I/O channel number for history data output.
c       grdbas   Flag indicating if this is a call for the data dump
c                of grid and base state arrays only. If so, grdbas=1.
c       stagrd   Indicating if the input variables are on a staggered
c                grid. If not (stgred=0), all variables are located at
c                the grid cell center, and the units of the coordinates
c                are in km insted of meter otherwise.
c
c       u        x component of velocity at a given time level (m/s)
c       v        y component of velocity at a given time level (m/s)
c       w        Vertical component of Cartesian velocity at a given
c                time level (m/s)
c       ptprt    Perturbation potential temperature at a given time
c                level (K)
c       pprt     Perturbation pressure at  a given time level (Pascal)
c       qv       Water vapor specific humidity at a given time level (kg/kg)
c       qc       Cloud water mixing ratio at a given time level (kg/kg)
c       qr       Rainwater mixing ratio at a given time level (kg/kg)
c       qi       Cloud ice mixing ratio at a given time level (kg/kg)
c       qs       Snow mixing ratio at a given time level (kg/kg)
c       qh       Hail mixing ratio at a given time level (kg/kg)
c
c       km       Turbulent mixing coefficient (m**2/s)
c
c       ubar     Base state zonal velocity component (m/s)
c       vbar     Base state meridional velocity component (m/s)
c       ptbar    Base state potential temperature (K)
c       pbar     Base state pressure (Pascal)
c       rhobar   Base state density (kg/m**3)
c       qvbar    Base state water vapor specific humidity (kg/kg)
c
c       x        x coordinate of grid points in physical/comp. space (m)
c       y        y coordinate of grid points in physical/comp. space (m)
c       z        z coordinate of grid points in computational space (m)
c       zp       Vertical coordinate of grid points in physical space (m)
c       hterain  The height of terrain (m)
c
c       j1       Coordinate transform Jacobian -d(zp)/dx
c       j2       Coordinate transform Jacobian -d(zp)/dy
c       j3       Coordinate transform Jacobian  d(zp)/dz
c
c     OUTPUT:
c
c       None.
c
c     WORK ARRAYS:
c
c       tem1       work array.
c       intem      integer work array.
c
c#######################################################################
c
c     Variable Declarations.
c
c#######################################################################
c
      implicit none 
c
      integer nx,ny,nz          ! Number of grid points in 3 directions
c
      integer nchanl            ! Fortran I/O channel number for output
      integer grdbas            ! If this is a grid/base state dump
      integer stagrd            ! If input arrays are on a staggered grid
c
      real u     (nx,ny,nz)     ! Total u-velocity (m/s)
      real v     (nx,ny,nz)     ! Total v-velocity (m/s)
      real w     (nx,ny,nz)     ! Total w-velocity (m/s)
      real ptprt (nx,ny,nz)     ! Perturbation potential temperature (K)
      real pprt  (nx,ny,nz)     ! Perturbation pressure (Pascal)
c
      real qv    (nx,ny,nz)     ! Water vapor specific humidity (kg/kg)
      real qc    (nx,ny,nz)     ! Cloud water mixing ratio (kg/kg)
      real qr    (nx,ny,nz)     ! Rain water mixing ratio (kg/kg)
      real qi    (nx,ny,nz)     ! Cloud ice mixing ratio (kg/kg)
      real qs    (nx,ny,nz)     ! Snow mixing ratio (kg/kg)
      real qh    (nx,ny,nz)     ! Hail mixing ratio (kg/kg)
c
      real km    (nx,ny,nz)     ! The turbulent mixing coefficient for
                                ! momentum. ( m**2/s )
c
      real ubar  (nx,ny,nz)     ! Base state u-velocity (m/s)
      real vbar  (nx,ny,nz)     ! Base state v-velocity (m/s)
      real ptbar (nx,ny,nz)     ! Base state potential temperature (K)
      real pbar  (nx,ny,nz)     ! Base state pressure (Pascal)
      real rhobar(nx,ny,nz)     ! Base state air density (kg/m**3)
      real qvbar (nx,ny,nz)     ! Base state water vapor specific humidity
                                ! (kg/kg)

      real x     (nx)           ! The x-coord. of the physical and
                                ! computational grid. Defined at u-point.
      real y     (ny)           ! The y-coord. of the physical and
                                ! computational grid. Defined at v-point.
      real z     (nz)           ! The z-coord. of the computational grid.
                                ! Defined at w-point on the staggered grid.
      real zp    (nx,ny,nz)     ! The physical height coordinate defined at
                                ! w-point of the staggered grid.

      real hterain(nx,ny)       ! The height of the terrain.

      real j1    (nx,ny,nz)     ! Coordinate transform Jacobian defined as
                                ! - d( zp )/d( x )
      real j2    (nx,ny,nz)     ! Coordinate transform Jacobian defined as
                                ! - d( zp )/d( y )
      real j3    (nx,ny,nz)     ! Coordinate transform Jacobian defined as
                                ! d( zp )/d( z )

      real tem1  (nx,ny,nz)     ! Temporary work array
      integer intem (nx*ny*nz)  ! Temporary integer work array
c
c#######################################################################
c
c     Parameters describing this routine
c
c#######################################################################
c
      character*40 fmtver
      parameter (fmtver='003.00 Packed Binary Data')
      character*10 tmunit
      parameter (tmunit='seconds   ')
c
c#######################################################################
c
c     Misc. local variables: 
c
c#######################################################################
c
      real mtokm    ! conversion factor: meters to kilometers
                    ! used for scaling coordinate grid for output
      parameter ( mtokm=0.001 ) 
      integer ihdlen,lablen
      parameter (ihdlen=5000, lablen=40)
      integer ihead(ihdlen)
      integer label(lablen)
      integer i,j,k,l
      integer nxyz
      integer ilen,illen
      integer n3dary,n3dsclr
      real amax,amin,ref
      integer iscale,nbits,ihend,iend
      integer ishf, jshf, kshf
      real scale
c
C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
C
C     Beginning of executable code...
C
C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
c

c
      RETURN
      END     
