*DECK RESDRV
      SUBROUTINE RESDRV(IPMAP,IPMTX,NFUEL,LX,LY,LZ,IMPX,IGEO,NCH,NB,
     1 NTOT,NCOMB)
*
*-----------------------------------------------------------------------
*
*Purpose:
* read and validate the fuel-map specification from the input file.
*
*Copyright:
* Copyright (C) 2007 Ecole Polytechnique de Montreal.
*
*Author(s): D. Sekki and V. Descotes
*
*Parameters: input
* IPMAP  pointer to fuel-map information.
* IPMTX  pointer to matex information.
* NFUEL  number of fuel types.
* LX     number of elements along x-axis in geometry.
* LY     number of elements along y-axis in geometry.
* LZ     number of elements along z-axis in geometry.
* IMPX   printing index (=0 for no print).
* IGEO   type of geometry (CAR3D=7 or HEXZ=9)
*
*Parameters: output
* NCH    number of reactor channels.
* NB     number of fuel bundles per channel.
* NTOT   total number of fuel bundles.
* NCOMB  number of combustion zones.
*
*-----------------------------------------------------------------------
*
      USE GANLIB
*----
*  SUBROUTINE ARGUMENTS
*----
      TYPE(C_PTR) IPMAP,IPMTX
      INTEGER NFUEL,LX,LY,LZ,IMPX,IGEO,NCH,NB,NTOT,NCOMB
*----
*  LOCAL VARIABLES
*----
      CHARACTER TEXT*12,TEXT4*4
      LOGICAL LGEOM,LXNAME,LYNAME
      DOUBLE PRECISION DFLOT
      REAL WEIGHT
*----
*  ALLOCATABLE ARRAYS
*----
      INTEGER, ALLOCATABLE, DIMENSION(:) :: INX,INY,IZONE
*
      IMPX=0
      LGEOM=.TRUE.
      NCH=0
      NB=0
