visir_util_convert_pos.c

00001 /* $Id: visir_util_convert_pos.c,v 1.3 2011/05/12 11:52:36 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: 2011/05/12 11:52:36 $
00024  * $Revision: 1.3 $
00025  * $Name: visir-3_4_4 $
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_util_convert_pos"
00043 
00044 #define FLUX_COL "FLUX_AUTO"
00045 #define X_COL    "XWIN_IMAGE"
00046 #define Y_COL    "YWIN_IMAGE"
00047 
00048 #define VISIR_FLUX     "FLXSCALE"
00049 #define VISIR_DRS_FLUX "ESO DRS FLXSCALE"
00050 
00051 /*-----------------------------------------------------------------------------
00052                             Private Functions prototypes
00053  -----------------------------------------------------------------------------*/
00054 
00055 static cpl_error_code visir_util_convert_pos_one(cpl_frameset *, double *,
00056                                                  irplib_framelist *,
00057                                                  irplib_framelist *, int,
00058                                                  const cpl_parameterlist *);
00059 
00060 
00061 VISIR_RECIPE_DEFINE(visir_util_convert_pos, 0,
00062                     "Conversion of a single object position from matching "
00063                     "pairs of sextractor table + object files",
00064                     "The files listed in the Set Of Frames (sof-file) "
00065                     "must be tagged:\n"
00066                     "Sextractor-object-file.fits " VISIR_UTIL_CONVERT_RAW
00067                     "Sextractor-table-file.fits "  VISIR_UTIL_CONVERT_TAB
00068                     "\nFor each input pair the produced object file will "
00069                     "have its WCS-ccordinates (CRPIX[12]) set to the position "
00070                     "from the brightest object in the corresponding table."
00071                     "\nThe product(s) will have a FITS card\n"
00072                     "'HIERARCH " CPL_DFS_PRO_CATG "' with a value of:\n"
00073                     VISIR_UTIL_CONVERT_PROCATG);
00074 
00075 /*----------------------------------------------------------------------------*/
00079 /*----------------------------------------------------------------------------*/
00080 
00081 /*-----------------------------------------------------------------------------
00082                                 Functions code
00083  -----------------------------------------------------------------------------*/
00084 
00085 /*----------------------------------------------------------------------------*/
00092 /*----------------------------------------------------------------------------*/
00093 static int visir_util_convert_pos(cpl_frameset            * framelist,
00094                              const cpl_parameterlist * parlist)
00095 {
00096 #ifdef _OPENMP
00097     cpl_errorstate     cleanstate = cpl_errorstate_get();
00098 #endif
00099     cpl_error_code     didfail = CPL_ERROR_NONE;
00100     irplib_framelist * allframes = NULL;
00101     irplib_framelist * rawframes = NULL;
00102     irplib_framelist * tabframes = NULL;
00103     double             flux1     = 0.0;
00104     int                i, n;
00105     
00106 
00107     /* Identify the RAW and TAB frames in the input frameset */
00108     skip_if (visir_dfs_set_groups(framelist));
00109 
00110     /* Objects observation */
00111     allframes = irplib_framelist_cast(framelist);
00112     skip_if(allframes == NULL);
00113     rawframes = irplib_framelist_extract(allframes, VISIR_UTIL_CONVERT_RAW);
00114     skip_if (rawframes == NULL);
00115     tabframes = irplib_framelist_extract(allframes, VISIR_UTIL_CONVERT_TAB);
00116     skip_if (tabframes == NULL);
00117     
00118     n = irplib_framelist_get_size(rawframes);
00119     skip_if(n != irplib_framelist_get_size(tabframes));
00120 #ifdef _OPENMP
00121 #pragma omp parallel for private(i)
00122 #endif
00123     for (i = 0; i < n; i++) {
00124         if (!didfail) {
00125 
00126             /* The total number of iterations must be pre-determined for the
00127                parallelism to work. In case of an error we can therefore not
00128                break, so instead we skip immediately to the next iteration.
00129                FIXME: This check on didfail does not guarantee that only one
00130                iteration can cause an error to be dumped, but it is not
00131                worse than checking on a thread-local state, e.g. errori. */
00132 
00133             if (visir_util_convert_pos_one(framelist, &flux1, rawframes,
00134                                            tabframes, i, parlist)) {
00135                 const cpl_error_code errori = cpl_error_set_where(cpl_func);
00136 #ifdef _OPENMP
00137                 /* Cannot access these errors after the join,
00138                    so dump them now. :-(((((((((((((((((((( */
00139                 cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
00140                 cpl_errorstate_set(cleanstate);
00141 #pragma omp critical(visir_util_convert_pos)
00142 #endif
00143                 didfail = errori;
00144             }
00145         }
00146     }
00147 
00148     error_if(didfail, didfail, "Failed to process %d frame(s)", n);
00149 
00150     end_skip;
00151 
00152     irplib_framelist_delete(allframes);
00153     irplib_framelist_delete(rawframes);
00154     irplib_framelist_delete(tabframes);
00155 
00156     return cpl_error_get_code();
00157 }
00158 
00159 
00160 /*----------------------------------------------------------------------------*/
00171 /*----------------------------------------------------------------------------*/
00172 static
00173 cpl_error_code visir_util_convert_pos_one(cpl_frameset * framelist,
00174                                           double * pflux1,
00175                                           irplib_framelist * rawframes,
00176                                           irplib_framelist * tabframes, int i,
00177                                           const cpl_parameterlist * parlist)
00178 {
00179 
00180     const int               n = irplib_framelist_get_size(rawframes);
00181     const cpl_error_code code =
00182         irplib_framelist_load_propertylist(rawframes, i, 0, "^("
00183                                            IRPLIB_PFITS_WCS_REGEXP ")$",
00184                                            CPL_FALSE);
00185     cpl_propertylist * updlist = cpl_propertylist_duplicate
00186         (irplib_framelist_get_propertylist_const(rawframes, i));
00187     cpl_frameset     * products   = cpl_frameset_new();
00188     cpl_frameset     * usedframes = cpl_frameset_new();
00189     const cpl_frame  * frame;
00190     const char    * imgname = cpl_frame_get_filename(irplib_framelist_get_const
00191                                                       (rawframes, i));
00192     const char    * tabname = cpl_frame_get_filename(irplib_framelist_get_const
00193                                                       (tabframes, i));
00194     cpl_table     * table = cpl_table_load(tabname, 1, 0);
00195     cpl_image     * image = cpl_image_load(imgname, CPL_TYPE_UNSPECIFIED, 0, 0);
00196     char          * proname = cpl_sprintf(RECIPE_STRING "_%d" CPL_DFS_FITS,
00197                                           1+i);
00198     int             maxpos;
00199     double          xpos, ypos, flux;
00200 
00201     skip_if(code);
00202     bug_if(pflux1 == NULL);
00203 
00204     skip_if(cpl_table_get_column_maxpos(table, FLUX_COL, &maxpos));
00205 
00206     xpos = cpl_table_get(table, X_COL, maxpos, NULL);
00207     ypos = cpl_table_get(table, Y_COL, maxpos, NULL);
00208     flux = cpl_table_get(table, FLUX_COL, maxpos, NULL);
00209 
00210     if (*pflux1 != 0.0) {
00211         flux /= *pflux1;
00212     } else {
00213         *pflux1 = flux;
00214         flux = 1.0;
00215     }
00216 
00217     cpl_msg_info(cpl_func, "Converting frame %d/%d: (%g,%g) using row %d",
00218                  1+i, n, xpos, ypos, maxpos);
00219 
00220     bug_if(cpl_propertylist_update_double(updlist, "CRPIX1", xpos));
00221     bug_if(cpl_propertylist_update_double(updlist, "CRPIX2", ypos));
00222     bug_if(cpl_propertylist_append_double(updlist, VISIR_DRS_FLUX, flux));
00223     bug_if(cpl_propertylist_append_double(updlist, VISIR_FLUX, flux));
00224 
00225     /* Comments copied from actual header */
00226     bug_if(cpl_propertylist_set_comment(updlist, "CRPIX1",
00227                                         "Windowed position estimate along x"));
00228     bug_if(cpl_propertylist_set_comment(updlist, "CRPIX2",
00229                                         "Windowed position estimate along y"));
00230     bug_if(cpl_propertylist_set_comment(updlist, VISIR_DRS_FLUX, "Flux relative "
00231                                         "to the aperture of the 1st product"));
00232     bug_if(cpl_propertylist_set_comment(updlist, VISIR_FLUX, "Flux relative "
00233                                         "to the aperture of the 1st product"));
00234 
00235     bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00236                                (irplib_framelist_get_const(rawframes, i))));
00237     bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00238                                (irplib_framelist_get_const(tabframes, i))));
00239 
00240     skip_if(irplib_dfs_save_image(products, parlist, usedframes, image,
00241                                   CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00242                                   VISIR_UTIL_CONVERT_PROCATG, updlist,
00243                                   NULL, visir_pipe_id, proname));
00244 
00245 
00246     for (frame = cpl_frameset_get_first_const(products);
00247          frame != NULL;
00248          frame = cpl_frameset_get_next_const(products)) {
00249         cpl_frame * copy = cpl_frame_duplicate(frame);
00250         cpl_error_code error;
00251 
00252 #ifdef _OPENMP
00253 #pragma omp critical(visir_util_convert_pos_one)
00254 #endif
00255         error = cpl_frameset_insert(framelist, copy);
00256 
00257         if (error) break;
00258     }
00259     bug_if(frame != NULL);
00260 
00261     end_skip;
00262 
00263     cpl_image_delete(image);
00264     cpl_table_delete(table);
00265     cpl_free(proname);
00266     cpl_propertylist_delete(updlist);
00267     cpl_frameset_delete(usedframes);
00268     cpl_frameset_delete(products);
00269 
00270     return cpl_error_get_code();
00271 }

Generated on Thu May 12 12:12:20 2011 for VISIR Pipeline Reference Manual by  doxygen 1.4.7