C Copyright(c) 1997, Space Science and Engineering Center, UW-Madison
C Refer to "McIDAS Software Acquisition and Distribution Policies"
C in the file  mcidas/data/license.txt

C *** $Id: mdkeys.f,v 1.10 1997/10/10 20:18:21 dglo Exp $ ***

C MDKEYS  BARRET 1181  RETURN MD-VALS/UNITS/LOCATIONS OF KEYS IN MD FILE
C $ FUNCTION MDKEYS(MDNO, NLIST, LIST, SCALES, UNITS, LOCS)  (GAD)
C $ RETURN MD-VALUES/UNITS/LOCATIONS OF KEYS IN MD FILE. FN VAL IS #
C $   OF KEYS FOR WHICH INFO IS RETURNED, IF NLIST .GT. 0.  IF FN VAL
C $   .LE. NLIST, 1 OR MORE KEYS WERE NOT FOUND IN SCHEMA. IF NLIST = -1
C $   FN VAL IS TOTAL # OF KEYS IN SCHEMA (.LE. 400). FN VAL IS -1 IF
C $   ERROR (IE, FILE DOES NOT EXIST).
C $ MDNO = (I) INPUT  MD FILE #
C $ NLIST = (I) INPUT  -1 TO RETRIEVE ALL KEYS IN SCHEMA, OR ELSE THE
C $   # OF KEYS IN THE LIST
C $ LIST = (I) INPUT  ARRAY OF KEYS, 1 PER WORD (UNLESS NLIST = -1)
C $ SCALES = (I) INPUT  ARRAY OF WORDS TO RECEIVE SCALE FACTORS
C $ UNITS = (I) INPUT  ARRAY OF WORDS TO RECEIVE KEY UNITS (A4)
C $ LOCS = (I) INPUT  ARRAY OF WORDS TO RECEIVE SUBSCRIPTS WITHIN A
C $   COMPLETE (ROW HEADER + COL HEADER + DATA RECORD) MD FILE RECORD
C $   WHERE THE FIELDS SPECIFIED BY THE KEYS ARE TO BE FOUND (EACH
C $   SUCH FIELD IS EXACTLY 1 WORD LONG).
C $$ MDKEYS = MD, FILE
C
C-----WHEN NLIST IS -1 ARRAYS SHOULD BE AT LEAST 400 WORDS IN ORDER
C-----   TO CONTAIN MAXIMUM POSSIBLE SCHEMA.
C
C-----SINCE THE MD FILE STRUCTURE PERMITS MULTIPLE FIELDS WITH THE
C-----   SAME NAME, MDKEYS IS CAREFUL TO RETURN THE VALUES FOR THE 1ST
C-----   KEY IN THE SCHEMA FOR THE 1ST OCCURRENCE OF THAT KEY IN 'LIST',
C-----   THE VALUES FOR THE 2ND KEY IN THE SCHEMA FOR THE 2ND
C-----   OCCURRENCE, AND SO ON.

      FUNCTION MDKEYS(MDNO,NLIST,LIST,SCALES,UNITS,LOCS)
      IMPLICIT INTEGER (A-Z)
      DIMENSION LIST(*)
      DIMENSION SCALES(*)
      DIMENSION UNITS(*)
      DIMENSION LOCS(*)
      INTEGER BUF(402)
      INTEGER MDHD(64)
      INTEGER FNAM(2)
      CHARACTER*12 CNAME
      INTEGER TEXTID(8)
C
C
C-----LAYOUT OF MD FILE HEADER
C        FILE HEADER IS FIRST 64 WORD OF MD FILE
C        VARIABLES ARE SHOWN EQUIVALENCED TO 64-WORD ARRAY 'MDHD'
C
C
C
C-----SNAME,SVSN,SDATE GIVE THE SCHEMA NAME, VERSION #, & DATE
      EQUIVALENCE (SNAME,MDHD(1)),(SVSN,MDHD(2)),(SDATE,MDHD(3))
C-----NR,NC ARE THE # OF ROWS & COLUMNS IN THIS FILE
      EQUIVALENCE (NR,MDHD(4)),(NC,MDHD(5))
C-----NKEYS IS THE TOTAL # OF RECORD KEYS (= RECORD LENGTH, IN WORDS)
C-----NRKEYS,NCKEYS,NDKEYS GIVE THE # OF KEYS IN THE ROW HEADER, COL
C-----   HEADER, AND DATA PORTION, RESP. SUM MUST BE = NKEYS
      EQUIVALENCE (NKEYS,MDHD(6)),(NRKEYS,MDHD(7)),
     &   (NCKEYS,MDHD(8)),(NDKEYS,MDHD(9))
C-----CPOS,DPOS GIVES (1-BASED) POS IN RECORD OF COL HDR, DATA PORTION
      EQUIVALENCE (CPOS,MDHD(10)),(DPOS,MDHD(11))
