CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
       SUBROUTINE CHECK_MIDNIGHT(OLD_EPOCH, SR_EPOCH, EDB0_EPOCH, FILENAME,
     &				 NAME_LEN, INPUT_TYPE, DATA_TYPE, SPAN_MIDNIGHT) 
C+
C
C FUNCTIONAL DESCRIPTION:
C
C    This routine is meant to deal with the case when an LZ file is absent.  In
C    such a case, those SOHO data programs that span files will (usually) go to
C    REL/QKL files.  Unfortunately, once this happens, the programs formerly
C    did not switch back to LZ's even if they were available for later dates in
C    the same run.  This routine corrects that problem.  It tests the times
C    passed to it to see if the times span midnight.  If so, a flag is set, and
C    an attempt is made to construct a new filename using CONSTRUCT_FILENAME. 
C    If an LZ is available, the filename constructed will be the LZ file's
C    name, allowing data processing to switch back to LZ's.  If no LZ is
C    available, then the status of the flag SPAN_MIDNIGHT tells the calling
C    program to continue processing.  Note that, if there are two REL/QKL files
C    which overlap and cross midnight, SPAN_MIDNIGHT will still be set .FALSE.
C    in all but the last case; see comments in code below.
C
C FORMAL ARGUMENTS:
C
C    OLD_EPOCH:
C      R*8	  I	Epoch time of previous science record.
C
C    SR_EPOCH:
C      R*8	  I	Epoch time of current science record.
C
C    EDB0_EPOCH:
C      R*8	  I	Epoch time of EDB 0 of current science record, to be
C			used in case SR_EPOCH is bad (zero).
C
C    FILENAME:
C      CHAR(*)	  I/O	Upon entry contains the name of the file currently
C			being processed.  Upon return, if SPAN_MIDNIGHT is set
C			to .TRUE. (see below) this will contain the name of
C			the LZ file to be processed next, otherwise it will
C			not be modified.
C
C    NAME_LEN:
C      I*4	  I/O	Upon entry contains the length of the filename stored
C			in the argument FILENAME.  If FILENAME is modified
C			(see above) NAME_LEN will also be updated to its new
C			value.
C
C    INPUT_TYPE:
C      I*4	  O	If SPAN_MIDNIGHT is set to .TRUE. (see below) this
C			will, upon return, contain the code for the type of
C			file (LZ, REL, QKL) that was selected and stored in
C			FILENAME.  Currently the logic allows SPAN_MIDNIGHT to
C			be set to true only if an LZ file is found, so this
C			argument, if set, will always be set to 1.
C
C    DATA_TYPE:
C      I*4	  I	The code for the data type (science, HK, etc.) of the
C			file in FILENAME (see above).
C
C    SPAN_MIDNIGHT:
C      L*4	  O	A logical variable that is set .TRUE. if and only if
C			the times is OLD_EPOCH and SR_EPOCH (or its alternate,
C			EDB0_EPOCH) are on different days AND there are no
C			more REL/QKL files on the day represented by OLD_EPOCH
C			that have not yet been processed AND there exists an
C			LZ file for the following day.
C
C INTERFACE:
C
!	INTERFACE
!	  SUBROUTINE CHECK_MIDNIGHT(OLD_EPOCH, SR_EPOCH, EDB0_EPOCH, FILENAME,
!     &				NAME_LEN, INPUT_TYPE, DATA_TYPE, SPAN_MIDNIGHT) 
!	    REAL(KIND=8), INTENT(IN) :: OLD_EPOCH, SR_EPOCH, EDB0_EPOCH, DATA_TYPE
!	    CHARACTER(LEN=*), INTENT(INOUT) :: FILENAME
!	    INTEGER, INTENT(INOUT) :: NAME_LEN, INPUT_TYPE
!	    LOGICAL, INTENT(OUT) :: SPAN_MIDNIGHT
!	  END SUBROUTINE CHECK_MIDNIGHT
!	END INTERFACE
C
C IMPLICIT INPUTS:
C
C    None
C
C IMPLICIT OUTPUTS:
C
C    None
C
C SIDE EFFECTS:
C
C    If the dates of the current and next science records are the same the
C    SPAN_MIDNIGHT flag is not set to either true or false; it retains its
C    previous value.
C
C    If the dates of the current and next science records are different and
C    the SPAN_MIDNIGHT flag is set to true, the input filename, length, and
C    data type is overwritten with the new filename and therefore lost.
C
C    When SPAN_MIDNIGHT is set to true a single line of output will be
C    written to unit 6 giving the name of the new filename.
C
C COMMON BLOCKS:
C
C    None
C
C INCLUDE FILES:
C
C    None
C
C DESIGN:
C
C     get date corresponding to the previous science record
C     if current science record time is bad, use EDB(0) time instead
C     get date corresponding to the current science record
C     if science records are on different days (the day number has increased
C      by one) then 
C	get the filename corresponding to the start of the next day
C	if current filename is returned or there is no file on the next day then
C	  set span midnight flag to false
C	else
C	  if file on next day is an LZ file then
C	    set span midnight flag to false
C	  else
C	    set span midnight flag to true
C	  endif
C	endif
C     endif
C
C CREATED:
C
C    16-AUG-1996    John Paquette
C
C This version:   1.4   27-AUG-1999
C
C DEVELOPMENT HISTORY:
C 
C  Author       Date		Description of Change 
C  --------	-----------	--------------------- 
C  J. PAQUETTE  16-AUG-1996     Created (excerpted from PMSW)
C  J. PAQUETTE  21-AUG-1996     If STATUS is -1, then treat as if no LZ file
C                               found (since, obviously, there's no LZ file if
C                               there's no file at all) 
C  J. PAQUETTE,  6-DEC-1996     allow for case of two REL/QKL files spanning
C  L. Bleau                     midnight with no LZ file on the following day
C  L. Bleau	11-MAR-1999	update comments only
C  L. Bleau	18-MAR-1999	added interface block
C  L. Bleau	13-APR-1999	update comments only
C  L. Bleau	16-APR-1999 1.3	added declarations, IMPLICIT NONE
C  L. Bleau	23-AUG-1999	update comments only
C  L. Bleau	27-AUG-1999 1.4	remove call to CONSTRUCT_TOMORROWS_FILENAME,
C				put code inline instead; suppress message
C				in CONSTRUCT_FILENAME, do own WRITE if we
C				switch to LZ
C  L. Bleau	 2-NOV-1999	add comments only
C  L. Bleau	15-NOV-1999	add comments
C-
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CDEC$  IDENT	'1.4'
       IMPLICIT NONE
       INTEGER*4 NAME_LEN, INPUT_TYPE, DATA_TYPE, NEW_NAME_LEN, NEW_INPUT_TYPE
       INTEGER YEAR, DOY, MONTH, DAY, HOUR, MINUTE, SECOND, NDOY, ODOY, MSEC
       INTEGER MICRO, STATUS
       REAL*8 OLD_EPOCH, SR_EPOCH, EDB0_EPOCH, EPOCH, NEWFILE_EPOCH
       LOGICAL SPAN_MIDNIGHT
       CHARACTER*80 NEW_NAME
       CHARACTER*(*) FILENAME
C
C Get time corresponding to the previous science record, in ordinary units.
C
       CALL SOHO_EPOCH_TO_GREG(OLD_EPOCH, YEAR, ODOY, HOUR, MINUTE, SECOND,
     1			       MSEC, MICRO)
C
C If the time corresponding to the current science record is bad, use the
C EDB(0) time instead.
C
       IF(SR_EPOCH .NE. 0.)THEN
         EPOCH = SR_EPOCH
       ELSE
         EPOCH = EDB0_EPOCH
       ENDIF
C
C Get time corresponding to the current science record, in ordinary units.
C
       CALL SOHO_EPOCH_TO_GREG(EPOCH, YEAR, NDOY, HOUR, MINUTE, SECOND, MSEC,
     1			       MICRO)
C
C Check to see if the day number has increased by one...
C
       IF ((NDOY - ODOY) .EQ. 1) THEN
C
C If it has, then get the filename corresponding to the start of the next day.
C This will be an LZ filename if an LZ file is available for that day.
C
C First construct a time corresponding to the start of the next day
C Actually, this has already been done.  The variable EPOCH contains the time
C corresponding to the next day, and YEAR and NDOY are already set to the year
C and day of year for that date.  Convert to month and day and use those to
C call CONSTRUCT_FILENAME, passing it zeros for the time.
C
         CALL CNVRT_YRDY_TO_YRMNDY( YEAR, NDOY, MONTH, DAY )
C
C Now construct a filename based on this date, using a time of 0:0:0.
C Tell CONSTRUCT_FILENAME to suppress the "Next file..." message; we'll print
C it ourselves if needed.
C
         CALL CONSTRUCT_FILENAME( DATA_TYPE, YEAR, MONTH, DAY, 0, 0, 0,
     1			          0, NEW_NAME, NEW_NAME_LEN, NEW_INPUT_TYPE, 
     1			          STATUS )
C
C Now check if the new name returned by CONSTRUCT_FILENAME is the same as the
C old name or if there does not exist any file on the following day.  This will
C happen if there is no LZ file on the next day and the file we are processing
C is the last REL/QKL file on its day.  In this case we set the SPAN_MIDNIGHT
C flag .FALSE. and allow proccssing to continue.  When the current file is
C completely processed CONSTRUCT_NEXT_FILENAME will be called and will
C automatically advance to the first REL/QKL on the next day. 
C
         IF((NEW_NAME(1:NEW_NAME_LEN) .EQ. FILENAME(1:NAME_LEN)) .OR. (STATUS .EQ. -1))THEN
           SPAN_MIDNIGHT = .FALSE.
         ELSE
C
C The names are different; test if the filename just returned is an LZ file.
C
           IF(INDEX(NEW_NAME(1:NEW_NAME_LEN), 'LZ') .EQ. 0)THEN
C
C If the name is different, and does not contain the letters 'LZ', we have TWO
C (or more!) files that potentially span midnight!  Since there is no LZ for
C the next day in this case either, treat the same as above: set SPAN_MIDNIGHT
C to .FALSE.  This ensures that the current file (still stored in FILENAME) is
C completely processed.  The next file will be opened and processed in its
C turn, and so on until we are processing the last REL/QKL of that day.  At
C that point, if that file spans midnight, we will be called again, and the
C above logic will detect this case and handle it.  
C
             SPAN_MIDNIGHT = .FALSE.
           ELSE
C
C If the name is different, and contains the letters 'LZ', then we have found a
C LZ file when we were reading REL/QKL files.  Reset the file name argument and
C its associated parameters, and set the flag SPAN_MIDNIGHT true so that the
C calling program will know to stop processing that file, close it, and open
C the LZ file.  Since CONSTRUCT_NEXT_FILENAME won't be called by SELECT_FILE in
C this case, write a message to the output stream for the user to see.
C
             FILENAME(1:NAME_LEN) = NEW_NAME(1:NEW_NAME_LEN)
             NAME_LEN = NEW_NAME_LEN
             INPUT_TYPE = NEW_INPUT_TYPE
             SPAN_MIDNIGHT = .TRUE.
	     WRITE(6,350) FILENAME(1:NAME_LEN)
350	     FORMAT(' Next input file is ',A)
           ENDIF
         ENDIF
       ENDIF
C
       RETURN
       END
