function offset_bias,telescope,readport,date,sum=sum
;
;+
; NAME:			OFFSET_BIAS
;
; PURPOSE:		Provides the electronic offset introduced for
;			each readout port
;
; CATEGORY:		LASCO Calibration
;
; CALLING SEQUENCE:	Offset = OFFSET_BIAS(Telescope,Readport)
;
; INPUTS:		Telescope = String indicating telescope
;				Values are 'C1','C2','C3','EIT'
;			Readport = String indicating read out port
;				Values are 'A','B','C','D'
;
; OPTIONAL INPUTS:	Date= string giving the date as YYMMDD
;
; KEYWORD PARAMETERS:	SUM:	If present, computes the proper bias for LEB Summing
;
; OUTPUTS:		Integer giving the offset bias in DN
;
; OPTIONAL OUTPUT PARAMETERS:
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:		Obtained from flight calibration
;
; MODIFICATION HISTORY:
;			RA Howard Writen 6 Feb 1996
;      V1    RAH   02/06/96     Initial Release
;      V2    RAH   06/02/97     Added function of date to C3
;      V3    RAH   06/10/97     Added function of date to C1 & C2
;      V4    RAH   08/21/97     Added correction for leb summing
;      V5    RAH   06/08/98     Updates to C2 and C3 coefficients for port C
;      V6    RAH   06/10/98     Updates to C2 and C3 coefficients for port C, QL through 5/98
;      V7    RAH   09/18/98     Updates to C2 and C3 coefficients for port C, LZ through 6/21/98
;      V8    NBR   10/21/98	Use lasco_ftshdr2struct, not ftshdr2struct
;      V9    RAH   08/20/99     Updates to C2 and C3 coefficients for port C
;      V10   RAH   12/21/99     Updates to C2 and C3 coefficients for port C
;      V11   RAH   01/21/00     Syntax change from ENDIF to END for IDL 5.3
;      V12   RAH   07/03/00     Updates to C2 and C3 coefficients for port C
;	NBR, 08/04/00 - Use SCCS version number; Add HISTORY to header if FITS header
;      V13   RAH   01/06/01     Updates to C2 and C3 coefficients for port C, split c2 before/after mispoint
;      V14   RAH   12/13/01     Updates to C2 and C3 coefficients for port C
;      V15   RAH   07/09/02     Updates to C2 and C3 coefficients for port C
;      V16   RAH   05/11/05     Updates to C2 and C3 coefficients for port C
;      V17   RAH   06/24/08     Updates to C2 and C3 coefficients for port C
;      V18   RAH   02/27/09     Updates to C2 and C3 coefficients for port C
;      V19   RAH   02/19/10     Updates to C2 and C3 coefficients for port C since 22 Nov 2008
;      V19   RAH   09/01/11     Updates to C2 and C3 coefficients for port C
;      V20   RAH   03/07/13     Updates to C2 and C3 coefficients for port C same start date as previous for C2 (9/1/11)
;					but C3 had a 5 DN jump on 8/31/2012
;      V21   RAH   07/05/13	Updates to C2 and C3 for port C
;      V22   RAH   05/09/14	Updates to C2 and C3 for port C
;      V23   RAH   12/10/14	Updates to C2 and C3 for port C  ; only use 0 sec darks
;      V24   RAH   11/04/15     Updates to C2 and C3 for port C
;      V25   RAH   12/27/16	Updates to C2 and C3 for port C
;      V26   RAH   10/11/18	Updates to C2 and C3 for port C
;      V27   RAH   10/22/20     Updates to C2 and C3 for port C
;
;-
;      W    H
ver= '%W%, %H%' ;LASCO IDL LIBRARY
version = STRMID(ver,4,strlen(ver))


np =  N_PARAMS()
CASE np OF
   1: BEGIN
        hdr = telescope
        IF (DATATYPE(hdr) NE 'STC') THEN  hdr = LASCO_FITSHDR2STRUCT(hdr)
	   
	
        port = STRUPCASE(STRTRIM(hdr.readport,2))
        tel = STRUPCASE(STRTRIM(hdr.detector,2))
        mjd = hdr.mid_date
        time = hdr.mid_time*1000.
        IF (mjd EQ 0)  THEN BEGIN
           dte = STR2UTC(hdr.date_obs)
           mjd = dte.mjd
           IF (mjd EQ 0)  THEN BEGIN
              dte = STR2UTC(hdr.date)
              mjd = dte.mjd
              IF (mjd EQ 0) THEN mjd=50083
           ENDIF
        ENDIF
      END
   3: BEGIN
        dte = YYMMDD2UTC(date)
        mjd = dte.mjd
        port = STRUPCASE(STRTRIM(readport,2))
        tel = STRUPCASE(STRTRIM(telescope,2))
        time = 0
      END
