C+
C NAME:
C	smei_frm_get
C PURPOSE:
C	Finds SMEI frame in specified time period
C CALLING SEQUENCE:
	logical function smei_frm_get(iFirst,TBeg,TEnd,icam,mode,bdig,cBase,cFrmSpec)
C INPUTS:
C	iFirst		integer		1 for new search
C					0 to continue search
C	TBeg(2)		integer		start time
C	TEnd(2)		integer		end time
C	icam		integer		camera number (1,2,3)
C					passed to smei_frm_path
C	mode		integer		mode number (0,1,2)
C	bdig		integer
C	cBase*(*)	character	root directory of SMEI DB
C					passed to smei_frm_path
C OUTPUTS:
C	cFrmSpec*(*)	character	fully qualified name of SMEI frame.
C INCLUDE:
	include		'filparts.h'
C CALLS:
C	smei_frm_path, smei_frm_getfirst, smei_frm_getnext
C	smei_Time2Split, Time2Differ, bOSFindClose
C PROCEDURE:
C	Typically this function is used in a construct like this:
C
C	include		'openfile.h'
C	include		'smei_frm_layout.h'
C
C	cBase  = ' '
C	call smei_Time2 Split(1, cFrameBeg, TBeg)
C	call smei_Time2 Split(1, cFrameEnd, TEnd)
C	icam   = 1
C	iFirst = 1
C	do while (smei_frm_get(iFirst, TBeg, TEnd, icam, cBase, cFrmSpec))
C	    if (bReadNic(OPN__REOPEN+OPN__STOP, cFrmSpec, SMEI__FRM_NPIX, nx, ny, nb, frame, ctrailer)) then
C		(process frame data in array frame)
C	    end if
C	end do
C
C	As long as smei_frm_get returns .TRUE. cFrmSpec should return
C	frame names from TBeg to TEnd in chronological order.
C MODIFICATION HISTORY:
C	JUN-2004, Paul Hick (UCSD/CASS)
C	FEB-2005, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
C	    Added check to make sure that the first record found
C	    wit t > TBeg also has t <= TEnd.
C-
	    integer	iFirst
	    integer	TBeg(2)
	    integer	TEnd(2)
	    integer	icam
	    integer	mode
	    logical	bdig
	    character	cBase   *(*)
	    character	cFrmSpec*(*)

	character	cPath*(FIL__LENGTH)
	integer		TTime(2)
	integer		Time2Differ
	integer		smei_frm_path
	logical		smei_frm_getfirst
	logical		smei_frm_getnext

	save		iDoy

	if (iFirst .eq. 1) then		! Initialize new search

	    iDoy = TBeg(1)-1

	    cFrmSpec = ' '

	    do while (iDoy .lt. TEnd(1) .and. cFrmSpec .eq. ' ')

		iDoy = iDoy+1

		TTime(1) = iDoy
		TTime(2) = 0		! Path to directory where frame is located

		I = smei_frm_path(bdig, ' ', TTime, icam, cBase, cPath)

		! smei_frm_getfirst returns
		! .TRUE.  if a file is found with mode less than the requested mode.
		!	Don't need it, so keep looking
		! .TRUE.  if a file is found with the right mode, but T < TBeg.
		!	Don't need it, so keep looking
		! .FALSE. if no more files are found, or if a file is found
		!	with mode larger than the requested mode.
		!	cFrmSpec is set to the blank string.
		!	Continue the while loop with the next day.
		! .FALSE. if a frame for the right mode with T >= TBeg is found.
		!	This is the frame we need.
		!	The frame name is in cFrmSpec.

		iFind = 1
		do while ( smei_frm_getfirst(iFind,cPath,TBeg,mode,cFrmSpec) )
		    iFind = 0
		end do

	    end do

	    smei_frm_get = cFrmSpec .ne. ' '

	    if (smei_frm_get) then

		! Found frame for right mode with t > TBeg. Make sure that
		! t <= TEnd. If it is not then return .FALSE.

		call smei_Time2Split(1,cFrmSpec,TTime)
		smei_frm_get = Time2Differ(TTime,TEnd) .le. 0

		if (smei_frm_get) then	! Frame is OK
		    iFirst = 0		! For next smei_frm_get call: continue search
		else			! Reject frame
		    cFrmSpec = ' '
		end if

	    end if

	else					! Continue search

	    cFrmSpec = ' '			! Redundant, I think (always iDoy <= TEnd(1) here)
	    iFind = 0

	    do while (iDoy .le. TEnd(1) .and. cFrmSpec .eq. ' ')

		TTime(1) = iDoy
		TTime(2) = 0			! Path to directory where frame is located
		I = smei_frm_path(bdig, ' ', TTime, icam, cBase, cPath)

		! smei_frm_getnext returns
		! .TRUE. if a file is found with mode less than requested.
		!	Don't need it, so keep looking
		!	(this never happens the first time this while loop is
		!	entered, since a frame with the right mode was found
		!	already with smei_frm_getfirst).

		! .FALSE. if a frame with T > TEnd is found.
		! .FALSE. if a frame is found with mode larger than requested
		! .FALSE. if no more files are found
		!	cFrmSpec is the blank string.
		!	Continue the while loop with the next day.
		!	If T > End than the while loop will terminate immediately.

		! .FALSE. if a file is found with the right mode and T <= TEnd
		!	This is what we need. The frame name is in cFrmSpec.

		do while ( smei_frm_getnext(iFind,cPath,TEnd,mode,cFrmSpec) )
		    iFind = 0
		end do

		if (cFrmSpec .eq. ' ') then	! No more files, mode too large, T > TEnd
		    if (bDig) then
			iDoy  = iDoy+1		! Try next day
			iFind = 1		! Start a new search
		    else
			iDoy = TEnd(1)+1	! Forces exit from do while loop
		    end if
		end if

	    end do

	    smei_frm_get = cFrmSpec .ne. ' '

	end if

	return
	end
