si_utl_skymap.c

00001 /* $Id: si_utl_skymap.c,v 1.3 2005/10/14 13:07:57 amodigli Exp $
00002  *
00003  * This file is part of the IIINSTRUMENT 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: 2005/10/14 13:07:57 $
00024  * $Revision: 1.3 $
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  
00039 /* sinfoni */
00040 #include "sinfoni_tpl_utils.h"
00041 #include "sinfoni_pfits.h"
00042 #include "sinfoni_tpl_dfs.h"
00043 #include "sinfoni_raw_types.h"
00044 #include "sinfoni_pro_types.h"
00045 #include "sinfoni_functions.h"
00046 #include <sinfoni_key_names.h>
00047 #include <sinfoni_memory.h>
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Functions prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static int si_utl_skymap_create(cpl_plugin *) ;
00054 static int si_utl_skymap_exec(cpl_plugin *) ;
00055 static int si_utl_skymap_destroy(cpl_plugin *) ;
00056 static int si_utl_skymap(cpl_parameterlist *, cpl_frameset *) ;
00057 /*-----------------------------------------------------------------------------
00058                             Static variables
00059  -----------------------------------------------------------------------------*/
00060 
00061 static char si_utl_skymap_description[] =
00062 "This recipe flags as bad pixels sky lines.\n"
00063 "Input are sky frames with tag SKY\n"
00064 "Output image is called out_skymap.fits\n"
00065 "Information on relevant parameters can be found with\n"
00066 "esorex --params si_utl_skymap\n"
00067 "esorex --help si_utl_skymap\n"
00068 "\n";
00069 
00070 /*-----------------------------------------------------------------------------
00071                                 Functions code
00072  -----------------------------------------------------------------------------*/
00073 
00074 /*----------------------------------------------------------------------------*/
00082 /*----------------------------------------------------------------------------*/
00083 int cpl_plugin_get_info(cpl_pluginlist * list)
00084 {
00085     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00086     cpl_plugin  *   plugin = &recipe->interface ;
00087 
00088     cpl_plugin_init(plugin,
00089                     CPL_PLUGIN_API,
00090                     SINFONI_BINARY_VERSION,
00091                     CPL_PLUGIN_TYPE_RECIPE,
00092                     "si_utl_skymap",
00093                     "Flags sky lines as bad pixels, with map generation",
00094                     si_utl_skymap_description,
00095                     "Andrea Modigliani",
00096                     "Andrea.Modigliani@eso.org",
00097                     sinfoni_get_license(),
00098                     si_utl_skymap_create,
00099                     si_utl_skymap_exec,
00100                     si_utl_skymap_destroy) ;
00101 
00102     cpl_pluginlist_append(list, plugin) ;
00103     
00104     return 0;
00105 }
00106 
00107 /*----------------------------------------------------------------------------*/
00116 /*----------------------------------------------------------------------------*/
00117 static int si_utl_skymap_create(cpl_plugin * plugin)
00118 {
00119     cpl_recipe      * recipe ;
00120     cpl_parameter   * p ;
00121 
00122     /* Get the recipe out of the plugin */
00123     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00124         recipe = (cpl_recipe *)plugin ;
00125     else return -1 ;
00126 
00127     /* Create the parameters list in the cpl_recipe object */
00128     recipe->parameters = cpl_parameterlist_new() ; 
00129 
00130     /* Fill the parameters list */
00131 
00132 
00133     /* --doubleopt */
00134     p = cpl_parameter_new_range("sinfoni.si_utl_skymap.xsize", 
00135             CPL_TYPE_INT, "X box size", "sinfoni.si_utl_skymap", 1,1,2047) ;
00136     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xsize") ;
00137     cpl_parameterlist_append(recipe->parameters, p) ;
00138 
00139     /* --doubleopt */
00140     p = cpl_parameter_new_range("sinfoni.si_utl_skymap.ysize", 
00141             CPL_TYPE_INT, "Y box size", "sinfoni.si_utl_skymap", 30,1,2047) ;
00142     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ysize") ;
00143     cpl_parameterlist_append(recipe->parameters, p) ;
00144 
00145 
00146 
00147     /* --doubleopt */
00148     p = cpl_parameter_new_value("sinfoni.si_utl_skymap.threshold", 
00149             CPL_TYPE_DOUBLE, "Threshold", "sinfoni.si_utl_skymap", 30.) ;
00150     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "thresh") ;
00151     cpl_parameterlist_append(recipe->parameters, p) ;
00152 
00153     /* Return */
00154     return 0;
00155 }
00156 
00157 /*----------------------------------------------------------------------------*/
00163 /*----------------------------------------------------------------------------*/
00164 static int si_utl_skymap_exec(cpl_plugin * plugin)
00165 {
00166     cpl_recipe  *   recipe ;
00167     
00168     /* Get the recipe out of the plugin */
00169     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00170         recipe = (cpl_recipe *)plugin ;
00171     else return -1 ;
00172 
00173     return si_utl_skymap(recipe->parameters, recipe->frames) ;
00174 }
00175 
00176 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 static int si_utl_skymap_destroy(cpl_plugin * plugin)
00184 {
00185     cpl_recipe  *   recipe ;
00186     
00187     /* Get the recipe out of the plugin */
00188     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00189         recipe = (cpl_recipe *)plugin ;
00190     else return -1 ;
00191 
00192     cpl_parameterlist_delete(recipe->parameters) ; 
00193     return 0 ;
00194 }
00195 
00196 static int si_utl_skymap(
00197         cpl_parameterlist   *   parlist, 
00198         cpl_frameset        *   framelist)
00199 {
00200     const char          *   fctid = "si_utl_skymap" ;
00201     cpl_parameter       *   param =NULL;
00202     const char *            name_i=NULL;
00203     int                    xsize=0;
00204     int                    ysize=0;
00205 
00206     cpl_frame           *   sky_frm=NULL;
00207 
00208     const char          *   name_o=NULL ;
00209     cpl_propertylist    *   plist =NULL;
00210     cpl_frame           *   product_frame=NULL;
00211     cpl_frameset        *   sky_set=NULL;
00212     cpl_image           *   sky_ima=NULL;
00213     cpl_image           *   sky_map=NULL;
00214     double threshold=0;
00215     int i=0;
00216     int j=0;
00217     double median=0;
00218     float* sky_ima_pix=NULL;
00219     float* sky_map_pix=NULL;
00220     int nx=0;
00221     int ny=0;
00222     int n=0;
00223   
00224     /* HOW TO RETRIEVE INPUT PARAMETERS */
00225     /* --stropt */
00226     param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_skymap.out_filename");
00227     name_o = "out_skymap.fits";
00228 
00229     /* --intopt */
00230     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_skymap.xsize");
00231     xsize = cpl_parameter_get_int(param) ;
00232 
00233     /* --intopt */
00234     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_skymap.ysize");
00235     ysize = cpl_parameter_get_int(param) ;
00236   
00237     param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_skymap.threshold");
00238     threshold = cpl_parameter_get_double(param) ;
00239  
00240     /* Identify the RAW and CALIB frames in the input frameset */
00241     if (sinfoni_dfs_set_groups(framelist)) {
00242         cpl_msg_error(fctid, "Cannot identify RAW and CALIB frames") ;
00243         return -1 ;
00244     }
00245     
00246     /* HOW TO ACCESS INPUT DATA */
00247     n=cpl_frameset_get_size(framelist);
00248     if(n<1) {
00249       cpl_msg_error(fctid,"Empty input frame list!");
00250       return -1;
00251     }
00252     sky_set=cpl_frameset_new();
00253     sinfoni_extract_frames_type(framelist,sky_set,RAW_SKY);
00254 
00255     n=cpl_frameset_get_size(framelist);
00256     if(n<1) {
00257       cpl_msg_error(fctid,"No sky frames in input list!");
00258       cpl_frameset_delete(sky_set);
00259       return -1;
00260     }
00261 
00262 
00263     sky_frm = cpl_frameset_get_frame(sky_set,0);
00264 
00265    if ((plist=cpl_propertylist_load(cpl_frame_get_filename(sky_frm), 
00266                     0)) == NULL) {
00267         cpl_msg_error(fctid, "Cannot read the FITS header") ;
00268         return -1 ;
00269     }
00270   
00271 
00272     name_i=cpl_frame_get_filename(sky_frm);
00273     sky_ima = cpl_image_load(name_i,CPL_TYPE_FLOAT,0,0);
00274     sky_map=cpl_image_duplicate(sky_ima);
00275     sky_ima_pix=cpl_image_get_data(sky_ima);
00276     sky_map_pix=cpl_image_get_data(sky_map);
00277     nx = cpl_image_get_size_x(sky_ima);
00278     ny = cpl_image_get_size_y(sky_ima);
00279 
00280     for(i=1;i<nx;i++) {
00281 
00282        for(j=ysize+1;j<ny-ysize;j++) {
00283 
00284           median=cpl_image_get_median_window(sky_ima,i,j-ysize,i,j+xsize);
00285           if(cpl_error_get_code() != CPL_ERROR_NONE) {
00286         cpl_msg_error(fctid,"Exit");
00287         cpl_image_delete(sky_ima);
00288         cpl_image_delete(sky_map);
00289             cpl_propertylist_delete(plist);
00290         cpl_frameset_delete(sky_set);
00291             return -1;
00292       }
00293       if(sky_ima_pix[i+j*nx] > median+threshold) {
00294         sky_map_pix[i+j*nx]=0.;
00295       } else {
00296         sky_map_pix[i+j*nx]=1.;
00297       }
00298        }
00299 
00300     }
00301 
00302 
00303 
00304     for(i=1;i<nx;i++) {
00305 
00306        for(j=0;j<ysize+1;j++) {
00307 
00308              sky_map_pix[i+j*nx]=0.;
00309 
00310        }
00311 
00312        for(j=ny-ysize+1;j<ny;j++) {
00313 
00314              sky_map_pix[i+j*nx]=0.;
00315 
00316        }
00317 
00318     }
00319 
00320 
00321 
00322     /* Now performing the data reduction */
00323     /* Let's generate one image for the example */
00324 
00325 
00326     
00327     /* HOW TO SAVE A PRODUCT ON DISK  */
00328     /* Set the file name */
00329     name_o = "sky_map.fits" ;
00330 
00331     /* Create product frame */
00332     product_frame = cpl_frame_new();
00333     cpl_frame_set_filename(product_frame, name_o) ;
00334     cpl_frame_set_tag(product_frame, PRO_SKY_DUMMY) ;
00335     cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00336     cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00337     cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00338 
00339     if (cpl_error_get_code()) {
00340         cpl_msg_error(fctid, "Error while initialising the product frame") ;
00341         cpl_propertylist_delete(plist) ;
00342         cpl_frame_delete(product_frame) ;
00343         return -1 ;
00344     }
00345 
00346     /* Add DataFlow keywords */
00347    
00348     if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00349             "si_utl_skymap", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00350         cpl_msg_error(fctid, "Problem in the product DFS-compliance") ;
00351         cpl_propertylist_delete(plist) ;
00352         cpl_frame_delete(product_frame) ;
00353         cpl_image_delete(sky_ima);
00354         cpl_image_delete(sky_map);
00355         cpl_frameset_delete(sky_set);
00356         return -1 ;
00357     }
00358     
00359 
00360 
00361     /* Save the file */
00362     if (cpl_image_save(sky_map, name_o, CPL_BPP_DEFAULT, plist,
00363                        CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00364         cpl_msg_error(fctid, "Could not save product");
00365         cpl_propertylist_delete(plist) ;
00366         cpl_frame_delete(product_frame) ;
00367         cpl_image_delete(sky_map) ;
00368         return -1 ;
00369     }
00370     cpl_propertylist_delete(plist) ;
00371     cpl_image_delete(sky_map) ;
00372     cpl_image_delete(sky_ima) ;
00373 
00374     /* Log the saved file in the input frameset */
00375     cpl_frameset_insert(framelist, product_frame) ;
00376     cpl_frameset_delete(sky_set);
00377    
00378 
00379 
00380     
00381     /* Return */
00382     if (cpl_error_get_code()) 
00383         return -1 ;
00384     else 
00385         return 0 ;
00386 }

Generated on Wed Oct 26 13:08:54 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001