!------------------------------------------------------------------------------
!  ALAD_FIND_Z  FIND ALADDIN DATA ENTRY, BASED ON LABEL KEYS
!
SUBROUTINE ALAD_FIND_Z(ICTRL,ZHLBL,ZBLBL,ZNBLBL,IER)
!
!  INPUT:
!============
! idecl:  explicitize implicit INTEGER declarations:
  use alpcom_mod
  use alcom_mod
  IMPLICIT NONE
  INTEGER ilptr,icptr,iflag
!============
  INTEGER ICTRL		  ! CONTROL FLAG
!  ICTRL=1:  REWIND FILE AT START
  CHARACTER*(*) ZHLBL	  ! HIERARCHICHAL LABEL
  CHARACTER*(*) ZBLBL         ! BOOLEAN .AND. LABEL
  CHARACTER*(*) ZNBLBL	  ! BOOLEAN .NOT. LABEL
!
!  OUTPUT:
  INTEGER IER		  ! COMPLETION CODE, 0 = SUCCESS
!
!------------------------------------------------------------------------------
!  THIS ROUTINE TRIES TO FIND AN ENTRY IN THE CURRENT ALADDIN DATABASE
!  MATCHING THE PASSED KEY LABEL SPECIFICATIONS
!  THESE LABEL SPECS ARE THE SAME ONES THAT ONE TYPES IN WHEN RUNNING
!  ALADDIN INTERACTIVELY USING THE "SL" COMMAND -- CF EXE:ALADDIN.EXE
!  AND THE ALADDIN USER'S MANUAL
!
!  IF A MATCHING ENTRY IS FOUND, THE FUNCTION EVALUATION COEFFICIENTS
!  FOR THE ENTRY ARE READ IN.
!
!  THE CODE FOR THIS ROUTINE IS ESSENTIALLY STOLEN FROM THE ALADDIN
!  INTERACTIVE DRIVER CODE, ESP. ROUTINES ALL, ALS, AND ALEV, WITH
!  MINOR MODIFICATIONS -- DMC 1 JUNE 1990
!
!  A COPY OF ALADDIN IS IN TRANSP SOURCE:ALADDIN.FOR
!------------------------------------------------------------------------------
!
!
  CHARACTER*1 ZNULL
!
  ZNULL=CHAR(0)
!
  IF((DFNAME.EQ.'**NONE**').OR.(DFNAME(1:1).EQ.ZNULL)) THEN
     IER=1
     LUTOUT=6
     WRITE(LUTOUT,1001)
1001 FORMAT(/&
          ' ALAD_FIND_Z:  ALADDIN FILE NOT OPEN-- CALL ALAD_INIT_Z FIRST')
     RETURN
  ENDIF
!
!      WRITE(LUTOUT,'('' **ENTERING ALAD_FIND_Z** (ORNL ALADDIN) '')')
!        Write(LUtout,9234) ZHLBL
! 9234   Format(1x, A)
!
  IER=1
!
  IF(ICTRL.EQ.1) THEN
     REWIND LUDF
!      	  WRITE(LUTOUT, '('' DATA FILE REWOUND'')' )
     LESEQN = 0
     EOF = .FALSE.
  ENDIF
!
!  PARSE PASSED LABELS INTO ALADDIN COMMON (AS IN ALADDIN.FOR SUBROUTINE
!  ALL) USING PASSED INPUT INSTEAD OF KEYBOARD INPUT
!
!---------------------------
!  HIERARCHICAL LABEL
!
  IF(ZHLBL.EQ.' ') THEN
     CMND='.'
  ELSE
     IF(INDEX(ZHLBL,'.').EQ.0) THEN
        CMND=ZHLBL//' .'
     ELSE
        CMND=ZHLBL
     ENDIF
  ENDIF
!
  NHSL = 0
!
  ILPTR = 1
  ICPTR = 1
  200 CALL ALXLBL_Z(CMND, 1, ILPTR, ICPTR, LABEL, NCHLBL, IFLAG)
!
  IF(IFLAG .EQ. -1) THEN
     WRITE(LUTOUT, '(''ALAD_FIND_Z: H.LABEL "'', A, ''" TOOLONG'')' )&
          LABEL
     RETURN
  ENDIF
!
  IF(IFLAG .EQ. 1) THEN
     WRITE(LUTOUT, '(''ALAD_FIND_Z: H.LABEL "'', A, ''" INVALID'')' )&
          LABEL
     RETURN
  ENDIF
!
  IF(IFLAG .EQ. 0 .AND. LABEL .NE. '.') THEN
     NHSL = NHSL + 1
     HSL(NHSL) = LABEL
     GO TO 200
  ENDIF
!
!---------------------------
!  BOOLEAN .AND. LABEL
!
  IF(ZBLBL.EQ.' ') THEN
     CMND='.'
  ELSE
     IF(INDEX(ZBLBL,'.').EQ.0) THEN
        CMND=ZBLBL//' .'
     ELSE
        CMND=ZBLBL
     ENDIF
  ENDIF
!
  NBASL = 0
!
  ILPTR = 1
  ICPTR = 1
