C   g77    convert.f -o convert.e
C   ftn    convert.f -o convert.e

      IMPLICIT NONE

      CHARACTER*4       FORM
      CHARACTER*5       TYPE
      CHARACTER*11      PICNM
      CHARACTER*11      PICDUN(50000)
      CHARACTER*17      XNAME
      CHARACTER*24      UTC
      CHARACTER*72      LIST
      CHARACTER*72      INFILE
      CHARACTER*72      OUTFILE
      CHARACTER*72      XLINE(50000)
      CHARACTER*256     LBL
      CHARACTER*2072    LINE

      INTEGER           DN(1024,1024)
      INTEGER           I, J, K, KMAX, KDUN 
      INTEGER*4         NC
      INTEGER*4         NS
      INTEGER           NPX
      INTEGER           NLN
      INTEGER           RECSIZE
      INTEGER           LBLSIZE
      INTEGER           SCL
      INTEGER           TABLE(0:255)
      LOGICAL           EX

      NS=1
      OPEN(UNIT=10,FILE='../CASSINI/DATA/TABLE.TXT', STATUS='OLD')
         READ(10,*) (TABLE(I), I=0,255)
      CLOSE(UNIT=10)

      DO K=1,50000
      DO I=1,72
        XLINE(K)(I:I)=' '
      ENDDO
      ENDDO

      KMAX=0
      KDUN=0
      INFILE='make_sumfiles.in'
      INQUIRE(FILE=INFILE, EXIST=EX)
      IF(EX) THEN
        OPEN(UNIT=60,FILE=INFILE,STATUS='OLD')
10        CONTINUE
          READ(60,FMT='(A72)',END=20) XLINE(KMAX+1)
          KMAX=KMAX+1
          KDUN=KDUN+1
          IF(XLINE(KMAX)(1:1).EQ.'#') THEN
            PICDUN(KDUN)=XLINE(KMAX)(2:12)
          ELSE
            PICDUN(KDUN)=XLINE(KMAX)(1:11)
          ENDIF
          GO TO 10
20        CONTINUE
        CLOSE(UNIT=60)
      ENDIF

      WRITE(6,*) 'INPUT LIST'
      READ(5,FMT='(A72)') LIST
      OPEN(UNIT=18, FILE='TUCK.TXT', STATUS='UNKNOWN')
      OPEN(UNIT=50,FILE=LIST,STATUS='OLD')
