Main Page   File List  

visir_img_achro.c

00001 /* $Id: visir_img_achro.c,v 1.45 2006/01/22 08:22:10 llundin Exp $
00002  *
00003  * This file is part of the VISIR 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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2006/01/22 08:22:10 $
00024  * $Revision: 1.45 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <cpl.h>
00038 
00039 #include "irplib_utils.h"
00040 
00041 #include "visir_utils.h"
00042 #include "visir_pfits.h"
00043 #include "visir_dfs.h"
00044 #include "visir_inputs.h"
00045 
00046 /*-----------------------------------------------------------------------------
00047                                    Define
00048  -----------------------------------------------------------------------------*/
00049 
00050 #define VISIR_IMG_ACHRO_MIN_DIST    5
00051 
00052 /*-----------------------------------------------------------------------------
00053                             Functions prototypes
00054  -----------------------------------------------------------------------------*/
00055 
00056 static int visir_img_achro_create(cpl_plugin *);
00057 static int visir_img_achro_exec(cpl_plugin *);
00058 static int visir_img_achro_destroy(cpl_plugin *);
00059 static int visir_img_achro(cpl_parameterlist *, cpl_frameset *);
00060 static cpl_table * visir_img_achro_reduce(const cpl_frameset *);
00061 static int visir_img_achro_save(const cpl_table *, const cpl_parameterlist *,
00062                                 cpl_frameset *);
00063 
00064 /*-----------------------------------------------------------------------------
00065                             Static variables
00066  -----------------------------------------------------------------------------*/
00067 
00068 static const char * recipename = "visir_img_achro";
00069 
00070 static char visir_img_achro_description[] =
00071 "This recipe computes the best transmission wavelength at different\n"
00072 "positions on the detector.\n"
00073 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00074 "VISIR-achromatism-file.fits IM_CAL_ACHRO\n"
00075 "The results are given in an output table.\n";
00076 
00077 /*-----------------------------------------------------------------------------
00078                                 Functions code
00079  -----------------------------------------------------------------------------*/
00080 
00081 /*----------------------------------------------------------------------------*/
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 
00097     if (cpl_plugin_init(plugin,
00098                     CPL_PLUGIN_API,
00099                     VISIR_BINARY_VERSION,
00100                     CPL_PLUGIN_TYPE_RECIPE,
00101                     recipename,
00102                     "Achromatism recipe",
00103                     visir_img_achro_description,
00104                     "Lars Lundin",
00105                     PACKAGE_BUGREPORT,
00106                     visir_get_license(),
00107                     visir_img_achro_create,
00108                     visir_img_achro_exec,
00109                     visir_img_achro_destroy)) return 1;
00110 
00111     if (cpl_pluginlist_append(list, plugin)) return 1;
00112     
00113     return 0;
00114 }
00115 
00116 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 static int visir_img_achro_create(cpl_plugin * plugin)
00127 {
00128     cpl_recipe * recipe = (cpl_recipe *)plugin;
00129 
00130     /* Verify plugin type */
00131     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00132 
00133     /* Create the parameters list in the cpl_recipe object */
00134     recipe->parameters = cpl_parameterlist_new();
00135 
00136     return 0;
00137 
00138 }
00139 
00140 /*----------------------------------------------------------------------------*/
00146 /*----------------------------------------------------------------------------*/
00147 static int visir_img_achro_exec(cpl_plugin * plugin)
00148 {
00149     cpl_recipe * recipe = (cpl_recipe *)plugin;
00150 
00151     /* Verify plugin type */
00152     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00153 
00154     return visir_img_achro(recipe->parameters, recipe->frames);
00155 }
00156 
00157 /*----------------------------------------------------------------------------*/
00163 /*----------------------------------------------------------------------------*/
00164 static int visir_img_achro_destroy(cpl_plugin * plugin)
00165 {
00166     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00167 
00168     /* Verify plugin type */
00169     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00170     cpl_parameterlist_delete(recipe->parameters);
00171     return  0;
00172 }
00173 
00174 /*----------------------------------------------------------------------------*/
00181 /*----------------------------------------------------------------------------*/
00182 static int visir_img_achro(
00183         cpl_parameterlist   *   parlist,
00184         cpl_frameset        *   framelist)
00185 {
00186     cpl_frameset    *   rawframes = NULL;
00187     cpl_table       *   tab = NULL;
00188         
00189 
00190     if (cpl_error_get_code()) return cpl_error_get_code();
00191 
00192     /* Identify the RAW and CALIB frames in the input frameset */
00193     skip_if (visir_dfs_set_groups(framelist));
00194 
00195     /* Objects observation */
00196     rawframes = irplib_frameset_extract(framelist, VISIR_IMG_ACHRO_RAW);
00197     skip_if (rawframes == NULL);
00198 
00199     skip_if(visir_dfs_check_frameset_tag(rawframes));
00200 
00201     /* Apply the reduction */
00202     cpl_msg_info(__func__, "Compute the central wavelengths");
00203     if ((tab = visir_img_achro_reduce(rawframes)) == NULL) {
00204         cpl_msg_error(__func__, "Cannot compute the wavelengths");
00205         skip_if(1);
00206     }
00207 
00208     /* Save the products */
00209     cpl_msg_info(__func__, "Save the products");
00210     if (visir_img_achro_save(tab, parlist, framelist)) {
00211         cpl_msg_error(__func__, "Cannot save products");
00212         skip_if(1);
00213     }
00214 
00215     end_skip;
00216     
00217     cpl_frameset_delete(rawframes);
00218     cpl_table_delete(tab);
00219 
00220     return cpl_error_get_code();
00221 }
00222 
00223 /*----------------------------------------------------------------------------*/
00229 /*----------------------------------------------------------------------------*/
00230 static cpl_table * visir_img_achro_reduce(const cpl_frameset * set)
00231 {
00232     cpl_imagelist   *   loaded = NULL;
00233     double          *   wls = NULL;
00234     cpl_table       *   tab = NULL;
00235     cpl_table       *   tab_out = NULL;
00236     int                 nfiles, nb_pos;
00237     int                 j;
00238 
00239 
00240     skip_if (cpl_error_get_code());
00241 
00242     skip_if (set == NULL);
00243     
00244     /* Load the frames */
00245     cpl_msg_info(__func__, "Load the input frames");
00246     if ((loaded = visir_imagelist_load_last(set)) == NULL) {
00247         cpl_msg_error(__func__, "Cannot load the input frames");
00248         skip_if(1);
00249     }
00250     nfiles = cpl_imagelist_get_size(loaded);
00251     skip_if (nfiles < 1);
00252 
00253     /* Get the wavelengths from the headers */
00254     cpl_msg_info(__func__, "Get the wavelengths from the input headers");
00255     if ((wls = visir_utils_get_wls(set)) == NULL) {
00256         cpl_msg_error(__func__, "Cannot get wavelengths");
00257         skip_if(1);
00258     }
00259 
00260     /* Apply detection and log the detected objects in a table */
00261     cpl_msg_info(__func__, "Detect the objects and get their flux");
00262 
00263     /* Create the table and fill it with the relevant informations */
00264     tab = visir_table_new_xypos(loaded, "FLUX");
00265     skip_if (tab == NULL);
00266 
00267     skip_if (cpl_table_wrap_double(tab, wls, "WAVELENGTH"));
00268     wls = NULL;
00269 
00270     /* Normalise the FLUX column */
00271     skip_if (cpl_table_divide_scalar(tab, "FLUX",
00272                                      cpl_table_get_column_max(tab, "FLUX")));
00273 
00274     /* Identify the different positions */
00275     cpl_msg_info(__func__, "Group the frames with the same object position");
00276     cpl_msg_info(__func__, "Compute the central wavelength at each position");
00277 
00278     /* Create the output table and fill it */
00279     tab_out = cpl_table_new(nfiles);
00280 
00281     /* In addition to the flux table it has an extra column for counting */
00282     skip_if (cpl_table_copy_structure(tab_out, tab));
00283     skip_if (cpl_table_new_column(tab_out, "NVALS", CPL_TYPE_DOUBLE));
00284 
00285     skip_if (cpl_table_fill_column_window(tab_out, "X_POS",      0, nfiles, 0));
00286     skip_if (cpl_table_fill_column_window(tab_out, "Y_POS",      0, nfiles, 0));
00287     skip_if (cpl_table_fill_column_window(tab_out, "WAVELENGTH", 0, nfiles, 0));
00288     skip_if (cpl_table_fill_column_window(tab_out, "FLUX",       0, nfiles, 0));
00289     skip_if (cpl_table_fill_column_window(tab_out, "NVALS",      0, nfiles, 0));
00290 
00291     nb_pos = 0;
00292 
00293     for (j=0 ; j < nfiles ; j++) {
00294         const double refx = cpl_table_get(tab, "X_POS",      j, NULL);
00295         const double refy = cpl_table_get(tab, "Y_POS",      j, NULL);
00296         const double flux = cpl_table_get(tab, "FLUX",       j, NULL);
00297         const double wlsj = cpl_table_get(tab, "WAVELENGTH", j, NULL);
00298 
00299         int i;
00300 
00301         if (refx <= 0 || refy <= 0 || flux <= 0) continue;
00302 
00303         for (i = 0; i < nb_pos; i++) {
00304             const double difx = cpl_table_get(tab, "X_POS", i, NULL) - refx;
00305             const double dify = cpl_table_get(tab, "Y_POS", i, NULL) - refy;
00306 
00307             /* Look for positions within the given distance */
00308             if (difx * difx + dify * dify
00309                 < VISIR_IMG_ACHRO_MIN_DIST * VISIR_IMG_ACHRO_MIN_DIST) break;
00310         }
00311         if (i == nb_pos) nb_pos++; /* Found a new (refx, refy) */
00312 
00313         /* Add row-values to its group in the output table */
00314         skip_if (cpl_table_set(tab_out, "NVALS", i, 1
00315                                    + cpl_table_get(tab_out, "NVALS", i, NULL)));
00316         skip_if (cpl_table_set(tab_out, "X_POS", i, refx
00317                                + cpl_table_get(tab_out, "X_POS", i, NULL)));
00318         skip_if (cpl_table_set(tab_out, "Y_POS", i, refy
00319                                + cpl_table_get(tab_out, "Y_POS", i, NULL)));
00320         skip_if (cpl_table_set(tab_out, "FLUX", i, flux
00321                                + cpl_table_get(tab_out, "FLUX", i, NULL)));
00322         skip_if (cpl_table_set(tab_out, "WAVELENGTH", i, flux * wlsj
00323                                + cpl_table_get(tab_out, "WAVELENGTH", i, NULL)));
00324 
00325     }
00326 
00327     skip_if (nb_pos < 1);
00328 
00329     skip_if (cpl_table_set_size(tab_out, nb_pos));
00330 
00331     skip_if (cpl_table_divide_columns(tab_out, "X_POS", "NVALS"));
00332     skip_if (cpl_table_divide_columns(tab_out, "Y_POS", "NVALS"));
00333     skip_if (cpl_table_divide_columns(tab_out, "WAVELENGTH", "FLUX"));
00334 
00335     skip_if (cpl_table_erase_column(tab_out, "FLUX"));
00336     skip_if (cpl_table_erase_column(tab_out, "NVALS"));
00337 
00338     end_skip;
00339     
00340     cpl_free(wls);    
00341     cpl_table_delete(tab);
00342     cpl_imagelist_delete(loaded);
00343 
00344     if (cpl_error_get_code()) {
00345         cpl_table_delete(tab_out);
00346         tab_out = NULL;
00347     }
00348 
00349     return tab_out;
00350 }
00351 
00352 /*----------------------------------------------------------------------------*/
00360 /*----------------------------------------------------------------------------*/
00361 static int visir_img_achro_save(
00362         const cpl_table         * tab,
00363         const cpl_parameterlist * parlist,
00364         cpl_frameset            * set)
00365 {
00366 
00367     return visir_table_save(parlist, set, tab, recipename,
00368                             VISIR_IMG_ACHRO_TAB_PROCATG, NULL, NULL);
00369 }

Generated on Mon Jan 23 12:16:36 2006 for VISIR Pipeline Reference Manual by doxygen1.2.18