C     g77 -O2 list.f /Users/rgaskell/toolkit/lib/spicelib.a -o list.e
C     ftn list.f /Users/rgaskell/toolkit/lib/spicelib.a -o list.e


      implicit none

      CHARACTER*(3)         FORMAT
      DOUBLE PRECISION      ET
      INTEGER               PREC

      real*8               diam, pxsiz, dilim, rpd, vdot, vnorm
      real*8               CLAT, CWLN, SLAT, SWLN, SZ(3), UZ(3),
     .                     LAT, LON, W(3), C(3), Z1, Z2	

      integer              i, j, k, n, q, slen
      integer*4            coverage(361,181)

      character*5          flt1
      character*5          flt2
      character*12         picnm
      character*13         xname
      character*17         imgnm
      character*24         utc
      character*24         target
      character*24         objnm
      character*29         index
      character*72         infile, outfile
      character*361        cline
      character*3057       line

      logical              ex
      CALL RESLUN(17)
      FORMAT = 'C'
      PREC   =  3
      CALL LDPOOL ('../CASSINI/DATA/naif0012.tls' )

      write(6,*) 'Input OBJNM'
      read(5,fmt='(a24)') OBJNM
      write(6,*) 'Input DILIM (px)'
      read(5,*) DILIM

      K=SLEN(OBJNM)
      OPEN(UNIT=16,FILE=OBJNM(1:K)//'.TXT',STATUS='UNKNOWN')

      do i=1,361
      do j=1,181
        coverage(i,j)=0
      enddo
      enddo
      q=0

      open(unit=17,file='./NEW_IMAGES/download.b',status='unknown')
C       Commenting out lots because now using cumindex.tab, not different tab files
C      DO N=1,99
C        write(index,fmt='(a23,i2,a4)')'../CASSINI/DATA/index20',n,'.tab'
C        write(index,fmt='(a23,i2,a4)')'../CASSINI/DATA/cumindex.tab'
C        if(n.lt.10) index(24:24)='0'
C        inquire(file=index,exist=ex)
C        IF(EX) THEN
          index='../CASSINI/DATA/cumindex.tab'
          open(unit=10,file=index,status='old')
10          continue
            read(10,fmt='(a3057)',END=100) line
            TARGET=LINE(1629:1652)
            FLT1=LINE(643:647)
            FLT2=LINE(651:655)
            DO K=1,24
            IF(ICHAR(TARGET(K:K)).GE.97) THEN
              TARGET(K:K)=CHAR(ICHAR(TARGET(K:K))-32)
            ENDIF
            ENDDO
            IF(OBJNM.NE.TARGET) GO TO 10
C            READ(LINE(1886:1896),*) PXSIZ
            READ(LINE(1898:1908),*) PXSIZ

            K=SLEN(OBJNM)
            IF(OBJNM(1:K).EQ.'MIMAS') DIAM=390/PXSIZ
            IF(OBJNM(1:K).EQ.'RHEA') DIAM=1530/PXSIZ
            IF(OBJNM(1:K).EQ.'ENCELADUS') DIAM=500/PXSIZ
            IF(OBJNM(1:K).EQ.'TETHYS') DIAM=1060/PXSIZ
            IF(OBJNM(1:K).EQ.'DIONE') DIAM=1120/PXSIZ
            IF(OBJNM(1:K).EQ.'HYPERION') DIAM=255/PXSIZ
            IF(OBJNM(1:K).EQ.'PHOEBE') DIAM=220/PXSIZ
            IF(OBJNM(1:K).EQ.'IAPETUS') DIAM=1460/PXSIZ
            IF(OBJNM(1:K).EQ.'PROMETHEUS') DIAM=86/PXSIZ
            IF(OBJNM(1:K).EQ.'PANDORA') DIAM=81/PXSIZ
            IF(OBJNM(1:K).EQ.'JANUS') DIAM=180/PXSIZ
            IF(OBJNM(1:K).EQ.'EPIMETHEUS') DIAM=116/PXSIZ
            IF(OBJNM(1:K).EQ.'CALYPSO') DIAM=21/PXSIZ
            IF(OBJNM(1:K).EQ.'TELESTO') DIAM=25/PXSIZ
            IF(OBJNM(1:K).EQ.'HELENE') DIAM=35/PXSIZ
            IF(DIAM.LT.DILIM) GO TO 10
C            READ(LINE(2517:2527),*) SLAT				;the format of the tab file changed
            READ(LINE(2529:2539),*) SLAT
C            READ(LINE(2529:2539),*) SWLN
            READ(LINE(2541:2551),*) SWLN
            SZ(1)= COS(SLAT*RPD())*COS(SWLN*RPD())
            SZ(2)=-COS(SLAT*RPD())*SIN(SWLN*RPD())
            SZ(3)= SIN(SLAT*RPD())
C            READ(LINE(2541:2551),*) CLAT
            READ(LINE(2553:2563),*) CLAT
C            READ(LINE(2553:2563),*) CWLN
            READ(LINE(2565:2575),*) CWLN
            UZ(1)= COS(CLAT*RPD())*COS(CWLN*RPD())
            UZ(2)=-COS(CLAT*RPD())*SIN(CWLN*RPD())
            UZ(3)= SIN(CLAT*RPD())
C            READ(LINE(2565:2575),*) LAT
            READ(LINE(2577:2587),*) LAT
C            READ(LINE(2577:2587),*) LON
            READ(LINE(2589:2599),*) LON
            C(1)= COS(LAT*RPD())*COS(LON*RPD())
            C(2)=-COS(LAT*RPD())*SIN(LON*RPD())
            C(3)= SIN(LAT*RPD())
            IMGNM=LINE(2:18)
            UTC=LINE(700:721)
            CALL UTC2ET(UTC,ET)
            CALL ET2UTC ( ET, FORMAT, PREC, UTC )
            WRITE(16,FMT='(A17,4X,A24,F10.0,I10,4X,A5,4X,2A5)') 
     .              IMGNM, UTC, DIAM, N, LINE(209:213), FLT1, FLT2
            OPEN(UNIT=20,FILE='PICTLIST.TXT',STATUS='OLD')
20            CONTINUE
              READ(20,FMT='(A13)') XNAME
              IF(XNAME(1:3).NE.'END') THEN
                PICNM=XNAME(2:13)
                I=SLEN(PICNM)
                IF(PICNM(1:I).EQ.IMGNM(1:I)) THEN
                  CLOSE(UNIT=20)
                  GO TO 30
                ENDIF
                GO TO 20
              ENDIF
            CLOSE(UNIT=20)
            WRITE(6,FMT='(A17)') IMGNM
            WRITE(17,FMT='(A150)') 
     .       'curl https://pdsimg.jpl.nasa.gov/data/cassini/'//
     .       'cassini_orbiter/coiss'//line(83:87)//'/'//
     .        line(27:74)//' -o "'//IMGNM//'"'
30          CONTINUE
            q=q+1

            DO I=1,361
            DO J=1,181
              LAT=91-J
              LON=I-1
              W(1)= COS(LAT*RPD())*COS(LON*RPD())
              W(2)= COS(LAT*RPD())*SIN(LON*RPD())
              W(3)= SIN(LAT*RPD())
              IF((VDOT(SZ,W).GT.0).AND.(VDOT(UZ,W).GT.0)) THEN
                CALL VSUB(W,C,W)
                IF(VNORM(W)*DIAM/2.LT.1300) THEN
                   COVERAGE(I,J)=COVERAGE(I,J)+1
                ENDIF
              ENDIF
            ENDDO
            ENDDO
            GO TO 10
100         CONTINUE
          CLOSE(UNIT=10)
C        ENDIF
C      ENDDO
      write(17,*)
      close(unit=17)

      Z1=0
      Z2=0
      DO I=1,361
      DO J=1,181
        LAT=91-J
        LON=I-1
        Z1=Z1+COS(LAT*RPD())
        IF(COVERAGE(I,J).GE.2) Z2=Z2+COS(LAT*RPD())
      ENDDO
      ENDDO

      CLOSE(UNIT=16)
      write(6,*) q, Z2/Z1

      open (unit=10, file='coverage.gray', access='direct', 
     .      recl=361, status='unknown')
        do j=1,181
          do i=1,361
            cline(i:i)=char(min(255,coverage(i,j)))
          enddo
          write(10,rec=j) cline(1:361)
        enddo
      close(unit=10)
      write(6,*) 
      write(6,*) 'coverage done'

      infile='coverage.gray'
      outfile='coverage_g.pgm'
      call raw2pgm(infile,outfile,361,181)
      write(6,*) 'gc coverage_g.pgm'
      open (unit=10, file='coverage.gray', status='unknown')
      close(unit=10, status='delete')

      STOP
      END

c  ..............................................
      subroutine raw2pgm(infile,outfile,npx,nln)
c  ..............................................

      implicit none
      
      integer*4       npx, nln, k, k1, k2
      character*10    line
      character*50    header
      character*72    infile, outfile
      character*5000  cline

      
      header(1:2)='P5'
      header(3:3)=char(10)
      header(4:5)='#.'
      header(6:6)=char(10)
      k=7
      write(line,fmt='(i10)') npx
      do k1=1,10
      if(line(k1:k1).ne.' ') then
        header(k:k+10-k1) = line(k1:10)
        k=k+11-k1
        go to 10
      endif
      enddo
10    continue
      header(k:k)=' '
      k=k+1
      write(line,fmt='(i10)') nln
      do k1=1,10
      if(line(k1:k1).ne.' ') then
        header(k:k+10-k1) = line(k1:10)
        k=k+11-k1
        go to 20
      endif
      enddo
20    continue
      header(k:k)=char(10)
      k=k+1
      header(k:k+2)='255'
      k=k+3
      header(k:k)=char(10)
      
      open(unit=20, file=outfile, recl=1, access='direct', 
     .     status='unknown')
      open(unit=10, file=infile, recl=npx, access='direct', 
     .     status='old')
        do k1=1,k
          write(20,rec=k1) header(k1:k1)
        enddo
        do k1=1,nln
          read(10,rec=k1) cline(1:npx)
          do k2=1,npx
            write(20,rec=k+npx*(k1-1)+k2) cline(k2:k2)
          enddo
        enddo
      close(unit=10)
      close(unit=20)
      
      return
      end

C   ..................................................
      FUNCTION SLEN(STRING)
C   ..................................................

      IMPLICIT NONE
      
      INTEGER*4         SLEN, I
      CHARACTER*(*)     STRING
      
      SLEN=LEN(STRING)
      DO I=1,SLEN
      IF(STRING(I:I).EQ.' ') THEN
        SLEN=I-1
        RETURN
      ENDIF
      ENDDO

      RETURN
      END            
