FUNCTION WLFCS, CHAN,ADDRSS,TEMPXL  ; Convert FCS address to wavelength
    common  lat_space,a2d       ; Return 2d spacing in common block

    stpcon = 2.746582D-3        ; Number of degrees per step
    rad    = 1.745329D-2        ; Number of radians per degree

;   X-TAL position for zero addresses:
    zero   = [10215.79, 14561.0 , 15313.48, 12361.26,   $
          11452.07, 12751.73, 13027.83] 

;   2d spacing info from User's guide
;   20C values - B.J.K. 5 FEB 1979

    A2D0   = [26.627828D0,15.962566D0,10.641419D0,  $
          8.50970D0,6.68628D0,4.0004D0,2.3096D0]

    temp0  = 20.            ; T0 = 20. C
    alpha  = [40.5e-6,1.5e-6,17.e-6,13.37e-6,11.4e-6,5.95e-6,5.95e-6]

;   `A' contains polynomial coeficients for refractive index correction:

    A   = [[-2.760297D-03, 7.102640D-04,-6.625707D-05,  $ ;KAP
         2.955649D-06,-4.832172D-08],           $
           [-6.024388D-04, 2.147863D-04,-2.564263D-05,  $ ;BERYL
         1.744025D-06,-3.983562D-08],           $
           [-2.582597D-05, 5.354728D-06, 2.275505D-06,0.,0.], $ ;ADP
           [-1.021262D-02, 7.342293D-03,-1.973360D-03,  $ ;Q1010
         2.364267D-04,-1.060451D-05],           $ ; RANGE  500 -  5778
           [-5.33324756D+01, 3.25260860D+01,-7.43876317D+00,$ ;Q1010
         7.56111640D-01,-2.88204825D-02],       $ ;RANGE 5779 -  6655
           [-1.314206D+00, 7.251885D-01,-1.500088D-01,  $ Q1010
         1.378694D-02,-4.749874D-04],           $ ;RANGE 6656 - 11000
           [ 3.885422D-05,-2.998710D-05, 1.139741D-05,  $ ;Q1011
        -6.734982D-07, 0.0],                $
           [ 1.016147D-04,-1.409090D-04, 7.844231D-05,  $ ;Ge 220
        -1.634191D-05,1.37168D-06],         $
           [ 1.649106D-04,-3.974733D-04, 3.617007D-04,  $ ;Ge 440
        -1.401797D-04,2.055069D-05 ]]


;   Make sure that chan is a valid FCS channel number

    if (chan lt 1) or (chan gt 7) then begin
       print,'   Error in WLFCS:   CHAN out of limits:',chan
       return, undefined        ; Return undefined value
    endif


;   See if tempxl has been passed in.

    if n_elements( Tempxl ) eq 0 then tempxx = temp0 else begin
       tempxx = tempxl
;      print,'  WLFCS: Using xtal temp = ',tempxx,'C for 2d'
    endelse

; ***
;-- Calculate the temperature dependent 2d spacing

    a2d = A2D0(chan-1)*(1+ALPHA(chan-1)*(TEMPXX-TEMP0))

;-- Calculate angle corresponding to address for drive B

    xpos  = zero(chan-1) + addrss           ; x-tal position
    angle = xpos * stpcon * rad

;-- Calculate 0th order wavelength from Bragg formula

    tempwl= a2d * sin(angle)            ; Bragg relation
                            ; Temporary WaveLength

;-- Get m - index of array containing 4'th order polynomial 
;   approx for refractive index correction

; ***
    m = replicate(chan-1,n_elements(addrss))

;   Channel 4 has been broken into 3 wavelength regions:
    if chan eq 4  then begin            ; Q1010
      x_addr = fltarr(1)                ; Convert x_addr
      var = size(addrss)                ;-  to vector if nec.
      if var(0) eq 0 then x_addr(0) = addrss else x_addr = addrss
      vv = where( x_addr ge 5779 )
      if !err gt 0 then m(vv) = 4
      vv = where( x_addr ge 6656 )
      if !err gt 0 then m(vv) = 5
    endif else if chan ge 5 then m = m + 2      ; not q1010
          

;-- Calculate the coefficients for refractive index correction

    a2x = a(1+5*m) * tempwl             ; Refractive index
    a3x = a(2+5*m) * tempwl^2           ; coefficients
    a4x = a(3+5*m) * tempwl^3
    a5x = a(4+5*m) * tempwl^4

    del = a(5*m)+a2x+a3x+a4x+a5x

;--     Return wavelength as float array

    return, float(tempwl*(1.0-del/(sin(angle)^2)))

    end