else: BEGIN
        port = STRUPCASE(STRTRIM(readport,2))
        tel = STRUPCASE(STRTRIM(telescope,2))
        IF (tel EQ 'C1') OR (tel EQ 'C3')  THEN BEGIN
           PRINT,'%WARNING:  OFFSET BIAS: Called with older parameters. Date used is base date'
        ENDIF
        mjd = 50083		; base date = 1/1/96
        time=0
      END
ENDCASE
CASE tel OF
   'C1':    CASE port OF
            'A':  b=364
            'B':  b=331
;            'C':  b=320
            'C':  BEGIN
                    del = (mjd-50395L)
                    bias = 351.958+30.739*(1-exp(-del/468.308)) 
;                    del = (mjd-50395L);*24.
;                    dt0 = '1996/11/08 09:08:08.569'
;                    t0  = anytim2utc(dt0)
;                    hours = (mjd - t0.mjd) + time/3.6e6
;                    bias = 351.86 + 0.0564*hours
;                    IF mjd GT t0.mjd THEN   $
;                      bias = 351.86 + 0.06879*hours - 5.94e-05*hours^2
                    b = ROUND(bias)
                  END
            'D':  b=522
            else: b=0
            ENDCASE
   'C2':    CASE port OF
            'A':  b=364
            'B':  b=540
;            'C':  b=470
            'C':  BEGIN
                     firstday = 50079L
                     IF (mjd LE 51057L)  THEN BEGIN
                        coeff = [470.97732,0.12792513,-3.6621933e-05];  before the mispoint
                     ENDIF
                     IF ((mjd GT 51057L) AND (mjd LT 51819L))  THEN BEGIN
                          coeff = [551.67277,0.091365902,-0.00012637790,7.4049597e-08]  ; since 1 Sep 98
                          firstday = 51099L
                     ENDIF
                     IF (mjd GE 51819L) AND (mjd LT 51915L) THEN BEGIN
                          coeff = [574.5788,0.019772032]  ; since 1 Oct 00
                          firstday = 51558L
                     ENDIF
                     IF (mjd GE 51915L) AND (mjd LT 54792L)  THEN BEGIN
;			   coeff = [581.48879,0.020564280,-3.1940216e-06]  ; sigma=.857, V16
;                          coeff = [581.77512,0.019351952,-2.3768227e-06]  ; sigma=.877, V17
                          coeff = [581.81517,0.019221920,-2.3110489e-06]  ;sigma=.808, V18
                          firstday = 51915L  ; since 1 Jun 2001
                     ENDIF
		     IF (mjd GE 54792L) AND (mjd LT 55044L)   THEN BEGIN
		          coeff = [617.70556,0.010290491,-6.0131545e-06]    ;sigma=0.10199634
			  firstday = 54792L	; since 22 Nov 2008
		     ENDIF
		     IF (mjd GE 55044L) AND (mjd LT 56450L)   THEN BEGIN
;		         coeff = [619.9746,0.005817418]	; sigma=0.14563050   9/1/11 version
;		         coeff = [619.98572,0.0060364606]	; sigma=0.20836720  3/7/2013
;		         coeff = [620.03527,0.0056439464,-6.7030760e-08 ]; sigma=0.208 7/5/2013
;			 coeff = [619.99836,.0058511051,-2.5122154e-7 ];sigma=.316 7/9/2014
			 coeff = [619.99733,.0059081617,-3.3932229e-7 ];sigma=.210 12/10/2014
			 firstday = 55044L	; since 1 Aug 2009
		     ENDIF
		     IF (mjd GE 56450L) AND (mjd LT 57388L)  THEN BEGIN
;			 coeff = [627.96366,.0047291577,-3.9630860e-7 ];sigma=.0492 11/4/2015
;			 firstday = 56520L	; since 16 Aug 2013
			 coeff = [627.61246,.0049003351,-5.3812001e-7 ];sigma=.0499 12/27/2016 
			 firstday = 56450L	; since 7 June 2013
		     ENDIF
		     IF (mjd GE 57388L)  AND (mjd LT 58571L)  THEN BEGIN   
			; since 1/1/2016 to 3/29/2019
			 coeff = [631.20515,0.0056815108,-1.3439596e-06]
;                       data fit from 9/25/2015 to 9/1/2017 with sigma=0.097687462 11/3/2020 
			 firstday = 57290L
		     ENDIF
;
;  An event occurred between 3/22/2019 and 3/29/2019 that caused a 20DN increase in
;  the bias which then over the course of several months declined by about 7DN 
;  where it plateaued for several months before it started a gradual increase.  
;
                     IF (mjd GE 58571L) AND (mjd LT 58802L) THEN BEGIN   ; 3/29/19 - 4/30/20 
			 coeff = [651.50189,-0.028926206,7.8531807e-05,-5.8964538e-08]
;                data fit from 3/29/2019-10/16/2020 with sigma=0.24150 11/3/2020 
			 firstday = 58578L	; 
		     ENDIF
                     IF (mjd GE 58802L) THEN BEGIN   ; since 5/1/19
			 coeff = [648.41904,-0.0020514176,1.8072963e-05] 
