GRAVI Pipeline Reference Manual  0.9.6
gravity_nab.c
1 /* $Id: gravity_nab.c,v 1.29 2015/01/10 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: 2015/01/10 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 <time.h>
40 
41 
42 #include "gravi_utils.h"
43 #include "gravi_pfits.h"
44 #include "gravi_dfs.h"
45 #include "gravi_calib.h"
46 
47 #include "gravi_data.h"
48 
49 /*-----------------------------------------------------------------------------
50  Private function prototypes
51  -----------------------------------------------------------------------------*/
52 
53 static int gravity_nab_create(cpl_plugin *);
54 static int gravity_nab_exec(cpl_plugin *);
55 static int gravity_nab_destroy(cpl_plugin *);
56 static int gravity_nab(cpl_frameset *, const cpl_parameterlist *);
57 
58 /*-----------------------------------------------------------------------------
59  Static variables
60  -----------------------------------------------------------------------------*/
61 
62 static char gravity_nab_short[] = GRAVI_UNOFFERED"Calibrate the narrow angle baseline.";
63 static char gravity_nab_description[] = GRAVI_UNOFFERED"This recipe computes the narrow angle baseline from a set of visibilities obtained on calibration stars. This is only used on DUAL mode."
64 "It is used in dual field mode\n"
65  GRAVI_RECIPE_INPUT"\n"
66  GRAVI_VIS_DUAL_CALIB" xN : visibilities on calibration stars\n"
67  GRAVI_RECIPE_OUTPUT"\n"
68  GRAVI_NAB_CAL" : model of narrow angle baseline\n"
69  "";
70 
71 /*-----------------------------------------------------------------------------
72  Function code
73  -----------------------------------------------------------------------------*/
74 
75 /*----------------------------------------------------------------------------*/
85 /*----------------------------------------------------------------------------*/
86 int cpl_plugin_get_info(cpl_pluginlist * list)
87 {
88  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
89  cpl_plugin * plugin = &recipe->interface;
90 
91  if (cpl_plugin_init(plugin,
92  CPL_PLUGIN_API,
93  GRAVI_BINARY_VERSION,
94  CPL_PLUGIN_TYPE_RECIPE,
95  "gravity_nab",
96  gravity_nab_short,
97  gravity_nab_description,
98  "Firstname Lastname",
99  PACKAGE_BUGREPORT,
100  gravi_get_license(),
101  gravity_nab_create,
102  gravity_nab_exec,
103  gravity_nab_destroy)) {
104  cpl_msg_error(cpl_func, "Plugin initialization failed");
105  (void)cpl_error_set_where(cpl_func);
106  return 1;
107  }
108 
109  if (cpl_pluginlist_append(list, plugin)) {
110  cpl_msg_error(cpl_func, "Error adding plugin to list");
111  (void)cpl_error_set_where(cpl_func);
112  return 1;
113  }
114 
115  return 0;
116 }
117 
118 /*----------------------------------------------------------------------------*/
126 /*----------------------------------------------------------------------------*/
127 static int gravity_nab_create(cpl_plugin * plugin)
128 {
129  cpl_recipe * recipe;
130 
131  /* Do not create the recipe if an error code is already set */
132  if (cpl_error_get_code() != CPL_ERROR_NONE) {
133  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
134  cpl_func, __LINE__, cpl_error_get_where());
135  return (int)cpl_error_get_code();
136  }
137 
138  if (plugin == NULL) {
139  cpl_msg_error(cpl_func, "Null plugin");
140  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
141  }
142 
143  /* Verify plugin type */
144  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
145  cpl_msg_error(cpl_func, "Plugin is not a recipe");
146  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
147  }
148 
149  /* Get the recipe */
150  recipe = (cpl_recipe *)plugin;
151 
152  /* Create the parameters list in the cpl_recipe object */
153  recipe->parameters = cpl_parameterlist_new();
154  if (recipe->parameters == NULL) {
155  cpl_msg_error(cpl_func, "Parameter list allocation failed");
156  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
157  }
158 
159  /* Fill the parameters list */
160 
161  return 0;
162 }
163 
164 /*----------------------------------------------------------------------------*/
170 /*----------------------------------------------------------------------------*/
171 static int gravity_nab_exec(cpl_plugin * plugin)
172 {
173 
174  cpl_recipe * recipe;
175  int recipe_status;
176  cpl_errorstate initial_errorstate = cpl_errorstate_get();
177 
178  /* Return immediately if an error code is already set */
179  if (cpl_error_get_code() != CPL_ERROR_NONE) {
180  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
181  cpl_func, __LINE__, cpl_error_get_where());
182  return (int)cpl_error_get_code();
183  }
184 
185  if (plugin == NULL) {
186  cpl_msg_error(cpl_func, "Null plugin");
187  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
188  }
189 
190  /* Verify plugin type */
191  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
192  cpl_msg_error(cpl_func, "Plugin is not a recipe");
193  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
194  }
195 
196  /* Get the recipe */
197  recipe = (cpl_recipe *)plugin;
198 
199  /* Verify parameter and frame lists */
200  if (recipe->parameters == NULL) {
201  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
202  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
203  }
204  if (recipe->frames == NULL) {
205  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
206  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
207  }
208 
209  /* Invoke the recipe */
210  recipe_status = gravity_nab(recipe->frames, recipe->parameters);
211 
212  /* Ensure DFS-compliance of the products */
213  if (cpl_dfs_update_product_header(recipe->frames)) {
214  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
215  }
216 
217  if (!cpl_errorstate_is_equal(initial_errorstate)) {
218  /* Dump the error history since recipe execution start.
219  At this point the recipe cannot recover from the error */
220  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
221  }
222 
223  return recipe_status;
224 }
225 
226 /*----------------------------------------------------------------------------*/
232 /*----------------------------------------------------------------------------*/
233 static int gravity_nab_destroy(cpl_plugin * plugin)
234 {
235  cpl_recipe * recipe;
236 
237  if (plugin == NULL) {
238  cpl_msg_error(cpl_func, "Null plugin");
239  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
240  }
241 
242  /* Verify plugin type */
243  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
244  cpl_msg_error(cpl_func, "Plugin is not a recipe");
245  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
246  }
247 
248  /* Get the recipe */
249  recipe = (cpl_recipe *)plugin;
250 
251  cpl_parameterlist_delete(recipe->parameters);
252 
253  return 0;
254 }
255 
256 
257 /*----------------------------------------------------------------------------*/
264 /*----------------------------------------------------------------------------*/
265 static int gravity_nab(cpl_frameset * frameset,
266  const cpl_parameterlist * parlist)
267 {
268  cpl_frameset * baseline_frameset = NULL;
269 
270  cpl_frame * frame;
271 
272  gravi_data * raw_data, * baseline_data;
273 
274  int comp, nb_frame;
275 
276  /* Message */
277  cpl_msg_set_time_on();
278  cpl_msg_set_component_on();
279  gravi_msg_function_start(1);
280 
281  /* Identify the RAW and CALIB frames in the input frameset */
282  cpl_ensure_code (gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
283  cpl_error_get_code()) ;
284 
285  /* - Extract a set of frame */
286  baseline_frameset = gravi_frameset_extract_vis_calib (frameset);
287  if (cpl_frameset_is_empty (baseline_frameset)) {
288  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
289  "Missing frames") ;
290  }
291 
292  /*
293  * Loop on baseline_frameset (?)
294  */
295 
296  nb_frame = cpl_frameset_get_size (baseline_frameset);
297  for (comp = 0; comp < nb_frame; comp++){
298 
299  cpl_msg_info (cpl_func, "*** Loop on file %i over %i ***", comp, nb_frame);
300 
301  frame = cpl_frameset_get_position (baseline_frameset, comp);
302  raw_data = gravi_data_load_frame (frame, NULL);
303 
304  baseline_data = gravi_compute_baseline (raw_data);
305  FREE (gravi_data_delete, raw_data);
306 
307  gravi_data_save_new (baseline_data, frameset, NULL, parlist,
308  NULL, frame, "gravity_nab",
309  NULL, GRAVI_NAB_CAL);
310 
311  CPLCHECK_CLEAN ("Cannot save the NAB_CAL");
312 
313  FREE (gravi_data_delete, baseline_data);
314  FREE (gravi_data_delete, raw_data);
315  }
316 
317  cleanup:
318  /* Deallocation of all variables */
319  cpl_msg_info(cpl_func,"Memory cleanup");
320 
321  gravi_msg_function_exit(1);
322  return (int)cpl_error_get_code();
323 }
324 
325