*DECK NCRCAL
      FUNCTION NCRCAL(IPLCM,NVP,NPAR,MUPLET)
*
*-----------------------------------------------------------------------
*
*Purpose:
* find the position of an elementary calculation in the Saphyb. This
* function is the translation of a nice Fortran-90 recursive function
* using Fortran-77 spaghetti coding.
*
*Copyright:
* Copyright (C) 2012 Ecole Polytechnique de Montreal
*
*Author(s): A. Hebert
*
*Parameters: input
* IPLCM   address of the TREE directory in the multicompo or Saphyb
*         LCM object.
* NVP     number of nodes in the parameter tree.
* NPAR    number of parameters.
* MUPLET  tuple used to identify an elementary calculation.
*
*Parameters: output
* NCRCAL  position of the elementary calculation (=0 if does not exist;
*         =-1 if more than one exists).
*
*-----------------------------------------------------------------------
*
      USE GANLIB
*----
*  SUBROUTINE ARGUMENTS
*----
      TYPE(C_PTR) IPLCM
      INTEGER NCRCAL,NVP,NPAR,MUPLET(NPAR)
*----
*  LOCAL VARIABLES
*----
      PARAMETER (MAXPAR=50)
      INTEGER II(2*MAXPAR+1),IKP(2*MAXPAR),NBOK(2*MAXPAR),
     1 IKEEP(2*MAXPAR)
      INTEGER, ALLOCATABLE, DIMENSION(:) ::JDEBAR,JARBVA
*
      CALL LCMLEN(IPLCM,'ARBVAL',MAXNVP,ITYLCM)
      IF(NVP.GT.MAXNVP) CALL XABORT('NCRCAL: NVP OVERFLOW.')
      ALLOCATE(JDEBAR(MAXNVP+1),JARBVA(MAXNVP))
      CALL LCMGET(IPLCM,'DEBARB',JDEBAR)
      CALL LCMGET(IPLCM,'ARBVAL',JARBVA)
      ILEV=1 ! level of recursion
      II(ILEV)=1
   10 IF(ILEV.EQ.NPAR+1) THEN
         ICAL=JDEBAR(II(ILEV)+1)
         IF(NPAR.EQ.0) GO TO 50
         GO TO 30
      ENDIF
      NBOK(ILEV)=0
      IKP(ILEV)=JDEBAR(II(ILEV))-1
   20 IKP(ILEV)=IKP(ILEV)+1
      IF(IKP(ILEV).GT.JDEBAR(II(ILEV)+1)-1) GO TO 40
      IF((MUPLET(ILEV).EQ.0).OR.
     1   (MUPLET(ILEV).EQ.JARBVA(IKP(ILEV)))) THEN
         IF(ILEV.GT.NPAR) CALL XABORT('NCRCAL: ALGORITHM FAILURE.')
         II(ILEV+1)=IKP(ILEV)
         ILEV=ILEV+1
         GO TO 10
      ENDIF
      GO TO 20
   30 ILEV=ILEV-1
      IF(ICAL.GT.0) THEN
         IKEEP(ILEV)=ICAL
         NBOK(ILEV)=NBOK(ILEV)+1
      ELSE IF(ICAL.EQ.-1) THEN
         NBOK(ILEV)=2
      ENDIF
      GO TO 20
   40 IF(NBOK(ILEV).GT.1) THEN
*        Many elementary calculation exist for this tuple.
         ICAL=-1
      ELSE IF(NBOK(ILEV).EQ.0) THEN
*        No elementary calculation exists for this tuple.
         ICAL=0
      ELSE
         ICAL=IKEEP(ILEV)
      ENDIF
      IF(ILEV.GT.1) GO TO 30
   50 CONTINUE
      DEALLOCATE(JARBVA,JDEBAR)
      NCRCAL=ICAL
      RETURN
      END
