c-----------------------------------------------------------------------
c
c                              ----------
c                               ssmt1rdf
c                              ----------
c
c     Read output of ssmt1rdc, place values into arrays.
c
c-----------------------------------------------------------------------
c
c     Written 7/95 by Don Moss, University of Alabama in Huntsville
c
c-----------------------------------------------------------------------
c
c     This program takes output from the C program ssmt1rdc and loads
c     the values (from an SSM/T file) into arrays.
c
c     Use it as a starting point.  Add to it as needed to process the
c     data to meet your requirements.
c
c-----------------------------------------------------------------------



c----------program variables----------
c   infile - Input File Name
c   ids    - Input Data Set Number
c   iscan  - Scan Number   (loop index)
c   ipos   - Beam Position (loop index)
c   CODE   - Check Value for OLS SSP Message Synchronization Code
c   I2MAX  - Maximum Value of a Two-byte Integer
c   I2FIX  - Conversion Constant (to recover a negative I*2 value)
c   ifov   - Field of View  (loop index, implied do-loop variable)
c   ichan  - Channel Number (implied do-loop variable)
c
      character*48 infile
      integer IDS/11/, iscan, ipos
      character*12 CODE/'0005717D78BE'/
      integer I2MAX/32767/, I2FIX/65536/
      integer ifov, ichan



c----------header record variables----------
c   scid   - Spacecraft ID
c   instr  - Instrument ID
c   stream - Ingest Data Stream Code
c   pbid   - Processing Block ID
c   syear  - Start Year
c   sday   - Start Day
c   stime  - Start Time
c   nscan  - Number of Scans
c   eyear  - End Year
c   eday   - End Day
c   etime  - End Time
c   ngap   - Number of Data Gaps
c
c-------Pre-Flight Calibration-----------------------------------
c       pccnt  - Count
c       pctemp - Temperature
c       pchot  - Hot Reference Temperature Calibration Constant
c       pccold - Cold Reference Temperature Calibration Constant
c----------------------------------------------------------------
c
c   scbias - Scan Bias Correction
c   apc    - Antenna Pattern Correction
c   mncoef - Manual Coefficients
c   limlo  - Raw Counts Lower Limits
c   limup  - Raw Counts Upper Limits
c
      integer scid, instr, stream
      character*8 pbid
      integer syear, sday, stime, nscan
      integer eyear, eday, etime, ngap
      integer pccnt(11), pctemp(11), pchot(7), pccold(7)
      integer scbias(7,7), apc(7,7)
      integer mncoef(7), limlo(7), limup(7)



c----------data record variables----------
c   scan   - Scan Number
c   orient - Orientation Vectors
c   alt    - Satellite Altitude
c   angle  - Satellite Angle
c   year   - Scan Year
c   day    - Scan Day
c   time   - Scan Time
c   orbit  - Orbit Number
c   lat    - Latitudes
c   lon    - Longitudes
c
c-------SSM/T Data------------------------------
c       osync  - OLS Sync Code
c       otime  - OLS Time Code
c       obs    - Field of View Observations
c       mux    - Housekeeping Parameters
c       sagc   - Stepped Automatic Gain Control
c       bpos   - Beam Position
c-----------------------------------------------
c
c-------Calibration Coefficients-------
c       wload  - Warm Load Temperature
c       slope  - Slope
c       wmean  - Warm Calibration Mean
c       cmean  - Cold Calibration Mean
c--------------------------------------
c
c   qflag  - Scan Quality Flags
c
      integer scan, orient(3), alt, angle
      integer year, day, time, orbit
      integer lat(7), lon(7)
      character*12 osync(9)
      integer otime(9)
      integer obs(9, 7)
      integer mux(9, 3), sagc(9), bpos(9)
      integer wload
      integer slope(7), wmean(7), cmean(7)
      integer*1 qflag(25)



c----------get input file name----------
      print *, 'Enter input file name'
      read (*, 555) infile
  555 format (a48)
      open (ids, file=infile)



c----------read header record----------
      read (ids, 101, err=7, end=8) scid, instr, stream, pbid
      read (ids, 102, err=7, end=8) syear, sday, stime, nscan
      read (ids, 102, err=7, end=8) eyear, eday, etime, ngap
      read (ids, 103, err=7, end=8) pccnt
      read (ids, 103, err=7, end=8) pctemp
      read (ids, 104, err=7, end=8) pchot
      read (ids, 104, err=7, end=8) pccold
      read (ids, 104, err=7, end=8)
     >  ((scbias(ichan,ifov),ifov=1,7),ichan=1,7)
      read (ids, 104, err=7, end=8)
     >  ((apc(ichan,ifov),ifov=1,7),ichan=1,7)
      read (ids, 104, err=7, end=8) mncoef
      read (ids, 104, err=7, end=8) limlo
      read (ids, 104, err=7, end=8) limup

  101 format (2i5, i7, 2x, a8)
  102 format (2i5, i10, i7)
  103 format (11i6)
  104 format (7i6)



c----------read data records----------
      do 1 iscan = 1, nscan
          read (ids, 201, err=7, end=8)
     >      scan, orient(1), orient(2), orient(3), alt, angle,
     >      year, day, time, orbit
          read (ids, 202, err=7, end=8) lat, lon

          do 2 ipos = 1, 9
              read (ids, 203, err=7, end=8) osync(ipos), otime(ipos),
     >          (obs(ipos, ichan), ichan=1,7),
     >          mux(ipos, 1), mux(ipos, 2), mux(ipos, 3),
     >          sagc(ipos), bpos(ipos)

c             ---check for valid ols sync code---
              if (osync(ipos) .ne. CODE)
     >          print *, '***  WARNING - Bad OLS Sync Code in Scan',
     >          iscan, '  ***'

    2     continue

          read (ids, 204, err=7, end=8) wload, slope
          read (ids, 202, err=7, end=8) wmean, cmean
          read (ids, 205, err=7, end=8) qflag

c         ---correct sign of lat & lon---
          do 3 ifov = 1, 7
              if (lat(ifov) .gt. I2MAX) lat(ifov) = lat(ifov) - I2FIX
              if (lon(ifov) .gt. I2MAX) lon(ifov) = lon(ifov) - I2FIX
    3     continue

    1 continue

  201 format (8i6, 2i12)
  202 format (14i6)
  203 format (a12, i12, 12i6)
  204 format (8i6)
  205 format (25i4)



c----------termination alternatives----------
      print *, '---end---     scans read: ', nscan
      stop

    7 print *, '***  READ ERROR at Scan', iscan, '  ***'
      stop

    8 print *, '***  PREMATURE END OF FILE at Scan', iscan, '  ***'
      stop

      end