;                data fit from 3/29/2019-10/16/2020 with sigma=0..09849 11/10/2020 
			 firstday = 58802L
		     ENDIF
                     nc = N_ELEMENTS(coeff)
                     dd = mjd-firstday
                     b = POLY(dd,coeff)
                  END
		  
            'D':  b=526
            else: b=0
            ENDCASE
   'C3':    CASE port OF
            'A':  b=314
            'B':  b=346
;            'C'  b=319
            'C':  IF (mjd LT 50072)  THEN b=319 ELSE BEGIN
                     IF (mjd LE 51057L)  THEN BEGIN
                          coeff = [322.21639,0.011775379,4.4256968E-05,-3.167423e-08];  before the misspoint
                          firstday = 50072L
                     ENDIF
                     IF ((mjd GT 51057L) AND (mjd LE 51696L)) THEN BEGIN
                          coeff = [354.50857,0.062196067,-8.8114799e-05,5.0505447e-08]   ;  since 1 Sep 98
                          firstday = 51099L
                     ENDIF

                     IF (mjd GT 51696L) AND (mjd LT 51915L) THEN BEGIN
                          coeff = [369.02719,0.014994955,-4.0873204e-06]   ;  since 1 Jun 00
                          firstday = 51558L
                     ENDIF
                     IF (mjd GE 51915L) AND (mjd LT 54792L) THEN BEGIN
;                          coeff = [373.94213,0.011357537,-1.4658300e-06]  ;  sigma=.596, V16
;                          coeff = [374.08239,0.010817279,-1.1140635e-06]  ;  sigma=.572, V17
                          coeff = [374.11139,0.010731823,-1.0726207e-06]  ;  sigma=.563, V18
                          firstday = 51915L ;since 1 Jun 01
		     ENDIF
		     IF (mjd GE 54792L) AND (mjd LT 55044L) THEN BEGIN
		         coeff = [395.85091,0.0079344115,-6.2530780e-06]	; sigma=0.12318004
			 firstday = 54792L	; since 22 Nov 2008
		     ENDIF
		     IF (mjd GE 55044L) AND (mjd LT 56170L)   THEN BEGIN
		         coeff = [397.52040,0.0040765192]	; sigma=0.14563050
			 firstday = 55044L	; since 1 Aug 2009
		     ENDIF
		     IF (mjd GE 56170L) AND (mjd LT 56366L)   THEN BEGIN
		         coeff = [407.04606,-0.024819780,0.00011694347]	; sigma=0.15435573
			 firstday = 56170L	; since 31 Aug 2012 
		     ENDIF
		     IF (mjd GE 56360L) AND (mjd LT 56478L)   THEN BEGIN
;		         coeff = [406.49998,-0.0087161036,0.00021532949]	; sigma=0.19194478
			 coeff = [406.01009,.0046765547,-9.912626e-7];sigma=.21981034 5/9/14
			 firstday = 56360L	; since 9 Mar 2013 
		     ENDIF
		     IF (mjd GE 56478L) AND (mjd LT 56597L)  THEN BEGIN
		         coeff = [406.72179,0.0045780623,-2.3134855e-06] ; sigma=0.11770141
			 firstday = 56478L	; since 5 July 2013 
		     ENDIF
		     IF (mjd GE 56597L) AND (mjd LT 57174L)  THEN BEGIN
;		         coeff = [406.76696,0.0041403238,-1.7700061e-06] ; sigma=0.078128474
		         coeff = [406.77706,0.0040538719,-1.6571028e-06] ; sigma=0.074904945
			 firstday = 56478L	; since 1 Nov 2013 
		     ENDIF
		     IF (mjd GE 57024L)  THEN BEGIN         ;GE 6/1/2015
		         coeff = [408.38117,0.0027558157,-1.3694218e-07] ; sigma=0.19165  11/3/2020
			 firstday = 57024L	;  data fit from 1/2/2015 to 10/16/2020
		     ENDIF

                     nc = N_ELEMENTS(coeff)
                     dd = mjd-firstday
                     b = POLY(dd,coeff)
                  ENDELSE
            'D':  b=283
            else: b=0
            ENDCASE
   'EIT':   CASE port OF
            'A':  b=1017
            'B':  b=840
            'C':  b=1041
            'D':  b=844
            else: b=0
            ENDCASE
   else:    b=0
ENDCASE
IF (KEYWORD_SET(sum) AND (np EQ 1)) THEN BEGIN
   lebsum = (hdr.lebxsum>1) * (hdr.lebysum>1)
   b = b*lebsum
ENDIF

IF np EQ 1 THEN IF datatype(telescope) NE 'STC' THEN FXADDPAR,telescope,'HISTORY',version+', '+TRIM(STRING(b))
RETURN,b
END
