38 #include "midi_utils.h"
39 #include "midi_pfits.h"
42 #include "midiTableToFits.h"
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 *);
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"
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"
94 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
95 cpl_plugin * plugin = &recipe->interface;
97 if (cpl_plugin_init(plugin,
100 CPL_PLUGIN_TYPE_RECIPE,
101 "midi_raw_to_fitsimage",
102 "MIDI raw data display",
103 midi_raw_to_fitsimage_description,
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);
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);
133 static int midi_raw_to_fitsimage_create(cpl_plugin * plugin)
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();
145 if (plugin == NULL) {
146 cpl_msg_error(cpl_func,
"Null plugin");
147 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
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);
157 recipe = (cpl_recipe *)plugin;
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);
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);
191 static int midi_raw_to_fitsimage_exec(cpl_plugin * plugin)
196 cpl_errorstate initial_errorstate = cpl_errorstate_get();
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();
205 if (plugin == NULL) {
206 cpl_msg_error(cpl_func,
"Null plugin");
207 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
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);
217 recipe = (cpl_recipe *)plugin;
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);
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);
230 recipe_status = midi_raw_to_fitsimage(recipe->frames, recipe->parameters);
233 if (cpl_dfs_update_product_header(recipe->frames)) {
234 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
237 if (!cpl_errorstate_is_equal(initial_errorstate)) {
240 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
243 return recipe_status;
253 static int midi_raw_to_fitsimage_destroy(cpl_plugin * plugin)
257 if (plugin == NULL) {
258 cpl_msg_error(cpl_func,
"Null plugin");
259 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
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);
269 recipe = (cpl_recipe *)plugin;
271 cpl_parameterlist_delete(recipe->parameters);
284 static int midi_raw_to_fitsimage(cpl_frameset * frameset,
285 const cpl_parameterlist * parlist)
288 const cpl_parameter * param;
289 const char * outputfilename;
290 const cpl_frame * rawframe;
294 cpl_propertylist * pHeader_tmp;
295 int ext_imaging_data;
296 char * current_extension;
299 cpl_errorstate prestate = cpl_errorstate_get();
305 param = cpl_parameterlist_find_const(parlist,
"midi.midi_raw_to_fitsimage.outputfilename");
306 outputfilename = cpl_parameter_get_string(param);
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 "
317 cpl_error_get_code());
321 rawframe = cpl_frameset_get_first(frameset);
323 if (rawframe == NULL) {
326 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
327 "SOF does not have any file");
333 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
339 extnum=cpl_frame_get_nextensions(rawframe);
340 cpl_msg_info(cpl_func,
"Number of extensions found: %d",extnum);
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){
354 cpl_msg_info(cpl_func,
" Extension <IMAGING_DATA> found in extension %d",ext_imaging_data);
355 cpl_propertylist_delete(pHeader_tmp);
358 cpl_propertylist_delete(pHeader_tmp);
362 if (ext_imaging_data==0){
363 cpl_msg_error(cpl_func,
" Extension <IMAGING_DATA> NOT found");
366 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
370 table = cpl_table_load(cpl_frame_get_filename(rawframe), ext_imaging_data, 1);
372 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
373 "Could not load the table");
375 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
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());
383 cpl_msg_info(cpl_func,
"DATA1 not found");
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());
392 cpl_msg_info(cpl_func,
"DATA2 not found");
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());
402 cpl_msg_info(cpl_func,
"DATA3 not found");
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());
411 cpl_msg_info(cpl_func,
"DATA4 not found");
423 cpl_table_delete(table);
425 return (
int)cpl_error_get_code();