midi_raw_to_fitsimage.c

00001 /* $Id: midi_raw_to_fitsimage.c,v 1.8 2010/05/28 09:16:01 agabasch Exp $
00002  *
00003  * This file is part of the MIDI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: agabasch $
00023  * $Date: 2010/05/28 09:16:01 $
00024  * $Revision: 1.8 $
00025  * $Name: midi-2_8_2 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
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                             Private function prototypes
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                             Static variables
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                                 Function code
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     /* Do not create the recipe if an error code is already set */     
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     /* Verify plugin type */                                           
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     /* Get the recipe */                                               
00157     recipe = (cpl_recipe *)plugin;                                     
00158                                                                        
00159     /* Create the parameters list in the cpl_recipe object */          
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     /* Fill the parameters list */
00167     /* --stropt */
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 /*     /\* --boolopt *\/ */
00175 /*     p = cpl_parameter_new_value("midi.midi_raw_to_fitsimage.bool_option",  */
00176 /*             CPL_TYPE_BOOL, "a flag", "midi.midi_raw_to_fitsimage", TRUE); */
00177 /*     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt"); */
00178 /*     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV); */
00179 /*     cpl_parameterlist_append(recipe->parameters, p); */
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     /* Return immediately if an error code is already set */               
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     /* Verify plugin type */                                               
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     /* Get the recipe */                                                   
00217     recipe = (cpl_recipe *)plugin;                                         
00218                                                                            
00219     /* Verify parameter and frame lists */                                 
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     /* Invoke the recipe */                                                
00230     recipe_status = midi_raw_to_fitsimage(recipe->frames, recipe->parameters);
00231                                                                            
00232     /* Ensure DFS-compliance of the products */                            
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         /* Dump the error history since recipe execution start.            
00239            At this point the recipe cannot recover from the error */       
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     /* Verify plugin type */                                      
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     /* Get the recipe */                                          
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     /* Use the errorstate to detect an error in a function that does not
00298        return an error code. */
00299     cpl_errorstate          prestate = cpl_errorstate_get();
00300 
00301 
00302 
00303     /* HOW TO RETRIEVE INPUT PARAMETERS */
00304     /* --stropt */
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     /* Identify the RAW and CALIB frames in the input frameset */
00316     cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00317                     cpl_error_get_code());
00318  
00319     /* HOW TO ACCESS INPUT DATA */
00320     /*  - A required file */
00321     rawframe = cpl_frameset_get_first(frameset);
00322 /*     rawframe = cpl_frameset_find_const(frameset, MIDI_ACQ); */
00323     if (rawframe == NULL) {
00324         /* cpl_frameset_find_const() does not set an error code, when a frame
00325            is not found, so we will set one here. */
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     /* Check for a change in the CPL error state */
00332     /* - if it did change then propagate the error and return */
00333     cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00334     
00335     /* NOW PERFORMING THE DATA REDUCTION */
00336 
00337 
00338     /* read the number of extensions from the rawfile */
00339     extnum=cpl_frame_get_nextensions(rawframe);
00340     cpl_msg_info(cpl_func, "Number of extensions found: %d",extnum); 
00341 
00342 
00343     /* Loop through the extension and find Extension "IMAGING_DATA" */
00344 
00345     ext_imaging_data=0;
00346 /*     pHeader_tmp = cpl_propertylist_new();   */
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     /* Load extension  Imaging Data  */ 
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 /* Free the Memory */
00418 
00419 /*      Not to be freed at the moment !!!!!!!*/
00420 /*     cpl_imagelist_delete(imagelist_data1); */
00421 /*     cpl_imagelist_delete(imagelist_data2); */
00422 
00423     cpl_table_delete(table);
00424 
00425     return (int)cpl_error_get_code();
00426 }
00427 

Generated on 5 Mar 2013 for MIDI Pipeline Reference Manual by  doxygen 1.6.1