      subroutine rdrec7(lun)
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c     author
c       colin seftor
c
c     modified by e.celarier
c
c     purpose
c       read and unpack 1 record of version 7 level 2 data
c       if record is a data record store in common block /level2frame/
c       otherwise don't do anything
c
c     usage
c
c       INCLUDE "tomsframe.cmn"
c       recno= 0
c       OPEN(UNIT=lun, FILE=filename, FORM='unformatted', READONLY)
c   100 CONTINUE
c         CALL rdrec7(lun)
c         IF(seq .LT. -1) GOTO 100    !ignore trailer records
c         IF(seq .EQ. -1) GOTO 101    !end of file
c            <code to use good data>
c         GOTO 100
c   101 CONTINUE
c       CLOSE(lun)
c
c
c     variables
c       name      type  i/o   description
c       ----      ----  ---   ------------
c       recno     int    o    record number in file (init. to 0 before 1st call)
c       orbit     int    o    orbit number
c       time      int    o    gmt time (in seconds)
c       seq       int    o    logical sequence number (> 0 for data 
c                             records, < -1 for orbital summary records, 
c                             = -1 for trailer record)
c       sync      int    o    chopper sync flag
c       year      int    o    year 
c       day       int    o    julian day of year
c       lat       real   o    latitude
c       long      real   o    longitude
c       sza       real   o    solar zenith angle
c       phi       real   o    azimuthal angle
c       n_w1      real   o    313 nm n value    
c       n_w2      real   o    _w2 nm n value    
c       n_w3      real   o    _w3 nm n value    
c       n_w4      real   o    _w4 nm n value    
c       n_w5      real   o    _w5 nm n value    
c       n_w6      real   o    _w6 nm n value    
c       sens_w1   real   o    313 nm sensitivity
c       sens_w2   real   o    _w2 nm sensitivity
c       sens_w3   real   o    _w3 nm sensitivity
c       sens_w4   real   o    _w4 nm sensitivity
c       sens_w5   real   o    _w5 nm sensitivity
c       ref       real   o    reflectivity
c       o3bst     real   o    ozone
c       dndr_w1   real   o    313 nm sensitivity to ref
c       dndr_w2   real   o    _w2 nm sensitivity to ref
c       dndr_w3   real   o    _w3 nm sensitivity to ref
c       dndr_w4   real   o    _w4 nm sensitivity to ref
c       dndr_w5   real   o    _w5 nm sensitivity to ref
c       dndr_w6   real   o    _w6 nm sensitivity to ref
c       pthir     real   o    thir cloud pressure (when available)
c       pres      real   o    terrain pressure (when available)
c       resn_w1   real   o    313 nm residue
c       resn_w2   real   o    _w2 nm residue
c       resn_w3   real   o    _w3 nm residue
c       resn_w4   real   o    _w4 nm residue
c       resn_w5   real   o    _w5 nm residue
c       ozcld     real   o    ozone below cloud
c       soi       real   o    so2 index
c       pcloud    real   o    cloud pressure
c       algflg    int    o    algorithm flag (0 if a triplet used, 1 if b,
c                             2 if b with profile mixing, and 3 if c)
c       errflg    int    o    error flag
c       prfrac    int    o    profile mixing fraction
c       surf      int    o    surface category
c       lect      int    o    local equator crossing time
c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
      integer ibuf(525)

      INCLUDE "tomsframe.cmn"

c     -- read record into buffer
      read (lun,err=900) ibuf
      recno= recno + 1
c     -- unpack values that pertain to the whole scan
      orbit = ibuf(1)
      time  = ibuf(2)
      seq   = iunp2b(1,ibuf(3))
      sync  = iunp2b(2,ibuf(3))
      day   = iunp2b(1,ibuf(4))
      year  = iunp2b(2,ibuf(4))
      alt   = iunp2b(1,ibuf(5))
      nadir = iunp2b(2,ibuf(5))
c     -- if not a data record return
      if (seq.eq.-1) return 
      if (seq.lt.-1) then
         lect = ibuf(13)
         return
      endif
