IIINSTRUMENT Pipeline Reference Manual 0.0.1
rrrecipe_calib.c
00001 /* $Id: rrrecipe_calib.c,v 1.1 2012/09/17 13:40:08 cgarcia Exp $
00002  *
00003  * This file is part of the IIINSTRUMENT 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: cgarcia $
00023  * $Date: 2012/09/17 13:40:08 $
00024  * $Revision: 1.1 $
00025  * $Name: iiinstrument-0_0_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <string.h>
00037 #include <cpl.h>
00038 
00039 #include "iiinstrument_utils.h"
00040 #include "iiinstrument_pfits.h"
00041 #include "iiinstrument_dfs.h"
00042 
00043 /*-----------------------------------------------------------------------------
00044                             Private function prototypes
00045  -----------------------------------------------------------------------------*/
00046 
00047 static int rrrecipe_calib_create(cpl_plugin *);
00048 static int rrrecipe_calib_exec(cpl_plugin *);
00049 static int rrrecipe_calib_destroy(cpl_plugin *);
00050 static int rrrecipe_calib(cpl_frameset *, const cpl_parameterlist *);
00051 
00052 /*-----------------------------------------------------------------------------
00053                             Static variables
00054  -----------------------------------------------------------------------------*/
00055 
00056 static char rrrecipe_calib_description[] =
00057 "This example text is used to describe the recipe.\n"
00058 "The description should include the required FITS-files and\n"
00059 "their associated tags, e.g.\n"
00060 "IIINSTRUMENT-RRRECIPE-CALIB-raw-file.fits " RRRECIPE_CALIB_RAW "\n"
00061 "\n"
00062 "Additionally, it should describe functionality of the expected output."
00063 "\n";
00064 
00065 /*-----------------------------------------------------------------------------
00066                                 Function code
00067  -----------------------------------------------------------------------------*/
00068 
00069 /*----------------------------------------------------------------------------*/
00079 /*----------------------------------------------------------------------------*/
00080 int cpl_plugin_get_info(cpl_pluginlist * list)
00081 {
00082     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe );
00083     cpl_plugin  *   plugin = &recipe->interface;
00084 
00085     if (cpl_plugin_init(plugin,
00086                     CPL_PLUGIN_API,
00087                     IIINSTRUMENT_BINARY_VERSION,
00088                     CPL_PLUGIN_TYPE_RECIPE,
00089                     "rrrecipe_calib",
00090                     "Short description of rrrecipe_calib",
00091                     rrrecipe_calib_description,
00092                     "Firstname Lastname",
00093                     PACKAGE_BUGREPORT,
00094                     iiinstrument_get_license(),
00095                     rrrecipe_calib_create,
00096                     rrrecipe_calib_exec,
00097                     rrrecipe_calib_destroy)) {    
00098         cpl_msg_error(cpl_func, "Plugin initialization failed");
00099         (void)cpl_error_set_where(cpl_func);                          
00100         return 1;                                               
00101     }                                                    
00102 
00103     if (cpl_pluginlist_append(list, plugin)) {                 
00104         cpl_msg_error(cpl_func, "Error adding plugin to list");
00105         (void)cpl_error_set_where(cpl_func);                         
00106         return 1;                                              
00107     }                                                          
00108     
00109     return 0;
00110 }
00111 
00112 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00121 static int rrrecipe_calib_create(cpl_plugin * plugin)
00122 {
00123     cpl_recipe    * recipe;                                               
00124     cpl_parameter * p;
00125                                                                        
00126     /* Do not create the recipe if an error code is already set */     
00127     if (cpl_error_get_code() != CPL_ERROR_NONE) {                      
00128         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00129                       cpl_func, __LINE__, cpl_error_get_where());      
00130         return (int)cpl_error_get_code();                              
00131     }                                                                  
00132                                                                        
00133     if (plugin == NULL) {                                              
00134         cpl_msg_error(cpl_func, "Null plugin");                        
00135         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                 
00136     }                                                                  
00137                                                                        
00138     /* Verify plugin type */                                           
00139     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {       
00140         cpl_msg_error(cpl_func, "Plugin is not a recipe");             
00141         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);              
00142     }                                                                  
00143                                                                        
00144     /* Get the recipe */                                               
00145     recipe = (cpl_recipe *)plugin;                                     
00146                                                                        
00147     /* Create the parameters list in the cpl_recipe object */          
00148     recipe->parameters = cpl_parameterlist_new();                      
00149     if (recipe->parameters == NULL) {                                  
00150         cpl_msg_error(cpl_func, "Parameter list allocation failed");   
00151         cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);             
00152     }                                                                  
00153 
00154     /* Fill the parameters list */
00155     /* --stropt */
00156     p = cpl_parameter_new_value("iiinstrument.rrrecipe_calib.str_option", 
00157             CPL_TYPE_STRING, "the string option", 
00158             "iiinstrument.rrrecipe_calib",NULL);
00159     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "stropt");
00160     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00161     cpl_parameterlist_append(recipe->parameters, p);
00162 
00163     /* --boolopt */
00164     p = cpl_parameter_new_value("iiinstrument.rrrecipe_calib.bool_option", 
00165             CPL_TYPE_BOOL, "a flag", "iiinstrument.rrrecipe_calib", TRUE);
00166     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "boolopt");
00167     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00168     cpl_parameterlist_append(recipe->parameters, p);
00169  
00170     return 0;
00171 }
00172 
00173 /*----------------------------------------------------------------------------*/
00179 /*----------------------------------------------------------------------------*/
00180 static int rrrecipe_calib_exec(cpl_plugin * plugin)
00181 {
00182 
00183     cpl_recipe * recipe;                                                   
00184     int recipe_status;                                                     
00185     cpl_errorstate initial_errorstate = cpl_errorstate_get();              
00186                                                                            
00187     /* Return immediately if an error code is already set */               
00188     if (cpl_error_get_code() != CPL_ERROR_NONE) {                          
00189         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",    
00190                       cpl_func, __LINE__, cpl_error_get_where());          
00191         return (int)cpl_error_get_code();                                  
00192     }                                                                      
00193                                                                            
00194     if (plugin == NULL) {                                                  
00195         cpl_msg_error(cpl_func, "Null plugin");                            
00196         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00197     }                                                                      
00198                                                                            
00199     /* Verify plugin type */                                               
00200     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {           
00201         cpl_msg_error(cpl_func, "Plugin is not a recipe");                 
00202         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);                  
00203     }                                                                      
00204                                                                            
00205     /* Get the recipe */                                                   
00206     recipe = (cpl_recipe *)plugin;                                         
00207                                                                            
00208     /* Verify parameter and frame lists */                                 
00209     if (recipe->parameters == NULL) {                                      
00210         cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00211         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00212     }                                                                      
00213     if (recipe->frames == NULL) {                                          
00214         cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");     
00215         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00216     }                                                                      
00217                                                                            
00218     /* Invoke the recipe */                                                
00219     recipe_status = rrrecipe_calib(recipe->frames, recipe->parameters);
00220                                                                            
00221     /* Ensure DFS-compliance of the products */                            
00222     if (cpl_dfs_update_product_header(recipe->frames)) {                   
00223         if (!recipe_status) recipe_status = (int)cpl_error_get_code();                         
00224     }                                                                      
00225                                                                            
00226     if (!cpl_errorstate_is_equal(initial_errorstate)) {                    
00227         /* Dump the error history since recipe execution start.            
00228            At this point the recipe cannot recover from the error */       
00229         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);          
00230     }                                                                      
00231                                                                            
00232     return recipe_status;                                                  
00233 }
00234 
00235 /*----------------------------------------------------------------------------*/
00241 /*----------------------------------------------------------------------------*/
00242 static int rrrecipe_calib_destroy(cpl_plugin * plugin)
00243 {
00244     cpl_recipe * recipe;                                          
00245                                                                   
00246     if (plugin == NULL) {                                         
00247         cpl_msg_error(cpl_func, "Null plugin");                   
00248         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);            
00249     }                                                             
00250                                                                   
00251     /* Verify plugin type */                                      
00252     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {  
00253         cpl_msg_error(cpl_func, "Plugin is not a recipe");        
00254         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);         
00255     }                                                             
00256                                                                   
00257     /* Get the recipe */                                          
00258     recipe = (cpl_recipe *)plugin;                                
00259                                                                   
00260     cpl_parameterlist_delete(recipe->parameters);             
00261                                                                   
00262     return 0;                                                    
00263 }
00264 
00265 /*----------------------------------------------------------------------------*/
00272 /*----------------------------------------------------------------------------*/
00273 static int rrrecipe_calib(cpl_frameset            * frameset,
00274                     const cpl_parameterlist * parlist)
00275 {
00276     const cpl_parameter *   param;
00277     const char          *   str_option;
00278     int                     bool_option;
00279     cpl_frameset        *   rawframes;
00280     const cpl_frame     *   firstframe;
00281     double                  qc_param;
00282     cpl_propertylist    *   plist;
00283     cpl_propertylist    *   applist;
00284     cpl_image           *   image;
00285     int                     nraw;
00286     int                     i;
00287 
00288     /* Use the errorstate to detect an error in a function that does not
00289        return an error code. */
00290     cpl_errorstate          prestate = cpl_errorstate_get();
00291 
00292     /* HOW TO RETRIEVE INPUT PARAMETERS */
00293     /* --stropt */
00294     param = cpl_parameterlist_find_const(parlist,
00295                                      "iiinstrument.rrrecipe_calib.str_option");
00296     str_option = cpl_parameter_get_string(param);
00297 
00298     /* --boolopt */
00299     param = cpl_parameterlist_find_const(parlist,
00300                                     "iiinstrument.rrrecipe_calib.bool_option");
00301     bool_option = cpl_parameter_get_bool(param);
00302   
00303     if (!cpl_errorstate_is_equal(prestate)) {
00304         return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00305                                           "Could not retrieve the input "
00306                                           "parameters");
00307     }
00308     
00309     /* Identify the RAW and CALIB frames in the input frameset */
00310     cpl_ensure_code(iiinstrument_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00311                     cpl_error_get_code());
00312  
00313     /* HOW TO ACCESS INPUT DATA */
00314     /*  - A required file */
00315     rawframes = cpl_frameset_new();
00316     nraw = 0;
00317     for (i = 0; i<cpl_frameset_get_size(frameset); i++) {
00318         const cpl_frame * current_frame;
00319         current_frame = cpl_frameset_get_frame_const(frameset, i);
00320         if(!strcmp(cpl_frame_get_tag(current_frame), RRRECIPE_CALIB_RAW)) {
00321             cpl_frame * new_frame = cpl_frame_duplicate(current_frame);
00322             cpl_frameset_insert(rawframes, new_frame);
00323             nraw++;
00324         }
00325     }
00326     if (nraw == 0) {
00327         return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00328                                           "SOF does not have any file tagged "
00329                                           "with %s", RRRECIPE_CALIB_RAW);
00330     }
00331     
00332 
00333     /* HOW TO GET THE FIRST FRAME OF A FRAME */ 
00334     firstframe = cpl_frameset_get_first_const(rawframes);
00335 
00336     /* HOW TO GET THE VALUE OF A FITS KEYWORD */
00337     /*  - Load only DETector related keys */
00338     plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(firstframe),
00339                                          0, "ESO DET ", 0);
00340     if (plist == NULL) {
00341         /* In this case an error message is added to the error propagation */
00342         return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00343                                           "Could not read the FITS header");
00344     }
00345 
00346     qc_param = iiinstrument_pfits_get_dit(plist);
00347     cpl_propertylist_delete(plist);
00348 
00349     /* Check for a change in the CPL error state */
00350     /* - if it did change then propagate the error and return */
00351     cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
00352     
00353     /* NOW PERFORMING THE DATA REDUCTION */
00354     /* Let's just load an image for the example */
00355     image = cpl_image_load(cpl_frame_get_filename(firstframe), CPL_TYPE_FLOAT, 0,
00356                            0);
00357     if (image == NULL) {
00358         return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
00359                                      "Could not load the image");
00360     }
00361 
00362     applist = cpl_propertylist_new();
00363 
00364     /* Add the product category  */
00365     cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
00366                                    RRRECIPE_XXX_PROCATG);
00367 
00368     /* Add a QC parameter  */
00369     cpl_propertylist_append_double(applist, "ESO QC QCPARAM", qc_param);
00370     
00371     /* HOW TO SAVE A DFS-COMPLIANT PRODUCT TO DISK  */
00372     if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
00373                            CPL_BPP_IEEE_FLOAT, "rrrecipe_calib", applist,
00374                            NULL, PACKAGE "/" PACKAGE_VERSION,
00375                            "rrrecipe_calib.fits")) {
00376         /* Propagate the error */
00377         (void)cpl_error_set_where(cpl_func);
00378     }
00379 
00380     cpl_image_delete(image);
00381     cpl_propertylist_delete(applist);
00382 
00383     return (int)cpl_error_get_code();
00384 }