!#################################################################
!
! Grids.
!
!### macro's #####################################################
!
#define TRACEBACK write (gol,'("in ",a," (",a,", line",i5,")")') rname, __FILE__, __LINE__; call goErr
#define IF_NOTOK_RETURN(action) if (status/=0) then; TRACEBACK; action; return; end if
#define IF_ERROR_RETURN(action) if (status> 0) then; TRACEBACK; action; return; end if
!
#include "tm5.inc"
!
!#################################################################

module dims_grid

  use GO, only : gol, goPr, goErr
  
  implicit none
  
  ! --- in/out ------------------------------
  
  public
  
  
  ! --- const -------------------------------
  
  
  ! Basic model definition: resolution etc. including some routines
  ! to fill the data structure.

  ! basic (coarsest) resolution in degrees for x and y (dz default 1.0)

  real, parameter     ::  dx = 6.0
  real, parameter     ::  dy = 4.0
  real, parameter     ::  dz = 1.0


  ! Maximum number of zoom regions, 
  ! including the basic (coarsest grid) region;
  ! arrays are allocated for each of these regions:
  integer, parameter  ::  nregions_max = 3
  
  ! extra grid:
  integer, parameter  ::  nregions_all = nregions_max + 1
  integer, parameter  ::  iglbsfc = nregions_max + 1

  ! Actual number of zoom regions,
  ! during testing this could be set to 1 to quickly run the model.
  ! The include file should contains the following line:
  ! 
  !  integer, parameter :: nregions=3
  !
  ! The include file 'dims_grid.h_template' is only a template;
  ! the actual file is written by the 'configure' script.
  include "dims_grid.h"

  ! region_name is used to recognise the METEO files
  ! region_name is also used in the HDF output file name
  ! region 1 should always be the global domain

  character(len=10), parameter  ::  region_name(0:nregions_all) = &
       (/ 'globe     ','glb600x400','eur300x200','eur100x100','glb100x100'/)

  ! coordinates (in degrees) for each region:
  ! xcyc = 1 if the region has cyclic x-boundary conditions
  ! touch_np = 1 if region touches the north pole
  ! touch_sp = 1 if region touches the south pole
  ! xbeg : the westmost border of the region
  ! xend : the eastmost border of the region
  ! ybeg : the southmost border of the region
  ! yend : the northmost border of the region
  ! zbeg : should be 0 (zooming in the vertical not implemented)
  ! zend : should be lm (zooming the the vertical not implemented)
  ! NOTE : zbeg and zend should be the same for all regions

  ! dimensions are NOT calculated automatically
  ! (several compilers complain about these obscure lines ...)
  !
  !integer,dimension(nregions),parameter :: &
  !     im = (xend(1:nregions)-xbeg(1:nregions))/(dx/xref(1:nregions)) + .5
  !integer,dimension(nregions),parameter :: &
  !     jm = (yend(1:nregions)-ybeg(1:nregions))/(dy/yref(1:nregions)) + .5

  integer, dimension(0:nregions_all), parameter  ::  &  
       xcyc     = (/    1,    1,    0,    0,    1 /),       &
       touch_np = (/    1,    1,    0,    0,    1 /),       &
       touch_sp = (/    1,    1,    0,    0,    1 /),       &
       xbeg     = (/ -180, -180,  -36,  -21, -180 /),       &
       xend     = (/  180,  180,   54,   39,  180 /),       &
       ybeg     = (/  -90,  -90,    2,   12,  -90 /),       &
       yend     = (/   90,   90,   74,   66,   90 /),       &
       im       = (/    1,   60,   30,   60,  360 /),       &
       jm       = (/    1,   45,   36,   54,  180 /)


  ! maximum refinement factor (can be arbitrary in principle):

  integer, parameter :: maxref = 10

  ! refinement factors for each region (<= maxref)
  ! tref may differ from xref/yref. In the current 
  ! implementation it should be 1,2,4,6,...

  integer, dimension(0:nregions_max), parameter ::    &
       xref = (/ 1, 1, 2, 6 /),                &
       yref = (/ 1, 1, 2, 4 /),                &
       zref = (/ 1, 1, 1, 1 /),                &
       tref = (/ 1, 1, 2, 4 /)

  ! Define the parent of each region. 
  ! Global region 1 should have parent 0.
  integer, parameter  ::  parent(nregions_max) = (/ 0, 1, 2 /)

  ! NB: number of region should not be smaller than number of its parent:
  ! region > parent(region)


end module dims_grid
