	SUBROUTINE GET_FITS_INFO(LU)
C
C       This subroutine helps OPENFITS by reading through an opened file
C  to obtain the essential information about the file, and enters that
C  information into the FITSFACTS common blocks.  Currently, we allow a
C  maximum of 256 maps in a file.  The information for a particular map
C  is referenced by an index equal to the map number.  If more than one
C  map exists, then there will also be a primary header which can be
C  referenced with an index equal to 0.

C
C			Donald H. Gudehus, June 27, 2005
C
	INCLUDE 'fitsfacts.for'
C
	BYTE PHYSREC(3000)
	CHARACTER*3000 CHARPHYSREC
	EQUIVALENCE (PHYSREC,CHARPHYSREC)
	CHARACTER*1 C1_TEMP
	CHARACTER*80 KEYCARD
	LOGICAL EXTENSION_HEADER
C
	IBLKLEN=2880
C  Initialize COMMON block variables
	SIMPLE(LU)=.FALSE.
	EXTEND(LU)=0
	NUMRECORDS(LU)=0
	NUMHEADERS(LU)=0
	NUMMAPS(LU)=0
	DO I=0,MAPMAX
	  BITPIX(I,LU)=0
	  NAXIS(I,LU)=0
	  NAXIS1(I,LU)=0
	  NAXIS2(I,LU)=0
	  NAXIS3(I,LU)=1 !  Default in case keyword is not present
	  BZERO(I,LU)=0.0D0 !  Default in case keyword is not present
	  BSCALE(I,LU)=1.0D0 !  Default in case keyword is not present
	  COMPLX(I,LU)=.FALSE.
	  NUMKEYCARDS(I,LU)=0
	END DO
C  Other initializations
	NUMSIMPLE=0
	NUMXTENSION=0
	EXTENSION_HEADER=.FALSE.
	NUMBLANK=0
C  Read the header(s) of the input file in order to get the number of
C  maps and the number of columns, rows, and data type for each map.
	DO WHILE(.TRUE.)
	  KEYCARD(1:80)=' '
	  IBPTR=1
	  READ(LU,REC=NUMRECORDS(LU)+1,IOSTAT=ISTAT) (PHYSREC(I),I=1,IBLKLEN)
	  IF (ISTAT.NE.0) THEN
	    GO TO 9999
	  ELSE
	    NUMRECORDS(LU)=NUMRECORDS(LU)+1 !  Current valid record number
	  END IF
	  DO WHILE(KEYCARD(1:3).NE.'END'.AND.IBPTR.LE.2801)
	    KEYCARD=CHARPHYSREC(IBPTR:IBPTR+79)
C  Eliminate control characters
	    DO I=1,80
	      IF (ICHAR(KEYCARD(I:I)).LT.32) KEYCARD(I:I)=' '
	    END DO
1000	    FORMAT(' ',A)
	    IF (KEYCARD(1:6).EQ.'SIMPLE') THEN
C  Mandatory keyword for primary header with extension, or header for
C  file with single map.
	      EXTENSION_HEADER=.FALSE.
	      NUMSIMPLE=NUMSIMPLE+1
	      C1_TEMP=KEYCARD(30:30)
	      IF (C1_TEMP.EQ.'T') THEN
	        SIMPLE(LU)=.TRUE.
	      ELSE IF (C1_TEMP.EQ.'F') THEN
	        SIMPLE(LU)=.FALSE.
	      END IF
	      HEADERRECORDS(1,0,LU)=NUMRECORDS(LU)
	    END IF
	    IF (KEYCARD(1:8).EQ.'XTENSION') THEN
	      EXTENSION_HEADER=.TRUE.
	      NUMXTENSION=NUMXTENSION+1
	      HEADERRECORDS(1,NUMXTENSION,LU)=NUMRECORDS(LU)
	    END IF
	    IF (EXTENSION_HEADER) THEN
	      INDEX=NUMXTENSION
	    ELSE
	      INDEX=0
	    END IF
	    IF (KEYCARD(1:6).EQ.'BITPIX') THEN
C  Mandatory keyword for primary and extension headers
	      DECODE (20,1020,KEYCARD(11:30)) BITPIX(INDEX,LU)
1020	      FORMAT(I20)
	      IF (BITPIX(INDEX,LU).EQ.0)
	1     CALL ERROR(-3,'BITPIX not defined')
	    END IF
	    IF (KEYCARD(1:6).EQ.'NAXIS') THEN
C  Mandatory keyword for primary and extension headers
	      DECODE (20,1020,KEYCARD(11:30)) NAXIS(INDEX,LU)
	    END IF
	    IF (KEYCARD(1:6).EQ.'NAXIS1') THEN
C  Mandatory keyword for primary and extension headers
	      DECODE (20,1020,KEYCARD(11:30)) NAXIS1(INDEX,LU)
	    END IF
	    IF (KEYCARD(1:6).EQ.'NAXIS2') THEN
C  Mandatory keyword for primary and extension headers
	      DECODE (20,1020,KEYCARD(11:30)) NAXIS2(INDEX,LU)
	    END IF
	    IF (KEYCARD(1:6).EQ.'NAXIS3') THEN
