00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <cpl.h>
00037
00038 #include "midi_utils.h"
00039 #include "midi_pfits.h"
00040 #include "midi_dfs.h"
00041 #include "string.h"
00042 #include "midiTableToFits.h"
00043
00044
00045
00046
00047 static int midi_raw_to_fitsimage_create(cpl_plugin *);
00048 static int midi_raw_to_fitsimage_exec(cpl_plugin *);
00049 static int midi_raw_to_fitsimage_destroy(cpl_plugin *);
00050 static int midi_raw_to_fitsimage(cpl_frameset *, const cpl_parameterlist *);
00051
00052
00053
00054
00055
00056 static char midi_raw_to_fitsimage_description[] =
00057 "The main purpose of this recipe is to convert the imaging sections of the\n"
00058 "Midi raw files into fits-cubes or fits-images. This recipe is able to\n"
00059 "process all midi raw files with a DATA1/2/3/4 column in the\n"
00060 "IMAGING_DATA extension, i.e. most of the midi raw files. Therefore no\n"
00061 "special classification tag needs to be given to the SOF. Moreover, the SOF\n"
00062 "should include only one midi raw file.\n"
00063 " \n"
00064 "As this recipe calculates up to 28 diagnostic product files the user\n"
00065 "should refer to the pipeline manual for a detailed description.\n"
00066 "\n";
00067
00068
00069
00070
00071
00072
00077
00078
00081
00091
00092 int cpl_plugin_get_info(cpl_pluginlist * list)
00093 {
00094 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe );
00095 cpl_plugin * plugin = &recipe->interface;
00096
00097 if (cpl_plugin_init(plugin,
00098 CPL_PLUGIN_API,
00099 MIDI_BINARY_VERSION,
00100 CPL_PLUGIN_TYPE_RECIPE,
00101 "midi_raw_to_fitsimage",
00102 "MIDI raw data display",
00103 midi_raw_to_fitsimage_description,
00104 "Armin Gabasch",
00105 PACKAGE_BUGREPORT,
00106 midi_get_license(),
00107 midi_raw_to_fitsimage_create,
00108 midi_raw_to_fitsimage_exec,
00109 midi_raw_to_fitsimage_destroy)) {
00110 cpl_msg_error(cpl_func, "Plugin initialization failed");
00111 (void)cpl_error_set_where(cpl_func);
00112 return 1;
00113 }
00114
00115 if (cpl_pluginlist_append(list, plugin)) {
00116 cpl_msg_error(cpl_func, "Error adding plugin to list");
00117 (void)cpl_error_set_where(cpl_func);
00118 return 1;
00119 }
00120
00121 return 0;
00122 }
00123
00124
00132
00133 static int midi_raw_to_fitsimage_create(cpl_plugin * plugin)
00134 {
00135 cpl_recipe * recipe;
00136 cpl_parameter * p;
00137
00138
00139 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00140 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00141 cpl_func, __LINE__, cpl_error_get_where());
00142 return (int)cpl_error_get_code();
00143 }
00144
00145 if (plugin == NULL) {
00146 cpl_msg_error(cpl_func, "Null plugin");
00147 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00148 }
00149
00150
00151 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00152 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00153 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00154 }
00155
00156
00157 recipe = (cpl_recipe *)plugin;
00158
00159
00160 recipe->parameters = cpl_parameterlist_new();
00161 if (recipe->parameters == NULL) {
00162 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00163 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00164 }
00165
00166
00167
00168 p = cpl_parameter_new_value("midi.midi_raw_to_fitsimage.outputfilename",
00169 CPL_TYPE_STRING, "Output Filename", "midi.midi_raw_to_fitsimage","cube.fits");
00170 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "outfile");
00171 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00172 cpl_parameterlist_append(recipe->parameters, p);
00173
00174
00175
00176
00177
00178
00179
00180
00181 return 0;
00182 }
00183
00184
00190
00191 static int midi_raw_to_fitsimage_exec(cpl_plugin * plugin)
00192 {
00193
00194 cpl_recipe * recipe;
00195 int recipe_status;
00196 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00197
00198
00199 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00200 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00201 cpl_func, __LINE__, cpl_error_get_where());
00202 return (int)cpl_error_get_code();
00203 }
00204
00205 if (plugin == NULL) {
00206 cpl_msg_error(cpl_func, "Null plugin");
00207 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00208 }
00209
00210
00211 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00212 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00213 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00214 }
00215
00216
00217 recipe = (cpl_recipe *)plugin;
00218
00219
00220 if (recipe->parameters == NULL) {
00221 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00222 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00223 }
00224 if (recipe->frames == NULL) {
00225 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00226 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00227 }
00228
00229
00230 recipe_status = midi_raw_to_fitsimage(recipe->frames, recipe->parameters);
00231
00232
00233 if (cpl_dfs_update_product_header(recipe->frames)) {
00234 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00235 }
00236
00237 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00238
00239
00240 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00241 }
00242
00243 return recipe_status;
00244 }
00245
00246
00252
00253 static int midi_raw_to_fitsimage_destroy(cpl_plugin * plugin)
00254 {
00255 cpl_recipe * recipe;
00256
00257 if (plugin == NULL) {
00258 cpl_msg_error(cpl_func, "Null plugin");
00259 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00260 }
00261
00262
00263 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00264 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00265 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00266 }
00267
00268
00269 recipe = (cpl_recipe *)plugin;
00270
00271 cpl_parameterlist_delete(recipe->parameters);
00272
00273 return 0;
00274 }
00275
00276
00283
00284 static int midi_raw_to_fitsimage(cpl_frameset * frameset,
00285 const cpl_parameterlist * parlist)
00286 {
00287
00288 const cpl_parameter * param;
00289 const char * outputfilename;
00290 const cpl_frame * rawframe;
00291 cpl_table * table;
00292 int extnum;
00293 int i;
00294 cpl_propertylist * pHeader_tmp;
00295 int ext_imaging_data;
00296 char * current_extension;
00297
00298
00299 cpl_errorstate prestate = cpl_errorstate_get();
00300
00301
00302
00303
00304
00305 param = cpl_parameterlist_find_const(parlist, "midi.midi_raw_to_fitsimage.outputfilename");
00306 outputfilename = cpl_parameter_get_string(param);
00307
00308
00309 if (!cpl_errorstate_is_equal(prestate)) {
00310 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00311 "Could not retrieve the input "
00312 "parameters");
00313 }
00314
00315
00316 cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00317 cpl_error_get_code());
00318
00319
00320
00321 rawframe = cpl_frameset_get_first(frameset);
00322
00323 if (rawframe == NULL) {
00324
00325
00326 return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00327 "SOF does not have any file");
00328 }
00329
00330
00331
00332
00333 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00334
00335
00336
00337
00338
00339 extnum=cpl_frame_get_nextensions(rawframe);
00340 cpl_msg_info(cpl_func, "Number of extensions found: %d",extnum);
00341
00342
00343
00344
00345 ext_imaging_data=0;
00346
00347
00348 for (i=1; i<=extnum; i++){
00349 pHeader_tmp = cpl_propertylist_load(cpl_frame_get_filename(rawframe), i );
00350 current_extension=(char *)cpl_propertylist_get_string(pHeader_tmp,"EXTNAME");
00351 cpl_msg_info(cpl_func, "Loop through extensions: %s", current_extension);
00352 if (strcmp(current_extension,"IMAGING_DATA")==0){
00353 ext_imaging_data=i;
00354 cpl_msg_info(cpl_func, " Extension <IMAGING_DATA> found in extension %d",ext_imaging_data);
00355 cpl_propertylist_delete(pHeader_tmp);
00356 break;
00357 }
00358 cpl_propertylist_delete(pHeader_tmp);
00359 }
00360
00361
00362 if (ext_imaging_data==0){
00363 cpl_msg_error(cpl_func, " Extension <IMAGING_DATA> NOT found");
00364 return -1;
00365 }
00366 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00367
00368
00369
00370 table = cpl_table_load(cpl_frame_get_filename(rawframe), ext_imaging_data, 1);
00371 if (table == NULL) {
00372 return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00373 "Could not load the table");
00374 }
00375 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00376
00377 cpl_msg_info(cpl_func, "Scanning for DATA1 ...");
00378 if (cpl_table_has_column(table,"DATA1")){
00379 table_to_fitsimage(frameset, parlist, "DATA1",outputfilename,table);
00380 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00381 }
00382 else {
00383 cpl_msg_info(cpl_func, "DATA1 not found");
00384 }
00385
00386 cpl_msg_info(cpl_func, "Scanning for DATA2 ...");
00387 if (cpl_table_has_column(table,"DATA2")){
00388 table_to_fitsimage(frameset, parlist, "DATA2",outputfilename,table);
00389 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00390 }
00391 else {
00392 cpl_msg_info(cpl_func, "DATA2 not found");
00393 }
00394
00395
00396 cpl_msg_info(cpl_func, "Scanning for DATA3 ...");
00397 if (cpl_table_has_column(table,"DATA3")){
00398 table_to_fitsimage(frameset, parlist, "DATA3",outputfilename,table);
00399 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00400 }
00401 else {
00402 cpl_msg_info(cpl_func, "DATA3 not found");
00403 }
00404
00405 cpl_msg_info(cpl_func, "Scanning for DATA4 ...");
00406 if (cpl_table_has_column(table,"DATA4")){
00407 table_to_fitsimage(frameset, parlist, "DATA4",outputfilename,table);
00408 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00409 }
00410 else {
00411 cpl_msg_info(cpl_func, "DATA4 not found");
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 cpl_table_delete(table);
00424
00425 return (int)cpl_error_get_code();
00426 }
00427