|
KMOS Pipeline Reference Manual
1.0.7
|
00001 /* $Id: kmo_arithmetic.c,v 1.10 2013/02/01 11:01:57 aagudo 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: aagudo $ 00023 * $Date: 2013/02/01 11:01:57 $ 00024 * $Revision: 1.10 $ 00025 * $Name: HEAD $ 00026 */ 00027 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 00032 #include <string.h> 00033 00034 #include <cpl.h> 00035 00036 #include "kmo_debug.h" 00037 #include "kmo_utils.h" 00038 #include "kmo_dfs.h" 00039 #include "kmo_error.h" 00040 #include "kmo_priv_arithmetic.h" 00041 #include "kmo_constants.h" 00042 00043 static int kmo_arithmetic_create(cpl_plugin *); 00044 static int kmo_arithmetic_exec(cpl_plugin *); 00045 static int kmo_arithmetic_destroy(cpl_plugin *); 00046 static int kmo_arithmetic(cpl_parameterlist *, cpl_frameset *); 00047 00048 static char kmo_arithmetic_description[] = 00049 "With this recipe simple arithmetic operations, like addition, subtraction,\n" 00050 "multiplication, divison and raising to a power can be performed.\n" 00051 "Since FITS files formatted as F1I, F2I and F3I can contain data (and eventually\n" 00052 "noise) of either just one IFU or of all 24 IFUs, kmo_arithmetic behaves diffe-\n" 00053 "rently in these cases.\n" 00054 "When the number of IFUs is the same for both operands, the first IFU of the\n" 00055 "first operand is processed with the first IFU of the second operand.\n" 00056 "When the second operand has only one IFU while the first operand has more IFUs,\n" 00057 "then the all the IFUs of the first operand are processed individually which the\n" 00058 "IFU of the second operand.\n" 00059 "If an operand contains noise and the other doesn't, the noise will not be\n" 00060 "processed.\n" 00061 "\n" 00062 "Noise is only propagated if both operand contain noise extensions. If the\n" 00063 "second operator is a scalar noise is also propagated, of course.\n" 00064 "\n" 00065 "If two cubes are given as operands, they will be combined according to the \n" 00066 "given operator.If a cube is given as first operand and an image as second,\n" 00067 "then it operates on each slice of the cube; similarly if a spectrum is given\n" 00068 "as the second operand, it operates on each spectrum of the cube; and a number\n" 00069 "as the second operand operates on each pixel of the cube.\n" 00070 "\n" 00071 "BASIC PARAMETERS:\n" 00072 "-----------------\n" 00073 "--operator\n" 00074 "Any of the following operations to perform: '+', '-', '*' or '/' (also '^' \n" 00075 "when the 2nd operand is a scalar\n" 00076 "\n" 00077 "--scalar\n" 00078 "To be provided if a frame should be processed together with a scalar\n" 00079 "\n" 00080 "-------------------------------------------------------------------------------\n" 00081 " Input files:\n" 00082 "\n" 00083 " DO KMOS \n" 00084 " category Type Explanation Required #Frames\n" 00085 " -------- ----- ----------- -------- -------\n" 00086 " <none> F3I or Data with or Y 1 \n" 00087 " F2I or without noise frame Y 1 \n" 00088 " F1I or \n" 00089 " F2D or \n" 00090 " RAW \n" 00091 " <none> F3I or Data with or N 0,1 \n" 00092 " F2I or without noise frame \n" 00093 " F1I or \n" 00094 " F2D or \n" 00095 " RAW \n" 00096 "\n" 00097 " Output files:\n" 00098 "\n" 00099 " DO KMOS\n" 00100 " category Type Explanation\n" 00101 " -------- ----- -----------\n" 00102 " ARITHMETIC F3I \n" 00103 " or \n" 00104 " F2D \n" 00105 "-------------------------------------------------------------------------------\n" 00106 "\n"; 00107 00124 int cpl_plugin_get_info(cpl_pluginlist *list) 00125 { 00126 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe); 00127 cpl_plugin *plugin = &recipe->interface; 00128 00129 cpl_plugin_init(plugin, 00130 CPL_PLUGIN_API, 00131 KMOS_BINARY_VERSION, 00132 CPL_PLUGIN_TYPE_RECIPE, 00133 "kmo_arithmetic", 00134 "Perform basic arithmetic on cubes", 00135 kmo_arithmetic_description, 00136 "Alex Agudo Berbel", 00137 "agudo@mpe.mpg.de", 00138 kmos_get_license(), 00139 kmo_arithmetic_create, 00140 kmo_arithmetic_exec, 00141 kmo_arithmetic_destroy); 00142 00143 cpl_pluginlist_append(list, plugin); 00144 00145 return 0; 00146 } 00147 00155 static int kmo_arithmetic_create(cpl_plugin *plugin) 00156 { 00157 cpl_recipe *recipe; 00158 cpl_parameter *p; 00159 00160 /* Check that the plugin is part of a valid recipe */ 00161 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00162 recipe = (cpl_recipe *)plugin; 00163 else 00164 return -1; 00165 00166 /* Create the parameters list in the cpl_recipe object */ 00167 recipe->parameters = cpl_parameterlist_new(); 00168 00169 /* Fill the parameters list */ 00170 /* --operator */ 00171 p = cpl_parameter_new_value("kmos.kmo_arithmetic.operator", 00172 CPL_TYPE_STRING, 00173 "The operator ('+', '-', '*', '/' or '^')", 00174 "kmos.kmo_arithmetic", 00175 ""); 00176 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "operator"); 00177 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00178 cpl_parameterlist_append(recipe->parameters, p); 00179 00180 /* --scalar */ 00181 p = cpl_parameter_new_value("kmos.kmo_arithmetic.scalar", 00182 CPL_TYPE_DOUBLE, 00183 "The scalar operand", 00184 "kmos.kmo_arithmetic", 00185 -DBL_MAX); 00186 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "scalar"); 00187 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); 00188 cpl_parameterlist_append(recipe->parameters, p); 00189 00190 return 0; 00191 } 00192 00198 static int kmo_arithmetic_exec(cpl_plugin *plugin) 00199 { 00200 cpl_recipe *recipe; 00201 00202 /* Get the recipe out of the plugin */ 00203 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00204 recipe = (cpl_recipe *)plugin; 00205 else return -1 ; 00206 00207 return kmo_arithmetic(recipe->parameters, recipe->frames); 00208 } 00209 00215 static int kmo_arithmetic_destroy(cpl_plugin *plugin) 00216 { 00217 cpl_recipe *recipe; 00218 00219 /* Get the recipe out of the plugin */ 00220 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 00221 recipe = (cpl_recipe *)plugin; 00222 else return -1 ; 00223 00224 cpl_parameterlist_delete(recipe->parameters); 00225 return 0 ; 00226 } 00227 00242 static int kmo_arithmetic(cpl_parameterlist *parlist, cpl_frameset *frameset) 00243 { 00244 const char *op = NULL; 00245 00246 cpl_imagelist *op1_3d = NULL, 00247 *op2_3d = NULL, 00248 *op1_noise_3d = NULL, 00249 *op2_noise_3d = NULL; 00250 00251 cpl_image *op1_2d = NULL, 00252 *op2_2d = NULL, 00253 *op1_noise_2d = NULL, 00254 *op2_noise_2d = NULL; 00255 00256 kmclipm_vector *op1_1d = NULL, 00257 *op2_1d = NULL, 00258 *op1_noise_1d = NULL, 00259 *op2_noise_1d = NULL; 00260 00261 double op2_scalar = -DBL_MAX; 00262 00263 int ret_val = 0, 00264 nr_devices = 0, 00265 i = 0, 00266 single_ifu = FALSE, 00267 calc_f3i = FALSE, 00268 calc_f2i = FALSE, 00269 calc_f1i = FALSE, 00270 devnr1 = 0, 00271 devnr2 = 0; 00272 00273 cpl_propertylist *main_header = NULL, 00274 *sub_header_data = NULL, 00275 *sub_header_noise = NULL; 00276 00277 main_fits_desc desc1, 00278 desc2; 00279 00280 cpl_frame *op1_frame = NULL, 00281 *op2_frame = NULL; 00282 00283 KMO_TRY 00284 { 00285 kmo_init_fits_desc(&desc1); 00286 kmo_init_fits_desc(&desc2); 00287 00288 // --- check input --- 00289 KMO_TRY_ASSURE((parlist != NULL) && 00290 (frameset != NULL), 00291 CPL_ERROR_NULL_INPUT, 00292 "Not all input data is provided!"); 00293 00294 KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset, "kmo_arithmetic") == 1, 00295 CPL_ERROR_ILLEGAL_INPUT, 00296 "Cannot identify RAW and CALIB frames!"); 00297 00298 cpl_msg_info("", "--- Parameter setup for kmo_arithmetic ----"); 00299 00300 op2_scalar = kmo_dfs_get_parameter_double(parlist, 00301 "kmos.kmo_arithmetic.scalar"); 00302 KMO_TRY_CHECK_ERROR_STATE(); 00303 00304 KMO_TRY_ASSURE((cpl_frameset_get_size(frameset) == 2) || 00305 ((cpl_frameset_get_size(frameset) == 1) && 00306 (op2_scalar != -DBL_MAX)), 00307 CPL_ERROR_NULL_INPUT, 00308 "Two fits-files or one fits-file and one " 00309 "scalar must be provided!"); 00310 00311 if (cpl_frameset_get_size(frameset) == 1) { 00312 KMO_TRY_EXIT_IF_ERROR( 00313 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_arithmetic.scalar")); 00314 } else { 00315 KMO_TRY_EXIT_IF_NULL( 00316 op2_frame = kmo_dfs_get_frame(frameset, "1")); 00317 } 00318 KMO_TRY_EXIT_IF_NULL( 00319 op1_frame = kmo_dfs_get_frame(frameset, "0")); 00320 00321 KMO_TRY_EXIT_IF_NULL( 00322 op = kmo_dfs_get_parameter_string(parlist, 00323 "kmos.kmo_arithmetic.operator")); 00324 KMO_TRY_ASSURE((strcmp(op, "+") == 0) || 00325 (strcmp(op, "-") == 0) || 00326 (strcmp(op, "*") == 0) || 00327 (strcmp(op, "/") == 0) || 00328 (strcmp(op, "^") == 0), 00329 CPL_ERROR_ILLEGAL_INPUT, 00330 "Operator not valid! Has it been provided?"); 00331 00332 KMO_TRY_EXIT_IF_ERROR( 00333 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_arithmetic.operator")); 00334 00335 cpl_msg_info("", "-------------------------------------------"); 00336 00337 // load descriptor, header and data of first operand 00338 desc1 = kmo_identify_fits_header( 00339 cpl_frame_get_filename(op1_frame)); 00340 KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be " 00341 "in KMOS-format!"); 00342 00343 KMO_TRY_ASSURE((desc1.fits_type == f3i_fits) || 00344 (desc1.fits_type == f2i_fits) || 00345 (desc1.fits_type == f1i_fits) || 00346 (desc1.fits_type == f2d_fits) || 00347 (desc1.fits_type == raw_fits), 00348 CPL_ERROR_ILLEGAL_INPUT, 00349 "First input file hasn't correct data type " 00350 "(KMOSTYPE must be F3I, F2I, F1I, F2D or RAW)!"); 00351 00352 // load descriptor, header of second operand 00353 if (op2_scalar == -DBL_MAX) { 00354 desc2 = kmo_identify_fits_header( 00355 cpl_frame_get_filename(op2_frame)); 00356 KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to " 00357 "be in KMOS-format!"); 00358 00359 if (desc1.fits_type == f3i_fits) { 00360 KMO_TRY_ASSURE((desc2.fits_type == f3i_fits) || 00361 (desc2.fits_type == f2i_fits)|| 00362 (desc2.fits_type == f1i_fits), 00363 CPL_ERROR_ILLEGAL_INPUT, 00364 "For a F3I frame, the 2nd frame must be a " 00365 "F3I, F2I or a F1I frame!"); 00366 00367 if (desc2.fits_type == f3i_fits) { 00368 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) && 00369 (desc1.naxis2 == desc2.naxis2) && 00370 (desc1.naxis3 == desc2.naxis3), 00371 CPL_ERROR_INCOMPATIBLE_INPUT, 00372 "The dimensions of the two operands do " 00373 "not match!"); 00374 } else if (desc2.fits_type == f2i_fits) { 00375 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) && 00376 (desc1.naxis2 == desc2.naxis2), 00377 CPL_ERROR_INCOMPATIBLE_INPUT, 00378 "The dimensions of the two operands do " 00379 "not match!"); 00380 } else if (desc2.fits_type == f1i_fits) { 00381 KMO_TRY_ASSURE((desc1.naxis3 == desc2.naxis1), 00382 CPL_ERROR_INCOMPATIBLE_INPUT, 00383 "The dimensions of the two operands do " 00384 "not match!"); 00385 } 00386 } else if (desc1.fits_type == f2i_fits) { 00387 KMO_TRY_ASSURE(desc2.fits_type == f2i_fits, 00388 CPL_ERROR_ILLEGAL_INPUT, 00389 "For a F2I frame, the 2nd frame must be a " 00390 "F2I frame!"); 00391 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) && 00392 (desc1.naxis2 == desc2.naxis2), 00393 CPL_ERROR_INCOMPATIBLE_INPUT, 00394 "The dimensions of the two operands do " 00395 "not match!"); 00396 } else if (desc1.fits_type == f1i_fits) { 00397 KMO_TRY_ASSURE(desc2.fits_type == f1i_fits, 00398 CPL_ERROR_ILLEGAL_INPUT, 00399 "For a F1I frame, the 2nd frame must be a " 00400 "F1I frame!"); 00401 KMO_TRY_ASSURE(desc1.naxis1 == desc2.naxis1, 00402 CPL_ERROR_INCOMPATIBLE_INPUT, 00403 "The dimensions of the two operands do " 00404 "not match!"); 00405 } else if (desc1.fits_type == f2d_fits) { 00406 KMO_TRY_ASSURE((desc2.fits_type == f2d_fits) || 00407 ((desc2.fits_type == raw_fits) && (desc1.ex_noise == FALSE)), 00408 CPL_ERROR_ILLEGAL_INPUT, 00409 "For a F2D frame, the 2nd frame must be a " 00410 "F2D frame!"); 00411 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) && 00412 (desc1.naxis2 == desc2.naxis2), 00413 CPL_ERROR_INCOMPATIBLE_INPUT, 00414 "The dimensions of the two operands do " 00415 "not match!"); 00416 } else if (desc1.fits_type == raw_fits) { 00417 KMO_TRY_ASSURE((desc2.fits_type == raw_fits) || 00418 ((desc2.fits_type == f2d_fits) && (desc2.ex_noise == FALSE)), 00419 CPL_ERROR_ILLEGAL_INPUT, 00420 "For a RAW frame, the 2nd frame must be a " 00421 "RAW frame!"); 00422 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) && 00423 (desc1.naxis2 == desc2.naxis2), 00424 CPL_ERROR_INCOMPATIBLE_INPUT, 00425 "The dimensions of the two operands do " 00426 "not match!"); 00427 } 00428 00429 if (((desc2.nr_ext == 1) && 00430 (desc2.sub_desc[0].valid_data == TRUE)) 00431 || 00432 ((desc2.nr_ext == 2) && 00433 (desc2.ex_noise == TRUE) && 00434 (desc2.sub_desc[0].valid_data == TRUE) && 00435 (desc2.sub_desc[1].valid_data == TRUE))) { 00436 single_ifu = TRUE; 00437 } else { 00438 if (desc1.ex_noise == desc2.ex_noise) { 00439 KMO_TRY_ASSURE(desc1.nr_ext == desc2.nr_ext, 00440 CPL_ERROR_INCOMPATIBLE_INPUT, 00441 "The number of IFUs of the two operands do " 00442 "not match!"); 00443 } else { 00444 KMO_TRY_ASSURE(desc1.nr_ext == desc2.nr_ext * 2, 00445 CPL_ERROR_INCOMPATIBLE_INPUT, 00446 "The number of IFUs of the two operands do " 00447 "not match!"); 00448 } 00449 } 00450 } 00451 00452 // --- load, update & save primary header --- 00453 KMO_TRY_EXIT_IF_ERROR( 00454 kmo_dfs_save_main_header(frameset, ARITHMETIC, "", op1_frame, NULL, 00455 parlist, cpl_func)); 00456 00457 // 00458 // load data 00459 // 00460 if (desc1.ex_noise == TRUE) { 00461 nr_devices = desc1.nr_ext / 2; 00462 } else { 00463 nr_devices = desc1.nr_ext; 00464 } 00465 00466 if ((single_ifu == TRUE) && 00467 (desc2.sub_desc[0].valid_data == TRUE)) 00468 { 00469 switch (desc2.fits_type) { 00470 case f3i_fits: 00471 00472 KMO_TRY_EXIT_IF_NULL( 00473 op2_3d = kmo_dfs_load_cube(frameset, "1", 00474 desc2.sub_desc[0].device_nr, 00475 FALSE)); 00476 00477 if ((desc1.ex_noise == TRUE) && (desc2.ex_noise == TRUE)) { 00478 KMO_TRY_EXIT_IF_NULL( 00479 op2_noise_3d = kmo_dfs_load_cube(frameset, "1", 00480 desc2.sub_desc[0].device_nr, 00481 TRUE)); 00482 } 00483 break; 00484 case f2i_fits: 00485 KMO_TRY_EXIT_IF_NULL( 00486 op2_2d = kmo_dfs_load_image(frameset, "1", 00487 desc2.sub_desc[0].device_nr, 00488 FALSE, FALSE, NULL)); 00489 00490 if ((desc1.ex_noise == TRUE) && (desc2.ex_noise == TRUE)) { 00491 KMO_TRY_EXIT_IF_NULL( 00492 op2_noise_2d = kmo_dfs_load_image(frameset, 00493 "1", 00494 desc2.sub_desc[0].device_nr, 00495 TRUE, FALSE, NULL)); 00496 } 00497 break; 00498 case f1i_fits: 00499 KMO_TRY_EXIT_IF_NULL( 00500 op2_1d = kmo_dfs_load_vector(frameset, "1", 00501 desc2.sub_desc[0].device_nr, 00502 FALSE)); 00503 00504 if ((desc1.ex_noise == TRUE) && (desc2.ex_noise == TRUE)) { 00505 KMO_TRY_EXIT_IF_NULL( 00506 op2_noise_1d = kmo_dfs_load_vector(frameset, 00507 "1", 00508 desc2.sub_desc[0].device_nr, 00509 TRUE)); 00510 } 00511 break; 00512 default: 00513 break; 00514 } 00515 } 00516 00517 for (i = 1; i <= nr_devices; i++) { 00518 if (desc1.ex_noise == FALSE) { 00519 devnr1 = desc1.sub_desc[i - 1].device_nr; 00520 } else { 00521 devnr1 = desc1.sub_desc[2 * i - 1].device_nr; 00522 } 00523 00524 if (op2_scalar == -DBL_MAX) { 00525 if (desc2.ex_noise == FALSE) { 00526 devnr2 = desc2.sub_desc[i - 1].device_nr; 00527 } else { 00528 devnr2 = desc2.sub_desc[2 * i - 1].device_nr; 00529 } 00530 } 00531 00532 KMO_TRY_EXIT_IF_NULL( 00533 sub_header_data = kmo_dfs_load_sub_header(frameset, "0", 00534 devnr1, 00535 FALSE)); 00536 switch (desc1.fits_type) { 00537 case raw_fits: 00538 // load data 1st operand 00539 KMO_TRY_EXIT_IF_NULL( 00540 op1_2d = kmo_dfs_load_image(frameset, "0", i, 00541 FALSE, TRUE, NULL)); 00542 // 00543 // process RAW & RAW 00544 // process RAW & F2D 00545 // 00546 if ((desc2.fits_type == raw_fits) || 00547 (desc2.fits_type == f2d_fits)) 00548 { 00549 /* load data 2nd operand */ 00550 KMO_TRY_EXIT_IF_NULL( 00551 op2_2d = kmo_dfs_load_image(frameset, "1", i, 00552 FALSE, TRUE, NULL)); 00553 00554 KMO_TRY_EXIT_IF_ERROR( 00555 kmo_arithmetic_2D_2D(op1_2d, op2_2d, NULL, NULL, op)); 00556 } 00557 00558 // 00559 // process RAW & scalar 00560 // 00561 else if (op2_scalar != -DBL_MAX) { 00562 KMO_TRY_EXIT_IF_ERROR( 00563 kmo_arithmetic_2D_scalar(op1_2d, op2_scalar, NULL, 00564 op)); 00565 } 00566 00567 KMO_TRY_EXIT_IF_ERROR( 00568 kmo_update_sub_keywords(sub_header_data, FALSE, FALSE, 00569 detector_frame, i)); 00570 00571 KMO_TRY_EXIT_IF_ERROR( 00572 kmo_dfs_save_image(op1_2d, ARITHMETIC, "", 00573 sub_header_data, 0./0.)); 00574 break; 00575 case f2d_fits: 00576 KMO_TRY_EXIT_IF_NULL( 00577 op1_2d = kmo_dfs_load_image(frameset, "0", i, 00578 FALSE, FALSE, NULL)); 00579 // 00580 // process F2D & F2D 00581 // process F2D & RAW 00582 // 00583 if ((desc2.fits_type == f2d_fits) || 00584 (desc2.fits_type == raw_fits)) 00585 { 00586 KMO_TRY_EXIT_IF_NULL( 00587 op2_2d = kmo_dfs_load_image(frameset, "1", i, 00588 FALSE, FALSE, NULL)); 00589 // load noise 00590 if ((desc1.ex_noise == TRUE) && 00591 (desc2.ex_noise == TRUE)) { 00592 KMO_TRY_EXIT_IF_NULL( 00593 op1_noise_2d = kmo_dfs_load_image(frameset, 00594 "0", i, TRUE, FALSE, NULL)); 00595 00596 KMO_TRY_EXIT_IF_NULL( 00597 op2_noise_2d = kmo_dfs_load_image(frameset, 00598 "1", i, TRUE, FALSE, NULL)); 00599 } 00600 KMO_TRY_EXIT_IF_ERROR( 00601 kmo_arithmetic_2D_2D(op1_2d, op2_2d, 00602 op1_noise_2d, op2_noise_2d, 00603 op)); 00604 } 00605 // 00606 // process F2D & scalar 00607 // 00608 else if (op2_scalar != -DBL_MAX) { 00609 // process data & noise 00610 if (desc1.ex_noise == TRUE) { 00611 KMO_TRY_EXIT_IF_NULL( 00612 op1_noise_2d = kmo_dfs_load_image(frameset, 00613 "0", i, TRUE, FALSE, NULL)); 00614 } 00615 KMO_TRY_EXIT_IF_ERROR( 00616 kmo_arithmetic_2D_scalar(op1_2d, 00617 op2_scalar, 00618 op1_noise_2d, 00619 op)); 00620 } 00621 00622 // save data (and noise) 00623 KMO_TRY_EXIT_IF_ERROR( 00624 kmo_dfs_save_image(op1_2d, ARITHMETIC, "", 00625 sub_header_data, 0./0.)); 00626 00627 if (op1_noise_2d != NULL) { 00628 KMO_TRY_EXIT_IF_NULL( 00629 sub_header_noise = kmo_dfs_load_sub_header(frameset, 00630 "0", i, TRUE)); 00631 KMO_TRY_EXIT_IF_ERROR( 00632 kmo_dfs_save_image(op1_noise_2d, ARITHMETIC, "", 00633 sub_header_noise, 0./0.)); 00634 00635 cpl_propertylist_delete(sub_header_noise); 00636 sub_header_noise = NULL; 00637 } 00638 break; 00639 case f3i_fits: 00640 calc_f3i = FALSE; 00641 00642 // check if IFUs are valid 00643 if (desc1.ex_noise == FALSE) { 00644 if (desc1.sub_desc[i - 1].valid_data == TRUE) { 00645 if (op2_scalar != -DBL_MAX) { 00646 calc_f3i = TRUE; 00647 } else if (((single_ifu == TRUE) && 00648 (desc2.sub_desc[0].valid_data == TRUE)) 00649 || 00650 (desc2.sub_desc[i - 1].valid_data == TRUE)) 00651 { 00652 calc_f3i = TRUE; 00653 } 00654 } 00655 } 00656 if (desc1.ex_noise == TRUE) { 00657 if (desc1.sub_desc[2 * i - 1].valid_data == TRUE) { 00658 calc_f3i = TRUE; 00659 } 00660 } 00661 if ((desc1.ex_noise == TRUE) && (desc2.ex_noise == TRUE)) { 00662 if (desc1.sub_desc[2 * i - 1].valid_data == TRUE) { 00663 if (((single_ifu == TRUE) && 00664 (desc2.sub_desc[1].valid_data == TRUE)) 00665 || 00666 (desc2.sub_desc[2 * i - 1].valid_data == TRUE)) 00667 { 00668 calc_f3i = TRUE; 00669 } 00670 } 00671 } 00672 if ((single_ifu == TRUE) && (desc1.ex_noise == FALSE)) { 00673 if ((desc1.sub_desc[i - 1].valid_data == TRUE) && 00674 (desc2.sub_desc[0].valid_data == TRUE)) { 00675 calc_f3i = TRUE; 00676 } 00677 } 00678 00679 if (calc_f3i == TRUE) 00680 { 00681 KMO_TRY_EXIT_IF_NULL( 00682 op1_3d = kmo_dfs_load_cube(frameset, "0", 00683 devnr1, FALSE)); 00684 // 00685 // process F3I & F3I 00686 // 00687 if (desc2.fits_type == f3i_fits) { 00688 if (single_ifu == FALSE) { 00689 KMO_TRY_EXIT_IF_NULL( 00690 op2_3d = kmo_dfs_load_cube(frameset, 00691 "1", devnr2, 00692 FALSE)); 00693 } 00694 00695 if ((desc1.ex_noise == TRUE) && 00696 (desc2.ex_noise == TRUE)) 00697 { 00698 KMO_TRY_EXIT_IF_NULL( 00699 op1_noise_3d = kmo_dfs_load_cube(frameset, 00700 "0", devnr1, 00701 TRUE)); 00702 00703 if (single_ifu == FALSE) { 00704 KMO_TRY_EXIT_IF_NULL( 00705 op2_noise_3d = kmo_dfs_load_cube( 00706 frameset, 00707 "1", devnr2, 00708 TRUE)); 00709 } 00710 } 00711 00712 KMO_TRY_EXIT_IF_ERROR( 00713 kmo_arithmetic_3D_3D(op1_3d, op2_3d, 00714 op1_noise_3d, op2_noise_3d, 00715 op)); 00716 } 00717 00718 // 00719 // process F3I & F2I 00720 // 00721 else if (desc2.fits_type == f2i_fits) { 00722 if (single_ifu == FALSE) { 00723 KMO_TRY_EXIT_IF_NULL( 00724 op2_2d = kmo_dfs_load_image(frameset, 00725 "1", devnr2, 00726 FALSE, FALSE, NULL)); 00727 } 00728 00729 if ((desc1.ex_noise == TRUE) && 00730 (desc2.ex_noise == TRUE)) 00731 { 00732 KMO_TRY_EXIT_IF_NULL( 00733 op1_noise_3d = kmo_dfs_load_cube(frameset, 00734 "0", devnr1, 00735 TRUE)); 00736 00737 if (single_ifu == FALSE) { 00738 KMO_TRY_EXIT_IF_NULL( 00739 op2_noise_2d = kmo_dfs_load_image( 00740 frameset, 00741 "1", devnr2, 00742 TRUE, FALSE, NULL)); 00743 } 00744 } 00745 00746 KMO_TRY_EXIT_IF_ERROR( 00747 kmo_arithmetic_3D_2D(op1_3d, op2_2d, 00748 op1_noise_3d, op2_noise_2d, 00749 op)); 00750 } 00751 // 00752 // process F3I & F1I 00753 // 00754 else if (desc2.fits_type == f1i_fits) { 00755 if (single_ifu == FALSE) { 00756 KMO_TRY_EXIT_IF_NULL( 00757 op2_1d = kmo_dfs_load_vector(frameset, 00758 "1", devnr2, 00759 FALSE)); 00760 } 00761 00762 if ((desc1.ex_noise == TRUE) && 00763 (desc2.ex_noise == TRUE)) 00764 { 00765 KMO_TRY_EXIT_IF_NULL( 00766 op1_noise_3d = kmo_dfs_load_cube(frameset, 00767 "0", devnr1, 00768 TRUE)); 00769 00770 if (single_ifu == FALSE) { 00771 KMO_TRY_EXIT_IF_NULL( 00772 op2_noise_1d = kmo_dfs_load_vector( 00773 frameset, 00774 "1", devnr2, 00775 TRUE)); 00776 } 00777 } 00778 00779 KMO_TRY_EXIT_IF_ERROR( 00780 kmo_arithmetic_3D_1D(op1_3d, op2_1d, 00781 op1_noise_3d, op2_noise_1d, 00782 op)); 00783 } 00784 // 00785 // process F3I & scalar 00786 // 00787 else if (op2_scalar != -DBL_MAX) { 00788 if (desc1.ex_noise == TRUE) { 00789 KMO_TRY_EXIT_IF_NULL( 00790 op1_noise_3d = kmo_dfs_load_cube(frameset, 00791 "0", devnr1, 00792 TRUE)); 00793 } 00794 00795 KMO_TRY_EXIT_IF_ERROR( 00796 kmo_arithmetic_3D_scalar(op1_3d, 00797 op2_scalar, 00798 op1_noise_3d, 00799 op)); 00800 } 00801 00802 // save data (and noise) 00803 KMO_TRY_EXIT_IF_ERROR( 00804 kmo_dfs_save_cube(op1_3d, ARITHMETIC, "", 00805 sub_header_data, 0./0.)); 00806 00807 if (op1_noise_3d != NULL) { 00808 KMO_TRY_EXIT_IF_NULL( 00809 sub_header_noise = kmo_dfs_load_sub_header( 00810 frameset, 00811 "0", devnr1, 00812 TRUE)); 00813 00814 KMO_TRY_EXIT_IF_ERROR( 00815 kmo_dfs_save_cube(op1_noise_3d, ARITHMETIC, "", 00816 sub_header_noise, 0./0.)); 00817 00818 cpl_propertylist_delete(sub_header_noise); 00819 sub_header_noise = NULL; 00820 } 00821 } else { 00822 // 00823 // invalid IFU, just save sub_header 00824 // 00825 KMO_TRY_EXIT_IF_ERROR( 00826 kmo_dfs_save_sub_header(ARITHMETIC, "", 00827 sub_header_data)); 00828 00829 // save noise if it has been calculated 00830 if ((desc1.ex_noise == TRUE) && 00831 ((((desc2.fits_type == f3i_fits) || 00832 (desc2.fits_type == f2i_fits) || 00833 (desc2.fits_type == f1i_fits) 00834 ) && 00835 (desc2.ex_noise == TRUE) 00836 ) || 00837 (op2_scalar != -DBL_MAX) 00838 ) 00839 ) 00840 { 00841 00842 KMO_TRY_EXIT_IF_NULL( 00843 sub_header_noise = kmo_dfs_load_sub_header( 00844 frameset, 00845 "0", devnr1, 00846 TRUE)); 00847 KMO_TRY_EXIT_IF_ERROR( 00848 kmo_dfs_save_sub_header(ARITHMETIC, "", 00849 sub_header_noise)); 00850 00851 cpl_propertylist_delete(sub_header_noise); 00852 sub_header_noise = NULL; 00853 } 00854 } 00855 break; 00856 case f2i_fits: 00857 calc_f2i = FALSE; 00858 00859 // check if IFUs are valid 00860 if (desc1.ex_noise == FALSE) { 00861 if (desc1.sub_desc[i - 1].valid_data == TRUE) { 00862 if (op2_scalar != -DBL_MAX) { 00863 calc_f2i = TRUE; 00864 } else if (((single_ifu == TRUE) && 00865 (desc2.sub_desc[0].valid_data == TRUE)) 00866 || 00867 (desc2.sub_desc[i - 1].valid_data == TRUE)) 00868 { 00869 calc_f2i = TRUE; 00870 } 00871 } 00872 } 00873 if (desc1.ex_noise == TRUE) { 00874 if (desc1.sub_desc[2 * i - 1].valid_data == TRUE) { 00875 calc_f2i = TRUE; 00876 } 00877 } 00878 if ((desc1.ex_noise == TRUE) && (desc2.ex_noise == TRUE)) { 00879 if (desc1.sub_desc[2 * i - 1].valid_data == TRUE) { 00880 if (((single_ifu == TRUE) && 00881 (desc2.sub_desc[1].valid_data == TRUE)) 00882 || 00883 (desc2.sub_desc[2 * i - 1].valid_data == TRUE)) 00884 { 00885 calc_f2i = TRUE; 00886 } 00887 } 00888 } 00889 if ((single_ifu == TRUE) && (desc1.ex_noise == FALSE)) { 00890 if ((desc1.sub_desc[i - 1].valid_data == TRUE) && 00891 (desc2.sub_desc[0].valid_data == TRUE)) { 00892 calc_f2i = TRUE; 00893 } 00894 } 00895 00896 if (calc_f2i == TRUE) 00897 { 00898 KMO_TRY_EXIT_IF_NULL( 00899 op1_2d = kmo_dfs_load_image(frameset, "0", 00900 devnr1, FALSE, FALSE, NULL)); 00901 // 00902 // process F2I & F2I 00903 // 00904 if (desc2.fits_type == f2i_fits) { 00905 if (single_ifu == FALSE) { 00906 KMO_TRY_EXIT_IF_NULL( 00907 op2_2d = kmo_dfs_load_image(frameset, "1", 00908 devnr2, FALSE, FALSE, NULL)); 00909 } 00910 00911 if ((desc1.ex_noise == TRUE) && 00912 (desc2.ex_noise == TRUE)) 00913 { 00914 KMO_TRY_EXIT_IF_NULL( 00915 op1_noise_2d = kmo_dfs_load_image( 00916 frameset, "0", 00917 devnr1, TRUE, FALSE, NULL)); 00918 00919 if (single_ifu == FALSE) { 00920 KMO_TRY_EXIT_IF_NULL( 00921 op2_noise_2d = kmo_dfs_load_image( 00922 frameset, "1", 00923 devnr2, TRUE, FALSE, NULL)); 00924 } 00925 } 00926 00927 KMO_TRY_EXIT_IF_ERROR( 00928 kmo_arithmetic_2D_2D(op1_2d, op2_2d, 00929 op1_noise_2d, op2_noise_2d, 00930 op)); 00931 } 00932 // 00933 // process F2I & scalar 00934 // 00935 else if (op2_scalar != -DBL_MAX) { 00936 if (desc1.ex_noise == TRUE) { 00937 KMO_TRY_EXIT_IF_NULL( 00938 op1_noise_2d = kmo_dfs_load_image( 00939 frameset, "0", 00940 devnr1, TRUE, FALSE, NULL)); 00941 } 00942 00943 KMO_TRY_EXIT_IF_ERROR( 00944 kmo_arithmetic_2D_scalar(op1_2d, 00945 op2_scalar, 00946 op1_noise_2d, 00947 op)); 00948 } 00949 00950 // save data (and noise) 00951 KMO_TRY_EXIT_IF_ERROR( 00952 kmo_dfs_save_image(op1_2d, ARITHMETIC, "", 00953 sub_header_data, 0./0.)); 00954 00955 if (op1_noise_2d != NULL) { 00956 KMO_TRY_EXIT_IF_NULL( 00957 sub_header_noise = kmo_dfs_load_sub_header( 00958 frameset, "0", 00959 devnr1, TRUE)); 00960 00961 KMO_TRY_EXIT_IF_ERROR( 00962 kmo_dfs_save_image(op1_noise_2d, ARITHMETIC, "", 00963 sub_header_noise, 0./0.)); 00964 00965 cpl_propertylist_delete(sub_header_noise); 00966 sub_header_noise = NULL; 00967 } 00968 } else { 00969 // 00970 // invalid IFU, just save sub_header 00971 // 00972 KMO_TRY_EXIT_IF_ERROR( 00973 kmo_dfs_save_sub_header(ARITHMETIC, "", sub_header_data)); 00974 00975 // save noise if it has been calculated 00976 if ((desc1.ex_noise == TRUE) 00977 && 00978 (((desc2.fits_type == f2i_fits) && (desc2.ex_noise == TRUE)) || 00979 (op2_scalar != -DBL_MAX))) 00980 { 00981 00982 KMO_TRY_EXIT_IF_NULL( 00983 sub_header_noise = kmo_dfs_load_sub_header( 00984 frameset, 00985 "0", devnr1, 00986 TRUE)); 00987 KMO_TRY_EXIT_IF_ERROR( 00988 kmo_dfs_save_sub_header(ARITHMETIC, "", sub_header_noise)); 00989 00990 cpl_propertylist_delete(sub_header_noise); 00991 sub_header_noise = NULL; 00992 } 00993 } 00994 break; 00995 case f1i_fits: 00996 calc_f1i = FALSE; 00997 00998 // check if IFUs are valid 00999 if (desc1.ex_noise == FALSE) { 01000 if (desc1.sub_desc[i - 1].valid_data == TRUE) { 01001 if (op2_scalar != -DBL_MAX) { 01002 calc_f1i = TRUE; 01003 } else if (((single_ifu == TRUE) && 01004 (desc2.sub_desc[0].valid_data == TRUE)) 01005 || 01006 (desc2.sub_desc[i - 1].valid_data == TRUE)) 01007 { 01008 calc_f1i = TRUE; 01009 } 01010 } 01011 } 01012 if (desc1.ex_noise == TRUE) { 01013 if (desc1.sub_desc[2 * i - 1].valid_data == TRUE) { 01014 calc_f1i = TRUE; 01015 } 01016 } 01017 if ((desc1.ex_noise == TRUE) && (desc2.ex_noise == TRUE)) { 01018 if (desc1.sub_desc[2 * i - 1].valid_data == TRUE) { 01019 if (((single_ifu == TRUE) && 01020 (desc2.sub_desc[1].valid_data == TRUE)) 01021 || 01022 (desc2.sub_desc[2 * i - 1].valid_data == TRUE)) 01023 { 01024 calc_f1i = TRUE; 01025 } 01026 } 01027 } 01028 if ((single_ifu == TRUE) && (desc1.ex_noise == FALSE)) { 01029 if ((desc1.sub_desc[i - 1].valid_data == TRUE) && 01030 (desc2.sub_desc[0].valid_data == TRUE)) { 01031 calc_f1i = TRUE; 01032 } 01033 } 01034 01035 if (calc_f1i == TRUE) 01036 { 01037 KMO_TRY_EXIT_IF_NULL( 01038 op1_1d = kmo_dfs_load_vector(frameset, "0", 01039 devnr1, FALSE)); 01040 // 01041 // process F1I & F1I 01042 // 01043 if (desc2.fits_type == f1i_fits) { 01044 if (single_ifu == FALSE) { 01045 KMO_TRY_EXIT_IF_NULL( 01046 op2_1d = kmo_dfs_load_vector(frameset, "1", 01047 devnr2, FALSE)); 01048 } 01049 01050 if ((desc1.ex_noise == TRUE) && 01051 (desc2.ex_noise == TRUE)) 01052 { 01053 KMO_TRY_EXIT_IF_NULL( 01054 op1_noise_1d = kmo_dfs_load_vector( 01055 frameset, "0", 01056 devnr1, TRUE)); 01057 01058 if (single_ifu == FALSE) { 01059 KMO_TRY_EXIT_IF_NULL( 01060 op2_noise_1d = kmo_dfs_load_vector( 01061 frameset, "1", 01062 devnr2, TRUE)); 01063 } 01064 } 01065 01066 KMO_TRY_EXIT_IF_ERROR( 01067 kmo_arithmetic_1D_1D(op1_1d, op2_1d, 01068 op1_noise_1d, op2_noise_1d, 01069 op)); 01070 } 01071 // 01072 // process F1I & scalar 01073 // 01074 else if (op2_scalar != -DBL_MAX) { 01075 if (desc1.ex_noise == TRUE) { 01076 KMO_TRY_EXIT_IF_NULL( 01077 op1_noise_1d = kmo_dfs_load_vector( 01078 frameset, "0", 01079 devnr1, TRUE)); 01080 } 01081 01082 KMO_TRY_EXIT_IF_ERROR( 01083 kmo_arithmetic_1D_scalar(op1_1d, 01084 op2_scalar, 01085 op1_noise_1d, 01086 op)); 01087 } 01088 01089 // save data (and noise) 01090 KMO_TRY_EXIT_IF_ERROR( 01091 kmo_dfs_save_vector(op1_1d, ARITHMETIC, "", 01092 sub_header_data, 0./0.)); 01093 01094 if (op1_noise_1d != NULL) { 01095 KMO_TRY_EXIT_IF_NULL( 01096 sub_header_noise = kmo_dfs_load_sub_header( 01097 frameset, "0", 01098 devnr1, TRUE)); 01099 01100 KMO_TRY_EXIT_IF_ERROR( 01101 kmo_dfs_save_vector(op1_noise_1d, ARITHMETIC, "", 01102 sub_header_noise, 0./0.)); 01103 01104 cpl_propertylist_delete(sub_header_noise); 01105 sub_header_noise = NULL; 01106 } 01107 } else { 01108 // 01109 // invalid IFU, just save sub_header 01110 // 01111 KMO_TRY_EXIT_IF_ERROR( 01112 kmo_dfs_save_sub_header(ARITHMETIC, "", sub_header_data)); 01113 01114 // save noise if it has been calculated 01115 if ((desc1.ex_noise == TRUE) 01116 && 01117 (((desc2.fits_type == f2i_fits) && (desc2.ex_noise == TRUE)) || 01118 (op2_scalar != -DBL_MAX))) 01119 { 01120 01121 KMO_TRY_EXIT_IF_NULL( 01122 sub_header_noise = kmo_dfs_load_sub_header( 01123 frameset, 01124 "0", devnr1, 01125 TRUE)); 01126 KMO_TRY_EXIT_IF_ERROR( 01127 kmo_dfs_save_sub_header(ARITHMETIC, "", 01128 sub_header_noise)); 01129 01130 cpl_propertylist_delete(sub_header_noise); 01131 sub_header_noise = NULL; 01132 } 01133 } 01134 break; 01135 default: 01136 break; 01137 } 01138 01139 cpl_propertylist_delete(sub_header_data); sub_header_data = NULL; 01140 01141 cpl_image_delete(op1_2d); op1_2d = NULL; 01142 cpl_imagelist_delete(op1_3d); op1_3d = NULL; 01143 01144 cpl_image_delete(op1_noise_2d); op1_noise_2d = NULL; 01145 cpl_imagelist_delete(op1_noise_3d); op1_noise_3d = NULL; 01146 01147 if (single_ifu == FALSE) { 01148 kmclipm_vector_delete(op2_1d); op2_1d = NULL, 01149 cpl_image_delete(op2_2d); op2_2d = NULL; 01150 cpl_imagelist_delete(op2_3d); op2_3d = NULL; 01151 01152 kmclipm_vector_delete(op2_noise_1d); op2_noise_1d = NULL, 01153 cpl_image_delete(op2_noise_2d); op2_noise_2d = NULL; 01154 cpl_imagelist_delete(op2_noise_3d); op2_noise_3d = NULL; 01155 } 01156 } 01157 } 01158 KMO_CATCH 01159 { 01160 KMO_CATCH_MSG(); 01161 ret_val = -1; 01162 } 01163 01164 kmo_free_fits_desc(&desc1); 01165 kmo_free_fits_desc(&desc2); 01166 cpl_propertylist_delete(main_header); main_header = NULL; 01167 cpl_propertylist_delete(sub_header_data); sub_header_data = NULL; 01168 cpl_propertylist_delete(sub_header_noise); sub_header_noise = NULL; 01169 cpl_image_delete(op1_2d); op1_2d = NULL; 01170 cpl_imagelist_delete(op1_3d); op1_3d = NULL; 01171 kmclipm_vector_delete(op2_1d); op2_1d = NULL; 01172 cpl_image_delete(op2_2d); op2_2d = NULL; 01173 cpl_imagelist_delete(op2_3d); op2_3d = NULL; 01174 cpl_image_delete(op1_noise_2d); op1_noise_2d = NULL; 01175 cpl_imagelist_delete(op1_noise_3d); op1_noise_3d = NULL; 01176 kmclipm_vector_delete(op2_noise_1d); op2_noise_1d = NULL; 01177 cpl_image_delete(op2_noise_2d); op2_noise_2d = NULL; 01178 cpl_imagelist_delete(op2_noise_3d); op2_noise_3d = NULL; 01179 01180 return ret_val; 01181 } 01182
1.7.6.1