;+
; NAME:
;	CNVRT_TEMPS
;
; PURPOSE:
;	This function converts the raw telemetry value associated with a 
;	temperature monitor into degrees centigrade.
;
; CATEGORY:
;	PACKETS
;
; CALLING SEQUENCE:
;	Result = CNVRT_TEMPS (Dn_in, Temp_type)
;
; INPUTS:
;	Dn_in:	The raw telemetry value to be converted in DN (Integer)
;	Temp_type:	An integer parameter indicating the type of 
;			temperature monitor to be converted.
;			0 = camera cold finger
;			1 = camera on-chip to V
;			2 = zone thermistors (ice)
;			3 = COB thermistors (tce)
;			4 = s/c thermistors type 3
;			5 = s/c thermistors type 4
;			6 = s/c thermistors type 2
;			7 = s/c thermistors type 5
;
; OUTPUTS:
;	This function returns the converted temperature value as a floating
;	point number in units of degrees centigrade.
;
; PROCEDURE:
;	The appropriate calibration curve is applied to the telemetry monitor.
;
; EXAMPLE:
;	To obtain the temperature of a zone thermistor:
;
;		T = CNVRT_TEMPS (128,2)
;
; MODIFICATION HISTORY:
; 	Written by:	R.A. Howard, NRL, 1994
;
;       Jan 23, 1998    Ed Esfandiari - changed FOR loops counters to long.
;       Jan 27, 1998    Ed Esfandiari - Modified code to perform array 
;                                       operations for better efficiency.
;
;
; @(#)cnvrt_temps.pro	1.3 01/27/98 :LASCO IDL LIBRARY
;
;-


FUNCTION MAKE_CNVRT_ARR,dn_in,dn,deg

   n= n_elements(dn)-1
   interp_ind= intarr(n_elements(dn_in))

   for i=0,n-1 do begin
     if(dn(0) gt dn(1)) then $ ; descending order
       ind= where(dn_in le dn(i),cnt) $
     else $                    ; ascending order
       ind= where(dn_in ge dn(i),cnt)
     if(cnt gt 0) then interp_ind(ind)= i
   endfor
   RETURN, deg(interp_ind) + (deg(interp_ind+1) - deg(interp_ind)) * $
           (dn_in - dn(interp_ind)) / (dn(interp_ind+1) - dn(interp_ind)) 
END



FUNCTION CNVRT_TEMPS, dn_in, temp_type 
CASE temp_type OF

    0:  BEGIN
    ;
    ;    convert the camera cold finger temperature DN to degrees C  
    ;
       coeff = [287.756797,-194.144823, -3.837903, -1.318325, -0.109120,  $
                -0.393265, 0.146911, -0.111192, 0.028877, -0.029286, 0.015619]
       sz= size(dn_in)
       IF(sz(0) eq 0) THEN BEGIN  ; a single point.
         tarray=FLTARR(11);
         y = 0.0
         volt =  dn_in / 1000.
         if ( (volt GE .3241) AND (volt LE .9856)) THEN BEGIN
            x = ((2.0*volt-1.079381) / 0.919847 )
            tarray(0) = 1.0
            tarray(1) = x
            for i=2,10 do tarray(i) = 2.0*x*tarray(i-1) - tarray(i-2)
            y = 0.0
            for i=0,10 do y =  y+coeff(i)*tarray(i)
            y = y-273
         ENDIF
       ENDIF ELSE BEGIN ; an array - perform array operation
         dn_cnt= n_elements(dn_in)
         y = fltarr(dn_cnt)
         volt =  dn_in / 1000.
         val_dn= where(volt GE .3241 AND volt LE .9856, valid)
         if(valid gt 0) then begin
           x = ((2.0*volt(val_dn)-1.079381) / 0.919847 )
           tarray= FLTARR(11,valid)
           tarray(0,*)= 1.0
           tarray(1,*) = x
           for i=2,10 do tarray(i,*)= 2.0*x*tarray(i-1,*) - tarray(i-2,*) 
           for i=0,10 do y(val_dn)=  y(val_dn)+coeff(i)*tarray(i,*)
           y(val_dn)= y(val_dn)-273
         endif
       ENDELSE

       RETURN,y
    END
    
    1:  BEGIN
    ;
    ;    convert CCD Temperature 
    ;
       y =  dn_in * .01
       RETURN,y
    END
    
    2:  BEGIN
    ;
    ;       convert_to_ice_temp( ADN )
;	NOTE:  the cal curve for types 2&3 are organized differently
;		the rest of the curves, ie the DN is decreasing/increasing
;		respectively.
;
    
       deg = [-20.,-10.,0.,10.,20.,30.,40.,50.,60.,70.,80.,90.]
       dn  = [228.,198.,163.,127.,96.,70.,51.,36.,26.,19.,14.,10.]
       RETURN, MAKE_CNVRT_ARR(dn_in,dn,deg)
    END
    
    3:   BEGIN
    ;
    ;    convert_to_cpu_temp( ADN )
;	NOTE:  the cal curve for types 2&3 are organized differently
;		the rest of the curves, ie the DN is decreasing/increasing
;		respectively.
;
       deg = [-20.,-10.,0.,10.,20.,30.,40.,50.,60.,70.,80.,90.]
       dn  = [242.,227.,209.,192.,176.,163.,153.,146., 141., 137., 135., 133. ]
       RETURN, MAKE_CNVRT_ARR(dn_in,dn,deg)
   END
    
    4:   BEGIN
    ;
    ;    spacecraft thermistor type 3 ( DN )
    ;
       dn  = [0.,10.,15.,21.,27.,39.,74.,132.,196.,220.,240.,255.]
       deg = [272.,110.7,94.3,81.6,72.5,59.6,37.5,16.0,-3.4,-9.5,-15.9,-21.4]
       RETURN, MAKE_CNVRT_ARR(dn_in,dn,deg)
    END
    
    5:   BEGIN
    ;
    ;    spacecraft thermistor type 4 ( DN )
    ;
       dn  = [0.,10.,15.,21.,27.,39.,74.,132.,196.,220.,240.,255.]
       deg = [314.4,170.1,113.8,99.6,89.5,75.2,50.2,24.9,-0.1,-12.0,-26.5,-49.2]
       RETURN, MAKE_CNVRT_ARR(dn_in,dn,deg)
    END
    
    6:   BEGIN
    ;
    ;    spacecraft thermistor type 2 ( DN )
    ;
       dn  = [0.,10.,15.,21.,27.,39.,74.,132.,196.,220.,240.,255.]
       deg = [300.4,126.8,109.5,96.2,86.7,73.6,51.9,32.8,19.3,15.1,11.8,9.4]
       RETURN, MAKE_CNVRT_ARR(dn_in,dn,deg)
    END
    
    7:   BEGIN
    ;
    ;    spacecraft thermistor type 5 ( DN )
    ;
       dn  = float([0,23,43,81,98,113,141,165,187,197,206,215])
       deg = float([-260,-220,-200,-160,-140,-120,-80,-40,0,20,40,60,80,140,180])
       RETURN, MAKE_CNVRT_ARR(dn_in,dn,deg)
    END
    
    DEFAULT: BEGIN
    	PRINT, "%CNVRT_TEMP: Invalid temp_type."
    	RETURN, -1
    END

ENDCASE

END
