visir_util_find_beam.c

00001 /* $Id: visir_util_find_beam.c,v 1.2 2011/12/21 14:42:52 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/12/21 14:42:52 $
00024  * $Revision: 1.2 $
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_find_beam"
00043 
00044 #ifndef VISIR_UTIL_FIND_BEAM_HMEAN
00045 #define VISIR_UTIL_FIND_BEAM_HMEAN 3
00046 #endif
00047 
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Private Functions prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static
00054 cpl_error_code visir_util_find_beam_one_refine(cpl_bivector *,
00055                                                const cpl_imagelist *,
00056                                                const cpl_propertylist *,
00057                                                const cpl_parameterlist *);
00058 
00059 static cpl_error_code visir_util_find_beam_one(cpl_frameset *,
00060                                                irplib_framelist *,
00061                                                irplib_framelist *,
00062                                                int, cpl_boolean,
00063                                                const cpl_parameterlist *);
00064 
00065 cpl_recipe_define(visir_util_find_beam, VISIR_BINARY_VERSION,                
00066                   "Lars Lundin", PACKAGE_BUGREPORT, "2011",
00067                   "Find the positive beam in a nodded image",
00068                   "The files listed in the Set Of Frames (sof-file) "
00069                   "must be tagged pair-wise:\n"
00070                   "VISIR-raw-file.fits " VISIR_UTIL_NODDED "\n"
00071                   "VISIR-bpm-file.fits " VISIR_CALIB_BPM "\n"
00072                   "\nThe product(s) will have a FITS card\n"
00073                   "'HIERARCH ESO PRO CATG' with a value of:\n"
00074                   VISIR_UTIL_NODDED_REFINED_POS "\n");
00075 
00076 /*----------------------------------------------------------------------------*/
00080 /*----------------------------------------------------------------------------*/
00081 
00082 /*-----------------------------------------------------------------------------
00083                                 Functions code
00084  -----------------------------------------------------------------------------*/
00085 
00086 /*----------------------------------------------------------------------------*/
00094 /*----------------------------------------------------------------------------*/
00095 static
00096 cpl_error_code visir_util_find_beam_fill_parameterlist(cpl_parameterlist * self)
00097 {
00098 
00099     const char * context = PACKAGE "." RECIPE_STRING;
00100     cpl_error_code err;
00101 
00102     cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
00103 
00104     /* Fill the parameters list */
00105 
00106     err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00107                                        "hmean", VISIR_UTIL_FIND_BEAM_HMEAN,
00108                                        NULL, context, "The number of frames in "
00109                                        "the running mean used to make the "
00110                                        "object visible is 1 + twice this "
00111                                        "number");
00112     cpl_ensure_code(!err, err);
00113 
00114     return CPL_ERROR_NONE;
00115 
00116 }
00117 
00118 /*----------------------------------------------------------------------------*/
00125 /*----------------------------------------------------------------------------*/
00126 static int visir_util_find_beam(cpl_frameset            * framelist,
00127                                 const cpl_parameterlist * parlist)
00128 {
00129 #ifdef _OPENMP
00130     cpl_errorstate     cleanstate = cpl_errorstate_get();
00131 #endif
00132     cpl_error_code     didfail = CPL_ERROR_NONE;
00133     irplib_framelist * allframes = NULL;
00134     irplib_framelist * rawframes = NULL;
00135     irplib_framelist * bpmframes = NULL;
00136     int                i, n, nbad;
00137     
00138 
00139     /* Identify the RAW and CALIB frames in the input frameset */
00140     skip_if (visir_dfs_set_groups(framelist));
00141 
00142     /* Objects observation */
00143     allframes = irplib_framelist_cast(framelist);
00144     skip_if(allframes == NULL);
00145     rawframes = irplib_framelist_extract(allframes, VISIR_UTIL_NODDED);
00146     skip_if (rawframes == NULL);
00147     bpmframes = irplib_framelist_extract(allframes, VISIR_CALIB_BPM);
00148     skip_if (bpmframes == NULL);
00149     
00150     n = irplib_framelist_get_size(rawframes);
00151     nbad = irplib_framelist_get_size(bpmframes);
00152     error_if(nbad != n && nbad != 1, CPL_ERROR_INCOMPATIBLE_INPUT,
00153              "%d raw-frames <=> %d bpm frames", n, nbad);
00154 
00155 #ifdef _OPENMP
00156 #pragma omp parallel for private(i)
00157 #endif
00158     for (i = 0; i < n; i++) {
00159         if (!didfail) {
00160 
00161             /* The total number of iterations must be pre-determined for the
00162                parallelism to work. In case of an error we can therefore not
00163                break, so instead we skip immediately to the next iteration.
00164                FIXME: This check on didfail does not guarantee that only one
00165                iteration can cause an error to be dumped, but it is not
00166                worse than checking on a thread-local state, e.g. errori. */
00167 
00168             if (visir_util_find_beam_one(framelist, rawframes, bpmframes, i,
00169                                          nbad == 1, parlist)) {
00170                 const cpl_error_code errori = cpl_error_set_where(cpl_func);
00171 #ifdef _OPENMP
00172                 /* Cannot access these errors after the join,
00173                    so dump them now. :-(((((((((((((((((((( */
00174                 cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
00175                 cpl_errorstate_set(cleanstate);
00176 #pragma omp critical(visir_util_find_beam)
00177 #endif
00178                 didfail = errori;
00179             }
00180         }
00181     }
00182 
00183     error_if(didfail, didfail, "Failed to find beam in %d frame(s)", n);
00184 
00185     end_skip;
00186 
00187     irplib_framelist_delete(allframes);
00188     irplib_framelist_delete(rawframes);
00189     irplib_framelist_delete(bpmframes);
00190 
00191     return cpl_error_get_code();
00192 }
00193 
00194 
00195 /*----------------------------------------------------------------------------*/
00206 /*----------------------------------------------------------------------------*/
00207 static
00208 cpl_error_code visir_util_find_beam_one(cpl_frameset * framelist,
00209                                         irplib_framelist * rawframes,
00210                                         irplib_framelist * bpmframes,
00211                                         int i, cpl_boolean bshared,
00212                                         const cpl_parameterlist * parlist)
00213 {
00214 
00215     cpl_frameset  * products   = cpl_frameset_new();
00216     cpl_frameset  * usedframes = cpl_frameset_new();
00217     const cpl_frame * frame;
00218 
00219     char * findname = cpl_sprintf(RECIPE_STRING "_%d" CPL_DFS_FITS, i);
00220 
00221     const int            n = irplib_framelist_get_size(rawframes);
00222     cpl_frameset       * rawone = cpl_frameset_new();
00223     cpl_frameset       * bpmone = cpl_frameset_new();
00224     cpl_frame * rawframe =
00225         cpl_frame_duplicate(irplib_framelist_get_const(rawframes, i));
00226     cpl_frame * bpmframe =
00227         cpl_frame_duplicate(irplib_framelist_get_const(bpmframes,
00228                                                        bshared ? 0 : i));
00229     const cpl_error_code errr = cpl_frameset_insert(rawone, rawframe);
00230     const cpl_error_code errb = cpl_frameset_insert(bpmone, bpmframe);
00231     const char * rawname = cpl_frame_get_filename(rawframe);
00232     cpl_propertylist * plist =
00233         cpl_propertylist_load_regexp(rawname, 0, "("
00234                                      "ESO DRS CUMOFFSETX" "|"
00235                                      "ESO DRS CUMOFFSETY" "|"
00236                                      VISIR_PFITS_DOUBLE_CHOP_THROW "|"
00237                                      VISIR_PFITS_STRING_PIXSCALE ")",
00238                                      CPL_FALSE);
00239     cpl_imagelist * rawlist =
00240         cpl_imagelist_load_frameset(rawone, CPL_TYPE_FLOAT, 0, -1);
00241     cpl_imagelist * bpmlist =
00242         cpl_imagelist_load_frameset(bpmone, CPL_TYPE_INT, 0, -1);
00243     cpl_mask      * bpm       = NULL;
00244     cpl_image     * newbpmimg = NULL;
00245     cpl_propertylist * qclist = cpl_propertylist_new();
00246 
00247     const int       m = cpl_imagelist_get_size(rawlist);
00248     cpl_bivector  * offsets = cpl_bivector_new(m);
00249     int             j;
00250 
00251     skip_if(rawlist == NULL);
00252     skip_if(bpmlist == NULL);
00253 
00254     bug_if(errr);
00255     bug_if(errb);
00256     skip_if(plist == NULL);
00257 
00258     error_if(cpl_imagelist_get_size(bpmlist) != m, CPL_ERROR_INCOMPATIBLE_INPUT,
00259              "Frame-pair %d/%d: %d image(s) <=> %d bad pixel map(s)", 1+i, n,
00260              m, (int)cpl_imagelist_get_size(bpmlist));
00261 
00262     bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate(rawframe)));
00263     bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate(bpmframe)));
00264 
00265     cpl_msg_info(cpl_func, "F(%d): %s", i, rawname);
00266 
00267     /* Apply the bad pixel maps to the images */
00268     for (j = 0; j < m; j++) {
00269         const cpl_image * bpmimg = cpl_imagelist_get_const(bpmlist, j);
00270         cpl_image * rawimg       = cpl_imagelist_get(rawlist, j);
00271 
00272         cpl_mask_delete(bpm);
00273         bpm = cpl_mask_threshold_image_create(bpmimg, 0.5, FLT_MAX);
00274 
00275         bug_if(cpl_image_reject_from_mask(rawimg, bpm));
00276 
00277     }
00278 
00279     skip_if(visir_util_find_beam_one_refine(offsets, rawlist, plist, parlist));
00280 
00281 
00282     for (frame = cpl_frameset_get_first_const(products);
00283          frame != NULL;
00284          frame = cpl_frameset_get_next_const(products)) {
00285         cpl_frame * copy = cpl_frame_duplicate(frame);
00286         cpl_error_code error;
00287 
00288 #ifdef _OPENMP
00289 #pragma omp critical(visir_util_find_beam_one)
00290 #endif
00291         error = cpl_frameset_insert(framelist, copy);
00292 
00293         if (error) break;
00294     }
00295 
00296     bug_if(frame != NULL);
00297 
00298 
00299     end_skip;
00300 
00301     cpl_free(findname);
00302 
00303     cpl_frameset_delete(rawone);
00304     cpl_frameset_delete(bpmone);
00305     cpl_frameset_delete(usedframes);
00306     cpl_frameset_delete(products);
00307 
00308     cpl_propertylist_delete(plist);
00309     cpl_propertylist_delete(qclist);
00310 
00311     cpl_mask_delete(bpm);
00312     cpl_image_delete(newbpmimg);
00313     cpl_imagelist_delete(rawlist);
00314     cpl_imagelist_delete(bpmlist);
00315 
00316     cpl_bivector_delete(offsets);
00317 
00318     return cpl_error_get_code();
00319 
00320 }
00321 
00322 
00323 /*----------------------------------------------------------------------------*/
00334 /*----------------------------------------------------------------------------*/
00335 static cpl_error_code
00336 visir_util_find_beam_one_refine(cpl_bivector * self,
00337                                 const cpl_imagelist * nodded,
00338                                 const cpl_propertylist * plist,
00339                                 const cpl_parameterlist * parlist)
00340 {
00341 
00342     /* Get the chopping throw in pixels */
00343     const char * sscale       = visir_pfits_get_pixscale(plist);
00344     const double pscale       = sscale ? atof(sscale) : 0.0;
00345     const double pthrow       = pscale > 0.0
00346         ? visir_pfits_get_chop_throw(plist) / pscale : 0.0;
00347     const double x0 = irplib_pfits_get_double(plist, "ESO DRS CUMOFFSETX");
00348     const double y0 = irplib_pfits_get_double(plist, "ESO DRS CUMOFFSETY");
00349     double x2[2], y2[2];
00350 
00351     const int hmean = irplib_parameterlist_get_int(parlist, PACKAGE,
00352                                                    RECIPE_STRING, "hmean");
00353     const int nmean = 1 + 2 * hmean;
00354     cpl_propertylist * qclist = cpl_propertylist_new();
00355     cpl_image * runsum = cpl_image_duplicate(cpl_imagelist_get_const(nodded,
00356                                                                       0));
00357     cpl_image * invsum = NULL;
00358     int i;
00359 
00360     skip_if(nmean < 1);
00361     bug_if(self == NULL);
00362 
00363     for (i = 1; i < nmean; i++) {
00364         skip_if(cpl_image_add(runsum, cpl_imagelist_get_const(nodded, i)));
00365     }
00366 
00367     for (i = 0; i < nmean; i++) {
00368 
00369         cpl_image_delete(invsum);
00370         invsum = cpl_image_multiply_scalar_create(runsum, -1.0);
00371 
00372         skip_if(visir_img_find_beam_two(qclist, runsum, invsum, 0.25, pthrow,
00373                                         x2, y2));
00374 
00375         cpl_msg_info(cpl_func, "BB(%d): %g <=> %g. %g <=> %g", i, x0, x2[0],
00376                      y0, y2[0]);
00377 
00378         if (i >= hmean && i < nmean - hmean) {
00379             cpl_image_subtract(runsum, cpl_imagelist_get_const(nodded,
00380                                                                 i-hmean));
00381             cpl_image_add(runsum, cpl_imagelist_get_const(nodded,
00382                                                                 i+hmean));
00383         }
00384     }
00385 
00386     end_skip;
00387 
00388     cpl_image_delete(runsum);
00389     cpl_image_delete(invsum);
00390     cpl_propertylist_delete(qclist);
00391 
00392     return cpl_error_get_code();
00393 }

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