;+
; Project     : SOHO - CDS     
;                   
; Name        : TM_READ_CAL()
;               
; Purpose     : Read telemetry calibration data from file.
;               
; Explanation : Some telemetry items have a calibration that cannot be
;               catered for in the database file.  Each of these parameters
;               keeps its calibration data in a file of the form 
;               cal_curve_xx where xx is the number given in the database
;               file when the calibration type is 6.
;               
; Use         : IDL> cal_data = tm_read_cal(number)
;    
; Inputs      : number - the calibration number - identifies the file
;               
; Opt. Inputs : None
;               
; Outputs     : Function returns a 2-d real array (2,n) where n = number of
;               pairs of calibration data or a (2,n) string array for number
;               = 4 type calibration
;               
; Opt. Outputs: None
;               
; Keywords    : None
;
; Calls       : None
;
; Common      : None
;               
; Restrictions: None
;               
; Side effects: None
;               
; Category    : Telemetry, calibration
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 30-Jan-95
;               
; Modified    : Add text_list for additional files. CDP, 3-Feb-95
;               Use CDS_ENG_DATA instead of CDS_ENGINEER. CDP, 31-Oct-95
;
; Version     : Version 3, 31-Oct-95
;-            

function tm_read_cal, number

;
;  list of those files which contain list of strings which are to label
;  the axes.  This list must be updated if more such files are created.
;
text_list = [4,5,6,7,8,9,10]


;
;  must have a number for the calibration file
;
if n_params() eq 0 then begin
   print,'Use:  IDL> data = tm_read_cal(number)'
   return, -1
endif

;
;  make up file name
;
charnum= strmid(string(number+100,form='(i3)'),1,2)
file = concat_dir('$CDS_ENG_DATA','cal_curve_'+charnum)

;
;  open calibration data file
;
openr,lun,file,/get_lun, error=err

if err ne 0 then begin
   print,'Error opening calibration file: '+file
   return, -1
endif

;
;  read through it
;
text = ' '
readf,lun,text
while strmid(text,0,1) eq '!' do readf,lun,text
;
;  first relevant item is check on number
;
if fix(text) ne number then begin
   print,'Error in file format. Curve ID = ',fix(text)
   free_lun,lun
   return,-1
endif
 
;
; continue reading
;
readf,lun,text
while strmid(text,0,1) eq '!' do readf,lun,text
nread = fix(text)

;
;  set up array
;
s = find_common([number],text_list)
if s(0) ge 0  then begin
   out = strarr(2,nread)
endif else begin
   out = fltarr(2,nread)
endelse

;
;  continue reading
;
readf,lun,text
while strmid(text,0,1) eq '!' do readf,lun,text

;
;  cut into separate items
;
z = str_sep(text,' ')
z = z(where(z ne ''))
out(0,0) = z(0) & out(1,0) = z(1)

for i=1,nread-1 do begin
   readf,lun,text
   z = str_sep(text,' ')
   z = z(where(z ne ''))
   out(0,i) = z(0) & out(1,i) = z(1)
endfor

;
; finished
;
free_lun,lun

return,out

end


