c -----------------------------------------------------------------------
c cette routine retourne les noms complets des fichiers trouves dans
c un intervalle horaire;
c si nfm=1 en entree, le programme ne cherche que le premier fichier
c  (recherche du debut d'une observation) 
c -----------------------------------------------------------------------
      Subroutine fnom38(ftype,date,ideb,ifin,nfm,fname,ftime,istop)
      implicit none
c INPUT:
      character jour*8,hdeb*6,hfin*6
c ideb = HHMMSS (heure de debut; exemple 103407 pour 10h34mn07s)
c ifin = HHMMSS (heure de fin;   exemple 110456 pour 11h04mn56s)
c OUTPUT:
      character*38 fname(4000),ftype,fic
      integer*4 ftime(4000)
      integer*4 nf,nfm,istop,reg,lin,date
c iu = numero de l'unite de lecture
c nf = nombre de fichiers trouves
c fname = tableau contenant le nom des nf fichiers trouves
c ftime = tableau contenant les heures des nf fichiers trouves
c
      character h*6,d*8
      integer*4 ideb,ifin,ih,im,is,i,j,ier
      integer*4 ideb2,ifin2
      logical*4 present
c
10    format(a9)
11    format(1x,a6)
c      read(8,10)ftype
c         istop=0
c         if(ftype.eq.'         '.or.ftype.eq.'end      ')then
c            istop=1
c            return
c         endif
c        write(ftype(1:1),'(a1)')let
99    format(I6)
      encode(6,99,hdeb)ideb
      encode(6,99,hfin)ifin
      type*,'fnom38',ideb,ifin

      nf=0
      read(hdeb(1:2),'(i2)',iostat=ier) ih
      if(ier.ne.0) stop 'Error reading start time 1'
c      type*, ih
      if(ih.lt.0.or.ih.gt.23) stop 'Error reading start time 2'
      read(hdeb(3:4),'(i2)',iostat=ier) im
c      type*,im
      if(ier.ne.0) stop 'Error reading start time 3'
      if(im.lt.0.or.im.gt.59) stop 'Error reading start time 4'
      read(hdeb(5:6),'(i2)',iostat=ier) is
      if(ier.ne.0) stop 'Error reading start time 5'
c      type*,is
      if(is.lt.0.or.is.gt.59) stop 'Error reading start time 6'
      ideb2=ih*3600+im*60+is

      read(hfin(1:2),'(i2)',iostat=ier) ih
      if(ier.ne.0) stop 'Error reading end time'
      if(ih.lt.0.or.ih.gt.23) stop 'Error reading end time'
      read(hfin(3:4),'(i2)',iostat=ier) im
      if(ier.ne.0) stop 'Error reading end time'
      if(im.lt.0.or.im.gt.59) stop 'Error reading end time'
      read(hfin(5:6),'(i2)',iostat=ier) is
      if(ier.ne.0) stop 'Error reading end time'
      if(is.lt.0.or.is.gt.59) stop 'Error reading end time'
      ifin2=ih*3600+im*60+is
       if(ifin2.eq.0) ifin2=ideb2
c
         fic=ftype
         write(fic(15:22),'(i8)')date
c
      nf=0
c
c                   type*, ' fnom38 secondes ', ideb2,ifin2
      do 1 i=ideb2,ifin2
         ih=i/3600
         im=(i-ih*3600)/60
         is=i-ih*3600-im*60
         write(h(1:2),'(i2)') ih
         write(h(3:4),'(i2)') im
         write(h(5:6),'(i2)') is
c
c          type*,i,ih,im,is
         write(fic(24:29),'(a6)')h
         do j=1,38         
            if(fic(j:j).eq.' ') fic(j:j)='0'
         end do
         ier=-1
c                  type*,'nom fichier ',fic
         open(unit=15,form='unformatted',access='sequential',
     1        file=fic,iostat=ier,status='old',readonly)
         inquire(file=fic,exist=present)
         if(present) ier=0
c           type *,'ier ',ier
         if(ier.ne.0) goto 1
         nf=nf+1
         if(nf.gt.4000) stop 'Too much files. Max allowed: 4000'
c         close(unit=15)
         fname(nf)=fic
         ftime(nf)=is+60*(im+60*ih)
                              type*, fname(nf)
         if(nfm.eq.1)return
            
1     continue
       nfm=nf
20    format(' No file found')
      if(nf.eq.0)then
         istop=1
         type 20
         write(3,20)
      endif
      if(nf.eq.0) stop 'Error: no files found'
c      type*,'nfm', nfm
      return
      end

