sinfo_utl_skymap.c

00001 /* $Id: sinfo_utl_skymap.c,v 1.7 2006/12/19 17:01:36 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: 2006/12/19 17:01:36 $
00024  * $Revision: 1.7 $
00025  * $Name:  $
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 
00051 /*-----------------------------------------------------------------------------
00052                             Functions prototypes
00053  ----------------------------------------------------------------------------*/
00054 
00055 static int sinfo_utl_skymap_create(cpl_plugin *) ;
00056 static int sinfo_utl_skymap_exec(cpl_plugin *) ;
00057 static int sinfo_utl_skymap_destroy(cpl_plugin *) ;
00058 static int sinfo_utl_skymap(cpl_parameterlist *, cpl_frameset *) ;
00059 /*-----------------------------------------------------------------------------
00060                             Static variables
00061  ----------------------------------------------------------------------------*/
00062 
00063 static char sinfo_utl_skymap_description[] =
00064 "This recipe flags as bad pixels sky lines.\n"
00065 "Input are sky frames with tag SKY\n"
00066 "Output image is called out_skymap.fits\n"
00067 "Information on relevant parameters can be found with\n"
00068 "esorex --params sinfo_utl_skymap\n"
00069 "esorex --help sinfo_utl_skymap\n"
00070 "\n";
00071 
00072 /*-----------------------------------------------------------------------------
00073                                 Functions code
00074  ----------------------------------------------------------------------------*/
00075 /*---------------------------------------------------------------------------*/
00079 /*---------------------------------------------------------------------------*/
00080 
00082 /*---------------------------------------------------------------------------*/
00090 /*---------------------------------------------------------------------------*/
00091 int cpl_plugin_get_info(cpl_pluginlist * list)
00092 {
00093     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00094     cpl_plugin  *   plugin = &recipe->interface ;
00095 
00096     cpl_plugin_init(plugin,
00097                     CPL_PLUGIN_API,
00098                     SINFONI_BINARY_VERSION,
00099                     CPL_PLUGIN_TYPE_RECIPE,
00100                     "sinfo_utl_skymap",
00101                     "Flags sky lines as bad pixels, with map generation",
00102                     sinfo_utl_skymap_description,
00103                     "Andrea Modigliani",
00104                     "Andrea.Modigliani@eso.org",
00105                     sinfo_get_license(),
00106                     sinfo_utl_skymap_create,
00107                     sinfo_utl_skymap_exec,
00108                     sinfo_utl_skymap_destroy) ;
00109 
00110     cpl_pluginlist_append(list, plugin) ;
00111     
00112     return 0;
00113 }
00114 
00115 /*---------------------------------------------------------------------------*/
00124 /*---------------------------------------------------------------------------*/
00125 static int sinfo_utl_skymap_create(cpl_plugin * plugin)
00126 {
00127     cpl_recipe      * recipe ;
00128     cpl_parameter   * p ;
00129 
00130     /* Get the recipe out of the plugin */
00131     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00132         recipe = (cpl_recipe *)plugin ;
00133     else return -1 ;
00134 
00135     /* Create the parameters list in the cpl_recipe object */
00136     recipe->parameters = cpl_parameterlist_new() ; 
00137 
00138     /* Fill the parameters list */
00139 
00140 
00141     /* --doubleopt */
00142     p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.xsize", 
00143             CPL_TYPE_INT, "X box size", "sinfoni.sinfo_utl_skymap", 1,1,2047) ;
00144     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xsize") ;
00145     cpl_parameterlist_append(recipe->parameters, p) ;
00146 
00147     /* --doubleopt */
00148     p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.ysize", 
00149             CPL_TYPE_INT, "Y box size", "sinfoni.sinfo_utl_skymap", 30,1,2047) ;
00150     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ysize") ;
00151     cpl_parameterlist_append(recipe->parameters, p) ;
00152 
00153 
00154 
00155     /* --doubleopt */
00156     p = cpl_parameter_new_value("sinfoni.sinfo_utl_skymap.threshold", 
00157             CPL_TYPE_DOUBLE, "Threshold", "sinfoni.sinfo_utl_skymap", 30.) ;
00158     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "thresh") ;
00159     cpl_parameterlist_append(recipe->parameters, p) ;
00160 
00161     /* Return */
00162     return 0;
00163 }
00164 
00165 /*---------------------------------------------------------------------------*/
00171 /*---------------------------------------------------------------------------*/
00172 static int sinfo_utl_skymap_exec(cpl_plugin * plugin)
00173 {
00174     cpl_recipe  *   recipe ;
00175     
00176     /* Get the recipe out of the plugin */
00177     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00178         recipe = (cpl_recipe *)plugin ;
00179     else return -1 ;
00180     cpl_error_reset();
00181     irplib_reset();
00182 
00183     return sinfo_utl_skymap(recipe->parameters, recipe->frames) ;
00184 }
00185 
00186 /*---------------------------------------------------------------------------*/
00192 /*---------------------------------------------------------------------------*/
00193 static int sinfo_utl_skymap_destroy(cpl_plugin * plugin)
00194 {
00195     cpl_recipe  *   recipe ;
00196     
00197     /* Get the recipe out of the plugin */
00198     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00199         recipe = (cpl_recipe *)plugin ;
00200     else return -1 ;
00201 
00202     cpl_parameterlist_delete(recipe->parameters) ; 
00203     return 0 ;
00204 }
00205 
00206 static int sinfo_utl_skymap(
00207         cpl_parameterlist   *   parlist, 
00208         cpl_frameset        *   framelist)
00209 {
00210     cpl_parameter       *   param =NULL;
00211     const char *            name_i=NULL;
00212     int                    xsize=0;
00213     int                    ysize=0;
00214 
00215     cpl_frame           *   sky_frm=NULL;
00216 
00217     const char          *   name_o=NULL ;
00218     cpl_propertylist    *   plist =NULL;
00219     cpl_frame           *   product_frame=NULL;
00220     cpl_frameset        *   sky_set=NULL;
00221     cpl_image           *   sky_ima=NULL;
00222     cpl_image           *   sky_map=NULL;
00223     double threshold=0;
00224     int i=0;
00225     int j=0;
00226     double sinfo_median=0;
00227     float* sky_ima_pix=NULL;
00228     float* sky_map_pix=NULL;
00229     int nx=0;
00230     int ny=0;
00231     int n=0;
00232   
00233     /* HOW TO RETRIEVE INPUT PARAMETERS */
00234     /* --stropt */
00235 
00236       sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00237         SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00238 
00239     param = cpl_parameterlist_find(parlist, 
00240                                    "sinfoni.sinfo_utl_skymap.out_filename");
00241     name_o = "out_skymap.fits";
00242 
00243     /* --intopt */
00244     param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.xsize");
00245     xsize = cpl_parameter_get_int(param) ;
00246 
00247     /* --intopt */
00248     param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.ysize");
00249     ysize = cpl_parameter_get_int(param) ;
00250   
00251     param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.threshold");
00252     threshold = cpl_parameter_get_double(param) ;
00253  
00254     /* Identify the RAW and CALIB frames in the input frameset */
00255     if (sinfo_dfs_set_groups(framelist)) {
00256         sinfo_msg_error("Cannot identify RAW and CALIB frames") ;
00257         return -1 ;
00258     }
00259     
00260     /* HOW TO ACCESS INPUT DATA */
00261     n=cpl_frameset_get_size(framelist);
00262     if(n<1) {
00263       sinfo_msg_error("Empty input frame list!");
00264       return -1;
00265     }
00266     sky_set=cpl_frameset_new();
00267     sinfo_extract_frames_type(framelist,sky_set,RAW_SKY);
00268 
00269     n=cpl_frameset_get_size(framelist);
00270     if(n<1) {
00271       sinfo_msg_error("No sky frames in input list!");
00272       cpl_frameset_delete(sky_set);
00273       return -1;
00274     }
00275 
00276 
00277     sky_frm = cpl_frameset_get_frame(sky_set,0);
00278 
00279    if ((plist=cpl_propertylist_load(cpl_frame_get_filename(sky_frm), 
00280                     0)) == NULL) {
00281         sinfo_msg_error("Cannot read the FITS header") ;
00282         return -1 ;
00283     }
00284   
00285 
00286     name_i=cpl_frame_get_filename(sky_frm);
00287     sky_ima = cpl_image_load(name_i,CPL_TYPE_FLOAT,0,0);
00288     sky_map=cpl_image_duplicate(sky_ima);
00289     sky_ima_pix=cpl_image_get_data(sky_ima);
00290     sky_map_pix=cpl_image_get_data(sky_map);
00291     nx = cpl_image_get_size_x(sky_ima);
00292     ny = cpl_image_get_size_y(sky_ima);
00293 
00294     for(i=1;i<nx;i++) {
00295 
00296        for(j=ysize+1;j<ny-ysize;j++) {
00297 
00298           sinfo_median=cpl_image_get_median_window(sky_ima,i,j-ysize,i,j+xsize);
00299           if(cpl_error_get_code() != CPL_ERROR_NONE) {
00300         sinfo_msg_error("Exit");
00301         cpl_image_delete(sky_ima);
00302         cpl_image_delete(sky_map);
00303             cpl_propertylist_delete(plist);
00304         cpl_frameset_delete(sky_set);
00305             return -1;
00306       }
00307       if(sky_ima_pix[i+j*nx] > sinfo_median+threshold) {
00308         sky_map_pix[i+j*nx]=0.;
00309       } else {
00310         sky_map_pix[i+j*nx]=1.;
00311       }
00312        }
00313 
00314     }
00315 
00316 
00317 
00318     for(i=1;i<nx;i++) {
00319 
00320        for(j=0;j<ysize+1;j++) {
00321 
00322              sky_map_pix[i+j*nx]=0.;
00323 
00324        }
00325 
00326        for(j=ny-ysize+1;j<ny;j++) {
00327 
00328              sky_map_pix[i+j*nx]=0.;
00329 
00330        }
00331 
00332     }
00333 
00334 
00335 
00336     /* Now performing the data reduction */
00337     /* Let's generate one image for the example */
00338 
00339 
00340     
00341     /* HOW TO SAVE A PRODUCT ON DISK  */
00342     /* Set the file name */
00343     name_o = "sky_map.fits" ;
00344 
00345     /* Create product frame */
00346     product_frame = cpl_frame_new();
00347     cpl_frame_set_filename(product_frame, name_o) ;
00348     cpl_frame_set_tag(product_frame, PRO_SKY_DUMMY) ;
00349     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00350     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00351     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00352 
00353     if (cpl_error_get_code()) {
00354         sinfo_msg_error("Error while initialising the product frame") ;
00355         cpl_propertylist_delete(plist) ;
00356         cpl_frame_delete(product_frame) ;
00357         return -1 ;
00358     }
00359 
00360     /* Add DataFlow keywords */
00361    
00362     if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00363             "sinfo_utl_skymap", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00364         sinfo_msg_error("Problem in the product DFS-compliance") ;
00365         cpl_propertylist_delete(plist) ;
00366         cpl_frame_delete(product_frame) ;
00367         cpl_image_delete(sky_ima);
00368         cpl_image_delete(sky_map);
00369         cpl_frameset_delete(sky_set);
00370         return -1 ;
00371     }
00372     
00373 
00374 
00375     /* Save the file */
00376     if (cpl_image_save(sky_map, name_o, CPL_BPP_DEFAULT, plist,
00377                        CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00378         sinfo_msg_error("Could not save product");
00379         cpl_propertylist_delete(plist) ;
00380         cpl_frame_delete(product_frame) ;
00381         cpl_image_delete(sky_map) ;
00382         return -1 ;
00383     }
00384     cpl_propertylist_delete(plist) ;
00385     cpl_image_delete(sky_map) ;
00386     cpl_image_delete(sky_ima) ;
00387 
00388     /* Log the saved file in the input frameset */
00389     cpl_frameset_insert(framelist, product_frame) ;
00390     cpl_frameset_delete(sky_set);
00391    
00392 
00393 
00394     
00395     /* Return */
00396     if (cpl_error_get_code()) 
00397         return -1 ;
00398     else 
00399         return 0 ;
00400 }

Generated on Wed Jan 17 08:33:44 2007 for SINFONI Pipeline Reference Manual by  doxygen 1.4.4