c==========================================================================
c
c NAME: open_read
c
c PURPOSE: This routine opens output files for hcloud.f
c
c INPUT:
c     iom..........file number for land mask file for time constrast test
c     ion..........file numbers for temp. scan line file (int)
c     iog(3).......file number for max/min grid files (int)
c     iof..........file number for final scan line output file
c     ioh..........file number of error messages
c     iol..........file number for land mask file for space constrast test
c     satid........satellite character id (character*3)
c     iyr4.........4 digit year (int)
c     jday.........julian day (int)
c     ierr.........error status 0 -> no errors
c                               1 -> scan line file does not exist
c                               2 -> grid file for -1 day does not exist
c                               3 -> grid file for 0 day does not exist
c                               4 -> grid file for +1 day does not exist
c
c OUTPUT:
c          Opens output files for hcloud.f
c
c EXTERNALS:
c            hirs1c.inc
c            recl_parms.inc
c            date_to_julian69.f
c            julian69_to_date.f
c
c HISTORY:
c          Darren Jackson CIRES/ETL April 2002
c
c============================================================================
      subroutine open_read(iom,ion,iog,iof,ioh,iol,satid,iyr4,
     .                     jday,ierr)

      implicit none
      include 'hirs1c.inc'
      include 'recl_parms.inc'

      integer   ioh,iom,iol,iof,ion,iog(3)
      integer   iyr4,jday,jday69,n,ierr,jd(3),yr(3)
      integer   mon,dy

      character satid*3,tmp_path*12,misc_path*38
      character dgrid_path*8,dgrid_file*50
      character tas_file*50,tcl_file*50
      character l_file*70

      logical   around      

      data tmp_path/'../tmp_data/'/
      data dgrid_path/'../grid/'/
      data misc_path/'/data/dlj/tovs1b/pathfinder/misc_data/'/
      ierr=0
c
c* Convert day of year and year to julian year from 1969
c
      mon=1
      call date_to_julian69(iyr4,mon,jday,jday69)
c
c*  Find day of year and year on day before and after current day
c
      call julian69_to_date(1,jday69-1,yr(1),mon,jd(1))
      call julian69_to_date(1,jday69+1,yr(3),mon,jd(3))
      yr(2)=iyr4
      do n=1,3
         if(yr(n) .ge. 2000) then
            yr(n)=yr(n)-2000
         else
            yr(n)=yr(n)-1900
         endif
      enddo
      jd(2)=jday
c
c* Open error message file
c
      open(ioh,file='hcloud.err',access='append')
c
c*  Open temporary scan line data file
c
      around=.true.
      write(tas_file,98) 'HIRS.',satid,'.Y',yr(2),'.D',jday,'.V22.AS'
      inquire(file=tmp_path//tas_file,exist=around)
      if(.not. around) then
         ierr=1
         return
      endif
 98   format(a5,a3,a2,i2.2,a2,i3.3,a7)
      open(unit=ion,file=tmp_path//tas_file,status='unknown',
     .     access='direct',recl=as_rec)
c
c* Open max/min daily grid files (3 days)
c
      do n=1,3
         write(dgrid_file,98) 'HIRS.',satid,'.Y',yr(n),'.D',jd(n),
     .        '.V22.GD'
         inquire(file=dgrid_path//dgrid_file,exist=around)
         if(.not. around) then
            ierr=n+1
            return
         endif
         open(unit=iog(n),file=dgrid_path//dgrid_file,access='direct',
     .        recl=nlon*nlat*ntim*4)
      enddo
c
c*  Open clear-sky temporary file
c
      around=.false.
      write(tcl_file,99) 'HIRS.',satid,'.Y',yr(2),'.D',jday,'.V22.CS1'
 99   format(a5,a3,a2,i2.2,a2,i3.3,a8)
      inquire(file=tmp_path//tcl_file,exist=around)
      if(around) then
         call system("rm "//tmp_path//tcl_file)
      endif
      open(unit=iof,file=tmp_path//tcl_file,status='unknown',
     .     access='direct',recl=cs1_rec)
c
c* Open land mask data files
c
      l_file=misc_path//'lmask_0.5_lsc_space.dat'
      open(iol,file=l_file,recl=nlon*nlat*1,access='direct')
      l_file=misc_path//'lmask_0.5_lsc_time.dat'
      open(iom,file=l_file,recl=nlon*nlat*1,access='direct')

      return
      end
