GRAVI Pipeline Reference Manual  0.6.3
gravi_all_viscal.c
1 /* $Id: gravi_all_viscal.c,v 1.29 2011/12/3 09:16:12 nazouaoui Exp $
2  *
3  * This file is part of the GRAVI 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: nazouaoui $
23  * $Date: 2011/12/3 09:16:12 $
24  * $Revision: 1.29 $
25  * $Name: $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <cpl.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <math.h>
40 #include "gravi_utils.h"
41 #include "gravi_pfits.h"
42 #include "gravi_dfs.h"
43 #include "gravi_calib.h"
44 #include "gravi_vis.h"
45 #include "gravi_data.h"
46 
47 
48 
49 /*-----------------------------------------------------------------------------
50  Private function prototypes
51  -----------------------------------------------------------------------------*/
52 
53 static int gravi_all_viscal_create(cpl_plugin *);
54 static int gravi_all_viscal_exec(cpl_plugin *);
55 static int gravi_all_viscal_destroy(cpl_plugin *);
56 static int gravi_all_viscal(cpl_frameset *, const cpl_parameterlist *);
57 
58 /*-----------------------------------------------------------------------------
59  Static variables
60  -----------------------------------------------------------------------------*/
61 
62 static char gravi_all_viscal_description[] =
63 "This recipe calibrate the visibilities acquired on sicence target using \n"
64 "visibilities acquired on calibrator target. It is used in single mode.\n"
65 "The insput file tags are " VIS_CALIB " and " VIS_SCIENCE "\n"
66 "The output FITS file is an OI_FITS with tag " VIS_SINGLE_CALIBRATED ". It contains , \n"
67 "the values of the visibility complex, squared visibility and the cloture phase.\n"
68 "\n"
69 "Additionally, it should describe functionality of the expected output."
70 "\n";
71 
72 /*-----------------------------------------------------------------------------
73  Function code
74  -----------------------------------------------------------------------------*/
75 
76 /*----------------------------------------------------------------------------*/
86 /*----------------------------------------------------------------------------*/
87 int cpl_plugin_get_info(cpl_pluginlist * list)
88 {
89  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
90  cpl_plugin * plugin = &recipe->interface;
91 
92  if (cpl_plugin_init(plugin,
93  CPL_PLUGIN_API,
94  GRAVI_BINARY_VERSION,
95  CPL_PLUGIN_TYPE_RECIPE,
96  "gravi_all_viscal",
97  "This recipe is used to calibrate the visibilities",
98  gravi_all_viscal_description,
99  "Firstname Lastname",
100  PACKAGE_BUGREPORT,
101  gravi_get_license(),
102  gravi_all_viscal_create,
103  gravi_all_viscal_exec,
104  gravi_all_viscal_destroy)) {
105  cpl_msg_error(cpl_func, "Plugin initialization failed");
106  (void)cpl_error_set_where(cpl_func);
107  return 1;
108  }
109 
110  if (cpl_pluginlist_append(list, plugin)) {
111  cpl_msg_error(cpl_func, "Error adding plugin to list");
112  (void)cpl_error_set_where(cpl_func);
113  return 1;
114  }
115 
116  return 0;
117 }
118 
119 /*----------------------------------------------------------------------------*/
127 /*----------------------------------------------------------------------------*/
128 static int gravi_all_viscal_create(cpl_plugin * plugin)
129 {
130  cpl_recipe * recipe;
131 
132  /* Do not create the recipe if an error code is already set */
133  if (cpl_error_get_code() != CPL_ERROR_NONE) {
134  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
135  cpl_func, __LINE__, cpl_error_get_where());
136  return (int)cpl_error_get_code();
137  }
138 
139  if (plugin == NULL) {
140  cpl_msg_error(cpl_func, "Null plugin");
141  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
142  }
143 
144  /* Verify plugin type */
145  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
146  cpl_msg_error(cpl_func, "Plugin is not a recipe");
147  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
148  }
149 
150  /* Get the recipe */
151  recipe = (cpl_recipe *)plugin;
152 
153  /* Create the parameters list in the cpl_recipe object */
154  recipe->parameters = cpl_parameterlist_new();
155  if (recipe->parameters == NULL) {
156  cpl_msg_error(cpl_func, "Parameter list allocation failed");
157  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
158  }
159 
160  /* Fill the parameters list */
161 
162  return 0;
163 }
164 
165 /*----------------------------------------------------------------------------*/
171 /*----------------------------------------------------------------------------*/
172 static int gravi_all_viscal_exec(cpl_plugin * plugin)
173 {
174 
175  cpl_recipe * recipe;
176  int recipe_status;
177  cpl_errorstate initial_errorstate = cpl_errorstate_get();
178 
179  /* Return immediately if an error code is already set */
180  if (cpl_error_get_code() != CPL_ERROR_NONE) {
181  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
182  cpl_func, __LINE__, cpl_error_get_where());
183  return (int)cpl_error_get_code();
184  }
185 
186  if (plugin == NULL) {
187  cpl_msg_error(cpl_func, "Null plugin");
188  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
189  }
190 
191  /* Verify plugin type */
192  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
193  cpl_msg_error(cpl_func, "Plugin is not a recipe");
194  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
195  }
196 
197  /* Get the recipe */
198  recipe = (cpl_recipe *)plugin;
199 
200  /* Verify parameter and frame lists */
201  if (recipe->parameters == NULL) {
202  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
203  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
204  }
205  if (recipe->frames == NULL) {
206  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
207  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
208  }
209 
210  /* Invoke the recipe */
211  recipe_status = gravi_all_viscal(recipe->frames, recipe->parameters);
212 
213  /* Ensure DFS-compliance of the products */
214 
215  if (cpl_dfs_update_product_header(recipe->frames)) {
216  if (!recipe_status){
217  recipe_status = (int)cpl_error_get_code();
218  }
219  }
220 
221  if (!cpl_errorstate_is_equal(initial_errorstate)) {
222  /* Dump the error history since recipe execution start.
223  At this point the recipe cannot recover from the error */
224  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
225  }
226 
227  return recipe_status;
228 }
229 
230 /*----------------------------------------------------------------------------*/
236 /*----------------------------------------------------------------------------*/
237 static int gravi_all_viscal_destroy(cpl_plugin * plugin)
238 {
239  cpl_recipe * recipe;
240 
241  if (plugin == NULL) {
242  cpl_msg_error(cpl_func, "Null plugin");
243  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
244  }
245 
246  /* Verify plugin type */
247  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
248  cpl_msg_error(cpl_func, "Plugin is not a recipe");
249  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
250  }
251 
252  /* Get the recipe */
253  recipe = (cpl_recipe *)plugin;
254 
255  cpl_parameterlist_delete(recipe->parameters);
256 
257  return 0;
258 }
259 
260 
261 /*----------------------------------------------------------------------------*/
269 /*----------------------------------------------------------------------------*/
270 static int gravi_all_viscal(cpl_frameset * frameset,
271  const cpl_parameterlist * parlist)
272 {
273  cpl_frameset * vis_calib_frameset, * vis_sci_frameset, *used_frame;
274  cpl_propertylist * applist, * primary_hdr;
275 
276  cpl_frame * frame;
277  const char * filename/*, * insname*/;
278  char * tf_func;
279  gravi_data ** vis_calib, * zero_data, * tf_science;
280  gravi_data * calibrated, * vis_data;
281  int nb_frame_calib, nb_frame_sci, i, j;
282 
283  /* Identify the RAW and CALIB frames in the input frameset */
284 
285  cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
286  cpl_error_get_code()) ;
287 
288  /* - Extract a set of vis data frameset */
289 // cpl_frameset * frameset = cpl_frameset_duplicate(_frameset);
290  vis_calib_frameset = gravi_frameset_extract_vis_calib (frameset);
291  vis_sci_frameset = gravi_frameset_extract_vis_science (frameset);
292 
293  if (cpl_frameset_is_empty(vis_calib_frameset)) {
294  /* To use this recipe the frameset must contain at least
295  * one vis frame. */
296  cpl_frameset_delete(vis_calib_frameset);
297  cpl_frameset_delete(vis_sci_frameset);
298 
299  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
300  "No calib frame or science frame in the input ") ;
301  }
302 
303  /* Get the number of the p2vm frame contained in the frameset */
304  nb_frame_calib = cpl_frameset_get_size(vis_calib_frameset);
305  nb_frame_sci = cpl_frameset_get_size(vis_sci_frameset);
306 
307  /* Compute of the transfer function */
308 
309  vis_calib = cpl_malloc (nb_frame_calib * sizeof (gravi_data *));
310 
311 
312 
313  for (j = 0; j < nb_frame_calib; j++){
314  /* Compute the flux and visibilities for each telescope and
315  * per acquisition */
316  frame = cpl_frameset_get_position (vis_calib_frameset, j);
317  filename = cpl_frame_get_filename (frame);
318  cpl_msg_info (NULL, "Compute the transfer function %s", filename);
319  vis_data = gravi_data_load (filename);
320  vis_calib[j] = gravi_compute_tf(vis_data);
321  if (cpl_error_get_code()){
322  for (i = 0; i <= j; i++)
323  gravi_data_delete(vis_calib[i]);
324  cpl_free (vis_calib);
325  cpl_frameset_delete(vis_calib_frameset);
326  cpl_frameset_delete(vis_sci_frameset);
327 
328 
329  return (int) cpl_error_set_message(cpl_func,
330  CPL_ERROR_ILLEGAL_OUTPUT, "error while computing "
331  "the transfer function");
332  }
333 
334  char * tf_func = cpl_sprintf("tf_func_%d.fits", j);
335 
336  primary_hdr = gravi_data_get_propertylist(vis_calib[j],
337  GRAVI_PRIMARY_HDR_NAME_EXT);
338 
339  applist = gravi_propertylist_get_qc (primary_hdr);
340 
341  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
342  TF_CAL);
343  used_frame = cpl_frameset_new();
344  cpl_frameset_insert(used_frame, cpl_frame_duplicate (frame));
345 // if (gravi_data_save(vis_calib[j], frameset, tf_func, parlist,
346 // vis_calib_frameset, "gravi_all_viscal", applist)
347 // != CPL_ERROR_NONE){
348  if (gravi_data_save(vis_calib[j], frameset, tf_func, parlist,
349  used_frame, frame, "gravi_all_viscal", applist)
350  != CPL_ERROR_NONE){
351  for (i = 0; i <= j; i++)
352  gravi_data_delete (vis_calib[i]);
353  cpl_free (vis_calib);
354  gravi_data_delete(vis_data);
355  cpl_frameset_delete(used_frame);
356  cpl_frameset_delete(vis_calib_frameset);
357  cpl_frameset_delete(vis_sci_frameset);
358  cpl_propertylist_delete(applist);
359  cpl_free(tf_func);
360 
361  return (int) cpl_error_set_message(cpl_func,
362  CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the transfer "
363  "function of the calibration file");
364  }
365 
366 // gravi_data_save_data(vis_calib[j], tf_func, CPL_IO_CREATE);
367  cpl_free(tf_func);
368  gravi_data_delete(vis_data);
369  cpl_propertylist_delete(applist);
370  cpl_frameset_delete (used_frame);
371  }
372  /* Compute the zero of the metrology */
373  cpl_msg_info (NULL, "Compute the zero of the metrology");
374  zero_data = gravi_compute_zp (vis_calib, nb_frame_calib);
375 
376  primary_hdr = gravi_data_get_propertylist(zero_data,
377  GRAVI_PRIMARY_HDR_NAME_EXT);
378 
379  applist = gravi_propertylist_get_qc (primary_hdr);
380 
381  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
382  ZP_CAL);
383  if (gravi_data_save(zero_data, frameset, "zero_metrology.fits", parlist,
384  vis_calib_frameset, NULL, "gravi_all_viscal", applist)
385  != CPL_ERROR_NONE){
386  gravi_data_delete (zero_data);
387  for (j = 0; j < nb_frame_calib; j++)
388  gravi_data_delete (vis_calib[j]);
389  cpl_free (vis_calib);
390  gravi_data_delete(vis_data);
391  cpl_frameset_delete(vis_calib_frameset);
392  cpl_frameset_delete(vis_sci_frameset);
393  cpl_propertylist_delete(applist);
394  cpl_free(tf_func);
395 
396  return (int) cpl_error_set_message(cpl_func,
397  CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the zero "
398  "of the metrology file");
399  }
400  cpl_propertylist_delete(applist);
401 
402  /* For each the calibrated visibility from science target data and
403  * the previously evaluated instrumental visibility */
404 
405  used_frame = cpl_frameset_duplicate (vis_calib_frameset);
406  if (!cpl_frameset_is_empty (vis_sci_frameset)) {
407 
408  for (i = 0; i < nb_frame_sci; i++){
409  frame = cpl_frameset_get_position (vis_sci_frameset, i);
410  filename = cpl_frame_get_filename (frame);
411  cpl_msg_info (NULL, "The calibration of the visibilities %s", filename);
412  vis_data = gravi_data_load (filename);
413  tf_science = gravi_data_duplicate (vis_data);
414  calibrated = gravi_calib_vis (vis_data, vis_calib, nb_frame_calib,
415  zero_data, tf_science);
416  if (cpl_error_get_code()){
417  gravi_data_delete (zero_data);
418  for (j = 0; j < nb_frame_calib; j++)
419  gravi_data_delete (vis_calib[j]);
420  cpl_free (vis_calib);
421  gravi_data_delete(vis_data);
422  gravi_data_delete(calibrated);
423  cpl_frameset_delete(vis_calib_frameset);
424  cpl_frameset_delete(vis_sci_frameset);
425 
426 
427  return (int) cpl_error_set_message(cpl_func,
428  CPL_ERROR_ILLEGAL_OUTPUT, "error while calibrating "
429  "the visbilities");
430  }
431 
432  primary_hdr = gravi_data_get_propertylist(calibrated,
433  GRAVI_PRIMARY_HDR_NAME_EXT);
434  applist = gravi_propertylist_get_qc (primary_hdr);
435 
436  if (!strcmp(cpl_frame_get_tag (frame), VIS_SCIENCE))
437  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
438  VIS_SINGLE_CALIBRATED);
439  else if (!strcmp(cpl_frame_get_tag (frame), VIS_DUAL_SCIENCE))
440  cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
441  VIS_DUAL_CALIBRATED);
442 
443  char * vis_cal = cpl_sprintf("vis_calibrated_%d.fits", i);
444 
445  // SET OIFITS Keywords
446  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
447  GRAVI_OI_VIS_SC_EXT), "INSNAME", "GRAVITY_SC");
448  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
449  GRAVI_OI_VIS_SC_EXT), "ARRNAME", "VLTI");
450  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
451  GRAVI_OI_VIS_SC_EXT), "DATE-OBS",
452  cpl_propertylist_get_string (primary_hdr, "DATE-OBS")); // FIXME
453  cpl_propertylist_update_int(gravi_data_get_propertylist(calibrated,
454  GRAVI_OI_VIS_SC_EXT), "OI_REVN", 1);
455  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
456  GRAVI_OI_VIS_SC_EXT), "EXTNAME", "OI_VIS");
457 
458  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
459  GRAVI_OI_VIS2_SC_EXT), "INSNAME", "GRAVITY_SC");
460  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
461  GRAVI_OI_VIS2_SC_EXT), "ARRNAME", "VLTI");
462  cpl_propertylist_update_int(gravi_data_get_propertylist(calibrated,
463  GRAVI_OI_VIS2_SC_EXT), "OI_REVN", 1);
464  // cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated, GRAVI_OI_VIS2_SC_EXT), "EXTNAME", "OI_VIS2");
465 
466  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
467  GRAVI_OI_T3_SC_EXT), "INSNAME", "GRAVITY_SC");
468  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
469  GRAVI_OI_T3_SC_EXT), "ARRNAME", "VLTI");
470  cpl_propertylist_update_int(gravi_data_get_propertylist(calibrated,
471  GRAVI_OI_T3_SC_EXT), "OI_REVN", 1);
472  // cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated, GRAVI_OI_T3_SC_EXT), "EXTNAME", "OI_VIS2");
473 
474  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
475  GRAVI_IO_WAVELENGTH_SC_EXT), "INSNAME", "GRAVITY_SC");
476  cpl_propertylist_update_int(gravi_data_get_propertylist(calibrated,
477  GRAVI_IO_WAVELENGTH_SC_EXT), "OI_REVN", 1);
478  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
479  GRAVI_IO_WAVELENGTH_SC_EXT), "EXTNAME", "OI_WAVELENGTH");
480 
481  cpl_propertylist_update_string(gravi_data_get_propertylist(calibrated,
482  GRAVI_OI_ARRAY_EXT), "ARRNAME", "VLTI");
483  cpl_propertylist_update_int(gravi_data_get_propertylist(calibrated,
484  GRAVI_OI_ARRAY_EXT), "OI_REVN", 1);
485 
486  cpl_frameset_insert (used_frame, cpl_frame_duplicate (frame));
487 
488  if (gravi_data_save(calibrated, frameset, vis_cal, parlist,
489  used_frame, frame, "gravi_all_viscal", applist)
490  != CPL_ERROR_NONE){
491  gravi_data_delete (zero_data);
492  for (j = 0; j < nb_frame_calib; j++)
493  gravi_data_delete (vis_calib[j]);
494  cpl_free (vis_calib);
495  gravi_data_delete(vis_data);
496  gravi_data_delete(calibrated);
497  cpl_frameset_delete(vis_calib_frameset);
498  cpl_frameset_delete(vis_sci_frameset);
499  cpl_propertylist_delete(applist);
500 
501  return (int) cpl_error_set_message(cpl_func,
502  CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the "
503  "calibrated visibilities on the output file");
504  }
505 
506  gravi_data_delete(vis_data);
507  gravi_data_delete(tf_science);
508  gravi_data_delete(calibrated);
509  cpl_propertylist_delete(applist);
510  cpl_free (vis_cal);
511  }
512  }
513 
514  /* Deallocation of all variables */
515  gravi_data_delete (zero_data);
516 
517  cpl_frameset_delete(vis_calib_frameset);
518  cpl_frameset_delete(vis_sci_frameset);
519  cpl_frameset_delete(used_frame);
520  for (j = 0; j < nb_frame_calib; j++)
521  gravi_data_delete (vis_calib[j]);
522  cpl_free (vis_calib);
523 
524  return (int)cpl_error_get_code();
525 }
526 
527