visir_img_pfov.c

00001 /* $Id: visir_img_pfov.c,v 1.85 2009/02/27 10:44:01 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: 2009/02/27 10:44:01 $
00024  * $Revision: 1.85 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "visir_recipe.h"
00037 
00038 /*-----------------------------------------------------------------------------
00039                                 Defines
00040  -----------------------------------------------------------------------------*/
00041 
00042 #define RECIPE_STRING "visir_img_pfov"
00043 
00044 /*-----------------------------------------------------------------------------
00045                             Private Functions prototypes
00046  -----------------------------------------------------------------------------*/
00047 
00048 static cpl_error_code visir_img_pfov_save(cpl_frameset *,
00049                                           const cpl_parameterlist *,
00050                                           const cpl_table *);
00051 
00052 VISIR_RECIPE_DEFINE(visir_img_pfov,
00053                     VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00054                     VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
00055                     VISIR_PARAM_STRIPNON |
00056                     VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
00057                     "Pixel field of view recipe",
00058                     "This recipe determines the pixel field of view by finding "
00059                     "the precise\n"
00060                     "position of a bright object and comparing it to the "
00061                     "header information\n"
00062                     "giving the telescope pointing.\n"
00063                     "The files listed in the Set Of Frames (sof-file) "
00064                     "must be tagged:\n"
00065                     "VISIR-field-of-view-file.fits " VISIR_IMG_PFOV_BIN " or\n"
00066                     "VISIR-field-of-view-file.fits " VISIR_IMG_PFOV_TEL "\n"
00067                     "\n"
00068                     "The corresponding product will have a FITS card\n"
00069                     "'HIERARCH ESO PRO CATG' with a value of\n"
00070                     VISIR_IMG_PFOV_TAB_PROCATG_BIN " or\n"
00071                     VISIR_IMG_PFOV_TAB_PROCATG_TEL
00072                     "\n"
00073                     MAN_VISIR_CALIB_BPM_IMG);
00074 
00075 /*-----------------------------------------------------------------------------
00076                             Static variables
00077  -----------------------------------------------------------------------------*/
00078 
00079 enum _visir_img_mode_ {
00080     visir_img_none = 0,
00081     visir_img_bin,
00082     visir_img_tel
00083 };
00084 
00085 typedef enum _visir_img_mode_ visir_img_mode;
00086 
00087 static struct {
00088     /* Inputs */
00089     visir_img_mode img_mode;
00090 
00091     /* Outputs */
00092 } visir_img_pfov_config;
00093 
00094 /*----------------------------------------------------------------------------*/
00098 /*----------------------------------------------------------------------------*/
00099 
00100 /*-----------------------------------------------------------------------------
00101                                 Functions code
00102  -----------------------------------------------------------------------------*/
00103 
00104 /*----------------------------------------------------------------------------*/
00111 /*----------------------------------------------------------------------------*/
00112 static int visir_img_pfov(cpl_frameset            * framelist,
00113                           const cpl_parameterlist * parlist)
00114 {
00115     cpl_errorstate cleanstate = cpl_errorstate_get();
00116     irplib_framelist * allframes = NULL;
00117     irplib_framelist * rawframes = NULL;
00118     const char       * badpix;
00119     const char       * flat;
00120     cpl_imagelist    * nodded = NULL;
00121     cpl_vector       * ok = NULL;
00122     cpl_table        * tab = NULL;
00123     double             xprev = 0.0; /* Avoid (false) uninit warning */
00124     double             yprev = 0.0; /* Avoid (false) uninit warning */
00125     double             aprev = 0.0; /* Avoid (false) uninit warning */
00126     double             dprev = 0.0; /* Avoid (false) uninit warning */
00127     int                oki, okprev;
00128     int                nrow;
00129     int                i;
00130 
00131 
00132     /* Identify the RAW and CALIB frames in the input frameset */
00133     skip_if (visir_dfs_set_groups(framelist));
00134 
00135     /* Objects observation */
00136     allframes = irplib_framelist_cast(framelist);
00137     skip_if(allframes == NULL);
00138     rawframes = irplib_framelist_extract_regexp(allframes, "^(" VISIR_IMG_PFOV_BIN
00139                                                 "|" VISIR_IMG_PFOV_TEL ")$",
00140                                                 CPL_FALSE);
00141     skip_if (rawframes == NULL);
00142 
00143     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
00144                                                    visir_property_regexp,
00145                                                    CPL_FALSE));
00146 
00147     skip_if(visir_dfs_check_framelist_tag(rawframes));
00148 
00149     /* Verify uniqueness of frame type */
00150     visir_img_pfov_config.img_mode = visir_img_none;
00151     if (cpl_frameset_find(framelist, VISIR_IMG_PFOV_BIN))
00152         visir_img_pfov_config.img_mode = visir_img_bin;
00153     if (cpl_frameset_find(framelist, VISIR_IMG_PFOV_TEL)) {
00154         skip_if (visir_img_pfov_config.img_mode);
00155         visir_img_pfov_config.img_mode = visir_img_tel;
00156     }
00157     
00158     bug_if(visir_img_pfov_config.img_mode == visir_img_none);
00159 
00160     /* Bad pixels calibration file */
00161     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00162 
00163     /* Flatfield calibration file */
00164     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00165 
00166     /* Combine the frames */
00167     cpl_msg_info(cpl_func, "Construct the nodded images");
00168     
00169     nodded = visir_inputs_combine(RECIPE_STRING, parlist, rawframes, badpix, flat,
00170                                   NULL, CPL_FALSE, 0.0, 0);
00171     if (nodded == NULL) {
00172         cpl_msg_error(cpl_func, "Cannot combine the input frames");
00173         skip_if(1);
00174     }
00175 
00176     nrow = cpl_imagelist_get_size(nodded);
00177 
00178     /* Allocate and initialise arrays */
00179     tab = visir_table_new_xypos(nodded, "FLUX");
00180     skip_if (tab == NULL);
00181 
00182     skip_if (cpl_table_erase_column(tab, "FLUX"));
00183 
00184     skip_if (cpl_table_new_column(tab, "A_POS", CPL_TYPE_DOUBLE));
00185     skip_if (cpl_table_new_column(tab, "D_POS", CPL_TYPE_DOUBLE));
00186     skip_if (cpl_table_new_column(tab, "PFOV",  CPL_TYPE_DOUBLE));
00187 
00188     skip_if (cpl_table_fill_column_window(tab, "A_POS", 0, nrow, -1));
00189     skip_if (cpl_table_fill_column_window(tab, "D_POS", 0, nrow, -1));
00190     skip_if (cpl_table_fill_column_window(tab, "PFOV",  0, nrow,  0));
00191 
00192     ok = cpl_vector_new(nrow);
00193 
00194     skip_if (cpl_vector_fill(ok, 1));
00195 
00196     oki = 0;
00197     for (i=0; i < nrow ; i++) {
00198         const cpl_propertylist * plist =
00199             irplib_framelist_get_propertylist_const(rawframes, 2*i);
00200 
00201         /* Angle from the header */
00202         const double apos = visir_pfits_get_alpha(plist);
00203         const double dpos = visir_pfits_get_delta(plist);
00204 
00205         if (cpl_error_get_code()) {
00206             visir_error_reset("Could not get FITS key");
00207             skip_if(cpl_vector_set(ok, i, 0));
00208             continue;
00209         }
00210 
00211         if (cpl_table_get_double(tab, "X_POS", i, NULL) <= 0 ||
00212             cpl_table_get_double(tab, "Y_POS", i, NULL) <= 0)
00213             skip_if(cpl_vector_set(ok, i, 0));
00214 
00215         skip_if (cpl_table_set_double(tab, "A_POS", i, apos));
00216         skip_if (cpl_table_set_double(tab, "D_POS", i, dpos));
00217 
00218         oki = 1;
00219 
00220     }
00221 
00222     if (oki == 0) {
00223         cpl_msg_error(cpl_func, "None of the %d files contain a valid combination "
00224                       "of centroids and offsets", nrow);
00225         visir_error_set(CPL_ERROR_DATA_NOT_FOUND);
00226         skip_if(1);
00227     }
00228 
00229     /* Compute the pixel field of view */
00230     okprev = 0;
00231     for (i=0; i < nrow ; i++, okprev = oki) {
00232         oki = cpl_vector_get(ok, i) != 0.0 ? 1 : 0;
00233         if (oki) {
00234             const double x = cpl_table_get_double(tab, "X_POS", i, NULL);
00235             const double y = cpl_table_get_double(tab, "Y_POS", i, NULL);
00236             const double a = cpl_table_get_double(tab, "A_POS", i, NULL);
00237             const double d = cpl_table_get_double(tab, "D_POS", i, NULL);
00238 
00239             if (okprev) {
00240 
00241                 const double dx = x - xprev;
00242                 const double dy = y - yprev;
00243                 const double da = a - aprev;
00244                 const double dd = d - dprev;
00245 
00246                 const double dividend = sqrt(da * da + dd * dd);
00247                 const double divisor  = sqrt(dx * dx + dy * dy);
00248 
00249                 if (dividend < FLT_MAX * divisor)
00250                     cpl_table_set_double(tab, "PFOV", i, dividend / divisor);
00251 
00252             }
00253 
00254             xprev = x;
00255             yprev = y;
00256             aprev = a;
00257             dprev = d;
00258         }
00259     }
00260     
00261     /* Save the results */
00262     cpl_msg_info(cpl_func, "Save the results");
00263     skip_if (visir_img_pfov_save(framelist, parlist, tab));
00264 
00265     end_skip;
00266 
00267     irplib_framelist_delete(allframes);
00268     irplib_framelist_delete(rawframes);
00269     cpl_vector_delete(ok);
00270     cpl_imagelist_delete(nodded);
00271     cpl_table_delete(tab);
00272 
00273     return cpl_error_get_code();
00274 }
00275 
00276 /*----------------------------------------------------------------------------*/
00284 /*----------------------------------------------------------------------------*/
00285 static cpl_error_code visir_img_pfov_save(cpl_frameset            * set,
00286                                           const cpl_parameterlist * parlist,
00287                                           const cpl_table         * tab)
00288 {
00289 
00290     skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
00291                               visir_img_pfov_config.img_mode == visir_img_bin
00292                               ? VISIR_IMG_PFOV_TAB_PROCATG_BIN
00293                               : VISIR_IMG_PFOV_TAB_PROCATG_TEL, NULL, NULL,
00294                               visir_pipe_id, RECIPE_STRING CPL_DFS_FITS));
00295 
00296     end_skip;
00297 
00298     return cpl_error_get_code();
00299 }

Generated on Thu Mar 24 11:59:39 2011 for VISIR Pipeline Reference Manual by  doxygen 1.5.8