CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      SUBROUTINE TABLE_PM_SR(UNIT, SR, DONE, STOP_EPOCH, WHO_CALLED)
C+
C
C FUNCTIONAL DESCRIPTION:
C
C     This subroutine accumulates and periodly dumps PM Rates and PM E/Q from
C     the science record SR.  It calls the routine TABLE_PM_EDB to perform
C     the accumulation and dumping.  This differs from DUMP_SR in that it calls
C     uses TABLE_PM_EDB, not DUMP_EDB.  Dumps in column format suitable for use
C     by Kaleidagraph.  
C
C FORMAL ARGUMENTS:
C
C     UNIT:
C	I*4	  I	The Fortran unit number to which the averaged values
C			will be written.
C
C     SR:
C	/SCI_REC/  I	A record of structure type /SCI_REC/ containing the
C			science record to be processed.
C
C     DONE:
C	L*4	  I/O	Upon entry will be true if the user specified stop
C			time has been passed.  If the user specified stop time
C			is passed while processing the science record, DONE
C			will be set to true and this routine will return.
C
C     STOP_EPOCH:
C	R*8	  I	The user specified stop time in Epoch format.
C
C     WHO_CALLED:
C	I*4	  I	An integer informing this routine which application
C			is calling it.  Allowable values are 1 (PMRATES) and
C			2 (PMSW).
C
C INTERFACE:
!
!	INTERFACE
!	  SUBROUTINE TABLE_PM_SR(UNIT, SR, DONE, STOP_EPOCH, WHO_CALLED)
!	    INTEGER, INTENT(IN) :: UNIT, WHO_CALLED
!	    RECORD /SR/ SR
!	    INTENT(IN) SR
!	    LOGICAL, INTENT(INOUT) :: DONE
!	    REAL(KIND=8), INTENT(IN) :: STOP_EPOCH
!	  END SUBROUTINE TABLE_PM_SR
!	END INTERFACE
C
C IMPLICIT INPUTS:
C
C     Value of local variable COUNTER is used on subsequent calls.  Its value
C     is modified by the routine that processes the EDB.
C
C IMPLICIT OUTPUTS:
C
C     None
C
C SIDE EFFECTS:
C
C     See Implicit Inputs.
C
C COMMON BLOCKS:
C
C     FORM		Contains the format statement and control variables to
C			be used to write to the output file.
C
C INCLUDE FILES:
C
C     EDBDEF.INC	Contains definitions for the structure of an EDB.
C     FORM.INC		Contains definitions for the common block FORM.
C
C DESIGN:
C
C     This routine handles science record processing for both the PMRATES and
C     PMSW programs.  These two cases are distinguished by the value of the
C     argument WHO_CALLED.  Except for whether or not to write out a flag
C     record, the operations performed by this routine are identical for both
C     applications.
C
C     If we are called to only take care of a partial average (DONE true), the
C     record SR will first have been filled with zeroes.  The loop processes
C     all 20 EDBs.  The EDB processing code does nothing if the EDB is all
C     zero.  So effectively the loop does nothing if we are called with DONE
C     true. 
C
C     loop over all 20 EDBs
C	if start time of EDB exceeds the stop time and
C	 we are at the start of an averaging interval then
C	  set DONE flag
C	  if called from PMSW write out flag record
C	  return
C	endif
C       process the EDB
C     endloop
C     if DONE flag true and COUNTER > 1 (we have a partial sum) then
C       call averaging routine to average sum and write out partial average
C     endif
C     if DONE flag true and we were called from PMSW then write out flag record
C     return
C
C This version:   3.3    4-JAN-2000
C
C CREATED:
C
C	 2-JAN-1996    John Paquette
C
C DEVELOPMENT HISTORY:
C 
C  Author       Date		Description of Change 
C  --------	-----------	--------------------- 
C  J. PAQUETTE   2-JAN-1996     Created(from TABLE_MTOF_SR)
C  J. PAQUETTE  30-JAN-1996     Check quality flags before calling TABLE_PM_EDB
C  J. PAQUETTE   7-FEB-1996     Corrected error in quality checking; added code to output
C                               final partial average and use start and stop times
C  J. PAQUETTE  17-MAY-1996     Added flag WHO_CALLED. This is passed to TABLE_PM_EDB
C  J. PAQUETTE   5-AUG-1996     Call TABLE_PM_EDB even if the quality flag for the relevant 
C                               EDB is bad; the subroutine will detect the bad QF and act 
C                               accordingly
C  J. PAQUETTE  26-AUG-1996     Write a line containing the end of the last averaging interval, 
C                               with a set of -2's as flags
C  J. PAQUETTE   7-JAN-1997     Amend line containing -2's to include new entries
C  J. PAQUETTE  29-JAN-1997     Change over to TABLE_PM_SR_A; only difference is calls
C                               TABLE_PM_EDB_A, PARTIAL_AVERAGE_PM_A
C  J. PAQUETTE   3-FEB-1997     Amend line containing -2's to include new entries
C  L. BLEAU	 5-FEB-1997 2.2	remove format 166, use runtime format; add
C				common block FORM
C  J. PAQUETTE   5-FEB-1997 2.3 Amend line containing -2's to include new entries
C  L. BLEAU      5-FEB-1997 2.4 remove common FORM declaraction, add INCLUDE FORM.INC
C  L. BLEAU	 6-FEB-1997 2.5	remove flag writes, add call to WRITE_FLAG_RECORD
C  J. PAQUETTE  24-JUL-1997     Change back to TABLE_PM_SR; only difference is calls
C                               TABLE_PM_EDB, PARTIAL_AVERAGE_PM
C  J. PAQUETTE   8-DEC-1997     Check on WHO_CALLED for flag record output
C  L. Bleau	20-SEP-1999 3.2	remove call to PARTIAL_AVERAGE_PM, replace with
C				call to TABLE_PM_EDB_AVERAGE, simplify logic
C				that decides if flag record should be written
C 
C  SEL      	31-DEC-1999	fixed bug in processing y2k data - changed
C                      		IF(YEAR.GT.100)YEAR =YEAR - 100 to
C                      		IF(YEAR.GE.100)YEAR =YEAR - 100
C  L. Bleau	 4-JAN-2000 3.3	fix y2k bug, replace .GT. with MOD
C-
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CDEC$ IDENT	'3.3'
      INCLUDE 'INC_DIR:EDBDEF.INC'
      INTEGER*4 UNIT, WHO_CALLED
      LOGICAL DONE
      REAL*8 STOP_EPOCH
      RECORD /SCI_REC/ SR
