CRIRES Pipeline Reference Manual  2.3.2
crires_util_combine.c
00001 /* $Id: crires_util_combine.c,v 1.41 2012-09-19 14:10:27 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: 2012-09-19 14:10:27 $
00024  * $Revision: 1.41 $
00025  * $Name: not supported by cvs2svn $
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 "raw-file.fits "CRIRES_WIN_NODDING_OBJECT_RAW" or\n"
00072 "raw-file.fits "CRIRES_WIN_NODDING_SKY_RAW" or\n"
00073 "raw-file.fits "CRIRES_WIN_NODDING_RAW" or\n"
00074 "raw-file.fits "CRIRES_WIN_NODDING_J_RAW" or\n"
00075 "raw-file.fits "CRIRES_WIN_NODDING_STD_RAW" or\n"
00076 "raw-file.fits "CRIRES_WIN_NODDING_J_STD_RAW" or\n"
00077 "flat-file.fits "CRIRES_CALPRO_FLAT" or\n" 
00078 "flat-file.fits "CRIRES_CALPRO_FLAT_WIN" or\n" 
00079 "bpm-file.fits "CRIRES_CALPRO_BPM" or\n" 
00080 "bpm-file.fits "CRIRES_CALPRO_BPM_WIN" or\n" 
00081 "dark-file.fits "CRIRES_CALPRO_DARK" or\n" 
00082 "dark-file.fits "CRIRES_CALPRO_DARK_WIN" or\n" 
00083 "detlin-file.fits "CRIRES_CALPRO_COEFFS_CUBE" or\n"   
00084 "\n"
00085 "In the case of a nodding observation, in order not to degrade the \n"
00086 "   instrument high resolution, the combined images using only NODA\n"
00087 "   or NODB nodding positions can be produced on request. (see --onlyA/B)\n"
00088 "\n"
00089 "This recipe produces 2 files:\n"
00090 "First product:     the combined image\n"
00091 "                   (PRO TYPE = "CRIRES_PROTYPE_COMBINED")\n"
00092 "Second product:    the contribution map\n"
00093 "                   (PRO TYPE = "CRIRES_PROTYPE_CONTRIB")\n" ;
00094 
00095 CRIRES_RECIPE_DEFINE(crires_util_combine,
00096         CRIRES_PARAM_REFINE         |
00097         CRIRES_PARAM_ONLYA          |
00098         CRIRES_PARAM_ONLYB          |
00099         CRIRES_PARAM_BLIND,
00100         "Images Combination tool",
00101         crires_util_combine_description) ;
00102 
00103 /*-----------------------------------------------------------------------------
00104                             Static variables
00105  -----------------------------------------------------------------------------*/
00106 
00107 static struct {
00108     /* Inputs */
00109     int                 blind ;
00110     int                 refine ;
00111     int                 onlyA ;
00112     int                 onlyB ;
00113     int                 nodding ;
00114     /* Outputs */
00115     int                 win_mode ;
00116     crires_illum_period period ;
00117 } crires_util_combine_config ;
00118 
00119 /*-----------------------------------------------------------------------------
00120                                 Functions code
00121  -----------------------------------------------------------------------------*/
00122 
00123 /*----------------------------------------------------------------------------*/
00130 /*----------------------------------------------------------------------------*/
00131 static int crires_util_combine(
00132         cpl_frameset            *   frameset,
00133         const cpl_parameterlist *   parlist)
00134 {
00135     cpl_frameset    *   rawframes ;
00136     cpl_frameset    *   skyframes ;
00137     const char      *   fname ;
00138     const char      *   flat ;
00139     const char      *   dark ;
00140     const char      *   bpm ;
00141     const char      *   flat_win ;
00142     const char      *   dark_win ;
00143     const char      *   bpm_win ;
00144     const char      *   detlin ;
00145     cpl_imagelist   **  comblist ;
00146     cpl_propertylist    *   plist ;
00147     const char          *   sval ;
00148 
00149     /* Initialise */
00150     rawframes = NULL ;
00151     skyframes = NULL ;
00152     
00153     /* Retrieve input parameters */
00154     crires_util_combine_config.refine = crires_parameterlist_get_bool(
00155             parlist, RECIPE_STRING, CRIRES_PARAM_REFINE) ;
00156     crires_util_combine_config.onlyA = crires_parameterlist_get_bool(
00157             parlist, RECIPE_STRING, CRIRES_PARAM_ONLYA) ;
00158     crires_util_combine_config.onlyB = crires_parameterlist_get_bool(
00159             parlist, RECIPE_STRING, CRIRES_PARAM_ONLYB) ;
00160     crires_util_combine_config.blind = crires_parameterlist_get_bool(
00161             parlist, RECIPE_STRING, CRIRES_PARAM_BLIND) ;
00162  
00163     /* Identify the RAW and CALIB frames in the input frameset */
00164     if (crires_dfs_set_groups(frameset, NULL)) {
00165         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00166         return -1 ;
00167     }
00168 
00169     /* Retrieve calibration data */
00170     flat        = crires_extract_filename(frameset, CRIRES_CALPRO_FLAT) ;
00171     dark        = crires_extract_filename(frameset, CRIRES_CALPRO_DARK) ;
00172     bpm         = crires_extract_filename(frameset, CRIRES_CALPRO_BPM) ;
00173     flat_win    = crires_extract_filename(frameset, CRIRES_CALPRO_FLAT_WIN) ;
00174     dark_win    = crires_extract_filename(frameset, CRIRES_CALPRO_DARK_WIN) ;
00175     bpm_win     = crires_extract_filename(frameset, CRIRES_CALPRO_BPM_WIN) ;
00176     detlin      = crires_extract_filename(frameset, CRIRES_CALPRO_COEFFS_CUBE) ;
00177  
00178     /* Retrieve raw frames */
00179     if ((rawframes = crires_extract_frameset(frameset,
00180                     CRIRES_SPEC_JITTER_RAW)) != NULL) {
00181         crires_util_combine_config.nodding = 0 ;
00182     } else if ((rawframes = crires_extract_frameset(frameset,
00183                     CRIRES_SPEC_JITTER_J_RAW)) != NULL) {
00184         crires_util_combine_config.nodding = 0 ;
00185     } else if ((rawframes = crires_extract_frameset(frameset,
00186                     CRIRES_SPEC_NODDING_RAW)) != NULL) {
00187         crires_util_combine_config.nodding = 1 ;
00188     } else if ((rawframes = crires_extract_frameset(frameset,
00189                     CRIRES_SPEC_NODDING_OBJECT_RAW)) != NULL) {
00190         crires_util_combine_config.nodding = 1 ;
00191     } else if ((rawframes = crires_extract_frameset(frameset,
00192                     CRIRES_SPEC_NODDING_J_RAW)) != NULL) {
00193         crires_util_combine_config.nodding = 1 ;
00194     } else if ((rawframes = crires_extract_frameset(frameset,
00195                     CRIRES_SPEC_GENERIC_OBJECT_RAW)) != NULL) {
00196         crires_util_combine_config.nodding = 0 ;
00197     } else if ((rawframes = crires_extract_frameset(frameset,
00198                     CRIRES_SPEC_JITTER_STD_RAW)) != NULL) {
00199         crires_util_combine_config.nodding = 0 ;
00200     } else if ((rawframes = crires_extract_frameset(frameset,
00201                     CRIRES_SPEC_JITTER_J_STD_RAW)) != NULL) {
00202         crires_util_combine_config.nodding = 0 ;
00203     } else if ((rawframes = crires_extract_frameset(frameset,
00204                     CRIRES_SPEC_NODDING_STD_RAW)) != NULL) {
00205         crires_util_combine_config.nodding = 1 ;
00206     } else if ((rawframes = crires_extract_frameset(frameset,
00207                     CRIRES_SPEC_NODDING_J_STD_RAW)) != NULL) {
00208         crires_util_combine_config.nodding = 1 ;
00209     } else if ((rawframes = crires_extract_frameset(frameset,
00210                     CRIRES_WIN_NODDING_OBJECT_RAW)) != NULL) {
00211         crires_util_combine_config.nodding = 1 ;
00212     } else if ((rawframes = crires_extract_frameset(frameset,
00213                     CRIRES_WIN_NODDING_RAW)) != NULL) {
00214         crires_util_combine_config.nodding = 1 ;
00215     } else if ((rawframes = crires_extract_frameset(frameset,
00216                     CRIRES_WIN_NODDING_J_RAW)) != NULL) {
00217         crires_util_combine_config.nodding = 1 ;
00218     } else if ((rawframes = crires_extract_frameset(frameset,
00219                     CRIRES_WIN_NODDING_STD_RAW)) != NULL) {
00220         crires_util_combine_config.nodding = 1 ;
00221     } else if ((rawframes = crires_extract_frameset(frameset,
00222                     CRIRES_WIN_NODDING_J_STD_RAW)) != NULL) {
00223         crires_util_combine_config.nodding = 1 ;
00224     } else {
00225         cpl_msg_error(__func__, "No raw frame in input") ;
00226         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00227         return -1 ;
00228     }
00229 
00230     /* OnlyA and OnlyB are only possible in nodding mode */
00231     if (crires_util_combine_config.nodding == 0) {
00232         if (crires_util_combine_config.onlyA) {
00233             cpl_msg_warning(__func__, "onlyA only possible in nodding mode") ;
00234             crires_util_combine_config.onlyA = 0 ;
00235         }
00236         if (crires_util_combine_config.onlyB) {
00237             cpl_msg_warning(__func__, "onlyB only possible in nodding mode") ;
00238             crires_util_combine_config.onlyB = 0 ;
00239         }
00240     }
00241 
00242     /* Retrieve sky frames if any */
00243     skyframes = crires_extract_frameset(frameset, CRIRES_SPEC_NODDING_SKY_RAW) ;
00244     if (skyframes == NULL) {
00245         skyframes = crires_extract_frameset(frameset,
00246                 CRIRES_WIN_NODDING_SKY_RAW) ;
00247     }
00248     if (skyframes == NULL) {
00249         skyframes = crires_extract_frameset(frameset,
00250                 CRIRES_SPEC_GENERIC_SKY_RAW) ;
00251     }
00252 
00253     /* Get the detector illumination period */
00254     crires_util_combine_config.period =
00255         crires_get_detector_illum_period(cpl_frame_get_filename(
00256                     cpl_frameset_get_position(rawframes, 0))) ;
00257     if (crires_util_combine_config.period == CRIRES_ILLUM_UNKNOWN) {
00258         cpl_msg_error(__func__,
00259                 "Cannot determine the detector illumination period") ;
00260         cpl_frameset_delete(rawframes) ;
00261         if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00262         return -1 ;
00263     }
00264 
00265     /* Windowing mode ? */
00266     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(
00267                         cpl_frameset_get_position(rawframes, 0)), 0)) == NULL)
00268         return -1 ;
00269     sval = crires_pfits_get_ncorrs(plist) ;
00270     if (!strcmp(sval, "FowlerNsampGRstWin")) {
00271         crires_util_combine_config.period = CRIRES_ILLUM_FULL_DETECTOR ;
00272         crires_util_combine_config.win_mode = 1 ;
00273     } else {
00274         crires_util_combine_config.win_mode = 0 ;
00275     }
00276     cpl_propertylist_delete(plist) ;
00277     
00278     /* Display the Detector illumination */
00279     crires_display_detector_illum(crires_util_combine_config.period) ;
00280 
00281     /* Verify the STRIPE keys conformity */
00282     if (crires_util_combine_config.win_mode == 1) {
00283         fname = cpl_frame_get_filename(cpl_frameset_get_position(rawframes,0)) ;
00284         if (flat_win != NULL) {
00285             if (crire_stripe_keys_mismatch(fname, flat_win)) {
00286                 cpl_msg_error(__func__,
00287                         "Mismatch of STRIPE keys with the flat frame") ;
00288                 cpl_frameset_delete(rawframes) ;
00289                 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00290                 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00291                 return -1 ;
00292             }
00293         }
00294         if (bpm_win != NULL) {
00295             if (crire_stripe_keys_mismatch(fname, bpm_win)) {
00296                 cpl_msg_error(__func__,
00297                         "Mismatch of STRIPE keys with the bpm frame") ;
00298                 cpl_frameset_delete(rawframes) ;
00299                 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00300                 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00301                 return -1 ;
00302             }
00303         }
00304         if (dark_win != NULL) {
00305             if (crire_stripe_keys_mismatch(fname, dark_win)) {
00306                 cpl_msg_error(__func__,
00307                         "Mismatch of STRIPE keys with the dark frame") ;
00308                 cpl_frameset_delete(rawframes) ;
00309                 if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00310                 cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00311                 return -1 ;
00312             }
00313         }
00314     }
00315 
00316     /* Images recombination */
00317     cpl_msg_info(__func__, "Images combination") ;
00318     cpl_msg_indent_more() ;
00319     if (crires_util_combine_config.win_mode == 0) {
00320         comblist = crires_combine_imagelist(rawframes, skyframes,
00321                     crires_util_combine_config.period,
00322                     flat, dark, bpm, detlin,
00323                     crires_util_combine_config.nodding,
00324                     crires_util_combine_config.blind,
00325                     crires_util_combine_config.refine,
00326                     crires_util_combine_config.onlyA,
00327                     crires_util_combine_config.onlyB) ;
00328     } else {
00329         comblist = crires_combine_imagelist_win(rawframes,
00330                 flat_win, dark_win, bpm_win, detlin,
00331                 crires_util_combine_config.refine,
00332                 crires_util_combine_config.onlyA,
00333                 crires_util_combine_config.onlyB) ;
00334     }
00335     if (comblist == NULL) {
00336         cpl_msg_error(__func__, "Cannot combine the images") ;
00337         cpl_frameset_delete(rawframes) ;
00338         if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00339         cpl_msg_indent_less() ;
00340         return -1 ;
00341     }
00342     cpl_frameset_delete(rawframes) ;
00343     if (skyframes != NULL) cpl_frameset_delete(skyframes) ;
00344     cpl_msg_indent_less() ;
00345     
00346     /* Save the product */
00347     cpl_msg_info(__func__, "Save the product") ;
00348     cpl_msg_indent_more() ;
00349     if (crires_util_combine_save((const cpl_imagelist **)comblist, parlist, 
00350                 frameset)) {
00351         cpl_msg_error(__func__, "Cannot save the product") ;
00352         cpl_imagelist_delete(comblist[0]) ;
00353         cpl_imagelist_delete(comblist[1]) ;
00354         if (crires_util_combine_config.onlyA) {
00355             cpl_imagelist_delete(comblist[2]) ;
00356             cpl_imagelist_delete(comblist[3]) ;
00357         }
00358         if (crires_util_combine_config.onlyB) {
00359             cpl_imagelist_delete(comblist[4]) ;
00360             cpl_imagelist_delete(comblist[5]) ;
00361         }
00362         cpl_free(comblist) ;
00363         cpl_msg_indent_less() ;
00364         return -1 ;
00365     }
00366     cpl_imagelist_delete(comblist[0]) ;
00367     cpl_imagelist_delete(comblist[1]) ;
00368     if (crires_util_combine_config.onlyA) {
00369         cpl_imagelist_delete(comblist[2]) ;
00370         cpl_imagelist_delete(comblist[3]) ;
00371     }
00372     if (crires_util_combine_config.onlyB) {
00373         cpl_imagelist_delete(comblist[4]) ;
00374         cpl_imagelist_delete(comblist[5]) ;
00375     }
00376     cpl_free(comblist) ;
00377     cpl_msg_indent_less() ;
00378 
00379     /* Return */
00380     if (cpl_error_get_code()) return -1 ;
00381     else return 0 ;
00382 }
00383 
00384 /*----------------------------------------------------------------------------*/
00393 /*----------------------------------------------------------------------------*/
00394 static int crires_util_combine_save(
00395         const cpl_imagelist     **  images,
00396         const cpl_parameterlist *   parlist,
00397         cpl_frameset            *   set)
00398 {
00399     const char          *   recipe_name = "crires_util_combine" ;
00400 
00401     /* Write the combined image */
00402     crires_image_save(set,
00403             parlist,
00404             set, 
00405             images[0], 
00406             recipe_name,
00407             CRIRES_OBS_COMBINED_IMA, 
00408             CRIRES_PROTYPE_COMBINED,
00409             crires_util_combine_config.period,
00410             NULL,
00411             NULL,
00412             PACKAGE "/" PACKAGE_VERSION,
00413             "crires_util_combine_comb.fits") ;
00414 
00415     /* Write the contribution map */
00416     crires_image_save(set,
00417             parlist,
00418             set, 
00419             images[1], 
00420             recipe_name,
00421             CRIRES_OBS_CONTRIBUTION_IMA, 
00422             CRIRES_PROTYPE_CONTRIB,
00423             crires_util_combine_config.period,
00424             NULL,
00425             NULL,
00426             PACKAGE "/" PACKAGE_VERSION,
00427             "crires_util_combine_contrib.fits") ;
00428 
00429     /* Nodded A support */
00430     if (crires_util_combine_config.onlyA) {
00431         /* Write the combined Nodded A image */
00432         crires_image_save(set,
00433                 parlist,
00434                 set,
00435                 images[2],
00436                 recipe_name,
00437                 CRIRES_OBS_COMBINED_NA_IMA,
00438                 CRIRES_PROTYPE_COMBINED,
00439                 crires_util_combine_config.period,
00440                 NULL,
00441                 NULL,
00442                 PACKAGE "/" PACKAGE_VERSION,
00443                 "crires_util_combine_comb_noddedA.fits") ;
00444 
00445         /* Write the contribution Nodded A image */
00446         crires_image_save(set,
00447                 parlist,
00448                 set,
00449                 images[3],
00450                 recipe_name,
00451                 CRIRES_OBS_CONTRIBUTION_NA_IMA,
00452                 CRIRES_PROTYPE_CONTRIB,
00453                 crires_util_combine_config.period,
00454                 NULL,
00455                 NULL,
00456                 PACKAGE "/" PACKAGE_VERSION,
00457                 "crires_util_combine_contrib_noddedA.fits") ;
00458     }
00459 
00460     /* Nodded B support */
00461     if (crires_util_combine_config.onlyB) {
00462         /* Write the combined Nodded B image */
00463         crires_image_save(set,
00464                 parlist,
00465                 set,
00466                 images[4],
00467                 recipe_name,
00468                 CRIRES_OBS_COMBINED_NB_IMA,
00469                 CRIRES_PROTYPE_COMBINED,
00470                 crires_util_combine_config.period,
00471                 NULL,
00472                 NULL,
00473                 PACKAGE "/" PACKAGE_VERSION,
00474                 "crires_util_combine_comb_noddedB.fits") ;
00475 
00476         /* Write the contribution Nodded B image */
00477         crires_image_save(set,
00478                 parlist,
00479                 set,
00480                 images[5],
00481                 recipe_name,
00482                 CRIRES_OBS_CONTRIBUTION_NB_IMA,
00483                 CRIRES_PROTYPE_CONTRIB,
00484                 crires_util_combine_config.period,
00485                 NULL,
00486                 NULL,
00487                 PACKAGE "/" PACKAGE_VERSION,
00488                 "crires_util_combine_contrib_noddedB.fits") ;
00489     }
00490     return  0;
00491 }
00492