400 CALL ALXLBL_Z(CMND, 1, ILPTR, ICPTR, LABEL, NCHLBL, IFLAG)
!
  IF(IFLAG .EQ. -1) THEN
     WRITE(LUTOUT, '(''ALAD_FIND_Z: B.LABEL "'', A, ''" TOOLONG'')' )&
          LABEL
     RETURN
  ENDIF
!
  IF(IFLAG .EQ. 1) THEN
     WRITE(LUTOUT, '(''ALAD_FIND_Z: B.LABEL "'', A, ''" INVALID'')' )&
          LABEL
     RETURN
  ENDIF
!
  IF(IFLAG .EQ. 0 .AND. LABEL .NE. '.') THEN
     NBASL = NBASL + 1
     BASL(NBASL) = LABEL
     GO TO 400
  ENDIF
!
!---------------------------
!  BOOLEAN .NOT. LABEL
!
  IF(ZNBLBL.EQ.' ') THEN
     CMND='.'
  ELSE
     IF(INDEX(ZNBLBL,'.').EQ.0) THEN
        CMND=ZNBLBL//' .'
     ELSE
        CMND=ZNBLBL
     ENDIF
  ENDIF
!
  NBNSL = 0
!
  ILPTR = 1
  ICPTR = 1
600 CALL ALXLBL_Z(CMND, 1, ILPTR, ICPTR, LABEL, NCHLBL, IFLAG)
!
  IF(IFLAG .EQ. -1) THEN
     WRITE(LUTOUT, '(''ALAD_FIND_Z: N.LABEL "'', A, ''" TOOLONG'')' )&
          LABEL
     RETURN
  ENDIF
!
  IF(IFLAG .EQ. 1) THEN
     WRITE(LUTOUT, '(''ALAD_FIND_Z: N.LABEL "'', A, ''" INVALID'')' )&
          LABEL
     RETURN
  ENDIF
!
  IF(IFLAG .EQ. 0 .AND. LABEL .NE. '.') THEN
     NBNSL = NBNSL + 1
     BNSL(NBNSL) = LABEL
     GO TO 600
  ENDIF
!
!------------------------------------------------------------------------------
!
!
!      WRITE(LUTOUT,
!     & '(/'' ALAD_FIND_Z: STARTING SEARCH AT ENTRY SEQ. NUMBER '',I6)' )
!     &     LESEQN+1
!
700 CALL ALREAD_Z(LUDF, LESEQN, EOF, ERRMSG)
!
  IF(EOF) THEN
     WRITE(LUTOUT,&
          '(/'' ***** END OF FILE REACHED, NO MATCH FOUND *****'')' )
     WRITE(LUTOUT,&
          '(/'' NOW AT LAST ENTRY IN FILE, SEQUENCE NUMBER '',I6)' )&
          LESEQN
     RETURN
  ENDIF
!
  IF(ERRMSG .NE. ' ') THEN
     WRITE(LUTOUT, '('' ERROR: '', A)' ) ERRMSG
     RETURN
  ENDIF
!
  CALL ALCOMP_Z(HSL, NHSL, BASL, NBASL, BNSL, NBNSL, MATCH)
  IF(.NOT. MATCH) GO TO 700
!
!      WRITE(LUTOUT,
!     &   '(/'' MATCH FOUND AT ENTRY SEQUENCE NUMBER '', I6)')LESEQN
!
!------------------------------------------------------------------------------
!
!      BEGIN SUBROUTINE SWITCH TO SELECT CORRECT SUBROUTINE TO CALL
!      IN ORDER TO EVALUATE THIS ENTRY, BASED ON THE EVALUATION
!      FUNCTION LABEL FROM THE ENTRY.  THIS LABEL IS STORED AS THE
!      LAST BOOLEAN LABEL.
!
!------------------------------------------------------------------------------
!
  IF(BL(NBL) .EQ. '#MEWE') THEN
     WRITE(LUTOUT,&
          '(/'' MEWE SPECTRAL LINE EXCITATION RATE COEFF. (cm3/s)'')' )
     WRITE(LUTOUT,&
          '(/'' AS FUNCTION OF ELECTRON TEMPERATURE (keV)'')')
!
!--------------  RETREIVE NUMERIC COEFFICIENTS
!
     CALL ALRECF_Z(CF, NCF, NCFMX, ERRMSG)
     IF(NCF .NE. 8 .AND. ERRMSG .EQ. ' ')&
          ERRMSG = 'INCORRECT NUMBER OF COEFFICIENTS'
     IF (ERRMSG .NE.  ' ') THEN
        WRITE(LUTOUT, '(/1X,A)') ERRMSG
        RETURN
     ENDIF
!
!
!------
!
  ELSE IF(BL(NBL) .EQ. '#BELI') THEN
     WRITE(LUTOUT,&
          '(/'' ELECTRON IMPACT IONIZATION CROSS SECTION cm[2]'')' )
     WRITE(LUTOUT,&
          '(/'' AS FUNCTION OF ELECTRON ENERGY (eV)'')')
