visir_img_combine.c

00001 /* $Id: visir_img_combine.c,v 1.100 2010/03/09 12:52:18 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/03/09 12:52:18 $
00024  * $Revision: 1.100 $
00025  * $Name: visir-3_3_1-public $
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_propertylist *,
00068                                            cpl_boolean,
00069                                            const irplib_framelist *,
00070                                            const cpl_image *);
00071 
00072 static cpl_error_code visir_img_combine_save(cpl_frameset *,
00073                                              const cpl_parameterlist *,
00074                                              const cpl_propertylist *,
00075                                              const cpl_propertylist *,
00076                                              const cpl_image *,
00077                                              const char      *,
00078                                              const cpl_image *,
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 |
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                     "\n"
00110                     MAN_VISIR_CALIB_BPM_IMG);
00111 
00112 /*----------------------------------------------------------------------------*/
00116 /*----------------------------------------------------------------------------*/
00117 
00118 /*-----------------------------------------------------------------------------
00119                                 Functions code
00120  -----------------------------------------------------------------------------*/
00121 
00122 /*----------------------------------------------------------------------------*/
00129 /*----------------------------------------------------------------------------*/
00130 static int visir_img_combine(cpl_frameset            * framelist,
00131                              const cpl_parameterlist * parlist)
00132 {
00133     irplib_framelist * allframes = NULL;
00134     irplib_framelist * rawframes = NULL;
00135     cpl_propertylist * qclist    = cpl_propertylist_new();
00136     cpl_propertylist * paflist   = cpl_propertylist_new();
00137     const char       * badpix;
00138     const char       * flat;
00139     cpl_image       ** combined = NULL;
00140     const char       * procatg_combine = NULL;
00141     const char       * procatg_contrib = NULL;
00142     cpl_boolean        drop_wcs;
00143     const char       * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS ")$";
00144 
00145     const char * combine_string;
00146     cpl_geom_combine combine_mode;
00147 
00148     /* Retrieve input parameters */
00149     combine_string = visir_parameterlist_get_string(parlist, RECIPE_STRING,
00150                                                     VISIR_PARAM_COMBINE);
00151 
00152     bug_if (combine_string == NULL);
00153 
00154     if (combine_string[0] == 'u')
00155         combine_mode = CPL_GEOM_UNION;
00156     else if (combine_string[0] == 'f')
00157         combine_mode = CPL_GEOM_FIRST;
00158     else if (combine_string[0] == 'i')
00159         combine_mode = CPL_GEOM_INTERSECT;
00160     else
00161         skip_if(1);
00162 
00163     /* Identify the RAW and CALIB frames in the input frameset */
00164     skip_if (visir_dfs_set_groups(framelist));
00165 
00166     /* Objects observation */
00167     allframes = irplib_framelist_cast(framelist);
00168     skip_if(allframes == NULL);
00169     rawframes = irplib_framelist_extract_regexp(allframes, "^("
00170                                                 VISIR_IMG_COMBINE_CNJ "|"
00171                                                 VISIR_IMG_COMBINE_CJ  "|"
00172                                                 VISIR_IMG_COMBINE_NJ  "|"
00173                                                 VISIR_IMG_COMBINE_DJ ")$",
00174                                                 CPL_FALSE);
00175     skip_if (rawframes == NULL);
00176     irplib_framelist_empty(allframes);
00177 
00178     skip_if( irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
00179                                                 CPL_FALSE));
00180 
00181     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
00182                                                    RECIPE_KEYS_REGEXP_ALL
00183                                                    ")$", CPL_FALSE));
00184 
00185     skip_if(visir_dfs_check_framelist_tag(rawframes));
00186     
00187     /* Set PRO.CATG - and verify uniqueness of frame type */
00188     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_DJ)) {
00189         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_DJ;
00190         procatg_contrib = VISIR_IMG_COMBINE_CONTRIB_PROCATG_DJ;
00191     }
00192     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_NJ)) {
00193         skip_if (procatg_combine != NULL);
00194         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_NJ;
00195         procatg_contrib = VISIR_IMG_COMBINE_CONTRIB_PROCATG_NJ;
00196     }
00197     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_CJ)) {
00198         skip_if (procatg_combine != NULL);
00199         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_CJ;
00200         procatg_contrib = VISIR_IMG_COMBINE_CONTRIB_PROCATG_CJ;
00201     }
00202     if (cpl_frameset_find(framelist, VISIR_IMG_COMBINE_CNJ)) {
00203         skip_if (procatg_combine != NULL);
00204         procatg_combine = VISIR_IMG_COMBINE_COMBINED_PROCATG_CNJ;
00205         procatg_contrib = VISIR_IMG_COMBINE_CONTRIB_PROCATG_CNJ;
00206     }
00207     bug_if (procatg_combine == NULL);
00208 
00209     /* Bad pixels calibration file */
00210     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00211 
00212     /* Flatfield calibration file */
00213     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00214 
00215     /* Combine the frames */
00216     combined = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix, flat,
00217                                    combine_mode, &drop_wcs, CPL_FALSE, 0.0, 0);
00218 
00219     if (combined == NULL) {
00220         cpl_msg_error(cpl_func, "Could not combine the input frames");
00221         skip_if(1);
00222     }
00223     
00224     /* Add QC parameters */
00225     skip_if(visir_img_combine_qc(qclist, paflist, drop_wcs, rawframes,
00226                                  combined[0]));
00227 
00228     /* Reduce maximum number of pointers used */
00229     irplib_framelist_empty(rawframes);
00230 
00231     /* Save the combined image and contribution map */
00232     cpl_msg_info(cpl_func, "Save the combined image and contribution map");
00233 
00234     /* PRO.CATG */
00235     bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00236                                            procatg_combine));
00237 
00238     skip_if(visir_img_combine_save(framelist, parlist, qclist, paflist,
00239                                    combined[0], procatg_combine,
00240                                    combined[1], procatg_contrib));
00241 
00242     end_skip;
00243 
00244     if (combined) {
00245         cpl_image_delete(combined[0]);
00246         cpl_image_delete(combined[1]);
00247         cpl_free(combined);
00248     }
00249 
00250     irplib_framelist_delete(allframes);
00251     irplib_framelist_delete(rawframes);
00252     cpl_propertylist_delete(qclist);
00253     cpl_propertylist_delete(paflist);
00254 
00255     return cpl_error_get_code();
00256 }
00257 
00258 /*----------------------------------------------------------------------------*/
00268 /*----------------------------------------------------------------------------*/
00269 static cpl_error_code visir_img_combine_qc(cpl_propertylist       * qclist,
00270                                            cpl_propertylist       * paflist,
00271                                            cpl_boolean              drop_wcs,
00272                                            const irplib_framelist * rawframes,
00273                                            const cpl_image        * combined)
00274 {
00275 
00276     const cpl_propertylist * reflist
00277         = irplib_framelist_get_propertylist_const(rawframes, 0);
00278     double bg_sigma;
00279 
00280 
00281     bug_if (0);
00282 
00283     /* QC.EXPTIME */
00284     skip_if (visir_qc_append_exptime(qclist, rawframes));
00285 
00286     /* Copy the filter name */
00287     skip_if (visir_qc_append_filter(qclist, rawframes));
00288 
00289 
00290     /* QC.BACKGD.MEAN */
00291     /* Compute the background values of the HCYCLE frames */
00292     skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
00293 
00294     /* Compute the background sigma */
00295     bg_sigma = visir_img_phot_sigma_clip(combined);
00296 
00297     skip_if (0);
00298 
00299     /* QC.BACKGD.SIGMA */
00300     bug_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD SIGMA",
00301                                            bg_sigma));
00302 
00303     /* QC.CAPA */
00304     skip_if (visir_qc_append_capa(qclist, rawframes));
00305 
00306     /* Copy the keywords for the paf file */
00307     bug_if(cpl_propertylist_copy_property_regexp(paflist, reflist, "^("
00308                                                  VISIR_PFITS_REGEXP_COMBINE_PAF
00309                                                  ")$", 0));
00310     bug_if (cpl_propertylist_append(paflist, qclist));
00311 
00312     if (drop_wcs) {
00313         cpl_propertylist * pcopy = cpl_propertylist_new();
00314         const cpl_error_code error
00315             = cpl_propertylist_copy_property_regexp(pcopy, reflist, "^("
00316                                                     IRPLIB_PFITS_WCS_REGEXP
00317                                                     ")$", 0);
00318         if (!error && cpl_propertylist_get_size(pcopy) > 0) {
00319             cpl_msg_warning(cpl_func, "Combined image will have no WCS "
00320                             "coordinates");
00321         }
00322         cpl_propertylist_delete(pcopy);
00323         bug_if(0);
00324     } else {
00325         bug_if(cpl_propertylist_copy_property_regexp(qclist, reflist, "^("
00326                                                      IRPLIB_PFITS_WCS_REGEXP
00327                                                      ")$", 0));
00328     }
00329 
00330     end_skip;
00331 
00332     return cpl_error_get_code();
00333 }
00334 
00335 /*----------------------------------------------------------------------------*/
00348 /*----------------------------------------------------------------------------*/
00349 static cpl_error_code visir_img_combine_save(cpl_frameset            * self,
00350                                              const cpl_parameterlist * parlist,
00351                                              const cpl_propertylist  * qclist,
00352                                              const cpl_propertylist  * paflist,
00353                                              const cpl_image         * combined,
00354                                              const char              * procat_c,
00355                                              const cpl_image         * contrib,
00356                                              const char              * procat_i)
00357 {
00358     bug_if (0);
00359 
00360     /* THE COMBINED IMAGE */
00361     skip_if (irplib_dfs_save_image(self, parlist, self, combined,
00362                                CPL_BPP_IEEE_FLOAT, RECIPE_STRING, procat_c, qclist,
00363                                NULL, visir_pipe_id,
00364                                RECIPE_STRING CPL_DFS_FITS));
00365 
00366     /* THE CONTRIBUTION MAP */
00367     skip_if (irplib_dfs_save_image(self, parlist, self, contrib, CPL_BPP_32_SIGNED,
00368                                RECIPE_STRING, procat_i, qclist, NULL,
00369                                visir_pipe_id,
00370                                RECIPE_STRING "_contrib" CPL_DFS_FITS));
00371 
00372 #ifdef VISIR_SAVE_PAF
00373     /* THE PAF FILE FOR QC PARAMETERS */
00374 
00375     skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
00376                               RECIPE_STRING CPL_DFS_PAF));
00377 #else
00378     bug_if(paflist == NULL);
00379 #endif
00380 
00381     end_skip;
00382 
00383     return cpl_error_get_code();
00384 
00385 }

Generated on Tue Jun 29 12:20:52 2010 for VISIR Pipeline Reference Manual by  doxygen 1.4.7