    module grid_types
!  
! SYNOPSIS
!   use grid_types
!
! DESCRIPTION
!  GRID_TYPES defines the model and observation data structures
!  needed for the Maryland local ensemble Kalman filter.
!
!  Types defined include:
!
!  Numerical parameters giving each dynamical variable type (e.g., PRES_TYPE)
!     and can be used as an index into the grid arrays.
!
!  REQUIRED PACKAGES
!    resolution, in the support directory
!
!  PROGRAMMER
!  Eric Kostelich, U. Maryland and Arizona State U.  kostelich@asu.edu
!
!  LANGUAGE
!    Fortran 95
!
!  Modification history:
!  08/06/05 - Moved observation types to grid_obs; added GRIDKIND parameter 
!             to adjust internal grid precision and IOKIND parameter to
!             adjust i/o precision.
!  03/28/05 - Modifications to handle internal observation consistency checks.
!  01/12/05 - Version to handle real observations.
!    Added PHI, LAMBDA, and TIME_DIFF components to both the RAW_OBS and
!    MODEL_OBS data structures.
!  10/12/04 - New version, adapted from previous grid_manager code.
!
    use precision
    use resolution,only:LATG,LATG2,LONF,LONFD,LEVS
    implicit none
!
!  GRIDKING and IOKIND need to be here to avoid circular module dependencies
!  in GRID_IO and SCALINGS.
!
    integer,parameter::GRIDKIND=R   ! type of internal model grids
    integer,parameter::IOKIND=R  ! type of i/o buffers
!
!  Patches are indexed by x, y, and z coordinates.
!
    type patch_index
       integer::x     ! 1 (Greenwich) to LONF
       integer::y     ! 1 (south pole) to LATG
       integer::z     ! 1 (surface) to LEVS
    end type
    interface operator(.eq.)
       module procedure patch_index_eq
    end interface
!
!  Mnemonics for dynamical variable types.  The variable types must be nonzero
!  and form a consecutive sequence, as they are used for indexing the model
!  grid arrays arrays.
!
    integer,parameter::NO_TYPE=-1
    integer,parameter::PRES_TYPE=1
    integer,parameter::TEMP_TYPE=2
    integer,parameter::UWIND_TYPE=3
    integer,parameter::VWIND_TYPE=4
!
!  The number of different observation types that we can handle.
!  Must agree with the number of types in the list above.
!  
    integer,parameter::NUM_DYN_TYPES=4
!
!  TYPEOF gives a 1-character mnemonic corresponding to the observation
!  variable type.  For example, TYPEOF(PRES_TYPE) is 'P'.
!
    character,parameter::TYPEOF(NUM_DYN_TYPES)=(/'P','T','U','V'/)
    contains
! -----------------------------
    pure logical function patch_index_eq(p1,p2)
    type(patch_index),intent(in)::p1,p2
!
    patch_index_eq=p1%x.eq.p2%x.and.p1%y.eq.p2%y.and.p1%z.eq.p2%z
    return
    end function patch_index_eq
! -----------------------------
    end module grid_types
