function mag_load_cal_table, apid, cal_type, filename, cal_table
; This function load the corresponding calibration table required by a calibration function from
; an external data file
;

offset_A_r0_file = 'offset_A_r0.dat'
offset_A_r1_file = 'offset_A_r1.dat'
offset_B_r0_file = 'offset_B_r0.dat'
offset_B_r1_file = 'offset_B_r1.dat'
scale_factor_A_r0_file = 'scale_A_r0.dat'
scale_factor_A_r1_file = 'scale_A_r1.dat'
scale_factor_B_r0_file = 'scale_B_r0.dat'
scale_factor_B_r1_file = 'scale_B_r1.dat'
sensor_orth_correction_A_r0_file = 'orth_A_r0.dat'
sensor_orth_correction_A_r1_file = 'orth_A_r1.dat'
sensor_orth_correction_B_r0_file = 'orth_B_r0.dat'
sensor_orth_correction_B_r1_file = 'orth_B_r1.dat'
sensor_rotation_correction_file = 'rot.dat'
frequency_response_file = 'freq.dat'
internal_calibration_file = 'intcal.dat'

;i = 0

if not !quiet then begin
print, "Expect to use calibration filename: ", FILENAME
endif
result = FILE_TEST (concat_dir(getenv('IMPACT_CALIB_DATA'),FILENAME))
if not !quiet then begin
print, "cal_type is: ", cal_type
endif

; if calibration file does not exist, pick and use a defaulted calibration file
if result eq 0 then begin
case cal_type of
   'all_cal': begin
       if (FILENAME eq offset_A_r0_file || FILENAME eq offset_A_r1_file || FILENAME eq offset_B_r0_file || FILENAME eq offset_B_r1_file) then begin FILENAME = 'vector_zero.dat'
if not !quiet then begin
          print, "Loading default offset Calibration file...", FILENAME
endif
       endif

       if (FILENAME eq sensor_orth_correction_A_r0_file || FILENAME eq sensor_orth_correction_A_r1_file || FILENAME eq sensor_orth_correction_B_r0_file || FILENAME eq sensor_orth_correction_B_r1_file || FILENAME eq sensor_rotation_correction_file || FILENAME eq frequency_response_file) then begin FILENAME = 'matrix_zero.dat'
if not !quiet then begin
          print, "Loading default sensor orth or sensor rotation or freq response Calibration file...", FILENAME
endif
       endif

       if (FILENAME eq scale_factor_A_r0_file || FILENAME eq scale_factor_A_r0_file || FILENAME eq scale_factor_A_r0_file || FILENAME eq scale_factor_A_r0_file) then begin FILENAME = 'vector_zero.dat'
if not !quiet then begin
          print, "Loading default scale factor Calibration file...", FILENAME
endif
       endif

   end

   'init_zero': begin
       FILENAME = 'vector_zero.dat'
if not !quiet then begin
       print, "Loading default Calibration file...", FILENAME
endif
   end

;   'scale_factor': begin
;       FILENAME = 'vector_zero.dat'
;   end

   'sensor_orth_correction': begin
       FILENAME = 'matrix_zero.dat'
if not !quiet then begin
       print, "Loading default Calibration file...", FILENAME
endif
   end

   'sensor_rotation_correction': begin
       FILENAME = 'matrix_zero.dat'
if not !quiet then begin
       print, "Loading default Calibration file...", FILENAME
endif
   end

   'frequency_response': begin
       FILENAME = 'vector_zero.dat'
if not !quiet then begin
       print, "Loading default Calibration file...", FILENAME
endif
   end

   'internal_cal': begin
       FILENAME = 'vector_zero.dat'
if not !quiet then begin
       print, "Loading default Calibration file...", FILENAME
endif
   end
endcase

if not !quiet then begin
print, "File does not exist, using calibration file: ", FILENAME
endif

endif else begin   ;end result = 0
if not !quiet then begin
print, "Calibration FILENAME exist: ", FILENAME
endif
endelse

if (FILENAME eq offset_A_r0_file || FILENAME eq offset_A_r1_file || FILENAME eq offset_B_r0_file || FILENAME eq offset_B_r1_file) then begin

; Only Beacon telemetry packet will get offset adjustment, other will neglect offset adjustment and produce the uncorrected dataset
   if apid eq '208'XU or apid eq '209'XU then begin
      if not !quiet then begin
         print,"******** packet 208, 209 always skip offset adjustments ..."
      endif
; action
      FILENAME = 'vector_zero.dat'
   endif
endif

   openr, UNIT, concat_dir(getenv('IMPACT_CALIB_DATA'),FILENAME), /get_lun
   while not eof(unit) do begin
      readf, unit, cal_table
;      i = i + 1
   endwhile
if not !quiet then begin
print, "cal_table is: ", cal_table
endif
   free_lun, unit
return, cal_table
end