sinfo_utl_table_ex.c

00001 /* $Id: sinfo_utl_table_ex.c,v 1.1 2009/05/20 15:22:42 kmirny Exp $
00002  *
00003  * This file is part of the SINFONI 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: kmirny $
00023  * $Date: 2009/05/20 15:22:42 $
00024  * $Revision: 1.1 $
00025  * $Name: sinfo-2_2_5 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 #include <string.h>
00036 
00037 /* cpl */
00038 #include <cpl.h>
00039 
00040 /* irplib */
00041 #include <irplib_utils.h>
00042 
00043 #include <sinfo_tpl_utils.h>
00044 #include <sinfo_pfits.h>
00045 #include <sinfo_tpl_dfs.h>
00046 #include <sinfo_key_names.h>
00047 #include <sinfo_pro_types.h>
00048 #include <sinfo_functions.h>
00049 #include <sinfo_msg.h>
00050 #include <sinfo_error.h>
00051 #include <sinfo_utils_wrappers.h>
00052 
00053 /*-----------------------------------------------------------------------------
00054                             Functions prototypes
00055  ----------------------------------------------------------------------------*/
00056 
00057 static int sinfo_utl_table_ex_create(cpl_plugin *) ;
00058 static int sinfo_utl_table_ex_exec(cpl_plugin *) ;
00059 static int sinfo_utl_table_ex_destroy(cpl_plugin *) ;
00060 static int sinfo_utl_table_ex(cpl_parameterlist *, cpl_frameset *) ;
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Static variables
00064  ----------------------------------------------------------------------------*/
00065 
00066 static char sinfo_utl_table_ex_description[] =
00067 "This recipe produce a table,\n";
00068 
00069 /*static char PARAM_NAME_SIZE_X[] = "sinfoni.sinfo_utl_table_ex.size_x";
00070 static char PARAM_NAME_SIZE_Y[] = "sinfoni.sinfo_utl_table_ex.size_y";
00071 static char PARAM_NAME_FWHM_X[] = "sinfoni.sinfo_utl_table_ex.fwhm_x";
00072 static char PARAM_NAME_FWHM_Y[] = "sinfoni.sinfo_utl_table_ex.fwhm_y";
00073 static char PARAM_NAME_SIGNAL[] = "sinfoni.sinfo_utl_table_ex.total_signal";
00074 static char PARAM_NAME_BKGSIG[] = "sinfoni.sinfo_utl_table_ex.bkg_signal";
00075 static char PARAM_NAME_NOISEL[] = "sinfoni.sinfo_utl_table_ex.noise_level";
00076 */
00077 static char RECIPE_NAME[] = "sinfoni.sinfo_utl_table_ex";
00078 static char CLMN_NAME_SPECTRUM[] = "spectrum";
00079 static char CLMN_NAME_WAVELENGTH[] = "wavelength";
00080 static char CLMN_NAME_BKG_TOT[] = "bkg_tot";
00081 static char CLMN_NAME_COUNTS_BKG[] = "counts_bkg";
00082 /*-----------------------------------------------------------------------------
00083                                 Functions code
00084  ----------------------------------------------------------------------------*/
00085 /*---------------------------------------------------------------------------*/
00089 /*---------------------------------------------------------------------------*/
00090 
00092 /*---------------------------------------------------------------------------*/
00100 /*---------------------------------------------------------------------------*/
00101 int cpl_plugin_get_info(cpl_pluginlist * list)
00102 {
00103     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00104     cpl_plugin  *   plugin = &recipe->interface ;
00105 
00106     cpl_plugin_init(plugin,
00107                     CPL_PLUGIN_API,
00108                     SINFONI_BINARY_VERSION,
00109                     CPL_PLUGIN_TYPE_RECIPE,
00110                     "sinfo_utl_table_ex",
00111                     "Test example shows table manipulating utilities",
00112                     sinfo_utl_table_ex_description,
00113                     "Konstantin Mirny",
00114                     "kmirny@eso.org",
00115                     sinfo_get_license(),
00116                     sinfo_utl_table_ex_create,
00117                     sinfo_utl_table_ex_exec,
00118                     sinfo_utl_table_ex_destroy) ;
00119 
00120     cpl_pluginlist_append(list, plugin) ;
00121 
00122     return 0;
00123 }
00124 
00125 /*---------------------------------------------------------------------------*/
00134 /*---------------------------------------------------------------------------*/
00135 static int sinfo_utl_table_ex_create(cpl_plugin * plugin)
00136 {
00137     cpl_recipe      * recipe ;
00138 //    cpl_parameter   * p ;
00139 
00140     /* Get the recipe out of the plugin */
00141     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00142         recipe = (cpl_recipe *)plugin ;
00143     else return -1 ;
00144     cpl_error_reset();
00145     irplib_reset();
00146 
00147     /* Create the parameters list in the cpl_recipe object */
00148     recipe->parameters = cpl_parameterlist_new() ;
00149 
00150     /* Fill the parameters list */
00151     /* --stropt */
00152 /*    p = cpl_parameter_new_value("sinfoni.sinfo_utl_table_ex.op",
00153                                 CPL_TYPE_STRING,
00154                                 "A possible operation",
00155                                 "sinfoni.sinfo_utl_table_ex","+");
00156     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00157     cpl_parameterlist_append(recipe->parameters, p) ;*/
00158 
00159     /* --size_x */
00160 /*    p = cpl_parameter_new_value(PARAM_NAME_SIZE_X,
00161             CPL_TYPE_INT, "size X axis", RECIPE_NAME, SIZE_X_DEFAULT) ;
00162     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "size_x") ;
00163     cpl_parameterlist_append(recipe->parameters, p) ;
00164 */
00165     /* Return */
00166     return 0;
00167 }
00168 
00169 /*---------------------------------------------------------------------------*/
00175 /*---------------------------------------------------------------------------*/
00176 static int sinfo_utl_table_ex_exec(cpl_plugin * plugin)
00177 {
00178     cpl_recipe  *   recipe ;
00179      int code=0;
00180      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00181 
00182     /* Get the recipe out of the plugin */
00183     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00184         recipe = (cpl_recipe *)plugin ;
00185     else return -1 ;
00186     cpl_error_reset();
00187     irplib_reset();
00188     code = sinfo_utl_table_ex(recipe->parameters, recipe->frames) ;
00189 
00190 
00191     if (!cpl_errorstate_is_equal(initial_errorstate)) {
00192         /* Dump the error history since recipe execution start.
00193            At this point the recipe cannot recover from the error */
00194         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00195     }
00196 
00197     return code ;
00198 }
00199 
00200 /*---------------------------------------------------------------------------*/
00206 /*---------------------------------------------------------------------------*/
00207 static int sinfo_utl_table_ex_destroy(cpl_plugin * plugin)
00208 {
00209     cpl_recipe  *   recipe ;
00210 
00211     /* Get the recipe out of the plugin */
00212     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00213         recipe = (cpl_recipe *)plugin ;
00214     else return -1 ;
00215 
00216     cpl_parameterlist_delete(recipe->parameters) ;
00217     return 0 ;
00218 }
00219 
00220 /*---------------------------------------------------------------------------*/
00227 /*---------------------------------------------------------------------------*/
00228 static int
00229 sinfo_utl_table_ex( cpl_parameterlist   *   parlist,
00230         cpl_frameset        *   framelist)
00231 {
00232     // parameters
00233     double range_min = 1.9;
00234     double range_max = 2.;
00236 
00237     const char          *   name_o = NULL ;
00238     const char          *   sourceTableName = NULL;
00239 //    cpl_parameter       *   param = NULL ;
00240 
00241     cpl_propertylist    *   plist = NULL ;
00242     cpl_frame           *   product_frame = NULL;
00243     int                     nrow = 0; // number of rows in the table
00244     cpl_table           *   tblSource = NULL; // source table
00245     cpl_table           *   tblResult = NULL; // result table
00246     float               *   pValues   = NULL; // source column
00247 
00248     sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00249     SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00250     ck0(sinfo_dfs_set_groups(framelist),"Cannot indentify RAW and CALIB frames");
00251 
00252 /* HOW TO RETRIEVE INPUT PARAMETERS */
00253       /* --size_x */
00254 //    check_nomsg(param=cpl_parameterlist_find(parlist,
00255 //                                             PARAM_NAME_SIZE_X));
00256 //    check_nomsg(iSizeX=cpl_parameter_get_int(param));
00257 
00258 
00259 
00260 
00261     /* HOW TO GET THE VALUE OF A FITS KEYWORD */
00262     check(plist=cpl_propertylist_new(),"Cannot create a Property List");
00263 
00264     /* Now performing the data reduction */
00265     /* Let's generate one image for the example */
00266 
00267     // get the source table
00268     check(product_frame = cpl_frameset_get_first(framelist),"Cannot get product frame");
00269     check(sourceTableName = cpl_frame_get_filename(product_frame),"Cannot get source table name");
00270     check_nomsg(tblSource = cpl_table_load(sourceTableName, 1, 0));
00271     // generate result table
00272     // 1. duplicate the table
00273     check(tblResult = cpl_table_duplicate(tblSource), "cannot duplicate table");
00274     // 2. Cycle through the table - select only predefined interval
00275     check_nomsg(nrow = cpl_table_get_nrow(tblResult));
00276     sinfo_msg("%d rows in the source table", nrow);
00277     if (nrow)
00278     {
00279         int i = 0;
00280         int m = 0;
00281         check_nomsg(pValues = cpl_table_get_data_float(tblResult, CLMN_NAME_WAVELENGTH));
00282         sinfo_msg("checking wavelenghts...");
00283         check(cpl_table_unselect_all(tblResult),"cannot unselect all");
00284         for (i = 0; i < nrow; ++i)
00285         {
00286             if ((*(pValues + i) < range_min) || (*(pValues + i) > range_max))
00287             {
00288                 // mark for removing
00289                 check(cpl_table_select_row(tblResult, i), "Cannot select a row in a table");
00290                 ++m;
00291             }
00292         }
00293         sinfo_msg("%d row(s) would be removed from the result", m);
00294         // delete select rows from the table
00295         check(cpl_table_erase_selected(tblResult),"Cannot delete selected rows from a table");
00296     }
00297     // 3. create a new column for the result table
00298     // 4. Fill in the new column with values
00299     check(cpl_table_duplicate_column(tblResult,CLMN_NAME_SPECTRUM, tblResult, CLMN_NAME_BKG_TOT),"Unable to create new column in the table");
00300     check(cpl_table_add_columns(tblResult, CLMN_NAME_SPECTRUM, CLMN_NAME_COUNTS_BKG), "Error during sum the columns");
00301 
00302     /* Set the file name */
00303     name_o = "ima_res.fits" ;
00304     /* Create product frame */
00305 
00306     check_nomsg(product_frame = cpl_frame_new());
00307     check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00308     check_nomsg(cpl_frame_set_tag(product_frame,"table_example" )) ;
00309     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE)) ;
00310     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00311     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00312       "Error while initialising the product frame") ;
00313 
00314     /* Add DataFlow keywords */
00315     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00316 //    cpl_frameset_dump(framelist, stdout);
00317     check(cpl_dfs_setup_product_header(plist,
00318                                        product_frame,
00319                                        framelist,
00320                                        parlist,
00321                                        "sinfo_utl_table_ex",
00322                                        "SINFONI",
00323                                        KEY_VALUE_HPRO_DID,NULL),
00324       "Problem in the product DFS-compliance") ;
00325 
00326 
00327     /* Save the file */
00328     cpl_table_dump(tblResult, 0, 3, stdout);
00329     check(cpl_table_save(tblResult,
00330                         NULL, NULL,
00331                          name_o,
00332                          CPL_IO_DEFAULT), "Cannot save table");
00333 
00334     sinfo_free_propertylist(&plist) ;
00335     /* Log the saved file in the input frameset */
00336     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00337 
00338 
00339  cleanup:
00340 
00341     sinfo_free_propertylist(&plist) ;
00342     cpl_table_delete(tblSource);
00343     cpl_table_delete(tblResult);
00344     /* This is usually freed by esorex: but what about if errors occurs?
00345     sinfo_free_frame(&product_frame) ;
00346     */
00347 
00348 
00349     return ((cpl_error_get_code()) ? -1 : 0 );
00350 
00351 }

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1