GRAVI Pipeline Reference Manual 0.1.1
gravi_all_dark.c
00001 /* $Id: gravi_all_dark.c,v 1.29 2011/03/10 09:16:12 nazouaoui Exp $
00002  *
00003  * This file is part of the GRAVI 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: nazouaoui $
00023  * $Date: 2011/03/10 09:16:12 $
00024  * $Revision: 1.29 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 
00040 
00041 #include "gravi_utils.h"
00042 #include "gravi_utils.c"
00043 #include "gravi_pfits.h"
00044 #include "gravi_dfs.h"
00045 #include "gravi_calib.h"
00046 
00047 #include "gravi_data.h"
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Private function prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static int gravi_all_dark_create(cpl_plugin *);
00054 static int gravi_all_dark_exec(cpl_plugin *);
00055 static int gravi_all_dark_destroy(cpl_plugin *);
00056 static int gravi_all_dark(cpl_frameset *, const cpl_parameterlist *);
00057 
00058 /*-----------------------------------------------------------------------------
00059                             Static variables
00060  -----------------------------------------------------------------------------*/
00061 
00062 static char gravi_all_dark_description[] =
00063 "This recipe is used compute the dark median image.\n"
00064 "The description should include the required FITS-files and\n"
00065 "their associated tags, e.g.\n"
00066 "GRAVI-GRAVI_ALL_DARK-raw-file.fits " GRAVI_FT_DARK "\n"
00067 "and any optional files, e.g.\n"
00068 "GRAVI-GRAVI_ALL_DARK-flat-file.fits " DARK_FT "\n"
00069 "\n"
00070 "Additionally, it should describe functionality of the expected output."
00071 "\n";
00072 
00073 /*-----------------------------------------------------------------------------
00074                                 Function code
00075  -----------------------------------------------------------------------------*/
00076 
00077 /*----------------------------------------------------------------------------*/
00087 /*----------------------------------------------------------------------------*/
00088 int cpl_plugin_get_info(cpl_pluginlist * list)
00089 {
00090     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe );
00091     cpl_plugin  *   plugin = &recipe->interface;
00092 
00093     if (cpl_plugin_init(plugin,
00094                     CPL_PLUGIN_API,
00095                     GRAVI_BINARY_VERSION,
00096                     CPL_PLUGIN_TYPE_RECIPE,
00097                     "gravi_all_dark",
00098                     "This recipe is used compute the dark median image",
00099                     gravi_all_dark_description,
00100                     "Firstname Lastname",
00101                     PACKAGE_BUGREPORT,
00102                     gravi_get_license(),
00103                     gravi_all_dark_create,
00104                     gravi_all_dark_exec,
00105                     gravi_all_dark_destroy)) {    
00106         cpl_msg_error(cpl_func, "Plugin initialization failed");
00107         (void)cpl_error_set_where(cpl_func);                          
00108         return 1;                                               
00109     }                                                    
00110 
00111     if (cpl_pluginlist_append(list, plugin)) {                 
00112         cpl_msg_error(cpl_func, "Error adding plugin to list");
00113         (void)cpl_error_set_where(cpl_func);                         
00114         return 1;                                              
00115     }                                                          
00116     
00117     return 0;
00118 }
00119 
00120 /*----------------------------------------------------------------------------*/
00128 /*----------------------------------------------------------------------------*/
00129 static int gravi_all_dark_create(cpl_plugin * plugin)
00130 {
00131     cpl_recipe    * recipe;                                               
00132     cpl_parameter * p;
00133                                                                        
00134     /* Do not create the recipe if an error code is already set */     
00135     if (cpl_error_get_code() != CPL_ERROR_NONE) {                      
00136         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00137                       cpl_func, __LINE__, cpl_error_get_where());      
00138         return (int)cpl_error_get_code();                              
00139     }                                                                  
00140                                                                        
00141     if (plugin == NULL) {                                              
00142         cpl_msg_error(cpl_func, "Null plugin");                        
00143         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                 
00144     }                                                                  
00145                                                                        
00146     /* Verify plugin type */                                           
00147     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {       
00148         cpl_msg_error(cpl_func, "Plugin is not a recipe");             
00149         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);              
00150     }                                                                  
00151                                                                        
00152     /* Get the recipe */                                               
00153     recipe = (cpl_recipe *)plugin;                                     
00154                                                                        
00155     /* Create the parameters list in the cpl_recipe object */          
00156     recipe->parameters = cpl_parameterlist_new();                      
00157     if (recipe->parameters == NULL) {                                  
00158         cpl_msg_error(cpl_func, "Parameter list allocation failed");   
00159         cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);             
00160     }                                                                  
00161 
00162     /* Fill the parameters list */
00163 
00164     return 0;
00165 }
00166 
00167 /*----------------------------------------------------------------------------*/
00173 /*----------------------------------------------------------------------------*/
00174 static int gravi_all_dark_exec(cpl_plugin * plugin)
00175 {
00176 
00177     cpl_recipe * recipe;                                                   
00178     int recipe_status;                                                     
00179     cpl_errorstate initial_errorstate = cpl_errorstate_get();              
00180                                                                            
00181     /* Return immediately if an error code is already set */               
00182     if (cpl_error_get_code() != CPL_ERROR_NONE) {                          
00183         cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",    
00184                       cpl_func, __LINE__, cpl_error_get_where());          
00185         return (int)cpl_error_get_code();                                  
00186     }                                                                      
00187                                                                            
00188     if (plugin == NULL) {                                                  
00189         cpl_msg_error(cpl_func, "Null plugin");                            
00190         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00191     }                                                                      
00192                                                                            
00193     /* Verify plugin type */                                               
00194     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {           
00195         cpl_msg_error(cpl_func, "Plugin is not a recipe");                 
00196         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);                  
00197     }                                                                      
00198                                                                            
00199     /* Get the recipe */                                                   
00200     recipe = (cpl_recipe *)plugin;                                         
00201                                                                            
00202     /* Verify parameter and frame lists */                                 
00203     if (recipe->parameters == NULL) {
00204         cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00205         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00206     }
00207     if (recipe->frames == NULL) {                                          
00208         cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");     
00209         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);                     
00210     }                                                                      
00211                                                                            
00212     /* Invoke the recipe */                                                
00213     recipe_status = gravi_all_dark(recipe->frames, recipe->parameters);
00214                                                                            
00215     /* Ensure DFS-compliance of the products */                            
00216     if (cpl_dfs_update_product_header(recipe->frames)) {                   
00217         if (!recipe_status) recipe_status = (int)cpl_error_get_code();                         
00218     }                                                                      
00219                                                                            
00220     if (!cpl_errorstate_is_equal(initial_errorstate)) {                    
00221         /* Dump the error history since recipe execution start.            
00222            At this point the recipe cannot recover from the error */       
00223         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);          
00224     }                                                                      
00225 
00226     return recipe_status;                                                  
00227 }
00228 
00229 /*----------------------------------------------------------------------------*/
00235 /*----------------------------------------------------------------------------*/
00236 static int gravi_all_dark_destroy(cpl_plugin * plugin)
00237 {
00238     cpl_recipe * recipe;                                          
00239                                                                   
00240     if (plugin == NULL) {                                         
00241         cpl_msg_error(cpl_func, "Null plugin");                   
00242         cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);            
00243     }                                                             
00244                                                                   
00245     /* Verify plugin type */                                      
00246     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {  
00247         cpl_msg_error(cpl_func, "Plugin is not a recipe");        
00248         cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);         
00249     }                                                             
00250                                                                   
00251     /* Get the recipe */                                          
00252     recipe = (cpl_recipe *)plugin;                                
00253                                                                   
00254     cpl_parameterlist_delete(recipe->parameters);             
00255                                                                   
00256     return 0;                                                    
00257 }
00258 
00259 
00260 /*----------------------------------------------------------------------------*/
00267 /*----------------------------------------------------------------------------*/
00268 static int gravi_all_dark(cpl_frameset            * frameset,
00269                           const cpl_parameterlist * parlist)
00270 {
00271     cpl_frameset * dark_frameset = NULL, * usedframes;
00272     cpl_frame * frame;
00273     cpl_propertylist * applist, * primary_hdr;
00274     cpl_property * p;
00275     const char * input, * frame_tag;
00276     char * output;
00277     gravi_data * raw_dark, * reduced_dark;
00278     double dit, darkrms;
00279     int comp, nb_frame;
00280 
00281     /* Identify the RAW and CALIB frames in the input frameset */
00282     cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00283                         cpl_error_get_code()) ;
00284 
00285     /*  - Extract a set of frame dark */
00286     dark_frameset = gravi_frameset_extract_dark(frameset);
00287     if (cpl_frameset_is_empty(dark_frameset)) {
00288         cpl_frameset_delete(dark_frameset);
00289        /* To use this recipe the frameset must contain at least
00290         * one dark frame. */
00291        return (int)cpl_error_set_message(cpl_func,  CPL_ERROR_INVALID_TYPE,
00292                                           "No dark frame on the frameset") ;
00293     }
00294 
00295     /* Apply the treatment for each dark frame contained in the frameset */
00296     nb_frame = cpl_frameset_get_size(dark_frameset);
00297     frame = cpl_frameset_get_first(dark_frameset);
00298 
00299     for (comp = 0; comp < nb_frame; comp++){
00300 
00301         /* Find the file name and initialization of the output name */
00302         input = cpl_frame_get_filename(frame) ;
00303         output = cpl_sprintf("gravi_all_dark_0%d.fits", comp + 1);
00304 
00305         if (input == NULL) {
00306             cpl_frameset_delete(dark_frameset);
00307             cpl_free(output);
00308            /* The file name does not exist */
00309            return (int)cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
00310                                          "SOF does not have any file tagged ") ;
00311         }
00312 
00313         /* Compute the median of a set of dark frames */
00314         raw_dark = gravi_data_load(input);
00315 
00316         reduced_dark = gravi_compute_dark( raw_dark );
00317 
00318         if (cpl_error_get_code()) {
00319             gravi_data_delete(reduced_dark);
00320             gravi_data_delete(raw_dark);
00321             cpl_frameset_delete(dark_frameset);
00322             cpl_free(output);
00323 
00324             return (int)cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
00325                                       "Error while computing the reduced_dark");
00326         }
00327 
00328         primary_hdr = gravi_data_get_propertylist(reduced_dark,
00329                                                     GRAVI_PRIMARY_HDR_NAME_EXT);
00330         p = cpl_propertylist_get_property(primary_hdr, QC_MEANDARK);
00331         dit = gravi_pfits_get_dit(primary_hdr);
00332         //insname = gravi_pfits_get_insname(primary_hdr_dark);
00333         darkrms = cpl_propertylist_get_double(primary_hdr,
00334                                                         QC_DARKRMS);
00335         /* Create product frame, add DataFlow keywords, save the file, log the
00336          * saved file in the input frameset*/
00337         usedframes = cpl_frameset_new();
00338         cpl_frameset_insert(usedframes, cpl_frame_duplicate(frame));
00339         applist = cpl_propertylist_new();
00340 
00341 
00342         frame_tag = cpl_frame_get_tag(frame) ;
00343 
00344         if (!strcmp(frame_tag, GRAVI_FT_DARK))
00345             cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, DARK_FT);
00346         else if (!strcmp(frame_tag, GRAVI_SC_DARK))
00347             cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG, DARK_SC);
00348         else{
00349             gravi_data_delete(reduced_dark);
00350             gravi_data_delete(raw_dark);
00351             cpl_frameset_delete(dark_frameset);
00352             cpl_frameset_delete(usedframes);
00353             cpl_propertylist_delete(applist);
00354             cpl_free(output);
00355             return (int)cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
00356                                                    "Invalid type of dark tag");
00357         }
00358         cpl_propertylist_append_property(applist, p);
00359         cpl_propertylist_append_double(applist, QC_DARKRMS, darkrms);
00360         cpl_propertylist_append_double(applist, GRAVI_DET_DIT, dit);
00361 
00362         if (gravi_data_save(reduced_dark, frameset, output, parlist,
00363                                   usedframes, "gravi_all_dark", applist)
00364                                                           != CPL_ERROR_NONE){
00365             gravi_data_delete(reduced_dark);
00366             gravi_data_delete(raw_dark);
00367             cpl_frameset_delete(dark_frameset);
00368             cpl_frameset_delete(usedframes);
00369             cpl_propertylist_delete(applist);
00370             cpl_free(output);
00371             return (int) cpl_error_set_message(cpl_func,
00372                     CPL_ERROR_ILLEGAL_OUTPUT, "Could not save the reduced_dark"
00373                                               " on the output file");
00374         }
00375 
00376         frame = cpl_frameset_get_next(dark_frameset);
00377 
00378         gravi_data_delete(reduced_dark);
00379         gravi_data_delete(raw_dark);
00380         cpl_frameset_delete(usedframes);
00381         cpl_propertylist_delete(applist);
00382         cpl_free(output);
00383     }
00384 
00385     /* Deallocation of all variables */
00386 
00387     cpl_frameset_delete(dark_frameset);
00388     return (int)cpl_error_get_code();
00389 }
00390 
00391