crires_util_combine.c

00001 /* $Id: crires_util_combine.c,v 1.39 2011/02/09 15:41:46 yjung Exp $
00002  *
00003  * This file is part of the CRIRES 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: yjung $
00023  * $Date: 2011/02/09 15:41:46 $
00024  * $Revision: 1.39 $
00025  * $Name: crire-2_1_1 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "crires_recipe.h"
00037 
00038 #include "crires_combine.h"
00039 #include "crires_wlcalib.h"
00040 #include "crires_extract.h"
00041 #include "crires_photom.h"
00042 
00043 /*-----------------------------------------------------------------------------
00044                                 Define
00045  -----------------------------------------------------------------------------*/
00046 
00047 #define RECIPE_STRING "crires_util_combine"
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Functions prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static int crires_util_combine_save(const cpl_imagelist **, 
00054         const cpl_parameterlist *, cpl_frameset *) ;
00055 
00056 static char crires_util_combine_description[] =
00057 "crires_util_combine -- Images Combination tool\n"
00058 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00059 "raw-file.fits "CRIRES_SPEC_JITTER_RAW" or\n"
00060 "raw-file.fits "CRIRES_SPEC_JITTER_J_RAW" or\n"
00061 "raw-file.fits "CRIRES_SPEC_JITTER_STD_RAW" or\n"
00062 "raw-file.fits "CRIRES_SPEC_JITTER_J_STD_RAW" or\n"
00063 "raw-file.fits "CRIRES_SPEC_NODDING_OBJECT_RAW" or\n"
00064 "raw-file.fits "CRIRES_SPEC_NODDING_SKY_RAW" or\n"
00065 "raw-file.fits "CRIRES_SPEC_GENERIC_OBJECT_RAW" or\n"
00066 "raw-file.fits "CRIRES_SPEC_GENERIC_SKY_RAW" or\n"
00067 "raw-file.fits "CRIRES_SPEC_NODDING_RAW" or\n"
00068 "raw-file.fits "CRIRES_SPEC_NODDING_J_RAW" or\n"
00069 "raw-file.fits "CRIRES_SPEC_NODDING_STD_RAW" or\n"
00070 "raw-file.fits "CRIRES_SPEC_NODDING_J_STD_RAW" or\n"
00071 "flat-file.fits "CRIRES_CALPRO_FLAT" or\n" 
00072 "bpm-file.fits "CRIRES_CALPRO_BPM" or\n" 
00073 "dark-file.fits "CRIRES_CALPRO_DARK" or\n" 
00074 "detlin-file.fits "CRIRES_CALPRO_COEFFS_CUBE" or\n"   
00075 "\n"
00076 "This recipe produces 2 files:\n"
00077 "First product:     the combined image\n"
00078 "                   (PRO TYPE = "CRIRES_PROTYPE_COMBINED")\n"
00079 "Second product:    the contribution map\n"
00080 "                   (PRO TYPE = "CRIRES_PROTYPE_CONTRIB")\n" ;
00081 
00082 CRIRES_RECIPE_DEFINE(crires_util_combine,
00083         CRIRES_PARAM_REFINE         |
00084         CRIRES_PARAM_ONLYA          |
00085         CRIRES_PARAM_ONLYB          |
00086         CRIRES_PARAM_BLIND,
00087         "Images Combination tool",
00088         crires_util_combine_description) ;
00089 
00090 /*-----------------------------------------------------------------------------
00091                             Static variables
00092  -----------------------------------------------------------------------------*/
00093 
00094 static struct {
00095     /* Inputs */
00096     int                 blind ;
00097     int                 refine ;
00098     int                 onlyA ;
00099     int                 onlyB ;
00100     int                 nodding ;
00101     /* Outputs */
00102     crires_illum_period period ;
00103 } crires_util_combine_config ;
00104 
00105 /*-----------------------------------------------------------------------------
00106                                 Functions code
00107  -----------------------------------------------------------------------------*/
00108 
00109 /*----------------------------------------------------------------------------*/
00116 /*----------------------------------------------------------------------------*/
00117 static int crires_util_combine(
00118         cpl_frameset            *   frameset,
00119         const cpl_parameterlist *   parlist)
00120 {
00121     cpl_frameset    *   rawframes ;
00122     cpl_frameset    *   skyframes ;
00123     const char      *   flat ;
00124     const char      *   dark ;
00125     const char      *   bpm ;
00126     const char      *   detlin ;
00127     cpl_imagelist   **  comblist ;
00128 
00129     /* Initialise */
00130     rawframes = NULL ;
00131     skyframes = NULL ;
00132     
00133     /* Retrieve input parameters */
00134     crires_util_combine_config.refine = crires_parameterlist_get_bool(
00135             parlist, RECIPE_STRING, CRIRES_PARAM_REFINE) ;
00136     crires_util_combine_config.onlyA = crires_parameterlist_get_bool(
00137             parlist, RECIPE_STRING, CRIRES_PARAM_ONLYA) ;
00138     crires_util_combine_config.onlyB = crires_parameterlist_get_bool(
00139             parlist, RECIPE_STRING, CRIRES_PARAM_ONLYB) ;
00140     crires_util_combine_config.blind = crires_parameterlist_get_bool(
00141             parlist, RECIPE_STRING, CRIRES_PARAM_BLIND) ;
00142  
00143     /* Identify the RAW and CALIB frames in the input frameset */
00144     if (crires_dfs_set_groups(frameset, NULL)) {
00145         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00146         return -1 ;
00147     }
00148 
00149     /* Retrieve calibration data */
00150     flat        = crires_extract_filename(frameset, CRIRES_CALPRO_FLAT) ;
00151     dark        = crires_extract_filename(frameset, CRIRES_CALPRO_DARK) ;
00152     bpm         = crires_extract_filename(frameset, CRIRES_CALPRO_BPM) ;
00153     detlin      = crires_extract_filename(frameset, CRIRES_CALPRO_COEFFS_CUBE) ;
00154  
00155     /* Retrieve raw frames */
00156     if ((rawframes = crires_extract_frameset(frameset,
00157                     CRIRES_SPEC_JITTER_RAW)) != NULL) {
00158         crires_util_combine_config.nodding = 0 ;
00159     } else if ((rawframes = crires_extract_frameset(frameset,
00160                     CRIRES_SPEC_JITTER_J_RAW)) != NULL) {
00161         crires_util_combine_config.nodding = 0 ;
00162     } else if ((rawframes = crires_extract_frameset(frameset,
00163                     CRIRES_SPEC_NODDING_RAW)) != NULL) {
00164         crires_util_combine_config.nodding = 1 ;
00165     } else if ((rawframes = crires_extract_frameset(frameset,
00166                     CRIRES_SPEC_NODDING_OBJECT_RAW)) != NULL) {
00167         crires_util_combine_config.nodding = 1 ;
00168     } else if ((rawframes = crires_extract_frameset(frameset,
00169                     CRIRES_SPEC_NODDING_J_RAW)) != NULL) {
00170         crires_util_combine_config.nodding = 1 ;
00171     } else if ((rawframes = crires_extract_frameset(frameset,
00172                     CRIRES_SPEC_GENERIC_OBJECT_RAW)) != NULL) {
00173         crires_util_combine_config.nodding = 0 ;
00174     } else if ((rawframes = crires_extract_frameset(frameset,
00175                     CRIRES_SPEC_JITTER_STD_RAW)) != NULL) {
00176         crires_util_combine_config.nodding = 0 ;
00177     } else if ((rawframes = crires_extract_frameset(frameset,
00178                     CRIRES_SPEC_JITTER_J_STD_RAW)) != NULL) {
00179         crires_util_combine_config.nodding = 0 ;
00180     } else if ((rawframes = crires_extract_frameset(frameset,
00181                     CRIRES_SPEC_NODDING_STD_RAW)) != NULL) {
00182         crires_util_combine_config.nodding = 1 ;
00183     } else if ((rawframes = crires_extract_frameset(frameset,
00184                     CRIRES_SPEC_NODDING_J_STD_RAW)) != NULL) {
00185         crires_util_combine_config.nodding = 1 ;
00186     } else {
00187         cpl_msg_error(__func__, "No raw frame in input") ;
00188         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00189         return -1 ;
00190     }
00191 
00192     /* OnlyA and OnlyB are only possible in nodding mode */
00193     if (crires_util_combine_config.nodding == 0) {
00194         if (crires_util_combine_config.onlyA) {
00195             cpl_msg_warning(__func__, "onlyA only possible in nodding mode") ;
00196             crires_util_combine_config.onlyA = 0 ;
00197         }
00198         if (crires_util_combine_config.onlyB) {
00199             cpl_msg_warning(__func__, "onlyB only possible in nodding mode") ;
00200             crires_util_combine_config.onlyB = 0 ;
00201         }
00202     }
00203 
00204     /* Retrieve sky frames if any */
00205     skyframes = crires_extract_frameset(frameset, CRIRES_SPEC_NODDING_SKY_RAW) ;
00206     if (skyframes == NULL) {
00207         skyframes = crires_extract_frameset(frameset,
00208                 CRIRES_SPEC_GENERIC_SKY_RAW) ;
00209     }
00210 
00211     /* Get the detector illumination period */
00212     crires_util_combine_config.period =
00213         crires_get_detector_illum_period(
00214                 cpl_frame_get_filename(cpl_frameset_get_frame(rawframes, 0))) ;
00215     if (crires_util_combine_config.period == CRIRES_ILLUM_UNKNOWN) {
00216         cpl_msg_error(__func__,
00217                 "Cannot determine the detector illumination period") ;
00218         cpl_frameset_delete(rawframes) ;
00219         if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00220         return -1 ;
00221     } else {
00222         crires_display_detector_illum(crires_util_combine_config.period) ;
00223     }
00224 
00225     /* Images recombination */
00226     cpl_msg_info(__func__, "Images combination") ;
00227     cpl_msg_indent_more() ;
00228     if ((comblist = crires_combine_imagelist(rawframes, skyframes,
00229                     crires_util_combine_config.period,
00230                     flat, dark, bpm, detlin,
00231                     crires_util_combine_config.nodding,
00232                     crires_util_combine_config.blind,
00233                     crires_util_combine_config.refine,
00234                     crires_util_combine_config.onlyA,
00235                     crires_util_combine_config.onlyB)) == NULL) {
00236         cpl_msg_error(__func__, "Cannot combine the images") ;
00237         cpl_frameset_delete(rawframes) ;
00238         if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00239         cpl_msg_indent_less() ;
00240         return -1 ;
00241     }
00242     cpl_frameset_delete(rawframes) ;
00243     if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00244     cpl_msg_indent_less() ;
00245     
00246     /* Save the product */
00247     cpl_msg_info(__func__, "Save the product") ;
00248     cpl_msg_indent_more() ;
00249     if (crires_util_combine_save((const cpl_imagelist **)comblist, parlist, 
00250                 frameset)) {
00251         cpl_msg_error(__func__, "Cannot save the product") ;
00252         cpl_imagelist_delete(comblist[0]) ;
00253         cpl_imagelist_delete(comblist[1]) ;
00254         if (crires_util_combine_config.onlyA) {
00255             cpl_imagelist_delete(comblist[2]) ;
00256             cpl_imagelist_delete(comblist[3]) ;
00257         }
00258         if (crires_util_combine_config.onlyB) {
00259             cpl_imagelist_delete(comblist[4]) ;
00260             cpl_imagelist_delete(comblist[5]) ;
00261         }
00262         cpl_free(comblist) ;
00263         cpl_msg_indent_less() ;
00264         return -1 ;
00265     }
00266     cpl_imagelist_delete(comblist[0]) ;
00267     cpl_imagelist_delete(comblist[1]) ;
00268     if (crires_util_combine_config.onlyA) {
00269         cpl_imagelist_delete(comblist[2]) ;
00270         cpl_imagelist_delete(comblist[3]) ;
00271     }
00272     if (crires_util_combine_config.onlyB) {
00273         cpl_imagelist_delete(comblist[4]) ;
00274         cpl_imagelist_delete(comblist[5]) ;
00275     }
00276     cpl_free(comblist) ;
00277     cpl_msg_indent_less() ;
00278 
00279     /* Return */
00280     if (cpl_error_get_code()) return -1 ;
00281     else return 0 ;
00282 }
00283 
00284 /*----------------------------------------------------------------------------*/
00293 /*----------------------------------------------------------------------------*/
00294 static int crires_util_combine_save(
00295         const cpl_imagelist     **  images,
00296         const cpl_parameterlist *   parlist,
00297         cpl_frameset            *   set)
00298 {
00299     const char          *   recipe_name = "crires_util_combine" ;
00300 
00301     /* Write the combined image */
00302     crires_image_save(set,
00303             parlist,
00304             set, 
00305             images[0], 
00306             recipe_name,
00307             CRIRES_OBS_COMBINED_IMA, 
00308             CRIRES_PROTYPE_COMBINED,
00309             crires_util_combine_config.period,
00310             NULL,
00311             NULL,
00312             PACKAGE "/" PACKAGE_VERSION,
00313             "crires_util_combine_comb.fits") ;
00314 
00315     /* Write the contribution map */
00316     crires_image_save(set,
00317             parlist,
00318             set, 
00319             images[1], 
00320             recipe_name,
00321             CRIRES_OBS_CONTRIBUTION_IMA, 
00322             CRIRES_PROTYPE_CONTRIB,
00323             crires_util_combine_config.period,
00324             NULL,
00325             NULL,
00326             PACKAGE "/" PACKAGE_VERSION,
00327             "crires_util_combine_contrib.fits") ;
00328 
00329     /* Nodded A support */
00330     if (crires_util_combine_config.onlyA) {
00331         /* Write the combined Nodded A image */
00332         crires_image_save(set,
00333                 parlist,
00334                 set,
00335                 images[2],
00336                 recipe_name,
00337                 CRIRES_OBS_COMBINED_NA_IMA,
00338                 CRIRES_PROTYPE_COMBINED,
00339                 crires_util_combine_config.period,
00340                 NULL,
00341                 NULL,
00342                 PACKAGE "/" PACKAGE_VERSION,
00343                 "crires_util_combine_comb_noddedA.fits") ;
00344 
00345         /* Write the contribution Nodded A image */
00346         crires_image_save(set,
00347                 parlist,
00348                 set,
00349                 images[3],
00350                 recipe_name,
00351                 CRIRES_OBS_CONTRIBUTION_NA_IMA,
00352                 CRIRES_PROTYPE_CONTRIB,
00353                 crires_util_combine_config.period,
00354                 NULL,
00355                 NULL,
00356                 PACKAGE "/" PACKAGE_VERSION,
00357                 "crires_util_combine_contrib_noddedA.fits") ;
00358     }
00359 
00360     /* Nodded B support */
00361     if (crires_util_combine_config.onlyB) {
00362         /* Write the combined Nodded B image */
00363         crires_image_save(set,
00364                 parlist,
00365                 set,
00366                 images[4],
00367                 recipe_name,
00368                 CRIRES_OBS_COMBINED_NB_IMA,
00369                 CRIRES_PROTYPE_COMBINED,
00370                 crires_util_combine_config.period,
00371                 NULL,
00372                 NULL,
00373                 PACKAGE "/" PACKAGE_VERSION,
00374                 "crires_util_combine_comb_noddedB.fits") ;
00375 
00376         /* Write the contribution Nodded B image */
00377         crires_image_save(set,
00378                 parlist,
00379                 set,
00380                 images[5],
00381                 recipe_name,
00382                 CRIRES_OBS_CONTRIBUTION_NB_IMA,
00383                 CRIRES_PROTYPE_CONTRIB,
00384                 crires_util_combine_config.period,
00385                 NULL,
00386                 NULL,
00387                 PACKAGE "/" PACKAGE_VERSION,
00388                 "crires_util_combine_contrib_noddedB.fits") ;
00389     }
00390     return  0;
00391 }
00392 

Generated on 22 Mar 2011 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1