function sxt_mwave,Te,filterN,date=date,version=version,noverbose=noverbose
;+
; NAME:
;  sxt_mwave
; PURPOSE:
;  Return the mean photon wavelength for a given SXT filter and 
;  assumed plasma temperature.
;
; CALLING SEQUENCE:
;  mwave = sxt_mwave(Te,filterN)	; mwave is in Angstroms
;  mwave = sxt_mwave(Te,index)		; mwave is in Angstroms
;  mwave = sxt_mwave(Te,filterN,date=date)
;
; INPUTS:
;  TE           = log10(Temp) (May be a vector: min = 5.5, max = 8.0)
;  FILTERN	= Filter B value (range is 1 to 6) or SXT index structure.
;                 Note:  If an SXT index structure is supplied, then this will
;                 be used to specify the filter number and DATE.  The Date
;                 will be overridden if the DATE=DATE keyword is supplied.
;
;  TE and FILTERN may be vectors, but their lengths must match:
;		  N_elements(TE)	N_elements(FILTERN)
;			1			1
;  These are the 	N			1
;    valid options:	1			N
;			N			N
; OUTPUTS:
;   This function returns the mean wavlength (Ang) given an assumed 
;   isothermal plasma temperature.
;
; OPTIONAL INPUT KEYWORDS:
;  date		= Time in any format (including structure). Used to
;		  determine entrance filter transmission. 
;		  Overrides date specified by FILTERN (index).
;  noverbose	= If set, suppress some of the informational messages
;
; OPTIONAL OUTPUT KEYWORDS:
;  version	= version number of SXT mean wavelength data base file.
;
; PROCEDURE:
;  The mean wavelength is determined by folding an isothermal spectrum
;  through the SXT response function.  The mean wavelength of the emission
;  that is integrated in the SXT CCD is computed and saved in a file.  The
;  routine that calculates the SXT response function files (sre*genx) is
;  also used to compute the mean wavelength data base.  Because the hole
;  in the entrance filter affected the SXT response function, the mean 
;  is a function of the entrance filter transmission.  Therefore, the
;  DATE keyword is necessary to specify the time of interest.  If an SXT
;  index is passed in, then the date will be taken from the index.
;
; RESTRICTIONS:
;  DATE must be of length 1.  If the date is obtained from an SXT 
;  INDEX, the first time in the structure  will be used to determine 
;  the entrance filter fraction.
;
;  If the dates of interest span the time of 13-nov-92 16:50 UT, then 
;  separate calls must be made for data before and after this date.
;
; MODIFICATION HISTORY:
;  21-apr-95, J. R. Lemen, Written.
;  24-nov-95, JRL, Renamed the "filter" variable to "filterN" to avoid a 
;		   conflict with the filter function that is on-line.
;-

common sxt_mwave_db, mwave_db, text, header, version1, Filein

; ------------------------
;  HARD-WIRED Assumptions:
; ------------------------

label_id    = [1, 2, 3, 6, 5, 4]		; HARD-WIRED ***
thick_order = [1, 2, 3, 6, 5, 4]		; HARD-WIRED ***
interp=0					; HARD-WIRED *** Linear interpolation

; if no parameters are present, assume information mode

if n_params() lt 2 then begin
  doc_library,'sxt_mwave'
  print,format="(75('-'))"
  fastdoc,'sxt_mwave',/summ			; Give the full parameter list
  print,format="(75('-'))"
  return,-1
endif

;----------------------------------------------------------------------------
;  ****  Step 1:  Set up default values of optional input parameter:
;----------------------------------------------------------------------------

D_date = get_yo_dates(/launch)		; Def = Launch date

;----------------------------------------------------------------------------
;  ****  Step 2:  Read the Data Base File
;----------------------------------------------------------------------------
; Read data from file only for first time call to this function.
; Save the data in common block sxt_mwave_db.

if(n_elements(mwave_db) eq 0) then begin		; srw = Mean wavelength vs Te
   filein = findfile(concat_dir('$DIR_SXT_SENSITIVE','srw*'))
   filein = filein(n_elements(filein)-1)		; Use latest version
   print,'Reading data base file = ',filein
   restgen, str=mwave_db,file=filein, text=text, header=header

   break_file, filein, disk, dir, filnam
   version1   = strmid(filnam,strpos(filnam,'srw')+3,strlen(filnam))
endif                               
version = version1			; Return to the caller

;----------------------------------------------------------------------------
;  ****  Step 3:  Perform Checks on Input Data
;----------------------------------------------------------------------------

; *****
; determine type of input for filterN
; *****
if (n_elements(Te) eq 0) or (n_elements(filterN) eq 0) then begin
   print,' **** Error in SXT_mwave (2)******************************' & tbeep
   print,'sxt_mwave:  *** Te or FilterN is not defined '
   help,Te,FilterN
   print,' *********************************************************'
   return,-1