c     -- unpack values for each sample in the scan
      do 100 i=1,37
         kk = 6 + (i-1)*14
         lat(i)     = iunp2b(1,ibuf(kk))/100.
         long(i)    = iunp2b(2,ibuf(kk))/100.
         sza(i)     = iunp2b(1,ibuf(kk+1))/100.
         phi(i)     = iunp2b(2,ibuf(kk+1))/100.
         n_w1(i)    = iunp2b(1,ibuf(kk+2))/50.
         n_w2(i)    = iunp2b(2,ibuf(kk+2))/50.
         n_w3(i)    = iunp2b(1,ibuf(kk+3))/50.
         n_w4(i)    = iunp2b(2,ibuf(kk+3))/50.
         n_w5(i)    = iunp2b(1,ibuf(kk+4))/50.
         n_w6(i)    = iunp2b(2,ibuf(kk+4))/50.
         sens_w1(i) = iunp2b(1,ibuf(kk+5))/10000.
         sens_w2(i) = iunp2b(2,ibuf(kk+5))/10000.
         sens_w3(i) = iunp2b(1,ibuf(kk+6))/10000.
         sens_w4(i) = iunp2b(2,ibuf(kk+6))/10000.
         sens_w5(i) = iunp2b(1,ibuf(kk+7))/10000.
         ref(i)     = iunp2b(2,ibuf(kk+7))/100.
         o3bst(i)   = iunp2b(1,ibuf(kk+8))/10.
         errflg(i)  = iunp2b(2,ibuf(kk+8))
         dndr_w1(i) = iunp1b(1,ibuf(kk+9))/-50.
         dndr_w2(i) = iunp1b(2,ibuf(kk+9))/-50.
         dndr_w3(i) = iunp1b(3,ibuf(kk+9))/-50.
         dndr_w4(i) = iunp1b(4,ibuf(kk+9))/-50.
         dndr_w5(i) = iunp1b(1,ibuf(kk+10))/-50.
         dndr_w6(i) = iunp1b(2,ibuf(kk+10))/-50.
         pres(i)    = iunp1b(4,ibuf(kk+10))/100.
         resn_w1(i) = (iunp1b(1,ibuf(kk+11))-127.)/10.
         resn_w2(i) = (iunp1b(2,ibuf(kk+11))-127.)/10.
         resn_w3(i) = (iunp1b(3,ibuf(kk+11))-127.)/10.
         resn_w4(i) = (iunp1b(4,ibuf(kk+11))-127.)/10.
         resn_w5(i) = (iunp1b(1,ibuf(kk+12))-127.)/10.
         ozcld(i)   = iunp1b(2,ibuf(kk+12))
         soi(i)     = iunp1b(3,ibuf(kk+12))-50.
         pcloud(i)  = iunp1b(4,ibuf(kk+12))/10.
         algflg(i)  = iunp1b(1,ibuf(kk+13))
         clfrac(i)  = iunp1b(2,ibuf(kk+13))/100.
         prfrac(i)  = iunp1b(3,ibuf(kk+13))/10.
         surf(i)    = iunp1b(4,ibuf(kk+13))
100   continue
      return
900   seq = -999
      write (6,*) 'record bad'
      return
      end


      FUNCTION IUNP1B (LBYTE,IPACKD)
C
C***********************************************************************
C*
C*    PURPOSE:
C*       RETURN THE VALUE OF THE SPECIFIED BYTE AS A FULLWORD INTEGER
C*
C*    ARGUMENTS:
C*       NAME       I/O    DESCRIPTION
C*       ----       ---    -----------
C*       LBYTE      I      BYTE LOCATION (1 TO 4) OF PACKED VALUE
C*       IPACKD     I      FULLWORD CONTAINING SELECTED BYTE
C*
C*    DATE: JAN 1991                           PROGRAMMER: B. KELLEY
C*
C***********************************************************************
C
C**     RIGHT-SHIFT INPUT VALUE TO MOVE SELECTED BYTE TO RIGHTMOST BYTE
C**     FIELD, THEN USE LOGICAL-AND TO GET VALUE OF THAT BYTE
C
      NBITS = (4-LBYTE) * 8
      IUNP1B = IAND(ISHFT(IPACKD,-NBITS),255)
C
      END


      FUNCTION IUNP2B (IHALF,IPACKD)
C
C***********************************************************************
C*
C*    PURPOSE:  RETURN THE VALUE OF SPECIFIED TWO-BYTE HALF OF INPUT
C*      INTEGER AS A FULLWORD INTEGER
C*
C*    ARGUMENTS:
C*      NAME    I/O   DESCRIPTION
C*      ----    ---   -----------
C*      IHALF     I   SPECIFIES 2-BYTE HALF OF WORD TO BE EXTRACTED
C*                    (1 FOR FIRST HALF, 2 FOR SECOND HALF)
C*      IPACKD    I   PACKED WORD
C*
C*    LOCAL VARIABLES:
C*      RMASK  -  MASK TO CLEAR LEFT  HALF OF WORD
C*      LMASK  -  MASK TO CLEAR RIGHT HALF OF WORD
C*
C*    DATE: JAN 1991                           PROGRAMMER: B. KELLEY
C*
C***********************************************************************
C
      INTEGER RMASK
      DATA RMASK/65535/,LMASK/-65536/
C
      IF (IHALF.EQ.1) THEN
C
C**       IUNPAK = IPACKD RIGHT-SHIFTED 16 BITS.  IF IPACKD IS NEGATIVE,
C**       LOGICAL-OR IUNPAK WITH LMASK TO SET LEFTMOST 16 BITS.
C
        IUNPAK=ISHFT(IPACKD,-16)
        IF (IPACKD.LT.0) IUNPAK=IOR(LMASK,IUNPAK)
      ELSE
C
C**       CLEAR LEFT HALF OF IPACKD.  IF IUNPAK IS GREATER THAN MAXIMUM
C**       VALUE OF TWO-BYTE SIGNED INTEGER, THEN PACKED VALUE IS A
C**       NEGATIVE NUMBER AND IS OBTAINED BY SUBTRACTING 2**16.
C
        IUNPAK=IAND(IPACKD,RMASK)
        IF (IUNPAK.GT.32767) IUNPAK=IUNPAK-65536
      END IF
C
      IUNP2B = IUNPAK
C
      END
