C----------------------------------------------------------------------
	SUBROUTINE GETFILNUM(file_list,num_files,max_file,min_file
	1	,max_lbl,getinp,string)
C----------------------------------------------------------------------
C   Subroutine to enter get file numbers from parameter list 
C	
C	file_list	list of file numbers (output)
C	num_files	number of files to be read
C	max_file	maximum number of files to be processed
C	min_file	minimum number of files needed to be processed
C	max_lbl		maximum number of labels to be processed
C	getinp		if GETINP is .true. prompt for file numbers
C	string		string to use for prompt for file(s)
C----------------------------------------------------------------------
	include 'shared$common'
	LOGICAL getinp
	CHARACTER*(*) string

	INTEGER LEN,file_list(10),num_files,inum(10)
	INTEGER STATUS,CLI$GET_VALUE
	CHARACTER*20 PARM
	CHARACTER*6 labels(8)/'file_1','file_2','file_3','file_4','file_5',
	1	'file_6','file_7','file_8'/

	num_files = 0
	max_files = min(max_file,10)
	max_label = min(max_lbl,8)
	
	do i=1,10
	  file_list(i) = 0
	enddo

	do i=1,max_label 
1	  STATUS = CLI$GET_VALUE(labels(i),parm,len)
C is there a valid parameter here?
	  if (status .and. len .gt. 0) then 
C Convert character string to a number
	    read(unit=parm(1:len),fmt='(I)',err=1) num
C is it a valid number?
	    if (num .gt. 0 .and. num .le. MFILE) then 
	      num_files = num_files + 1
	      file_list(num_files) = num
	    else
	      WRITE(LO,44) num,MFILE
44	      FORMAT (' FILE NUMBER 'I2' IS OUT OF THE RANGE 1 - 'I2,
	1 	2X,'IGNORING THIS FILE ENTRY.')
	      call PS_PAUSE(PROMPT)
	    endif
	    if (num_files .ge. max_files) return
	    goto 1
	  endif
	enddo
	IF (num_files .gt. 0) return
C if there is only one file and only one is needed
	if (mfile .eq. 1 .and. min_file .eq. 1) then
	  num_files = 1
	  file_list(num_files) = 1
	  RETURN
	endif
	IF (.not. getinp) return
C----------------------------------------------------------------------
100	IF (max_file .gt. 1 .and. num_files .eq. 0) then
	  Write (lo,'(a,i2,3A)') '$Enter numbers of up to ',max_file,
	1	' files ',string(:lench(string)),': '
	  read (li,'(10i4)',err=430,end=999) inum
	  J = 0
	  do i=num_files + 1,max_file
	    J = J + 1
	    IF (inum(J) .eq. 0 .and. j .eq. 1) goto 420
	    IF (inum(J) .eq. 0) goto 110
	    IF (inum(J) .lt. 0 .or. inum(j) .gt. MFILE) goto 410
	    num_files = num_files + 1
	    file_list(num_files) = inum(j)
	  ENDDO
	ELSEIF (min_file .eq. 1 .and. max_file .eq. 1) then
	  Write (lo,'(3A)') '$Enter number of file ',
	1	string(:lench(string)),' (one file only): '
	  read (li,'(10i4)',err=430,end=999) inum
	  IF (inum(1) .lt. 0 .or. inum(1) .gt. MFILE) goto 410
	  IF (inum(1) .eq. 0 .or. inum(2) .gt. ne0) THEN
	    write (lo,'(/A,/)') ' *** one file number is required here ***'
	    goto 100
	  ENDIF
	  num_files = 1
	  file_list(num_files) = inum(1)
	ENDIF
	IF (num_files .ge. max_file) return
110	IF (num_files .lt. min_file) THEN
	  write (lo,'(i3,A,i2,a,i2,a)') num_files,
	1	' files have been entered. At least ',min_file-num_files,
	1	' and up to ',max_file-num_files,' files are needed'
	  Write (lo,'(3A)') '$Enter number(s) of additional file(s) ',
	1	string(:lench(string)),': '
	ELSE
	  write (lo,'(i3,A,i2,a)') num_files,
	1	' files have been entered. Up to ',
	1	max_file-num_files,' more are allowed (none are required)'
	  Write (lo,'(3A)') '$Enter number(s) of additional file(s) ',
	1	string(:lench(string)),' (optional): '
	ENDIF
	read (li,'(10i4)',err=430,end=999) inum
	J = 0
	do i=num_files + 1,max_file
	    J = J + 1
	    IF (inum(J) .eq. 0 .and. j .eq. 1 .and. 
	1	num_files .lt. min_file) goto 420
	    IF (inum(J) .eq. 0 .and. j .eq. 1) return
	    IF (inum(J) .eq. 0) goto 110
	    IF (inum(J) .lt. 0 .or. inum(j) .gt. MFILE) goto 410
	    num_files = num_files + 1
	    file_list(num_files) = inum(j)
	ENDDO
	goto 100
C----------------------------------------------------------------------
420	write (lo,'(/A,i2,a,i2,a/)') 
	1	' *** at least ',min_file,' up to ',max_file,
	1	' files are required for input here ***'
	goto 100
410	write (lo,'(/A,i5,a/)')  ' *** File number ',inum(j),
	1	' is out of range ***'
430	write (lo,'(/A,i2,a,i2,a/)') 
	1	' Enter from ',min_file,' to ',max_file,
	1	' file(s) separated by commas or by blank lines'
	write (lo,'(A,i2)') ' Valid file numbers are from 1 to ',MFILE
	goto 100
999	return
	end
