|
KMOS Pipeline Reference Manual
1.2.5
|
00001 /* $Id: kmo_reconstruct.c,v 1.61 2013/10/08 14:55:01 erw Exp $ 00002 * 00003 * This file is part of the KMOS Pipeline 00004 * Copyright (C) 2002,2003 European Southern Observatory 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 /* 00022 * $Author: erw $ 00023 * $Date: 2013/10/08 14:55:01 $ 00024 * $Revision: 1.61 $ 00025 * $Name: HEAD $ 00026 */ 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 00032 /*----------------------------------------------------------------------------- 00033 * Includes 00034 *----------------------------------------------------------------------------*/ 00035 00036 #include <string.h> 00037 #include <math.h> 00038 00039 #include <cpl.h> 00040 00041 #include "kmo_utils.h" 00042 #include "kmo_functions.h" 00043 #include "kmo_priv_reconstruct.h" 00044 #include "kmo_priv_functions.h" 00045 #include "kmo_priv_lcorr.h" 00046 #include "kmo_cpl_extensions.h" 00047 #include "kmo_dfs.h" 00048 #include "kmo_error.h" 00049 #include "kmo_utils.h" 00050 #include "kmo_constants.h" 00051 #include "kmo_debug.h" 00052 00053 /*----------------------------------------------------------------------------- 00054 * Functions prototypes 00055 *----------------------------------------------------------------------------*/ 00056 00057 static int kmo_reconstruct_create(cpl_plugin *); 00058 static int kmo_reconstruct_exec(cpl_plugin *); 00059 static int kmo_reconstruct_destroy(cpl_plugin *); 00060 static int kmo_reconstruct(cpl_parameterlist *, cpl_frameset *); 00061 00062 /*----------------------------------------------------------------------------- 00063 * Static variables 00064 *----------------------------------------------------------------------------*/ 00065 00066 static char kmo_reconstruct_description[] = 00067 "Data with or without noise is reconstructed into a cube using the calibration\n" 00068 "frames XCAL, YCAL and LCAL. XCAL and YCAL are generated using recipe kmo_flat,\n" 00069 "LCAL is generated using recipe kmo_wave_cal.\n" 00070 "The input data can contain noise extensions and will be reconstructed into\n" 00071 "additional extensions.\n" 00072 "If an OH spectrum is given in the SOF file the lambda axis will be corrected\n" 00073 "using the OH lines as reference.\n" 00074 "\n" 00075 "BASIC PARAMETERS:\n" 00076 "-----------------\n" 00077 "--imethod\n" 00078 "The interpolation method used for reconstruction.\n" 00079 "\n" 00080 "--detectorimage\n" 00081 "Specify if the resampled image of the input frame should be generated. There-\n" 00082 "fore all slitlets of all IFUs are aligned one next to the other. This frame\n" 00083 "serves for quality control. One can immediately see if the reconstruction was\n" 00084 "successful.\n" 00085 "\n" 00086 "--file_extension" 00087 "Set to TRUE if OBS_ID (from input frame header) should be appended to the\n" 00088 "output frame.\n" 00089 "\n" 00090 "ADVANCED PARAMETERS\n" 00091 "-------------------\n" 00092 "--flux\n" 00093 "Specify if flux conservation should be applied.\n" 00094 "\n" 00095 "--neighborhoodRange\n" 00096 "Defines the range to search for neighbors during reconstruction\n" 00097 "\n" 00098 "--b_samples\n" 00099 "The number of samples in spectral direction for the reconstructed cube. Ideal-\n" 00100 "ly this number should be greater than 2048, the detector size.\n" 00101 "\n" 00102 "--b_start\n" 00103 "--b_end\n" 00104 "Used to define manually the start and end wavelength for the reconstructed\n" 00105 "cube. By default the internally defined values are used.\n" 00106 "\n" 00107 "--pix_scale\n" 00108 "Change the pixel scale [arcsec]. Default of 0.2\" results into cubes of\n" 00109 "14x14pix, a scale of 0.1\" results into cubes of 28x28pix, etc.\n" 00110 "\n" 00111 "--xcal_interpolation\n" 00112 "If TRUE interpolate the pixel position in the slitlet (xcal) using the two\n" 00113 "closest rotator angles in the calibration file. Otherwise take the values\n" 00114 "of the closest rotator angle\n" 00115 "\n" 00116 "-------------------------------------------------------------------------------\n" 00117 " Input files:\n" 00118 "\n" 00119 " DO KMOS \n" 00120 " category Type Explanation Required #Frames\n" 00121 " -------- ----- ----------- -------- -------\n" 00122 " DARK or RAW/F2D data with Y 1 \n" 00123 " FLAT_ON or RAW/F2D or without noise \n" 00124 " ARC_ON or RAW/F2D \n" 00125 " OBJECT or RAW \n" 00126 " STD or RAW \n" 00127 " SCIENCE RAW \n" 00128 " XCAL F2D x-direction calib. frame Y 1 \n" 00129 " YCAL F2D y-direction calib. frame Y 1 \n" 00130 " LCAL F2D Wavelength calib. frame Y 1 \n" 00131 " WAVE_BAND F2L Table with start-/end-wavelengths Y 1 \n" 00132 " OH_SPEC F1S Vector holding OH lines N 1 \n" 00133 "\n" 00134 " Output files:\n" 00135 "\n" 00136 " DO KMOS\n" 00137 " category Type Explanation\n" 00138 " -------- ----- -----------\n" 00139 " CUBE_DARK or F3I Reconstructed cube \n" 00140 " CUBE_FLAT or RAW/F2D with or without noise\n" 00141 " CUBE_ARC or \n" 00142 " CUBE_OBJECT or \n" 00143 " CUBE_STD or \n" 00144 " CUBE_SCIENCE \n" 00145 "-------------------------------------------------------------------------------\n" 00146 "\n"; 00147 00148 /*----------------------------------------------------------------------------- 00149 * Functions code 00150 *----------------------------------------------------------------------------*/ 00151 00168 int cpl_plugin_get_info(cpl_pluginlist *list) 00169 { 00170 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe); 00171 cpl_plugin *plugin = &recipe->interface; 00172 00173 cpl_plugin_init(plugin, 00174 CPL_PLUGIN_API, 00175 KMOS_BINARY_VERSION, 00176 CPL_PLUGIN_TYPE_RECIPE, 00177 "kmo_reconstruct", 00178 "Performs the cube reconstruction " 00179 "using different interpolation methods.", 00180 kmo_reconstruct_description, 00181 "Alex Agudo Berbel", 00182 "kmos-spark@mpe.mpg.de", 00183 kmos_get_license(), 00184 kmo_reconstruct_create, 00185 kmo_reconstruct_exec, 00186 kmo_reconstruct_destroy); 00187 00188 cpl_pluginlist_append(list, plugin); 00189 00190 return 0; 00191 } 00192 00200 static int kmo_reconstruct_create(cpl_plugin *plugin) 00201 { 00202 cpl_recipe *recipe; 00203 cpl_parameter *p; 00204 00205 /* Check that the plugin is part of a valid recipe */ 00206 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00207 recipe = (cpl_recipe *)plugin; 00208 else 00209 return -1; 00210 00211 /* Create the parameters list in the cpl_recipe object */ 00212 recipe->parameters = cpl_parameterlist_new(); 00213 00214 /* Fill the parameters list */ 00215 /* --imethod */ 00216 p = cpl_parameter_new_value("kmos.kmo_reconstruct.imethod", 00217 CPL_TYPE_STRING, 00218 "Method to use for interpolation. " 00219 "[\"NN\" (nearest neighbour), " 00220 "\"lwNN\" (linear weighted nearest neighbor), " 00221 "\"swNN\" (square weighted nearest neighbor), " 00222 "\"MS\" (Modified Shepard's method)" 00223 "\"CS\" (Cubic spline)]", 00224 "kmos.kmo_reconstruct", 00225 "CS"); 00226 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "imethod"); 00227 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00228 cpl_parameterlist_append(recipe->parameters, p); 00229 00230 /* --neighborhoodRange */ 00231 p = cpl_parameter_new_value("kmos.kmo_reconstruct.neighborhoodRange", 00232 CPL_TYPE_DOUBLE, 00233 "Defines the range to search for neighbors. " 00234 "in pixels", 00235 "kmos.kmo_reconstruct", 00236 1.001); 00237 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "neighborhoodRange"); 00238 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00239 cpl_parameterlist_append(recipe->parameters, p); 00240 00241 /* --flux */ 00242 p = cpl_parameter_new_value("kmos.kmo_reconstruct.flux", 00243 CPL_TYPE_BOOL, 00244 "TRUE: Apply flux conservation. FALSE: otherwise", 00245 "kmos.kmo_reconstruct", 00246 FALSE); 00247 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flux"); 00248 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00249 cpl_parameterlist_append(recipe->parameters, p); 00250 00251 /* --detectorimage */ 00252 p = cpl_parameter_new_value("kmos.kmo_reconstruct.detectorimage", 00253 CPL_TYPE_BOOL, 00254 "TRUE: if resampled detector frame should be " 00255 "created, FALSE: otherwise", 00256 "kmos.kmo_reconstruct", 00257 FALSE); 00258 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "detimg"); 00259 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00260 cpl_parameterlist_append(recipe->parameters, p); 00261 00262 /* --file_extension */ 00263 p = cpl_parameter_new_value("kmos.kmo_reconstruct.file_extension", 00264 CPL_TYPE_BOOL, 00265 "TRUE: if OBS_ID keyword should be appended to " 00266 "output frames, FALSE: otherwise", 00267 "kmos.kmo_reconstruct", 00268 FALSE); 00269 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "file_extension"); 00270 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00271 cpl_parameterlist_append(recipe->parameters, p); 00272 00273 /* --pix_scale */ 00274 p = cpl_parameter_new_value("kmos.kmo_reconstruct.pix_scale", 00275 CPL_TYPE_DOUBLE, 00276 "Change the pixel scale [arcsec]. " 00277 "Default of 0.2\" results into cubes of 14x14pix, " 00278 "a scale of 0.1\" results into cubes of 28x28pix, " 00279 "etc.", 00280 "kmos.kmo_reconstruct", 00281 KMOS_PIX_RESOLUTION); 00282 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "pix_scale"); 00283 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00284 cpl_parameterlist_append(recipe->parameters, p); 00285 00286 /* --dev_flip */ 00287 p = cpl_parameter_new_value("kmos.kmo_reconstruct.dev_flip", 00288 CPL_TYPE_BOOL, 00289 "INTENDED FOR PIPELINE DEVELOPERS ONLY: " 00290 "Set this parameter to TRUE if the wavelengths " 00291 "are ascending on the detector from bottom to " 00292 "top (only for old simulation data).", 00293 "kmos.kmo_reconstruct", 00294 FALSE); 00295 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dev_flip"); 00296 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00297 cpl_parameterlist_append(recipe->parameters, p); 00298 00299 /* --xcal_interpolation */ 00300 p = cpl_parameter_new_value("kmos.kmo_reconstruct.xcal_interpolation", 00301 CPL_TYPE_BOOL, 00302 "TRUE: Interpolate xcal between rotator angles. FALSE: otherwise", 00303 "kmos.kmo_reconstruct", 00304 TRUE); 00305 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xcal_interpolation"); 00306 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00307 cpl_parameterlist_append(recipe->parameters, p); 00308 00309 // add parameters for band-definition 00310 kmo_band_pars_create(recipe->parameters, 00311 "kmos.kmo_reconstruct"); 00312 00313 return 0; 00314 } 00315 00321 static int kmo_reconstruct_exec(cpl_plugin *plugin) 00322 { 00323 cpl_recipe *recipe; 00324 00325 /* Get the recipe out of the plugin */ 00326 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00327 recipe = (cpl_recipe *)plugin; 00328 else return -1; 00329 00330 return kmo_reconstruct(recipe->parameters, recipe->frames); 00331 } 00332 00338 static int kmo_reconstruct_destroy(cpl_plugin *plugin) 00339 { 00340 cpl_recipe *recipe; 00341 00342 /* Get the recipe out of the plugin */ 00343 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00344 recipe = (cpl_recipe *)plugin; 00345 else return -1 ; 00346 00347 cpl_parameterlist_delete(recipe->parameters); 00348 return 0 ; 00349 } 00350 00365 static int kmo_reconstruct(cpl_parameterlist *parlist, cpl_frameset *frameset) 00366 { 00367 int ret_val = 0, 00368 nr_devices = 0, 00369 i = 0, 00370 j = 0, 00371 flux = FALSE, 00372 background = FALSE, 00373 index = 0, 00374 detectorimage = 0, 00375 *bounds = NULL, 00376 ifu_nr = 0, 00377 obs_id = 0, 00378 file_extension = FALSE, 00379 dev_flip = FALSE, 00380 xcal_interpolation = FALSE, 00381 detImgCube = FALSE, 00382 l = 0, x = 0, y = 0; 00383 float *pdet_img_data = NULL, 00384 *pdet_img_noise = NULL, 00385 *slice = NULL; 00386 double neighborhoodRange = 1.001, 00387 pix_scale = 0.0; 00388 00389 const char *imethod = NULL, 00390 *input_frame_name = NULL, 00391 *output_frame_name = NULL, 00392 *filter_id = NULL, 00393 *filter_id_tmp = NULL, 00394 *tmp_str = NULL; 00395 char *keyword = NULL, 00396 *filename_cube = NULL, 00397 *filename_img = NULL, 00398 // *fn_lut = NULL, 00399 *suffix = NULL, 00400 *obs_suffix = NULL, 00401 *my_filter_id = NULL, 00402 *extname = NULL; 00403 cpl_image *lcal = NULL, 00404 *det_img_data[KMOS_NR_DETECTORS], 00405 *det_img_noise[KMOS_NR_DETECTORS], 00406 *tmp_img = NULL; 00407 cpl_imagelist *cube_data = NULL, 00408 *cube_noise = NULL; 00409 cpl_frame *rec_frame = NULL, 00410 *xcal_frame = NULL, 00411 *ycal_frame = NULL, 00412 *lcal_frame = NULL, 00413 *ref_spectrum_frame = NULL; 00414 cpl_propertylist *main_header = NULL, 00415 *sub_header = NULL, 00416 *sub_header_orig = NULL, 00417 *actual_sub_header = NULL, 00418 *tmp_header = NULL; 00419 cpl_table *band_table = NULL; 00420 gridDefinition gd; 00421 main_fits_desc desc1, 00422 desc2; 00423 cpl_polynomial *lcorr_coeffs = NULL; 00424 00425 for (i = 0; i < KMOS_NR_DETECTORS; i++) { 00426 det_img_data[i] = NULL; 00427 det_img_noise[i] = NULL; 00428 } 00429 00430 KMO_TRY 00431 { 00432 kmo_init_fits_desc(&desc1); 00433 kmo_init_fits_desc(&desc2); 00434 00435 // --- check input --- 00436 KMO_TRY_ASSURE((parlist != NULL) && 00437 (frameset != NULL), 00438 CPL_ERROR_NULL_INPUT, 00439 "Not all input data is provided!"); 00440 00441 KMO_TRY_ASSURE((cpl_frameset_count_tags(frameset, DARK) == 1) || 00442 (cpl_frameset_count_tags(frameset, FLAT_ON) == 1) || 00443 (cpl_frameset_count_tags(frameset, ARC_ON) == 1) || 00444 (cpl_frameset_count_tags(frameset, OBJECT) == 1) || 00445 (cpl_frameset_count_tags(frameset, STD) == 1) || 00446 (cpl_frameset_count_tags(frameset, SCIENCE) == 1), 00447 CPL_ERROR_NULL_INPUT, 00448 "A data frame (DARK, FLAT_ON, ARC_ON, OBJECT, STD or SCIENCE) must " 00449 "be provided!"); 00450 00451 KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, XCAL) == 1, 00452 CPL_ERROR_FILE_NOT_FOUND, 00453 "XCAL frame missing in frameset!!"); 00454 00455 KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, YCAL) == 1, 00456 CPL_ERROR_FILE_NOT_FOUND, 00457 "YCAL frame missing in frameset!!"); 00458 00459 KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, LCAL) == 1, 00460 CPL_ERROR_FILE_NOT_FOUND, 00461 "LCAL frame missing in frameset!!"); 00462 00463 KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, WAVE_BAND) == 1, 00464 CPL_ERROR_FILE_NOT_FOUND, 00465 "WAVE_BAND frame missing in frameset!!"); 00466 00467 KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_reconstruct") == 1, 00468 CPL_ERROR_ILLEGAL_INPUT, 00469 "Cannot identify RAW and CALIB frames!"); 00470 00471 KMO_TRY_ASSURE(cpl_frameset_count_tags(frameset, OH_SPEC) == 0 || 00472 cpl_frameset_count_tags(frameset, OH_SPEC) == 1, 00473 CPL_ERROR_ILLEGAL_INPUT, 00474 "Only a single reference spectrum can be provided!"); 00475 00476 // --- get parameters --- 00477 cpl_msg_info("", "--- Parameter setup for kmo_reconstruct ---"); 00478 00479 KMO_TRY_EXIT_IF_NULL( 00480 imethod = kmo_dfs_get_parameter_string(parlist, 00481 "kmos.kmo_reconstruct.imethod")); 00482 00483 KMO_TRY_ASSURE((strcmp(imethod, "NN") == 0) || 00484 (strcmp(imethod, "lwNN") == 0) || 00485 (strcmp(imethod, "swNN") == 0) || 00486 (strcmp(imethod, "MS") == 0) || 00487 (strcmp(imethod, "CS") == 0), 00488 CPL_ERROR_ILLEGAL_INPUT, 00489 "imethod must be either \"NN\", \"lwNN\", " 00490 "\"swNN\", \"MS\" or \"CS\"!"); 00491 00492 KMO_TRY_EXIT_IF_ERROR( 00493 kmo_dfs_print_parameter_help(parlist, 00494 "kmos.kmo_reconstruct.imethod")); 00495 00496 flux = kmo_dfs_get_parameter_bool(parlist, 00497 "kmos.kmo_reconstruct.flux"); 00498 00499 KMO_TRY_ASSURE((flux == 0) || 00500 (flux == 1), 00501 CPL_ERROR_ILLEGAL_INPUT, 00502 "flux must be either FALSE or TRUE!"); 00503 00504 KMO_TRY_EXIT_IF_ERROR( 00505 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_reconstruct.flux")); 00506 00507 detectorimage = kmo_dfs_get_parameter_bool(parlist, 00508 "kmos.kmo_reconstruct.detectorimage"); 00509 00510 KMO_TRY_ASSURE((detectorimage == 0) || 00511 (detectorimage == 1), 00512 CPL_ERROR_ILLEGAL_INPUT, 00513 "detectorimage must be either 0 or 1 !"); 00514 00515 KMO_TRY_EXIT_IF_ERROR( 00516 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_reconstruct.detectorimage")); 00517 00518 neighborhoodRange = kmo_dfs_get_parameter_double(parlist, 00519 "kmos.kmo_reconstruct.neighborhoodRange"); 00520 KMO_TRY_CHECK_ERROR_STATE(); 00521 KMO_TRY_ASSURE(neighborhoodRange > 0.0, 00522 CPL_ERROR_ILLEGAL_INPUT, 00523 "neighborhoodRange must be greater than 0.0"); 00524 KMO_TRY_EXIT_IF_ERROR( 00525 kmo_dfs_print_parameter_help(parlist, 00526 "kmos.kmo_reconstruct.neighborhoodRange")); 00527 00528 kmo_band_pars_load(parlist, "kmos.kmo_reconstruct"); 00529 00530 file_extension = kmo_dfs_get_parameter_bool(parlist, 00531 "kmos.kmo_reconstruct.file_extension"); 00532 KMO_TRY_CHECK_ERROR_STATE(); 00533 KMO_TRY_EXIT_IF_ERROR( 00534 kmo_dfs_print_parameter_help(parlist, 00535 "kmos.kmo_reconstruct.file_extension")); 00536 00537 pix_scale = kmo_dfs_get_parameter_double(parlist, 00538 "kmos.kmo_reconstruct.pix_scale"); 00539 KMO_TRY_CHECK_ERROR_STATE(); 00540 KMO_TRY_EXIT_IF_ERROR( 00541 kmo_dfs_print_parameter_help(parlist, 00542 "kmos.kmo_reconstruct.pix_scale")); 00543 KMO_TRY_ASSURE((pix_scale >= 0.01) && 00544 (pix_scale <= 0.4), 00545 CPL_ERROR_ILLEGAL_INPUT, 00546 "pix_scale must be between 0.01 and 0.4 (results in cubes " 00547 "with 7x7 to 280x280 pixels)!"); 00548 00549 dev_flip = kmo_dfs_get_parameter_bool(parlist, 00550 "kmos.kmo_reconstruct.dev_flip"); 00551 KMO_TRY_CHECK_ERROR_STATE(); 00552 KMO_TRY_EXIT_IF_ERROR( 00553 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_reconstruct.dev_flip")); 00554 KMO_TRY_ASSURE((dev_flip == TRUE) || 00555 (dev_flip == FALSE), 00556 CPL_ERROR_ILLEGAL_INPUT, 00557 "dev_flip must be TRUE or FALSE!"); 00558 00559 xcal_interpolation = kmo_dfs_get_parameter_bool(parlist, 00560 "kmos.kmo_reconstruct.xcal_interpolation"); 00561 KMO_TRY_CHECK_ERROR_STATE(); 00562 KMO_TRY_EXIT_IF_ERROR( 00563 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_reconstruct.xcal_interpolation")); 00564 KMO_TRY_ASSURE((xcal_interpolation == TRUE) || 00565 (xcal_interpolation == FALSE), 00566 CPL_ERROR_ILLEGAL_INPUT, 00567 "xcal_interpolation must be TRUE or FALSE!"); 00568 00569 00570 cpl_msg_info("", "-------------------------------------------"); 00571 00572 // load descriptor and header of data frame to reconstruct 00573 if (cpl_frameset_count_tags(frameset, DARK) == 1) { 00574 input_frame_name = DARK; 00575 output_frame_name = CUBE_DARK; 00576 } else if (cpl_frameset_count_tags(frameset, FLAT_ON) == 1) { 00577 input_frame_name = FLAT_ON; 00578 output_frame_name = CUBE_FLAT; 00579 } else if (cpl_frameset_count_tags(frameset, ARC_ON) == 1) { 00580 input_frame_name = ARC_ON; 00581 output_frame_name = CUBE_ARC; 00582 } else if (cpl_frameset_count_tags(frameset, OBJECT) == 1) { 00583 input_frame_name = OBJECT; 00584 output_frame_name = CUBE_OBJECT; 00585 } else if (cpl_frameset_count_tags(frameset, STD) == 1) { 00586 input_frame_name = STD; 00587 output_frame_name = CUBE_STD; 00588 } else if (cpl_frameset_count_tags(frameset, SCIENCE) == 1) { 00589 input_frame_name = SCIENCE; 00590 output_frame_name = CUBE_SCIENCE; 00591 } 00592 00593 // assure that filters, grating and rotation offsets match for 00594 // XCAL, YCAL, LCAL and for data frame to reconstruct (except DARK 00595 // frames) 00596 // check if filter_id and grating_id match for all detectors 00597 KMO_TRY_EXIT_IF_ERROR( 00598 kmo_check_frame_setup(frameset, XCAL, YCAL, 00599 TRUE, FALSE, TRUE)); 00600 KMO_TRY_EXIT_IF_ERROR( 00601 kmo_check_frame_setup(frameset, XCAL, LCAL, 00602 TRUE, FALSE, TRUE)); 00603 00604 if (cpl_frameset_count_tags(frameset, DARK) != 1) { 00605 00606 // check if filters, gratings and rotator offset match 00607 // (except for DARK frames) 00608 KMO_TRY_EXIT_IF_ERROR( 00609 kmo_check_frame_setup(frameset, XCAL, input_frame_name, 00610 TRUE, FALSE, FALSE)); 00611 /* 00612 // check if rotator offset don't differ to much 00613 cpl_frame *f1 = NULL, *f2 = NULL; 00614 cpl_propertylist *h1 = NULL, *h2 = NULL; 00615 char *kw = NULL; 00616 double tmp_dbl1 = 0.0, tmp_dbl2 = 0.0; 00617 00618 KMO_TRY_EXIT_IF_NULL( 00619 f1 = kmo_dfs_get_frame(frameset, XCAL)); 00620 00621 KMO_TRY_EXIT_IF_NULL( 00622 f2 = kmo_dfs_get_frame(frameset, input_frame_name)); 00623 h1 = kmclipm_propertylist_load(cpl_frame_get_filename(f1), 0); 00624 if (cpl_error_get_code() != CPL_ERROR_NONE) { 00625 cpl_msg_error("","File not found: %s!", 00626 cpl_frame_get_filename(f1)); 00627 KMO_TRY_CHECK_ERROR_STATE(); 00628 } 00629 00630 h2 = kmclipm_propertylist_load(cpl_frame_get_filename(f2), 0); 00631 if (cpl_error_get_code() != CPL_ERROR_NONE) { 00632 cpl_msg_error("","File not found: %s!", 00633 cpl_frame_get_filename(f2)); 00634 KMO_TRY_CHECK_ERROR_STATE(); 00635 } 00636 KMO_TRY_EXIT_IF_NULL( 00637 kw = cpl_sprintf("%s", ROTANGLE)); 00638 tmp_dbl1 = cpl_propertylist_get_double(h1, kw); 00639 if (cpl_error_get_code() != CPL_ERROR_NONE) { 00640 KMO_TRY_ASSURE(1 == 0, 00641 CPL_ERROR_ILLEGAL_INPUT, 00642 "keyword \n%s\n of frame %s is missing!", 00643 keyword, XCAL); 00644 } 00645 00646 tmp_dbl2 = cpl_propertylist_get_double(h2, kw); 00647 if (cpl_error_get_code() != CPL_ERROR_NONE) { 00648 KMO_TRY_ASSURE(1 == 0, 00649 CPL_ERROR_ILLEGAL_INPUT, 00650 "keyword \n%s\n of frame %s is missing!", 00651 keyword, input_frame_name); 00652 } 00653 00654 // strip angles below 0 deg and above 360 deg 00655 kmclipm_strip_angle(&tmp_dbl1); 00656 kmclipm_strip_angle(&tmp_dbl2); 00657 00658 if (fabs(tmp_dbl1 - tmp_dbl2) > 30.) { 00659 if ((fabs(tmp_dbl1) < 0.001) && (tmp_dbl2>330) && (tmp_dbl2<360)) { 00660 // singularity! 00661 // we have rot=0 for XCAL and rot>330 | rot<360 for input frame 00662 } else { 00663 cpl_msg_warning("","The angle of the calibration files (%g deg) " 00664 "and the angle of the frame to reconstruct" 00665 " (%g deg) differ by %g deg! Think about using " 00666 "calibration files matching better the actual " 00667 "rotator offset (ESO OCS ROT NAANGLE)", 00668 tmp_dbl1, tmp_dbl2, 00669 fabs(tmp_dbl1 - tmp_dbl2)); 00670 } 00671 } 00672 00673 cpl_propertylist_delete(h1); h1 = NULL; 00674 cpl_propertylist_delete(h2); h2 = NULL; 00675 cpl_free(kw); kw = NULL; 00676 */ 00677 } 00678 00679 KMO_TRY_EXIT_IF_NULL( 00680 xcal_frame = kmo_dfs_get_frame(frameset, XCAL)); 00681 KMO_TRY_EXIT_IF_NULL( 00682 rec_frame = kmo_dfs_get_frame(frameset, input_frame_name)); 00683 KMO_TRY_EXIT_IF_NULL( 00684 suffix = kmo_dfs_get_suffix(rec_frame, TRUE, TRUE)); 00685 00686 KMO_TRY_EXIT_IF_ERROR( 00687 kmo_check_frame_setup_md5_xycal(frameset)); 00688 KMO_TRY_EXIT_IF_ERROR( 00689 kmo_check_frame_setup_md5(frameset)); 00690 00691 cpl_msg_info("", "Detected instrument setup: %s", suffix+1); 00692 cpl_msg_info("", "(grating 1, 2 & 3, rotation angle)"); 00693 cpl_msg_info("", "-------------------------------------------"); 00694 00695 KMO_TRY_EXIT_IF_NULL( 00696 main_header = kmo_dfs_load_primary_header(frameset, 00697 input_frame_name)); 00698 00699 if (cpl_frameset_count_tags(frameset, OH_SPEC) != 0) { 00700 if (cpl_propertylist_has(main_header, ORIGFILE)) { 00701 KMO_TRY_EXIT_IF_NULL( 00702 tmp_str = cpl_propertylist_get_string(main_header, ORIGFILE)); 00703 if (strstr(tmp_str, "OBS") != NULL) { 00704 // we are reconstructing an OBS-frame, allow OH_SPEC correction 00705 KMO_TRY_EXIT_IF_NULL( 00706 ref_spectrum_frame = kmo_dfs_get_frame(frameset, OH_SPEC)); 00707 } else { 00708 cpl_msg_warning("", "Supplied OH_SPEC is ignored since a calibration frame is being reconstructed."); 00709 } 00710 } else { 00711 cpl_msg_warning("", "The supplied frame %s is assumed to be a science frame. If it is a calibration frame, omit OH_SPEC from sof-file", input_frame_name); 00712 KMO_TRY_EXIT_IF_NULL( 00713 ref_spectrum_frame = kmo_dfs_get_frame(frameset, OH_SPEC)); 00714 } 00715 } 00716 00717 desc1 = kmo_identify_fits_header(cpl_frame_get_filename(rec_frame)); 00718 KMO_TRY_CHECK_ERROR_STATE(); 00719 00720 KMO_TRY_ASSURE(((desc1.nr_ext == KMOS_NR_DETECTORS) || 00721 ((desc1.nr_ext == 2*KMOS_NR_DETECTORS))) && 00722 (desc1.ex_badpix == FALSE) && 00723 ((desc1.fits_type == raw_fits) || 00724 (desc1.fits_type == f2d_fits)) && 00725 (desc1.frame_type == detector_frame), 00726 CPL_ERROR_ILLEGAL_INPUT, 00727 "The frame to reconstruct isn't in the correct format!!!" 00728 "Exactly 3 frames, or 6 with noise are expected!"); 00729 00730 if (!desc1.ex_noise) { 00731 nr_devices = desc1.nr_ext; 00732 } else { 00733 nr_devices = desc1.nr_ext / 2; 00734 } 00735 00736 // compare descriptor of XCAL and data frame to reconstruct 00737 desc2 = kmo_identify_fits_header(cpl_frame_get_filename(xcal_frame)); 00738 KMO_TRY_CHECK_ERROR_STATE(); 00739 00740 KMO_TRY_ASSURE((desc2.nr_ext % 3 == 0) && 00741 (desc1.ex_badpix == desc2.ex_badpix) && 00742 (desc1.frame_type == desc2.frame_type), 00743 CPL_ERROR_ILLEGAL_INPUT, 00744 "XCAL isn't in the correct format!!!"); 00745 00746 kmo_free_fits_desc(&desc2); 00747 00748 // compare descriptor of YCAL and data frame to reconstruct 00749 kmo_init_fits_desc(&desc2); 00750 00751 KMO_TRY_EXIT_IF_NULL( 00752 ycal_frame = kmo_dfs_get_frame(frameset, YCAL)); 00753 00754 desc2 = kmo_identify_fits_header(cpl_frame_get_filename(ycal_frame)); 00755 KMO_TRY_CHECK_ERROR_STATE(); 00756 00757 KMO_TRY_ASSURE((desc2.nr_ext % 3 == 0) && 00758 (desc1.ex_badpix == desc2.ex_badpix) && 00759 (desc1.frame_type == desc2.frame_type), 00760 CPL_ERROR_ILLEGAL_INPUT, 00761 "YCAL isn't in the correct format!!!"); 00762 00763 kmo_free_fits_desc(&desc2); 00764 00765 // compare descriptor of LCAL and data frame to reconstruct 00766 kmo_init_fits_desc(&desc2); 00767 00768 KMO_TRY_EXIT_IF_NULL( 00769 lcal_frame = kmo_dfs_get_frame(frameset, LCAL)); 00770 00771 desc2 = kmo_identify_fits_header(cpl_frame_get_filename(lcal_frame)); 00772 KMO_TRY_CHECK_ERROR_STATE(); 00773 00774 KMO_TRY_ASSURE((desc2.nr_ext % 3 == 0) && 00775 (desc1.ex_badpix == desc2.ex_badpix) && 00776 (desc1.frame_type == desc2.frame_type), 00777 CPL_ERROR_ILLEGAL_INPUT, 00778 "LCAL isn't in the correct format!!!"); 00779 00780 kmo_free_fits_desc(&desc2); 00781 00782 // 00783 // --- update & save primary header --- 00784 // 00785 KMO_TRY_EXIT_IF_NULL( 00786 tmp_header = kmo_dfs_load_primary_header(frameset, LCAL)); 00787 00788 // assert that filters have correct IDs and that all detectors of all 00789 // input frames have the same filter set 00790 for (i = 1; i <= nr_devices; i++) { 00791 // ESO INS FILTi ID 00792 KMO_TRY_EXIT_IF_NULL( 00793 keyword = cpl_sprintf("%s%d%s", IFU_FILTID_PREFIX, i, 00794 IFU_FILTID_POSTFIX)); 00795 00796 KMO_TRY_EXIT_IF_NULL( 00797 filter_id = cpl_propertylist_get_string(tmp_header, keyword)); 00798 00799 KMO_TRY_ASSURE((strcmp(filter_id, "IZ") == 0) || 00800 (strcmp(filter_id, "YJ") == 0) || 00801 (strcmp(filter_id, "H") == 0) || 00802 (strcmp(filter_id, "K") == 0) || 00803 (strcmp(filter_id, "HK") == 0), 00804 CPL_ERROR_ILLEGAL_INPUT, 00805 "Filter ID in primary header of LCAL frame must " 00806 "be either \"IZ\", \"YJ\", \"H\", \"K\" or " 00807 "\"HK\" !"); 00808 00809 if (strcmp(input_frame_name, DARK) != 0) { 00810 // dark needn't to be taken with filter! 00811 00812 KMO_TRY_EXIT_IF_NULL( 00813 filter_id_tmp = cpl_propertylist_get_string(main_header, 00814 keyword)); 00815 KMO_TRY_ASSURE(strcmp(filter_id, filter_id_tmp) == 0, 00816 CPL_ERROR_ILLEGAL_INPUT, 00817 "Filter IDs must be the same for LCAL frame and " 00818 "the frame to reconstruct!" 00819 "Detector No.: %d\nLCAL: %s\n%s: %s\n", 00820 i, filter_id, input_frame_name, filter_id_tmp); 00821 } 00822 cpl_free(keyword); keyword = NULL; 00823 } 00824 KMO_TRY_EXIT_IF_NULL( 00825 my_filter_id = cpl_strdup(filter_id)); 00826 cpl_propertylist_delete(tmp_header); tmp_header = NULL; 00827 00828 obs_id = cpl_propertylist_get_int(main_header, OBS_ID); 00829 KMO_TRY_CHECK_ERROR_STATE(); 00830 00831 KMO_TRY_EXIT_IF_NULL( 00832 filename_cube = cpl_sprintf("%s", output_frame_name)); 00833 KMO_TRY_EXIT_IF_NULL( 00834 filename_img = cpl_sprintf("%s", DET_IMG_REC)); 00835 if (file_extension) { 00836 KMO_TRY_EXIT_IF_NULL( 00837 obs_suffix = cpl_sprintf("%s%d", "_", obs_id)); 00838 } else { 00839 KMO_TRY_EXIT_IF_NULL( 00840 obs_suffix = cpl_sprintf("%s", "")); 00841 } 00842 00843 KMO_TRY_EXIT_IF_ERROR( 00844 kmo_dfs_save_main_header(frameset, filename_cube, obs_suffix, 00845 rec_frame, NULL, parlist, cpl_func)); 00846 00847 // setup grid definition, wavelength start and end points will be set 00848 // in the detector loop 00849 KMO_TRY_EXIT_IF_ERROR( 00850 kmclipm_setup_grid(&gd, imethod, neighborhoodRange, pix_scale, 0.)); 00851 00852 KMO_TRY_EXIT_IF_NULL( 00853 tmp_header = kmo_dfs_load_primary_header(frameset, XCAL)); 00854 00855 KMO_TRY_EXIT_IF_NULL( 00856 bounds = kmclipm_extract_bounds(tmp_header)); 00857 cpl_propertylist_delete(tmp_header); tmp_header = NULL; 00858 00859 if (detectorimage == TRUE) { 00860 KMO_TRY_EXIT_IF_ERROR( 00861 kmo_dfs_save_main_header(frameset, filename_img, obs_suffix, 00862 rec_frame, NULL, parlist, cpl_func)); 00863 } 00864 00865 /* loop through all detectors */ 00866 for (i = 1; i <= nr_devices; i++) { 00867 cpl_msg_info("","Processing detector No. %d", i); 00868 00869 // load lcal 00870 // extract LCAL image close to ROTANGLE 0. assuming that the wavelength range 00871 // doesn't differ too much with different ROTANGLEs. 00872 double rotangle_found; 00873 print_cal_angle_msg_once = FALSE; 00874 KMO_TRY_EXIT_IF_NULL( 00875 lcal = kmo_dfs_load_cal_image(frameset, LCAL, i, FALSE, 0., 00876 FALSE, NULL, &rotangle_found, -1, 0, 0)); 00877 if (i==1) { 00878 print_cal_angle_msg_once = TRUE; 00879 } 00880 char *tmp_band_method = getenv("KMO_BAND_METHOD"); 00881 int band_method = 0; 00882 if (tmp_band_method != NULL) { 00883 band_method = atoi(tmp_band_method); 00884 } 00885 00886 KMO_TRY_EXIT_IF_NULL( 00887 band_table = kmo_dfs_load_table(frameset, WAVE_BAND, 1, FALSE)); 00888 00889 KMO_TRY_EXIT_IF_ERROR( 00890 kmclipm_setup_grid_band_lcal(&gd, lcal, my_filter_id, 00891 band_method, band_table)); 00892 cpl_table_delete(band_table); band_table = NULL; 00893 00894 cpl_image_delete(lcal); lcal = NULL; 00895 00896 if (detectorimage == TRUE) { 00897 KMO_TRY_EXIT_IF_NULL( 00898 det_img_data[i-1] = cpl_image_new(gd.x.dim*gd.y.dim*KMOS_IFUS_PER_DETECTOR, 00899 gd.l.dim, CPL_TYPE_FLOAT)); 00900 KMO_TRY_EXIT_IF_NULL( 00901 pdet_img_data = cpl_image_get_data_float(det_img_data[i-1])); 00902 00903 KMO_TRY_EXIT_IF_NULL( 00904 det_img_noise[i-1] = cpl_image_new(gd.x.dim*gd.y.dim*KMOS_IFUS_PER_DETECTOR, 00905 gd.l.dim, CPL_TYPE_FLOAT)); 00906 KMO_TRY_EXIT_IF_NULL( 00907 pdet_img_noise = cpl_image_get_data_float(det_img_noise[i-1])); 00908 } 00909 00910 00911 for (j = 0; j < KMOS_IFUS_PER_DETECTOR; j++) { 00912 /* update sub-header */ 00913 ifu_nr = (i-1)*KMOS_IFUS_PER_DETECTOR + j + 1; 00914 00915 /* load raw image and sub-header*/ 00916 KMO_TRY_EXIT_IF_NULL( 00917 sub_header = kmo_dfs_load_sub_header(frameset, input_frame_name, 00918 i, FALSE)); 00919 KMO_TRY_EXIT_IF_NULL( 00920 sub_header_orig = cpl_propertylist_duplicate(sub_header)); 00921 00922 // check if IFU is valid according to main header keywords & 00923 // calibration files 00924 00925 if (getenv("KMOS_RECONSTRUCT_ALL") == NULL) { 00926 KMO_TRY_EXIT_IF_NULL( 00927 keyword = cpl_sprintf("%s%d%s", IFU_VALID_PREFIX, ifu_nr, 00928 IFU_VALID_POSTFIX)); 00929 KMO_TRY_CHECK_ERROR_STATE(); 00930 cpl_propertylist_get_string(main_header, keyword); 00931 cpl_free(keyword); keyword = NULL; 00932 } else { 00933 // if KMOS_RECONSTRUCT_ALL is set all IFUs should be 00934 // reconstructed 00935 cpl_propertylist_get_string(main_header, "ggg"); 00936 } 00937 00938 if ((cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) && 00939 (bounds[2*(ifu_nr-1)] != -1) && 00940 (bounds[2*(ifu_nr-1)+1] != -1)) 00941 { 00942 cpl_error_reset(); 00943 // IFU is valid 00944 actual_sub_header = sub_header; 00945 00946 // 00947 // calc WCS & update subheader 00948 // 00949 KMO_TRY_EXIT_IF_ERROR( 00950 kmo_calc_wcs_gd(main_header, actual_sub_header, ifu_nr, gd)); 00951 00952 KMO_TRY_EXIT_IF_ERROR( 00953 kmclipm_update_property_int(actual_sub_header, 00954 NAXIS, 3, 00955 "number of data axes")); 00956 KMO_TRY_EXIT_IF_ERROR( 00957 kmclipm_update_property_int(actual_sub_header, 00958 NAXIS1, gd.x.dim, 00959 "length of data axis 1")); 00960 KMO_TRY_EXIT_IF_ERROR( 00961 kmclipm_update_property_int(actual_sub_header, 00962 NAXIS2, gd.y.dim, 00963 "length of data axis 2")); 00964 KMO_TRY_EXIT_IF_ERROR( 00965 kmclipm_update_property_int(actual_sub_header, 00966 NAXIS3, gd.l.dim, 00967 "length of data axis 3")); 00968 00969 // reconstruct data and noise (if available) 00970 // if (j == 0) { 00971 // sat_mode_msg = FALSE; 00972 // } else { 00973 // sat_mode_msg = TRUE; 00974 // } 00975 KMO_TRY_EXIT_IF_ERROR( 00976 kmo_reconstruct_sci(ifu_nr, 00977 bounds[2*(ifu_nr-1)], 00978 bounds[2*(ifu_nr-1)+1], 00979 rec_frame, 00980 input_frame_name, 00981 NULL, 00982 NULL, 00983 NULL, 00984 xcal_frame, 00985 ycal_frame, 00986 lcal_frame, 00987 NULL, 00988 &gd, 00989 &cube_data, 00990 &cube_noise, 00991 flux, 00992 background, 00993 xcal_interpolation)); 00994 00995 if (ref_spectrum_frame != NULL && cube_data != NULL) { 00996 KMO_TRY_EXIT_IF_NULL( 00997 lcorr_coeffs = kmo_lcorr_get(cube_data, 00998 actual_sub_header, 00999 ref_spectrum_frame, 01000 gd, 01001 my_filter_id, 01002 ifu_nr)); 01003 01004 cpl_imagelist_delete(cube_data); cube_data = NULL; 01005 if (cube_noise != NULL) { 01006 cpl_imagelist_delete(cube_noise); cube_noise = NULL; 01007 } 01008 KMO_TRY_EXIT_IF_ERROR( 01009 kmo_reconstruct_sci(ifu_nr, 01010 bounds[2*(ifu_nr-1)], 01011 bounds[2*(ifu_nr-1)+1], 01012 rec_frame, 01013 input_frame_name, 01014 NULL, 01015 NULL, 01016 NULL, 01017 xcal_frame, 01018 ycal_frame, 01019 lcal_frame, 01020 lcorr_coeffs, 01021 &gd, 01022 &cube_data, 01023 &cube_noise, 01024 flux, 01025 background, 01026 xcal_interpolation)); 01027 /* 01028 // show that lambda correction improved the data cube, a second one would improve even more 01029 01030 cpl_bivector *obj_spectrum2, *obj_spectrum3; 01031 cpl_polynomial *lcorr_coeffs2, *lcorr_coeffs3; 01032 KMO_TRY_EXIT_IF_NULL( 01033 obj_spectrum2 = kmo_lcorr_extract_spectrum( 01034 cube_data, actual_sub_header, 0.8, NULL)); 01035 01036 KMO_TRY_EXIT_IF_NULL( 01037 lcorr_coeffs2 = kmo_lcorr_crosscorrelate_spectra( 01038 obj_spectrum2, ref_spectrum, peaks, 0.002)); 01039 01040 cpl_bivector_delete(obj_spectrum2); 01041 01042 coeff_dump[0] = 0; 01043 for (ic=0; ic<=cpl_polynomial_get_degree(lcorr_coeffs2) && ic < max_coeffs; ic++) { 01044 pows[0] = ic; 01045 coeff_string = cpl_sprintf(" %*g,", 01046 format_width-2, cpl_polynomial_get_coeff(lcorr_coeffs2,pows)); 01047 strncat(coeff_dump, coeff_string, format_width); 01048 cpl_free(coeff_string); 01049 double c1 = cpl_polynomial_get_coeff(lcorr_coeffs,pows); 01050 double c2 = cpl_polynomial_get_coeff(lcorr_coeffs2,pows); 01051 cpl_polynomial_set_coeff(lcorr_coeffs, pows, c1+c2); 01052 } 01053 cpl_msg_debug("","Lambda correction coeffs for ifu %d %s",ifu_nr, coeff_dump); 01054 cpl_polynomial_delete(lcorr_coeffs2); lcorr_coeffs2=NULL; 01055 01056 cpl_imagelist_delete(cube_data); cube_data = NULL; 01057 cpl_imagelist_delete(cube_noise); cube_noise = NULL; 01058 KMO_TRY_EXIT_IF_ERROR( 01059 kmo_reconstruct_sci(ifu_nr, 01060 bounds[2*(ifu_nr-1)], 01061 bounds[2*(ifu_nr-1)+1], 01062 rec_frame, 01063 input_frame_name, 01064 NULL, 01065 NULL, 01066 NULL, 01067 xcal_frame, 01068 ycal_frame, 01069 lcal_frame, 01070 lcorr_coeffs, 01071 &gd, 01072 &cube_data, 01073 &cube_noise, 01074 flux, 01075 background, 01076 xcal_interpolation)); 01077 01078 KMO_TRY_EXIT_IF_NULL( 01079 obj_spectrum3 = kmo_lcorr_extract_spectrum( 01080 cube_data, actual_sub_header, 0.8, NULL)); 01081 01082 KMO_TRY_EXIT_IF_NULL( 01083 lcorr_coeffs3 = kmo_lcorr_crosscorrelate_spectra( 01084 obj_spectrum3, ref_spectrum, peaks, 0.002)); 01085 01086 cpl_bivector_delete(obj_spectrum3); 01087 01088 coeff_dump[0] = 0; 01089 for (ic=0; ic<=cpl_polynomial_get_degree(lcorr_coeffs3) && ic < max_coeffs; ic++) { 01090 pows[0] = ic; 01091 coeff_string = cpl_sprintf(" %*g,", 01092 format_width-2, cpl_polynomial_get_coeff(lcorr_coeffs3,pows)); 01093 strncat(coeff_dump, coeff_string, format_width); 01094 cpl_free(coeff_string); 01095 } 01096 cpl_msg_debug("","Lambda correction coeffs for iFu %d %s",ifu_nr, coeff_dump); 01097 cpl_polynomial_delete(lcorr_coeffs3); lcorr_coeffs3=NULL; 01098 */ 01099 cpl_polynomial_delete(lcorr_coeffs); lcorr_coeffs = NULL; 01100 01101 } 01102 01103 // scale flux according to pixel_scale 01104 KMO_TRY_EXIT_IF_NULL( 01105 tmp_img = cpl_imagelist_get(cube_data, 0)); 01106 double scaling = (cpl_image_get_size_x(tmp_img)*cpl_image_get_size_y(tmp_img)) / 01107 (KMOS_SLITLET_X*KMOS_SLITLET_Y); 01108 KMO_TRY_EXIT_IF_ERROR( 01109 cpl_imagelist_divide_scalar(cube_data, scaling)); 01110 if (cube_noise != NULL) { 01111 KMO_TRY_EXIT_IF_ERROR( 01112 cpl_imagelist_divide_scalar(cube_noise, scaling)); 01113 } 01114 } else { 01115 // IFU is invalid 01116 actual_sub_header = sub_header_orig; 01117 cpl_error_reset(); 01118 } // if ((cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) .. 01119 01120 if (detectorimage) { 01121 if (cube_data != NULL) { 01122 for (l = 0; l < gd.l.dim; l++) { 01123 KMO_TRY_EXIT_IF_NULL( 01124 slice = cpl_image_get_data_float(cpl_imagelist_get(cube_data, l))); 01125 for (y = 0; y < gd.y.dim; y++) { 01126 for (x = 0; x < gd.x.dim; x++) { 01127 int ix = x + 01128 y * gd.x.dim + 01129 j * gd.x.dim*gd.y.dim + //IFU offset 01130 l * gd.x.dim*gd.y.dim*KMOS_IFUS_PER_DETECTOR; 01131 pdet_img_data[ix] = slice[x + y*gd.x.dim]; 01132 } 01133 } 01134 } 01135 } 01136 if (cube_noise != NULL) { 01137 if (detectorimage) { 01138 detImgCube = TRUE; 01139 } 01140 for (l = 0; l < gd.l.dim; l++) { 01141 KMO_TRY_EXIT_IF_NULL( 01142 slice = cpl_image_get_data_float(cpl_imagelist_get(cube_noise, l))); 01143 for (y = 0; y < gd.y.dim; y++) { 01144 for (x = 0; x < gd.x.dim; x++) { 01145 int ix = x + 01146 y * gd.x.dim + 01147 j * gd.x.dim*gd.y.dim + //IFU offset 01148 l * gd.x.dim*gd.y.dim*KMOS_IFUS_PER_DETECTOR; 01149 pdet_img_noise[ix] = slice[x + y*gd.x.dim]; 01150 } 01151 } 01152 } 01153 } 01154 } 01155 01156 // save output 01157 KMO_TRY_EXIT_IF_NULL( 01158 extname = kmo_extname_creator(ifu_frame, ifu_nr, 01159 EXT_DATA)); 01160 01161 KMO_TRY_EXIT_IF_ERROR( 01162 kmclipm_update_property_string(actual_sub_header, 01163 EXTNAME, 01164 extname, 01165 "FITS extension name")); 01166 01167 cpl_free(extname); extname = NULL; 01168 01169 KMO_TRY_EXIT_IF_ERROR( 01170 kmo_dfs_save_cube(cube_data, filename_cube, obs_suffix, 01171 actual_sub_header, 0./0.)); 01172 01173 if (cube_noise != NULL) { 01174 KMO_TRY_EXIT_IF_NULL( 01175 extname = kmo_extname_creator(ifu_frame, ifu_nr, 01176 EXT_NOISE)); 01177 01178 KMO_TRY_EXIT_IF_ERROR( 01179 kmclipm_update_property_string(actual_sub_header, 01180 EXTNAME, 01181 extname, 01182 "FITS extension name")); 01183 01184 cpl_free(extname); extname = NULL; 01185 01186 KMO_TRY_EXIT_IF_ERROR( 01187 kmo_dfs_save_cube(cube_noise, filename_cube, obs_suffix, 01188 actual_sub_header, 0./0.)); 01189 } 01190 01191 cpl_imagelist_delete(cube_data); cube_data = NULL; 01192 cpl_imagelist_delete(cube_noise); cube_noise = NULL; 01193 cpl_propertylist_delete(sub_header); sub_header = NULL; 01194 cpl_propertylist_delete(sub_header_orig); sub_header_orig = NULL; 01195 } // for j IFUs 01196 01197 if (detectorimage) { 01198 index = kmo_identify_index(cpl_frame_get_filename(rec_frame), 01199 i, FALSE); 01200 KMO_TRY_CHECK_ERROR_STATE(); 01201 01202 KMO_TRY_EXIT_IF_NULL( 01203 tmp_header = kmclipm_propertylist_load( 01204 cpl_frame_get_filename(rec_frame), index)); 01205 KMO_TRY_EXIT_IF_ERROR( 01206 kmo_save_det_img_ext(det_img_data[i-1], gd, i, filename_img, 01207 obs_suffix, tmp_header, dev_flip, FALSE)); 01208 cpl_propertylist_delete(tmp_header); tmp_header = NULL; 01209 01210 if (detImgCube) { 01211 if (desc1.ex_noise) { 01212 index = kmo_identify_index(cpl_frame_get_filename(rec_frame), 01213 i, TRUE); 01214 KMO_TRY_CHECK_ERROR_STATE(); 01215 } else { 01216 // use same index as for data frame, since input frame 01217 // has just 3 extensions 01218 } 01219 KMO_TRY_EXIT_IF_NULL( 01220 tmp_header = kmclipm_propertylist_load( 01221 cpl_frame_get_filename(rec_frame), index)); 01222 KMO_TRY_EXIT_IF_ERROR( 01223 kmo_save_det_img_ext(det_img_noise[i-1], gd, i, filename_img, 01224 obs_suffix, tmp_header, dev_flip, TRUE)); 01225 cpl_propertylist_delete(tmp_header); tmp_header = NULL; 01226 } 01227 } 01228 01229 // free memory 01230 cpl_imagelist_delete(cube_data); cube_data = NULL; 01231 cpl_imagelist_delete(cube_noise); cube_noise = NULL; 01232 cpl_propertylist_delete(sub_header); sub_header = NULL; 01233 cpl_propertylist_delete(sub_header_orig); sub_header_orig = NULL; 01234 } // for i devices 01235 } 01236 KMO_CATCH 01237 { 01238 KMO_CATCH_MSG(); 01239 ret_val = -1; 01240 } 01241 01242 kmo_free_fits_desc(&desc1); 01243 kmo_free_fits_desc(&desc2); 01244 for (i = 0; i < KMOS_NR_DETECTORS; i++) { 01245 cpl_image_delete(det_img_data[i]); det_img_data[i] = NULL; 01246 cpl_image_delete(det_img_noise[i]); det_img_noise[i] = NULL; 01247 } 01248 cpl_free(my_filter_id); my_filter_id = NULL; 01249 cpl_free(bounds); bounds = NULL; 01250 cpl_propertylist_delete(main_header); main_header = NULL; 01251 cpl_propertylist_delete(sub_header); sub_header = NULL; 01252 cpl_propertylist_delete(sub_header_orig); sub_header_orig = NULL; 01253 cpl_imagelist_delete(cube_data); cube_data = NULL; 01254 cpl_imagelist_delete(cube_noise); cube_noise = NULL; 01255 cpl_free(obs_suffix); obs_suffix = NULL; 01256 cpl_free(suffix); suffix = NULL; 01257 cpl_free(filename_img); filename_img = NULL; 01258 cpl_free(filename_cube); filename_cube = NULL; 01259 // sat_mode_msg = FALSE; 01260 01261 return ret_val; 01262 } 01263
1.7.6.1