MIDI Pipeline Reference Manual  2.8.3
midi_kappamatrix.c
1 /* $Id: midi_kappamatrix.c,v 1.31 2012-04-10 14:01:39 agabasch Exp $
2  *
3  * This file is part of the MIDI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: agabasch $
23  * $Date: 2012-04-10 14:01:39 $
24  * $Revision: 1.31 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <cpl.h>
37 
38 #include "midi_utils.h"
39 #include "midi_pfits.h"
40 #include "midi_dfs.h"
41 #include "string.h"
42 #include "midiTableToFits.h"
43 #include "midi_cplupgrade.h"
44 
45 /*-----------------------------------------------------------------------------
46  Private function prototypes
47  -----------------------------------------------------------------------------*/
48 
49 static int midi_kappamatrix_create(cpl_plugin *);
50 static int midi_kappamatrix_exec(cpl_plugin *);
51 static int midi_kappamatrix_destroy(cpl_plugin *);
52 static int midi_kappamatrix(cpl_frameset *, const cpl_parameterlist *);
53 static int table_to_imglst_sky_target(const char * ,
54  const char * ,
55  cpl_imagelist * ,
56  cpl_imagelist * ,
57  cpl_table *);
58 static int table_to_imglst_mask(const char *,
59  cpl_imagelist *,
60  cpl_table *);
61 static void kappa_to_ascii(const cpl_image * ,
62  const cpl_image * ,
63  const cpl_image * ,
64  const cpl_image * ,
65  const char * );
66 
67 
68 
69 
70 /*-----------------------------------------------------------------------------
71  Static variables
72  -----------------------------------------------------------------------------*/
73 
74 static char midi_kappamatrix_description[] =
75 "This recipe derives the Kappa Matrix in the SCI_PHOT mode following\n"
76 "the definition in A&A 425 1161. It uses a set of AOPEN and BOPEN\n"
77 "photometry files as well as a spatial profile of the spectrum. The\n"
78 "spectrum is extracted using the Optimal Extraction algorithm\n"
79 "(Horne 1986, PASP 98 209) adapted to NIR data, i.e. background dominated\n"
80 "images. The errors of the kappa matrix are not calculated but set a\n"
81 "priori to 5 per cent. "
82 "The main output is a fits table with the PRO\n"
83 "CATG keyword set to MIDI_KAPPAMATRIX_(G|P)RISM. For diagnostic reasons the\n"
84 "different kappa matrixes are also saved as single fits images\n\n"
85 
86 
87 "Input files:\n\n"
88 " DO category: Type: Explanation: Required:\n"
89 " PHOTOM_SP_CALIB Raw Raw data frame \n"
90 " or Y\n"
91 " PHOTOM_SP_SCIENCE Raw Raw data frame \n\n"
92 " KAPPA_SP_MASK_PRISM Calib Spectral profile \n"
93 " or Y\n"
94 " KAPPA_SP_MASK_GRISM Calib Spectral profile \n\n"
95 "Output files:\n\n"
96 " DO category: Data type: Explanation:\n"
97 " MIDI_KAPPAMATRIX_PRISM FITS table Kappa matrix: Main product \n"
98 " or \n"
99 " MIDI_KAPPAMATRIX_GRISM FITS table Kappa matrix: Main product \n\n"
100 " MIDI_KAPPAMATRIX11 FITS image Kappa matrix: For diagnostics\n"
101 " MIDI_KAPPAMATRIX11_FILTERED FITS image Kappa matrix: For diagnostics\n"
102 " MIDI_KAPPAMATRIX11_NOMASK FITS image Kappa matrix: For diagnostics\n"
103 " MIDI_KAPPAMATRIX12 FITS image Kappa matrix: For diagnostics\n"
104 " MIDI_KAPPAMATRIX12_FILTERED FITS image Kappa matrix: For diagnostics\n"
105 " MIDI_KAPPAMATRIX12_NOMASK FITS image Kappa matrix: For diagnostics\n"
106 " MIDI_KAPPAMATRIX21 FITS image Kappa matrix: For diagnostics\n"
107 " MIDI_KAPPAMATRIX21_FILTERED FITS image Kappa matrix: For diagnostics\n"
108 " MIDI_KAPPAMATRIX21_NOMASK FITS image Kappa matrix: For diagnostics\n"
109 " MIDI_KAPPAMATRIX22 FITS image Kappa matrix: For diagnostics\n"
110 " MIDI_KAPPAMATRIX22_FILTERED FITS image Kappa matrix: For diagnostics\n"
111 " MIDI_KAPPAMATRIX22_NOMASK FITS image Kappa matrix: For diagnostics\n\n";
112 
113 
114 /*-----------------------------------------------------------------------------
115  Function code
116  -----------------------------------------------------------------------------*/
117 
118 
119 /*----------------------------------------------------------------------------*/
124 /*----------------------------------------------------------------------------*/
125 
128 /*----------------------------------------------------------------------------*/
138 /*----------------------------------------------------------------------------*/
139 int cpl_plugin_get_info(cpl_pluginlist * list)
140 {
141  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
142  cpl_plugin * plugin = &recipe->interface;
143 
144  if (cpl_plugin_init(plugin,
145  CPL_PLUGIN_API,
146  MIDI_BINARY_VERSION,
147  CPL_PLUGIN_TYPE_RECIPE,
148  "midi_kappamatrix",
149  "Derives the Kappamatrix for the SCI_PHOT mode",
150  midi_kappamatrix_description,
151  "Armin Gabasch",
152  PACKAGE_BUGREPORT,
154  midi_kappamatrix_create,
155  midi_kappamatrix_exec,
156  midi_kappamatrix_destroy)) {
157  cpl_msg_error(cpl_func, "Plugin initialization failed");
158  (void)cpl_error_set_where(cpl_func);
159  return 1;
160  }
161 
162  if (cpl_pluginlist_append(list, plugin)) {
163  cpl_msg_error(cpl_func, "Error adding plugin to list");
164  (void)cpl_error_set_where(cpl_func);
165  return 1;
166  }
167 
168  return 0;
169 }
170 
171 /*----------------------------------------------------------------------------*/
179 /*----------------------------------------------------------------------------*/
180 static int midi_kappamatrix_create(cpl_plugin * plugin)
181 {
182  cpl_recipe * recipe;
183  cpl_parameter * p;
184 
185  /* Do not create the recipe if an error code is already set */
186  if (cpl_error_get_code() != CPL_ERROR_NONE) {
187  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
188  cpl_func, __LINE__, cpl_error_get_where());
189  return (int)cpl_error_get_code();
190  }
191 
192  if (plugin == NULL) {
193  cpl_msg_error(cpl_func, "Null plugin");
194  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
195  }
196 
197  /* Verify plugin type */
198  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
199  cpl_msg_error(cpl_func, "Plugin is not a recipe");
200  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
201  }
202 
203  /* Get the recipe */
204  recipe = (cpl_recipe *)plugin;
205 
206  /* Create the parameters list in the cpl_recipe object */
207  recipe->parameters = cpl_parameterlist_new();
208  if (recipe->parameters == NULL) {
209  cpl_msg_error(cpl_func, "Parameter list allocation failed");
210  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
211  }
212 
213  /* Fill the parameters list */
214 
215  /* --stropt */
216 /* p = cpl_parameter_new_value("midi.midi_kappamatrix.outputfilename", */
217 /* CPL_TYPE_STRING, "Output Filename (placeholder; not used for the moment!!)", "midi.midi_kappamatrix","kappamatrix.fits"); */
218 /* cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "outfile"); */
219 /* cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); */
220 /* cpl_parameterlist_append(recipe->parameters, p); */
221 
222  p = cpl_parameter_new_value("midi.midi_kappamatrix.medianwindow",
223  CPL_TYPE_INT,
224  "The window size of the median filter. No influence on the main product",
225  "midi.midi_kappamatrix",9);
226  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "medianwindow");
227  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
228  cpl_parameterlist_append(recipe->parameters, p);
229 
230 
231 /* /\* --boolopt *\/ */
232 /* p = cpl_parameter_new_value("midi.midi_kappamatrix.bool_option", */
233 /* CPL_TYPE_BOOL, "a flag", "midi.midi_kappamatrix", TRUE); */
234 /* cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt"); */
235 /* cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); */
236 /* cpl_parameterlist_append(recipe->parameters, p); */
237 
238  return 0;
239 }
240 
241 /*----------------------------------------------------------------------------*/
247 /*----------------------------------------------------------------------------*/
248 static int midi_kappamatrix_exec(cpl_plugin * plugin)
249 {
250 
251  cpl_recipe * recipe;
252  int recipe_status;
253  cpl_errorstate initial_errorstate = cpl_errorstate_get();
254 
255  /* Return immediately if an error code is already set */
256  if (cpl_error_get_code() != CPL_ERROR_NONE) {
257  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
258  cpl_func, __LINE__, cpl_error_get_where());
259  return (int)cpl_error_get_code();
260  }
261 
262  if (plugin == NULL) {
263  cpl_msg_error(cpl_func, "Null plugin");
264  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
265  }
266 
267  /* Verify plugin type */
268  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
269  cpl_msg_error(cpl_func, "Plugin is not a recipe");
270  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
271  }
272 
273  /* Get the recipe */
274  recipe = (cpl_recipe *)plugin;
275 
276  /* Verify parameter and frame lists */
277  if (recipe->parameters == NULL) {
278  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
279  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
280  }
281  if (recipe->frames == NULL) {
282  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
283  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
284  }
285 
286  /* Invoke the recipe */
287  recipe_status = midi_kappamatrix(recipe->frames, recipe->parameters);
288 
289  /* Ensure DFS-compliance of the products */
290  if (cpl_dfs_update_product_header(recipe->frames)) {
291  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
292  }
293 
294  if (!cpl_errorstate_is_equal(initial_errorstate)) {
295  /* Dump the error history since recipe execution start.
296  At this point the recipe cannot recover from the error */
297  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
298  }
299 
300  return recipe_status;
301 }
302 
303 /*----------------------------------------------------------------------------*/
309 /*----------------------------------------------------------------------------*/
310 static int midi_kappamatrix_destroy(cpl_plugin * plugin)
311 {
312  cpl_recipe * recipe;
313 
314  if (plugin == NULL) {
315  cpl_msg_error(cpl_func, "Null plugin");
316  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
317  }
318 
319  /* Verify plugin type */
320  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
321  cpl_msg_error(cpl_func, "Plugin is not a recipe");
322  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
323  }
324 
325  /* Get the recipe */
326  recipe = (cpl_recipe *)plugin;
327 
328  cpl_parameterlist_delete(recipe->parameters);
329 
330  return 0;
331 }
332 
333 /*----------------------------------------------------------------------------*/
340 /*----------------------------------------------------------------------------*/
341 static int midi_kappamatrix(cpl_frameset * frameset,
342  const cpl_parameterlist * parlist)
343 {
344  const cpl_parameter * param;
345  cpl_frame * cur_frame;
346  cpl_table * table=NULL;
347  cpl_table * kappamatrix_table=NULL;
348  cpl_propertylist * plist;
349 
350  cpl_propertylist * qclist11;
351  cpl_propertylist * qclist12;
352  cpl_propertylist * qclist21;
353  cpl_propertylist * qclist22;
354  cpl_propertylist * qclist11_nomask;
355  cpl_propertylist * qclist12_nomask;
356  cpl_propertylist * qclist21_nomask;
357  cpl_propertylist * qclist22_nomask;
358  cpl_propertylist * qclist11_filtered;
359  cpl_propertylist * qclist12_filtered;
360  cpl_propertylist * qclist21_filtered;
361  cpl_propertylist * qclist22_filtered;
362  cpl_propertylist * qclist_all;
363  cpl_propertylist * qclist_all_extension;
364  cpl_mask * mask=NULL;
365 
366  const char * shutter_id =NULL;
367  cpl_errorstate prestate = cpl_errorstate_get();
368  char * tag=NULL;
369 /* cpl_matrix * kernel ; */
370  char * cubename;
371 
372  char gris_name[100]= "";
373  int ext_imaging_data;
374  int isPHOTAdata=0;
375  int isPHOTBdata=0;
376  double * pmask_grism_DATA1=NULL;
377  double * pmask_grism_DATA1_norm=NULL;
378  double * pmask_grism_DATA2=NULL;
379  double * pmask_grism_DATA2_norm=NULL;
380  double * pmask_grism_DATA3=NULL;
381  double * pmask_grism_DATA3_norm=NULL;
382  double * pmask_grism_DATA4=NULL;
383  double * pmask_grism_DATA4_norm=NULL;
384 
385  double * pmask_prism_DATA1=NULL;
386  double * pmask_prism_DATA1_norm=NULL;
387  double * pmask_prism_DATA2=NULL;
388  double * pmask_prism_DATA2_norm=NULL;
389  double * pmask_prism_DATA3=NULL;
390  double * pmask_prism_DATA3_norm=NULL;
391  double * pmask_prism_DATA4=NULL;
392  double * pmask_prism_DATA4_norm=NULL;
393 
394  double kappamatrix11_stdev=0.;
395  double kappamatrix11_stdev_nomask=0.;
396  double kappamatrix11_stdev_filtered=0.;
397  double kappamatrix12_stdev=0.;
398  double kappamatrix12_stdev_nomask=0.;
399  double kappamatrix12_stdev_filtered=0.;
400  double kappamatrix21_stdev=0.;
401  double kappamatrix21_stdev_nomask=0.;
402  double kappamatrix21_stdev_filtered=0.;
403  double kappamatrix22_stdev=0.;
404  double kappamatrix22_stdev_nomask=0.;
405  double kappamatrix22_stdev_filtered=0.;
406 
407  double kappamatrix11_median=0.;
408  double kappamatrix11_median_nomask=0.;
409  double kappamatrix11_median_filtered=0.;
410  double kappamatrix12_median=0.;
411  double kappamatrix12_median_nomask=0.;
412  double kappamatrix12_median_filtered=0.;
413  double kappamatrix21_median=0.;
414  double kappamatrix21_median_nomask=0.;
415  double kappamatrix21_median_filtered=0.;
416  double kappamatrix22_median=0.;
417  double kappamatrix22_median_nomask=0.;
418  double kappamatrix22_median_filtered=0.;
419 
420 
421  int nx_DATA1=0,nx_DATA2=0,nx_DATA3=0,nx_DATA4=0;
422  int ny_DATA1=0,ny_DATA2=0,ny_DATA3=0,ny_DATA4=0;
423 
424 
425  int xpos=0, ypos=0;
426 
427 
428  cpl_image * image_AOPEN_DATA2_T = NULL;
429  cpl_image * image_AOPEN_DATA3_T = NULL;
430  cpl_image * image_AOPEN_DATA4_T = NULL;
431 
432  cpl_image * image_BOPEN_DATA1_T = NULL;
433  cpl_image * image_BOPEN_DATA2_T = NULL;
434  cpl_image * image_BOPEN_DATA3_T = NULL;
435 
436  cpl_image * image_AOPEN_DATA2_T_collapsed = NULL;
437  cpl_image * image_AOPEN_DATA3_T_collapsed = NULL;
438  cpl_image * image_AOPEN_DATA4_T_collapsed = NULL;
439  cpl_image * image_BOPEN_DATA1_T_collapsed = NULL;
440  cpl_image * image_BOPEN_DATA2_T_collapsed = NULL;
441  cpl_image * image_BOPEN_DATA3_T_collapsed = NULL;
442 
443  cpl_image * image_AOPEN_DATA2_T_collapsed_nomask = NULL;
444  cpl_image * image_AOPEN_DATA3_T_collapsed_nomask = NULL;
445  cpl_image * image_AOPEN_DATA4_T_collapsed_nomask = NULL;
446  cpl_image * image_BOPEN_DATA1_T_collapsed_nomask = NULL;
447  cpl_image * image_BOPEN_DATA2_T_collapsed_nomask = NULL;
448  cpl_image * image_BOPEN_DATA3_T_collapsed_nomask = NULL;
449 
450 
451  cpl_image * mask_grism_DATA1_collapsed = NULL;
452  cpl_image * mask_grism_DATA2_collapsed = NULL;
453  cpl_image * mask_grism_DATA3_collapsed = NULL;
454  cpl_image * mask_grism_DATA4_collapsed = NULL;
455 
456  cpl_image * mask_prism_DATA1_collapsed = NULL;
457  cpl_image * mask_prism_DATA2_collapsed = NULL;
458  cpl_image * mask_prism_DATA3_collapsed = NULL;
459  cpl_image * mask_prism_DATA4_collapsed = NULL;
460 
461 
462 
463  cpl_image * kappamatrix11 = NULL;
464  cpl_image * kappamatrix12 = NULL;
465  cpl_image * kappamatrix21 = NULL;
466  cpl_image * kappamatrix22 = NULL;
467  cpl_image * kappamatrix11_nomask = NULL;
468  cpl_image * kappamatrix12_nomask = NULL;
469  cpl_image * kappamatrix21_nomask = NULL;
470  cpl_image * kappamatrix22_nomask = NULL;
471  cpl_image * kappamatrix11_filtered = NULL;
472  cpl_image * kappamatrix12_filtered = NULL;
473  cpl_image * kappamatrix21_filtered = NULL;
474  cpl_image * kappamatrix22_filtered = NULL;
475 
476  cpl_image * mask_grism_DATA1 = NULL;
477  cpl_image * mask_grism_DATA2 = NULL;
478  cpl_image * mask_grism_DATA3 = NULL;
479  cpl_image * mask_grism_DATA4 = NULL;
480 
481  cpl_image * mask_grism_DATA1_norm = NULL;
482  cpl_image * mask_grism_DATA2_norm = NULL;
483  cpl_image * mask_grism_DATA3_norm = NULL;
484  cpl_image * mask_grism_DATA4_norm = NULL;
485 
486  cpl_image * mask_prism_DATA1 = NULL;
487  cpl_image * mask_prism_DATA2 = NULL;
488  cpl_image * mask_prism_DATA3 = NULL;
489  cpl_image * mask_prism_DATA4 = NULL;
490 
491 
492  cpl_image * mask_prism_DATA1_norm = NULL;
493  cpl_image * mask_prism_DATA2_norm = NULL;
494  cpl_image * mask_prism_DATA3_norm = NULL;
495  cpl_image * mask_prism_DATA4_norm = NULL;
496 
497 /* cpl_imagelist * imglst_AOPEN_DATA1_S; */
498 /* cpl_imagelist * imglst_AOPEN_DATA1_T; */
499  cpl_imagelist * imglst_AOPEN_DATA2_S;
500  cpl_imagelist * imglst_AOPEN_DATA2_T;
501  cpl_imagelist * imglst_AOPEN_DATA3_S;
502  cpl_imagelist * imglst_AOPEN_DATA3_T;
503  cpl_imagelist * imglst_AOPEN_DATA4_S;
504  cpl_imagelist * imglst_AOPEN_DATA4_T;
505 
506  cpl_imagelist * imglst_BOPEN_DATA1_S;
507  cpl_imagelist * imglst_BOPEN_DATA1_T;
508  cpl_imagelist * imglst_BOPEN_DATA2_S;
509  cpl_imagelist * imglst_BOPEN_DATA2_T;
510  cpl_imagelist * imglst_BOPEN_DATA3_S;
511  cpl_imagelist * imglst_BOPEN_DATA3_T;
512 /* cpl_imagelist * imglst_BOPEN_DATA4_S; */
513 /* cpl_imagelist * imglst_BOPEN_DATA4_T; */
514 
515  cpl_imagelist * imglst_mask_prism;
516  cpl_imagelist * imglst_mask_grism;
517 
518  int medianwindow=0;
519  int lowlimit=0;
520  int highlimit=0;
521 
522  /* Check if SOF is empty */
523  cur_frame = cpl_frameset_get_first(frameset);
524  if (cur_frame == NULL) {
525  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
526  "SOF does not have any file");
527  }
528 
529 
530 /* imglst_AOPEN_DATA1_S=cpl_imagelist_new(); */
531 /* imglst_AOPEN_DATA1_T=cpl_imagelist_new(); */
532  imglst_AOPEN_DATA2_S=cpl_imagelist_new();
533  imglst_AOPEN_DATA2_T=cpl_imagelist_new();
534  imglst_AOPEN_DATA3_S=cpl_imagelist_new();
535  imglst_AOPEN_DATA3_T=cpl_imagelist_new();
536  imglst_AOPEN_DATA4_S=cpl_imagelist_new();
537  imglst_AOPEN_DATA4_T=cpl_imagelist_new();
538 
539  imglst_BOPEN_DATA1_S=cpl_imagelist_new();
540  imglst_BOPEN_DATA1_T=cpl_imagelist_new();
541  imglst_BOPEN_DATA2_S=cpl_imagelist_new();
542  imglst_BOPEN_DATA2_T=cpl_imagelist_new();
543  imglst_BOPEN_DATA3_S=cpl_imagelist_new();
544  imglst_BOPEN_DATA3_T=cpl_imagelist_new();
545 /* imglst_BOPEN_DATA4_S=cpl_imagelist_new(); */
546 /* imglst_BOPEN_DATA4_T=cpl_imagelist_new(); */
547 
548  imglst_mask_prism=cpl_imagelist_new();
549  imglst_mask_grism=cpl_imagelist_new();
550 
551 
552 
553  /* RETRIEVE THE INPUT PARAMETERS */
554 /* param = cpl_parameterlist_find_const(parlist, "midi.midi_kappamatrix.outputfilename"); */
555 /* outputfilename = cpl_parameter_get_string(param); */
556 
557  param = cpl_parameterlist_find_const(parlist,
558  "midi.midi_kappamatrix.medianwindow");
559  medianwindow = cpl_parameter_get_int(param);
560 
561  if(medianwindow%2 == 0)/* even number */
562  {
563  cpl_msg_warning(cpl_func, "The window size of the median filter is not odd,");
564  cpl_msg_warning(cpl_func, "therefore the size is increased by unity");
565  medianwindow=medianwindow+1;
566  }
567 
568  if(medianwindow > 81)
569  {
570  cpl_msg_warning(cpl_func, "The window size of the median filter exceeds the maximal supported value,");
571  cpl_msg_warning(cpl_func, "therefore the size is reset to 81");
572  medianwindow=81;
573  }
574 
575  if(medianwindow < 0)
576  {
577  cpl_msg_warning(cpl_func, "The window size of the median filter must be positive,");
578  cpl_msg_warning(cpl_func, "therefore the size is reset to 1");
579  medianwindow=1;
580  }
581 
582 
583 
584  if (!cpl_errorstate_is_equal(prestate)) {
585  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not retrieve the input parameters");
586  }
587 
588  /* Identify the RAW and CALIB frames in the input frameset */
589  cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
590  cpl_error_get_code());
591 
592 
593  /*
594  Walk through the whole Set of Frames SOF and search for KAPPA_SP_SCIENCE and/or KAPPA_SP_CALIB
595  */
596 
597 
598 
599 /* Start 1 */
600 
601  cur_frame = cpl_frameset_get_first(frameset);
602  if (cur_frame == NULL) {
603  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
604  "SOF does not have any file");
605  }
606  /* Append the images from the tables to the various imagelists */
607  while(cur_frame)
608  {
609  /* Check the right tags */
610  tag = (char*)cpl_frame_get_tag(cur_frame);
611  if (strcmp(tag, MIDI_PHOTOM_SP_CALIB) && strcmp(tag, MIDI_PHOTOM_SP_SCIENCE)) {
612  cur_frame = cpl_frameset_get_next( frameset );
613  continue;
614  }
615 
616  cpl_msg_info(cpl_func, "Processing file %s",cpl_frame_get_filename(cur_frame));
617  ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
618 
619 /* Check if the file is AOPEN or BOPEN */
620 
621  plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
622  if (cpl_propertylist_has(plist, "ESO INS SHUT ID") == 1)
623  {
624  shutter_id=(cpl_propertylist_get_string(plist, "ESO INS SHUT ID"));
625  }
626 
627 /* Checking if PRISM or GRISM */
628  if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
629  {
630  strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
631  }
632 
633  if (!cpl_errorstate_is_equal(prestate)) {
634  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not ...");
635  }
636 
637  if (strcmp(shutter_id,"AOPEN")==0)
638  {
639 /* PhotA Data found */
640  isPHOTAdata=1;
641 
642  /* Load extension Imaging Data */
643  table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
644  if (table == NULL) {
645  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
646  "Could not load the table");
647  }
648  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
649 
650 /* No Signal in DATA1 for AOPEN */
651 
652  /* cpl_msg_info(cpl_func, "Scanning for DATA2 ..."); */
653  if (cpl_table_has_column(table,"DATA2")){
654  table_to_imglst_sky_target("DATA2","TARTYP2",imglst_AOPEN_DATA2_S,imglst_AOPEN_DATA2_T,table);
655  }
656 /* cpl_msg_info(cpl_func, "Scanning for DATA3 ..."); */
657  if (cpl_table_has_column(table,"DATA3")){
658  table_to_imglst_sky_target("DATA3","TARTYP2",imglst_AOPEN_DATA3_S,imglst_AOPEN_DATA3_T,table);
659  }
660 /* cpl_msg_info(cpl_func, "Scanning for DATA4 ..."); */
661  if (cpl_table_has_column(table,"DATA4")){
662  table_to_imglst_sky_target("DATA4","TARTYP2",imglst_AOPEN_DATA4_S,imglst_AOPEN_DATA4_T,table);
663  }
664 
665  cpl_msg_info(cpl_func, "Number of so far processed AOPEN patches: % " CPL_SIZE_FORMAT " ",cpl_imagelist_get_size(imglst_AOPEN_DATA2_T));
666 
667  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
668 
669  cpl_table_delete(table);
670  }
671 
672  if (strcmp(shutter_id,"BOPEN")==0)
673  {
674 /* PhotB Data found */
675  isPHOTBdata=1;
676 
677  /* Load extension Imaging Data */
678  table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
679  if (table == NULL) {
680  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
681  "Could not load the table");
682  }
683  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
684 
685 
686  /* cpl_msg_info(cpl_func, "Scanning for DATA1 ..."); */
687  if (cpl_table_has_column(table,"DATA1")){
688  table_to_imglst_sky_target("DATA1","TARTYP2",imglst_BOPEN_DATA1_S,imglst_BOPEN_DATA1_T,table);
689  }
690 
691 /* cpl_msg_info(cpl_func, "Scanning for DATA2 ..."); */
692  if (cpl_table_has_column(table,"DATA2")){
693  table_to_imglst_sky_target("DATA2","TARTYP2",imglst_BOPEN_DATA2_S,imglst_BOPEN_DATA2_T,table);
694  }
695 /* cpl_msg_info(cpl_func, "Scanning for DATA3 ..."); */
696  if (cpl_table_has_column(table,"DATA3")){
697  table_to_imglst_sky_target("DATA3","TARTYP2",imglst_BOPEN_DATA3_S,imglst_BOPEN_DATA3_T,table);
698  }
699 
700 
701 /* No Signal in DATA4 for BOPEN */
702 
703  cpl_msg_info(cpl_func, "Number of so far processed BOPEN patches: % " CPL_SIZE_FORMAT " ",cpl_imagelist_get_size(imglst_BOPEN_DATA2_T));
704 
705 
706  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
707 
708  cpl_table_delete(table);
709  }
710 
711 
712  cpl_propertylist_delete(plist);
713 
714  /* Get next frame from SOF */
715  cur_frame = cpl_frameset_get_next( frameset );
716 
717  } /* while more frames */
718 
719 
720  if (isPHOTAdata==0 || isPHOTBdata==0)
721  {
722  cpl_msg_error(cpl_func, "No suitable SetOfFrame fround");
723  (void)cpl_error_set_where(cpl_func);
724  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Mask for filter name PRISM needed but not found");
725 
726  }
727 
728 
729 /* Subtract the sky frame from the target frame*/
730 
731  cpl_msg_info(cpl_func, "Doing sky subtraction ...");
732 
733  if(cpl_imagelist_get_size(imglst_AOPEN_DATA2_T)==cpl_imagelist_get_size(imglst_AOPEN_DATA2_S)
734  && cpl_imagelist_get_size(imglst_BOPEN_DATA1_T)==cpl_imagelist_get_size(imglst_BOPEN_DATA1_S)
735  )
736  {
737  cpl_imagelist_subtract(imglst_AOPEN_DATA2_T,imglst_AOPEN_DATA2_S);
738  cpl_imagelist_subtract(imglst_AOPEN_DATA3_T,imglst_AOPEN_DATA3_S);
739  cpl_imagelist_subtract(imglst_AOPEN_DATA4_T,imglst_AOPEN_DATA4_S);
740  cpl_imagelist_subtract(imglst_BOPEN_DATA1_T,imglst_BOPEN_DATA1_S);
741  cpl_imagelist_subtract(imglst_BOPEN_DATA2_T,imglst_BOPEN_DATA2_S);
742  cpl_imagelist_subtract(imglst_BOPEN_DATA3_T,imglst_BOPEN_DATA3_S);
743  }
744  else
745  {
746  cpl_msg_info(cpl_func, "The number of Skyframes and Targetframes differ");
747  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
748  "The number of Skyframes and Targetframes differ");
749  }
750 
751 
752  cpl_msg_info(cpl_func, "Collapsing the images in the time domain ...");
753 
754 /* Collapse the sky-subtracted targets */
755  image_AOPEN_DATA2_T=cpl_imagelist_collapse_create(imglst_AOPEN_DATA2_T);
756  image_AOPEN_DATA3_T=cpl_imagelist_collapse_create(imglst_AOPEN_DATA3_T);
757  image_AOPEN_DATA4_T=cpl_imagelist_collapse_create(imglst_AOPEN_DATA4_T);
758  image_BOPEN_DATA1_T=cpl_imagelist_collapse_create(imglst_BOPEN_DATA1_T);
759  image_BOPEN_DATA2_T=cpl_imagelist_collapse_create(imglst_BOPEN_DATA2_T);
760  image_BOPEN_DATA3_T=cpl_imagelist_collapse_create(imglst_BOPEN_DATA3_T);
761 
762 
763  cpl_imagelist_delete(imglst_AOPEN_DATA2_T);
764  cpl_imagelist_delete(imglst_AOPEN_DATA3_T);
765  cpl_imagelist_delete(imglst_AOPEN_DATA4_T);
766  cpl_imagelist_delete(imglst_AOPEN_DATA2_S);
767  cpl_imagelist_delete(imglst_AOPEN_DATA3_S);
768  cpl_imagelist_delete(imglst_AOPEN_DATA4_S);
769 
770  cpl_imagelist_delete(imglst_BOPEN_DATA1_T);
771  cpl_imagelist_delete(imglst_BOPEN_DATA2_T);
772  cpl_imagelist_delete(imglst_BOPEN_DATA3_T);
773  cpl_imagelist_delete(imglst_BOPEN_DATA1_S);
774  cpl_imagelist_delete(imglst_BOPEN_DATA2_S);
775  cpl_imagelist_delete(imglst_BOPEN_DATA3_S);
776 
777 /* END 1 */
778 
779 
780 /* Extract the masks from the table and store them in a cpl_image */
781 
782 /* The masks are stored in the two imagelists imglst_mask_grism and imglst_mask_prism */
783 /* !! DATA1 is read twice, therefore DATA1 is stored in imglst_mask_grism[1] and so on !! */
784 
785  cpl_msg_info(cpl_func, "Extracting the masks ...");
786 
787  cur_frame = cpl_frameset_get_first(frameset);
788  if (cur_frame == NULL) {
789  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
790  "SOF does not have any file");
791  }
792 
793  while(cur_frame)
794  {
795  ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
796 
797  /* Check the right tags */
798  tag = (char*)cpl_frame_get_tag(cur_frame);
799 
800  if (!strcmp(tag, MIDI_KAPPA_SP_MASK_GRISM)) {
801  table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
802  if (table == NULL) {
803  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
804  "Could not load the table");
805  }
806  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
807 
808  table_to_imglst_mask("DATA1",imglst_mask_grism,table);
809  table_to_imglst_mask("DATA1",imglst_mask_grism,table);
810  table_to_imglst_mask("DATA2",imglst_mask_grism,table);
811  table_to_imglst_mask("DATA3",imglst_mask_grism,table);
812  table_to_imglst_mask("DATA4",imglst_mask_grism,table);
813 
814 /* Normalizing the mask to 1*/
815 
816  cpl_msg_info(cpl_func, "Normalizing the integral of the masks (GRISM) to unity ...");
817  mask_grism_DATA1=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 1),CPL_TYPE_DOUBLE);
818  mask_grism_DATA2=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 2),CPL_TYPE_DOUBLE);
819  mask_grism_DATA3=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 3),CPL_TYPE_DOUBLE);
820  mask_grism_DATA4=cpl_image_cast(cpl_imagelist_get(imglst_mask_grism, 4),CPL_TYPE_DOUBLE);
821 
822  mask_grism_DATA1_norm=cpl_image_collapse_create(mask_grism_DATA1,0);
823  mask_grism_DATA2_norm=cpl_image_collapse_create(mask_grism_DATA2,0);
824  mask_grism_DATA3_norm=cpl_image_collapse_create(mask_grism_DATA3,0);
825  mask_grism_DATA4_norm=cpl_image_collapse_create(mask_grism_DATA4,0);
826 
827  nx_DATA1=cpl_image_get_size_x(mask_grism_DATA1);
828  ny_DATA1=cpl_image_get_size_y(mask_grism_DATA1);
829  nx_DATA2=cpl_image_get_size_x(mask_grism_DATA2);
830  ny_DATA2=cpl_image_get_size_y(mask_grism_DATA2);
831  nx_DATA3=cpl_image_get_size_x(mask_grism_DATA3);
832  ny_DATA3=cpl_image_get_size_y(mask_grism_DATA3);
833  nx_DATA4=cpl_image_get_size_x(mask_grism_DATA4);
834  ny_DATA4=cpl_image_get_size_y(mask_grism_DATA4);
835 
836  pmask_grism_DATA1 = cpl_image_get_data_double(mask_grism_DATA1);
837  pmask_grism_DATA1_norm= cpl_image_get_data_double(mask_grism_DATA1_norm);
838  pmask_grism_DATA2 = cpl_image_get_data_double(mask_grism_DATA2);
839  pmask_grism_DATA2_norm= cpl_image_get_data_double(mask_grism_DATA2_norm);
840  pmask_grism_DATA3 = cpl_image_get_data_double(mask_grism_DATA3);
841  pmask_grism_DATA3_norm= cpl_image_get_data_double(mask_grism_DATA3_norm);
842  pmask_grism_DATA4 = cpl_image_get_data_double(mask_grism_DATA4);
843  pmask_grism_DATA4_norm= cpl_image_get_data_double(mask_grism_DATA4_norm);
844 
845 
846 
847  for (xpos=0; xpos<nx_DATA1; ++xpos)
848  {
849  for(ypos=0; ypos<ny_DATA1;++ypos)
850  {
851  pmask_grism_DATA1[xpos+nx_DATA1*ypos] /= pmask_grism_DATA1_norm[xpos];
852  }
853  }
854 
855  for (xpos=0; xpos<nx_DATA2; ++xpos)
856  {
857  for(ypos=0; ypos<ny_DATA2;++ypos)
858  {
859  pmask_grism_DATA2[xpos+nx_DATA2*ypos] /= pmask_grism_DATA2_norm[xpos];
860  }
861  }
862 
863 
864  for (xpos=0; xpos<nx_DATA3; ++xpos)
865  {
866  for(ypos=0; ypos<ny_DATA3;++ypos)
867  {
868  pmask_grism_DATA3[xpos+nx_DATA3*ypos] /= pmask_grism_DATA3_norm[xpos];
869  }
870  }
871 
872 
873  for (xpos=0; xpos<nx_DATA4; ++xpos)
874  {
875  for(ypos=0; ypos<ny_DATA4;++ypos)
876  {
877  pmask_grism_DATA4[xpos+nx_DATA4*ypos] /= pmask_grism_DATA4_norm[xpos];
878  }
879  }
880 
881  cpl_image_delete(mask_grism_DATA1_norm);
882  cpl_image_delete(mask_grism_DATA2_norm);
883  cpl_image_delete(mask_grism_DATA3_norm);
884  cpl_image_delete(mask_grism_DATA4_norm);
885 
886  cpl_table_delete(table);
887 
888  }
889 
890  if (!strcmp(tag, MIDI_KAPPA_SP_MASK_PRISM)) {
891  table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
892  if (table == NULL) {
893  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
894  "Could not load the table");
895  }
896  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
897 
898  table_to_imglst_mask("DATA1",imglst_mask_prism,table);
899  table_to_imglst_mask("DATA1",imglst_mask_prism,table);
900  table_to_imglst_mask("DATA2",imglst_mask_prism,table);
901  table_to_imglst_mask("DATA3",imglst_mask_prism,table);
902  table_to_imglst_mask("DATA4",imglst_mask_prism,table);
903 
904 
905 /* Normalizing the mask to 1*/
906 
907  cpl_msg_info(cpl_func, "Normalizing the integral of the masks (PRISM) to unity ...");
908  mask_prism_DATA1=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 1),CPL_TYPE_DOUBLE);
909  mask_prism_DATA2=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 2),CPL_TYPE_DOUBLE);
910  mask_prism_DATA3=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 3),CPL_TYPE_DOUBLE);
911  mask_prism_DATA4=cpl_image_cast(cpl_imagelist_get(imglst_mask_prism, 4),CPL_TYPE_DOUBLE);
912 
913  mask_prism_DATA1_norm=cpl_image_collapse_create(mask_prism_DATA1,0);
914  mask_prism_DATA2_norm=cpl_image_collapse_create(mask_prism_DATA2,0);
915  mask_prism_DATA3_norm=cpl_image_collapse_create(mask_prism_DATA3,0);
916  mask_prism_DATA4_norm=cpl_image_collapse_create(mask_prism_DATA4,0);
917 
918  nx_DATA1=cpl_image_get_size_x(mask_prism_DATA1);
919  ny_DATA1=cpl_image_get_size_y(mask_prism_DATA1);
920  nx_DATA2=cpl_image_get_size_x(mask_prism_DATA2);
921  ny_DATA2=cpl_image_get_size_y(mask_prism_DATA2);
922  nx_DATA3=cpl_image_get_size_x(mask_prism_DATA3);
923  ny_DATA3=cpl_image_get_size_y(mask_prism_DATA3);
924  nx_DATA4=cpl_image_get_size_x(mask_prism_DATA4);
925  ny_DATA4=cpl_image_get_size_y(mask_prism_DATA4);
926 
927  pmask_prism_DATA1 = cpl_image_get_data_double(mask_prism_DATA1);
928  pmask_prism_DATA1_norm= cpl_image_get_data_double(mask_prism_DATA1_norm);
929  pmask_prism_DATA2 = cpl_image_get_data_double(mask_prism_DATA2);
930  pmask_prism_DATA2_norm= cpl_image_get_data_double(mask_prism_DATA2_norm);
931  pmask_prism_DATA3 = cpl_image_get_data_double(mask_prism_DATA3);
932  pmask_prism_DATA3_norm= cpl_image_get_data_double(mask_prism_DATA3_norm);
933  pmask_prism_DATA4 = cpl_image_get_data_double(mask_prism_DATA4);
934  pmask_prism_DATA4_norm= cpl_image_get_data_double(mask_prism_DATA4_norm);
935 
936 
937 
938  for (xpos=0; xpos<nx_DATA1; ++xpos)
939  {
940  for(ypos=0; ypos<ny_DATA1;++ypos)
941  {
942  pmask_prism_DATA1[xpos+nx_DATA1*ypos] /= pmask_prism_DATA1_norm[xpos];
943  }
944  }
945 
946  for (xpos=0; xpos<nx_DATA2; ++xpos)
947  {
948  for(ypos=0; ypos<ny_DATA2;++ypos)
949  {
950  pmask_prism_DATA2[xpos+nx_DATA2*ypos] /= pmask_prism_DATA2_norm[xpos];
951  }
952  }
953 
954 
955  for (xpos=0; xpos<nx_DATA3; ++xpos)
956  {
957  for(ypos=0; ypos<ny_DATA3;++ypos)
958  {
959  pmask_prism_DATA3[xpos+nx_DATA3*ypos] /= pmask_prism_DATA3_norm[xpos];
960  }
961  }
962 
963 
964  for (xpos=0; xpos<nx_DATA4; ++xpos)
965  {
966  for(ypos=0; ypos<ny_DATA4;++ypos)
967  {
968  pmask_prism_DATA4[xpos+nx_DATA4*ypos] /= pmask_prism_DATA4_norm[xpos];
969  }
970  }
971 
972  cpl_image_delete(mask_prism_DATA1_norm);
973  cpl_image_delete(mask_prism_DATA2_norm);
974  cpl_image_delete(mask_prism_DATA3_norm);
975  cpl_image_delete(mask_prism_DATA4_norm);
976 
977 
978  cpl_table_delete(table);
979  }
980 
981  /* Get next frame from SOF */
982  cur_frame = cpl_frameset_get_next( frameset );
983 
984  } /* while more frames */
985 
986 
987 /* Check if the mask is the right one */
988 
989  if (strcmp(gris_name,"GRISM")==0 && cpl_imagelist_get_size(imglst_mask_grism) <1)
990  {
991  cpl_msg_error(cpl_func, "Mask for GRISM needed but not found");
992  (void)cpl_error_set_where(cpl_func);
993  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Mask for filter name GRISM needed but not found");
994  }
995 
996  else if (strcmp(gris_name,"PRISM")==0 && cpl_imagelist_get_size(imglst_mask_prism) <1)
997  {
998  cpl_msg_error(cpl_func, "Mask for PRISM needed but not found");
999  (void)cpl_error_set_where(cpl_func);
1000  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Mask for filter name PRISM needed but not found");
1001  }
1002 
1003 
1004  cpl_msg_info(cpl_func, "Extracting the signal without using a mask ...");
1005 
1006  image_AOPEN_DATA2_T_collapsed_nomask=cpl_image_collapse_create(image_AOPEN_DATA2_T,0);
1007  image_AOPEN_DATA3_T_collapsed_nomask=cpl_image_collapse_create(image_AOPEN_DATA3_T,0);
1008  image_AOPEN_DATA4_T_collapsed_nomask=cpl_image_collapse_create(image_AOPEN_DATA4_T,0);
1009  image_BOPEN_DATA1_T_collapsed_nomask=cpl_image_collapse_create(image_BOPEN_DATA1_T,0);
1010  image_BOPEN_DATA2_T_collapsed_nomask=cpl_image_collapse_create(image_BOPEN_DATA2_T,0);
1011  image_BOPEN_DATA3_T_collapsed_nomask=cpl_image_collapse_create(image_BOPEN_DATA3_T,0);
1012 
1013 
1014 
1015 /* Multiply the image by the mask and collapse in the y-direction */
1016 
1017 cpl_msg_info(cpl_func, "Extracting the signal using Horne 1986, PASP 98 209 ...");
1018 cpl_msg_info(cpl_func, "and assuming that the frames are sky-dominated: V(x)-> V0 ...");
1019 
1020 
1021 if (strcmp(gris_name,"GRISM")==0){
1022  cpl_msg_info(cpl_func, "Calculating the kappa matrix for the GRISM Data...");
1023 
1024 /* Multiply the images by the Mask */
1025  cpl_msg_info(cpl_func, "1) Multiplying the images by the mask ...");
1026  cpl_image_multiply(image_AOPEN_DATA2_T,mask_grism_DATA2);
1027  cpl_image_multiply(image_AOPEN_DATA3_T,mask_grism_DATA3);
1028  cpl_image_multiply(image_AOPEN_DATA4_T,mask_grism_DATA4);
1029  cpl_image_multiply(image_BOPEN_DATA1_T,mask_grism_DATA1);
1030  cpl_image_multiply(image_BOPEN_DATA2_T,mask_grism_DATA2);
1031  cpl_image_multiply(image_BOPEN_DATA3_T,mask_grism_DATA3);
1032 
1033 /* Collapse the images in the y-direction */
1034  cpl_msg_info(cpl_func, "2) Integrating the images perpendicular to the dispersion direction ...");
1035  image_AOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA2_T,0);
1036  image_AOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA3_T,0);
1037  image_AOPEN_DATA4_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA4_T,0);
1038  image_BOPEN_DATA1_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA1_T,0);
1039  image_BOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA2_T,0);
1040  image_BOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA3_T,0);
1041 
1042 /* Derive P^2 following Horne et al. */
1043 
1044  cpl_msg_info(cpl_func, "3) Deriving mask^2 ");
1045 
1046  cpl_image_power(mask_grism_DATA1,2.0);
1047  cpl_image_power(mask_grism_DATA2,2.0);
1048  cpl_image_power(mask_grism_DATA3,2.0);
1049  cpl_image_power(mask_grism_DATA4,2.0);
1050 
1051 
1052 /* Summing up P^2 in y direction*/
1053  cpl_msg_info(cpl_func, "4) Integrating mask^2 perpendicular to the dispersion direction");
1054  mask_grism_DATA1_collapsed=cpl_image_collapse_create(mask_grism_DATA1,0);
1055  mask_grism_DATA2_collapsed=cpl_image_collapse_create(mask_grism_DATA2,0);
1056  mask_grism_DATA3_collapsed=cpl_image_collapse_create(mask_grism_DATA3,0);
1057  mask_grism_DATA4_collapsed=cpl_image_collapse_create(mask_grism_DATA4,0);
1058 
1059 
1060  cpl_msg_info(cpl_func, "5) Extracting the final flux by dividing 2) through 4)");
1061  cpl_image_divide(image_AOPEN_DATA2_T_collapsed,mask_grism_DATA2_collapsed);
1062  cpl_image_divide(image_AOPEN_DATA3_T_collapsed,mask_grism_DATA3_collapsed);
1063  cpl_image_divide(image_AOPEN_DATA4_T_collapsed,mask_grism_DATA4_collapsed);
1064  cpl_image_divide(image_BOPEN_DATA1_T_collapsed,mask_grism_DATA1_collapsed);
1065  cpl_image_divide(image_BOPEN_DATA2_T_collapsed,mask_grism_DATA2_collapsed);
1066  cpl_image_divide(image_BOPEN_DATA3_T_collapsed,mask_grism_DATA3_collapsed);
1067 
1068 /* Free the memory */
1069  cpl_image_delete(mask_grism_DATA1_collapsed);
1070  cpl_image_delete(mask_grism_DATA2_collapsed);
1071  cpl_image_delete(mask_grism_DATA3_collapsed);
1072  cpl_image_delete(mask_grism_DATA4_collapsed);
1073 
1074  }
1075 /* Free the memory */
1076  if(mask_grism_DATA1 != NULL)cpl_image_delete(mask_grism_DATA1);
1077  if(mask_grism_DATA2 != NULL)cpl_image_delete(mask_grism_DATA2);
1078  if(mask_grism_DATA3 != NULL)cpl_image_delete(mask_grism_DATA3);
1079  if(mask_grism_DATA4 != NULL)cpl_image_delete(mask_grism_DATA4);
1080 
1081 
1082 
1083 if (strcmp(gris_name,"PRISM")==0){
1084  cpl_msg_info(cpl_func, "Calculating the kappa matrix for the PRISM Data...");
1085 
1086 /* Multiply the images by the Mask */
1087  cpl_msg_info(cpl_func, "1) Multiplying the images by the mask ...");
1088  cpl_image_multiply(image_AOPEN_DATA2_T,mask_prism_DATA2);
1089  cpl_image_multiply(image_AOPEN_DATA3_T,mask_prism_DATA3);
1090  cpl_image_multiply(image_AOPEN_DATA4_T,mask_prism_DATA4);
1091  cpl_image_multiply(image_BOPEN_DATA1_T,mask_prism_DATA1);
1092  cpl_image_multiply(image_BOPEN_DATA2_T,mask_prism_DATA2);
1093  cpl_image_multiply(image_BOPEN_DATA3_T,mask_prism_DATA3);
1094 
1095 /* Collapse the images in the y-direction */
1096  cpl_msg_info(cpl_func, "2) Integrating the images perpendicular to the dispersion direction ...");
1097  image_AOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA2_T,0);
1098  image_AOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA3_T,0);
1099  image_AOPEN_DATA4_T_collapsed=cpl_image_collapse_create(image_AOPEN_DATA4_T,0);
1100  image_BOPEN_DATA1_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA1_T,0);
1101  image_BOPEN_DATA2_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA2_T,0);
1102  image_BOPEN_DATA3_T_collapsed=cpl_image_collapse_create(image_BOPEN_DATA3_T,0);
1103 
1104 /* Derive P^2 following Horne et al. */
1105 
1106  cpl_msg_info(cpl_func, "3) Deriving mask^2 ");
1107 
1108  cpl_image_power(mask_prism_DATA1,2.0);
1109  cpl_image_power(mask_prism_DATA2,2.0);
1110  cpl_image_power(mask_prism_DATA3,2.0);
1111  cpl_image_power(mask_prism_DATA4,2.0);
1112 
1113 
1114 /* Summing up P^2 in y direction*/
1115 
1116  cpl_msg_info(cpl_func, "4) Integrating mask^2 perpendicular to the dispersion direction");
1117  mask_prism_DATA1_collapsed=cpl_image_collapse_create(mask_prism_DATA1,0);
1118  mask_prism_DATA2_collapsed=cpl_image_collapse_create(mask_prism_DATA2,0);
1119  mask_prism_DATA3_collapsed=cpl_image_collapse_create(mask_prism_DATA3,0);
1120  mask_prism_DATA4_collapsed=cpl_image_collapse_create(mask_prism_DATA4,0);
1121 
1122  cpl_msg_info(cpl_func, "5) Extracting the final flux by dividing 2) through 4)");
1123  cpl_image_divide(image_AOPEN_DATA2_T_collapsed,mask_prism_DATA2_collapsed);
1124  cpl_image_divide(image_AOPEN_DATA3_T_collapsed,mask_prism_DATA3_collapsed);
1125  cpl_image_divide(image_AOPEN_DATA4_T_collapsed,mask_prism_DATA4_collapsed);
1126  cpl_image_divide(image_BOPEN_DATA1_T_collapsed,mask_prism_DATA1_collapsed);
1127  cpl_image_divide(image_BOPEN_DATA2_T_collapsed,mask_prism_DATA2_collapsed);
1128  cpl_image_divide(image_BOPEN_DATA3_T_collapsed,mask_prism_DATA3_collapsed);
1129 
1130 /* Free the memory */
1131  cpl_image_delete(mask_prism_DATA1_collapsed);
1132  cpl_image_delete(mask_prism_DATA2_collapsed);
1133  cpl_image_delete(mask_prism_DATA3_collapsed);
1134  cpl_image_delete(mask_prism_DATA4_collapsed);
1135 }
1136 
1137 /* Free the memory */
1138  if(mask_prism_DATA1 != NULL)cpl_image_delete(mask_prism_DATA1);
1139  if(mask_prism_DATA2 != NULL)cpl_image_delete(mask_prism_DATA2);
1140  if(mask_prism_DATA3 != NULL)cpl_image_delete(mask_prism_DATA3);
1141  if(mask_prism_DATA4 != NULL)cpl_image_delete(mask_prism_DATA4);
1142 
1143 
1144 /* Derive the Kappa matrix as defined in the VINCI paper (A&A 425 1161) */
1145 cpl_msg_info(cpl_func, "Deriving the KAPPA matrix following A&A 425 1161 ...");
1146 
1147 kappamatrix11=cpl_image_divide_create (image_BOPEN_DATA2_T_collapsed, image_BOPEN_DATA1_T_collapsed);
1148 kappamatrix12=cpl_image_divide_create (image_AOPEN_DATA2_T_collapsed, image_AOPEN_DATA4_T_collapsed);
1149 kappamatrix21=cpl_image_divide_create (image_BOPEN_DATA3_T_collapsed, image_BOPEN_DATA1_T_collapsed);
1150 kappamatrix22=cpl_image_divide_create (image_AOPEN_DATA3_T_collapsed, image_AOPEN_DATA4_T_collapsed);
1151 
1152 kappamatrix11_nomask=cpl_image_divide_create (image_BOPEN_DATA2_T_collapsed_nomask, image_BOPEN_DATA1_T_collapsed_nomask);
1153 kappamatrix12_nomask=cpl_image_divide_create (image_AOPEN_DATA2_T_collapsed_nomask, image_AOPEN_DATA4_T_collapsed_nomask);
1154 kappamatrix21_nomask=cpl_image_divide_create (image_BOPEN_DATA3_T_collapsed_nomask, image_BOPEN_DATA1_T_collapsed_nomask);
1155 kappamatrix22_nomask=cpl_image_divide_create (image_AOPEN_DATA3_T_collapsed_nomask, image_AOPEN_DATA4_T_collapsed_nomask);
1156 
1157 
1158 /* Free the memory */
1159 
1160 cpl_image_delete(image_AOPEN_DATA2_T);
1161 cpl_image_delete(image_AOPEN_DATA3_T);
1162 cpl_image_delete(image_AOPEN_DATA4_T);
1163 
1164 cpl_image_delete(image_BOPEN_DATA1_T);
1165 cpl_image_delete(image_BOPEN_DATA2_T);
1166 cpl_image_delete(image_BOPEN_DATA3_T);
1167 
1168 cpl_image_delete(image_AOPEN_DATA2_T_collapsed);
1169 cpl_image_delete(image_AOPEN_DATA3_T_collapsed);
1170 cpl_image_delete(image_AOPEN_DATA4_T_collapsed);
1171 
1172 cpl_image_delete(image_BOPEN_DATA1_T_collapsed);
1173 cpl_image_delete(image_BOPEN_DATA2_T_collapsed);
1174 cpl_image_delete(image_BOPEN_DATA3_T_collapsed);
1175 
1176 cpl_image_delete(image_AOPEN_DATA2_T_collapsed_nomask);
1177 cpl_image_delete(image_AOPEN_DATA3_T_collapsed_nomask);
1178 cpl_image_delete(image_AOPEN_DATA4_T_collapsed_nomask);
1179 
1180 cpl_image_delete(image_BOPEN_DATA1_T_collapsed_nomask);
1181 cpl_image_delete(image_BOPEN_DATA2_T_collapsed_nomask);
1182 cpl_image_delete(image_BOPEN_DATA3_T_collapsed_nomask);
1183 
1184 /* Smooth the kappa matrix */
1185 
1186 /* Create the kernel */
1187 
1188 
1189 /*
1190 kernel = cpl_matrix_new(1, medianwindow);
1191 cpl_matrix_fill(kernel, 1.0);
1192 */
1193 
1194 cpl_msg_info(cpl_func, "Smoothing the KAPPA matrix ...");
1195 
1196 mask = cpl_mask_new(medianwindow,1);
1197 cpl_mask_not(mask);
1198 
1199 kappamatrix11_filtered=cpl_image_duplicate(kappamatrix11);
1200 kappamatrix12_filtered=cpl_image_duplicate(kappamatrix12);
1201 kappamatrix21_filtered=cpl_image_duplicate(kappamatrix21);
1202 kappamatrix22_filtered=cpl_image_duplicate(kappamatrix22);
1203 
1204 
1205 cpl_image_filter_mask(kappamatrix11_filtered,kappamatrix11, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
1206 cpl_image_filter_mask(kappamatrix12_filtered,kappamatrix12, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
1207 cpl_image_filter_mask(kappamatrix21_filtered,kappamatrix21, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
1208 cpl_image_filter_mask(kappamatrix22_filtered,kappamatrix22, mask,CPL_FILTER_MEDIAN ,CPL_BORDER_FILTER);
1209 
1210 cpl_mask_delete(mask);
1211 /*
1212 
1213 kappamatrix11_filtered=cpl_image_filter_median(kappamatrix11, kernel);
1214 kappamatrix12_filtered=cpl_image_filter_median(kappamatrix12, kernel);
1215 kappamatrix21_filtered=cpl_image_filter_median(kappamatrix21, kernel);
1216 kappamatrix22_filtered=cpl_image_filter_median(kappamatrix22, kernel);
1217 */
1218 
1219 
1220 /* Deriving QC Parameters */
1221 
1222 if (strcmp(gris_name,"GRISM")==0)
1223 {
1224  lowlimit=140;
1225  highlimit=160;
1226 }
1227 
1228 else
1229 {
1230  lowlimit=65;
1231  highlimit=85;
1232 }
1233 
1234 
1235 
1236  /* Derives the standard deviation */
1237  kappamatrix11_stdev=cpl_image_get_stdev_window (kappamatrix11,lowlimit,1,highlimit,1);
1238  kappamatrix11_stdev_nomask=cpl_image_get_stdev_window (kappamatrix11_nomask,lowlimit,1,highlimit,1);
1239  kappamatrix11_stdev_filtered=cpl_image_get_stdev_window (kappamatrix11_filtered,lowlimit,1,highlimit,1);
1240 
1241  kappamatrix12_stdev=cpl_image_get_stdev_window (kappamatrix12,lowlimit,1,highlimit,1);
1242  kappamatrix12_stdev_nomask=cpl_image_get_stdev_window (kappamatrix12_nomask,lowlimit,1,highlimit,1);
1243  kappamatrix12_stdev_filtered=cpl_image_get_stdev_window (kappamatrix12_filtered,lowlimit,1,highlimit,1);
1244 
1245  kappamatrix21_stdev=cpl_image_get_stdev_window (kappamatrix21,lowlimit,1,highlimit,1);
1246  kappamatrix21_stdev_nomask=cpl_image_get_stdev_window (kappamatrix21_nomask,lowlimit,1,highlimit,1);
1247  kappamatrix21_stdev_filtered=cpl_image_get_stdev_window (kappamatrix21_filtered,lowlimit,1,highlimit,1);
1248 
1249  kappamatrix22_stdev=cpl_image_get_stdev_window (kappamatrix22,lowlimit,1,highlimit,1);
1250  kappamatrix22_stdev_nomask=cpl_image_get_stdev_window (kappamatrix22_nomask,lowlimit,1,highlimit,1);
1251  kappamatrix22_stdev_filtered=cpl_image_get_stdev_window (kappamatrix22_filtered,lowlimit,1,highlimit,1);
1252 
1253  /* Derives the median */
1254  kappamatrix11_median=cpl_image_get_median_window (kappamatrix11,lowlimit,1,highlimit,1);
1255  kappamatrix11_median_nomask=cpl_image_get_median_window (kappamatrix11_nomask,lowlimit,1,highlimit,1);
1256  kappamatrix11_median_filtered=cpl_image_get_median_window (kappamatrix11_filtered,lowlimit,1,highlimit,1);
1257 
1258  kappamatrix12_median=cpl_image_get_median_window (kappamatrix12,lowlimit,1,highlimit,1);
1259  kappamatrix12_median_nomask=cpl_image_get_median_window (kappamatrix12_nomask,lowlimit,1,highlimit,1);
1260  kappamatrix12_median_filtered=cpl_image_get_median_window (kappamatrix12_filtered,lowlimit,1,highlimit,1);
1261 
1262  kappamatrix21_median=cpl_image_get_median_window (kappamatrix21,lowlimit,1,highlimit,1);
1263  kappamatrix21_median_nomask=cpl_image_get_median_window (kappamatrix21_nomask,lowlimit,1,highlimit,1);
1264  kappamatrix21_median_filtered=cpl_image_get_median_window (kappamatrix21_filtered,lowlimit,1,highlimit,1);
1265 
1266  kappamatrix22_median=cpl_image_get_median_window (kappamatrix22,lowlimit,1,highlimit,1);
1267  kappamatrix22_median_nomask=cpl_image_get_median_window (kappamatrix22_nomask,lowlimit,1,highlimit,1);
1268  kappamatrix22_median_filtered=cpl_image_get_median_window (kappamatrix22_filtered,lowlimit,1,highlimit,1);
1269 
1270 
1271 
1272 
1273 
1274 cpl_msg_info(cpl_func, "Saving the KAPPA matrix(es) ...");
1275 
1276 
1277  /* Save the KAPPA matrix(es) and add QC parameters */
1278  qclist11 = cpl_propertylist_new();
1279  qclist12 = cpl_propertylist_new();
1280  qclist21 = cpl_propertylist_new();
1281  qclist22 = cpl_propertylist_new();
1282 
1283  qclist11_nomask = cpl_propertylist_new();
1284  qclist12_nomask = cpl_propertylist_new();
1285  qclist21_nomask = cpl_propertylist_new();
1286  qclist22_nomask = cpl_propertylist_new();
1287 
1288  qclist11_filtered = cpl_propertylist_new();
1289  qclist12_filtered = cpl_propertylist_new();
1290  qclist21_filtered = cpl_propertylist_new();
1291  qclist22_filtered = cpl_propertylist_new();
1292 
1293  /* Adding the standard median */
1294  cpl_propertylist_append_double(qclist11, "ESO QC KAPPA MEDIAN 11", kappamatrix11_median);
1295  cpl_propertylist_append_double(qclist12, "ESO QC KAPPA MEDIAN 12", kappamatrix12_median);
1296  cpl_propertylist_append_double(qclist21, "ESO QC KAPPA MEDIAN 21", kappamatrix21_median);
1297  cpl_propertylist_append_double(qclist22, "ESO QC KAPPA MEDIAN 22", kappamatrix22_median);
1298 
1299  cpl_propertylist_append_double(qclist11_nomask, "ESO QC KAPPA NOMASK MEDIAN 11", kappamatrix11_median_nomask);
1300  cpl_propertylist_append_double(qclist12_nomask, "ESO QC KAPPA NOMASK MEDIAN 12", kappamatrix12_median_nomask);
1301  cpl_propertylist_append_double(qclist21_nomask, "ESO QC KAPPA NOMASK MEDIAN 21", kappamatrix21_median_nomask);
1302  cpl_propertylist_append_double(qclist22_nomask, "ESO QC KAPPA NOMASK MEDIAN 22", kappamatrix22_median_nomask);
1303 
1304  cpl_propertylist_append_double(qclist11_filtered, "ESO QC KAPPA FILTERED MEDIAN 11", kappamatrix11_median_filtered);
1305  cpl_propertylist_append_double(qclist12_filtered, "ESO QC KAPPA FILTERED MEDIAN 12", kappamatrix12_median_filtered);
1306  cpl_propertylist_append_double(qclist21_filtered, "ESO QC KAPPA FILTERED MEDIAN 21", kappamatrix21_median_filtered);
1307  cpl_propertylist_append_double(qclist22_filtered, "ESO QC KAPPA FILTERED MEDIAN 22", kappamatrix22_median_filtered);
1308 
1309  /* Adding the standard deviation */
1310  cpl_propertylist_append_double(qclist11, "ESO QC KAPPA STDEV 11", kappamatrix11_stdev);
1311  cpl_propertylist_append_double(qclist12, "ESO QC KAPPA STDEV 12", kappamatrix12_stdev);
1312  cpl_propertylist_append_double(qclist21, "ESO QC KAPPA STDEV 21", kappamatrix21_stdev);
1313  cpl_propertylist_append_double(qclist22, "ESO QC KAPPA STDEV 22", kappamatrix22_stdev);
1314 
1315  cpl_propertylist_append_double(qclist11_nomask, "ESO QC KAPPA NOMASK STDEV 11", kappamatrix11_stdev_nomask);
1316  cpl_propertylist_append_double(qclist12_nomask, "ESO QC KAPPA NOMASK STDEV 12", kappamatrix12_stdev_nomask);
1317  cpl_propertylist_append_double(qclist21_nomask, "ESO QC KAPPA NOMASK STDEV 21", kappamatrix21_stdev_nomask);
1318  cpl_propertylist_append_double(qclist22_nomask, "ESO QC KAPPA NOMASK STDEV 22", kappamatrix22_stdev_nomask);
1319 
1320  cpl_propertylist_append_double(qclist11_filtered, "ESO QC KAPPA FILTERED STDEV 11", kappamatrix11_stdev_filtered);
1321  cpl_propertylist_append_double(qclist12_filtered, "ESO QC KAPPA FILTERED STDEV 12", kappamatrix12_stdev_filtered);
1322  cpl_propertylist_append_double(qclist21_filtered, "ESO QC KAPPA FILTERED STDEV 21", kappamatrix21_stdev_filtered);
1323  cpl_propertylist_append_double(qclist22_filtered, "ESO QC KAPPA FILTERED STDEV 22", kappamatrix22_stdev_filtered);
1324 
1325 
1326 
1327 cpl_propertylist_update_string(qclist11, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX11");
1328 cpl_propertylist_update_string(qclist12, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX12");
1329 cpl_propertylist_update_string(qclist21, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX21");
1330 cpl_propertylist_update_string(qclist22, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX22");
1331 
1332 cpl_propertylist_update_string(qclist11_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX11_NOMASK");
1333 cpl_propertylist_update_string(qclist12_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX12_NOMASK");
1334 cpl_propertylist_update_string(qclist21_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX21_NOMASK");
1335 cpl_propertylist_update_string(qclist22_nomask, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX22_NOMASK");
1336 
1337 cpl_propertylist_update_string(qclist11_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX11_FILTERED");
1338 cpl_propertylist_update_string(qclist12_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX12_FILTERED");
1339 cpl_propertylist_update_string(qclist21_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX21_FILTERED");
1340 cpl_propertylist_update_string(qclist22_filtered, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX22_FILTERED");
1341 
1342 
1343 
1344 
1345 
1346 
1347 
1348 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix11,
1349  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1350  qclist11, NULL,
1351  PACKAGE "/" PACKAGE_VERSION,
1352  "midi_kappamatrix11.fits")) {
1353  /* Propagate the error */
1354  (void)cpl_error_set_where(cpl_func);
1355 }
1356 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix12,
1357  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1358  qclist12, NULL,
1359  PACKAGE "/" PACKAGE_VERSION,
1360  "midi_kappamatrix12.fits")) {
1361  /* Propagate the error */
1362  (void)cpl_error_set_where(cpl_func);
1363 }
1364 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix21,
1365  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1366  qclist21, NULL,
1367  PACKAGE "/" PACKAGE_VERSION,
1368  "midi_kappamatrix21.fits")) {
1369  /* Propagate the error */
1370  (void)cpl_error_set_where(cpl_func);
1371 }
1372 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix22,
1373  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1374  qclist22, NULL,
1375  PACKAGE "/" PACKAGE_VERSION,
1376  "midi_kappamatrix22.fits")) {
1377  /* Propagate the error */
1378  (void)cpl_error_set_where(cpl_func);
1379 }
1380 
1381 
1382 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix11_nomask,
1383  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1384  qclist11_nomask, NULL,
1385  PACKAGE "/" PACKAGE_VERSION,
1386  "midi_kappamatrix11_nomask.fits")) {
1387  /* Propagate the error */
1388  (void)cpl_error_set_where(cpl_func);
1389 }
1390 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix12_nomask,
1391  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1392  qclist12_nomask, NULL,
1393  PACKAGE "/" PACKAGE_VERSION,
1394  "midi_kappamatrix12_nomask.fits")) {
1395  /* Propagate the error */
1396  (void)cpl_error_set_where(cpl_func);
1397 }
1398 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix21_nomask,
1399  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1400  qclist21_nomask, NULL,
1401  PACKAGE "/" PACKAGE_VERSION,
1402  "midi_kappamatrix21_nomask.fits")) {
1403  /* Propagate the error */
1404  (void)cpl_error_set_where(cpl_func);
1405 }
1406 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix22_nomask,
1407  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1408  qclist22_nomask, NULL,
1409  PACKAGE "/" PACKAGE_VERSION,
1410  "midi_kappamatrix22_nomask.fits")) {
1411  /* Propagate the error */
1412  (void)cpl_error_set_where(cpl_func);
1413 }
1414 
1415 
1416 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix11_filtered,
1417  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1418  qclist11_filtered, NULL,
1419  PACKAGE "/" PACKAGE_VERSION,
1420  "midi_kappamatrix11_filtered.fits")) {
1421  /* Propagate the error */
1422  (void)cpl_error_set_where(cpl_func);
1423 }
1424 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix12_filtered,
1425  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1426  qclist12_filtered, NULL,
1427  PACKAGE "/" PACKAGE_VERSION,
1428  "midi_kappamatrix12_filtered.fits")) {
1429  /* Propagate the error */
1430  (void)cpl_error_set_where(cpl_func);
1431 }
1432 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix21_filtered,
1433  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1434  qclist21_filtered, NULL,
1435  PACKAGE "/" PACKAGE_VERSION,
1436  "midi_kappamatrix21_filtered.fits")) {
1437  /* Propagate the error */
1438  (void)cpl_error_set_where(cpl_func);
1439 }
1440 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, kappamatrix22_filtered,
1441  CPL_BPP_IEEE_FLOAT, "midi_kappamatrix",
1442  qclist22_filtered, NULL,
1443  PACKAGE "/" PACKAGE_VERSION,
1444  "midi_kappamatrix22_filtered.fits")) {
1445  /* Propagate the error */
1446  (void)cpl_error_set_where(cpl_func);
1447 }
1448 
1449 
1450 /* Save it to a fits table */
1451 
1452 kappamatrix_table=cpl_table_new(cpl_image_get_size_x(kappamatrix11));
1453 cpl_table_new_column(kappamatrix_table, "kappam11", CPL_TYPE_FLOAT);
1454 cpl_table_new_column(kappamatrix_table, "kappam11_error", CPL_TYPE_FLOAT);
1455 cpl_table_new_column(kappamatrix_table, "kappam12", CPL_TYPE_FLOAT);
1456 cpl_table_new_column(kappamatrix_table, "kappam12_error", CPL_TYPE_FLOAT);
1457 cpl_table_new_column(kappamatrix_table, "kappam21", CPL_TYPE_FLOAT);
1458 cpl_table_new_column(kappamatrix_table, "kappam21_error", CPL_TYPE_FLOAT);
1459 cpl_table_new_column(kappamatrix_table, "kappam22", CPL_TYPE_FLOAT);
1460 cpl_table_new_column(kappamatrix_table, "kappam22_error", CPL_TYPE_FLOAT);
1461 
1462 cpl_table_copy_data_float(kappamatrix_table, "kappam11", cpl_image_get_data_float((kappamatrix11)));
1463 cpl_table_copy_data_float(kappamatrix_table, "kappam12", cpl_image_get_data_float((kappamatrix12)));
1464 cpl_table_copy_data_float(kappamatrix_table, "kappam21", cpl_image_get_data_float((kappamatrix21)));
1465 cpl_table_copy_data_float(kappamatrix_table, "kappam22", cpl_image_get_data_float((kappamatrix22)));
1466 
1467 cpl_table_fill_column_window_float (kappamatrix_table, "kappam11_error", 0, cpl_image_get_size_x(kappamatrix11), 0.05);
1468 cpl_table_fill_column_window_float (kappamatrix_table, "kappam12_error", 0, cpl_image_get_size_x(kappamatrix12), 0.05);
1469 cpl_table_fill_column_window_float (kappamatrix_table, "kappam21_error", 0, cpl_image_get_size_x(kappamatrix21), 0.05);
1470 cpl_table_fill_column_window_float (kappamatrix_table, "kappam22_error", 0, cpl_image_get_size_x(kappamatrix22), 0.05);
1471 
1472 qclist_all = cpl_propertylist_new();
1473 cpl_propertylist_append(qclist_all,qclist11);
1474 cpl_propertylist_append(qclist_all,qclist12);
1475 cpl_propertylist_append(qclist_all,qclist21);
1476 cpl_propertylist_append(qclist_all,qclist22);
1477 cpl_propertylist_erase_regexp(qclist_all, CPL_DFS_PRO_CATG,0);
1478 
1479 
1480 /* Propertylist to write to the extension */
1481 qclist_all_extension = cpl_propertylist_new();
1482 cpl_propertylist_append_string (qclist_all_extension, "EXTNAME", "MIDI_KAPPAMATRIX");
1483 
1484 
1485 if (strcmp(gris_name,"GRISM")==0)
1486 {
1487 
1488 cpl_propertylist_update_string(qclist_all, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX_GRISM");
1489 
1490 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, kappamatrix_table,
1491  qclist_all_extension, "midi_kappamatrix",
1492  qclist_all, NULL,
1493  PACKAGE "/" PACKAGE_VERSION,
1494  "midi_kappamatrix_grism.fits");
1495  cpl_table_delete(kappamatrix_table);
1496 }
1497 
1498 if (strcmp(gris_name,"PRISM")==0)
1499 {
1500 cpl_propertylist_update_string(qclist_all, CPL_DFS_PRO_CATG, "MIDI_KAPPAMATRIX_PRISM");
1501 cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, kappamatrix_table,
1502  qclist_all_extension, "midi_kappamatrix",
1503  qclist_all, NULL,
1504  PACKAGE "/" PACKAGE_VERSION,
1505  "midi_kappamatrix_prism.fits");
1506  cpl_table_delete(kappamatrix_table);
1507 }
1508 
1509 /* Save it to an ascii file */
1510 
1511 
1512 
1513 if (strcmp(gris_name,"GRISM")==0)
1514 {
1515  cubename = cpl_sprintf("%s_%s%s", "transferRatios", "GRISM", ".dat");
1516  kappa_to_ascii( kappamatrix11, kappamatrix12, kappamatrix21, kappamatrix22, cubename);
1517  cpl_free(cubename);
1518 
1519  cubename = cpl_sprintf("%s_%s%s", "transferRatios", "GRISM", "_nomask.dat");
1520  kappa_to_ascii( kappamatrix11_nomask, kappamatrix12_nomask, kappamatrix21_nomask, kappamatrix22_nomask, cubename);
1521  cpl_free(cubename);
1522 
1523  cubename = cpl_sprintf("%s_%s%s", "transferRatios", "GRISM", "_filtered.dat");
1524  kappa_to_ascii( kappamatrix11_filtered, kappamatrix12_filtered, kappamatrix21_filtered, kappamatrix22_filtered, cubename);
1525  cpl_free(cubename);
1526 
1527 
1528 }
1529 
1530 if (strcmp(gris_name,"PRISM")==0)
1531 {
1532  cubename = cpl_sprintf("%s_%s%s", "transferRatios", "PRISM", ".dat");
1533  kappa_to_ascii( kappamatrix11, kappamatrix12, kappamatrix21, kappamatrix22, cubename);
1534  cpl_free(cubename);
1535 
1536  cubename = cpl_sprintf("%s_%s%s", "transferRatios", "PRISM", "_nomask.dat");
1537  kappa_to_ascii( kappamatrix11_nomask, kappamatrix12_nomask, kappamatrix21_nomask, kappamatrix22_nomask, cubename);
1538  cpl_free(cubename);
1539 
1540  cubename = cpl_sprintf("%s_%s%s", "transferRatios", "PRISM", "_filtered.dat");
1541  kappa_to_ascii( kappamatrix11_filtered, kappamatrix12_filtered, kappamatrix21_filtered, kappamatrix22_filtered, cubename);
1542  cpl_free(cubename);
1543 
1544 }
1545 
1546 
1547 
1548 
1549 /* Free the memory */
1550 cpl_image_delete(kappamatrix11);
1551 cpl_image_delete(kappamatrix12);
1552 cpl_image_delete(kappamatrix21);
1553 cpl_image_delete(kappamatrix22);
1554 
1555 cpl_image_delete(kappamatrix11_nomask);
1556 cpl_image_delete(kappamatrix12_nomask);
1557 cpl_image_delete(kappamatrix21_nomask);
1558 cpl_image_delete(kappamatrix22_nomask);
1559 
1560 cpl_image_delete(kappamatrix11_filtered);
1561 cpl_image_delete(kappamatrix12_filtered);
1562 cpl_image_delete(kappamatrix21_filtered);
1563 cpl_image_delete(kappamatrix22_filtered);
1564 
1565 
1566 /*cpl_matrix_delete(kernel) ;*/
1567 
1568  while(cpl_imagelist_get_size(imglst_mask_prism)>0){
1569  cpl_image_delete(cpl_imagelist_unset(imglst_mask_prism,0));
1570  }
1571 
1572  while(cpl_imagelist_get_size(imglst_mask_grism)>0){
1573  cpl_image_delete(cpl_imagelist_unset(imglst_mask_grism,0));
1574  }
1575 
1576 
1577 cpl_imagelist_delete(imglst_mask_prism);
1578 cpl_imagelist_delete(imglst_mask_grism);
1579 
1580 
1581 
1582 
1583 
1584 cpl_propertylist_delete(qclist11);
1585 cpl_propertylist_delete(qclist12);
1586 cpl_propertylist_delete(qclist21);
1587 cpl_propertylist_delete(qclist22);
1588 cpl_propertylist_delete(qclist11_nomask);
1589 cpl_propertylist_delete(qclist12_nomask);
1590 cpl_propertylist_delete(qclist21_nomask);
1591 cpl_propertylist_delete(qclist22_nomask);
1592 cpl_propertylist_delete(qclist11_filtered);
1593 cpl_propertylist_delete(qclist12_filtered);
1594 cpl_propertylist_delete(qclist21_filtered);
1595 cpl_propertylist_delete(qclist22_filtered);
1596 cpl_propertylist_delete(qclist_all);
1597 cpl_propertylist_delete(qclist_all_extension);
1598 
1599 
1600 return (int)cpl_error_get_code();
1601 }
1602 
1603 
1604 
1605 static int table_to_imglst_sky_target(const char * columname,
1606  const char * columntype,
1607  cpl_imagelist * imglst_sky,
1608  cpl_imagelist * imglst_target,
1609  cpl_table * table)
1610 {
1611 
1612  int dimenDATA;
1613  int i,csky=0, ctarget=0,csky_tmp=0, ctarget_tmp=0,first_unknown=0;
1614  cpl_array * array_data=NULL;
1615  cpl_image * image_data_int=NULL;
1616  cpl_errorstate prestate = cpl_errorstate_get();
1617  char ** target_type;
1618  cpl_type ctype;
1619  int flag_issky=0;
1620  int flag_istarget=0;
1621 
1622  cpl_imagelist * imglst_sky_tmp=NULL;
1623  cpl_imagelist * imglst_target_tmp=NULL;
1624 
1625  imglst_sky_tmp=cpl_imagelist_new();
1626  imglst_target_tmp=cpl_imagelist_new();
1627 
1628  /* Load extension Imaging Data */
1629 
1630  dimenDATA=cpl_table_get_column_dimensions(table, columname);
1631  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
1632  if (dimenDATA != 2) {
1633  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1634  "DATA has a wrong dimension");
1635  }
1636  /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
1637  /* Loop over all "images" stored in DATA and add them to the imagelist */
1638 
1639 
1640  if (cpl_table_has_column(table, columntype))
1641  {
1642  target_type=cpl_table_get_data_string(table, columntype);
1643 
1644  }
1645  else
1646  {
1647  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1648  "TYPE of the Column not found");
1649  }
1650 /* cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
1651 /* cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
1652 
1653 
1654  ctype=cpl_table_get_column_type(table, columname);
1655 /* cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
1656 
1657 
1658 
1659  csky=cpl_imagelist_get_size(imglst_sky);
1660 
1661  ctarget=cpl_imagelist_get_size(imglst_target);
1662 
1663 /* cpl_msg_info(cpl_func, "Length of imagelist S T : %d %d",csky,ctarget ); */
1664 
1665 
1666 
1667  flag_issky=0;
1668  flag_istarget=0;
1669 
1670 /* Find the first "U" image and discard the images befor */
1671 
1672  for (i=0; i<cpl_table_get_nrow(table);i++){
1673  if(strcmp(target_type[i],"U")== 0){
1674  first_unknown=i;
1675  break;
1676  }
1677  }
1678 
1679 /* cpl_msg_info(cpl_func, "First unknown: %d",first_unknown); */
1680 
1681  for (i=first_unknown; i<cpl_table_get_nrow(table);i++){
1682 
1683  array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
1684  if(ctype&CPL_TYPE_INT){
1685  image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
1686  }
1687  if(ctype&CPL_TYPE_FLOAT){
1688  image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
1689  }
1690  /* Cast the image to float */
1691 /* image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
1692 
1693 /* Append the image to the imagelists */
1694 
1695  if(strcmp(target_type[i],"S")== 0){
1696  cpl_imagelist_set(imglst_sky_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),csky_tmp++);
1697  flag_issky=1;
1698  cpl_image_unwrap(image_data_int);
1699  continue;
1700  }
1701  if(strcmp(target_type[i],"T")== 0){
1702  cpl_imagelist_set(imglst_target_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),ctarget_tmp++);
1703  flag_istarget=1;
1704  cpl_image_unwrap(image_data_int);
1705  continue;
1706  }
1707 
1708  /*Unwrap processed image manualy if nor Target nor Sky*/
1709  if(image_data_int!=NULL){
1710  cpl_image_unwrap(image_data_int);
1711  }
1712  if(flag_issky >0 && flag_istarget >0){
1713  /*Collapse the temporary imagelist and add the image to the "final" imagelist */
1714  cpl_imagelist_set(imglst_sky,cpl_imagelist_collapse_create(imglst_sky_tmp),csky++);
1715  cpl_imagelist_set(imglst_target,cpl_imagelist_collapse_create(imglst_target_tmp),ctarget++);
1716  csky_tmp=0;
1717  ctarget_tmp=0;
1718  flag_issky=0;
1719  flag_istarget=0;
1720 
1721  /*Empty imagelist and free the image buffer*/
1722  while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
1723  cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
1724  }
1725  while(cpl_imagelist_get_size(imglst_target_tmp)>0){
1726  cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
1727  }
1728 
1729  }
1730 
1731  }
1732 
1733 
1734 /* Empty imagelist and free the image buffer*/
1735  while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
1736  cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
1737  }
1738  while(cpl_imagelist_get_size(imglst_target_tmp)>0){
1739  cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
1740  }
1741 
1742  cpl_imagelist_delete(imglst_sky_tmp);
1743  cpl_imagelist_delete(imglst_target_tmp);
1744 
1745 
1746 
1747  return (int)cpl_error_get_code();
1748 }
1749 
1750 
1751 
1752 
1753 
1754 
1755 
1756 static int table_to_imglst_mask(const char * columname,
1757  cpl_imagelist * imglst,
1758  cpl_table * table)
1759 {
1760 
1761  int dimenDATA;
1762  int i=0, counter=0;
1763  cpl_array * array_data=NULL;
1764  cpl_image * image_data_int=NULL;
1765  cpl_errorstate prestate = cpl_errorstate_get();
1766  cpl_type ctype;
1767 
1768  /* Load extension Imaging Data */
1769 
1770  dimenDATA=cpl_table_get_column_dimensions(table, columname);
1771  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
1772  if (dimenDATA != 2) {
1773  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1774  "DATA has a wrong dimension");
1775  }
1776  /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
1777  /* Loop over all "images" stored in DATA and add them to the imagelist */
1778 
1779 
1780 /* cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
1781 /* cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
1782 
1783 
1784  ctype=cpl_table_get_column_type(table, columname);
1785 /* cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
1786 
1787 
1788 
1789  counter=cpl_imagelist_get_size(imglst);
1790 
1791 
1792  for (i=0; i<cpl_table_get_nrow(table);i++){
1793 
1794  array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
1795  if(ctype&CPL_TYPE_INT){
1796  image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
1797  }
1798  if(ctype&CPL_TYPE_FLOAT){
1799  image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
1800  }
1801  /* Cast the image to float */
1802 /* image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
1803 
1804 /* Append the image to the imagelists */
1805 
1806  cpl_imagelist_set(imglst,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),counter++);
1807 
1808  /*Unwrap processed image manualy if nor Target nor Sky*/
1809  if(image_data_int!=NULL){
1810  cpl_image_unwrap(image_data_int);
1811  }
1812 
1813 
1814  }
1815 
1816 
1817 
1818  return (int)cpl_error_get_code();
1819 }
1820 
1821 
1822 
1823 
1824 
1825 
1826 
1827 static void kappa_to_ascii(const cpl_image * kappa11,
1828  const cpl_image * kappa12,
1829  const cpl_image * kappa21,
1830  const cpl_image * kappa22,
1831  const char * filename)
1832 {
1833  FILE * fp ;
1834  int pis_rejected;
1835  int i;
1836  if ( filename == NULL)
1837  {
1838  cpl_msg_error(cpl_func,"please give the filename!") ;
1839  return ;
1840  }
1841 
1842  if ( NULL == (fp = fopen ( filename, "w" ) ) )
1843  {
1844  cpl_msg_error(cpl_func,"cannot open %s", filename) ;
1845  return ;
1846  }
1847 
1848 
1849  fprintf (fp, "%d \n",cpl_image_get_size_x(kappa11));
1850 
1851  for ( i = 1 ; i <= cpl_image_get_size_x(kappa11) ; i++ )
1852  {
1853  fprintf (fp, "%f 0.05 %f 0.05 %f 0.05 %f 0.05 \n",
1854  cpl_image_get(kappa11, i, 1,&pis_rejected),
1855  cpl_image_get(kappa12, i, 1,&pis_rejected),
1856  cpl_image_get(kappa21, i, 1,&pis_rejected),
1857  cpl_image_get(kappa22, i, 1,&pis_rejected)) ;
1858  }
1859  fclose (fp ) ;
1860 }
1861 
1862