      PROGRAM write_c2_txt

c----------------------------------------------------------------
c PROGRAM:  write_c2_txt.f
c
c PURPOSE:  Writes HIRS C2 all-sky data files to text file.
c
c LANGUAGE:  Fortran-77
c
c CREATION HISTORY:  Darren Jackson  CIRES/ETL October 2004
c-----------------------------------------------------------------

      IMPLICIT NONE

c Declarations
      
      INTEGER   MAXREC
      PARAMETER (MAXREC=500000)

      BYTE         ispot,iref,iemis,icmask,icldfnl
      INTEGER*2    ilon,ilat,line,isolz,ialt,itb(7),ictt,isfct
      INTEGER*2    idif(4),icld(5)
      INTEGER      itime,i,j,ic
      REAL         xlon,xlat,xsolz,xalt,tb(7),ctt,sfct,dif(4)
      CHARACTER    ifile*70,ofile*80

c Open and read C2 data

      WRITE(*,*) 'Enter input file name:'
      READ(*,*) ifile
      OPEN(5,FILE=ifile,ACCESS='DIRECT',RECL=55)
      ic = index(ifile,' ')-1
      ofile = ifile(1:ic)//'.txt'
      OPEN(20,FILE=ofile)
      DO i=1,MAXREC
         READ(5,rec=i,err=99) itime,ilon,ilat,line,ispot,isolz,ialt,
     &        iref,itb,iemis,ictt,isfct,idif,icmask,icld,icldfnl

c Make conversions
         xlon = ilon/100.
         xlat = ilat/100.
         xsolz =  isolz/100.
         xalt = ialt/10.
         DO j=1,7
            tb(j) = itb(j)/100. + 100.
         ENDDO
         ctt = ictt/100. + 100.
         sfct = isfct/100. + 100.
         DO j=1,4
            dif(j) = idif(j)/100.
         ENDDO
         
         WRITE(20,50) xlat,xlon,icmask,tb(5),icld(icldfnl)
 50      FORMAT(2(1X,F7.2),1x,i3,1x,f7.2,1x,i5)
      ENDDO

 99   CLOSE(5)
      CLOSE(20)
      END
