sinfo_utl_ima_gauss.c

00001 /* $Id: sinfo_utl_ima_gauss.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_ima_gauss_create(cpl_plugin *) ;
00058 static int sinfo_utl_ima_gauss_exec(cpl_plugin *) ;
00059 static int sinfo_utl_ima_gauss_destroy(cpl_plugin *) ;
00060 static int sinfo_utl_ima_gauss(cpl_parameterlist *, cpl_frameset *) ;
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Static variables
00064  ----------------------------------------------------------------------------*/
00065 
00066 static char sinfo_utl_ima_gauss_description[] =
00067 "This recipe produce an image,\n"
00068 "containing a 2D Gaussian.\n"
00069 "Information on relevant parameters can be found with\n"
00070 "esorex --params sinfo_utl_ima_gauss\n"
00071 "esorex --help sinfo_utl_ima_gauss\n"
00072 "\n";
00073 
00074 static char PARAM_NAME_SIZE_X[] = "sinfoni.sinfo_utl_ima_gauss.size_x";
00075 static char PARAM_NAME_SIZE_Y[] = "sinfoni.sinfo_utl_ima_gauss.size_y";
00076 static char PARAM_NAME_FWHM_X[] = "sinfoni.sinfo_utl_ima_gauss.fwhm_x";
00077 static char PARAM_NAME_FWHM_Y[] = "sinfoni.sinfo_utl_ima_gauss.fwhm_y";
00078 static char PARAM_NAME_SIGNAL[] = "sinfoni.sinfo_utl_ima_gauss.total_signal";
00079 static char PARAM_NAME_BKGSIG[] = "sinfoni.sinfo_utl_ima_gauss.bkg_signal";
00080 static char PARAM_NAME_NOISEL[] = "sinfoni.sinfo_utl_ima_gauss.noise_level";
00081 
00082 static char RECIPE_NAME[] = "sinfoni.sinfo_utl_ima_gauss";
00083 
00084 /*-----------------------------------------------------------------------------
00085                                 Functions code
00086  ----------------------------------------------------------------------------*/
00087 /*---------------------------------------------------------------------------*/
00091 /*---------------------------------------------------------------------------*/
00092 
00094 /*---------------------------------------------------------------------------*/
00102 /*---------------------------------------------------------------------------*/
00103 int cpl_plugin_get_info(cpl_pluginlist * list)
00104 {
00105     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00106     cpl_plugin  *   plugin = &recipe->interface ;
00107 
00108     cpl_plugin_init(plugin,
00109                     CPL_PLUGIN_API,
00110                     SINFONI_BINARY_VERSION,
00111                     CPL_PLUGIN_TYPE_RECIPE,
00112                     "sinfo_utl_ima_gauss",
00113                     "Produce an image containing 2D Gaussian",
00114                     sinfo_utl_ima_gauss_description,
00115                     "Konstantin Mirny",
00116                     "kmirny@eso.org",
00117                     sinfo_get_license(),
00118                     sinfo_utl_ima_gauss_create,
00119                     sinfo_utl_ima_gauss_exec,
00120                     sinfo_utl_ima_gauss_destroy) ;
00121 
00122     cpl_pluginlist_append(list, plugin) ;
00123 
00124     return 0;
00125 }
00126 
00127 /*---------------------------------------------------------------------------*/
00136 /*---------------------------------------------------------------------------*/
00137 static int sinfo_utl_ima_gauss_create(cpl_plugin * plugin)
00138 {
00139     const int SIZE_X_DEFAULT    = 1024;
00140     const int SIZE_Y_DEFAULT    = 1024;
00141     const int FWHM_X            = 100;
00142     const int FWHM_Y            = 100;
00143     const double TOTAL_SIGNAL   = 10E5;
00144     const double BKG_SIGNAL     = 400.;
00145     const double NOISE_VALUE    = 20.;
00146 
00147     cpl_recipe      * recipe ;
00148     cpl_parameter   * p ;
00149 
00150     /* Get the recipe out of the plugin */
00151     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00152         recipe = (cpl_recipe *)plugin ;
00153     else return -1 ;
00154     cpl_error_reset();
00155     irplib_reset();
00156 
00157     /* Create the parameters list in the cpl_recipe object */
00158     recipe->parameters = cpl_parameterlist_new() ;
00159 
00160     /* Fill the parameters list */
00161     /* --stropt */
00162 /*    p = cpl_parameter_new_value("sinfoni.sinfo_utl_ima_gauss.op",
00163                                 CPL_TYPE_STRING,
00164                                 "A possible operation",
00165                                 "sinfoni.sinfo_utl_ima_gauss","+");
00166     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "op") ;
00167     cpl_parameterlist_append(recipe->parameters, p) ;*/
00168 
00169     /* --size_x */
00170     p = cpl_parameter_new_value(PARAM_NAME_SIZE_X,
00171             CPL_TYPE_INT, "size X axis", RECIPE_NAME, SIZE_X_DEFAULT) ;
00172     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "size_x") ;
00173     cpl_parameterlist_append(recipe->parameters, p) ;
00174 
00175     /* --size_y */
00176     p = cpl_parameter_new_value(PARAM_NAME_SIZE_Y,
00177             CPL_TYPE_INT, "size Y axis", RECIPE_NAME, SIZE_Y_DEFAULT) ;
00178     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "size_y") ;
00179     cpl_parameterlist_append(recipe->parameters, p) ;
00180 
00181     /* --fwhm_x */
00182     p = cpl_parameter_new_value(PARAM_NAME_FWHM_X,
00183             CPL_TYPE_INT, "FWHM X axis", RECIPE_NAME, FWHM_X) ;
00184     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fwhm_x") ;
00185     cpl_parameterlist_append(recipe->parameters, p) ;
00186 
00187     /* --fwhm_y */
00188     p = cpl_parameter_new_value(PARAM_NAME_FWHM_Y,
00189             CPL_TYPE_INT, "FWHM Y axis", RECIPE_NAME, FWHM_Y) ;
00190     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fwhm_y") ;
00191     cpl_parameterlist_append(recipe->parameters, p) ;
00192 
00193     /* --total_signal */
00194     p = cpl_parameter_new_value(PARAM_NAME_SIGNAL,
00195             CPL_TYPE_DOUBLE, "Total signal value", RECIPE_NAME, TOTAL_SIGNAL) ;
00196     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "total_signal") ;
00197     cpl_parameterlist_append(recipe->parameters, p) ;
00198 
00199     /* --bkg_signal */
00200     p = cpl_parameter_new_value(PARAM_NAME_BKGSIG,
00201             CPL_TYPE_DOUBLE, "background signal level", RECIPE_NAME, BKG_SIGNAL) ;
00202     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "bkg_signal") ;
00203     cpl_parameterlist_append(recipe->parameters, p) ;
00204 
00205     /* --noise_level */
00206     p = cpl_parameter_new_value(PARAM_NAME_NOISEL,
00207             CPL_TYPE_DOUBLE, "Noise level", RECIPE_NAME, NOISE_VALUE) ;
00208     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "noise_level") ;
00209     cpl_parameterlist_append(recipe->parameters, p) ;
00210     /* Return */
00211     return 0;
00212 }
00213 
00214 /*---------------------------------------------------------------------------*/
00220 /*---------------------------------------------------------------------------*/
00221 static int sinfo_utl_ima_gauss_exec(cpl_plugin * plugin)
00222 {
00223     cpl_recipe  *   recipe ;
00224      int code=0;
00225      cpl_errorstate initial_errorstate = cpl_errorstate_get();
00226 
00227     /* Get the recipe out of the plugin */
00228     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00229         recipe = (cpl_recipe *)plugin ;
00230     else return -1 ;
00231     cpl_error_reset();
00232     irplib_reset();
00233     code = sinfo_utl_ima_gauss(recipe->parameters, recipe->frames) ;
00234 
00235 
00236     if (!cpl_errorstate_is_equal(initial_errorstate)) {
00237         /* Dump the error history since recipe execution start.
00238            At this point the recipe cannot recover from the error */
00239         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00240     }
00241 
00242     return code ;
00243 }
00244 
00245 /*---------------------------------------------------------------------------*/
00251 /*---------------------------------------------------------------------------*/
00252 static int sinfo_utl_ima_gauss_destroy(cpl_plugin * plugin)
00253 {
00254     cpl_recipe  *   recipe ;
00255 
00256     /* Get the recipe out of the plugin */
00257     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00258         recipe = (cpl_recipe *)plugin ;
00259     else return -1 ;
00260 
00261     cpl_parameterlist_delete(recipe->parameters) ;
00262     return 0 ;
00263 }
00264 
00265 /*---------------------------------------------------------------------------*/
00272 /*---------------------------------------------------------------------------*/
00273 static int
00274 sinfo_utl_ima_gauss( cpl_parameterlist   *   parlist,
00275         cpl_frameset        *   framelist)
00276 {
00277     // parameters
00278     int     iSizeX = 0;
00279     int     iSizeY = 0;
00280     int     iFWHMX = 0;
00281     int     iFWHMY = 0;
00282     double dSignal = 0;
00283     double dBkgSignal = 0;
00284     double dNoiseLvl = 0;
00286 
00287     const char          *   name_o = NULL ;
00288     cpl_parameter       *   param = NULL ;
00289 
00290     cpl_propertylist    *   plist = NULL ;
00291     cpl_image           *   imNoise = NULL ;
00292     cpl_image           *   imGauss = NULL ;
00293     cpl_image           *   imResult = NULL;
00294     cpl_frame           *   product_frame = NULL;
00295     const double K = 2.35482;//1. / (2. * sqrt(2.* ln(2.)));
00296 
00297     sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00298     SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00299     ck0(sinfo_dfs_set_groups(framelist),"Cannot indentify RAW and CALIB frames");
00300 
00301 /* HOW TO RETRIEVE INPUT PARAMETERS */
00302       /* --size_x */
00303       check_nomsg(param=cpl_parameterlist_find(parlist,
00304                                                PARAM_NAME_SIZE_X));
00305       check_nomsg(iSizeX=cpl_parameter_get_int(param));
00306 
00307       /* --size_y */
00308       check_nomsg(param=cpl_parameterlist_find(parlist,
00309                                                PARAM_NAME_SIZE_Y));
00310       check_nomsg(iSizeY=cpl_parameter_get_int(param));
00311 
00312       /* --fwhm_x */
00313       check_nomsg(param=cpl_parameterlist_find(parlist,
00314                                                PARAM_NAME_FWHM_X));
00315       check_nomsg(iFWHMX=cpl_parameter_get_int(param));
00316 
00317       /* --fwhm_y */
00318       check_nomsg(param=cpl_parameterlist_find(parlist,
00319                                                PARAM_NAME_FWHM_Y));
00320       check_nomsg(iFWHMY=cpl_parameter_get_int(param));
00321 
00322       /* --total_signal */
00323       check_nomsg(param=cpl_parameterlist_find(parlist,
00324                                                PARAM_NAME_SIGNAL));
00325       check_nomsg(dSignal=cpl_parameter_get_double(param));
00326 
00327       /* --bkg_signal */
00328       check_nomsg(param=cpl_parameterlist_find(parlist,
00329                                                PARAM_NAME_BKGSIG));
00330       check_nomsg(dBkgSignal=cpl_parameter_get_double(param));
00331 
00332       /* --noise_level */
00333       check_nomsg(param=cpl_parameterlist_find(parlist,
00334                                                PARAM_NAME_NOISEL));
00335       check_nomsg(dNoiseLvl=cpl_parameter_get_double(param));
00336 
00337     /* Identify the RAW and CALIB frames in the input frameset */
00338 //    check(sinfo_dfs_set_groups(framelist),
00339 //         "Cannot identify RAW and CALIB frames") ;
00340 
00341     /* HOW TO ACCESS INPUT DATA */
00342 /*    n=cpl_frameset_get_size(framelist);
00343     if(n<1) {
00344       sinfo_msg_error("Empty input frame list!");
00345       goto cleanup ;
00346     }
00347 */
00348     /* HOW TO ACCESS INPUT DATA */
00349 //    check_nomsg(raw_set=cpl_frameset_new());
00350 /*
00351     check(sinfo_contains_frames_kind(framelist,raw_set,PRO_IMA),
00352      "Found no input frames with tag %s",PRO_IMA);
00353     check_nomsg(nraw=cpl_frameset_get_size(raw_set));
00354     if (nraw<1) {
00355       sinfo_msg_error("Found no input frames with tag %s",PRO_IMA);
00356       goto cleanup;
00357     } else {
00358          check_nomsg(frm_ima1=cpl_frameset_get_frame(framelist,0));
00359          check_nomsg(ima1=cpl_image_load(cpl_frame_get_filename(frm_ima1),
00360                                          CPL_TYPE_FLOAT,0,0));
00361      if (nraw>1) {
00362          check_nomsg(frm_ima2=cpl_frameset_get_frame(framelist,1));
00363          check_nomsg(ima2 = cpl_image_load(cpl_frame_get_filename(frm_ima2),
00364                                            CPL_TYPE_FLOAT,0,0));
00365          switch_ima2=1;
00366       } else if (value == 9999.) {
00367         sinfo_msg_error("Found only one input frames with tag %s",PRO_IMA);
00368         goto cleanup;
00369       } else {
00370         sinfo_msg("Produce an image %s",PRO_IMA);
00371       }
00372     }
00373 
00374     sinfo_free_frameset(&raw_set);
00375 */
00376     /* HOW TO GET THE VALUE OF A FITS KEYWORD */
00377     check(plist=cpl_propertylist_new(),"Cannot create a Property List");
00378 
00379     /* Now performing the data reduction */
00380     /* Let's generate one image for the example */
00381 
00382 
00383   check(imNoise = cpl_image_fill_test_create(iSizeX, iSizeY),
00384                 "Cannot generate the image") ;
00385   check_nomsg(cpl_image_fill_noise_uniform(imNoise, dBkgSignal - dNoiseLvl, dBkgSignal + dNoiseLvl));
00386   check(imGauss = cpl_image_fill_test_create(iSizeX, iSizeY),
00387                 "Cannot generate the image") ;
00388 //  double dfwhm_x = iFWHMX;
00389 //  double dfwhm_y = iFWHMY;
00390 //  double xcen = 0;
00391 //  double ycen = 0;
00392 //  check_nomsg(cpl_image_fit_gaussian(imNoise, iSizeX/2, iSizeY/2, 5, &dSignal, &xcen, &ycen, 0, 0, &dfwhm_y, &dfwhm_y));
00393   check(imResult = cpl_image_fill_test_create(iSizeX, iSizeY),
00394                 "Cannot generate the image") ;
00395   check_nomsg(cpl_image_fill_gaussian(imGauss, iSizeX/2, iSizeY/2, dSignal, iFWHMX / K, iFWHMY / K));
00396   check(imResult = cpl_image_add_create(imGauss, imNoise), "Cannot generate the image");
00397     /* HOW TO SAVE A PRODUCT ON DISK  */
00398     /* Set the file name */
00399     name_o = "ima_res.fits" ;
00400 
00401     /* Create product frame */
00402     check_nomsg(product_frame = cpl_frame_new());
00403     check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00404     check_nomsg(cpl_frame_set_tag(product_frame,"image_gauss" )) ;
00405     check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00406     check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00407     check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00408       "Error while initialising the product frame") ;
00409 
00410     /* Add DataFlow keywords */
00411     check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00412 //    cpl_frameset_dump(framelist, stdout);
00413     check(cpl_dfs_setup_product_header(plist,
00414                                        product_frame,
00415                                        framelist,
00416                                        parlist,
00417                                        "sinfo_utl_ima_gauss",
00418                                        "SINFONI",
00419                                        KEY_VALUE_HPRO_DID,NULL),
00420       "Problem in the product DFS-compliance") ;
00421 
00422 
00423     /* Save the file */
00424     check(cpl_image_save(imResult,
00425                          name_o,
00426                          CPL_BPP_IEEE_FLOAT,
00427                          plist,
00428                          CPL_IO_DEFAULT),
00429                          "Could not save product");
00430 
00431 
00432     sinfo_free_propertylist(&plist) ;
00433     sinfo_free_image(&imNoise);
00434     sinfo_free_image(&imGauss);
00435     sinfo_free_image(&imResult);
00436     /* Log the saved file in the input frameset */
00437     check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00438 
00439 
00440  cleanup:
00441 
00442 //    sinfo_free_frameset(&raw_set);
00443     sinfo_free_propertylist(&plist) ;
00444     /* This is usually freed by esorex: but what about if errors occurs?
00445     sinfo_free_frame(&product_frame) ;
00446     */
00447     sinfo_free_image(&imNoise) ;
00448 
00449     if (cpl_error_get_code()) {
00450         return -1 ;
00451     } else {
00452         return 0 ;
00453     }
00454 
00455 }

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