!
!--------------  RETREIVE NUMERIC COEFFICIENTS
!
     CALL ALRECF_Z(CF, NCF, NCFMX, ERRMSG)
     IF(NCF.EQ.0) THEN
        WRITE(LUTOUT,&
             '(/'' NO FITTING PARAMETERS   EV FUNCTION TERMINATED'')' )
        RETURN
     ENDIF
     IF(ERRMSG .EQ. ' ') THEN
        CONTINUE
     ELSE
        WRITE(LUTOUT, '(/1X,A)') ERRMSG
        RETURN
     ENDIF
!
!------
!
  ELSE IF(BL(NBL) .EQ. '#CHEB') THEN
!        WRITE(LUTOUT,
!     &       '(/'' ORNL:CFADC CHEBYSHEV POLYNOMIAL FIT'')' )
!
     CALL ALRECF_Z(CF, NCF, NCFMX, ERRMSG)
!
     IF(NCF .NE. 11 .AND. ERRMSG .EQ. ' ')&
          ERRMSG = 'INCORRECT NUMBER OF COEFFICIENTS'
     IF(ERRMSG .EQ. ' ') THEN
        CONTINUE
     ELSE
        WRITE(LUTOUT, '(/1X,A)') ERRMSG
        RETURN
     ENDIF
!
!------
!
  ELSE IF(BL(NBL) .EQ. '#KINGEX') THEN
     WRITE(LUTOUT,&
          '(/'' POLYNOMIAL FIT OF EXCITATION RATE COEFFICIENT (cm3/s)'')')
     WRITE(LUTOUT,&
          '(/'' AS FUNCTION OF ELECTRON TEMPERATURE (eV)'')')
!
     CALL ALRECF_Z(CF, NCF, NCFMX, ERRMSG)
     IF(ERRMSG .EQ. ' ') THEN
        CONTINUE
     ELSE
        WRITE(LUTOUT, '(/1X,A)') ERRMSG
        RETURN
     ENDIF
!
!------
!
!------
!
  ELSE IF(BL(NBL) .EQ. '#JAN1') THEN
     WRITE(LUTOUT,&
          '(/'' LOG(N) POLYNOMIAL FIT '')' )
!
     CALL ALRECF_Z(CF, NCF, NCFMX, ERRMSG)
     IF(NCF .NE. 11 .AND. ERRMSG .EQ. ' ')&
          ERRMSG = 'INCORRECT NUMBER OF COEFFICIENTS'
     IF(ERRMSG .EQ. ' ') THEN
        CONTINUE
     ELSE
        WRITE(LUTOUT, '(/1X,A)') ERRMSG
        RETURN
     ENDIF
!
!------
!
!------
!
!      OTHER EVALUATION LABELS GO HERE IN STRUCTURE OF THE FORM:
!
!      ELSE IF(BL(NBL) .EQ. '#{EVALUATION LABEL}') THEN
!             CALL {APPROPRIATE SUBROUTINE TO DO INTERACTIVE
!                    EVALUATION}
!
!      FOR 1-D DATA TYPES WITH SUBROUTINES IN PROPER FORM, ALTB1D CAN
!      BE USED TO CALL THE EVALUATION SUBROUTINE DIRECTLY BY NAME, WITH NO
!      NO SEPARATE ROUTINE NEEDED FOR ALEV TO INTERACT WITH THE
!      TERMINAL ETC.
!
!      IF ALTB1D CANNOT BE USED, THE TYPICAL STRUCTURE OF SUBROUTINE
!      CALLED BY ALEV FOR INTERACTIVE EVALUATION OF A DATA ENTRY CON
!      CONTAINS:
!
!          {CODING TO CONVERT COEFFICIENT FIELD AS REQUIRED}
!          {CODING TO OBTAIN REQUIRED INDEPENDENT VARIABLE(S) FOR THIS
!           DATA TYPE}
!          {CODING TO CALL EVALUATION FUNCTION SUBROUTINE}
!          {CODING TO OUTPUT RESULTING DATA POINT VALUE(S)}
!          {LOOP BACK FOR MORE EVALUATIONS OR RETURN}
!
!------
!
!          CATCH-ALL FOR ENTRIES WITH EVALUATION LABELS NOT CURRENTLY IN
!          THIS VERSION OF ALADDIN
!
  ELSE
     WRITE(LUTOUT,&
          '(/'' ALAD_FIND_Z:  EVALUATION CURRENTLY UNAVAILABLE'')')
     IER=1
     RETURN
!
!------
!
  ENDIF
!
!  MADE IT!  INFO TO EVALUATE CROSS SECTION HAS BEEN LOADED
!  DMC 1 JUN 1990 -- BASED ON ALEV.FOR
!
!  EVALUATION FUNCTIONS REQUIRING ADDITIONAL USER INPUT WERE
!  CENSORED OUT OF THIS CODE
!
!  MODIFIED:
!      6/13/00 PCR - MINOR CHANGES
!
  IER=0
!
  RETURN
!
END SUBROUTINE ALAD_FIND_Z
!
!------------------------------------------------------------------------------
! 30Jun2000 fgtok -s r8_precision.sub "r8con.csh conversion"
