GRAVI Pipeline Reference Manual  1.2.3
gravity_piezo.c
1 /* $Id: gravity_vis.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 /*
29  * History :
30  * 16/01/2019 fix warning unused parameter isCalib
31  */
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 /*-----------------------------------------------------------------------------
37  Includes
38  -----------------------------------------------------------------------------*/
39 
40 #include <cpl.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 
45 #include "gravi_data.h"
46 #include "gravi_pfits.h"
47 #include "gravi_dfs.h"
48 
49 #include "gravi_utils.h"
50 
51 #include "gravi_calib.h"
52 #include "gravi_p2vmred.h"
53 #include "gravi_eop.h"
54 #include "gravi_metrology.h"
55 
56 #include "gravi_signal.h"
57 #include "gravi_vis.h"
58 #include "gravi_tf.h"
59 
60 #include "gravi_preproc.h"
61 // #include "gravi_p2vm.h"
62 
63 /*-----------------------------------------------------------------------------
64  Private function prototypes
65  -----------------------------------------------------------------------------*/
66 
67 static int gravity_piezo_create(cpl_plugin *);
68 static int gravity_piezo_exec(cpl_plugin *);
69 static int gravity_piezo_destroy(cpl_plugin *);
70 static int gravity_piezo(cpl_frameset *, const cpl_parameterlist *);
71 
72 /*-----------------------------------------------------------------------------
73  Static variables
74  -----------------------------------------------------------------------------*/
75 
76 static char gravity_piezo_short[] = "Calibrate the response of the piezo actuators.";
77 static char gravity_piezo_description[] = "This recipe compute the response (open loop transfer function) of the piezo actuators used to fringe-track in GRAVITY.\n"
78  GRAVI_RECIPE_FLOW"\n"
79  "* Compute the piezo TF QC parameter"
80  "* Write product\n"
81  GRAVI_RECIPE_INPUT"\n"
82  GRAVI_PIEZOTF_RAW" : dedicated observations (DPR.CATG=PIEZOTF)\n"
83  GRAVI_RECIPE_OUTPUT"\n"
84  GRAVI_PIEZOTF_MAP" : Response of the piezo\n"
85  "";
86 
87 /*-----------------------------------------------------------------------------
88  Function code
89  -----------------------------------------------------------------------------*/
90 
91 /*----------------------------------------------------------------------------*/
101 /*----------------------------------------------------------------------------*/
102 int cpl_plugin_get_info(cpl_pluginlist * list)
103 {
104  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
105  cpl_plugin * plugin = &recipe->interface;
106 
107  if (cpl_plugin_init(plugin,
108  CPL_PLUGIN_API,
109  GRAVI_BINARY_VERSION,
110  CPL_PLUGIN_TYPE_RECIPE,
111  "gravity_piezo",
112  gravity_piezo_short,
113  gravity_piezo_description,
114  "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
115  PACKAGE_BUGREPORT,
117  gravity_piezo_create,
118  gravity_piezo_exec,
119  gravity_piezo_destroy)) {
120  cpl_msg_error(cpl_func, "Plugin initialization failed");
121  (void)cpl_error_set_where(cpl_func);
122  return 1;
123  }
124 
125  if (cpl_pluginlist_append(list, plugin)) {
126  cpl_msg_error(cpl_func, "Error adding plugin to list");
127  (void)cpl_error_set_where(cpl_func);
128  return 1;
129  }
130 
131  return 0;
132 }
133 
134 /*----------------------------------------------------------------------------*/
142 /*----------------------------------------------------------------------------*/
143 static int gravity_piezo_create(cpl_plugin * plugin)
144 {
145  cpl_recipe * recipe;
146  // cpl_parameter * p;
147 
148  /* Do not create the recipe if an error code is already set */
149  if (cpl_error_get_code() != CPL_ERROR_NONE) {
150  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
151  cpl_func, __LINE__, cpl_error_get_where());
152  return (int)cpl_error_get_code();
153  }
154 
155  if (plugin == NULL) {
156  cpl_msg_error(cpl_func, "Null plugin");
157  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
158  }
159 
160  /* Verify plugin type */
161  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
162  cpl_msg_error(cpl_func, "Plugin is not a recipe");
163  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
164  }
165 
166  /* Get the recipe */
167  recipe = (cpl_recipe *)plugin;
168 
169  /* Create the parameters list in the cpl_recipe object */
170  recipe->parameters = cpl_parameterlist_new();
171  if (recipe->parameters == NULL) {
172  cpl_msg_error(cpl_func, "Parameter list allocation failed");
173  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
174  }
175 
176  /* Fill the parameters list */
177  /* int isCalib = 1; */
178 
179  /* Use static names (output_procatg.fits) */
180  gravi_parameter_add_static_name (recipe->parameters);
181 
182  return 0;
183 }
184 
185 /*----------------------------------------------------------------------------*/
191 /*----------------------------------------------------------------------------*/
192 static int gravity_piezo_exec(cpl_plugin * plugin)
193 {
194 
195  cpl_recipe * recipe;
196  int recipe_status;
197  cpl_errorstate initial_errorstate = cpl_errorstate_get();
198 
199 
200  /* Return immediately if an error code is already set */
201  if (cpl_error_get_code() != CPL_ERROR_NONE) {
202  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
203  cpl_func, __LINE__, cpl_error_get_where());
204  return (int)cpl_error_get_code();
205  }
206 
207  if (plugin == NULL) {
208  cpl_msg_error(cpl_func, "Null plugin");
209  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
210  }
211 
212  /* Verify plugin type */
213  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
214  cpl_msg_error(cpl_func, "Plugin is not a recipe");
215  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
216  }
217 
218  /* Get the recipe */
219  recipe = (cpl_recipe *)plugin;
220 
221  /* Verify parameter and frame lists */
222  if (recipe->parameters == NULL) {
223  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
224  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
225  }
226  if (recipe->frames == NULL) {
227  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
228  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
229  }
230 
231  /* Invoke the recipe */
232  recipe_status = gravity_piezo(recipe->frames, recipe->parameters);
233 
234  /* Ensure DFS-compliance of the products */
235  if (cpl_dfs_update_product_header(recipe->frames)) {
236  if (!recipe_status){
237  recipe_status = (int)cpl_error_get_code();
238  }
239  }
240 
241  if (!cpl_errorstate_is_equal(initial_errorstate)) {
242  /* Dump the error history since recipe execution start.
243  At this point the recipe cannot recover from the error */
244  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
245  }
246 
247  return recipe_status;
248 }
249 
250 /*----------------------------------------------------------------------------*/
256 /*----------------------------------------------------------------------------*/
257 static int gravity_piezo_destroy(cpl_plugin * plugin)
258 {
259  cpl_recipe * recipe;
260 
261  if (plugin == NULL) {
262  cpl_msg_error(cpl_func, "Null plugin");
263  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
264  }
265 
266  /* Verify plugin type */
267  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
268  cpl_msg_error(cpl_func, "Plugin is not a recipe");
269  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
270  }
271 
272  /* Get the recipe */
273  recipe = (cpl_recipe *)plugin;
274 
275  cpl_parameterlist_delete(recipe->parameters);
276 
277  return 0;
278 }
279 
280 
281 /*----------------------------------------------------------------------------*/
289 /*----------------------------------------------------------------------------*/
290 static int gravity_piezo(cpl_frameset * frameset,
291  const cpl_parameterlist * parlist)
292 {
293  cpl_frameset * recipe_frameset=NULL, *used_frameset=NULL, * current_frameset=NULL;
294 
295  cpl_frame * frame=NULL;
296 
297  gravi_data * data=NULL, * piezo_data=NULL;
298 
299  int nb_frame;
300 
301  /* Message */
302  gravity_print_banner ();
303  cpl_msg_set_time_on();
304  cpl_msg_set_component_on();
305  gravi_msg_function_start(1);
306 
307  /* Identify the frames in the input frameset */
308  cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
309  cpl_error_get_code()) ;
310 
311  /* Dispatch the frameset */
312  recipe_frameset = gravi_frameset_extract_piezotf_data (frameset);
313 
314  /* Check the frameset */
315  if (cpl_frameset_is_empty (recipe_frameset)) {
316  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
317  "No PIEZOTF file on the frameset") ;
318  goto cleanup;
319  }
320 
321 
322  /* Insert calibration frame into the used frameset */
323  used_frameset = cpl_frameset_new();
324 
325  /*
326  * Loop on input RAW frames to be reduced
327  */
328 
329 
330  nb_frame = cpl_frameset_get_size (recipe_frameset);
331 
332  for (int i_file = 0; i_file < nb_frame; i_file++){
333  current_frameset = cpl_frameset_duplicate (used_frameset);
334 
335  cpl_msg_info (cpl_func, " ***** File %d over %d ***** ", i_file+1, nb_frame);
336 
337  /*
338  * Reduce the File
339  */
340 
341  frame = cpl_frameset_get_position (recipe_frameset, i_file);
342  data = gravi_data_load_rawframe (frame, current_frameset);
343  piezo_data = gravi_compute_piezotf (data, parlist);
344  CPLCHECK_CLEAN ("Cannot compute the piezo TF");
345 
346  /* Save the PIEZOTF which is in fact a P2VMREDUCED */
347  gravi_data_save_new (piezo_data, frameset, NULL, NULL, parlist,
348  current_frameset, frame, "gravity_piezo",
349  NULL, GRAVI_PIEZOTF_MAP);
350 
351  CPLCHECK_CLEAN ("Cannot save the PIEZOTF product");
352 
353  cpl_msg_info (cpl_func,"Free the piezotf");
354  FREE (cpl_frameset_delete, current_frameset);
355  FREE (gravi_data_delete, piezo_data);
356  }
357  /* End loop on the input files to reduce */
358 
359  /* Terminate the function */
360  goto cleanup;
361 
362 cleanup:
363  /* Deallocation of all variables */
364  cpl_msg_info(cpl_func,"Memory cleanup");
365 
366  FREE (gravi_data_delete,data);
367  FREE (gravi_data_delete,piezo_data);
368  FREE (cpl_frameset_delete,recipe_frameset);
369  FREE (cpl_frameset_delete,current_frameset);
370  FREE (cpl_frameset_delete,used_frameset);
371 
372  gravi_msg_function_exit(1);
373  return (int)cpl_error_get_code();
374 }
gravi_data * gravi_data_load_rawframe(cpl_frame *frame, cpl_frameset *used_frameset)
Load a RAW FITS file and create a gravi_data.
Definition: gravi_data.c:690
cpl_error_code gravi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: gravi_dfs.c:78
gravi_data * gravi_compute_piezotf(gravi_data *data, const cpl_parameterlist *params)
Create piezo transfer function for Kalman Calibration & monitoring.
Definition: gravi_calib.c:2226
const char * gravi_get_license(void)
Get the pipeline copyright and license.
Definition: gravi_utils.c:104
cpl_error_code gravi_data_save_new(gravi_data *self, cpl_frameset *allframes, const char *filename, const char *suffix, const cpl_parameterlist *parlist, cpl_frameset *usedframes, cpl_frame *frame, const char *recipe, cpl_propertylist *applist, const char *proCatg)
Save a gravi data in a CPL-complian FITS file.
Definition: gravi_data.c:896
void gravi_data_delete(gravi_data *self)
Delete a gravi data.
Definition: gravi_data.c:137