*DECK EDIMRC
      SUBROUTINE EDIMRC(IPTRK ,IPRINT  ,NREGIO, NMERGE, IMERGE)
*
*-----------------------------------------------------------------------
*
*Purpose:
* To generate the region merging index for homogenisation
* per CELL for NXT treated geometry
*
*Copyright:
* Copyright (C) 2011 Ecole Polytechnique de Montreal.
*
*Author(s):
* G. Marleau.
*
*Parameters: input
* IPTRK   pointer to the TRACKING data structure
* IPRINT  print level
* NREGIO  number of regions
*
*Parameters: output
* NMERGE  final number of merged regions
* IMERGE  merged region index
*
*-----------------------------------------------------------------------
*
      USE         GANLIB
      IMPLICIT    NONE
*----
*  Subroutine arguments
*----
      TYPE(C_PTR) IPTRK
      INTEGER     IPRINT
      INTEGER     NREGIO
      INTEGER     NMERGE
      INTEGER     IMERGE(NREGIO)
*----
*  Local parameters
*----
      CHARACTER   NAMSBR*6
      PARAMETER  (NAMSBR='EDIMRC')
      INTEGER     NSTATE,NTC
      PARAMETER  (NSTATE=40,NTC=3)
      INTEGER     ILCMUP,ILCMDN
      PARAMETER  (ILCMUP=1,ILCMDN=2)
*----
*  Local variables
*----
      INTEGER     ISTATE(NSTATE),IEDIMG(NSTATE)
      INTEGER     KCHAR(NTC),ITC
      CHARACTER   HSIGN*12
      INTEGER     NDIM,NFREG,NFSUR,NNC,NBUCEL,NUCELL(3),MAXREG
*----
*  Test if valid tracking data structure
*  EXCELL with type 4 tracking
*----
      CALL LCMGET(IPTRK,'SIGNATURE',KCHAR)
      WRITE(HSIGN,'(3A4)') (KCHAR(ITC),ITC=1,NTC)
*----
*  TEST IF GEOMETRY OR EXCELL TRACK DATA STRUCTURE
*----
      IF(HSIGN .NE. 'L_TRACK     ') CALL XABORT(NAMSBR//
     >': Invalid data structure for merge by cell')
      CALL LCMGET(IPTRK,'TRACK-TYPE',KCHAR)
      WRITE(HSIGN,'(3A4)') (KCHAR(ITC),ITC=1,NTC)
      IF((HSIGN .NE. 'EXCELL') .AND. (HSIGN .NE. 'MCCG')) THEN
        CALL XABORT(NAMSBR//': Invalid tracking for merge by cell')
      ENDIF
      CALL XDISET(ISTATE,NSTATE,0)
      CALL LCMGET(IPTRK,'STATE-VECTOR',ISTATE)
      IF(ISTATE(7) .NE. 4) CALL XABORT(NAMSBR//
     >': Only NXT tracking permitted for merge by cell')
      IF(ISTATE(40) .EQ. 1) CALL XABORT(NAMSBR//
     >': Double heterogeneity (Bihet) not implemented')
      CALL LCMSIX(IPTRK,'NXTRecords  ',ILCMUP)
      CALL LCMGET(IPTRK,'G00000001DIM',IEDIMG)
      NDIM     =IEDIMG( 1)
      NNC      =IEDIMG( 4)
      NBUCEL   =IEDIMG( 5)
      NUCELL(1)=IEDIMG(13)
      NUCELL(2)=IEDIMG(14)
      NUCELL(3)=IEDIMG(15)
      NFSUR    =IEDIMG(22)
      NFREG    =IEDIMG(23)
      MAXREG   =IEDIMG(25)
      CALL EDIMCN(IPTRK ,IPRINT,NDIM  ,NUCELL,NBUCEL,MAXREG,
     >            NFREG,NFSUR,NNC,NREGIO,NMERGE,IMERGE)
      CALL LCMSIX(IPTRK,'NXTRecords  ',ILCMDN)
      RETURN
      END