C  Mandatory keyword for primary and extension headers
	      DECODE (20,1020,KEYCARD(11:30)) NAXIS3(INDEX,LU)
	    END IF
	    IF (KEYCARD(1:6).EQ.'EXTEND') THEN
	      C1_TEMP=KEYCARD(30:30)
	      IF (C1_TEMP.EQ.'T') THEN
	        EXTEND(LU)=1
C  One would think that if the EXTEND keyword is true, a map should follow the
C  primary header.  However, according to the FITS documentation, it may be
C  that EXTEND is true but there are no current extensions, just the single
C  map.
	      ELSE IF (C1_TEMP.EQ.'F') THEN
	        EXTEND(LU)=2
	      END IF
	    END IF
	    IF (KEYCARD(1:5).EQ.'BZERO'.OR.KEYCARD(1:6).EQ.'BSCALE') THEN
	      I=10
	      DO WHILE (KEYCARD(I:I).EQ.' ')
	        I=I+1
	      END DO
	      ISTART=I
	      DO WHILE (KEYCARD(I:I).NE.' '.AND.KEYCARD(I:I).NE.'/')
	        I=I+1
	      END DO
	      IEND=I-1
	      NCHARS=IEND-ISTART+1
	      IF (KEYCARD(1:5).EQ.'BZERO') THEN
	        DECODE (NCHARS,1100,KEYCARD(ISTART:IEND)) BZERO(INDEX,LU)
1100	        FORMAT(E24.14)
	      ELSE IF (KEYCARD(1:6).EQ.'BSCALE') THEN
	        DECODE (NCHARS,1100,KEYCARD(ISTART:IEND))
	1       BSCALE(INDEX,LU)
	      END IF
	    END IF
	    IF (KEYCARD(1:6).EQ.'CTYPE3') THEN
	      IF (KEYCARD(12:18).EQ.'COMPLEX') THEN
	        COMPLX(INDEX,LU)=.TRUE.
	        IF (NAXIS3(INDEX,LU).NE.2) THEN
	          CALL ERROR
	1         (-2,'Inconsistent keyword values for COMPLEX data')
	        END IF
	      END IF
	    END IF
	    IF (KEYCARD.EQ.' ') NUMBLANK=NUMBLANK+1
	    IF (KEYCARD(1:3).EQ.'END') THEN
	      HEADERRECORDS(2,INDEX,LU)=NUMRECORDS(LU)
	      NUMKEYCARDS(INDEX,LU)=(HEADERRECORDS(2,INDEX,LU)-
	1     HEADERRECORDS(1,INDEX,LU))*36+(IBPTR+79)/80
	    END IF
	    IBPTR=IBPTR+80
	  END DO
	END DO
9999	CONTINUE
	IF (NUMXTENSION.NE.0) THEN
	  NUMMAPS(LU)=NUMXTENSION
	ELSE
	  IF (NAXIS(0,LU).EQ.2.OR.NAXIS(0,LU).EQ.3)
	1 NUMMAPS(LU)=NUMSIMPLE
	END IF
	IF (NUMMAPS(LU).EQ.0) THEN
	  CALL ERROR(-2,'The file contained no maps')
	  CALL MESSAGE('Forcing number of maps to be 1')
	  NUMMAPS(LU)=1
	  NAXIS3(0,LU)=0 !  Flag for other routines
	END IF
	NUMHEADERS(LU)=NUMSIMPLE+NUMXTENSION
C  Cover the case of a single header with a single map.  The map
C  can be referenced by using an index equal to 1.
	IF (NUMMAPS(LU).EQ.1) THEN
	  BITPIX(1,LU)=BITPIX(0,LU)
	  NAXIS(1,LU)=NAXIS(0,LU)
	  NAXIS1(1,LU)=NAXIS1(0,LU)
	  NAXIS2(1,LU)=NAXIS2(0,LU)
	  NAXIS3(1,LU)=NAXIS3(0,LU)
	  BZERO(1,LU)=BZERO(0,LU)
	  BSCALE(1,LU)=BSCALE(0,LU)
	  HEADERRECORDS(1,1,LU)=HEADERRECORDS(1,0,LU)
	  HEADERRECORDS(2,1,LU)=HEADERRECORDS(2,0,LU)
	  NUMKEYCARDS(1,LU)=NUMKEYCARDS(0,LU)
	  COMPLX(1,LU)=COMPLX(0,LU)
	END IF
C  Compute the starting and ending records for each map
	DO I=1,NUMMAPS(LU)-1
	  MAPRECORDS(1,I,LU)=HEADERRECORDS(2,I,LU)+1
	  MAPRECORDS(2,I,LU)=HEADERRECORDS(1,1+I,LU)-1
	END DO
	MAPRECORDS(1,NUMMAPS(LU),LU)=HEADERRECORDS(2,NUMMAPS(LU),LU)+1
	MAPRECORDS(2,NUMMAPS(LU),LU)=NUMRECORDS(LU)
C
	RETURN
	END
