KMOS Pipeline Reference Manual 4.5.10
kmo_stats.c
1/*
2 * This file is part of the KMOS Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 * Includes
26 *----------------------------------------------------------------------------*/
27
28#include <string.h>
29
30#include <cpl.h>
31
32#include "kmo_utils.h"
33#include "kmo_dfs.h"
34#include "kmo_priv_stats.h"
35#include "kmo_priv_functions.h"
36#include "kmo_error.h"
37#include "kmo_debug.h"
38#include "kmo_constants.h"
39
40/*-----------------------------------------------------------------------------
41 * Functions prototypes
42 *----------------------------------------------------------------------------*/
43
44static int kmo_stats_create(cpl_plugin *);
45static int kmo_stats_exec(cpl_plugin *);
46static int kmo_stats_destroy(cpl_plugin *);
47static int kmo_stats(cpl_parameterlist *, cpl_frameset *);
48
49/*-----------------------------------------------------------------------------
50 * Static variables
51 *----------------------------------------------------------------------------*/
52
53static char kmo_stats_description[] =
54"This recipe performs basic statistics on KMOS-conform data-frames of type F2D,\n"
55"F1I, F2I and F3I either with or without noise and RAW. Optionally a 2D mask\n"
56"can be provided to define a region on which the statistics should be calculated\n"
57"on (mask 0: exclude pixel, mask 1: include pixel). A mask can’t be provided for\n"
58"statistics on F1I frames.\n"
59"The output is stored in a vector of length 11. The vector represents following\n"
60"values:\n"
61" 1. Number of pixels\n"
62" 2. Number of finite pixels\n"
63" 3. Mean\n"
64" 4. Standard Deviation\n"
65" 5. Mean with iterative rejection (i.e. mean & sigma are calculated iterati-\n"
66" vely, each time rejecting pixels more than +/-N sigma from the mean)\n"
67" 6. Standard Deviation with iterative rejection\n"
68" 7. Median\n"
69" 8. Mode (i.e. the peak in a histogram of pixel values)\n"
70" 9. Noise (a robust estimate given by the standard deviation from the nega-\n"
71" tive side of the histogram of pixel values)\n"
72" 10. Minimum\n"
73" 11. Maximum\n"
74"\n"
75"The same numerical operations are applied to the noise as with the data itself.\n"
76"\n"
77"BASIC PARAMETERS:\n"
78"-----------------\n"
79"--ext\n"
80"These parameters specify with extensions to process. The value 0, which is\n"
81"default, calulates all extensions.\n"
82"\n"
83"ADVANCED PARAMETERS\n"
84"-------------------\n"
85"--cpos_rej\n"
86"--cneg_rej\n"
87"--citer\n"
88"An iterative sigma clipping is applied in order to calculate the mode (using\n"
89"kmo_stats). For each position all pixels in the spectrum are examined. If they\n"
90"deviate significantly, they will be rejected according to the conditions:\n"
91" val > mean + stdev * cpos_rej\n"
92" and\n"
93" val < mean - stdev * cneg_rej\n"
94"In the first iteration median and percentile level are used.\n"
95"\n"
96"-------------------------------------------------------------------------------\n"
97
98" Input files:\n"
99"\n"
100" DO DO KMOS \n"
101" category group Type Explanation Required #Frames\n"
102" -------- ----- ----- ----------- -------- -------\n"
103" <none or any> - F3I or The datacubes Y 1 \n"
104" F2I or \n"
105" F1I or \n"
106" F2D or \n"
107" B2D or \n"
108" RAW \n"
109" <none or any> - F2I or The mask N 0,1 \n"
110" F2D or \n"
111" B2D or \n"
112" RAW \n"
113"\n"
114" Output files:\n"
115"\n"
116" DO KMOS\n"
117" category Type Explanation\n"
118" -------- ----- -----------\n"
119" STATS F1I Calculated statistics parameters \n"
120"-------------------------------------------------------------------------------\n"
121"\n";
122
123/*-----------------------------------------------------------------------------
124 * Functions code
125 *----------------------------------------------------------------------------*/
126
143int cpl_plugin_get_info(cpl_pluginlist *list)
144{
145 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
146 cpl_plugin *plugin = &recipe->interface;
147
148 cpl_plugin_init(plugin,
149 CPL_PLUGIN_API,
150 KMOS_BINARY_VERSION,
151 CPL_PLUGIN_TYPE_RECIPE,
152 "kmo_stats",
153 "Perform basic statistics on a KMOS-conform fits-file",
154 kmo_stats_description,
155 "Alex Agudo Berbel",
156 "https://support.eso.org/",
157 kmos_get_license(),
158 kmo_stats_create,
159 kmo_stats_exec,
160 kmo_stats_destroy);
161
162 cpl_pluginlist_append(list, plugin);
163
164 return 0;
165}
166
174static int kmo_stats_create(cpl_plugin *plugin)
175{
176 cpl_recipe *recipe;
177 cpl_parameter *p;
178
179 /* Check that the plugin is part of a valid recipe */
180 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
181 recipe = (cpl_recipe *)plugin;
182 else
183 return -1;
184
185 /* Create the parameters list in the cpl_recipe object */
186 recipe->parameters = cpl_parameterlist_new();
187
188 /* --ext */
189 p = cpl_parameter_new_value("kmos.kmo_stats.ext",
190 CPL_TYPE_INT,
191 "The extension the stats should be calculated "
192 "of. Zero is default and calculates the stats "
193 "of all extensions",
194 "kmos.kmo_stats",
195 0);
196 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ext");
197 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
198 cpl_parameterlist_append(recipe->parameters, p);
199
200 /* Fill the parameters list */
201 return kmos_combine_pars_create(recipe->parameters,
202 "kmos.kmo_stats",
203 DEF_REJ_METHOD,
204 TRUE);
205}
206
212static int kmo_stats_exec(cpl_plugin *plugin)
213{
214 cpl_recipe *recipe;
215
216 /* Get the recipe out of the plugin */
217 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
218 recipe = (cpl_recipe *)plugin;
219 else return -1 ;
220
221 return kmo_stats(recipe->parameters, recipe->frames);
222}
223
229static int kmo_stats_destroy(cpl_plugin *plugin)
230{
231 cpl_recipe *recipe;
232
233 /* Get the recipe out of the plugin */
234 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
235 recipe = (cpl_recipe *)plugin;
236 else return -1 ;
237
238 cpl_parameterlist_delete(recipe->parameters);
239 return 0 ;
240}
241
256static int kmo_stats(cpl_parameterlist *parlist, cpl_frameset *frameset)
257{
258 double cpos_rej = 0.0,
259 cneg_rej = 0.0;
260
261 cpl_imagelist *data_in = NULL;
262
263 cpl_image *mask = NULL,
264 *img_in = NULL;
265
266 kmclipm_vector *vec_in = NULL,
267 *mask_vec = NULL,
268 *data_out = NULL;
269
270 int ret_val = 0,
271 nr_devices = 0,
272 i = 0,
273 j = 0,
274 tmpi = 0,
275 valid_ifu = FALSE,
276 citer = 0,
277 mask_available = FALSE,
278 ifu = 0,
279 stats_size = 0,
280 extnr = 0,
281 devnr = 0,
282 index_data = 0,
283 index_noise = 0;
284
285 cpl_propertylist *sub_header = NULL;
286
287 main_fits_desc desc1,
288 desc2;
289
290 cpl_frame *data_frame = NULL,
291 *mask_frame = NULL;
292
293 char do_mode1[256],
294 do_mode2[256];
295
296 const char *cmethod = "ksigma";
297
298 char *tmp_str = NULL,
299 **strarr = NULL;
300
301 KMO_TRY
302 {
303 kmo_init_fits_desc(&desc1);
304
305 // --- check inputs ---
306 KMO_TRY_ASSURE((parlist != NULL) &&
307 (frameset != NULL),
308 CPL_ERROR_NULL_INPUT,
309 "Not all input data is provided!");
310
311 KMO_TRY_ASSURE((cpl_frameset_get_size(frameset) == 1) ||
312 ((cpl_frameset_get_size(frameset) == 2)),
313 CPL_ERROR_NULL_INPUT,
314 "Either a cube (F3I) or a cube and a mask (F2I) "
315 "must be provided!");
316
317 KMO_TRY_ASSURE(kmo_dfs_set_groups(frameset) == 1,
318 CPL_ERROR_ILLEGAL_INPUT,
319 "Cannot identify RAW and CALIB frames!");
320
321 if (cpl_frameset_get_size(frameset) == 1) {
322 strcpy(do_mode1, "0");
323 strcpy(do_mode2, "");
324 } else {
325 strcpy(do_mode1, "0");
326 strcpy(do_mode2, "1");
327 KMO_TRY_EXIT_IF_NULL(
328 mask_frame = kmo_dfs_get_frame(frameset, do_mode2));
329 }
330 KMO_TRY_EXIT_IF_NULL(
331 data_frame = kmo_dfs_get_frame(frameset, do_mode1));
332
333 cpl_msg_info("", "--- Parameter setup for kmo_stats ---------");
334
335 // load descriptor of first operand
336 desc1 = kmo_identify_fits_header(
337 cpl_frame_get_filename(data_frame));
338 KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
339 "in KMOS-format!");
340
341 KMO_TRY_ASSURE((desc1.fits_type == f3i_fits) ||
342 (desc1.fits_type == f1i_fits) ||
343 (desc1.fits_type == f2i_fits) ||
344 (desc1.fits_type == f2d_fits) ||
345 (desc1.fits_type == b2d_fits) ||
346 (desc1.fits_type == raw_fits),
347 CPL_ERROR_ILLEGAL_INPUT,
348 "First input file hasn't correct data type "
349 "(KMOSTYPE must be F1I, F2I, F3I, F2D, B2D or RAW)!");
350
351 ifu = kmo_dfs_get_parameter_int(parlist,
352 "kmos.kmo_stats.ext");
353 KMO_TRY_EXIT_IF_ERROR(
354 kmo_dfs_print_parameter_help(parlist, "kmos.kmo_stats.ext"));
355
356 KMO_TRY_ASSURE((desc1.nr_ext >= ifu) ||
357 (ifu == 0),
358 CPL_ERROR_ILLEGAL_INPUT,
359 "ext must be smaller or equal to the number of "
360 "extensions!");
361
362 KMO_TRY_EXIT_IF_ERROR(
363 kmos_combine_pars_load(parlist,
364 "kmos.kmo_stats",
365 &cmethod,
366 &cpos_rej,
367 &cneg_rej,
368 &citer,
369 NULL,
370 NULL,
371 TRUE));
372
373
374 cpl_msg_info("", "-------------------------------------------");
375
376 // check the (optional) mask
377 if (cpl_frameset_get_size(frameset) == 2) {
378 kmo_init_fits_desc(&desc2);
379
380 // load descriptor of second operand
381 desc2 = kmo_identify_fits_header(
382 cpl_frame_get_filename(mask_frame));
383 KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem "
384 "to be in KMOS-format!");
385
386 mask_available = TRUE;
387 if ((desc1.fits_type == f3i_fits) ||
388 (desc1.fits_type == f2i_fits))
389 {
390 KMO_TRY_ASSURE(desc2.fits_type == f2i_fits,
391 CPL_ERROR_ILLEGAL_INPUT,
392 "Mask hasn't correct data type "
393 "(KMOSTYPE must be F2I)!");
394 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
395 (desc1.naxis2 == desc2.naxis2),
396 CPL_ERROR_ILLEGAL_INPUT,
397 "STAT_DATA and STAT_MASK don't have the same "
398 "dimensions!");
399 } else if ((desc1.fits_type == f2d_fits) ||
400 (desc1.fits_type == b2d_fits) ||
401 (desc1.fits_type == raw_fits))
402 {
403 KMO_TRY_ASSURE((desc2.fits_type == f2d_fits) ||
404 (desc2.fits_type == b2d_fits),
405 CPL_ERROR_ILLEGAL_INPUT,
406 "Mask file hasn't correct data type "
407 "(KMOSTYPE must be F2D or B2D)!");
408 KMO_TRY_ASSURE((desc1.naxis1 == desc2.naxis1) &&
409 (desc1.naxis2 == desc2.naxis2),
410 CPL_ERROR_ILLEGAL_INPUT,
411 "STAT_DATA and STAT_MASK don't have the same "
412 "dimensions!");
413 } else if (desc1.fits_type == f1i_fits)
414 {
415 KMO_TRY_ASSURE(1==0,
416 CPL_ERROR_ILLEGAL_INPUT,
417 "Mask can't be provided for F1I frames!");
418 } else {
419 mask_available = FALSE;
420 }
421
422 KMO_TRY_ASSURE((desc1.nr_ext == desc2.nr_ext) ||
423 (desc1.nr_ext/2 == desc2.nr_ext) ||
424 ((desc2.nr_ext == 1) && (ifu > 0) &&
425 (ifu <= desc1.nr_ext)),
426 CPL_ERROR_ILLEGAL_INPUT,
427 "Mask hasn't same number of extensions as data!");
428 }
429
430 if (desc1.ex_noise == TRUE) {
431 nr_devices = desc1.nr_ext / 2;
432 } else {
433 nr_devices = desc1.nr_ext;
434 }
435
436 if (ifu != 0) {
437 if (((desc1.ex_noise == FALSE) &&
438 (desc1.sub_desc[ifu-1].valid_data == FALSE)) ||
439 ((desc1.ex_noise == TRUE) &&
440 (desc1.sub_desc[2 * (ifu-1) - 1].valid_data == FALSE)))
441 {
442 cpl_msg_info(cpl_func, "No valid IFU or detector has been "
443 "selected!");
444 kmo_free_fits_desc(&desc1);
445 kmo_free_fits_desc(&desc2);
446 return 0;
447 }
448 }
449
450 // --- load, update & save primary header ---
451 KMO_TRY_EXIT_IF_ERROR(
452 kmo_dfs_save_main_header(frameset, STATS, "", data_frame,
453 NULL, parlist, cpl_func));
454 //
455 // load & process data
456 //
457 for (i = 1; i <= nr_devices; i++) {
458 if (desc1.ex_noise == FALSE) {
459 devnr = desc1.sub_desc[i - 1].device_nr;
460 } else {
461 devnr = desc1.sub_desc[2 * i - 1].device_nr;
462 }
463
464 if (desc1.ex_badpix == FALSE) {
465 index_data = kmo_identify_index_desc(desc1, devnr, FALSE);
466 } else {
467 index_data = kmo_identify_index_desc(desc1, devnr, 2);
468 }
469 KMO_TRY_CHECK_ERROR_STATE();
470
471 if (desc1.ex_noise) {
472 index_noise = kmo_identify_index_desc(desc1, devnr, TRUE);
473 }
474 KMO_TRY_CHECK_ERROR_STATE();
475
476 if (i > 1) {
477 kmclipm_omit_warning_one_slice = TRUE;
478 }
479 if ((ifu == 0) || (ifu == i)) {
480 // check if IFU is valid
481 valid_ifu = FALSE;
482 if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
483 valid_ifu = TRUE;
484 }
485
486 if (valid_ifu) {
487 // load data
488// if ((desc1.fits_type == f3i_fits) ||
489// (desc1.fits_type == f2i_fits) ||
490// (desc1.fits_type == f2d_fits) ||
491// (desc1.fits_type == raw_fits)) {
492 // load mask, if available
493 if (mask_available == TRUE) {
494 tmpi = i;
495 if (desc2.nr_ext == 1) {
496 tmpi = 1;
497 }
498 if (desc2.naxis == 1) {
499 KMO_TRY_EXIT_IF_NULL(
500 mask_vec = kmo_dfs_load_vector(frameset,
501 do_mode2,
502 tmpi,
503 FALSE));
504 } else if (desc2.naxis == 2) {
505 if (desc2.fits_type == b2d_fits) {
506 KMO_TRY_EXIT_IF_NULL(
507 mask = kmo_dfs_load_image(frameset,
508 do_mode2,
509 tmpi,
510 2, FALSE, NULL));
511 } else {
512 KMO_TRY_EXIT_IF_NULL(
513 mask = kmo_dfs_load_image(frameset,
514 do_mode2,
515 tmpi,
516 FALSE, FALSE, NULL));
517 }
518 } else {
519 KMO_TRY_ASSURE(1 == 0,
520 CPL_ERROR_ILLEGAL_INPUT,
521 "STAT_MASK must either be a "
522 "vector or an image!");
523 }
524 }
525// }
526
527 if (desc1.fits_type == f3i_fits) {
528 KMO_TRY_EXIT_IF_NULL(
529 data_in = kmo_dfs_load_cube(frameset, do_mode1,
530 devnr, FALSE));
531 KMO_TRY_EXIT_IF_NULL(
532 data_out = kmo_calc_stats_cube(data_in,
533 mask,
534 cpos_rej,
535 cneg_rej,
536 citer));
537 cpl_imagelist_delete(data_in); data_in = NULL;
538 } else if (desc1.fits_type == f1i_fits) {
539 KMO_TRY_EXIT_IF_NULL(
540 vec_in = kmo_dfs_load_vector(frameset, do_mode1,
541 devnr, FALSE));
542 KMO_TRY_EXIT_IF_NULL(
543 data_out = kmo_calc_stats_vec(vec_in,
544 mask_vec,
545 cpos_rej,
546 cneg_rej,
547 citer));
548 kmclipm_vector_delete(vec_in); vec_in = NULL;
549 } else if ((desc1.fits_type == f2i_fits) ||
550 (desc1.fits_type == f2d_fits) ||
551 (desc1.fits_type == b2d_fits) ||
552 (desc1.fits_type == raw_fits))
553 {
554 int sat_mode = FALSE;
555 if (desc1.fits_type == raw_fits) {
556 sat_mode = TRUE;
557 }
558
559 if (desc1.fits_type == b2d_fits) {
560 KMO_TRY_EXIT_IF_NULL(
561 img_in = kmo_dfs_load_image(frameset, do_mode1,
562 devnr, 2, sat_mode, NULL));
563 } else {
564 KMO_TRY_EXIT_IF_NULL(
565 img_in = kmo_dfs_load_image(frameset, do_mode1,
566 devnr,
567 FALSE, sat_mode, NULL));
568 }
569 KMO_TRY_EXIT_IF_NULL(
570 data_out = kmo_calc_stats_img(img_in,
571 mask,
572 cpos_rej,
573 cneg_rej,
574 citer));
575 cpl_image_delete(img_in); img_in = NULL;
576 } else {
577 KMO_TRY_ASSURE(1==0,
578 CPL_ERROR_ILLEGAL_INPUT,
579 "Unsupported fits_type!");
580 }
581
582 stats_size = kmclipm_vector_get_size(data_out);
583
584 if ((desc1.fits_type == f3i_fits) ||
585 (desc1.fits_type == f2i_fits) ||
586 (desc1.fits_type == f2d_fits) ||
587 (desc1.fits_type == b2d_fits) ||
588 (desc1.fits_type == raw_fits))
589 {
590 if (mask_available == TRUE) {
591 cpl_image_delete(mask); mask = NULL;
592 }
593 }
594
595 // save data
596 if (desc1.fits_type == b2d_fits) {
597 KMO_TRY_EXIT_IF_NULL(
598 sub_header = kmo_dfs_load_sub_header(frameset, do_mode1, devnr,
599 2));
600 } else {
601 KMO_TRY_EXIT_IF_NULL(
602 sub_header = kmo_dfs_load_sub_header(frameset, do_mode1, devnr,
603 FALSE));
604 }
605
606 if ((desc1.fits_type == raw_fits) ||
607 (desc1.fits_type == b2d_fits))
608 {
609 KMO_TRY_EXIT_IF_NULL(
610 tmp_str = cpl_sprintf("%s%d%s", "DET.",
611 devnr,
612 ".DATA"));
613 KMO_TRY_EXIT_IF_ERROR(
614 kmclipm_update_property_string(sub_header,
615 EXTNAME,
616 tmp_str,
617 "FITS extension name"));
618 cpl_free(tmp_str); tmp_str = NULL;
619 }
620
621 KMO_TRY_EXIT_IF_ERROR(
622 kmo_dfs_save_vector(data_out, STATS, "", sub_header, 0./0.));
623
624 cpl_propertylist_delete(sub_header); sub_header = NULL;
625 kmclipm_vector_delete(data_out); data_out = NULL;
626
627 //
628 // do exactly the same thing for noise, if existing
629 //
630 if (desc1.ex_noise) {
631 if (desc1.sub_desc[index_noise-1].valid_data) {
632 if (desc1.fits_type == f3i_fits) {
633 KMO_TRY_EXIT_IF_NULL(
634 data_in = kmo_dfs_load_cube(frameset, do_mode1,
635 devnr, TRUE));
636
637 KMO_TRY_EXIT_IF_NULL(
638 data_out = kmo_calc_stats_cube(data_in,
639 mask,
640 cpos_rej,
641 cneg_rej,
642 citer));
643 cpl_imagelist_delete(data_in); data_in = NULL;
644 } else if (desc1.fits_type == f1i_fits) {
645 KMO_TRY_EXIT_IF_NULL(
646 vec_in = kmo_dfs_load_vector(frameset, do_mode1,
647 devnr, TRUE));
648 KMO_TRY_EXIT_IF_NULL(
649 data_out = kmo_calc_stats_vec(vec_in,
650 mask_vec,
651 cpos_rej,
652 cneg_rej,
653 citer));
654 kmclipm_vector_delete(vec_in); vec_in = NULL;
655 } else if ((desc1.fits_type == f2i_fits) ||
656 (desc1.fits_type == f2d_fits) ||
657 (desc1.fits_type == raw_fits))
658 {
659 KMO_TRY_EXIT_IF_NULL(
660 img_in = kmo_dfs_load_image(frameset, do_mode1,
661 devnr, TRUE, FALSE, NULL));
662
663 KMO_TRY_EXIT_IF_NULL(
664 data_out = kmo_calc_stats_img(img_in,
665 mask,
666 cpos_rej,
667 cneg_rej,
668 citer));
669 cpl_image_delete(img_in); img_in = NULL;
670 }
671
672 if ((desc1.fits_type == f3i_fits) ||
673 (desc1.fits_type == f2i_fits) ||
674 (desc1.fits_type == f2d_fits) ||
675 (desc1.fits_type == raw_fits))
676 {
677 if (mask_available == TRUE) {
678 cpl_image_delete(mask); mask = NULL;
679 kmclipm_vector_delete(mask_vec); mask_vec = NULL;
680 }
681 }
682
683 // save noise
684 KMO_TRY_EXIT_IF_NULL(
685 sub_header = kmo_dfs_load_sub_header(frameset,
686 do_mode1,
687 devnr, TRUE));
688
689 if ((desc1.fits_type == raw_fits) ||
690 (desc1.fits_type == b2d_fits))
691 {
692 KMO_TRY_EXIT_IF_NULL(
693 tmp_str = cpl_sprintf("%s%d%s", "DET.",
694 devnr,
695 ".NOISE"));
696 KMO_TRY_EXIT_IF_ERROR(
697 kmclipm_update_property_string(sub_header,
698 EXTNAME,
699 tmp_str,
700 "FITS extension name"));
701 cpl_free(tmp_str); tmp_str = NULL;
702 }
703
704 KMO_TRY_EXIT_IF_ERROR(
705 kmo_dfs_save_vector(data_out, STATS, "",
706 sub_header, 0./0.));
707
708 cpl_propertylist_delete(sub_header);sub_header = NULL;
709 kmclipm_vector_delete(data_out); data_out = NULL;
710 }
711 }
712 } else {
713 // invalid IFU, just save sub_headers
714 KMO_TRY_EXIT_IF_NULL(
715 sub_header = kmo_dfs_load_sub_header(frameset,
716 do_mode1,
717 devnr, FALSE));
718
719 if ((desc1.fits_type == raw_fits) ||
720 (desc1.fits_type == b2d_fits))
721 {
722 KMO_TRY_EXIT_IF_NULL(
723 tmp_str = cpl_sprintf("%s%d%s", "DET.",
724 devnr,
725 ".DATA"));
726 KMO_TRY_EXIT_IF_ERROR(
727 kmclipm_update_property_string(sub_header,
728 EXTNAME,
729 tmp_str,
730 "FITS extension name"));
731 cpl_free(tmp_str); tmp_str = NULL;
732 }
733
734 KMO_TRY_EXIT_IF_ERROR(
735 kmo_dfs_save_sub_header(STATS, "", sub_header));
736 cpl_propertylist_delete(sub_header); sub_header = NULL;
737
738 if (desc1.ex_noise) {
739 cpl_propertylist_delete(sub_header); sub_header = NULL;
740
741 KMO_TRY_EXIT_IF_NULL(
742 sub_header = kmo_dfs_load_sub_header(frameset,
743 do_mode1,
744 i, TRUE));
745
746 if ((desc1.fits_type == raw_fits) ||
747 (desc1.fits_type == b2d_fits))
748 {
749 KMO_TRY_EXIT_IF_NULL(
750 tmp_str = cpl_sprintf("%s%d%s", "DET.",
751 devnr,
752 ".NOISE"));
753 KMO_TRY_EXIT_IF_ERROR(
754 kmclipm_update_property_string(sub_header,
755 EXTNAME,
756 tmp_str,
757 "FITS extension name"));
758 cpl_free(tmp_str); tmp_str = NULL;
759 }
760
761 KMO_TRY_EXIT_IF_ERROR(
762 kmo_dfs_save_sub_header(STATS, "", sub_header));
763 cpl_propertylist_delete(sub_header); sub_header = NULL;
764 }
765 }
766 }
767 }
768
769 // print stats info to console
770 kmo_free_fits_desc(&desc1);
771 kmo_init_fits_desc(&desc1);
772
773 KMO_TRY_EXIT_IF_NULL(
774 data_frame = kmo_dfs_get_frame(frameset, STATS));
775
776 desc1 = kmo_identify_fits_header(
777 cpl_frame_get_filename(data_frame));
778
779 KMO_TRY_CHECK_ERROR_STATE_MSG("Provided fits file doesn't seem to be "
780 "in KMOS-format!");
781
782 cpl_msg_info("", "-------------------------------------------");
783 cpl_msg_info("", "--- kmo_stats info ---");
784 cpl_msg_info("", "-------------------------------------------");
785
786 KMO_TRY_EXIT_IF_NULL(
787 strarr = (char**)cpl_malloc((stats_size+1) * sizeof(char*)));
788
789 for (i = 0; i < stats_size+1; i++) {
790 KMO_TRY_EXIT_IF_NULL(
791 strarr[i] = (char*)cpl_malloc(1024 * sizeof(char)));
792 switch (i) {
793 case 0: strcpy(strarr[i], " |"); break;
794 case 1: strcpy(strarr[i], "1. #pixels: | "); break;
795 case 2: strcpy(strarr[i], "2. #finite pix.: | "); break;
796 case 3: strcpy(strarr[i], "3. mean: | "); break;
797 case 4: strcpy(strarr[i], "4. stdev: | "); break;
798 case 5: strcpy(strarr[i], "5. mean w. rej.: | "); break;
799 case 6: strcpy(strarr[i], "6. stdev w. rej.:| "); break;
800 case 7: strcpy(strarr[i], "7. median: | "); break;
801 case 8: strcpy(strarr[i], "8. mode: | "); break;
802 case 9: strcpy(strarr[i], "9. noise est.: | "); break;
803 case 10: strcpy(strarr[i], "10. min. value: | "); break;
804 case 11: strcpy(strarr[i], "11. max. value: | "); break;
805 default: cpl_msg_error(cpl_func, "To much values in output "
806 "statistic vector!"); break;
807 }
808 }
809
810 if (desc1.ex_noise == TRUE) {
811 nr_devices = desc1.nr_ext / 2;
812 } else {
813 nr_devices = desc1.nr_ext;
814 }
815
816 for (j = 1; j <= nr_devices; j++) {
817 if (desc1.ex_noise == FALSE) {
818 devnr = desc1.sub_desc[j - 1].device_nr;
819 } else {
820 devnr = desc1.sub_desc[2 * j - 1].device_nr;
821 }
822
823 if (desc1.ex_badpix == FALSE) {
824 index_data = kmo_identify_index_desc(desc1, devnr, FALSE);
825 } else {
826 index_data = kmo_identify_index_desc(desc1, devnr, 2);
827 }
828 KMO_TRY_CHECK_ERROR_STATE();
829
830 if (desc1.ex_noise) {
831 index_noise = kmo_identify_index_desc(desc1, devnr, TRUE);
832 }
833 KMO_TRY_CHECK_ERROR_STATE();
834
835 // check if IFU is valid
836 valid_ifu = FALSE;
837 if (desc1.sub_desc[index_data-1].valid_data == TRUE) {
838 valid_ifu = TRUE;
839 }
840
841 if (valid_ifu) {
842 if (ifu != 0) {
843 extnr = ifu;
844 } else {
845 extnr = devnr;
846 }
847
848 KMO_TRY_EXIT_IF_NULL(
849 sub_header = kmo_dfs_load_sub_header(frameset, STATS, extnr,
850 FALSE));
851
852 strcat(strarr[0],
853 cpl_propertylist_get_string(sub_header, EXTNAME));
854 strcat(strarr[0], "|");
855 cpl_propertylist_delete(sub_header); sub_header = NULL;
856
857 KMO_TRY_EXIT_IF_NULL(
858 data_out = kmo_dfs_load_vector(frameset, STATS, extnr, FALSE));
859
860 for (i = 1; i < stats_size+1; i++) {
861 double val = 0.;
862 int rejected = 0;
863 val = kmclipm_vector_get(data_out, i-1, &rejected);
864 if (!rejected) {
865 KMO_TRY_EXIT_IF_NULL(
866 tmp_str = cpl_sprintf("%8.7g |", val));
867 } else {
868 KMO_TRY_EXIT_IF_NULL(
869 tmp_str = cpl_sprintf(" - |"));
870 }
871
872 strcat(strarr[i], tmp_str);
873 cpl_free(tmp_str); tmp_str = NULL;
874 }
875 kmclipm_vector_delete(data_out); data_out = NULL;
876
877 if (desc1.ex_noise == TRUE) {
878 KMO_TRY_EXIT_IF_NULL(
879 sub_header = kmo_dfs_load_sub_header(frameset, STATS,
880 extnr, TRUE));
881 strcat(strarr[0],
882 cpl_propertylist_get_string(sub_header, EXTNAME));
883 strcat(strarr[0], "|");
884 cpl_propertylist_delete(sub_header); sub_header = NULL;
885
886 KMO_TRY_EXIT_IF_NULL(
887 data_out = kmo_dfs_load_vector(frameset, STATS, extnr,
888 TRUE));
889
890 for (i = 1; i < stats_size+1; i++) {
891 double val = 0.;
892 int rejected = 0;
893 val = kmclipm_vector_get(data_out, i-1, &rejected);
894 if (!rejected) {
895 KMO_TRY_EXIT_IF_NULL(
896 tmp_str = cpl_sprintf("%9.7g | ", val));
897 } else {
898 KMO_TRY_EXIT_IF_NULL(
899 tmp_str = cpl_sprintf(" - |"));
900 }
901
902 strcat(strarr[i], tmp_str);
903 cpl_free(tmp_str); tmp_str = NULL;
904 }
905 kmclipm_vector_delete(data_out); data_out = NULL;
906 }
907 }
908 }
909
910 for (i = 0; i < stats_size+1; i++) {
911 cpl_msg_info("", "%s", strarr[i]);
912 cpl_free(strarr[i]); strarr[i] = NULL;
913 }
914 cpl_free(strarr); strarr = NULL;
915 cpl_msg_info("", "-------------------------------------------");
916 }
917 KMO_CATCH
918 {
919 KMO_CATCH_MSG();
920
921 ret_val = -1;
922 }
923
924 kmo_free_fits_desc(&desc1);
925 if (mask_available == TRUE) {
926 kmo_free_fits_desc(&desc2);
927 }
928 cpl_propertylist_delete(sub_header); sub_header = NULL;
929 cpl_imagelist_delete(data_in); data_in = NULL;
930 kmclipm_vector_delete(data_out); data_out = NULL;
931 cpl_image_delete(mask); mask = NULL;
932 kmclipm_vector_delete(mask_vec); mask_vec = NULL;
933
934 return ret_val;
935}
936
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: kmo_stats.c:143