MIDI Pipeline Reference Manual  2.8.3
midi_raw_to_fitsimage.c
1 /* $Id: midi_raw_to_fitsimage.c,v 1.8 2010-05-28 09:16:01 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: 2010-05-28 09:16:01 $
24  * $Revision: 1.8 $
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 /*-----------------------------------------------------------------------------
44  Private function prototypes
45  -----------------------------------------------------------------------------*/
46 
47 static int midi_raw_to_fitsimage_create(cpl_plugin *);
48 static int midi_raw_to_fitsimage_exec(cpl_plugin *);
49 static int midi_raw_to_fitsimage_destroy(cpl_plugin *);
50 static int midi_raw_to_fitsimage(cpl_frameset *, const cpl_parameterlist *);
51 
52 /*-----------------------------------------------------------------------------
53  Static variables
54  -----------------------------------------------------------------------------*/
55 
56 static char midi_raw_to_fitsimage_description[] =
57 "The main purpose of this recipe is to convert the imaging sections of the\n"
58 "Midi raw files into fits-cubes or fits-images. This recipe is able to\n"
59 "process all midi raw files with a DATA1/2/3/4 column in the\n"
60 "IMAGING_DATA extension, i.e. most of the midi raw files. Therefore no\n"
61 "special classification tag needs to be given to the SOF. Moreover, the SOF\n"
62 "should include only one midi raw file.\n"
63 " \n"
64 "As this recipe calculates up to 28 diagnostic product files the user\n"
65 "should refer to the pipeline manual for a detailed description.\n"
66 "\n";
67 
68 /*-----------------------------------------------------------------------------
69  Function code
70  -----------------------------------------------------------------------------*/
71 
72 /*----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------*/
78 
81 /*----------------------------------------------------------------------------*/
91 /*----------------------------------------------------------------------------*/
92 int cpl_plugin_get_info(cpl_pluginlist * list)
93 {
94  cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
95  cpl_plugin * plugin = &recipe->interface;
96 
97  if (cpl_plugin_init(plugin,
98  CPL_PLUGIN_API,
99  MIDI_BINARY_VERSION,
100  CPL_PLUGIN_TYPE_RECIPE,
101  "midi_raw_to_fitsimage",
102  "MIDI raw data display",
103  midi_raw_to_fitsimage_description,
104  "Armin Gabasch",
105  PACKAGE_BUGREPORT,
107  midi_raw_to_fitsimage_create,
108  midi_raw_to_fitsimage_exec,
109  midi_raw_to_fitsimage_destroy)) {
110  cpl_msg_error(cpl_func, "Plugin initialization failed");
111  (void)cpl_error_set_where(cpl_func);
112  return 1;
113  }
114 
115  if (cpl_pluginlist_append(list, plugin)) {
116  cpl_msg_error(cpl_func, "Error adding plugin to list");
117  (void)cpl_error_set_where(cpl_func);
118  return 1;
119  }
120 
121  return 0;
122 }
123 
124 /*----------------------------------------------------------------------------*/
132 /*----------------------------------------------------------------------------*/
133 static int midi_raw_to_fitsimage_create(cpl_plugin * plugin)
134 {
135  cpl_recipe * recipe;
136  cpl_parameter * p;
137 
138  /* Do not create the recipe if an error code is already set */
139  if (cpl_error_get_code() != CPL_ERROR_NONE) {
140  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
141  cpl_func, __LINE__, cpl_error_get_where());
142  return (int)cpl_error_get_code();
143  }
144 
145  if (plugin == NULL) {
146  cpl_msg_error(cpl_func, "Null plugin");
147  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
148  }
149 
150  /* Verify plugin type */
151  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
152  cpl_msg_error(cpl_func, "Plugin is not a recipe");
153  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
154  }
155 
156  /* Get the recipe */
157  recipe = (cpl_recipe *)plugin;
158 
159  /* Create the parameters list in the cpl_recipe object */
160  recipe->parameters = cpl_parameterlist_new();
161  if (recipe->parameters == NULL) {
162  cpl_msg_error(cpl_func, "Parameter list allocation failed");
163  cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
164  }
165 
166  /* Fill the parameters list */
167  /* --stropt */
168  p = cpl_parameter_new_value("midi.midi_raw_to_fitsimage.outputfilename",
169  CPL_TYPE_STRING, "Output Filename", "midi.midi_raw_to_fitsimage","cube.fits");
170  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "outfile");
171  cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
172  cpl_parameterlist_append(recipe->parameters, p);
173 
174 /* /\* --boolopt *\/ */
175 /* p = cpl_parameter_new_value("midi.midi_raw_to_fitsimage.bool_option", */
176 /* CPL_TYPE_BOOL, "a flag", "midi.midi_raw_to_fitsimage", TRUE); */
177 /* cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt"); */
178 /* cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); */
179 /* cpl_parameterlist_append(recipe->parameters, p); */
180 
181  return 0;
182 }
183 
184 /*----------------------------------------------------------------------------*/
190 /*----------------------------------------------------------------------------*/
191 static int midi_raw_to_fitsimage_exec(cpl_plugin * plugin)
192 {
193 
194  cpl_recipe * recipe;
195  int recipe_status;
196  cpl_errorstate initial_errorstate = cpl_errorstate_get();
197 
198  /* Return immediately if an error code is already set */
199  if (cpl_error_get_code() != CPL_ERROR_NONE) {
200  cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
201  cpl_func, __LINE__, cpl_error_get_where());
202  return (int)cpl_error_get_code();
203  }
204 
205  if (plugin == NULL) {
206  cpl_msg_error(cpl_func, "Null plugin");
207  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
208  }
209 
210  /* Verify plugin type */
211  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
212  cpl_msg_error(cpl_func, "Plugin is not a recipe");
213  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
214  }
215 
216  /* Get the recipe */
217  recipe = (cpl_recipe *)plugin;
218 
219  /* Verify parameter and frame lists */
220  if (recipe->parameters == NULL) {
221  cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
222  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
223  }
224  if (recipe->frames == NULL) {
225  cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
226  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
227  }
228 
229  /* Invoke the recipe */
230  recipe_status = midi_raw_to_fitsimage(recipe->frames, recipe->parameters);
231 
232  /* Ensure DFS-compliance of the products */
233  if (cpl_dfs_update_product_header(recipe->frames)) {
234  if (!recipe_status) recipe_status = (int)cpl_error_get_code();
235  }
236 
237  if (!cpl_errorstate_is_equal(initial_errorstate)) {
238  /* Dump the error history since recipe execution start.
239  At this point the recipe cannot recover from the error */
240  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
241  }
242 
243  return recipe_status;
244 }
245 
246 /*----------------------------------------------------------------------------*/
252 /*----------------------------------------------------------------------------*/
253 static int midi_raw_to_fitsimage_destroy(cpl_plugin * plugin)
254 {
255  cpl_recipe * recipe;
256 
257  if (plugin == NULL) {
258  cpl_msg_error(cpl_func, "Null plugin");
259  cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
260  }
261 
262  /* Verify plugin type */
263  if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
264  cpl_msg_error(cpl_func, "Plugin is not a recipe");
265  cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
266  }
267 
268  /* Get the recipe */
269  recipe = (cpl_recipe *)plugin;
270 
271  cpl_parameterlist_delete(recipe->parameters);
272 
273  return 0;
274 }
275 
276 /*----------------------------------------------------------------------------*/
283 /*----------------------------------------------------------------------------*/
284 static int midi_raw_to_fitsimage(cpl_frameset * frameset,
285  const cpl_parameterlist * parlist)
286 {
287 
288  const cpl_parameter * param;
289  const char * outputfilename;
290  const cpl_frame * rawframe;
291  cpl_table * table;
292  int extnum;
293  int i;
294  cpl_propertylist * pHeader_tmp;
295  int ext_imaging_data;
296  char * current_extension;
297  /* Use the errorstate to detect an error in a function that does not
298  return an error code. */
299  cpl_errorstate prestate = cpl_errorstate_get();
300 
301 
302 
303  /* HOW TO RETRIEVE INPUT PARAMETERS */
304  /* --stropt */
305  param = cpl_parameterlist_find_const(parlist, "midi.midi_raw_to_fitsimage.outputfilename");
306  outputfilename = cpl_parameter_get_string(param);
307 
308 
309  if (!cpl_errorstate_is_equal(prestate)) {
310  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
311  "Could not retrieve the input "
312  "parameters");
313  }
314 
315  /* Identify the RAW and CALIB frames in the input frameset */
316  cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
317  cpl_error_get_code());
318 
319  /* HOW TO ACCESS INPUT DATA */
320  /* - A required file */
321  rawframe = cpl_frameset_get_first(frameset);
322 /* rawframe = cpl_frameset_find_const(frameset, MIDI_ACQ); */
323  if (rawframe == NULL) {
324  /* cpl_frameset_find_const() does not set an error code, when a frame
325  is not found, so we will set one here. */
326  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
327  "SOF does not have any file");
328  }
329 
330 
331  /* Check for a change in the CPL error state */
332  /* - if it did change then propagate the error and return */
333  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
334 
335  /* NOW PERFORMING THE DATA REDUCTION */
336 
337 
338  /* read the number of extensions from the rawfile */
339  extnum=cpl_frame_get_nextensions(rawframe);
340  cpl_msg_info(cpl_func, "Number of extensions found: %d",extnum);
341 
342 
343  /* Loop through the extension and find Extension "IMAGING_DATA" */
344 
345  ext_imaging_data=0;
346 /* pHeader_tmp = cpl_propertylist_new(); */
347 
348  for (i=1; i<=extnum; i++){
349  pHeader_tmp = cpl_propertylist_load(cpl_frame_get_filename(rawframe), i );
350  current_extension=(char *)cpl_propertylist_get_string(pHeader_tmp,"EXTNAME");
351  cpl_msg_info(cpl_func, "Loop through extensions: %s", current_extension);
352  if (strcmp(current_extension,"IMAGING_DATA")==0){
353  ext_imaging_data=i;
354  cpl_msg_info(cpl_func, " Extension <IMAGING_DATA> found in extension %d",ext_imaging_data);
355  cpl_propertylist_delete(pHeader_tmp);
356  break;
357  }
358  cpl_propertylist_delete(pHeader_tmp);
359  }
360 
361 
362  if (ext_imaging_data==0){
363  cpl_msg_error(cpl_func, " Extension <IMAGING_DATA> NOT found");
364  return -1;
365  }
366  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
367 
368 
369  /* Load extension Imaging Data */
370  table = cpl_table_load(cpl_frame_get_filename(rawframe), ext_imaging_data, 1);
371  if (table == NULL) {
372  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
373  "Could not load the table");
374  }
375  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
376 
377  cpl_msg_info(cpl_func, "Scanning for DATA1 ...");
378  if (cpl_table_has_column(table,"DATA1")){
379  table_to_fitsimage(frameset, parlist, "DATA1",outputfilename,table);
380  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
381  }
382  else {
383  cpl_msg_info(cpl_func, "DATA1 not found");
384  }
385 
386  cpl_msg_info(cpl_func, "Scanning for DATA2 ...");
387  if (cpl_table_has_column(table,"DATA2")){
388  table_to_fitsimage(frameset, parlist, "DATA2",outputfilename,table);
389  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
390  }
391  else {
392  cpl_msg_info(cpl_func, "DATA2 not found");
393  }
394 
395 
396  cpl_msg_info(cpl_func, "Scanning for DATA3 ...");
397  if (cpl_table_has_column(table,"DATA3")){
398  table_to_fitsimage(frameset, parlist, "DATA3",outputfilename,table);
399  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
400  }
401  else {
402  cpl_msg_info(cpl_func, "DATA3 not found");
403  }
404 
405  cpl_msg_info(cpl_func, "Scanning for DATA4 ...");
406  if (cpl_table_has_column(table,"DATA4")){
407  table_to_fitsimage(frameset, parlist, "DATA4",outputfilename,table);
408  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
409  }
410  else {
411  cpl_msg_info(cpl_func, "DATA4 not found");
412  }
413 
414 
415 
416 
417 /* Free the Memory */
418 
419 /* Not to be freed at the moment !!!!!!!*/
420 /* cpl_imagelist_delete(imagelist_data1); */
421 /* cpl_imagelist_delete(imagelist_data2); */
422 
423  cpl_table_delete(table);
424 
425  return (int)cpl_error_get_code();
426 }
427