sinfo_utl_skymap.c

00001 /* $Id: sinfo_utl_skymap.c,v 1.13 2009/01/30 14:56:12 amodigli 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: amodigli $
00023  * $Date: 2009/01/30 14:56:12 $
00024  * $Revision: 1.13 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 /* cpl */
00037 #include <cpl.h>
00038 /* irplib */
00039 #include <irplib_utils.h>
00040 
00041 /* sinfoni */
00042 #include <sinfo_tpl_utils.h>
00043 #include <sinfo_pfits.h>
00044 #include <sinfo_tpl_dfs.h>
00045 #include <sinfo_raw_types.h>
00046 #include <sinfo_pro_types.h>
00047 #include <sinfo_functions.h>
00048 #include <sinfo_key_names.h>
00049 #include <sinfo_msg.h>
00050 #include <sinfo_error.h>
00051 #include <sinfo_utils_wrappers.h>
00052 /*-----------------------------------------------------------------------------
00053                             Functions prototypes
00054  ----------------------------------------------------------------------------*/
00055 
00056 static int sinfo_utl_skymap_create(cpl_plugin *) ;
00057 static int sinfo_utl_skymap_exec(cpl_plugin *) ;
00058 static int sinfo_utl_skymap_destroy(cpl_plugin *) ;
00059 static int sinfo_utl_skymap(cpl_parameterlist *, cpl_frameset *) ;
00060 /*-----------------------------------------------------------------------------
00061                             Static variables
00062  ----------------------------------------------------------------------------*/
00063 
00064 static char sinfo_utl_skymap_description[] =
00065 "This recipe flags as bad pixels sky lines.\n"
00066 "Input are sky frames with tag SKY\n"
00067 "Output image is called out_skymap.fits\n"
00068 "Information on relevant parameters can be found with\n"
00069 "esorex --params sinfo_utl_skymap\n"
00070 "esorex --help sinfo_utl_skymap\n"
00071 "\n";
00072 
00073 /*-----------------------------------------------------------------------------
00074                                 Functions code
00075  ----------------------------------------------------------------------------*/
00076 /*---------------------------------------------------------------------------*/
00080 /*---------------------------------------------------------------------------*/
00081 
00083 /*---------------------------------------------------------------------------*/
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     cpl_plugin_init(plugin,
00098                     CPL_PLUGIN_API,
00099                     SINFONI_BINARY_VERSION,
00100                     CPL_PLUGIN_TYPE_RECIPE,
00101                     "sinfo_utl_skymap",
00102                     "Flags sky lines as bad pixels, with map generation",
00103                     sinfo_utl_skymap_description,
00104                     "Andrea Modigliani",
00105                     "Andrea.Modigliani@eso.org",
00106                     sinfo_get_license(),
00107                     sinfo_utl_skymap_create,
00108                     sinfo_utl_skymap_exec,
00109                     sinfo_utl_skymap_destroy) ;
00110 
00111     cpl_pluginlist_append(list, plugin) ;
00112     
00113     return 0;
00114 }
00115 
00116 /*---------------------------------------------------------------------------*/
00125 /*---------------------------------------------------------------------------*/
00126 static int sinfo_utl_skymap_create(cpl_plugin * plugin)
00127 {
00128     cpl_recipe      * recipe ;
00129     cpl_parameter   * p ;
00130 
00131     /* Get the recipe out of the plugin */
00132     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00133         recipe = (cpl_recipe *)plugin ;
00134     else return -1 ;
00135 
00136     /* Create the parameters list in the cpl_recipe object */
00137     recipe->parameters = cpl_parameterlist_new() ; 
00138 
00139     /* Fill the parameters list */
00140 
00141 
00142     /* --doubleopt */
00143     p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.xsize", 
00144             CPL_TYPE_INT, "X box size", "sinfoni.sinfo_utl_skymap", 1,1,2047) ;
00145     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xsize") ;
00146     cpl_parameterlist_append(recipe->parameters, p) ;
00147 
00148     /* --doubleopt */
00149     p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.ysize", 
00150             CPL_TYPE_INT, "Y box size", "sinfoni.sinfo_utl_skymap", 30,1,2047) ;
00151     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ysize") ;
00152     cpl_parameterlist_append(recipe->parameters, p) ;
00153 
00154 
00155 
00156     /* --doubleopt */
00157     p = cpl_parameter_new_value("sinfoni.sinfo_utl_skymap.threshold", 
00158             CPL_TYPE_DOUBLE, "Threshold", "sinfoni.sinfo_utl_skymap", 30.) ;
00159     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "thresh") ;
00160     cpl_parameterlist_append(recipe->parameters, p) ;
00161 
00162     /* Return */
00163     return 0;
00164 }
00165 
00166 /*---------------------------------------------------------------------------*/
00172 /*---------------------------------------------------------------------------*/
00173 static int sinfo_utl_skymap_exec(cpl_plugin * plugin)
00174 {
00175     cpl_recipe  *   recipe ;
00176     int code=0;
00177     cpl_errorstate initial_errorstate = cpl_errorstate_get();
00178 
00179     /* Get the recipe out of the plugin */
00180     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00181         recipe = (cpl_recipe *)plugin ;
00182     else return -1 ;
00183     cpl_error_reset();
00184     //irplib_reset();
00185     check_nomsg(code = sinfo_utl_skymap(recipe->parameters, recipe->frames)) ;
00186     if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00187         /* Dump the error history since recipe execution start.                
00188            At this point the recipe cannot recover from the error */           
00189         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00190     } 
00191   cleanup:
00192     return code ;
00193 }
00194 
00195 /*---------------------------------------------------------------------------*/
00201 /*---------------------------------------------------------------------------*/
00202 static int sinfo_utl_skymap_destroy(cpl_plugin * plugin)
00203 {
00204     cpl_recipe  *   recipe ;
00205     
00206     /* Get the recipe out of the plugin */
00207     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00208         recipe = (cpl_recipe *)plugin ;
00209     else return -1 ;
00210 
00211     cpl_parameterlist_delete(recipe->parameters) ; 
00212     return 0 ;
00213 }
00214 
00215 static int sinfo_utl_skymap(
00216         cpl_parameterlist   *   parlist, 
00217         cpl_frameset        *   framelist)
00218 {
00219     cpl_parameter       *   param =NULL;
00220     const char *            name_i=NULL;
00221     int                    xsize=0;
00222     int                    ysize=0;
00223 
00224     cpl_frame           *   sky_frm=NULL;
00225 
00226     const char          *   name_o=NULL ;
00227     cpl_propertylist    *   plist =NULL;
00228     cpl_frame           *   product_frame=NULL;
00229     cpl_frameset        *   sky_set=NULL;
00230     cpl_image           *   sky_ima=NULL;
00231     cpl_image           *   sky_map=NULL;
00232     double threshold=0;
00233     int i=0;
00234     int j=0;
00235     double sinfo_median=0;
00236     float* sky_ima_pix=NULL;
00237     float* sky_map_pix=NULL;
00238     int nx=0;
00239     int ny=0;
00240     int n=0;
00241   
00242     /* HOW TO RETRIEVE INPUT PARAMETERS */
00243     /* --stropt */
00244 
00245       sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00246         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00247 
00248     param = cpl_parameterlist_find(parlist, 
00249                                    "sinfoni.sinfo_utl_skymap.out_filename");
00250     name_o = "out_skymap.fits";
00251 
00252     /* --intopt */
00253     param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.xsize");
00254     xsize = cpl_parameter_get_int(param) ;
00255 
00256     /* --intopt */
00257     param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.ysize");
00258     ysize = cpl_parameter_get_int(param) ;
00259   
00260     param = cpl_parameterlist_find(parlist,
00261                    "sinfoni.sinfo_utl_skymap.threshold");
00262     threshold = cpl_parameter_get_double(param) ;
00263  
00264     /* Identify the RAW and CALIB frames in the input frameset */
00265     if (sinfo_dfs_set_groups(framelist)) {
00266         sinfo_msg_error("Cannot identify RAW and CALIB frames") ;
00267         return -1 ;
00268     }
00269     
00270     /* HOW TO ACCESS INPUT DATA */
00271     n=cpl_frameset_get_size(framelist);
00272     if(n<1) {
00273       sinfo_msg_error("Empty input frame list!");
00274       return -1;
00275     }
00276     sky_set=cpl_frameset_new();
00277     sinfo_extract_frames_type(framelist,sky_set,RAW_SKY);
00278 
00279     n=cpl_frameset_get_size(framelist);
00280     if(n<1) {
00281       sinfo_msg_error("No sky frames in input list!");
00282       sinfo_free_frameset(&sky_set);
00283       return -1;
00284     }
00285 
00286 
00287     check_nomsg(sky_frm = cpl_frameset_get_frame(sky_set,0));
00288 
00289    if ((plist=cpl_propertylist_load(cpl_frame_get_filename(sky_frm), 
00290                     0)) == NULL) {
00291         sinfo_msg_error("Cannot read the FITS header") ;
00292         return -1 ;
00293     }
00294   
00295 
00296     name_i=cpl_frame_get_filename(sky_frm);
00297     sky_ima = cpl_image_load(name_i,CPL_TYPE_FLOAT,0,0);
00298     sky_map=cpl_image_duplicate(sky_ima);
00299     sky_ima_pix=cpl_image_get_data(sky_ima);
00300     sky_map_pix=cpl_image_get_data(sky_map);
00301     nx = cpl_image_get_size_x(sky_ima);
00302     ny = cpl_image_get_size_y(sky_ima);
00303     if (nx != SIZEX || ny != SIZEY) {
00304        sinfo_msg_error("nx=%d ny=%d, expected nx=%d ny=%d",nx,ny,SIZEX,SIZEY);
00305        goto cleanup;
00306 
00307     }
00308     for(i=1;i<nx;i++) {
00309 
00310        for(j=ysize+1;j<ny-ysize;j++) {
00311 
00312           sinfo_median=cpl_image_get_median_window(sky_ima,i,j-ysize,i,j+xsize);
00313           if(cpl_error_get_code() != CPL_ERROR_NONE) {
00314              sinfo_msg_error("Exit");
00315              sinfo_free_image(&sky_ima);
00316              sinfo_free_image(&sky_map);
00317              sinfo_free_propertylist(&plist);
00318              sinfo_free_frameset(&sky_set);
00319              return -1;
00320       }
00321       if(sky_ima_pix[i+j*nx] > sinfo_median+threshold) {
00322         sky_map_pix[i+j*nx]=0.;
00323       } else {
00324         sky_map_pix[i+j*nx]=1.;
00325       }
00326        }
00327 
00328     }
00329 
00330 
00331 
00332     for(i=1;i<nx;i++) {
00333 
00334        for(j=0;j<ysize+1;j++) {
00335 
00336              sky_map_pix[i+j*nx]=0.;
00337 
00338        }
00339 
00340        for(j=ny-ysize+1;j<ny;j++) {
00341 
00342              sky_map_pix[i+j*nx]=0.;
00343 
00344        }
00345 
00346     }
00347 
00348 
00349 
00350     /* Now performing the data reduction */
00351     /* Let's generate one image for the example */
00352 
00353 
00354     
00355     /* HOW TO SAVE A PRODUCT ON DISK  */
00356     /* Set the file name */
00357     name_o = "sky_map.fits" ;
00358 
00359     /* Create product frame */
00360     product_frame = cpl_frame_new();
00361     cpl_frame_set_filename(product_frame, name_o) ;
00362     cpl_frame_set_tag(product_frame, PRO_SKY_DUMMY) ;
00363     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00364     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00365     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00366 
00367     if (cpl_error_get_code()) {
00368         sinfo_msg_error("Error while initialising the product frame") ;
00369         sinfo_free_propertylist(&plist) ;
00370         sinfo_free_frame(&product_frame) ;
00371         return -1 ;
00372     }
00373 
00374     /* Add DataFlow keywords */
00375 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)     
00376     if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00377                      "sinfo_utl_skymap", 
00378                      "SINFONI", KEY_VALUE_HPRO_DID,NULL) 
00379     != CPL_ERROR_NONE) {
00380         sinfo_msg_error("Problem in the product DFS-compliance") ;
00381         sinfo_free_propertylist(&plist) ;
00382         sinfo_free_frame(&product_frame) ;
00383         sinfo_free_image(&sky_ima);
00384         sinfo_free_image(&sky_map);
00385         sinfo_free_frameset(&sky_set);
00386         return -1 ;
00387     }
00388 #else
00389     if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00390                      "sinfo_utl_skymap", 
00391                      "SINFONI", KEY_VALUE_HPRO_DID) 
00392     != CPL_ERROR_NONE) {
00393         sinfo_msg_error("Problem in the product DFS-compliance") ;
00394         sinfo_free_propertylist(&plist) ;
00395         sinfo_free_frame(&product_frame) ;
00396         sinfo_free_image(&sky_ima);
00397         sinfo_free_image(&sky_map);
00398         sinfo_free_frameset(&sky_set);
00399         return -1 ;
00400     }
00401 #endif    
00402 
00403 
00404     /* Save the file */
00405     if (cpl_image_save(sky_map, name_o, CPL_BPP_IEEE_FLOAT, plist,
00406                        CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00407         sinfo_msg_error("Could not save product");
00408         sinfo_free_propertylist(&plist) ;
00409         sinfo_free_frame(&product_frame) ;
00410         sinfo_free_image(&sky_map) ;
00411         return -1 ;
00412     }
00413     sinfo_free_propertylist(&plist) ;
00414     sinfo_free_image(&sky_map) ;
00415     sinfo_free_image(&sky_ima) ;
00416 
00417     /* Log the saved file in the input frameset */
00418     cpl_frameset_insert(framelist, product_frame) ;
00419     sinfo_free_frameset(&sky_set);
00420    
00421 
00422 
00423   cleanup:
00424     sinfo_free_image(&sky_ima);
00425     sinfo_free_image(&sky_map);
00426     sinfo_free_propertylist(&plist);
00427     sinfo_free_frameset(&sky_set);
00428 
00429     if(  cpl_error_get_code()!=CPL_ERROR_NONE) {
00430         return -1 ;
00431     } else {
00432         return 0 ;
00433     }
00434 }

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1