C  %Z%SCCS: %G%  FILE: %M% (%I%)

	subroutine open_l2_sim ( iprt, l2io, l2h, iret )

c  Subroutine OPEN_L2_sim opens an AIRS level 2 data file for input or 
c  output and writes header information.  The following are inputs: 
c
c	IPRT	(integer) flag for detailed print,
c	L2IO	(IO_struct) contains parameters which control the mode of 
c		I/O transfer, 
c	L2H	(Level2_header) level 2 header information. 
c
c  Be sure the following flags are set in L2IO: oldornew, unformatted, 
c  version. 
c
c  Output: 
c
c	IRET	(integer) set to 0 for successful completion, set to 
c		1 if bad data was input, -1 if an end-of-file was 
c		encountered, -2 if an I/O error occurred. 
c
c
c  Stephen Leroy, 11 November 1999
c

	implicit none

#include "structures.h"


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


c Local variables

	character*120 junk_descr
	integer i, l, n


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


	if ( l2io.oldornew .ne. 'old' .and. l2io.oldornew .ne. 'new' ) then
	  print *, 'open_l2:  bad values of oldornew:', l2io.oldornew
	  go to 600
	end if


c********************
c  Open the file. 
c********************

	if ( iprt .gt. 0 ) 
     $     print *, 'Openl2: trying to open the data set ', l2io.filename

	if ( l2io.iounit .ne. 6 ) then

	  if ( l2io.unformatted ) then
	    open ( l2io.iounit, file=l2io.filename, form='unformatted',
     $            status=l2io.oldornew, err=710 )
	  else
	    open ( l2io.iounit, file=l2io.filename, form='formatted',
     $            status=l2io.oldornew, err=710 )
	  end if

	endif


c********************
c  Read the header. 
c********************

	if ( l2io.oldornew .eq. 'old' ) then

	  if ( iprt .gt. 0 ) 
     &       print *, 'open_l2:  reading header record for ', l2io.filename


	  if ( l2io.unformatted ) then
            read ( l2io.iounit, end=700, err=710 )
     &		l2io.version, l2h.nobs, l2h.nemis, l2h.ncemis, 
     &		l2h.numlev, l2h.ndesc, (l2h.pobs(l), l = 1, l2h.numlev)
	  else
            read ( l2io.iounit, 900, end=700, err=710 )
     &		l2io.version, l2h.nobs, l2h.nemis, l2h.ncemis, 
     &		l2h.numlev, l2h.ndesc, (l2h.pobs(l), l = 1, l2h.numlev)
	  endif

          if ( iprt .gt. 1 )
     &	    print *,  l2io.version, l2h.nobs, l2h.nemis, l2h.ncemis, 
     &			l2h.numlev, l2h.ndesc, (l2h.pobs(l), l = 1, l2h.numlev)

c  Check that pressure levels exist. 

	  if (l2h.nobs .lt. 1) then
	    write (*,*) 'open_l2: nobs must be greater than zero'
	    go to 600
	  endif

c  Read the description lines. 

          if ( l2h.ndesc .lt. 1 ) go to 600

          if ( l2h.ndesc .le. MAXDESCR ) then
	    n = l2h.ndesc
	  else
	    n = MAXDESCR
	  endif

	  if ( l2io.unformatted ) then
            do i = 1, n
              read (l2io.iounit, end=700, err=710) l2h.descr(i)
              if (iprt .gt. 1) print 920, l2h.descr(i)
	    end do
	  else
            do i = 1, n
              read (l2io.iounit, 910, end=700, err=710) l2h.descr(i)
              if (iprt .gt. 1) print 920, l2h.descr(i)
	    end do
	  endif

c  If number of description lines exceeds the maximum allowable, read over 
c  the extra lines and junk them. 

	  if ( l2h.ndesc .gt. MAXDESCR ) then
	    if (l2io.unformatted) then
	      do i = 1 + MAXDESCR, l2h.ndesc
	        read (l2io.iounit, end=700, err=710) junk_descr
	        if (iprt .gt. 1) print 921, junk_descr
	      end do
	    else
	      do i = 1 + MAXDESCR, l2h.ndesc
	        read (l2io.iounit, 910, end=700, err=710) junk_descr
	        if (iprt .gt. 1) print 921, junk_descr
	      end do
	    endif
	  endif


	endif

c  Normal execution complete. 

	iret  = 0
	return

c  Handle exceptions. First, bad input data. 

 600	continue
	iret  = 1
	return

c  Second, end of file. 

 700	continue
	iret  = -1
	return

c  Third, I/O error. 

 710	continue
	iret  = -2
	return

c  Format statements. 

 900	format ( a5, 5i10/(8f10.3) )
 910	format ( a120 )
 920	format ( ' open_l2: ', a120 )
 921	format ( ' (discarding): ', a120 )

	end