endif

sz1=size(Te)				; check args 1 and 2
sz2=size(filterN)

if (sz2(sz2(0)+1) eq 8) then begin	; Yes = filterN = index
   j_filter = gt_filtb(filterN)
   j_date   = anytim2ints(filterN(0))	; Get the first date
   j_t1     = gt_expdur(filterN)
endif else j_filter = filterN

if (min(j_filter) lt 1) or (max(j_filter) gt 6) then begin
   print,' **** Error in SXT_mwave (3)********************************' & tbeep
   print,'      Valid range for filterN is between 1 and 6 (inclusive)'
   print,' ***********************************************************'
   return,-1
endif

; *****
; The next section of code ensures that the lengths of Te, FilterN match.
; *****

J_Te = Te	; Save the original value of Te
NTe = N_elements(J_Te) & arg_problem = 0		; Assume no problem
NFt = N_elements(J_filter)

aa = [NTe,NFt]
if n_elements(uniq(aa(sort(aa)))) gt 2 then arg_problem=1 else begin

  if n_elements(uniq(aa(sort(aa)))) eq 2 then begin
     MaxN = max(aa)
     MinN = min(aa)
     if MinN ne 1 then arg_problem=1 else begin
     
        if NTe ne MaxN then J_Te     = replicate(J_Te(0), MaxN)
        if NFt ne MaxN then J_filter = replicate(J_filter(0), MaxN)
     endelse
  endif
endelse					; n_elements(uniq(aa(sort(aa)))) ...

if arg_problem then begin
  print,' **** Error in SXT_mwave (4)******************************' & tbeep
  print, '      Length of FilterN must match length of Te '
  print, '      -- OR FilterN must have a length of 1'
  print,' *********************************************************'
  return,-1
endif

mwave = J_Te * 0.				; Set up the output array

; *****
; Decide which frac_open dataset to use:
; *****

  if n_elements(date) ne 0 then j_date = anytim2ints(date)
  if n_elements(j_date) eq 0 then j_date = d_date
  if n_elements(j_date) gt 1 then begin
    print,' **** Warning in SXT_mwave (1)*******************************' & tbeep
    print,'      Date must be a scalar'
    j_date = j_date(0)

    print,'      Will use the first value = ',fmt_tim(j_date)
  endif

 tt0 = int2secarr(mwave_db.dates)
 tt1 = int2secarr(j_date,mwave_db.dates(0))

 jj = n_elements(mwave_db.dates)-1
 for i=0,n_elements(mwave_db.dates)-2 do begin
    if (tt1 ge tt0(i)) and (tt1 lt tt0(i+1)) then jj = i
 endfor
 if (total(addtime(mwave_db.dates,diff=get_yo_dates(/entr))) ne 0) or $
    (n_elements(mwave_db.dates) ne n_elements(get_yo_dates(/entr))) then begin 
    tbeep,5
    message,'The mean wavelength database file may need to be updated:',/cont
    message,'The file = '+filein,/cont
    print,' ---------------'
    print,'The reference times in the database file are:',format='(t10,a)'
    print,fmt_tim(mwave_db.dates),format='(t18,a)'
    print,' ---------------'
    print,'The reference times from get_yo_dates are (/entrance):',format='(t10,a)'
    print,fmt_tim(get_yo_dates(/entrance)),format='(t18,a)'
    print,' ---------------'
    print,'The mwave data that is returned may be in error!',format='(t13,a)'
    print,'Proceed at your own risk!',format='(t13,a)'
    ans='' & read,'* Enter <cr> to continue: ',ans
 endif

; *****
; Set up the data base vectors
; *****

te_db = reform(mwave_db.Te)			; temperature array in database

filt1_col = label_id([J_filter-1])-1		; column number in database 
filt1_unq = filt1_col(uniq(filt1_col,sort(filt1_col)))	; Get unique cases

;----------------------------------------------------------------------------
;  ****  Step 4:  Interpolate for the mean wavelengths
;----------------------------------------------------------------------------

Tmin = min(te_db) & Tmax = max(te_db)
if not keyword_set(noverbose) then begin
  if (min(J_Te) lt Tmin) or (max(J_Te) gt Tmax) then begin
    print,' **** Warning in SXT_mwave (4)******************************' & tbeep
    print,'      Valid range of Te must be between:',Tmin,' and',Tmax,format='(2(a,f5.2))'
    print,'      A Value of 0 is returned for out of range values'
  endif
endif

for i=0,n_elements(filt1_unq)-1 do begin

  i1_db = transpose(mwave_db.mwave(*,filt1_unq(i),jj))

  ii = where((filt1_col eq filt1_unq(i)) and (J_Te ge Tmin) and (J_Te le Tmax),npts)
  if npts gt 0 then mwave(ii) = dspline(Te_db, i1_db, J_Te(ii),interp=interp)
endfor

return,mwave
end




