      SUBROUTINE LE_GAIN_TIME
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine LE_GAIN_TIME
C.LIBRARY:        LECSLIB
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 28 Dec 98 - based on ME_GAIN_TIME 1.0
C.PURPOSE:        load gain history for LECS gain vs time correction
C.METHOD:         read file created by LECSGAINACCUM 
C
C.SYNTAX:         called only by INIT_CORRECT_LE
C.PARAMETERS:     none
C.RESTRICTIONS:
C.NOTES:          
C.FILES:
C.REFERENCES
C
C----------------------------------------------------------------------
C      
       INTEGER MEUNIT,I,TRUE_LENGTH,J,LU,IZOFF,IVOS,ISYS,IERR,N
       INTEGER START70,DAY70
       LOGICAL VOSERROR
       REAL R,DUM,G,EXTRPD,TOLERANCE
       DOUBLE PRECISION TJ,T0,UDOUBLE,TS,TE,OLDEND
       CHARACTER*80  BUFFER,BUFFER2
       CHARACTER VAL*4
       INCLUDE 'accumcommon.inc'
       INCLUDE 'opcommon.inc'
       INCLUDE 'mecommon.inc'
       INCLUDE 'timecommon.inc'
       INCLUDE 'hcommon.inc'
       INCLUDE 'bincommon.inc'
       INCLUDE 'wcommon.inc'
       PARAMETER (TOLERANCE=60.)
C
C     is gain correction requested ? as default assume not
      T_HERE=ACCUMCOMMON_TL
      T_NEXT=ACCUMCOMMON_TH
      GAIN_N=1
      GAIN_PTR=1
      GAIN(1)=1.0
      IOF4=0
      IOF5=0
      CALL GET_GLOBAL_DEFAULT('GAINHISTORY',' ',BUFFER)
      GAINFILE=BUFFER
      IF(GAINFILE.EQ.' ')RETURN
C
C     build gain history file name
C-->  code borrowed from BUILDWINDOWNAME in MERGEWINDOW
      BUFFER2=BUFFER
      CALL DEPATH_1(BUFFER2)
C     search must be with true substring !!
      I=TRUE_LENGTH(BUFFER2)
      J=INDEX(BUFFER,BUFFER2(1:I))
      I=INDEX(BUFFER2,'.')
      IF(I.EQ.0)THEN
         I=TRUE_LENGTH(BUFFER2)
         BUFFER2(I+1:)='.time'
      ENDIF
      BUFFER(J:)=BUFFER2
      CALL BUILDPATH(BUFFER,'DATA',GAINFILE)
C
C     open gain history file
      CALL OPEN_TIME (3,GAINFILE ,I,GAIN_N,J,IZOFF)
      IF (VOSERROR(IVOS,ISYS))GOTO 999
C     the file shall be for LECS
      CALL H_READ_KEYWORD('INSTRUME',VAL,'none',IERR)
      CALL UPCASE(VAL)
      IF(VAL.NE.'LECS')GOTO 998
C
C     allocate memory and load time windows
      N=2*GAIN_N
      CALL Z_ALLOC(N,4,GAIN,  I,IOF4) 
      CALL Z_ALLOC(N,8,GAIN_T,I,IOF5)  
      IF (VOSERROR(IVOS,ISYS))GOTO 996
C
C     prepare constants for time conversion to OBT
      T0=UDOUBLE(STARTOBT)*TIMECOMMON_SCCONVERSION
C-->  this takes account also of any day shift ?
      CALL H_READ_JKEYWORD('TIMEREF',DAY70,0,IERR,I) 
      CALL TIME_1970(STARTARR,START70)
      TJ=START70-DAY70
C
C     read file, assuming fixed structure
      N=0
C     this will determine gains for a CONTINUOUS coverage of time axis
C     i.e. the values in the gain history for the intervals covered by it
C     and the interpolation for the gaps in the gain history
      DO 1 I=1,GAIN_N
      CALL READ_BIN(3,I,IZOFF,TS,R,DUM,
     . G,DUM,DUM,DUM)
C     convert times to elapsed OBT
      TS=(TS-TJ)*TIMECOMMON_SCALE
      TS=TS+T0
      TE=TS+R*TIMECOMMON_SCALE
*     write(*,*)' input bin ',I,TS,TE,G
      IF(I.EQ.1)THEN
C        first bin is first bin
         N=1
         IF(TIMELO.LT.TS-TOLERANCE)GOTO 990
         GAIN_T(N+IOF5)=(TS+TE)/2.
C        invert relative gain into correcting factor
         GAIN(N+IOF4)=1./G
*        write(*,*)' output bin ',N,GAIN_T(N+IOF5),GAIN(N+IOF4)
      ELSE
C        other bins can be doubled in a gap and a bin
         IF(TS.GT.OLDEND)THEN
C          there is a gap
           N=N+1
           GAIN_T(N+IOF5)=(OLDEND+TS)/2.
           GAIN(N+IOF4)=EXTRPD(GAIN(N-1+IOF4),1./G,
     .                  GAIN_T(N-1+IOF5),(TS+TE)/2.,GAIN_T(N+IOF5))
*          write(*,*)' output gap ',N,GAIN_T(N+IOF5),GAIN(N+IOF4)
         ENDIF
C        followed by a bin
         N=N+1
         GAIN_T(N+IOF5)=(TS+TE)/2.
         GAIN(N+IOF4)=1./G
*        write(*,*)' output bin ',N,GAIN_T(N+IOF5),GAIN(N+IOF4)
      ENDIF
      OLDEND=TE
 1    CONTINUE
      GAIN_N=N
      CALL RESET_TABLE_DESC(HCOMMON_CURFILE)
      CALL CLOSE_XAS_FILE(3)
      WRITE(*,*)' Loaded gain history with ',GAIN_N,' points'
      T_HERE=GAIN_T(1+IOF5)
      T_NEXT=GAIN_T(2+IOF5)
      G_HERE=GAIN(1+IOF4)
      G_NEXT=GAIN(2+IOF4)
      RETURN
C
C..... errors ...............................................
C
990   CONTINUE
      WRITE(*,*)' start time before gain history starts ! abort !'
      CALL Z_EXIT(99)
996   CONTINUE
      WRITE(*,*)' memory allocation error : VOS ',IVOS,' sys ',ISYS
      GOTO 1000
998   CONTINUE
      WRITE(*,*)' gain history file not for LECS but for ',VAL
      GOTO 1000
 999  CONTINUE
      WRITE(*,*)' gain history file error : VOS ',IVOS,' sys ',ISYS
1000  CONTINUE
      CALL RESET_TABLE_DESC(HCOMMON_CURFILE)
      CALL CLOSE_XAS_FILE(LU)
      WRITE(*,*)' gain vs time correction DISABLED '
      GAINFILE=' '
      END
C
C      SUBROUTINE RESET_TABLE_DESC(NTAB)
C      this is in ME-GAIN_TIME so far
C      and will be relocated with it !
