function dark_frame, raw_label, cfg_content, logname, quintic = quintic, explin = explin, polylin = polylin, interpolate_always = interpolate_always, scale = scale, temperature = temperature, master_bias = master_bias, master_slope = master_slope ;+ ; NAME: ; dark_frame, raw_label, cfg_content, logname, quintic = quintic, explin = explin, polylin = polylin, interpolate_always = interpolate_always, scale = scale, temperature = temperature ; ; PURPOSE: ; Interpolates a dark frame given any temperature and exposure time from the AMIE master dark images. ; ; CATEGORY: ; AMIE Calibration - part of the project 'amie_pipeline' ; ; CALLING SEQUENCE: ; interpolated_dark_matrix = dark_interpolation (raw_label, cfg_content, logname) ; ; INPUTS: ; raw_label: label of the image to be calibrated where the temperature and exposure durations are read ; ; OPTIONAL INPUT PARAMETERS: ; /Quintic: In case a quintic fit is requested for the master dark interpolation. ; ; OUTPUTS: ; a float array containing the dark frame matrix ; ; COMMON BLOCKS: ; None. ; ; SIDE EFFECTS: ; Hopefully none. ; ; RESTRICTIONS: ; No restrictions ; ; MODIFICATION HISTORY: ; ; 2004 Feb 12: First version ; 2008 Jan 08: sm. Name changed from dark_interpolation to dark_frame. ; Scale method added. ; Image temperature from table added. ; ; (c) 2004, Miguel Almeida, ESA/RSSD ;- ;--------------------------------------------------------------------------------------------------------------------------------- ; ; Initialize Log and Configuration File if needed ; ;--------------------------------------------------------------------------------------------------------------------------------- ;if (n_elements(logname) eq 0) then begin ; logname = initialize_log('AMIE_darkframe_') ;endif ;if (n_elements(cfg_content) eq 0) then begin ; cfg_file = 'AMIE_Pipeline.cfg' ; cfg_content = read_amie_pipeline_cfg(cfg_file, logname) ;endif ;if (n_elements(cfg_content) eq 1) then begin ; cfg_file = cfg_content[0] ; cfg_content = read_amie_pipeline_cfg(cfg_file, logname) ;endif ;--------------------------------------------------------------------------------------------------------------------------------- ; ;Log message ; ;--------------------------------------------------------------------------------------------------------------------------------- create_log_entry, logname, 'Entering Master Dark Frame Module' ;--------------------------------------------------------------------------------------------------------------------------------- ; ;Initialize interpolated master dark matrix ; ;--------------------------------------------------------------------------------------------------------------------------------- dark_frame_matrix = fltarr(1024,1024) ;--------------------------------------------------------------------------------------------------------------------------------- ; ;Looks in the label for the raw temperature and raw integration time ; ;--------------------------------------------------------------------------------------------------------------------------------- ;; Temperature if (keyword_set(temperature)) then begin raw_temperature = amie_getTemperature(raw_label,cfg_content(7)) if (raw_temperature eq -1.) then begin create_log_entry, logname, '[Dark Frame Module] No temperature (-1) retrieved from file' endif else begin create_log_entry, logname, '[Dark Frame Module] Temperature ('+strtrim(string(raw_temperature),2)+') retrieved from file' endelse endif else begin raw_temperature = strtrim(pdspar (raw_label, 'FOCAL_PLANE_TEMPERATURE'),2) if (raw_temperature eq 'UNK') then begin create_log_entry, logname, '[Dark Frame Module] No temperature (UNK) value retrieved from the header' raw_temperature = -1. endif else begin create_log_entry, logname, '[Dark Frame Module] Temperature ('+raw_temperature+') retrieved from the header' endelse raw_temperature = float(raw_temperature) endelse ;; Integration time raw_integration = strtrim(pdspar(raw_label,'EXPOSURE_DURATION'),2) create_log_entry, logname, '[Dark Frame Module] Exposure duration ('+raw_integration+') retrieved from the header' ;--------------------------------------------------------------------------------------------------------------------------------- ; ;Selection of the master dark frame method: interpolation or scale (KEYWORD: scale) ; ;--------------------------------------------------------------------------------------------------------------------------------- ;; Scale method if (keyword_set(scale)) then begin offset = fltarr(1024,1024) offset[*,*] = 8. ;; 2008 Mar 19, BG: bias and slope frames are now read from the PDS ;; files rather than from the naked binaries. ;; 2008-03-27, BG: Moved reading to main program outside the loop ;sBiasFile = findfile(cfg_content(6) + 'AMI_CMA_*_00000.IMG', count = iNumberFiles) ;reads in the filenames in a directory ;if (iNumberFiles ne 1) then begin ; create_log_entry, logname, '[Dark Frame module] Warning: More than one AMI_CMA_*_00000.IMG file found in the directory' ;endif ;help = read_amie( sBiasFile(iNumberFiles-1), bias_label, orientation = 'CCD_FRAME') ;bias_matrix = help.image1 bias_matrix = master_bias.image1 ;create_log_entry, logname, '[Dark Frame module] Master bias read from file ' + sBiasFile(iNumberFiles-1) ;sSlopeFile = findfile(cfg_content(6) + 'AMI_CMA_*_00001.IMG', count = iNumberFiles) ;reads in the filenames in a directory ;if (iNumberFiles ne 1) then begin ; create_log_entry, logname, '[Dark Frame module] Warning: More than one AMI_CMA_*_00001.IMG file found in the directory' ;endif ;help = read_amie( sSlopeFile(iNumberFiles-1), slope_label, orientation = 'CCD_FRAME') ;slope_matrix = help.image1 slope_matrix = master_slope.image1 ;create_log_entry, logname, '[Dark Frame module] Dark current read from file ' + sSlopeFile(iNumberFiles-1) fTemp = fltarr(1024,1024) fTemp[*,*] = float(amie_getTemperatureFactor(raw_temperature)) create_log_entry, logname, '[Dark Frame module] Temperature dependence factor (' + strtrim(string(fTemp[0,0]),2) + ')' exposure = fltarr(1024,1024) exposure[*,*] = float(raw_integration) dark_frame_matrix = offset + fTemp*(bias_matrix + exposure*slope_matrix) create_log_entry, logname, '[Dark Frame module] Master dark frame matrix created using the scale method' ;; Interpolation method endif else begin ;--------------------------------------------------------------------------------------------------------------------------------- ; ;Selection of the master dark file with the closest temperature and integration time to the raw image ; ;--------------------------------------------------------------------------------------------------------------------------------- ;Read master dark list index file containing all the master dark file names, temperatures and exposure durations restore, cfg_content(4) ;reads the master dark template master_table = read_ascii(cfg_content(3),template = master_file_template, COUNT = counter, DELIMITER=',',DATA_START = 78) ;looks in the master dark file list the closest temperature to the raw image for list_temp = 243, 323, 10 do begin if abs(raw_temperature - list_temp) LE 5 then begin temperature = list_temp endif endfor ;looks in the master dark file list the closest integration to the raw image old_diference = 60000 for i = 0, 88 do begin diference = abs(master_table.field3(i) - raw_integration) if diference LT old_diference then begin integration = master_table.field3(i) old_diference = diference endif endfor ;finds dark file in the master table dark_file_subscript = where((master_table.field2 eq temperature) and (master_table.field3 eq integration)) filename_dark = cfg_content(2) + strmid(master_table.field1(dark_file_subscript),1,31) ;reads the dark image file ;; 2008 Jan 28, BG: Added orientation = 'CCD_FRAME'. dark = read_amie (filename_dark, dark_label, orientation = 'CCD_FRAME') create_log_entry, logname, '[Dark Frame module] Loaded Lab Master Dark that closest matches the required master dark' create_log_entry, logname, '[Dark Frame module] Dark file is: ' + filename_dark ;-------------------------------------------------------------------------------------------------------------------------------- ; ;Interpolation based on the Master Dark just selected. If the raw image has an integration and temperature that matches the lab ;frames than the lab image is used ; ;-------------------------------------------------------------------------------------------------------------------------------- ;sets the raw integration to -1 to avoid the program to select a lab master file with the same exposure time and ;integration of the raw image raw_integration_test = raw_integration if keyword_set(interpolate_always) then raw_integration_test = -1 ;finds if the lab image is suitable for the raw image calibration and returns it if so if ( raw_integration_test eq integration) AND (raw_temperature eq temperature) then begin dark_frame_matrix = dark.image1 print, '[Dark Frame module] no interpolation needed as one of the dark frames obtained in the lab fulfills the requested characteristics' create_log_entry, logname, '[Dark Frame module] no interpolation needed as one of the dark frames obtained in the lab fulfills the requested characteristics' endif else begin ;if the lab image is not suitable calculation of the interpolated matrix if keyword_set(explin) then begin DN_min_bottom = 6e-8 * exp(0.0589 * raw_temperature) + 8 DN_max_bottom = 7e-11 * exp(0.1035 * raw_temperature) + 8 DN_min_top = 4e-9 * exp(0.0731 * raw_temperature) + 8 DN_max_top = 1e-10 * exp(0.1011 * raw_temperature) + 8 slope_bottom = (DN_max_bottom - DN_min_bottom)/(64000-10) slope_top = (DN_max_top - DN_min_top)/(64000-10) DN_Dark_bottom = slope_bottom * raw_integration DN_Dark_top = slope_top * raw_integration image_slope = (DN_Dark_top - DN_Dark_bottom)/1024 for line = 0, 1023 do begin dark_frame_matrix(*,line) = image_slope * (line + 1) + DN_Dark_bottom endfor create_log_entry, logname, '[Dark Frame module] Interpolated matrix created using an exponential fit and a linear fit' endif if keyword_set(quintic) then begin ;calculates the slope due to the read out time min_dark = median(dark.image1(*,0)) max_dark = median(dark.image1(*,1023)) dark_slope = (max_dark - min_dark)/1024 ;Read interpolation matrix restore, 'x:\idl procedures\amie\interpolation.sav' dark_median = interpolation_matrix(raw_temperature - 243, raw_integration - 10) ;Calculate the full dark matrix based on the slope and the dark median new_min_dark = dark_median - (dark_slope*512) for line = 0, 1023 do begin dark_frame_matrix(*,line) = dark_slope * (line +1) + new_min_dark endfor create_log_entry, logname, '[Dark Frame module] Interpolated matrix created using a quintic fit matrix' endif if keyword_set(polylin) then begin coef_DN_10ms_b = [-3.18718e-009, 5.67535e-006, -0.00381150, 1.23198, -193.793, 11946.7] coef_DN_10ms_t = [1.94835e-007,-0.000266658,0.145932,-39.9096,5453.32,-297802.] coef_DN_64s = [3.26105e-006, -0.00405761, 2.02091,-503.518, 62747.8, -3.12840e+006] DN_10ms_bottom = coef_DN_10ms_b(0) * (raw_temperature^5) + coef_DN_10ms_b(1) * (raw_temperature^4) + $ coef_DN_10ms_b(2) * (raw_temperature^3) + coef_DN_10ms_b(3) * (raw_temperature^2) + $ coef_DN_10ms_b(4) * raw_temperature + coef_DN_10ms_b(5) + 8 DN_10ms_top = coef_DN_10ms_t(0) * (raw_temperature^5) + coef_DN_10ms_t(1) * (raw_temperature^4) + $ coef_DN_10ms_t(2) * (raw_temperature^3) + coef_DN_10ms_t(3) * (raw_temperature^2) + $ coef_DN_10ms_t(4) * raw_temperature + coef_DN_10ms_t(5) + 8 DN_64s = coef_DN_64s(0) * (raw_temperature^5) + coef_DN_64s(1) * (raw_temperature^4) + $ coef_DN_64s(2) * (raw_temperature^3) + coef_DN_64s(3) * (raw_temperature^2) + $ coef_DN_64s(4) * raw_temperature + coef_DN_64s(5) + 8 slope_bottom = (DN_64s - DN_10ms_bottom)/(64000-10) slope_top = (DN_64s - DN_10ms_top)/(64000-10) DN_Dark_bottom = slope_bottom * (raw_integration - 10) + DN_10ms_bottom DN_Dark_top = slope_top * (raw_integration - 10) + DN_10ms_top image_slope = (DN_Dark_top - DN_Dark_bottom)/1024 for line = 0, 1023 do begin dark_frame_matrix(*,line) = image_slope * line + DN_Dark_bottom endfor create_log_entry, logname, '[Dark Frame module] Interpolated matrix created using an exponential fit and a linear fit' endif endelse endelse return, dark_frame_matrix end