MIDI Pipeline Reference Manual  2.8.3
midi_profile.c
1 /* $Id: midi_profile.c,v 1.32 2012-04-10 13:55:16 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 13:55:16 $
24  * $Revision: 1.32 $
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 <fitsio.h>
44 #include "midi_cplupgrade.h"
45 
46 /*-----------------------------------------------------------------------------
47  Private function prototypes
48  -----------------------------------------------------------------------------*/
49 
50 static int midi_profile_create(cpl_plugin *);
51 static int midi_profile_exec(cpl_plugin *);
52 static int midi_profile_destroy(cpl_plugin *);
53 static int midi_profile(cpl_frameset *, const cpl_parameterlist *);
54 static int midi_normalize_image_y(cpl_image * image);
55 static int table_to_imglst_sky_target(const char * ,
56  const char * ,
57  cpl_imagelist * ,
58  cpl_imagelist * ,
59  cpl_table *);
60 static int append_image_to_table(cpl_table * table, const char * columname,
61  cpl_image * image, int row);
62 static int midi_copy_extension(const char * infile , const char * outfile,
63  const char * extension_name);
64 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list, int index,
65  cpl_image * image, const int * x_start, const int * x_end);
66 static void midi_profile_qc_maximum(cpl_propertylist * pro_list, int index,
67  cpl_image * image, const int * x_start, const int * x_end);
68 
69 /*-----------------------------------------------------------------------------
70  Static variables
71  -----------------------------------------------------------------------------*/
72 
73 static char midi_profile_description[] =
74 "This recipe derives the 2D profile of the spectral dispersed signal in\n"
75 "the SCI_PHOT and HIGH_SENS mode. It combines a set of AOPEN and BOPEN\n"
76 "photometry files with the following tags:\n"
77 "\n"
78 "PHOTOM_SP_CALIB/PHOTOM_SP_SCIENCE \n"
79 "or\n"
80 "PHOTOM_HS_CALIB/PHOTOM_HS_SCIENCE. \n"
81 "\n"
82 "The sky subtracted data are co-added and then a two pass threshold is\n"
83 "applied in order to get a clean profile of the signal: In a first pass\n"
84 "the standard deviation of the image is derived and all the pixels\n"
85 "exceeding the latter are marked as they are supposed to belong to the\n"
86 "source and not to the background. In a second pass the standard\n"
87 "deviation is recalculated excluding these source-pixels. After that,\n"
88 "all pixels of the image not exceeding (<threshold> times\n"
89 "standard-deviation) are set to zero. Please note, that the <threshold>\n"
90 "variable can be set as a recipe option in sigma units. Finally the\n"
91 "integral of the profile perpendicular to the dispersion direction is\n"
92 "normalized to unity.\n"
93 
94 "Input files:\n\n"
95 " DO category: Type: Explanation: Required:\n"
96 " PHOTOM_SP_CALIB Raw Raw data frame \n"
97 " or \n"
98 " PHOTOM_SP_SCIENCE Raw Raw data frame \n"
99 " or Y\n"
100 " PHOTOM_HS_CALIB Raw Raw data frame \n"
101 " or \n"
102 " PHOTOM_HS_SCIENCE Raw Raw data frame \n\n"
103 "Output files:\n\n"
104 " DO category: Data type: Explanation:\n"
105 " KAPPA_HS_MASK_PRISM FITS table Spectral profile: Main product\n"
106 " or \n"
107 " KAPPA_HS_MASK_GRISM FITS table Spectral profile: Main product \n\n"
108 " KAPPA_HS_MASK_PRISM_PROFILE FITS image single profile: For diagnostics\n"
109 " or \n"
110 " KAPPA_HS_MASK_GRISM_PROFILE FITS image single profile: For diagnostics\n\n"
111 " KAPPA_HS_MASK_PRISM_SIGNAL FITS image single signal: For diagnostics\n"
112 " or \n"
113 " KAPPA_HS_MASK_GRISM_SIGNAL FITS image single signal: For diagnostics\n";
114 
115 /*-----------------------------------------------------------------------------
116  Function code
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_profile",
149  "Derives the Profile of the spectrum",
150  midi_profile_description,
151  "Armin Gabasch",
152  PACKAGE_BUGREPORT,
154  midi_profile_create,
155  midi_profile_exec,
156  midi_profile_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_profile_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  p = cpl_parameter_new_value("midi.midi_profile.threshold",
216  CPL_TYPE_DOUBLE, "The threshold to discriminate between source and background pixels in sigma units",
217  "midi.midi_profile",1.7);
218  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "threshold");
219  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
220  cpl_parameterlist_append(recipe->parameters, p);
221 
222 
223  return 0;
224 }
225 
226 /*----------------------------------------------------------------------------*/
232 /*----------------------------------------------------------------------------*/
233 static int midi_profile_exec(cpl_plugin * plugin)
234 {
235 
236  cpl_recipe * recipe;
237  int recipe_status;
238  cpl_errorstate initial_errorstate = cpl_errorstate_get();
239 
240  /* Return immediately if an error code is already set */
241  if (cpl_error_get_code() != CPL_ERROR_NONE) {
242  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
243  cpl_func, __LINE__, cpl_error_get_where());
244  return (int)cpl_error_get_code();
245  }
246 
247  if (plugin == NULL) {
248  cpl_msg_error(cpl_func, "Null plugin");
249  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
250  }
251 
252  /* Verify plugin type */
253  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
254  cpl_msg_error(cpl_func, "Plugin is not a recipe");
255  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
256  }
257 
258  /* Get the recipe */
259  recipe = (cpl_recipe *)plugin;
260 
261  /* Verify parameter and frame lists */
262  if (recipe->parameters == NULL) {
263  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
264  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
265  }
266  if (recipe->frames == NULL) {
267  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
268  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
269  }
270 
271  /* Invoke the recipe */
272  recipe_status = midi_profile(recipe->frames, recipe->parameters);
273 
274  /* Ensure DFS-compliance of the products */
275  if (cpl_dfs_update_product_header(recipe->frames)) {
276  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
277  }
278 
279  if (!cpl_errorstate_is_equal(initial_errorstate)) {
280  /* Dump the error history since recipe execution start.
281  At this point the recipe cannot recover from the error */
282  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
283  }
284 
285  return recipe_status;
286 }
287 
288 /*----------------------------------------------------------------------------*/
294 /*----------------------------------------------------------------------------*/
295 static int midi_profile_destroy(cpl_plugin * plugin)
296 {
297  cpl_recipe * recipe;
298 
299  if (plugin == NULL) {
300  cpl_msg_error(cpl_func, "Null plugin");
301  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
302  }
303 
304  /* Verify plugin type */
305  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
306  cpl_msg_error(cpl_func, "Plugin is not a recipe");
307  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
308  }
309 
310  /* Get the recipe */
311  recipe = (cpl_recipe *)plugin;
312 
313  cpl_parameterlist_delete(recipe->parameters);
314 
315  return 0;
316 }
317 
318 /*----------------------------------------------------------------------------*/
325 /*----------------------------------------------------------------------------*/
326 static int midi_profile(cpl_frameset * frameset,
327  const cpl_parameterlist * parlist)
328 {
329  cpl_propertylist * plist=NULL;
330  cpl_propertylist * pro_list=NULL;
331 
332  cpl_propertylist * qclist_all_extension;
333  cpl_propertylist **pHeader_ext= NULL;
334 
335  const cpl_parameter * param;
336 
337  cpl_frame * cur_frame=NULL;
338 
339  cpl_table * table=NULL;
340  cpl_table * profile_table=NULL;
341  cpl_table ** pTable_ext= NULL;
342 
343  cpl_imagelist * imglst_AOPEN_DATA_S[4];
344  cpl_imagelist * imglst_AOPEN_DATA_T[4];
345  cpl_imagelist * imglst_BOPEN_DATA_S[4];
346  cpl_imagelist * imglst_BOPEN_DATA_T[4];
347 
348 
349  cpl_image * image_AOPEN_DATA_T[4];
350  cpl_image * image_BOPEN_DATA_T[4];
351  cpl_image * image_AOPEN_DATA_T_float[4];
352  cpl_image * image_BOPEN_DATA_T_float[4];
353 
354  cpl_mask * image_AOPEN_DATA_T_mask[4];
355  cpl_mask * image_BOPEN_DATA_T_mask[4];
356 
357  cpl_image * dummy_image;
358 
359  cpl_errorstate prestate = cpl_errorstate_get();
360 
361  char * tag=NULL;
362  char * dataname=NULL;
363  char * dataname1=NULL;
364  char gris_name[100]= "";
365  char * first_valid_frame=NULL;
366 
367 
368  const char * shutter_id =NULL;
369 
370  int i=0;
371  int ext_imaging_data=0;
372  int ext_imaging_detector=0;
373  int isPHOTAdata=0;
374  int isPHOTBdata=0;
375  int isSP=0;
376  int isHS=0;
377  int dimenDATA=0;
378  int extnum=1;
379  int isFirst=0;
380 
381  double std_AOPEN [4];
382  double std_BOPEN[4];
383  double threshold=0.;
384 
385  int maxloop=0;
386  int image_size_y=0;
387 
388  const int x_pos_grism_start[]={38, 52, 116, 176, 217};
389  const int x_pos_grism_end[] ={51, 69, 137, 201, 246};
390 
391  const int x_pos_prism_start[]={120, 113, 88, 60, 36};
392  const int x_pos_prism_end[] ={127, 122, 99, 73, 51};
393 
394  param = cpl_parameterlist_find_const(parlist,
395  "midi.midi_profile.threshold");
396  threshold = cpl_parameter_get_double(param);
397 
398  /* Check if SOF is empty */
399  cur_frame = cpl_frameset_get_first(frameset);
400  if (cur_frame == NULL) {
401  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
402  "SOF does not have any file");
403  }
404 
405 
406  pTable_ext = cpl_malloc((extnum) * sizeof(cpl_table *));
407  pHeader_ext = cpl_malloc((extnum) * sizeof(cpl_propertylist *));
408 
409 
410  /* Identify the RAW and CALIB frames in the input frameset */
411  cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
412  cpl_error_get_code());
413 
414  /* Check if SCI_PHOT or HIGH_SENS */
415  cur_frame = cpl_frameset_get_first(frameset);
416 
417  while(cur_frame)
418  {
419  /* Check the right tags */
420  tag = (char*)cpl_frame_get_tag(cur_frame);
421  if (strcmp(tag,MIDI_PHOTOM_SP_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_SP_SCIENCE)==0) {
422  isSP=1;
423  dimenDATA=4;
424  }
425  if (strcmp(tag,MIDI_PHOTOM_HS_CALIB) ==0 || strcmp(tag,MIDI_PHOTOM_HS_SCIENCE)==0) {
426  isHS=1;
427  dimenDATA=2;
428  }
429  /* cpl_msg_info(cpl_func,"tag: %s SP: %d HS:%d ",tag,isSP,isHS); */
430 
431  /* Get next frame from SOF */
432  cur_frame = cpl_frameset_get_next( frameset );
433 
434 
435 
436  }
437 
438  if (isHS>0 && isSP>0) {
439  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
440  "SOF has mixed data: SCI_PHOT and HIGH_SENS. This is not allowed!");
441  }
442 
443 
444  for (i=0; i<dimenDATA;i++){
445 
446  imglst_AOPEN_DATA_S[i]=cpl_imagelist_new();
447  imglst_AOPEN_DATA_T[i]=cpl_imagelist_new();
448 
449  imglst_BOPEN_DATA_S[i]=cpl_imagelist_new();
450  imglst_BOPEN_DATA_T[i]=cpl_imagelist_new();
451 
452  }
453 
454 
455 
456 /* Walk through the whole Set of Frames SOF */
457 
458 
459 /* Start 1 */
460 
461  cur_frame = cpl_frameset_get_first(frameset);
462  if (cur_frame == NULL) {
463  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
464  "SOF does not have any file");
465  }
466  /* Append the images from the tables to the various imagelists */
467  while(cur_frame)
468  {
469  /* Check the right tags */
470  tag = (char*)cpl_frame_get_tag(cur_frame);
471  if (strcmp(tag, MIDI_PHOTOM_SP_SCIENCE) && strcmp(tag, MIDI_PHOTOM_SP_CALIB) &&
472  strcmp(tag, MIDI_PHOTOM_HS_SCIENCE) && strcmp(tag, MIDI_PHOTOM_HS_CALIB)) {
473  cur_frame = cpl_frameset_get_next( frameset );
474  continue;
475  }
476 
477  cpl_msg_info(cpl_func, "Processing file %s",cpl_frame_get_filename(cur_frame));
478  ext_imaging_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DATA");
479  ext_imaging_detector=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"IMAGING_DETECTOR");
480 
481 
482  /* Read the IMAGING_DETECTOR extension from the first valid image */
483 
484  if(isFirst<1){
485  isFirst=1;
486 
487  /* Save the name of the first valid frame */
488  first_valid_frame=cpl_sprintf(cpl_frame_get_filename(cur_frame));
489 
490  for (i=0; i<extnum; i++)
491  {
492  pHeader_ext[i] = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector );
493  pTable_ext[i] = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_detector, 1 );
494 
495  if (pHeader_ext[i] ==NULL || pTable_ext[i]==NULL)
496  {
497  cpl_msg_error(cpl_func,"Error loading the extended header or table of %s",cpl_frame_get_filename(cur_frame));
498 
499  for (i=0; i<extnum; i++)
500  {
501  cpl_propertylist_delete(pHeader_ext[i]);
502  cpl_table_delete(pTable_ext[i]);
503  }
504  cpl_free (pHeader_ext);
505  cpl_free (pTable_ext);
506 
507 
508 
509  return -1;
510  }
511  }
512  }
513 
514 
515 /* Check if the file is AOPEN or BOPEN */
516 
517  plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
518  if (cpl_propertylist_has(plist, "ESO INS SHUT ID") == 1)
519  {
520  shutter_id=(cpl_propertylist_get_string(plist, "ESO INS SHUT ID"));
521  }
522 
523 /* Checking if PRISM or GRISM */
524  if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
525  {
526  strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
527  }
528 
529  if (!cpl_errorstate_is_equal(prestate)) {
530  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "Could not ...");
531  }
532 
533  if (strcmp(shutter_id,"AOPEN")==0)
534  {
535 /* PhotA Data found */
536  isPHOTAdata=1;
537 
538  /* Load extension Imaging Data */
539  table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
540  if (table == NULL) {
541  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
542  "Could not load the table");
543  }
544  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
545 
546 
547  for (i=0; i<dimenDATA;i++){
548 
549  dataname=cpl_sprintf("DATA%d",i+1);
550 
551  /* cpl_msg_info(cpl_func, "Scanning for dataname ..."); */
552  if (cpl_table_has_column(table,dataname)){
553  table_to_imglst_sky_target(dataname,"TARTYP2",imglst_AOPEN_DATA_S[i],imglst_AOPEN_DATA_T[i],table);
554  }
555 
556  cpl_msg_info(cpl_func, "Number of so far processed AOPEN %s patches: % " CPL_SIZE_FORMAT "",dataname,cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i]));
557  cpl_free(dataname);
558  }
559 
560  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
561 
562  cpl_table_delete(table);
563  }
564 
565  if (strcmp(shutter_id,"BOPEN")==0)
566  {
567 /* PhotB Data found */
568  isPHOTBdata=1;
569 
570  /* Load extension Imaging Data */
571  table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_imaging_data, 1);
572  if (table == NULL) {
573  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
574  "Could not load the table");
575  }
576  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
577 
578 
579  for (i=0; i<dimenDATA;i++){
580 
581  dataname=cpl_sprintf("DATA%d",i+1);
582  /* cpl_msg_info(cpl_func, "Scanning for dataname ..."); */
583  if (cpl_table_has_column(table,dataname)){
584  table_to_imglst_sky_target(dataname,"TARTYP2",imglst_BOPEN_DATA_S[i],imglst_BOPEN_DATA_T[i],table);
585  }
586 
587  cpl_msg_info(cpl_func, "Number of so far processed BOPEN %s patches: % " CPL_SIZE_FORMAT "",dataname,cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i]));
588  cpl_free(dataname);
589  }
590 
591  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
592 
593  cpl_table_delete(table);
594  }
595 
596 
597  cpl_propertylist_delete(plist);
598 
599  /* Get next frame from SOF */
600  cur_frame = cpl_frameset_get_next( frameset );
601 
602  } /* while more frames */
603 
604 
605  if (isPHOTAdata==0 || isPHOTBdata==0)
606  {
607  cpl_msg_error(cpl_func, "No suitable SetOfFrame fround");
608  (void)cpl_error_set_where(cpl_func);
609  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(), "No suitable SetOfFrame fround");
610 
611  }
612 
613 
614 /* /\* Save the single sky and target cubes *\/ */
615 
616 
617 /* for (i=0; i<dimenDATA;i++){ */
618 /* if (strcmp(gris_name,"GRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_GRISM"); */
619 /* if (strcmp(gris_name,"PRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_PRISM"); */
620 /* if (strcmp(gris_name,"GRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_GRISM"); */
621 /* if (strcmp(gris_name,"PRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_PRISM"); */
622 
623 
624 /* dataname1=cpl_sprintf("imglst_AOPEN_DATA%d_T.fits",i+1); */
625 /* if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_T[i], */
626 /* CPL_BPP_IEEE_FLOAT, "midi_profile", */
627 /* dataname, NULL, NULL, */
628 /* PACKAGE "/" PACKAGE_VERSION, */
629 /* dataname1)) { */
630 /* /\* Propagate the error *\/ */
631 /* (void)cpl_error_set_where(cpl_func); */
632 /* } */
633 /* cpl_free(dataname1); */
634 /* dataname1=cpl_sprintf("imglst_AOPEN_DATA%d_S.fits",i+1); */
635 /* if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_S[i], */
636 /* CPL_BPP_IEEE_FLOAT, "midi_profile", */
637 /* dataname, NULL, NULL, */
638 /* PACKAGE "/" PACKAGE_VERSION, */
639 /* dataname1)) { */
640 /* /\* Propagate the error *\/ */
641 /* (void)cpl_error_set_where(cpl_func); */
642 /* } */
643 /* cpl_free(dataname1); */
644 
645 
646 /* dataname1=cpl_sprintf("imglst_BOPEN_DATA%d_T.fits",i+1); */
647 /* if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_T[i], */
648 /* CPL_BPP_IEEE_FLOAT, "midi_profile", */
649 /* dataname, NULL, NULL, */
650 /* PACKAGE "/" PACKAGE_VERSION, */
651 /* dataname1)) { */
652 /* /\* Propagate the error *\/ */
653 /* (void)cpl_error_set_where(cpl_func); */
654 /* } */
655 /* cpl_free(dataname1); */
656 /* dataname1=cpl_sprintf("imglst_BOPEN_DATA%d_S.fits",i+1); */
657 /* if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_S[i], */
658 /* CPL_BPP_IEEE_FLOAT, "midi_profile", */
659 /* dataname, NULL, NULL, */
660 /* PACKAGE "/" PACKAGE_VERSION, */
661 /* dataname1)) { */
662 /* /\* Propagate the error *\/ */
663 /* (void)cpl_error_set_where(cpl_func); */
664 /* } */
665 /* cpl_free(dataname); */
666 
667 /* cpl_free(dataname1); */
668 
669 
670 /* } */
671 
672 
673 
674 
675 
676 
677 /* Subtract the sky frame from the target frame*/
678 
679  cpl_msg_info(cpl_func, "Doing sky subtraction ...");
680 
681 
682  for (i=0; i<dimenDATA;i++){
683 
684  if(cpl_imagelist_get_size(imglst_AOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_AOPEN_DATA_S[i])
685  && cpl_imagelist_get_size(imglst_BOPEN_DATA_T[i])==cpl_imagelist_get_size(imglst_BOPEN_DATA_S[i])
686  )
687  {
688  cpl_imagelist_subtract(imglst_AOPEN_DATA_T[i],imglst_AOPEN_DATA_S[i]);
689  cpl_imagelist_subtract(imglst_BOPEN_DATA_T[i],imglst_BOPEN_DATA_S[i]);
690 
691  }
692  else
693  {
694  cpl_msg_info(cpl_func, "The number of Skyframes and Targetframes differ");
695  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
696  "The number of Skyframes and Targetframes differ");
697  }
698 
699  }
700 
701 
702  cpl_msg_info(cpl_func, "Collapsing the images in the time domain ...");
703 
704 /* Collapse the sky-subtracted targets */
705  for (i=0; i<dimenDATA;i++){
706 
707  dummy_image=cpl_imagelist_collapse_create(imglst_AOPEN_DATA_T[i]);
708  image_AOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
709  cpl_image_delete(dummy_image);
710 
711  dummy_image=cpl_imagelist_collapse_create(imglst_BOPEN_DATA_T[i]);
712  image_BOPEN_DATA_T[i]=cpl_image_cast(dummy_image,CPL_TYPE_DOUBLE);
713  cpl_image_delete(dummy_image);
714 
715  }
716 
717 
718 /* For HIGH_SENS: stack AOPEN and BOPEN DATA1/DATA2 frames */
719 
720  if (isHS>0) {
721  cpl_image_add(image_AOPEN_DATA_T[0], image_BOPEN_DATA_T[0]);
722  cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);
723  }
724 
725 /* For SCI_PHOT: stack AOPEN and BOPEN DATA2/DATA3 frames */
726 
727  if (isSP>0) {
728  cpl_image_add(image_AOPEN_DATA_T[1], image_BOPEN_DATA_T[1]);
729  cpl_image_add(image_AOPEN_DATA_T[2], image_BOPEN_DATA_T[2]);
730  }
731 
732 
733  pro_list = cpl_propertylist_new();
734 
735 /* Save the images for diagnostics */
736 
737  if(1){
738  for (i=0; i<dimenDATA;i++){
739  if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM_SIGNAL");
740  if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM_SIGNAL");
741  if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM_SIGNAL");
742  if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM_SIGNAL");
743 
744 
745  dataname1=cpl_sprintf("image_AOPEN_DATA%d_signal.fits",i+1);
746  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_AOPEN_DATA_T[i],
747  CPL_BPP_IEEE_FLOAT, "midi_profile",
748  pro_list, NULL,
749  PACKAGE "/" PACKAGE_VERSION,
750  dataname1)) {
751  /* Propagate the error */
752  (void)cpl_error_set_where(cpl_func);
753  }
754  cpl_free(dataname1);
755 
756 
757  dataname1=cpl_sprintf("image_BOPEN_DATA%d_signal.fits",i+1);
758  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_BOPEN_DATA_T[i],
759  CPL_BPP_IEEE_FLOAT, "midi_profile",
760  pro_list, NULL,
761  PACKAGE "/" PACKAGE_VERSION,
762  dataname1)) {
763  /* Propagate the error */
764  (void)cpl_error_set_where(cpl_func);
765  }
766  cpl_free(dataname1);
767 
768  }
769  }
770 
771 
772 /* Calculate the standard deviation */
773 
774  for (i=0; i<dimenDATA;i++){
775  std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
776  std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
777  cpl_msg_info(cpl_func,"First pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
778  cpl_msg_info(cpl_func,"First pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
779 
780  }
781 
782 
783 
784 /* Create a bad pixel mask and connect it with the image*/
785  for (i=0; i<dimenDATA;i++){
786  image_AOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_AOPEN_DATA_T[i], std_AOPEN[i], FLT_MAX);
787  image_BOPEN_DATA_T_mask[i] = cpl_mask_threshold_image_create(image_BOPEN_DATA_T[i], std_BOPEN[i], FLT_MAX);
788  cpl_image_reject_from_mask(image_AOPEN_DATA_T[i], image_AOPEN_DATA_T_mask[i]) ;
789  cpl_image_reject_from_mask(image_BOPEN_DATA_T[i], image_BOPEN_DATA_T_mask[i]) ;
790 
791  }
792 
793 /* Re Calculate the standard deviation using the mask file*/
794 
795  cpl_msg_info(cpl_func," ");
796  for (i=0; i<dimenDATA;i++){
797  std_AOPEN[i]=cpl_image_get_stdev(image_AOPEN_DATA_T[i]);
798  std_BOPEN[i]=cpl_image_get_stdev(image_BOPEN_DATA_T[i]);
799  cpl_msg_info(cpl_func,"Second pass STDEV of AOPEN DATA%d: %f", i+1, std_AOPEN[i]);
800  cpl_msg_info(cpl_func,"Second pass STDEV of BOPEN DATA%d: %f", i+1, std_BOPEN[i]);
801  }
802 
803 /* un-connect the mask and free the memory*/
804  for (i=0; i<dimenDATA;i++){
805  cpl_image_accept_all(image_AOPEN_DATA_T[i]) ;
806  cpl_image_accept_all(image_BOPEN_DATA_T[i]) ;
807 
808  cpl_mask_delete(image_AOPEN_DATA_T_mask[i]);
809  cpl_mask_delete(image_BOPEN_DATA_T_mask[i]);
810  }
811 
812 
813 
814 
815 
816 /* Threshold the image */
817 
818  for (i=0; i<dimenDATA;i++){
819  cpl_image_threshold(image_AOPEN_DATA_T[i],threshold*std_AOPEN[i],FLT_MAX,0.,FLT_MAX);
820  cpl_image_threshold(image_BOPEN_DATA_T[i],threshold*std_BOPEN[i],FLT_MAX,0.,FLT_MAX);
821 
822  }
823 
824 
825 /* Normalize the profile */
826 
827  for (i=0; i<dimenDATA;i++){
828  midi_normalize_image_y(image_AOPEN_DATA_T[i]);
829  midi_normalize_image_y(image_BOPEN_DATA_T[i]);
830 
831  }
832 
833 /* Cast images do float as the midi core library expects a float profile*/
834 /* ONLY THE TABLE IS SAVED AS FLOAT !!!! */
835 
836  for (i=0; i<dimenDATA;i++){
837  image_AOPEN_DATA_T_float[i]=cpl_image_cast(image_AOPEN_DATA_T[i],CPL_TYPE_FLOAT);
838  image_BOPEN_DATA_T_float[i]=cpl_image_cast(image_BOPEN_DATA_T[i],CPL_TYPE_FLOAT);
839 
840  }
841 
842 /* Derive QC parameters */
843 
844  cpl_msg_info(cpl_func, "Deriving QC parameters ...");
845  image_size_y=cpl_image_get_size_y(image_AOPEN_DATA_T[0]);
846 
847  if (isHS>0) {
848  maxloop=2;
849  }
850  else {
851  maxloop=4;
852  }
853 
854  /* Compute the barycenter in y and the maximum... */
855  for (i = 0; i < maxloop; i++){
856 
857  if (strcmp(gris_name,"GRISM")==0)
858  {
859  if(isSP>0 && i==0){
860  /* Take data from BOBEN image as AOPEN has no flux */
861  midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
862  x_pos_grism_start,x_pos_grism_end);
863  midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
864  x_pos_grism_start,x_pos_grism_end);
865  }
866  else{
867  midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
868  x_pos_grism_start,x_pos_grism_end);
869  midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
870  x_pos_grism_start,x_pos_grism_end);
871  }
872  }
873 
874  if (strcmp(gris_name,"PRISM")==0)
875  {
876  if(isSP>0 && i==0){
877  /* Take data from BOBEN image as AOPEN has no flux */
878  midi_profile_qc_barycenter(pro_list, i, image_BOPEN_DATA_T[i],
879  x_pos_prism_start,x_pos_prism_end);
880  midi_profile_qc_maximum(pro_list, i, image_BOPEN_DATA_T[i],
881  x_pos_prism_start,x_pos_prism_end);
882  }
883  else{
884  midi_profile_qc_barycenter(pro_list, i, image_AOPEN_DATA_T[i],
885  x_pos_prism_start,x_pos_prism_end);
886  midi_profile_qc_maximum(pro_list, i, image_AOPEN_DATA_T[i],
887  x_pos_prism_start,x_pos_prism_end);
888  }
889  }
890  }
891 
892 
893 
894 /* Save the result */
895 
896 
897 /* Save it to a fits table */
898 
899  profile_table=cpl_table_new(1);
900 
901 /* Propertylist to write to the extension */
902  qclist_all_extension = cpl_propertylist_new();
903  cpl_propertylist_append_string (qclist_all_extension, "EXTNAME", "IMAGING_DATA");
904 
905  if (isHS>0) {
906  append_image_to_table(profile_table,"DATA1",image_AOPEN_DATA_T_float[0],0);
907  append_image_to_table(profile_table,"DATA2",image_AOPEN_DATA_T_float[1],0);
908  }
909 
910  if (isSP>0) {
911  append_image_to_table(profile_table,"DATA1",image_BOPEN_DATA_T_float[0],0);
912  append_image_to_table(profile_table,"DATA2",image_AOPEN_DATA_T_float[1],0);
913  append_image_to_table(profile_table,"DATA3",image_AOPEN_DATA_T_float[2],0);
914  append_image_to_table(profile_table,"DATA4",image_AOPEN_DATA_T_float[3],0);
915  }
916 
917 
918  if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM");
919  if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM");
920  if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM");
921  if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM");
922 
923  if (strcmp(gris_name,"GRISM")==0)
924  {
925  cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table,
926  qclist_all_extension, "midi_profile",
927  pro_list, NULL,
928  PACKAGE "/" PACKAGE_VERSION,
929  "midi_profile_grism.fits");
930  cpl_table_delete(profile_table);
931 
932  /* Append the IMAGING_DETECTOR table from the first valid image */
933  /* midi_copy_extension() is a workaround to cpl_table_save() as
934  * the latter is modifiing the content during saving */
935 
936  midi_copy_extension(first_valid_frame, "midi_profile_grism.fits", "IMAGING_DETECTOR");
937 
938 /* if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0], NULL, pHeader_ext[0], "midi_profile_grism.fits", CPL_IO_EXTEND )) */
939 /* { */
940 /* cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where()); */
941 /* } */
942  }
943 
944  if (strcmp(gris_name,"PRISM")==0)
945  {
946  cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, profile_table,
947  qclist_all_extension, "midi_profile",
948  pro_list, NULL,
949  PACKAGE "/" PACKAGE_VERSION,
950  "midi_profile_prism.fits");
951  cpl_table_delete(profile_table);
952 
953  /* Append the IMAGING_DETECTOR table from the first valid image */
954  /* midi_copy_extension() is a workaround to cpl_table_save() as
955  * the latter is modifiing the content during saving */
956 
957  midi_copy_extension(first_valid_frame, "midi_profile_prism.fits", "IMAGING_DETECTOR");
958 
959 /* if (CPL_ERROR_NONE != cpl_table_save(pTable_ext[0], NULL, pHeader_ext[0], "midi_profile_prism.fits", CPL_IO_EXTEND )) */
960 /* { */
961 /* cpl_msg_error(cpl_func,"Error when saving Extension: %s %s",cpl_error_get_message(),cpl_error_get_where()); */
962 /* } */
963  }
964 
965 
966  cpl_propertylist_delete(qclist_all_extension);
967 /* cpl_free(dataname); */
968 
969  for (i=0; i<extnum; i++)
970  {
971  cpl_propertylist_delete(pHeader_ext[i]);
972  cpl_table_delete(pTable_ext[i]);
973  }
974  cpl_free (pHeader_ext);
975  cpl_free (pTable_ext);
976  cpl_free(first_valid_frame);
977 
978 
979 /* Save the images for diagnostics */
980  if(1){
981  for (i=0; i<dimenDATA;i++){
982  if (strcmp(gris_name,"GRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_GRISM_PROFILE");
983  if (strcmp(gris_name,"PRISM")==0 && isSP>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_SP_MASK_PRISM_PROFILE");
984  if (strcmp(gris_name,"GRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_GRISM_PROFILE");
985  if (strcmp(gris_name,"PRISM")==0 && isHS>0) cpl_propertylist_update_string(pro_list, CPL_DFS_PRO_CATG, "KAPPA_HS_MASK_PRISM_PROFILE");
986 
987 
988  dataname1=cpl_sprintf("image_AOPEN_DATA%d_profile.fits",i+1);
989  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_AOPEN_DATA_T[i],
990  CPL_BPP_IEEE_FLOAT, "midi_profile",
991  pro_list, NULL,
992  PACKAGE "/" PACKAGE_VERSION,
993  dataname1)) {
994  /* Propagate the error */
995  (void)cpl_error_set_where(cpl_func);
996  }
997  cpl_free(dataname1);
998 
999 
1000  dataname1=cpl_sprintf("image_BOPEN_DATA%d_profile.fits",i+1);
1001  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_BOPEN_DATA_T[i],
1002  CPL_BPP_IEEE_FLOAT, "midi_profile",
1003  pro_list, NULL,
1004  PACKAGE "/" PACKAGE_VERSION,
1005  dataname1)) {
1006  /* Propagate the error */
1007  (void)cpl_error_set_where(cpl_func);
1008  }
1009 /* cpl_free(dataname); */
1010  cpl_free(dataname1);
1011 
1012  }
1013  }
1014 
1015 
1016 /* Free the memory */
1017 /* cpl_propertylist_delete(qclist_all_extension); */
1018  cpl_propertylist_delete(pro_list);
1019 
1020 
1021  for (i=0; i<dimenDATA;i++){
1022  cpl_image_delete(image_AOPEN_DATA_T[i]);
1023  cpl_image_delete(image_BOPEN_DATA_T[i]);
1024  cpl_image_delete(image_AOPEN_DATA_T_float[i]);
1025  cpl_image_delete(image_BOPEN_DATA_T_float[i]);
1026  }
1027 
1028 
1029 
1030 
1031 
1032 
1033 /* /\* Save the image lists for diagnostics *\/ */
1034 /* if(0){ */
1035 /* for (i=0; i<dimenDATA;i++){ */
1036 /* if (strcmp(gris_name,"GRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_GRISM_DIAGNOSTIC"); */
1037 /* if (strcmp(gris_name,"PRISM")==0 && isSP>0) dataname=cpl_sprintf("KAPPA_SP_MASK_PRISM_DIAGNOSTIC"); */
1038 /* if (strcmp(gris_name,"GRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_GRISM_DIAGNOSTIC"); */
1039 /* if (strcmp(gris_name,"PRISM")==0 && isHS>0) dataname=cpl_sprintf("KAPPA_HS_MASK_PRISM_DIAGNOSTIC"); */
1040 
1041 
1042 /* dataname1=cpl_sprintf("imglst_AOPEN_DATA%d.fits",i+1); */
1043 /* if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_AOPEN_DATA_T[i], */
1044 /* CPL_BPP_IEEE_FLOAT, "midi_profile", */
1045 /* dataname, NULL, NULL, */
1046 /* PACKAGE "/" PACKAGE_VERSION, */
1047 /* dataname1)) { */
1048 /* /\* Propagate the error *\/ */
1049 /* (void)cpl_error_set_where(cpl_func); */
1050 /* } */
1051 /* cpl_free(dataname1); */
1052 
1053 
1054 /* dataname1=cpl_sprintf("imglst_BOPEN_DATA%d.fits",i+1); */
1055 /* if (cpl_dfs_save_imagelist(frameset, parlist, frameset, imglst_BOPEN_DATA_T[i], */
1056 /* CPL_BPP_IEEE_FLOAT, "midi_profile", */
1057 /* dataname, NULL, NULL, */
1058 /* PACKAGE "/" PACKAGE_VERSION, */
1059 /* dataname1)) { */
1060 /* /\* Propagate the error *\/ */
1061 /* (void)cpl_error_set_where(cpl_func); */
1062 /* } */
1063 /* cpl_free(dataname); */
1064 /* cpl_free(dataname1); */
1065 
1066 
1067 
1068 
1069 /* } */
1070 
1071 /* } */
1072 
1073 
1074 /* Free the memory */
1075  for (i=0; i<dimenDATA;i++){
1076  cpl_imagelist_delete(imglst_AOPEN_DATA_T[i]);
1077  cpl_imagelist_delete(imglst_AOPEN_DATA_S[i]);
1078 
1079  cpl_imagelist_delete(imglst_BOPEN_DATA_T[i]);
1080  cpl_imagelist_delete(imglst_BOPEN_DATA_S[i]);
1081 
1082  }
1083 
1084 
1085 
1086 
1087  /* Free memory */
1088 
1089 
1090  return (int)cpl_error_get_code();
1091 }
1092 
1093 
1094 
1095 static int table_to_imglst_sky_target(const char * columname,
1096  const char * columntype,
1097  cpl_imagelist * imglst_sky,
1098  cpl_imagelist * imglst_target,
1099  cpl_table * table)
1100 {
1101 
1102  int dimenDATA;
1103  int i,csky=0, ctarget=0,csky_tmp=0, ctarget_tmp=0,first_unknown=0;
1104  cpl_array * array_data=NULL;
1105  cpl_image * image_data_int=NULL;
1106  cpl_errorstate prestate = cpl_errorstate_get();
1107  char ** target_type;
1108  cpl_type ctype;
1109  int flag_issky=0;
1110  int flag_istarget=0;
1111 
1112  cpl_imagelist * imglst_sky_tmp=NULL;
1113  cpl_imagelist * imglst_target_tmp=NULL;
1114 
1115  imglst_sky_tmp=cpl_imagelist_new();
1116  imglst_target_tmp=cpl_imagelist_new();
1117 
1118  /* Load extension Imaging Data */
1119 
1120  dimenDATA=cpl_table_get_column_dimensions(table, columname);
1121  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
1122  if (dimenDATA != 2) {
1123  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1124  "DATA has a wrong dimension");
1125  }
1126  /* Read target type T(arget),S(ky),B(ackground) and store them in the image list */
1127  /* Loop over all "images" stored in DATA and add them to the imagelist */
1128 
1129 
1130  if (cpl_table_has_column(table, columntype))
1131  {
1132  target_type=cpl_table_get_data_string(table, columntype);
1133 
1134  }
1135  else
1136  {
1137  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
1138  "TYPE of the Column not found");
1139  }
1140 /* cpl_msg_info(cpl_func, "Number of images in the Table: %d",cpl_table_get_nrow(table)); */
1141 /* cpl_msg_info(cpl_func, "Extracting %d images: please wait ...",cpl_table_get_nrow(table)); */
1142 
1143 
1144  ctype=cpl_table_get_column_type(table, columname);
1145 /* cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
1146 
1147 
1148 
1149  csky=cpl_imagelist_get_size(imglst_sky);
1150 
1151  ctarget=cpl_imagelist_get_size(imglst_target);
1152 
1153 /* cpl_msg_info(cpl_func, "Length of imagelist S T : %d %d",csky,ctarget ); */
1154 
1155 
1156 
1157  flag_issky=0;
1158  flag_istarget=0;
1159 
1160 /* Find the first "U" image and discard the images befor */
1161 
1162  for (i=0; i<cpl_table_get_nrow(table);i++){
1163  if(strcmp(target_type[i],"U")== 0){
1164  first_unknown=i;
1165  break;
1166  }
1167  }
1168 
1169 /* cpl_msg_info(cpl_func, "First unknown: %d",first_unknown); */
1170 
1171  for (i=first_unknown; i<cpl_table_get_nrow(table);i++){
1172 
1173  array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
1174  if(ctype&CPL_TYPE_INT){
1175  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) );
1176  }
1177  if(ctype&CPL_TYPE_FLOAT){
1178  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) );
1179  }
1180  /* Cast the image to float */
1181 /* image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
1182 
1183 /* Append the image to the imagelists */
1184 
1185  if(strcmp(target_type[i],"S")== 0){
1186  cpl_imagelist_set(imglst_sky_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),csky_tmp++);
1187  flag_issky=1;
1188  cpl_image_unwrap(image_data_int);
1189  continue;
1190  }
1191  if(strcmp(target_type[i],"T")== 0){
1192  cpl_imagelist_set(imglst_target_tmp,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),ctarget_tmp++);
1193  flag_istarget=1;
1194  cpl_image_unwrap(image_data_int);
1195  continue;
1196  }
1197 
1198  /*Unwrap processed image manualy if nor Target nor Sky*/
1199  if(image_data_int!=NULL){
1200  cpl_image_unwrap(image_data_int);
1201  }
1202  if(flag_issky >0 && flag_istarget >0){
1203  /*Collapse the temporary imagelist and add the image to the "final" imagelist */
1204  cpl_imagelist_set(imglst_sky,cpl_imagelist_collapse_create(imglst_sky_tmp),csky++);
1205  cpl_imagelist_set(imglst_target,cpl_imagelist_collapse_create(imglst_target_tmp),ctarget++);
1206  csky_tmp=0;
1207  ctarget_tmp=0;
1208  flag_issky=0;
1209  flag_istarget=0;
1210 
1211  /*Empty imagelist and free the image buffer*/
1212  while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
1213  cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
1214  }
1215  while(cpl_imagelist_get_size(imglst_target_tmp)>0){
1216  cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
1217  }
1218 
1219  }
1220 
1221  }
1222 
1223 
1224 /* Empty imagelist and free the image buffer*/
1225  while(cpl_imagelist_get_size(imglst_sky_tmp)>0){
1226  cpl_image_delete(cpl_imagelist_unset(imglst_sky_tmp,0));
1227  }
1228  while(cpl_imagelist_get_size(imglst_target_tmp)>0){
1229  cpl_image_delete(cpl_imagelist_unset(imglst_target_tmp,0));
1230  }
1231 
1232  cpl_imagelist_delete(imglst_sky_tmp);
1233  cpl_imagelist_delete(imglst_target_tmp);
1234 
1235 
1236 
1237  return (int)cpl_error_get_code();
1238 }
1239 
1240 
1241 /*----------------------------------------------------------------------------*/
1247 /*----------------------------------------------------------------------------*/
1248 
1249 int midi_normalize_image_y(cpl_image * image)
1250  {
1251 
1252  int nx_image=0;
1253  int ny_image=0;
1254  int xpos=0;
1255  int ypos=0;
1256 
1257  double * pimage=NULL;
1258  double * pimage_norm=NULL;
1259 
1260  cpl_image * image_norm;
1261 
1262  cpl_msg_info(cpl_func, "Normalizing the integral of the profile to unity ...");
1263 
1264  image_norm=cpl_image_collapse_create(image,0);
1265 
1266  nx_image=cpl_image_get_size_x(image);
1267  ny_image=cpl_image_get_size_y(image);
1268 
1269  pimage = cpl_image_get_data_double(image);
1270  pimage_norm= cpl_image_get_data_double(image_norm);
1271 
1272 
1273  for (xpos=0; xpos<nx_image; ++xpos)
1274  {
1275  for(ypos=0; ypos<ny_image;++ypos)
1276  {
1277  if (pimage_norm[xpos]>0) pimage[xpos+nx_image*ypos] /= pimage_norm[xpos];
1278  if (pimage_norm[xpos]<=0) pimage[xpos+nx_image*ypos] = 0.;
1279  }
1280  }
1281 
1282 
1283  cpl_image_delete(image_norm);
1284 
1285  return 0;
1286 }
1287 
1288 /*----------------------------------------------------------------------------*/
1297 /*----------------------------------------------------------------------------*/
1298 
1299 static int append_image_to_table(cpl_table * table, const char * columname, cpl_image * image, int row)
1300 {
1301 
1302  cpl_array * array_dimension=NULL;
1303  cpl_array * array_dummy=NULL;
1304 
1305  array_dimension=cpl_array_new(2,CPL_TYPE_INT);
1306  cpl_array_set(array_dimension, 0,cpl_image_get_size_x(image));
1307  cpl_array_set(array_dimension, 1,cpl_image_get_size_y(image));
1308 
1309  cpl_table_new_column_array(table, columname, CPL_TYPE_FLOAT, cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
1310  cpl_table_set_column_dimensions(table,columname,array_dimension);
1311  array_dummy = cpl_array_wrap_float(cpl_image_get_data_float(image), cpl_image_get_size_x(image)*cpl_image_get_size_y(image));
1312  cpl_table_set_array(table, columname, row, array_dummy);
1313  cpl_array_unwrap(array_dummy);
1314 
1315 
1316  cpl_array_delete(array_dimension);
1317 
1318  return 0;
1319 }
1320 
1321 /*----------------------------------------------------------------------------*/
1329 /*----------------------------------------------------------------------------*/
1330 
1331 static int midi_copy_extension(const char * infile , const char * outfile, const char * extension_name)
1332 {
1333  fitsfile * fptrin=NULL;
1334  fitsfile * fptrout=NULL;
1335  int status=0;
1336 
1337  fits_open_diskfile(&fptrin, infile, READONLY, &status);
1338  fits_open_diskfile(&fptrout, outfile, READWRITE, &status);
1339  fits_movnam_hdu(fptrin, ANY_HDU, (char *)extension_name, 0, &status);
1340  fits_copy_hdu(fptrin, fptrout, 0, &status);
1341  fits_close_file(fptrin, &status);
1342  fits_close_file(fptrout, &status);
1343 
1344  if (status != 0){
1345  cpl_msg_error(cpl_func,"A problem occured while copying the EXTENSION: %s", extension_name);
1346  return 1;
1347  }
1348  else{
1349  return 0;
1350  }
1351 }
1352 
1353 /*----------------------------------------------------------------------------*/
1364 /*----------------------------------------------------------------------------*/
1365 static void midi_profile_qc_barycenter(cpl_propertylist * pro_list, int index,
1366  cpl_image * image, const int * x_start, const int * x_end)
1367 {
1368  char * qcname=NULL;
1369  int i=0;
1370  int image_size_y=0;
1371 
1372  image_size_y=cpl_image_get_size_y(image);
1373  for (i = 0; i < 5; i++){
1374 
1375  qcname=cpl_sprintf("ESO QC Y%d CENT BINNED%d",index+1,i+1);
1376  cpl_propertylist_update_double(pro_list, qcname,
1377  cpl_image_get_centroid_y_window(image, x_start[i], 1,
1378  x_end[i], image_size_y));
1379  cpl_free(qcname);
1380  }
1381  return;
1382 }
1383 
1384 /*----------------------------------------------------------------------------*/
1395 /*----------------------------------------------------------------------------*/
1396 static void midi_profile_qc_maximum(cpl_propertylist * pro_list, int index,
1397  cpl_image * image, const int * x_start, const int * x_end)
1398 {
1399  char * qcname=NULL;
1400  char * qcname_collapsed=NULL;
1401  int i=0;
1402  int image_size_y=0;
1403  cpl_image * image_collapsed=NULL;
1404 
1405  image_size_y=cpl_image_get_size_y(image);
1406 
1407  for (i = 0; i < 5; i++){
1408  image_collapsed=cpl_image_collapse_window_create(image, x_start[i], 1,
1409  x_end[i], image_size_y,1);
1410  qcname=cpl_sprintf("ESO QC Y%d MAX BIN%d",index+1,i+1);
1411  qcname_collapsed=cpl_sprintf("ESO QC Y%d MAXSUM BIN%d",index+1,i+1);
1412 
1413  cpl_propertylist_update_double(pro_list, qcname,
1414  cpl_image_get_max_window(image, x_start[i], 1,
1415  x_end[i], image_size_y));
1416  cpl_propertylist_update_double(pro_list, qcname_collapsed,
1417  cpl_image_get_max_window(image_collapsed, 1, 1,
1418  1, image_size_y));
1419 
1420 
1421  cpl_free(qcname);
1422  cpl_free(qcname_collapsed);
1423  cpl_image_delete(image_collapsed);
1424  }
1425  return;
1426 }
1427