visir_util_insert_pos.c

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

Generated on Mon Feb 6 15:23:49 2012 for VISIR Pipeline Reference Manual by  doxygen 1.5.8