*DECK D2PREF
      SUBROUTINE D2PREF( IPDAT,   NVAR,   CRDINF,   NCRD,  GRID,IPRINT)
*
*-----------------------------------------------------------------------
*
*Purpose:
* Select the reference state. This routine determine the reference state
* for all cases of meshing
*
*Author(s): J. Taforeau
*
*parameters: input
* IPDAT      address of info data block
* NVAR       number of state variables
* CRDINF     control rod compostition array
* NCRD       number of crontrol rod comosition
* GRID       type of griddind for branching calculation
*
*-----------------------------------------------------------------------
*
      USE GANLIB
*----
*  SUBROUTINE ARGUMENTS
*----
      TYPE(C_PTR) IPDAT
      INTEGER NVAR,NBR,NCRD,GRID
      INTEGER CRDINF(NCRD)
*----
*  LOCAL VARIABLES
*----
      INTEGER ITYLCM,i
      INTEGER STAIDX(NVAR),REFIDX(NVAR)
      INTEGER NVALPA(NVAR)
      REAL STATE(NVAR) ,REFSTA(NVAR-1),HSTSTA(NVAR-1)
      REAL VALPAR(NVAR,100)
      CHARACTER(LEN=4) PKEY(NVAR)

      ! RECOVER INFORMATION FROM INFO DATA BLOCK
      CALL LCMSIX(IPDAT,' ',0)
      CALL LCMSIX(IPDAT,'SAPHYB_INFO',1)

      CALL LCMGTC(IPDAT,'STATE_VAR',4,NVAR,PKEY)
      ! INITIALIZATION OF THE NUMBER OF BRANCHES TO BE CALCULATED
      NBR=1
      DO i=1, NVAR
         CALL LCMLEN(IPDAT,PKEY(i),NVALPA(i),ITYLCM)
         CALL LCMGET(IPDAT,PKEY(i),VALPAR(i,1:NVALPA(i)))

        ! ATTRIBUTION OF VALUES FOR THE BARR PARAMETERS
         IF (PKEY(i)=='BARR') THEN
          REFSTA(i)= CRDINF(1)
          HSTSTA(i)= CRDINF(1)
          REFIDX(i)=1         ! INITIALIZATION OF BARR REFERENCE INDEX
          STATE(i)=CRDINF(1)  ! ATTRIBUTION OF  CONTROL ROD COMPOSITION
          STAIDX(i)=1    ! ATTRIBUTION OF  CONTROL ROD COMPOSITION INDEX
          NBR=NBR*NVALPA(i)   ! CALCULATION OF NUMBER OF BRANCHES
        ! IDEM FOR BURN PARAMETERS
         ELSE IF (PKEY(i)=='BURN') THEN
          STATE(i)=VALPAR(i,1)
          STAIDX(i)=1
          REFIDX(i)=1
          EXIT
        ! IDEM FOR OTHER PARAMETERS
         ELSE
         ! THE REFERENCE STATES IS SET TO THE MIDDLE VALUE IN THE LIST
          REFSTA(i)= VALPAR(i,NINT(NVALPA(i)/2.0))
          HSTSTA(i)= VALPAR(i,NINT(NVALPA(i)/2.0))
          REFIDX(i)=NINT(NVALPA(i)/2.0)
          STATE(i)=REFSTA(i)
          STAIDX(i)=NINT(NVALPA(i)/2.0)
          NBR=NBR*NVALPA(i)
         ENDIF
      ENDDO

      CALL LCMSIX(IPDAT,' ',0)
      CALL LCMSIX(IPDAT,'BRANCH_INFO',1)
      CALL LCMPUT(IPDAT,'PRINT',1,1,1)

      IF((NBR>9999).OR.(GRID==0)) THEN
         ! IN THE CASE WHERE THE NUMBER OF BRANCHES EXCEED 999, A
         ! DEFAULT BANCHING CALCULATION IS CALLED
         GRID = 0
         CALL D2PDEF( IPDAT,  PKEY, VALPAR,   NVALPA,  STAIDX, REFIDX,
     >                REFSTA,HSTSTA,  STATE,   CRDINF,    NCRD,  NVAR,
     >                IPRINT                                         )
      ELSE
         ! UPADATE THE INFO DATA BLOCK WITH THE INITIAL MESHING FROM
         ! SAPHYB
         CALL LCMSIX(IPDAT,' ',0)
         CALL LCMSIX(IPDAT,'BRANCH_INFO',1)
         CALL LCMPUT(IPDAT,'BRANCH',1,3,'BARR')
         CALL LCMPUT(IPDAT,'BRANCH_IT',1,1,1)
         CALL LCMPUT(IPDAT,'REF_STATE',NVAR-1,2,REFSTA)
         CALL LCMPUT(IPDAT,'HST_STATE',NVAR-1,2,REFSTA)
         CALL LCMPUT(IPDAT,'REF_INDEX',NVAR,1,REFIDX)
         CALL LCMPUT(IPDAT,'BRANCH_NB',1,1,NBR)
         CALL LCMPUT(IPDAT,'STATE',NVAR,2,STATE)
         CALL LCMPUT(IPDAT,'STATE_INDEX',NVAR,1,STAIDX)
         CALL LCMPUT(IPDAT,'BRANCH_INDEX',1,1,1)
         CALL LCMPUT(IPDAT,'REWIND',1,1,1)
         CALL LCMPUT(IPDAT,'STOP',1,1,0)

         IF(IPRINT > 1)  THEN
          WRITE(6,*)
          WRITE(6,*) "*** INFORMATION ABOUT BRANCHING CALCULATION  ***"
          WRITE(6,*)
          WRITE(6,*) "DEFAULT MESHING (Y/N) : N"
          IF(GRID==4) WRITE(6,*) "MESHING: NEW GRID WITH ADDITIONAL PTS"
          IF (GRID==3) WRITE(6,*) "MESHING: SAPHYB WITH ADDITIONAL PTS"
          IF (GRID==2) WRITE(6,*) "MESHING: USER DEFINED "
          IF (GRID==1) WRITE(6,*) "MESHING: SAPHYB "
          WRITE(6,*) "STATE PARAMETERS : ",PKEY(1:NVAR-1)
          WRITE(6,*) "REFERENCE STATES VALUES :", REFSTA
          WRITE(6,*) "INITIAL STATES VALUES :", STATE
          WRITE(6,*) "INITIAL STATES INDEX VALUES :", STAIDX
         ENDIF
      ENDIF
      END