*----
*  TYPE OF GEOMETRY
*----
      LXNAME=.TRUE.
      LYNAME=.TRUE.
      IF (IGEO.EQ.7) THEN
        LXNAME=.TRUE.
        LYNAME=.TRUE.
      ELSEIF (IGEO.EQ.9) THEN
        LXNAME=.FALSE.
        LYNAME=.FALSE.
      ELSE
        CALL XABORT('@RESDRV: ONLY 3D-CARTESIAN OR 3D HEXAGONAL' 
     1 //' GEOMETRY EXPECTED')
      ENDIF
   10 CALL REDGET(ITYP,NITMA,FLOT,TEXT,DFLOT)
      IF(ITYP.NE.3)CALL XABORT('@RESDRV: CHARACTER DATA EXPECTED.')
      IF(TEXT.EQ.'EDIT')THEN
*----
*  PRINTING INDEX
*----
        CALL REDGET(ITYP,NITMA,FLOT,TEXT,DFLOT)
        IF(ITYP.NE.1)CALL XABORT('@RESDRV: INTEGER DATA EXPECTED(1).')
        IMPX=MAX(0,NITMA)
        IF(IMPX.GT.4)CALL LCMLIB(IPMTX)
      ELSE IF(TEXT.EQ.'WEIGHT') THEN
*----
*  FUEL WEIGHT
*----
        CALL REDGET(ITYP,NB,WEIGHT,TEXT,DFLOT)
        IF(ITYP.NE.2) CALL XABORT('@RESDRV : REAL DATA '
     +          //'EXPECTED(1).')
        IF(WEIGHT.EQ.0.0 ) CALL XABORT('@RESDRV: INVALID'
     +       //'VALUE FOR FUEL WEIGHT')
        CALL LCMPUT(IPMAP,'FUEL-WEIGHT',1,2,WEIGHT)
      ELSE IF(TEXT.EQ.':::') THEN
*----
*  FUEL-MAP GEOMETRY
*----
        LGEOM=.FALSE.
        CALL REDGET(ITYP,NITMA,FLOT,TEXT,DFLOT)
        IF(ITYP.NE.3)CALL XABORT('@RESDRV: CHARACTER DATA EXPECTED(5).')
        IF(TEXT.NE.'GEO:')CALL XABORT('@RESDRV: EMBEDDED GEO: MODULE '
     1  //'EXPECTED.')
*----
*  CHECK GEOMETRY
*----
        CALL RESGEO(IPMAP,IPMTX,LX,LY,LZ,NFUEL,IMPX,IGEO,NX,NY,NZ,NCH,
     1  NB,NTOT)
      ELSEIF(TEXT.EQ.'NXNAME') THEN
*----
*  CHANNEL X-NAMES
*----
        LXNAME=.FALSE.
        ALLOCATE(INX(NX))
        DO I=1,NX
          CALL REDGET(ITYP,NITMA,FLOT,TEXT4,DFLOT)
          IF(ITYP.NE.3)CALL XABORT('@RESDRV: CHARACTER DATA FOR NXNAME'
     1    //' EXPECTED.')
          READ(TEXT4,'(A4)') INX(I)
        ENDDO
        CALL LCMPUT(IPMAP,'XNAME',NX,3,INX)
        DEALLOCATE(INX)
      ELSE IF(TEXT.EQ.'NYNAME') THEN
*----
*  CHANNEL Y-NAMES
*----
        LYNAME=.FALSE.
        ALLOCATE(INY(NY))
        DO I=1,NY
          CALL REDGET(ITYP,NITMA,FLOT,TEXT4,DFLOT)
          IF(ITYP.NE.3)CALL XABORT('@RESDRV: CHARACTER DATA FOR NYNAME'
     1    //' EXPECTED.')
          READ(TEXT4,'(A4)') INY(I)
        ENDDO
        CALL LCMPUT(IPMAP,'YNAME',NY,3,INY)
        DEALLOCATE(INY)
      ELSE IF(TEXT.EQ.'NCOMB') THEN
*----
*  NUMBER OF COMBUSTION ZONES
*----
        IF(NCH.EQ.0) CALL XABORT('@RESDRV: NO FUEL CHANNELS DEFINED')
        ALLOCATE(IZONE(NCH))
        CALL XDISET(IZONE,NCH,0)
        CALL REDGET(ITYP,NITMA,FLOT,TEXT,DFLOT)
        IF((ITYP.EQ.3).AND.(TEXT.EQ.'ALL'))THEN
          NCOMB=NCH
          DO 20 ICH=1,NCH
   20     IZONE(ICH)=ICH
        ELSEIF(ITYP.EQ.1)THEN
          IF(NITMA.LT.1)CALL XABORT('@RESDRV: INVALID NCOMB < 1')
          IF(NITMA.GT.NCH)CALL XABORT('@RESDRV: INVALID NCOMB > NCH')
          NCOMB=NITMA
*----
*  COMBUSTION-ZONE INDICES
*----
          CALL REDGET(ITYP,NITMA,FLOT,TEXT,DFLOT)
          IF(TEXT.NE.'B-ZONE')CALL XABORT('@RESDRV: KEYWORD B-ZONE'
     1    //' EXPECTED.')
          DO 30 ICH=1,NCH
          CALL REDGET(ITYP,NITMA,FLOT,TEXT,DFLOT)
          IF(ITYP.NE.1)CALL XABORT('@RESDRV: INTEGER COMBUSTION'
     1    //'-ZONE INDEX EXPECTED.')
          IF(NITMA.LT.1)CALL XABORT('@RESDRV: INVALID COMBUSTION'
     1    //'-ZONE INDEX < 1')
          IF(NITMA.GT.NCOMB)CALL XABORT('@RESDRV: INVALID COMBUSTION'
     1    //'-ZONE INDEX > NCOMB')
          IZONE(ICH)=NITMA
   30     CONTINUE
        ELSE
          CALL XABORT('@RESDRV: INVALID INPUT FOR NCOMB.')
        ENDIF
        CALL LCMPUT(IPMAP,'B-ZONE',NCH,1,IZONE)
        DEALLOCATE(IZONE)
        GO TO 40
      ELSE
        CALL XABORT('@RESDRV: INVALID KEYWORD ('//TEXT//').')
      ENDIF
      GO TO 10
*
   40 IF(NCH.EQ.0) CALL XABORT('@RESDRV: NO FUEL CHANNELS DEFINED.')
      IF(NB.EQ.0) CALL XABORT('@RESDRV: NO FUEL BUNDLES DEFINED.')
      IF(LGEOM) CALL XABORT('@RESDRV: OPERATOR ::: EXPECTED.')
      IF(LXNAME) CALL XABORT('@RESDRV: KEYWORD NXNAME EXPECTED.')
      IF(LYNAME) CALL XABORT('@RESDRV: KEYWORD NYNAME EXPECTED.')
      RETURN
      END
