Main Page   File List  

visir_img_pfov.c

00001 /* $Id: visir_img_pfov.c,v 1.54 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.54 $
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 <float.h>
00038 #include <assert.h>
00039 #include <cpl.h>
00040 
00041 #include "irplib_utils.h"
00042 
00043 #include "visir_utils.h"
00044 #include "visir_pfits.h"
00045 #include "visir_dfs.h"
00046 #include "visir_inputs.h"
00047 
00048 /*-----------------------------------------------------------------------------
00049                             Functions prototypes
00050  -----------------------------------------------------------------------------*/
00051 
00052 static int visir_img_pfov_create(cpl_plugin *);
00053 static int visir_img_pfov_exec(cpl_plugin *);
00054 static int visir_img_pfov_destroy(cpl_plugin *);
00055 static int visir_img_pfov(cpl_parameterlist *, cpl_frameset *);
00056 static int visir_img_pfov_save(const cpl_table *, const cpl_parameterlist *,
00057                                cpl_frameset *);
00058 
00059 /*-----------------------------------------------------------------------------
00060                             Static variables
00061  -----------------------------------------------------------------------------*/
00062 
00063 enum _visir_img_mode_ {
00064     visir_img_none = 0,
00065     visir_img_bin,
00066     visir_img_tel
00067 };
00068 
00069 typedef enum _visir_img_mode_ visir_img_mode;
00070 
00071 static const char * recipename = "visir_img_pfov";
00072 
00073 static struct {
00074     /* Inputs */
00075     visir_img_mode img_mode;
00076     char        nodding[512];
00077     int         auto_bpm;
00078     int         rem_glitch;
00079     int         purge_bad;
00080 
00081     /* Outputs */
00082 } visir_img_pfov_config;
00083 
00084 static char visir_img_pfov_description[] =
00085 "This recipe determines the pixel field of view by finding the precise\n"
00086 "position of a bright object and comparing it to the header information\n"
00087 "giving the telescope pointing.\n"
00088 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00089 "VISIR-field-of-view-file.fits " VISIR_IMG_PFOV_BIN " or\n"
00090 "VISIR-field-of-view-file.fits " VISIR_IMG_PFOV_TEL "\n"
00091 "\n"
00092 "The corresponding product will have a FITS card\n"
00093 "'HIERARCH ESO PRO CATG' with a value of\n"
00094 VISIR_IMG_PFOV_TAB_PROCATG_BIN " or\n"
00095 VISIR_IMG_PFOV_TAB_PROCATG_TEL
00096 "\n";
00097 
00098 /*-----------------------------------------------------------------------------
00099                                 Functions code
00100  -----------------------------------------------------------------------------*/
00101 
00102 /*----------------------------------------------------------------------------*/
00111 /*----------------------------------------------------------------------------*/
00112 int cpl_plugin_get_info(cpl_pluginlist * list)
00113 {
00114     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00115     cpl_plugin  *   plugin = &recipe->interface;
00116 
00117 
00118     if (cpl_plugin_init(plugin,
00119                     CPL_PLUGIN_API,
00120                     VISIR_BINARY_VERSION,
00121                     CPL_PLUGIN_TYPE_RECIPE,
00122                     recipename,
00123                     "Pixel field of view recipe",
00124                     visir_img_pfov_description,
00125                     "Lars Lundin",
00126                     PACKAGE_BUGREPORT,
00127                     visir_get_license(),
00128                     visir_img_pfov_create,
00129                     visir_img_pfov_exec,
00130                     visir_img_pfov_destroy)) return 1;
00131 
00132     if (cpl_pluginlist_append(list, plugin)) return 1;
00133     
00134     return 0;
00135 }
00136 
00137 /*----------------------------------------------------------------------------*/
00146 /*----------------------------------------------------------------------------*/
00147 static int visir_img_pfov_create(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     /* Create the parameters list in the cpl_recipe object */
00155     recipe->parameters = cpl_parameterlist_new();
00156 
00157     /* Fill the parameters list */
00158     return visir_parameter_set(recipe->parameters, recipename,
00159                                VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00160                                VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE );
00161 
00162 }
00163 
00164 /*----------------------------------------------------------------------------*/
00170 /*----------------------------------------------------------------------------*/
00171 static int visir_img_pfov_exec(cpl_plugin * plugin)
00172 {
00173     cpl_recipe * recipe = (cpl_recipe *)plugin;
00174 
00175     /* Verify plugin type */
00176     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00177 
00178     return visir_img_pfov(recipe->parameters, recipe->frames);
00179 }
00180 
00181 /*----------------------------------------------------------------------------*/
00187 /*----------------------------------------------------------------------------*/
00188 static int visir_img_pfov_destroy(cpl_plugin * plugin)
00189 {
00190     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00191 
00192     /* Verify plugin type */
00193     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00194     cpl_parameterlist_delete(recipe->parameters);
00195     return  0;
00196 }
00197 
00198 /*----------------------------------------------------------------------------*/
00205 /*----------------------------------------------------------------------------*/
00206 static int visir_img_pfov(cpl_parameterlist   *   parlist, 
00207                           cpl_frameset        *   framelist)
00208 {
00209     const cpl_frame *  frame;
00210     cpl_parameter    * par;
00211     const char       * badpix;
00212     const char       * flat;
00213     cpl_frameset     * rawframes = NULL;
00214     cpl_propertylist * plist = NULL;
00215     cpl_imagelist    * nodded = NULL;
00216     cpl_vector       * ok = NULL;
00217     cpl_table        * tab = NULL;
00218     double             xprev, yprev, aprev, dprev;
00219     double             oki, okprev;
00220     int                nrow;
00221     int                i;
00222 
00223 
00224     if (cpl_error_get_code()) return cpl_error_get_code();
00225 
00226     /* Retrieve input parameters */
00227     par = cpl_parameterlist_find(parlist, "visir.visir_img_pfov.nodding");
00228     strcpy(visir_img_pfov_config.nodding, cpl_parameter_get_string(par));
00229     par = cpl_parameterlist_find(parlist, "visir.visir_img_pfov.auto_bpm");
00230     visir_img_pfov_config.auto_bpm = cpl_parameter_get_bool(par);
00231     par = cpl_parameterlist_find(parlist, "visir.visir_img_pfov.rem_glitch");
00232     visir_img_pfov_config.rem_glitch = cpl_parameter_get_bool(par);
00233     par = cpl_parameterlist_find(parlist, "visir.visir_img_pfov.purge_bad");
00234     visir_img_pfov_config.purge_bad = cpl_parameter_get_bool(par);
00235 
00236     /* Identify the RAW and CALIB frames in the input frameset */
00237     skip_if (visir_dfs_set_groups(framelist));
00238 
00239     /* Objects observation */
00240     rawframes = irplib_frameset_extract_regexp(framelist, "^(" VISIR_IMG_PFOV_BIN
00241                                               "|" VISIR_IMG_PFOV_TEL ")$");
00242     skip_if (rawframes == NULL);
00243 
00244     skip_if(visir_dfs_check_frameset_tag(rawframes));
00245 
00246     /* Verify uniqueness of frame type */
00247     visir_img_pfov_config.img_mode = visir_img_none;
00248     if (cpl_frameset_find(rawframes, VISIR_IMG_PFOV_BIN))
00249         visir_img_pfov_config.img_mode = visir_img_bin;
00250     if (cpl_frameset_find(rawframes, VISIR_IMG_PFOV_TEL)) {
00251         skip_if (visir_img_pfov_config.img_mode);
00252         visir_img_pfov_config.img_mode = visir_img_tel;
00253     }
00254     
00255 
00256     /* Bad pixels calibration file */
00257     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00258 
00259     /* Flatfield calibration file */
00260     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00261 
00262     /* Combine the frames */
00263     cpl_msg_info(__func__, "Construct the nodded images");
00264     
00265     nodded = visir_inputs_combine(rawframes, badpix, flat,
00266                                   visir_img_pfov_config.nodding,
00267                                   visir_img_pfov_config.auto_bpm,
00268                                   visir_img_pfov_config.rem_glitch,
00269                                   visir_img_pfov_config.purge_bad,
00270                                   NULL,
00271                                    CPL_FALSE, 0,0, 0,0,0,0);
00272     if (nodded == NULL) {
00273         cpl_msg_error(__func__, "Cannot combine the input frames");
00274         skip_if(1);
00275     }
00276 
00277     nrow = cpl_imagelist_get_size(nodded);
00278 
00279     /* Allocate and initialise arrays */
00280     tab = visir_table_new_xypos(nodded, "FLUX");
00281     skip_if (tab == NULL);
00282 
00283     skip_if (cpl_table_erase_column(tab, "FLUX"));
00284 
00285     skip_if (cpl_table_new_column(tab, "A_POS", CPL_TYPE_DOUBLE));
00286     skip_if (cpl_table_new_column(tab, "D_POS", CPL_TYPE_DOUBLE));
00287     skip_if (cpl_table_new_column(tab, "PFOV",  CPL_TYPE_DOUBLE));
00288 
00289     skip_if (cpl_table_fill_column_window(tab, "A_POS", 0, nrow, -1));
00290     skip_if (cpl_table_fill_column_window(tab, "D_POS", 0, nrow, -1));
00291     skip_if (cpl_table_fill_column_window(tab, "PFOV",  0, nrow,  0));
00292 
00293     ok = cpl_vector_new(nrow);
00294 
00295     skip_if (cpl_vector_fill(ok, 1));
00296 
00297     oki = 0;
00298     for (i=0, frame = cpl_frameset_get_first(rawframes) ; i < nrow ;
00299          i++, frame = cpl_frameset_get_next(rawframes),
00300              frame = cpl_frameset_get_next(rawframes)) {
00301         const char * reffile = cpl_frame_get_filename(frame);
00302         double apos;
00303         double dpos;
00304 
00305         if (cpl_table_get_double(tab, "X_POS", i, NULL) <= 0 ||
00306             cpl_table_get_double(tab, "Y_POS", i, NULL) <= 0)
00307             cpl_vector_set(ok, i, 0);
00308 
00309         /* Angle from the header */
00310         plist = cpl_propertylist_load(reffile, 0);
00311         if (plist == NULL) {
00312             cpl_msg_warning(__func__, "Could not load FITS header in %d. file %s: "
00313                             "'%s' in %s", i, reffile,
00314                             cpl_error_get_message(), cpl_error_get_where());
00315             cpl_error_reset();
00316             cpl_vector_set(ok, i, 0);
00317             continue;
00318         }
00319 
00320         apos = visir_pfits_get_alpha(plist);
00321         dpos = visir_pfits_get_delta(plist);
00322 
00323         cpl_propertylist_delete(plist);
00324         plist = NULL;
00325 
00326         if (cpl_error_get_code()) {
00327             cpl_error_reset();
00328             cpl_vector_set(ok, i, 0);
00329             continue;
00330         }
00331 
00332         skip_if (cpl_table_set_double(tab, "A_POS", i, apos));
00333         skip_if (cpl_table_set_double(tab, "D_POS", i, dpos));
00334 
00335         oki = 1;
00336 
00337     }
00338 
00339     if (oki == 0) {
00340         cpl_msg_error(__func__, "None of the %d files contain a valid combination "
00341                       "of centroids and offsets", nrow);
00342         visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
00343         skip_if(1);
00344     }
00345 
00346     /* Compute the pixel field of view */
00347     okprev = 0;
00348     for (i=0; i < nrow ; i++, okprev = oki) if ((oki = cpl_vector_get(ok, i))) {
00349         const double x = cpl_table_get_double(tab, "X_POS", i, NULL);
00350         const double y = cpl_table_get_double(tab, "Y_POS", i, NULL);
00351         const double a = cpl_table_get_double(tab, "A_POS", i, NULL);
00352         const double d = cpl_table_get_double(tab, "D_POS", i, NULL);
00353 
00354         if (okprev) {
00355 
00356             const double dx = x - xprev;
00357             const double dy = y - yprev;
00358             const double da = a - aprev;
00359             const double dd = d - dprev;
00360 
00361             const double dividend = sqrt(da * da + dd * dd);
00362             const double divisor  = sqrt(dx * dx + dy * dy);
00363 
00364             if (dividend < FLT_MAX * divisor)
00365                 cpl_table_set_double(tab, "PFOV", i, dividend / divisor);
00366 
00367         }
00368 
00369         xprev = x;
00370         yprev = y;
00371         aprev = a;
00372         dprev = d;
00373     }
00374     
00375     /* Save the results */
00376     cpl_msg_info(__func__, "Save the results");
00377     if (visir_img_pfov_save(tab, parlist, framelist)) {
00378         cpl_msg_error(__func__, "Cannot save products");
00379         skip_if(1);
00380     }
00381 
00382     end_skip;
00383 
00384     cpl_vector_delete(ok);
00385     cpl_propertylist_delete(plist);
00386     cpl_frameset_delete(rawframes);
00387     cpl_imagelist_delete(nodded);
00388     cpl_table_delete(tab);
00389 
00390     return cpl_error_get_code();
00391 }
00392 
00393 /*----------------------------------------------------------------------------*/
00401 /*----------------------------------------------------------------------------*/
00402 static int visir_img_pfov_save(
00403         const cpl_table         * tab,
00404         const cpl_parameterlist * parlist,
00405         cpl_frameset            * set)
00406 {
00407 
00408     return visir_table_save(parlist, set, tab, recipename,
00409                             visir_img_pfov_config.img_mode == visir_img_bin
00410                             ? VISIR_IMG_PFOV_TAB_PROCATG_BIN
00411                             : VISIR_IMG_PFOV_TAB_PROCATG_TEL, NULL, NULL);
00412 }

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