visir_img_combine.c

00001 /* $Id: visir_img_combine.c,v 1.107 2010/10/21 11:51:32 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: 2010/10/21 11:51:32 $
00024  * $Revision: 1.107 $
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_combine"
00043 
00044 /* FITS keys to be loaded for all raw files */
00045 #define RECIPE_KEYS_REGEXP_ALL \
00046         VISIR_PFITS_REGEXP_IMG_RECOMBINE
00047 
00048 /* FITS keys to be loaded for first raw file */
00049 #define RECIPE_KEYS_REGEXP               \
00050         RECIPE_KEYS_REGEXP_ALL           \
00051     "|" VISIR_PFITS_REGEXP_COMBINE_PAF   \
00052     "|" VISIR_PFITS_INT_NDIT             \
00053     "|" VISIR_PFITS_INT_CHOP_NCYCLES     \
00054     "|" VISIR_PFITS_STRING_FILTER1       \
00055     "|" VISIR_PFITS_STRING_FILTER2
00056 
00057 /* FITS keys to be loaded for first raw file, in case WCS is used */
00058 #define RECIPE_KEYS_REGEXP_WCS \
00059         RECIPE_KEYS_REGEXP \
00060     "|" IRPLIB_PFITS_WCS_REGEXP
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Private Functions prototypes
00064  -----------------------------------------------------------------------------*/
00065 
00066 static cpl_error_code visir_img_combine_qc(cpl_propertylist *,
00067                                            cpl_boolean,
00068                                            const irplib_framelist *,
00069                                            const cpl_image *);
00070 
00071 static cpl_error_code visir_img_combine_save(cpl_frameset *,
00072                                              const cpl_parameterlist *,
00073                                              const cpl_propertylist *,
00074                                              const cpl_image *,
00075                                              const cpl_image *,
00076                                              const cpl_image *,
00077                                              const cpl_image *,
00078                                              const char      *,
00079                                              const char      *);
00080 
00081 VISIR_RECIPE_DEFINE(visir_img_combine,
00082                     VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |    
00083                     VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS | 
00084                     VISIR_PARAM_NODPOS |  VISIR_PARAM_AUTOBPM | 
00085                     VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |    
00086                     VISIR_PARAM_UNION  | VISIR_PARAM_REJECT |   
00087                     VISIR_PARAM_COMBINE | VISIR_PARAM_ECCMAX |
00088                     VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
00089                     VISIR_PARAM_PLOT,
00090                     "Images combination recipe",
00091                     "This recipe recombines the data observed in "
00092                     "chopping/nodding or\n"
00093                     "chopping or nodding modes into one combined image using "
00094                     "optionally\n"
00095                     "cross-correlation methods.\n"
00096                     "The files listed in the Set Of Frames (sof-file) "
00097                     "must be tagged:\n"
00098                     "VISIR-observation-file.fits " VISIR_IMG_COMBINE_CNJ " or\n"
00099                     "VISIR-observation-file.fits " VISIR_IMG_COMBINE_CJ " or\n"
00100                     "VISIR-observation-file.fits " VISIR_IMG_COMBINE_NJ " or\n"
00101                     "VISIR-observation-file.fits " VISIR_IMG_COMBINE_DJ "\n"
00102                     "\n"
00103                     "The corresponding primary product will have a FITS card\n"
00104                     "'HIERARCH ESO PRO CATG' with a value of\n"
00105                     VISIR_IMG_COMBINE_COMBINED_PROCATG_CNJ " or\n"
00106                     VISIR_IMG_COMBINE_COMBINED_PROCATG_CJ " or\n"
00107                     VISIR_IMG_COMBINE_COMBINED_PROCATG_NJ " or\n"
00108                     VISIR_IMG_COMBINE_COMBINED_PROCATG_DJ
00109                     " and the corresponding beam-collapsed product will "
00110                     "have a FITS card\n"
00111                     "'HIERARCH ESO PRO CATG' with a value of\n"
00112                     VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CNJ " or\n"
00113                     VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CJ " or\n"
00114                     VISIR_IMG_COMBINE_ONEBEAM_PROCATG_NJ " or\n"
00115                     VISIR_IMG_COMBINE_ONEBEAM_PROCATG_DJ
00116                     "\n"
00117                     MAN_VISIR_CALIB_BPM_IMG);
00118 
00119 /*----------------------------------------------------------------------------*/
00123 /*----------------------------------------------------------------------------*/
00124 
00125 /*-----------------------------------------------------------------------------
00126                                 Functions code
00127  -----------------------------------------------------------------------------*/
00128 
00129 /*----------------------------------------------------------------------------*/
00136 /*----------------------------------------------------------------------------*/
00137 static int visir_img_combine(cpl_frameset            * framelist,
00138                              const cpl_parameterlist * parlist)
00139 {
00140     cpl_errorstate  cleanstate = cpl_errorstate_get();
00141     irplib_framelist * allframes = NULL;
00142     irplib_framelist * rawframes = NULL;
00143     cpl_propertylist * qclist    = cpl_propertylist_new();
00144     const char       * badpix;
00145     const char       * flat;
00146     cpl_image       ** combined        = NULL;
00147     cpl_image       ** beam1           = NULL;
00148     const char       * procatg_combine = NULL;
00149     const char       * procatg_onebeam = NULL;
00150     cpl_boolean        drop_wcs;
00151     const char       * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS ")$";
00152 
00153     const char * combine_string;
00154     cpl_geom_combine combine_mode;
00155 
00156     /* Retrieve input parameters */
00157     combine_string = visir_parameterlist_get_string(parlist, RECIPE_STRING,
00158                                                     VISIR_PARAM_COMBINE);
00159 
00160     bug_if (combine_string == NULL);
00161 
00162     if (combine_string[0] == 'u')
00163         combine_mode = CPL_GEOM_UNION;
00164     else if (combine_string[0] == 'f')
00165         combine_mode = CPL_GEOM_FIRST;
00166     else if (combine_string[0] == 'i')
00167         combine_mode = CPL_GEOM_INTERSECT;
00168     else
00169         skip_if(1);
00170 
00171     /* Identify the RAW and CALIB frames in the input frameset */
00172     skip_if (visir_dfs_set_groups(framelist));
00173 
00174     /* Objects observation */
00175     allframes = irplib_framelist_cast(framelist);
00176     skip_if(allframes == NULL);
00177     rawframes = irplib_framelist_extract_regexp(allframes, "^("
00178                                                 VISIR_IMG_COMBINE_CNJ "|"
00179                                                 VISIR_IMG_COMBINE_CJ  "|"
00180                                                 VISIR_IMG_COMBINE_NJ  "|"
00181                                                 VISIR_IMG_COMBINE_DJ ")$",
00182                                                 CPL_FALSE);
00183     skip_if (rawframes == NULL);
00184     irplib_framelist_empty(allframes);
00185 
00186     skip_if( irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
00187                                                 CPL_FALSE));
00188 
00189     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
00190                                                    RECIPE_KEYS_REGEXP_ALL
00191                                                    ")$", CPL_FALSE));
00192 
00193     skip_if(visir_dfs_check_framelist_tag(rawframes));
00194     
00195     /* Set PRO.CATG - and verify uniqueness of frame type */
00196     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_DJ)) {
00197         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_DJ;
00198         procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_DJ;
00199     }
00200     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_NJ)) {
00201         skip_if (procatg_combine != NULL);
00202         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_NJ;
00203         procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_NJ;
00204     }
00205     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_CJ)) {
00206         skip_if (procatg_combine != NULL);
00207         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_CJ;
00208         procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CJ;
00209     }
00210     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_CNJ)) {
00211         skip_if (procatg_combine != NULL);
00212         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_CNJ;
00213         procatg_onebeam = VISIR_IMG_COMBINE_ONEBEAM_PROCATG_CNJ;
00214     }
00215     bug_if (procatg_combine == NULL);
00216 
00217     /* Bad pixels calibration file */
00218     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00219 
00220     /* Flatfield calibration file */
00221     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00222 
00223     /* Combine the frames */
00224     combined = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix,
00225                                    flat, combine_mode, &drop_wcs,
00226                                    CPL_FALSE, 0.0, 0);
00227 
00228     if (combined == NULL) {
00229         cpl_msg_error(cpl_func, "Could not combine the input frames");
00230         skip_if(1);
00231     }
00232 
00233     beam1 = visir_img_collapse_beam(qclist, combined[0], parlist, RECIPE_STRING,
00234                                     VISIR_CHOPNOD_AUTO,
00235                                     irplib_framelist_get_propertylist_const
00236                                     (rawframes, 0));
00237 
00238     if (beam1 == NULL) {
00239         irplib_error_recover(cleanstate, "Could not collapse the beams of "
00240                              "the combined image");
00241     }
00242 
00243     /* Add QC parameters */
00244     skip_if(visir_img_combine_qc(qclist, drop_wcs, rawframes, combined[0]));
00245 
00246     /* Reduce maximum number of pointers used */
00247     irplib_framelist_empty(rawframes);
00248 
00249     /* Save the combined image and contribution map */
00250     cpl_msg_info(cpl_func, "Save the combined image and contribution map");
00251 
00252     if (beam1 == NULL) {
00253         skip_if(visir_img_combine_save(framelist, parlist, qclist,
00254                                        combined[0], combined[1], NULL,
00255                                        NULL, procatg_combine, procatg_onebeam));
00256     } else {
00257         skip_if(visir_img_combine_save(framelist, parlist, qclist,
00258                                        combined[0], combined[1], beam1[0],
00259                                        beam1[1], procatg_combine,
00260                                        procatg_onebeam));
00261     }
00262 
00263     end_skip;
00264 
00265     if (combined) {
00266         cpl_image_delete(combined[0]);
00267         cpl_image_delete(combined[1]);
00268         cpl_free(combined);
00269     }
00270 
00271     if (beam1) {
00272         cpl_image_delete(beam1[0]);
00273         cpl_image_delete(beam1[1]);
00274         cpl_free(beam1);
00275     }
00276 
00277     irplib_framelist_delete(allframes);
00278     irplib_framelist_delete(rawframes);
00279     cpl_propertylist_delete(qclist);
00280 
00281     return cpl_error_get_code();
00282 }
00283 
00284 /*----------------------------------------------------------------------------*/
00293 /*----------------------------------------------------------------------------*/
00294 static cpl_error_code visir_img_combine_qc(cpl_propertylist       * qclist,
00295                                            cpl_boolean              drop_wcs,
00296                                            const irplib_framelist * rawframes,
00297                                            const cpl_image        * combined)
00298 {
00299 
00300     const cpl_propertylist * reflist
00301         = irplib_framelist_get_propertylist_const(rawframes, 0);
00302     double bg_sigma;
00303 
00304 
00305     bug_if (0);
00306 
00307     /* QC.EXPTIME */
00308     skip_if (visir_qc_append_exptime(qclist, rawframes));
00309 
00310     /* Copy the filter name */
00311     skip_if (visir_qc_append_filter(qclist, rawframes));
00312 
00313 
00314     /* QC.BACKGD.MEAN */
00315     /* Compute the background values of the HCYCLE frames */
00316     skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
00317 
00318     /* Compute the background sigma */
00319     bg_sigma = visir_img_phot_sigma_clip(combined);
00320 
00321     skip_if (0);
00322 
00323     /* QC.BACKGD.SIGMA */
00324     bug_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD SIGMA",
00325                                            bg_sigma));
00326 
00327     /* QC.CAPA */
00328     skip_if (visir_qc_append_capa(qclist, rawframes));
00329 
00330     if (drop_wcs) {
00331         cpl_propertylist * pcopy = cpl_propertylist_new();
00332         const cpl_error_code error
00333             = cpl_propertylist_copy_property_regexp(pcopy, reflist, "^("
00334                                                     IRPLIB_PFITS_WCS_REGEXP
00335                                                     ")$", 0);
00336         if (!error && cpl_propertylist_get_size(pcopy) > 0) {
00337             cpl_msg_warning(cpl_func, "Combined image will have no WCS "
00338                             "coordinates");
00339         }
00340         cpl_propertylist_delete(pcopy);
00341         bug_if(0);
00342     } else {
00343         bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
00344                                                      IRPLIB_PFITS_WCS_REGEXP
00345                                                      ")$", 0));
00346     }
00347 
00348     end_skip;
00349 
00350     return cpl_error_get_code();
00351 }
00352 
00353 /*----------------------------------------------------------------------------*/
00367 /*----------------------------------------------------------------------------*/
00368 static
00369 cpl_error_code visir_img_combine_save(cpl_frameset            * self,
00370                                       const cpl_parameterlist * parlist,
00371                                       const cpl_propertylist  * qclist,
00372                                       const cpl_image         * combined,
00373                                       const cpl_image         * contrib,
00374                                       const cpl_image         * beam1,
00375                                       const cpl_image         * beam1i,
00376                                       const char              * procat_c,
00377                                       const char              * procat_b)
00378 {
00379     cpl_propertylist * xtlist = cpl_propertylist_new();
00380 
00381 
00382     bug_if(cpl_propertylist_append_string(xtlist, "EXTNAME",
00383                                           "Contribution Map"));
00384 
00385     /* THE COMBINED IMAGE */
00386     skip_if (irplib_dfs_save_image(self, parlist, self, combined,
00387                                    CPL_BPP_IEEE_FLOAT, RECIPE_STRING, procat_c,
00388                                    qclist, NULL, visir_pipe_id,
00389                                    RECIPE_STRING CPL_DFS_FITS));
00390 
00391     /* THE CONTRIBUTION MAP */
00392     skip_if (cpl_image_save(contrib, RECIPE_STRING CPL_DFS_FITS,
00393                             CPL_BPP_16_UNSIGNED, xtlist, CPL_IO_EXTEND));
00394 
00395     if (beam1 != NULL) {
00396         /* THE BEAM COLLAPSED IMAGE */
00397         skip_if (irplib_dfs_save_image(self, parlist, self, beam1,
00398                                        CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00399                                        procat_b, qclist, NULL, visir_pipe_id,
00400                                        RECIPE_STRING "_onebeam" CPL_DFS_FITS));
00401 
00402         /* THE BEAM COLLAPSED CONTRIBUTION MAP */
00403         skip_if (cpl_image_save(beam1i, RECIPE_STRING "_onebeam" CPL_DFS_FITS,
00404                                 CPL_BPP_8_UNSIGNED, xtlist, CPL_IO_EXTEND));
00405     }
00406 
00407     end_skip;
00408 
00409     cpl_propertylist_delete(xtlist);
00410 
00411     return cpl_error_get_code();
00412 
00413 }

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