        subroutine read_l2_sim (iprt, l2io, l2h, l2d, iret)

        implicit none

#include "structures.h"       

C ========================================================================

        integer iprt
        record /IO_struct/ l2io
        record /Level2_header/ l2h
        record /airs_level2/ l2d
        integer iret

        integer i, j, l

        logical dump_ok

c  Externals. 

        real*8 date_to_tai
        external date_to_tai

c***********************************************************************
c  Begin execution. 
c***********************************************************************

        if (iprt .gt. 2) then
          dump_ok = .true.
        else
          dump_ok = .false. 
        endif

c  Check version type. 

        if (l2io.version .ne. 'L2V2C' )  then
          write (*,*) 'read_l2: invalid format version ', l2io.version
          goto 705
        endif

        if ( l2io.unformatted ) then
           
           read ( l2io.iounit, end=700, err=710 ) l2d.idprof,
     *          l2d.alat, l2d.alon, l2d.tai_time, 
     *          l2d.satzen, l2d.satazi, l2d.solzen, l2d.solazi, l2d.satheight, 
     *          l2d.topog, l2d.topog_err, l2d.landFrac, l2d.landFrac_err, 
     *        l2d.psurf, l2d.tsurf, l2d.co2ppmv, l2d.MWSurfClass, l2d.ncld,
     *        l2d.emismw, l2d.emismwStd,
     *        (l2d.pcldtop(l), l2d.cldfrc(l), l = 1, l2d.ncld),
     *        (l2d.tair(l), l2d.h2ocd(l), l2d.ozocd(l), l2d.wlcd(l), 
     *        l2d.ciw(l),  l2d.cocd(l), l2d.ch4cd(l), 
     *        l = 1, l2h.numlev),
     *        (l2d.freqemis(i), l2d.emisir(i), l2d.rhoir(i), 
     *        i = 1, l2h.nemis),
     *        ( (l2d.cldfreq(j,i), l2d.cldemiss(j,i), l2d.cldrho(j,i), 
     *        j = 1, l2h.ncemis), i = 1, l2d.ncld),
     *        l2d.nispare, l2d.nrspare,
     *        (l2d.ispare(j), j = 1, l2d.nispare),
     *        (l2d.rspare(j), j = 1, l2d.nrspare)

      else

c  Now for formatted output. 

         read ( l2io.iounit, 903, end=700, err=710 ) l2d.idprof,
     *          l2d.alat, l2d.alon, l2d.tai_time, 
     *          l2d.satzen, l2d.satazi, l2d.solzen, l2d.solazi, l2d.satheight, 
     *          l2d.topog, l2d.topog_err, l2d.landFrac, l2d.landFrac_err, 
     *          l2d.psurf, l2d.tsurf, l2d.co2ppmv, l2d.MWSurfClass, l2d.ncld,
     *          l2d.emismw, l2d.emismwStd

c  No read of cloud data if no cloud data. 

         if (l2d.ncld .gt. 0) then
            read ( l2io.iounit, 905, end=700, err=710 )
     *           ( l2d.pcldtop(l), l2d.cldfrc(l), l = 1, l2d.ncld)
         endif

c  Continue with read . Column density fields. 
       
         read ( l2io.iounit, 912, end=700, err=710 )
     *          (l2d.tair(l), l2d.h2ocd(l), l2d.ozocd(l), l2d.wlcd(l), 
     *          l2d.ciw(l), l2d.cocd(l), l2d.ch4cd(l), 
     *           l = 1, l2h.numlev) 


         read ( l2io.iounit, 920, end=700, err=710 )
     *        (l2d.freqemis(i), l2d.emisir(i), l2d.rhoir(i), 
     *          i = 1, l2h.nemis)

         if (l2d.ncld .gt. 0 .and. l2h.ncemis .gt. 0) then
            read (l2io.iounit, 921, end=700, err=710)
     *          ((l2d.cldfreq(j,i), l2d.cldemiss(j,i), l2d.cldrho(j,i), 
     *          j = 1, l2h.ncemis), i = 1, l2d.ncld)
         endif

c  read the spares. 

         read (l2io.iounit, 925, end=700, err=710) l2d.nispare, l2d.nrspare

         if (l2d.nispare .ge. 1) then
            read (l2io.iounit, 930, end=700, err=710) (l2d.ispare(j), 
     *           j = 1, l2d.nispare )
         endif

         if (l2d.nrspare .ge. 1) then
            read (l2io.iounit, 935, end=700, err=710) (l2d.rspare(j), 
     *           j = 1, l2d.nrspare )
         endif

      endif

        iret  = 0
        goto 300

c  Handle exceptions. 

c  End of input reached. 

 700    iret  = -1
        dump_ok = .false. 
        goto 300

c  Bad version type. 

 705    iret = 1
        goto 300

c  I/O error. 

 710    iret  = -2
        goto 300


300     continue

c  Do the dumping if it's still called for. 

        if ( dump_ok ) then

           write (*, 903) l2d.idprof,
     *          l2d.alat, l2d.alon, l2d.tai_time, 
     *          l2d.satzen, l2d.satazi, l2d.solzen, l2d.solazi, l2d.satheight, 
     *          l2d.topog, l2d.topog_err, l2d.landFrac, l2d.landFrac_err, 
     *          l2d.psurf, l2d.tsurf, l2d.co2ppmv, l2d.MWSurfClass, l2d.ncld,
     *          l2d.emismw, l2d.emismwStd


          if (l2d.ncld .gt. 0) then
            write (*,905) 
     *          ( l2d.pcldtop(l), l2d.cldfrc(l), l = 1, l2d.ncld)
          endif

          write (*,912)
     *          (l2d.tair(l), l2d.h2ocd(l), l2d.ozocd(l), l2d.wlcd(l), 
     *          l2d.ciw(l), l2d.cocd(l), l2d.ch4cd(l), 
     *           l = 1, l2h.numlev) 

          write (*,920) 
     *         (l2d.freqemis(i), l2d.emisir(i), l2d.rhoir(i), 
     *          i = 1, l2h.nemis)
          
          if (l2d.ncld .gt. 0 .and. l2h.ncemis .gt. 0) then
             write (*,921)
     *            ((l2d.cldfreq(j,i), l2d.cldemiss(j,i), l2d.cldrho(j,i), 
     *            j = 1, l2h.ncemis), i = 1, l2d.ncld)
          endif

          write (*,925) l2d.nispare, l2d.nrspare
          if (l2d.nispare .ge. 1) then
             write (*,930) (l2d.ispare(j), j = 1, l2d.nispare)
          endif

          if (l2d.nrspare .ge. 1) then
             write (*,935) (l2d.rspare(j), j = 1, l2d.nrspare)
          endif
          
       endif


c  Format statements. 

 903    format ( a8, 2(1x, f9.3), 2x, f18.5 /
     *          4(1x, f11.6) , 2x, f11.6 /
     *          f12.6, 1x, f11.6, 1x, f9.3, 1x, f9.3 /
     *          2(f10.2, 1x), f11.5, i4, i4/ 8f10.5)
 905    format ( f10.2, f10.5 )
 912    format ( f9.3, 3e15.5, i2, 2e15.5 )
 920    format ( f10.3, 2f10.5 )
 921    format ( f11.3, 2f10.5 )
 925    format ( 2i3 )
 930    format ( 8i8 )
 935    format ( 4e15.5 )

        end



