SUBROUTINE PIKLVL(LVLS1, LVLS2, CCSTOR, IDFLT, ISDIR)
! PIKLVL CHOOSES THE LEVEL STRUCTURE USED IN NUMBERING GRAPH
! LVLS1- ON INPUT CONTAINS FORWARD LEVELING INFO
! LVLS2- ON INPUT CONTAINS REVERSE LEVELING INFO
! ON OUTPUT THE FINAL LEVEL STRUCTURE CHOSEN
! CCSTOR- ON INPUT CONTAINS CONNECTED COMPONENT INFO
! IDFLT- ON INPUT =1 IF WDTH LVLS1 <= WDTH LVLS2, =2 OTHERWISE
! NHIGH KEEPS TRACK OF LEVEL WIDTHS FOR HIGH NUMBERING
! NLOW- KEEPS TRACK OF LEVEL WIDTHS FOR LOW NUMBERING
! NACUM- KEEPS TRACK OF LEVEL WIDTHS FOR CHOSEN LEVEL STRUCTURE
! XCC- NUMBER OF CONNECTED COMPONENTS
! SIZEG(I)- SIZE OF ITH CONNECTED COMPONENT
! STPT(I)- INDEX INTO CCSTORE OF 1ST NODE IN ITH CON COMPT
! ISDIR- FLAG WHICH INDICATES WHICH WAY THE LARGEST CONNECTED
! COMPONENT FELL. =+1 IF LOW AND -1 IF HIGH
INTEGER CCSTOR, ENDC
! COMMON /GRA/ N, IDPTH, IDEG
! IT IS ASSUMED THAT THE GRAPH HAS AT MOST 50 COMPONENTS AND
! THAT THERE ARE AT MOST 100 LEVELS.
! COMMON /LVLW/ NHIGH(100), NLOW(100), NACUM(100)
! COMMON /CC/ XCC, SIZEG(50), STPT(50)
DIMENSION LVLS1(N), LVLS2(N), CCSTOR(N)
INTEGER :: SZ
! FOR EACH CONNECTED COMPONENT DO
DO 80 I=1,XCC
J = STPT(I)
ENDC= SIZEG(I) + J - 1
! SET NHIGH AND NLOW EQUAL TO NACUM
!-----------------------------------------------------------
SZ=SIZE(NHIGH)
IF(SZ < IDPTH) THEN
WRITE(*,*) 'GPS_PIKLVL: on the fly reallocation of NHIGH'
CALL psb_realloc(idpth,nhigh,info) !CALL REALLOC(NHIGH,SZ,IDPTH)
END IF
SZ=SIZE(NLOW)
IF(SZ < IDPTH) THEN
WRITE(*,*) 'GPS_PIKLVL: on the fly reallocation of NLOW'
CALL psb_realloc(idpth,nlow,info) !CALL REALLOC(NLOW,SZ,IDPTH)
END IF
!-----------------------------------------------------------
DO 10 K=1,IDPTH
NHIGH(K) = NACUM(K)
NLOW(K) = NACUM(K)
10 END DO
! UPDATE NHIGH AND NLOW FOR EACH NODE IN CONNECTED COMPONENT
DO 20 K=J,ENDC
INODE = CCSTOR(K)
LVLNH = LVLS1(INODE)
NHIGH(LVLNH) = NHIGH(LVLNH) + 1
LVLNL = LVLS2(INODE)
NLOW(LVLNL) = NLOW(LVLNL) + 1
20 END DO
MAX1 = 0
MAX2 = 0
! SET MAX1=LARGEST NEW NUMBER IN NHIGH
! SET MAX2=LARGEST NEW NUMBER IN NLOW
DO 30 K=1,IDPTH
IF (2*NACUM(K) == NLOW(K)+NHIGH(K)) CYCLE
IF (NHIGH(K)>MAX1) MAX1 = NHIGH(K)
IF (NLOW(K)>MAX2) MAX2 = NLOW(K)
30 END DO
! SET IT= NUMBER OF LEVEL STRUCTURE TO BE USED
IT = 1
IF (MAX1>MAX2) IT = 2
IF (MAX1 == MAX2) IT = IDFLT
IF (IT == 2) GO TO 60
IF (I == 1) ISDIR = -1
! COPY LVLS1 INTO LVLS2 FOR EACH NODE IN CONNECTED COMPONENT
DO 40 K=J,ENDC
INODE = CCSTOR(K)
LVLS2(INODE) = LVLS1(INODE)
40 END DO
! UPDATE NACUM TO BE THE SAME AS NHIGH
DO 50 K=1,IDPTH
NACUM(K) = NHIGH(K)
50 END DO
CYCLE
! UPDATE NACUM TO BE THE SAME AS NLOW
60 DO 70 K=1,IDPTH
NACUM(K) = NLOW(K)
70 END DO
80 END DO
RETURN
END SUBROUTINE PIKLVL