FTN7X,S,Q,L
$CDS OFF
C Interface routines for HP1000 : SLIBHP.FTN
C
C   Copyright (C) 1989, California Institute of Technology
C   All rights reserved.  U. S. Government Sponsorship under
C   NASA Contract NAS7-918 is acknowledged.
C
C   Herbert M. Pickett, 20 Mar 1989
C   OPENLU modified,    30 Oct 1991
C
C installation notes:
C   uncomment initial lines for each program file as well as all
C   lines which contain the characters EMA
C   command line arguments implemented, hot key is BR
C
      SUBROUTINE CHTIME(STR)
C
C     SUBROUTINE TO RETURN 30 CHARACTERS WITH THE TIME AND DATE
C
      CHARACTER*(*)STR
      INTEGER*4 TIMENOW
      CALL DAYTIME(TIMENOW(),STR)
      RETURN
      END
      INTEGER FUNCTION NDBLE(INUM)
      INTEGER INUM
C     .. number of REAL*8 words for INUM integer*2 words
      NDBLE= (INUM+3)/4
      RETURN
      END
      SUBROUTINE FILGET(CFIL,CEXT,NFILE)
C
C     Get NFILE names with extent given in CEXT
C     Put names in CFIL
C
C     gets file names from command line or interactively
C     subroutine RCPAR(I,FIL) gets the I-th command line argument
C                             and returns it in string file
C
      CHARACTER*(*) CFIL(*),CEXT(*)
      INTEGER NFILE
      CHARACTER EXT*4,EXTQ*4,FIL*62,EXTDOT*1
C     setup standard I/O units
      CALL FSYSU(1,1)
C     define character which delimits EXT field
      EXTDOT='.'
C  get first parameter
      CALL RCPAR(1,FIL)
      IFLG=INDEX(FIL,' ')-1
      IF(IFLG.LT.0) IFLG=0
      NARG=NFILE+1
      IF(IFLG.EQ.0) THEN
          WRITE(*,*) ' Enter file name '
          READ(*,100) FIL
      ENDIF
      NLEN=INDEX(FIL,EXTDOT)
      IF(NLEN.EQ.0) THEN
C         extdot not found
          NLEN=INDEX(FIL,' ')
          IF(NLEN.LE.1) STOP
          FIL(NLEN:NLEN)=EXTDOT
      ENDIF
C     set up defaults
      DO 10 J=1,NFILE
 10       CFIL(J)=FIL(1:NLEN)//CEXT(J)
C     find non default values
      DO 20 IARG=2,NARG
          IF(IFLG.EQ.0) THEN
              WRITE(*,*) ' Enter file name '
              READ(*,100) FIL
          ELSE
              CALL RCPAR(IARG,FIL)
          ENDIF
          K=INDEX(FIL,EXTDOT)
          IF(K.EQ.0) GO TO  50
          EXT=FIL(K+1:)
          DO 30 J=1,NFILE
              EXTQ=CEXT(J)
              DO 40 K=1,3
                  IEXT =ICHAR( EXT(K:K))
                  IEXTQ=ICHAR(EXTQ(K:K))
C                 compare upper and lower case
                  IF(IEXT.NE.IEXTQ.AND.IEXT+32.NE.IEXTQ) GO TO 30
 40               CONTINUE
              CFIL(J)=FIL
              GO TO 20
 30           CONTINUE
          IF(EXT.EQ.'LOG ') THEN
              OPEN(1,FILE=FIL,STATUS='UNKNOWN',USE='NON')
          ENDIF
 20       CONTINUE
 50   WRITE(*,'(1X,A)') (CFIL(J),J=1,NFILE)
      RETURN
 100  FORMAT(A)
      END
      SUBROUTINE OPENLU(LU,FNAME,CHSW)
C     fancy file opening and closing
      INTEGER LU,LUORG,LUIN,IREC,IOCHK      
      PARAMETER (LUORG=10)
      CHARACTER*8 LULIST
      CHARACTER*(*) FNAME,CHSW
      CHARACTER*64  FIL
      SAVE LULIST
      DATA LULIST /'CCCCCCCC'/
      LUIN=LU
      IF(CHSW(1:1).EQ.'C') THEN
          LU=LU-LUORG
          IF(LU.GT.0.AND.LU.LE.LEN(LULIST)) THEN
              IF(LULIST(LU:LU).EQ.'R') THEN
                  REWIND (LUIN)
              ELSE IF(LULIST(LU:LU).EQ.'W') THEN
                  ENDFILE (LUIN)
              ENDIF
              CLOSE(LUIN,ERR=50)
              LULIST(LU:LU)='C'
          ENDIF
      ELSE
          LU=INDEX(LULIST,'C')
          IF(LU.EQ.0) THEN
              WRITE(*,*) ' Too many open files'
              STOP
          ENDIF
          LULIST(LU:LU)=CHSW(1:1) 
          LU=LU+LUORG
          IF(CHSW.EQ.'R') THEN
            OPEN(LU,FILE=FNAME,ERR=99,STATUS='OLD',IOSTAT=IOCHK)
          ELSE IF(CHSW.EQ.'RL') THEN
            OPEN(LU,FILE=FNAME,ERR=80,STATUS='OLD')
          ELSE IF(CHSW.EQ.'RB') THEN
            OPEN(LU,FILE=FNAME,ERR=85,STATUS='OLD',
     :           FORM='UNFORMATTED')
          ELSE IF(CHSW.EQ.'W') THEN
            OPEN(LU,FILE=FNAME,ERR=99,STATUS='UNKNOWN',IOSTAT=IOCHK)
          ELSE IF(CHSW.EQ.'WP') THEN
C           allow others to read my partially written file
            OPEN(LU,FILE=FNAME,ERR=99,STATUS='UNKNOWN',USE='NON'
     :          ,IOSTAT=IOCHK)
          ELSE IF(CHSW.EQ.'WB') THEN
            OPEN(LU,FILE=FNAME,ERR=85,STATUS='UNKNOWN',
     :           FORM='UNFORMATTED')
          ELSE IF(CHSW.EQ.'S') THEN
            IREC=LUIN*8
            CALL LGBUF(LBUF,LBUFSIZE)
            OPEN(LU,STATUS='SCRATCH',ACCESS='DIRECT',RECL=IREC,ERR=99
     :        ,IOSTAT=IOCHK)
          ELSE
            WRITE(*,*) ' OPENLU unknown option =',CHSW
            STOP
         ENDIF
      ENDIF
  50  RETURN
C    try to open from default directory for parameter labels
  80  FIL='/SPECTRA/'//FNAME
      OPEN(LU,FILE=FIL,ERR=90,STATUS='OLD')
      RETURN
  85  LU=0
      RETURN
  90  WRITE(*,*) ' Trouble opening ',FIL
      RETURN
  99  WRITE(*,*) ' Trouble opening ',FNAME
      WRITE(*,*) ' IOSTAT = ',IOCHK
      STOP
      END
      INTEGER FUNCTION RQEXIT(IVAL)
      LOGICAL IFBRK
      IF(IFBRK()) THEN
          RQEXIT=1
      ELSE
          RQEXIT=0
      ENDIF
      RETURN
      END
      SUBROUTINE BRKQR
      RETURN
      END