C
C Local variables
C
      INTEGER*4 EDB_CNTR, COUNTER/0/, YEAR, DOY, HR, MIN, SEC, MSEC, MICRO
      REAL*8 LAST_EPOCH
      STATIC LAST_EPOCH, COUNTER
      INCLUDE 'INC_DIR:FORM.INC'
C
C There are 20 EDB's per Science Record, numbered 0-19. Loop accordingly.
C
      DO 100 EDB_CNTR = 0, 19
C
C If the start time of an average exceeds the stop time, then set a flag
C and RETURN.  We are at the start of an average if COUNTER = 0.
C
        IF ((SR.EDB(EDB_CNTR).EPOCH .GE. STOP_EPOCH) .AND. (COUNTER .EQ. 0)) THEN
          DONE =.TRUE.
C
C Write out last time used, with -2's as flag values.
C
          CALL SOHO_EPOCH_TO_GREG(LAST_EPOCH, YEAR, DOY, HR, MIN, SEC, MSEC, MICRO)
          IF (YEAR .GT. 1900) YEAR = MOD(YEAR,100)
          IF (WHO_CALLED .EQ. 2) CALL WRITE_FLAG_RECORD(UNIT, YEAR, DOY, HR, MIN, SEC, -2.)
C
          RETURN
        ENDIF
C                                                       
C Otherwise, process one EDB
C
        CALL TABLE_PM_EDB(UNIT,EDB_CNTR,SR.EDB(EDB_CNTR),COUNTER,WHO_CALLED,LAST_EPOCH)
 100  CONTINUE
C
      IF ((DONE) .AND. (COUNTER .NE. 0).AND.(COUNTER .NE. 1)) THEN
C
C We get here if this routine was called with DONE = .TRUE. and COUNTER is not
C 0 or 1; ie, is 2 or greater.  If these conditions exist, then we have an
C incomplete averaging interval to print out. 
C This used to be done by PARTIAL_AVERAGE_PM.  This has been replaced by
C TABLE_PM_EDB_AVERAGE, so call the latter routine directly.  Also write
C out flag line if we are PMSW.
C
	CALL TABLE_PM_EDB_AVERAGE( .TRUE., UNIT, WHO_CALLED, LAST_EPOCH )
      ENDIF
C
C Write out last time used, with -2's as flag values for PMSW.
C Do this regardless of whether or not there was an incomplete averaging
C interval.
C
      IF (DONE .AND. WHO_CALLED .EQ. 2) THEN
        CALL SOHO_EPOCH_TO_GREG(LAST_EPOCH, YEAR, DOY, HR, MIN, SEC, MSEC, MICRO)
        IF (YEAR .GT. 1900) YEAR = YEAR - 1900
        IF (YEAR .GE. 100) YEAR = YEAR - 100
        CALL WRITE_FLAG_RECORD(UNIT, YEAR, DOY, HR, MIN, SEC, -2.)
      ENDIF
C
      RETURN
      END