C-----NREPS,REPSIZ,REPPOS GIVES # OF REPEAT GROUPS (NULL CASE IS 1),
C-----   SIZE OF REP. GP (NULL CASE =NDKEYS) AND START OF GP (NULL=DPOS)
      EQUIVALENCE (NREPS,MDHD(12)),(REPSIZ,MDHD(13)),(REPPOS,MDHD(14))
C-----MISSING DATA CODE (BY 7/82 CONVENTION = Z80808080)
      EQUIVALENCE (MDATA,MDHD(15))
C-----ID,TEXTID (8-WORD ARRAY) ARE INTEGER AND TEXT ID'S FOR FILE
      EQUIVALENCE (ID,MDHD(16)),(TEXTID,MDHD(17))
C-----CPROJ,CDATE,COWNER ARE CREATOR'S PROJECT,CREATE DATE, & TERMINAL
      EQUIVALENCE (CPROJ,MDHD(25)),(CDATE,MDHD(26)),(COWNER,MDHD(27))
C-----0-BASED WORD OFFSETS INTO MD FILE WHERE CAN BE FOUND THE FIRST
C-----   WORD OF THE FIRST: ROW HEADER, COL HEADER, DATA PORTION,
C-----   END-OF-MDFILE-DATA (EVERYTHING BEYOND HERE AVAILABLE TO USER),
C-----   400-WORD USER RECORD,KEYNAME LIST, KEYSCALE LIST, KEYUNITS LIST
      EQUIVALENCE (FRHOF,MDHD(28)),(FCHOF,MDHD(29)),(FDATOF,MDHD(30))
      EQUIVALENCE (FENDOF,MDHD(31)),(FUSROF,MDHD(32))
      EQUIVALENCE (FNAMOF,MDHD(33)),(FSCAOF,MDHD(34)),(FUNIOF,MDHD(35))
C-----REMAINING 29 WORDS UNUSED
C
C
C
C
      EQUIVALENCE (CF,MDHD(64)),(FNAM,MDHD(62))
      EQUIVALENCE (CRH,MDHD(58)),(CCH,MDHD(59)),(CR,MDHD(60)),
     &  (CC,MDHD(61))
      DATA MAXFLD/400/
C
C
C
C
      IF (MDINFO(MDNO,MDHD).NE.0) GOTO 91
C
C--- Check for flipped bytes
C
      FLIP = ISMFMT(MDNO)

      NFLD=MIN0(MAXFLD,NKEYS)

      CNAME=' '
      CALL MOVWC(FNAM,CNAME(1:8))

      IF (NLIST.LT.0) THEN

         III=LWI(CNAME,FNAMOF,NFLD,LIST)

         III=LWI(CNAME,FSCAOF,NFLD,SCALES)
         IF (FLIP .NE. 0) CALL FBYTE4(SCALES, NFLD)

         III=LWI(CNAME,FUNIOF,NFLD,UNITS)

         DO 6 J=1,NFLD
 6       LOCS(J)=J
         MDKEYS=NFLD
         RETURN

      ENDIF

      III=LWI(CNAME,FNAMOF,NFLD,BUF)

      BUF(NFLD+1)=LIT('ROW ')
      BUF(NFLD+2)=LIT('COL ')
C
C-----NOW SEARCH THRU THE KEYS IN BUF REPEATEDLY UNTIL
C-----   ALL THE KEYS IN THE LIST ARE SATISFIED.
C
      DO 9 J=1,NLIST
 9    LOCS(J)=-1
      MDKEYS=0
      DO 10 I=1,NFLD+2
         IKEY=BUF(I)
         DO 11 J=1,NLIST
            IF (LOCS(J).GE.0) GOTO 11
            IF (IKEY.NE.LIST(J)) GOTO 11
C              CHECK FOR KEYS 'ROW','COL'.SET LOCS(J) TO -1 BUT NO ERROR
               IF (I.LE.NFLD) LOCS(J)=I
               MDKEYS=MDKEYS+1
               GOTO 10
 11      CONTINUE
 10   CONTINUE
C
C-----DONE SEARCHING FOR KEYS; NOW PICK UP CORRESPONDING
C-----   SCALES/UNITS INFO
C
 12   CONTINUE

      III=LWI(CNAME,FSCAOF,NFLD,BUF)
      IF (FLIP .NE. 0) CALL FBYTE4(BUF, NFLD)

      DO 14 J=1,NLIST
         K=LOCS(J)
         IF (K.GT.0) SCALES(J)=BUF(K)
 14   CONTINUE
      III=LWI(CNAME,FUNIOF,NFLD,BUF)
      DO 16 J=1,NLIST
         K=LOCS(J)
         IF (K.GT.0) UNITS(J)=BUF(K)
 16   CONTINUE
      GOTO 99
 91   MDKEYS=-1
 99   RETURN
      END