100     CONTINUE
        READ(50,FMT='(A17,4X,A24,24X,A5)',END=200) XNAME, UTC, TYPE
        PICNM=XNAME(1:11)
        DO K=1,KDUN
          IF(PICNM.EQ.PICDUN(K)) GO TO 100
        ENDDO
        NC=0
        IF(XNAME(1:1).EQ.'N') NC=1
        IF(XNAME(1:1).EQ.'W') NC=2
        OUTFILE='IMAGEFILES/'//PICNM//'.DAT'
        INFILE='NEW_IMAGES/'//XNAME
        INQUIRE(FILE=INFILE, EXIST=EX)
        IF(.NOT.EX) THEN
          INFILE='NEW_IMAGES/SMALL/'//XNAME
          INQUIRE(FILE=INFILE, EXIST=EX)
          IF(.NOT.EX) GO TO 100
        ENDIF

        OPEN(UNIT=10,FILE=INFILE,STATUS='OLD')
          READ(10,FMT='(A256)') LBL
        CLOSE(UNIT=10)

        DO I=1,200
          IF(LBL(I:I+7).EQ.'LBLSIZE=') THEN
            READ(LBL(I+8:I+18),*) LBLSIZE
          ENDIF
          IF(LBL(I:I+6).EQ.'FORMAT=') THEN
            FORM=LBL(I+8:I+11)
          ENDIF
          IF(LBL(I:I+7).EQ.'RECSIZE=') THEN
            READ(LBL(I+8:I+18),*) RECSIZE
          ENDIF
          IF(LBL(I:I+2).EQ.'NS=') THEN
            READ(LBL(I+3:I+18),*) NPX
          ENDIF
          IF(LBL(I:I+2).EQ.'NL=') THEN
            READ(LBL(I+3:I+18),*) NLN
          ENDIF
        ENDDO
        K=1+LBLSIZE/RECSIZE
        SCL=1024/NPX      

        KMAX=KMAX+1
        WRITE(XLINE(KMAX),FMT='(A12,4X,A24,3I5)') 
     .    PICNM//' ', UTC, NC, NS, SCL

        INQUIRE(FILE=OUTFILE, EXIST=EX)
        IF(EX) GO TO 100

        OPEN(UNIT=10,FILE=INFILE,ACCESS='DIRECT',
     .        RECL=RECSIZE, STATUS='OLD')
          DO J=1,NLN
            READ(10,REC=K+J) LINE(1:RECSIZE)
            DO I=1,NPX
              IF(TYPE.EQ.'12BIT') THEN
                DN(I,J)=256*ICHAR(LINE(24+2*I-1:24+2*I-1))
     .                 +ICHAR(LINE(24+2*I:24+2*I))
              ENDIF
              IF(TYPE.EQ.'8LSB ') THEN
                DN(I,J)=ICHAR(LINE(24+I:24+I))
              ENDIF
              IF(TYPE.EQ.'TABLE') THEN
                DN(I,J)=TABLE(ICHAR(LINE(24+I:24+I)))
              ENDIF
            ENDDO
          ENDDO
        CLOSE(UNIT=10)
        IF((TYPE.EQ.'8LSB ').AND.(.NOT.EX)) THEN
          WRITE(18,FMT='(A1)') 'p'
          WRITE(18,FMT='(A11)') PICNM
          WRITE(18,FMT='(A1)') '1'
        ENDIF

C        IF(TYPE.EQ.'8LSB ') THEN
C          DO J=1,NLN-1
C          DO I=1,NPX-1
C            DDN(1,I,J)=DN(I+1,J)-DN(I,J)
C            DDN(2,I,J)=DN(I,J+1)-DN(I,J)
C          ENDDO
C          ENDDO
C            DO I=1,NPX-1
C              DN(I+1,J)=DN(I,J)+DDN(I)
C            ENDDO
C          ENDDO
C        ENDIF

        IF((TYPE.EQ.'12BIT').OR.(TYPE.EQ.'TABLE')) THEN
          DO I=1,NPX
          DO J=2,NLN-1
          IF((DN(I,J).EQ.0).AND.(DN(I,J+1).NE.0).AND.
     .       (DN(I,J-1).NE.0)) THEN      
            DN(I,J)=(DN(I,J+1)+DN(I,J-1))/2
          ENDIF
          ENDDO
          ENDDO
        ENDIF

        DO I=1,NPX
          DN(I,1)=0
          DN(I,NLN)=0
        ENDDO
        DO J=1,NLN
          DN(1,J)=0
          DN(NPX,J)=0
        ENDDO
       
        OPEN(UNIT=10,FILE=OUTFILE,ACCESS='DIRECT',
     .        RECL=2*NPX, STATUS='UNKNOWN')
          DO J=1,NLN
            DO I=1,NPX
              K=DN(I,J)/256
              LINE(2*I-1:2*I-1)=CHAR(K)
              LINE(2*I:2*I)=CHAR(DN(I,J)-256*K)
            ENDDO
            WRITE(10,REC=J) LINE(1:2*NPX)
          ENDDO
        CLOSE(UNIT=10)

        GO TO 100
200     CONTINUE
        WRITE(18,FMT='(A1)') 'q'
      CLOSE(UNIT=50)
      CLOSE(UNIT=18)

      INFILE='make_sumfiles.in'
      OPEN(UNIT=60,FILE=INFILE,STATUS='UNKNOWN')
        DO K=1,KMAX
          WRITE(60,FMT='(A72)') XLINE(K)
        ENDDO
      CLOSE(UNIT=60)

      STOP
      END
      