crires_spec_astro.c

00001 /* $Id: crires_spec_astro.c,v 1.48 2012/01/16 08:58:47 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/01/16 08:58:47 $
00024  * $Revision: 1.48 $
00025  * $Name: crire-2_2_0 $
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_model_kernel.h"
00039 #include "crires_combine.h"
00040 #include "crires_extract.h"
00041 #include "crires_photom.h"
00042 #include "crires_wlcalib.h"
00043 
00044 /*-----------------------------------------------------------------------------
00045                                 Define
00046  -----------------------------------------------------------------------------*/
00047 
00048 #define RECIPE_STRING "crires_spec_astro"
00049 
00050 /*-----------------------------------------------------------------------------
00051                             Functions prototypes
00052  -----------------------------------------------------------------------------*/
00053 
00054 static int crires_spec_astro_compare(const cpl_frame *, const cpl_frame *) ;
00055 static cpl_imagelist ** crires_spec_astro_reduce(cpl_frameset *,
00056         const char *, const char *, const char *, const char *, const char *, 
00057         const char *, const char *, const char *, cpl_imagelist **, 
00058         cpl_imagelist **, cpl_imagelist **, cpl_imagelist **,
00059         cpl_table **, cpl_table **, cpl_table **, cpl_table **) ;
00060 static int crires_spec_astro_save(const cpl_imagelist **,
00061         const cpl_imagelist *, const cpl_imagelist *, const cpl_table **, 
00062         const cpl_imagelist *, const cpl_imagelist *, int, 
00063         const cpl_frameset *, const cpl_parameterlist *, cpl_frameset *) ;
00064 
00065 static char crires_spec_astro_description[] =
00066 "crires_spec_astro -- Astrometry recipe\n"
00067 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00068 "raw-file.fits "CRIRES_SPEC_ASTRO_RAW" or\n"
00069 "flat-file.fits "CRIRES_CALPRO_FLAT" or\n" 
00070 "bpm-file.fits "CRIRES_CALPRO_BPM" or\n" 
00071 "dark-file.fits "CRIRES_CALPRO_DARK" or\n" 
00072 "detlin-file.fits "CRIRES_CALPRO_COEFFS_CUBE" or\n" 
00073 "wavecal-file.fits "CRIRES_CALPRO_WAVE" or\n" 
00074 "catalog-file.fits "CRIRES_CALPRO_OH_CAT" or\n" 
00075 "catalog-file.fits "CRIRES_CALPRO_HITRAN_CAT" or\n" 
00076 "model-config-file.fits "CRIRES_CALPRO_MODEL_CONFIG".\n"
00077 "\n"
00078 "The input data set is a series of nodding observations using different\n"
00079 "   derotator position angles. The data are reduced separately for each\n"
00080 "   of those positions. In order not to degrade the instrument high\n"
00081 "   resolution, the combined images using only NODA or NODB nodding\n"
00082 "   positions can be produced on request. (see --onlyA/B)\n"
00083 "   In this case, the following spectrum extraction can be applied \n"
00084 "   either on the usual combined image or on those NODA/B combined\n"
00085 "   images (see --comb_used).\n"
00086 "\n"
00087 "This recipe produces 6 to 11 files per rotation angle:\n"
00088 "   The combined image      (PRO TYPE = "CRIRES_PROTYPE_COMBINED")\n"
00089 "   The contribution map    (PRO TYPE = "CRIRES_PROTYPE_CONTRIB")\n"
00090 "   The combined image using only Nodding A frames (optional)\n"
00091 "                           (PRO TYPE = "CRIRES_PROTYPE_COMBINED")\n"
00092 "   The contribution map using only Nodding A frames (optional)\n"
00093 "                           (PRO TYPE = "CRIRES_PROTYPE_CONTRIB")\n"
00094 "   The combined image using only Nodding B frames (optional)\n"
00095 "                           (PRO TYPE = "CRIRES_PROTYPE_COMBINED")\n"
00096 "   The contribution map using only Nodding B frames (optional)\n"
00097 "                           (PRO TYPE = "CRIRES_PROTYPE_CONTRIB")\n"
00098 "   The table with the extracted spectrum\n"
00099 "                           (PRO TYPE = "CRIRES_PROTYPE_SPEC_WL")\n"
00100 "   The profile image       (PRO TYPE = "CRIRES_PROTYPE_PROFILE")\n"
00101 "   The background map      (PRO TYPE = "CRIRES_PROTYPE_BGD_MAP")\n"
00102 "   The wavelength map      (PRO TYPE = "CRIRES_PROTYPE_WL_MAP")\n"
00103 "   The wavelength map from the model (optional)\n"
00104 "                           (PRO TYPE = "CRIRES_PROTYPE_WL_MAP")\n" ;
00105 
00106 CRIRES_RECIPE_DEFINE(crires_spec_astro,
00107         CRIRES_PARAM_WAVES          |
00108         CRIRES_PARAM_DISPLAY        |
00109         CRIRES_PARAM_REFINE         |
00110         CRIRES_PARAM_ONLYA          |
00111         CRIRES_PARAM_ONLYB          |
00112         CRIRES_PARAM_COMB_USED      |
00113         CRIRES_PARAM_BLIND          |
00114         CRIRES_PARAM_HOR_SIZE       |
00115         CRIRES_PARAM_SPEC_HSIZE     |
00116         CRIRES_PARAM_KAPPA          |
00117         CRIRES_PARAM_CLOSING_HSIZE  |
00118         CRIRES_PARAM_CLEAN_RATE     |
00119         CRIRES_PARAM_REJECT         |
00120         CRIRES_PARAM_SPEC_ZONE      |
00121         CRIRES_PARAM_WL_ERROR       |
00122         CRIRES_PARAM_XC_LIMIT       |
00123         CRIRES_PARAM_WL_LOG         |
00124         CRIRES_PARAM_WL_NOLIMIT     |
00125         CRIRES_PARAM_WL_NBSAMPLES   |
00126         CRIRES_PARAM_WL_STARTY      |
00127         CRIRES_PARAM_WL_STOPY       |
00128         CRIRES_PARAM_DEGREE         |
00129         CRIRES_PARAM_WL_CLEAN,
00130         "Astrometry recipe",
00131         crires_spec_astro_description) ;
00132 
00133 /*-----------------------------------------------------------------------------
00134                             Static variables
00135  -----------------------------------------------------------------------------*/
00136 
00137 static struct {
00138     /* Inputs */
00139     int                 comb_blind ;
00140     int                 comb_refine ;
00141     int                 comb_onlyA ;
00142     int                 comb_onlyB ;
00143     crires_comb_method  comb_used ;
00144     double              wstart[CRIRES_NB_DETECTORS] ;
00145     double              wstop[CRIRES_NB_DETECTORS] ;
00146     int                 wl_nolimit ;
00147     int                 wl_log ;
00148     int                 wl_starty ;
00149     int                 wl_stopy ;
00150     double              wl_fwhm ;
00151     double              wl_slitw ;
00152     int                 wl_degree ;
00153     double              wl_err ;
00154     int                 wl_samples ;
00155     int                 wl_clean ;
00156     double              wl_xclimit ;
00157     int                 wl_ppm ;
00158     int                 extr_box_hor_size ;
00159     int                 extr_spec_hsize ;
00160     double              extr_kappa ;
00161     int                 extr_closing_hs ;
00162     int                 extr_clean_rate ;
00163     int                 extr_rej_left ;
00164     int                 extr_rej_right ;
00165     int                 extr_spec_starty ;
00166     int                 extr_spec_stopy ;
00167     int                 display ;
00168     /* Outputs */
00169     crires_illum_period period ;
00170     int                 qc_specpos[CRIRES_NB_DETECTORS] ;
00171     int                 qc_specwrec[CRIRES_NB_DETECTORS] ;
00172     int                 qc_specwopt[CRIRES_NB_DETECTORS] ;
00173     double              qc_specoptmed[CRIRES_NB_DETECTORS] ;
00174     double              qc_s2nmed[CRIRES_NB_DETECTORS] ;
00175     double              qc_wlxc[CRIRES_NB_DETECTORS] ;
00176     double              qc_wlcent[CRIRES_NB_DETECTORS] ;
00177     double              qc_wldisp[CRIRES_NB_DETECTORS] ;
00178     double              qc_fwhm_comb_pix[CRIRES_NB_DETECTORS] ;
00179     double              qc_fwhm_comb_as[CRIRES_NB_DETECTORS] ;
00180     double              qc_fwhm_prof_pix[CRIRES_NB_DETECTORS] ;
00181     double              qc_fwhm_prof_as[CRIRES_NB_DETECTORS] ;
00182     double              qc_fwhm_diff[CRIRES_NB_DETECTORS] ;
00183 } crires_spec_astro_config ;
00184 
00185 /*-----------------------------------------------------------------------------
00186                                 Functions code
00187  -----------------------------------------------------------------------------*/
00188 
00189 /*----------------------------------------------------------------------------*/
00196 /*----------------------------------------------------------------------------*/
00197 static int crires_spec_astro(
00198         cpl_frameset            *   frameset,
00199         const cpl_parameterlist *   parlist)
00200 {
00201     cpl_frameset    *   raw_one ;
00202     const char      *   sval ;
00203     cpl_frameset    *   rawframes ;
00204     const char      *   flat ;
00205     const char      *   dark ;
00206     const char      *   bpm ;
00207     const char      *   detlin ;
00208     const char      *   wavecal ;
00209     const char      *   oh_cat ;
00210     const char      *   hitran_cat ;
00211     const char      *   cfg_model ;
00212     cpl_size        *   labels ;
00213     cpl_size            nlabels ;
00214     cpl_imagelist   **  comblist ;
00215     cpl_table       *   extr_tab[CRIRES_NB_DETECTORS] ;
00216     cpl_imagelist   *   prof_list ;
00217     cpl_imagelist   *   bgmap_list ;
00218     cpl_imagelist   *   wl_map ;
00219     cpl_imagelist   *   wl_map_model ;
00220     int                 i, j ;
00221 
00222     /* Initialise */
00223     rawframes = NULL ;
00224     crires_spec_astro_config.wl_ppm = 0 ;
00225     crires_spec_astro_config.wl_slitw = 2.0 ;
00226     crires_spec_astro_config.wl_fwhm = 2.0 ;
00227 
00228     /* Retrieve input parameters */
00229     crires_spec_astro_config.display = crires_parameterlist_get_int(parlist,
00230             RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00231     crires_spec_astro_config.comb_refine = crires_parameterlist_get_bool(
00232             parlist, RECIPE_STRING, CRIRES_PARAM_REFINE) ;
00233     crires_spec_astro_config.comb_onlyA = crires_parameterlist_get_bool(
00234             parlist, RECIPE_STRING, CRIRES_PARAM_ONLYA) ;
00235     crires_spec_astro_config.comb_onlyB = crires_parameterlist_get_bool(
00236             parlist, RECIPE_STRING, CRIRES_PARAM_ONLYB) ;
00237     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00238             CRIRES_PARAM_COMB_USED) ;
00239     if (!strcmp(sval, "NODA"))
00240         crires_spec_astro_config.comb_used = CRIRES_COMB_METHOD_NODA ;
00241     else if (!strcmp(sval, "NODB"))
00242         crires_spec_astro_config.comb_used = CRIRES_COMB_METHOD_NODB ;
00243     else if (!strcmp(sval, "COMB"))
00244         crires_spec_astro_config.comb_used = CRIRES_COMB_METHOD_COMB ;
00245     else {
00246         cpl_msg_error(__func__, "Invalid combination method specified");
00247         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00248         return -1;
00249     }
00250     crires_spec_astro_config.comb_blind = crires_parameterlist_get_bool(
00251             parlist, RECIPE_STRING, CRIRES_PARAM_BLIND) ;
00252     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00253             CRIRES_PARAM_WAVES) ;
00254     if (sscanf(sval, "%lg,%lg,%lg,%lg,%lg,%lg,%lg,%lg",
00255                     &crires_spec_astro_config.wstart[0],
00256                     &crires_spec_astro_config.wstop[0],
00257                     &crires_spec_astro_config.wstart[1],
00258                     &crires_spec_astro_config.wstop[1],
00259                     &crires_spec_astro_config.wstart[2],
00260                     &crires_spec_astro_config.wstop[2],
00261                     &crires_spec_astro_config.wstart[3],
00262                     &crires_spec_astro_config.wstop[3])!=2*CRIRES_NB_DETECTORS){
00263         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00264         return -1 ;
00265     }
00266     crires_spec_astro_config.wl_log = crires_parameterlist_get_bool(parlist, 
00267             RECIPE_STRING, CRIRES_PARAM_WL_LOG) ;
00268     crires_spec_astro_config.wl_nolimit = crires_parameterlist_get_bool(
00269             parlist, RECIPE_STRING, CRIRES_PARAM_WL_NOLIMIT) ; 
00270     crires_spec_astro_config.wl_degree = crires_parameterlist_get_int(parlist, 
00271             RECIPE_STRING, CRIRES_PARAM_DEGREE) ;
00272     crires_spec_astro_config.wl_err = crires_parameterlist_get_double(parlist, 
00273             RECIPE_STRING, CRIRES_PARAM_WL_ERROR) ;
00274     crires_spec_astro_config.wl_xclimit = crires_parameterlist_get_double(
00275             parlist, RECIPE_STRING, CRIRES_PARAM_XC_LIMIT) ;
00276     crires_spec_astro_config.wl_samples = crires_parameterlist_get_int(parlist,
00277             RECIPE_STRING, CRIRES_PARAM_WL_NBSAMPLES) ;
00278     crires_spec_astro_config.wl_clean = crires_parameterlist_get_bool(parlist, 
00279             RECIPE_STRING, CRIRES_PARAM_WL_CLEAN) ;
00280     crires_spec_astro_config.wl_starty = crires_parameterlist_get_int(parlist, 
00281             RECIPE_STRING, CRIRES_PARAM_WL_STARTY) ;
00282     crires_spec_astro_config.wl_stopy = crires_parameterlist_get_int(parlist, 
00283             RECIPE_STRING, CRIRES_PARAM_WL_STOPY) ;
00284     crires_spec_astro_config.extr_box_hor_size = crires_parameterlist_get_int(
00285             parlist, RECIPE_STRING, CRIRES_PARAM_HOR_SIZE) ;
00286     crires_spec_astro_config.extr_spec_hsize = crires_parameterlist_get_int(
00287             parlist, RECIPE_STRING, CRIRES_PARAM_SPEC_HSIZE) ;
00288     crires_spec_astro_config.extr_kappa = crires_parameterlist_get_double(
00289             parlist, RECIPE_STRING, CRIRES_PARAM_KAPPA) ;
00290     crires_spec_astro_config.extr_closing_hs = crires_parameterlist_get_int(
00291             parlist, RECIPE_STRING, CRIRES_PARAM_CLOSING_HSIZE) ;
00292     crires_spec_astro_config.extr_clean_rate = crires_parameterlist_get_double(
00293             parlist, RECIPE_STRING, CRIRES_PARAM_CLEAN_RATE) ;
00294     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING, 
00295             CRIRES_PARAM_REJECT) ;
00296     if (sscanf(sval, "%d,%d",
00297                     &crires_spec_astro_config.extr_rej_left,
00298                     &crires_spec_astro_config.extr_rej_right)!=2) {
00299         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00300         return -1 ;
00301     }
00302     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING, 
00303             CRIRES_PARAM_SPEC_ZONE) ;
00304     if (sscanf(sval, "%d,%d",
00305                     &crires_spec_astro_config.extr_spec_starty,
00306                     &crires_spec_astro_config.extr_spec_stopy)!=2) {
00307         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00308         return -1 ;
00309     }
00310  
00311     /* Identify the RAW and CALIB frames in the input frameset */
00312     if (crires_dfs_set_groups(frameset, "crires_spec_astro")) {
00313         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00314         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00315         return -1 ;
00316     }
00317 
00318     /* Retrieve calibration data */
00319     flat        = crires_extract_filename(frameset, CRIRES_CALPRO_FLAT) ;
00320     dark        = crires_extract_filename(frameset, CRIRES_CALPRO_DARK) ;
00321     bpm         = crires_extract_filename(frameset, CRIRES_CALPRO_BPM) ;
00322     detlin      = crires_extract_filename(frameset, CRIRES_CALPRO_COEFFS_CUBE) ;
00323     wavecal     = crires_extract_filename(frameset, CRIRES_CALPRO_WAVE) ;
00324     oh_cat      = crires_extract_filename(frameset, CRIRES_CALPRO_OH_CAT) ;
00325     hitran_cat  = crires_extract_filename(frameset, CRIRES_CALPRO_HITRAN_CAT) ;
00326     cfg_model   = crires_extract_filename(frameset, CRIRES_CALPRO_MODEL_CONFIG);
00327     
00328     /* Retrieve raw frames */
00329     if ((rawframes = crires_extract_frameset(frameset,
00330                     CRIRES_SPEC_ASTRO_RAW)) == NULL) {
00331         cpl_msg_error(__func__, "No raw frame in input") ;
00332         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00333         return -1 ;
00334     }
00335 
00336     /* Checks on the parameters validity */
00337     if ((crires_spec_astro_config.comb_used == CRIRES_COMB_METHOD_NODA)
00338         && (crires_spec_astro_config.comb_onlyA == 0)) {
00339         cpl_msg_warning(__func__,
00340                 "You forgot to require the NODA image to be produced !") ;
00341         crires_spec_astro_config.comb_onlyA = 1 ;
00342     }
00343     if ((crires_spec_astro_config.comb_used == CRIRES_COMB_METHOD_NODB)
00344         && (crires_spec_astro_config.comb_onlyB == 0)) {
00345         cpl_msg_warning(__func__,
00346                 "You forgot to require the NODB image to be produced !") ;
00347         crires_spec_astro_config.comb_onlyB = 1 ;
00348     }
00349 
00350     /* Get the detector illumination period */
00351     crires_spec_astro_config.period =
00352         crires_get_detector_illum_period(
00353                 cpl_frame_get_filename(cpl_frameset_get_frame(rawframes, 0))) ;
00354     if (crires_spec_astro_config.period == CRIRES_ILLUM_UNKNOWN) {
00355         cpl_msg_error(__func__,
00356                 "Cannot determine the detector illumination period") ;
00357         cpl_frameset_delete(rawframes) ;
00358         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00359         return -1 ;
00360     } else {
00361         crires_display_detector_illum(crires_spec_astro_config.period) ;
00362     }
00363 
00364     /* Labelise the raw frames with the DROT POSANG */
00365     if ((labels = cpl_frameset_labelise(rawframes, crires_spec_astro_compare,
00366                 &nlabels)) == NULL) {
00367         cpl_msg_error(__func__, "Cannot labelise input frames") ;
00368         cpl_frameset_delete(rawframes) ;
00369         cpl_error_set(__func__, CPL_ERROR_ILLEGAL_INPUT) ;
00370         return -1 ;
00371     }
00372 
00373     /* Loop on the settings */
00374     for (i=0 ; i<(int)nlabels ; i++) {
00375 
00376         /* Initialise */
00377         for (j=0 ; j<CRIRES_NB_DETECTORS ; j++) {
00378             crires_spec_astro_config.qc_specpos[j] = -1 ;
00379             crires_spec_astro_config.qc_specwrec[j] = -1 ;
00380             crires_spec_astro_config.qc_specwopt[j] = -1 ;
00381             crires_spec_astro_config.qc_specoptmed[j] = -1.0 ;
00382             crires_spec_astro_config.qc_s2nmed[j] = -1.0 ;
00383             crires_spec_astro_config.qc_wlxc[j] = -1.0 ;
00384             crires_spec_astro_config.qc_wlcent[j] = -1.0 ;
00385             crires_spec_astro_config.qc_wldisp[j] = -1.0 ;
00386             crires_spec_astro_config.qc_fwhm_comb_pix[j] = -1.0 ;
00387             crires_spec_astro_config.qc_fwhm_comb_as[j] = -1.0 ;
00388             crires_spec_astro_config.qc_fwhm_prof_pix[j] = -1.0 ;
00389             crires_spec_astro_config.qc_fwhm_prof_as[j] = -1.0 ;
00390             crires_spec_astro_config.qc_fwhm_diff[j] = -1.0 ;
00391         }
00392 
00393         /* Get the frames for the current setting */
00394         raw_one = cpl_frameset_extract(rawframes, labels, (cpl_size)i) ;
00395 
00396         /* Reduce data set nb i */
00397         cpl_msg_info(__func__, "Reduce data set %d / %"CPL_SIZE_FORMAT, 
00398                 i+1, nlabels);
00399         cpl_msg_indent_more() ;
00400 
00401         /* Apply the reduction */
00402         if ((comblist = crires_spec_astro_reduce(raw_one, flat, dark, bpm, 
00403                 detlin, wavecal, oh_cat, hitran_cat, cfg_model,
00404                 &wl_map,
00405                 &wl_map_model,
00406                 &prof_list,
00407                 &bgmap_list,
00408                 &(extr_tab[0]),
00409                 &(extr_tab[1]),
00410                 &(extr_tab[2]),
00411                 &(extr_tab[3]))) == NULL) {
00412             cpl_msg_error(__func__, "Cannot reduce setting") ;
00413             cpl_frameset_delete(rawframes) ;
00414             cpl_frameset_delete(raw_one) ;
00415             cpl_msg_indent_less() ;
00416             return -1 ;
00417         }
00418         
00419         /* Save the product */
00420         cpl_msg_info(__func__, "Save the products") ;
00421         cpl_msg_indent_more() ;
00422         if (crires_spec_astro_save((const cpl_imagelist **)comblist, 
00423                     prof_list, bgmap_list,
00424                     (const cpl_table **)extr_tab, wl_map, wl_map_model,
00425                     i+1, raw_one, parlist, frameset)) {
00426             cpl_msg_error(__func__, "Cannot save the product") ;
00427             cpl_imagelist_delete(prof_list) ;
00428             cpl_imagelist_delete(bgmap_list) ;
00429             for (j=0 ; j<CRIRES_NB_DETECTORS ; j++) 
00430                 if (extr_tab[j] != NULL) cpl_table_delete(extr_tab[j]) ;
00431             cpl_imagelist_delete(comblist[0]) ;
00432             cpl_imagelist_delete(comblist[1]) ;
00433             if (crires_spec_astro_config.comb_onlyA) {
00434                 cpl_imagelist_delete(comblist[2]) ;
00435                 cpl_imagelist_delete(comblist[3]) ;
00436             }
00437             if (crires_spec_astro_config.comb_onlyB) {
00438                 cpl_imagelist_delete(comblist[4]) ;
00439                 cpl_imagelist_delete(comblist[5]) ;
00440             }
00441             cpl_free(comblist) ;
00442             cpl_imagelist_delete(wl_map) ;
00443             if (wl_map_model) cpl_imagelist_delete(wl_map_model) ;
00444             cpl_msg_indent_less() ;
00445             return -1 ;
00446         }
00447         cpl_imagelist_delete(comblist[0]) ;
00448         cpl_imagelist_delete(comblist[1]) ;
00449         if (crires_spec_astro_config.comb_onlyA) {
00450             cpl_imagelist_delete(comblist[2]) ;
00451             cpl_imagelist_delete(comblist[3]) ;
00452         }
00453         if (crires_spec_astro_config.comb_onlyB) {
00454             cpl_imagelist_delete(comblist[4]) ;
00455             cpl_imagelist_delete(comblist[5]) ;
00456         }
00457         cpl_free(comblist) ;
00458         
00459         cpl_imagelist_delete(wl_map) ;
00460         if (wl_map_model) cpl_imagelist_delete(wl_map_model) ;
00461         cpl_imagelist_delete(prof_list) ;
00462         cpl_imagelist_delete(bgmap_list) ;
00463         for (j=0 ; j<CRIRES_NB_DETECTORS ; j++) 
00464             if (extr_tab[j] != NULL) cpl_table_delete(extr_tab[j]) ;
00465         cpl_msg_indent_less() ;
00466 
00467         cpl_frameset_delete(raw_one) ;
00468         cpl_msg_indent_less() ;
00469     }
00470 
00471     cpl_frameset_delete(rawframes) ;
00472     cpl_free(labels) ;
00473 
00474     /* Here comes the postprocessing on the 180 degrees difference spectra */
00475 
00476 
00477     
00478 
00479 
00480 
00481 
00482 
00483 
00484     /* Return */
00485     if (cpl_error_get_code()) return -1 ;
00486     else return 0 ;
00487 }
00488 
00489 /*----------------------------------------------------------------------------*/
00494 /*----------------------------------------------------------------------------*/
00495 static cpl_imagelist ** crires_spec_astro_reduce(
00496         cpl_frameset            *   rawframes,
00497         const char              *   flat,
00498         const char              *   dark,
00499         const char              *   bpm,
00500         const char              *   detlin,
00501         const char              *   wavecal,
00502         const char              *   oh_cat,
00503         const char              *   hitran_cat,
00504         const char              *   cfg_model,
00505         cpl_imagelist           **  wl_map,
00506         cpl_imagelist           **  wl_map_model,
00507         cpl_imagelist           **  prof_list,
00508         cpl_imagelist           **  bgmap_list,
00509         cpl_table               **  extr_tab1,
00510         cpl_table               **  extr_tab2,
00511         cpl_table               **  extr_tab3,
00512         cpl_table               **  extr_tab4)
00513 {
00514     cpl_frame           *   fr ;
00515     const char          *   fname ;
00516     cpl_propertylist    *   plist ;
00517     double                  wmin, wmax ;
00518     double                  tot_ndit ;
00519     cpl_imagelist       **  comblist ;
00520     int                     comblist_offset ;
00521     cpl_table           *   wave_tab ;
00522     cpl_polynomial      *   wave_poly[CRIRES_NB_DETECTORS] ;
00523     cpl_image           *   profiles[CRIRES_NB_DETECTORS] ;
00524     cpl_image           *   bg_maps[CRIRES_NB_DETECTORS] ;
00525     cpl_table           **  wl_tab ;
00526     cpl_imagelist       *   wl_map_loc ;
00527     cpl_imagelist       *   wl_map_model_loc ;
00528     cpl_imagelist       *   prof_list_loc ;
00529     cpl_imagelist       *   bgmap_list_loc ;
00530     cpl_table           *   extr_tab[CRIRES_NB_DETECTORS] ;
00531     cpl_size                power ;
00532     double                  wl_dx1, wl_dx2 ;
00533     cpl_vector          **  wavelengths ;
00534     int                     i, j ;
00535 
00536     /* Set comblist_offset */
00537     if (crires_spec_astro_config.comb_used == CRIRES_COMB_METHOD_COMB)
00538         comblist_offset = 0 ;
00539     else if (crires_spec_astro_config.comb_used == CRIRES_COMB_METHOD_NODA)
00540         comblist_offset = 1 ;
00541     else if (crires_spec_astro_config.comb_used == CRIRES_COMB_METHOD_NODB)
00542         comblist_offset = 2 ;
00543  
00544     /* Get the total number of NDIT */
00545     fr = cpl_frameset_get_frame(rawframes, 0);
00546     tot_ndit = crires_get_totndit(cpl_frame_get_filename(fr)) ;
00547     if (tot_ndit < 0) {
00548         cpl_msg_error(__func__, "Cannot get the total number of NDIT") ;
00549         return NULL ;
00550     }
00551     tot_ndit *= cpl_frameset_get_size(rawframes) ;
00552     
00553     /* Images recombination */
00554     cpl_msg_info(__func__, "Images combination") ;
00555     cpl_msg_indent_more() ;
00556     if ((comblist = crires_combine_imagelist(rawframes, NULL,
00557                     crires_spec_astro_config.period,
00558                     flat, dark, bpm, detlin, 1,
00559                     crires_spec_astro_config.comb_blind,
00560                     crires_spec_astro_config.comb_refine,
00561                     crires_spec_astro_config.comb_onlyA,
00562                     crires_spec_astro_config.comb_onlyB)) == NULL) {
00563         cpl_msg_error(__func__, "Cannot combine the images") ;
00564         cpl_msg_indent_less() ;
00565         return NULL ;
00566     }
00567     cpl_msg_indent_less() ;
00568     
00569     /* Wavelength calibration */
00570     cpl_msg_info(__func__, "Wavelength Calibration") ;
00571     cpl_msg_indent_more() ;
00572     if (wavecal != NULL) {
00573         /* Wavelength solution is provided */
00574         cpl_msg_info(__func__, "Use the provided solution") ;
00575         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00576             if ((wave_tab = crires_load_table_check(wavecal, i+1,
00577                             CRIRES_PROTYPE_WL_POLY, -1, -1, 0)) == NULL) {
00578                 cpl_msg_error(__func__, "Cannot load the wavelength table") ;
00579                 cpl_msg_indent_less() ;
00580                 cpl_imagelist_delete(comblist[0]) ;
00581                 cpl_imagelist_delete(comblist[1]) ;
00582                 if (crires_spec_astro_config.comb_onlyA) {
00583                     cpl_imagelist_delete(comblist[2]) ;
00584                     cpl_imagelist_delete(comblist[3]) ;
00585                 }
00586                 if (crires_spec_astro_config.comb_onlyB) {
00587                     cpl_imagelist_delete(comblist[4]) ;
00588                     cpl_imagelist_delete(comblist[5]) ;
00589                 }
00590                 cpl_free(comblist) ;
00591                 return NULL ;
00592             }
00593             /* Create the polynomial */
00594             wave_poly[i] = crires_wlcalib_gen_wlpoly(wave_tab) ;
00595             cpl_table_delete(wave_tab) ;
00596         }
00597     } else {
00598         /* Calibrate from the science */
00599         cpl_msg_info(__func__, "Use the science frame sky to calibrate") ;
00600 
00601         /* first raw frame */
00602         fname = cpl_frame_get_filename(cpl_frameset_get_frame(rawframes,0)) ;
00603 
00604         /* Get the Minimum and Maximum wavelengths */
00605         if (crires_spec_astro_config.wl_nolimit == 0) {
00606             plist = cpl_propertylist_load(fname, 0) ;
00607             wmin = crires_pfits_get_wlen_min(plist) ;
00608             wmax = crires_pfits_get_wlen_max(plist) ;
00609             cpl_propertylist_delete(plist) ;
00610             if (cpl_error_get_code()) {
00611                 wmin = wmax = -1.0 ;
00612                 cpl_error_reset() ;
00613             }
00614         } else {
00615             wmin = wmax = -1.0 ;
00616         }
00617 
00618         /* Loop on detectors */
00619         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00620             cpl_msg_info(__func__, "Calibrate chip number %d", i+1) ;
00621             cpl_msg_indent_more() ;
00622             wave_poly[i] = crires_wlcalib_sky(fname,
00623                     crires_spec_astro_config.period,
00624                     oh_cat, flat, dark, bpm, detlin,
00625                     crires_spec_astro_config.wstart[i],
00626                     crires_spec_astro_config.wstop[i],
00627                     wmin, wmax,
00628                     crires_spec_astro_config.wl_log,
00629                     hitran_cat, i+1,
00630                     crires_spec_astro_config.wl_starty,
00631                     crires_spec_astro_config.wl_stopy,
00632                     crires_spec_astro_config.wl_degree,
00633                     crires_spec_astro_config.wl_slitw,
00634                     crires_spec_astro_config.wl_fwhm,
00635                     crires_spec_astro_config.wl_err,
00636                     crires_spec_astro_config.wl_samples,
00637                     crires_spec_astro_config.wl_clean,
00638                     crires_spec_astro_config.wl_xclimit,
00639                     crires_spec_astro_config.wl_ppm,
00640                     (i+1==crires_spec_astro_config.display),
00641                     &(crires_spec_astro_config.qc_wlxc[i])) ;
00642             cpl_msg_indent_less() ;
00643         }
00644     }
00645     
00646     /* Check the global wavelength calibration */
00647     for (i=1 ; i<CRIRES_NB_DETECTORS ; i++) {
00648         if (wave_poly[i-1] != NULL && wave_poly[i] != NULL) {
00649             power = 1 ;
00650             wl_dx1 = cpl_polynomial_get_coeff(wave_poly[i-1], &power) ;
00651             wl_dx2 = cpl_polynomial_get_coeff(wave_poly[i], &power) ;
00652             if (wl_dx1<wl_dx2) {
00653                 cpl_msg_warning(__func__, 
00654     "The linear coefficient is increasing from chip %d to chip %d : %g -> %g",
00655                         i, i+1, wl_dx1, wl_dx2) ;
00656             }
00657         }
00658     }
00659     cpl_msg_indent_less() ;
00660    
00661     /* Compute the QC parameters */
00662     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00663         if (wave_poly[i] != NULL) {
00664             crires_spec_astro_config.qc_wlcent[i] =
00665                 cpl_polynomial_eval_1d(wave_poly[i], (double)512, NULL) ;
00666             crires_spec_astro_config.qc_wldisp[i] =
00667                 (cpl_polynomial_eval_1d(wave_poly[i], (double)1024, NULL) -
00668                  cpl_polynomial_eval_1d(wave_poly[i], (double)1, NULL)) / 1024 ;
00669         }
00670     }
00671 
00672     /* Create the wave map */
00673     wl_tab = crires_wlcalib_gen_wltab((const cpl_polynomial **)wave_poly) ;
00674     wl_map_loc = crires_wlcalib_gen_wlmap((const cpl_table **)wl_tab) ;
00675     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) cpl_table_delete(wl_tab[i]) ;
00676     cpl_free(wl_tab) ;
00677 
00678     /* Get the wl map from the model */
00679     fname  = cpl_frame_get_filename(cpl_frameset_get_frame(rawframes,0)) ;
00680     if ((cfg_model != NULL) && (!crires_model_off()) && 
00681             (crires_model_config_check(cfg_model, fname) == 0)) {
00682         cpl_msg_info(__func__, "Call the model to get the wavelength map") ;
00683         cpl_msg_indent_more() ;
00684         wl_map_model_loc = crires_model_wavpix( fname, cfg_model, -1) ;
00685         if (wl_map_model_loc == NULL) {
00686             cpl_msg_warning(__func__, "Model function returns NULL") ;
00687             cpl_error_reset() ;
00688         }
00689         cpl_msg_indent_less() ;
00690     } else {
00691         wl_map_model_loc = NULL ;
00692     }
00693 
00694     /* Spectrum extraction */
00695     cpl_msg_info(__func__, "Spectrum extraction") ;
00696     cpl_msg_indent_more() ;
00697     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00698         cpl_msg_info(__func__, "Chip number %d extraction", i+1) ;
00699         cpl_msg_indent_more() ;
00700         if ((extr_tab[i] = crires_extract_spectrum(
00701                         cpl_imagelist_get(comblist[0+2*comblist_offset], i),
00702                         cpl_imagelist_get(comblist[1+2*comblist_offset], i),
00703                         crires_spec_astro_config.extr_box_hor_size,
00704                         crires_spec_astro_config.extr_spec_hsize,
00705                         crires_spec_astro_config.extr_kappa,
00706                         crires_spec_astro_config.extr_closing_hs,
00707                         crires_spec_astro_config.extr_clean_rate,
00708                         crires_spec_astro_config.extr_rej_left,
00709                         crires_spec_astro_config.extr_rej_right,
00710                         crires_spec_astro_config.extr_spec_starty,
00711                         crires_spec_astro_config.extr_spec_stopy,
00712                         i+1,
00713                         tot_ndit,
00714                         crires_spec_astro_config.period,
00715                         &(crires_spec_astro_config.qc_specpos[i]),
00716                         &(crires_spec_astro_config.qc_specwrec[i]),
00717                         &(crires_spec_astro_config.qc_specwopt[i]),
00718                         &(crires_spec_astro_config.qc_specoptmed[i]),
00719                         &(crires_spec_astro_config.qc_s2nmed[i]),
00720                         &(profiles[i]),
00721                         &(bg_maps[i]))) == NULL) {
00722             cpl_msg_error(__func__, "Cannot extract the spectrum") ;
00723             cpl_msg_indent_less() ;
00724             cpl_msg_indent_less() ;
00725             for (j=0 ; j<CRIRES_NB_DETECTORS ; j++) 
00726                 if (wave_poly[j] != NULL) cpl_polynomial_delete(wave_poly[j]);
00727             for (j=0 ; j<i ; j++) 
00728                 cpl_table_delete(extr_tab[j]) ;
00729             for (j=0 ; j<i ; j++) 
00730                 cpl_image_delete(profiles[j]) ;
00731             for (j=0 ; j<i ; j++) 
00732                 cpl_image_delete(bg_maps[j]) ;
00733             cpl_imagelist_delete(comblist[0]) ;
00734             cpl_imagelist_delete(comblist[1]) ;
00735             if (crires_spec_astro_config.comb_onlyA) {
00736                 cpl_imagelist_delete(comblist[2]) ;
00737                 cpl_imagelist_delete(comblist[3]) ;
00738             }
00739             if (crires_spec_astro_config.comb_onlyB) {
00740                 cpl_imagelist_delete(comblist[4]) ;
00741                 cpl_imagelist_delete(comblist[5]) ;
00742             }
00743             cpl_free(comblist) ;
00744             cpl_imagelist_delete(wl_map_loc) ;
00745             if (wl_map_model_loc) cpl_imagelist_delete(wl_map_model_loc) ;
00746             return NULL ;
00747         }
00748         cpl_msg_info(__func__, "Chip number %d FWHM Computation", i+1) ;
00749         if (crires_extract_qc_fwhm(
00750                     cpl_imagelist_get(comblist[0+2*comblist_offset], i),
00751                     profiles[i],
00752                     &(crires_spec_astro_config.qc_fwhm_comb_pix[i]),
00753                     &(crires_spec_astro_config.qc_fwhm_comb_as[i]),
00754                     &(crires_spec_astro_config.qc_fwhm_prof_pix[i]),
00755                     &(crires_spec_astro_config.qc_fwhm_prof_as[i]),
00756                     &(crires_spec_astro_config.qc_fwhm_diff[i])) == -1) {
00757             cpl_msg_warning(__func__, "Failed for FWHM computation") ;
00758             crires_spec_astro_config.qc_fwhm_comb_pix[i] = -1.0 ;
00759             crires_spec_astro_config.qc_fwhm_comb_as[i] = -1.0 ;
00760             crires_spec_astro_config.qc_fwhm_prof_pix[i] = -1.0 ;
00761             crires_spec_astro_config.qc_fwhm_prof_as[i] = -1.0 ;
00762             crires_spec_astro_config.qc_fwhm_diff[i] = -1.0 ;
00763         }
00764         cpl_msg_indent_less() ;
00765     }
00766     
00767     /* Create the profile and bg maps */
00768     prof_list_loc = cpl_imagelist_new() ;
00769     bgmap_list_loc = cpl_imagelist_new() ;
00770     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00771         cpl_imagelist_set(prof_list_loc, profiles[i], i) ;
00772         cpl_imagelist_set(bgmap_list_loc, bg_maps[i], i) ;
00773     }
00774    
00775     /* Test that the spectrum is at the same place in all detectors */
00776     for (i=1 ; i<CRIRES_NB_DETECTORS ; i++) {
00777         if (crires_spec_astro_config.qc_specpos[i-1] > 0 && 
00778                 crires_spec_astro_config.qc_specpos[i] > 0 &&
00779                 fabs(crires_spec_astro_config.qc_specpos[i-1] -
00780                     crires_spec_astro_config.qc_specpos[i]) > 
00781                 CRIRES_SPEC_POS_TOLERANCE) {
00782             cpl_msg_warning(__func__,
00783     "The spectrum positions in chip %d and chip %d are too different: %d -> %d",
00784                     i, i+1, crires_spec_astro_config.qc_specpos[i-1], 
00785                     crires_spec_astro_config.qc_specpos[i]) ;
00786         }
00787     }
00788     cpl_msg_indent_less() ;
00789 
00790     /* Apply the wavelength */
00791     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00792         cpl_table_new_column(extr_tab[i], CRIRES_COL_WAVELENGTH, 
00793                 CPL_TYPE_DOUBLE) ;
00794         for (j=0 ; j<cpl_table_get_nrow(extr_tab[i]) ; j++) {
00795             if (wave_poly[i] != NULL) {
00796                 cpl_table_set_double(extr_tab[i], CRIRES_COL_WAVELENGTH, j, 
00797                     cpl_polynomial_eval_1d(wave_poly[i], (double)(j+1), NULL));
00798             } else {
00799                 cpl_table_set_double(extr_tab[i], CRIRES_COL_WAVELENGTH, j,
00800                         0.0);
00801             }
00802         }
00803     }
00804     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) 
00805         if (wave_poly[i] != NULL) cpl_polynomial_delete(wave_poly[i]);
00806 
00807     /* Add the Model Wavelength and Call the model to fill it */
00808     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00809         cpl_table_new_column(extr_tab[i], CRIRES_COL_WAVELENGTH_MODEL, 
00810                 CPL_TYPE_DOUBLE) ;
00811         cpl_table_fill_column_window_double(extr_tab[i],
00812                 CRIRES_COL_WAVELENGTH_MODEL, 0,
00813                 cpl_table_get_nrow(extr_tab[i]), -1.0) ;
00814     }
00815     if ((cfg_model != NULL) && (!crires_model_off()) && (1)) {
00816         cpl_msg_info(__func__, "Call the model to get the wavelengths") ;
00817         cpl_msg_indent_more() ;
00818         wavelengths = crires_model_wavelengths(
00819                 cpl_frame_get_filename(cpl_frameset_get_frame(rawframes,0)),
00820                 cfg_model, -1,
00821                 (double)(crires_spec_astro_config.qc_specpos[0]),
00822                 wl_map_model_loc) ;
00823         if (wavelengths != NULL) {
00824             /* Loop on the detectors */
00825             for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00826                 /* Loop on the x values */
00827                 for (j=0 ; j<cpl_vector_get_size(wavelengths[i]) ; j++) {
00828                     cpl_table_set_double(extr_tab[i], 
00829                             CRIRES_COL_WAVELENGTH_MODEL, j, 
00830                             cpl_vector_get(wavelengths[i], j)) ;
00831                 }
00832             }
00833             for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) 
00834                 cpl_vector_delete(wavelengths[i]) ;
00835             cpl_free(wavelengths) ;
00836         } else {
00837             cpl_msg_warning(__func__, "Model function returns NULL") ;
00838             cpl_error_reset() ;
00839         }
00840         cpl_msg_indent_less() ;
00841     }
00842 
00843     /* Return */
00844     if (wl_map != NULL)         *wl_map = wl_map_loc ;
00845     else                        cpl_imagelist_delete(wl_map_loc) ;
00846     if (wl_map_model != NULL)   *wl_map_model = wl_map_model_loc ;
00847     else                        cpl_imagelist_delete(wl_map_model_loc) ;
00848     if (prof_list != NULL)      *prof_list = prof_list_loc ;
00849     else                        cpl_imagelist_delete(prof_list_loc) ;
00850     if (bgmap_list != NULL)     *bgmap_list = bgmap_list_loc ;
00851     else                        cpl_imagelist_delete(bgmap_list_loc) ;
00852     if (extr_tab1 != NULL)      *extr_tab1 = extr_tab[0] ;
00853     else                        cpl_table_delete(extr_tab[0]) ;
00854     if (extr_tab2 != NULL)      *extr_tab2 = extr_tab[1] ;
00855     else                        cpl_table_delete(extr_tab[1]) ;
00856     if (extr_tab3 != NULL)      *extr_tab3 = extr_tab[2] ;
00857     else                        cpl_table_delete(extr_tab[2]) ;
00858     if (extr_tab4 != NULL)      *extr_tab4 = extr_tab[3] ;
00859     else                        cpl_table_delete(extr_tab[3]) ;
00860     return comblist ;
00861 }
00862 
00863 /*----------------------------------------------------------------------------*/
00879 /*----------------------------------------------------------------------------*/
00880 static int crires_spec_astro_save(
00881         const cpl_imagelist     **  images,
00882         const cpl_imagelist     *   prof,
00883         const cpl_imagelist     *   bgmap,
00884         const cpl_table         **  extr_tab,
00885         const cpl_imagelist     *   wl_map,
00886         const cpl_imagelist     *   wl_map_model,
00887         int                         setting,
00888         const cpl_frameset      *   cur_set,
00889         const cpl_parameterlist *   parlist,
00890         cpl_frameset            *   set)
00891 {
00892     cpl_propertylist    **  qclists ;
00893     const cpl_frame     *   ref_frame ;
00894     char                *   filename ;
00895     cpl_propertylist    *   inputlist ;
00896     const char          *   recipe_name = "crires_spec_astro" ;
00897     int                     i ;
00898 
00899     /* Get the reference frame */
00900     ref_frame = irplib_frameset_get_first_from_group(cur_set, 
00901             CPL_FRAME_GROUP_RAW) ;
00902 
00903     /* Create the QC lists */
00904     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00905     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00906         qclists[i] = cpl_propertylist_new() ;
00907         cpl_propertylist_append_int(qclists[i], "ESO QC SPECPOS",
00908                 crires_spec_astro_config.qc_specpos[i]) ;
00909         cpl_propertylist_append_int(qclists[i], "ESO QC SPECWREC",
00910                 crires_spec_astro_config.qc_specwrec[i]) ;
00911         cpl_propertylist_append_int(qclists[i], "ESO QC SPECWOPT",
00912                 crires_spec_astro_config.qc_specwopt[i]) ;
00913         cpl_propertylist_append_double(qclists[i], "ESO QC SIGNAL MED",
00914                 crires_spec_astro_config.qc_specoptmed[i]) ;
00915         cpl_propertylist_append_double(qclists[i], "ESO QC S2NMED",
00916                 crires_spec_astro_config.qc_s2nmed[i]) ;
00917         cpl_propertylist_append_double(qclists[i], "ESO QC XCORR",
00918                 crires_spec_astro_config.qc_wlxc[i]) ;
00919         cpl_propertylist_append_double(qclists[i], "ESO QC CENTWL",
00920                 crires_spec_astro_config.qc_wlcent[i]) ;
00921         cpl_propertylist_append_double(qclists[i], "ESO QC DISPWL",
00922                 crires_spec_astro_config.qc_wldisp[i]) ;
00923         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMPIX COMBINED",
00924                 crires_spec_astro_config.qc_fwhm_comb_pix[i]) ;
00925         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMARC COMBINED",
00926                 crires_spec_astro_config.qc_fwhm_comb_as[i]) ;
00927         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMPIX PROFILE",
00928                 crires_spec_astro_config.qc_fwhm_prof_pix[i]) ;
00929         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMARC PROFILE",
00930                 crires_spec_astro_config.qc_fwhm_prof_as[i]) ;
00931         cpl_propertylist_append_double(qclists[i], "ESO QC FWHM DIFF",
00932                 crires_spec_astro_config.qc_fwhm_diff[i]) ;
00933         /* Propagate some keywords from input raw frame extensions */
00934         inputlist = cpl_propertylist_load_regexp(
00935                 cpl_frame_get_filename(ref_frame), i+1,
00936                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00937         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00938                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00939         cpl_propertylist_delete(inputlist) ;
00940     }
00941 
00942     /* Write the combined image */
00943     filename = cpl_sprintf("%s_comb_set%02d.fits", recipe_name, setting) ;
00944     crires_image_save(set,
00945             parlist,
00946             cur_set, 
00947             images[0], 
00948             recipe_name,
00949             CRIRES_ASTRO_COMBINED_IMA, 
00950             CRIRES_PROTYPE_COMBINED,
00951             crires_spec_astro_config.period,
00952             NULL,
00953             (const cpl_propertylist **)qclists, 
00954             PACKAGE "/" PACKAGE_VERSION,
00955             filename) ;
00956     cpl_free(filename) ;
00957 
00958     /* Write the contribution map */
00959     filename = cpl_sprintf("%s_contrib_set%02d.fits", recipe_name, setting) ;
00960     crires_image_save(set,
00961             parlist,
00962             cur_set, 
00963             images[1], 
00964             recipe_name,
00965             CRIRES_ASTRO_CONTRIBUTION_IMA, 
00966             CRIRES_PROTYPE_CONTRIB,
00967             crires_spec_astro_config.period,
00968             NULL,
00969             (const cpl_propertylist **)qclists, 
00970             PACKAGE "/" PACKAGE_VERSION,
00971             filename) ;
00972     cpl_free(filename) ;
00973 
00974     /* Nodded A support */
00975     if (crires_spec_astro_config.comb_onlyA) {
00976         /* Write the combined image */
00977         filename = cpl_sprintf("%s_comb_noddedA_set%02d.fits", 
00978                 recipe_name, setting) ;
00979         crires_image_save(set,
00980                 parlist,
00981                 cur_set,
00982                 images[2],
00983                 recipe_name,
00984                 CRIRES_ASTRO_COMBINED_IMA,
00985                 CRIRES_PROTYPE_COMBINED,
00986                 crires_spec_astro_config.period,
00987                 NULL,
00988                 (const cpl_propertylist **)qclists,
00989                 PACKAGE "/" PACKAGE_VERSION,
00990                 filename) ;
00991         cpl_free(filename) ;
00992 
00993         /* Write the contribution map */
00994         filename = cpl_sprintf("%s_contrib_noddedA_set%02d.fits", 
00995                 recipe_name, setting) ;
00996         crires_image_save(set,
00997                 parlist,
00998                 cur_set,
00999                 images[3],
01000                 recipe_name,
01001                 CRIRES_ASTRO_CONTRIBUTION_IMA,
01002                 CRIRES_PROTYPE_CONTRIB,
01003                 crires_spec_astro_config.period,
01004                 NULL,
01005                 (const cpl_propertylist **)qclists,
01006                 PACKAGE "/" PACKAGE_VERSION,
01007                 filename) ;
01008         cpl_free(filename) ;
01009     }
01010 
01011     /* Nodded B support */
01012     if (crires_spec_astro_config.comb_onlyB) {
01013         /* Write the combined image */
01014         filename = cpl_sprintf("%s_comb_noddedB_set%02d.fits", 
01015                 recipe_name, setting) ;
01016         crires_image_save(set,
01017                 parlist,
01018                 cur_set,
01019                 images[4],
01020                 recipe_name,
01021                 CRIRES_ASTRO_COMBINED_IMA,
01022                 CRIRES_PROTYPE_COMBINED,
01023                 crires_spec_astro_config.period,
01024                 NULL,
01025                 (const cpl_propertylist **)qclists,
01026                 PACKAGE "/" PACKAGE_VERSION,
01027                 filename) ;
01028         cpl_free(filename) ;
01029 
01030         /* Write the contribution map */
01031         filename = cpl_sprintf("%s_contrib_noddedB_set%02d.fits", 
01032                 recipe_name, setting) ;
01033         crires_image_save(set,
01034                 parlist,
01035                 cur_set,
01036                 images[5],
01037                 recipe_name,
01038                 CRIRES_ASTRO_CONTRIBUTION_IMA,
01039                 CRIRES_PROTYPE_CONTRIB,
01040                 crires_spec_astro_config.period,
01041                 NULL,
01042                 (const cpl_propertylist **)qclists,
01043                 PACKAGE "/" PACKAGE_VERSION,
01044                 filename) ;
01045         cpl_free(filename) ;
01046     }
01047 
01048     /* Write the profile image */
01049     filename = cpl_sprintf("%s_prof_set%02d.fits", recipe_name, setting) ;
01050     crires_image_save(set,
01051             parlist,
01052             cur_set, 
01053             prof, 
01054             recipe_name,
01055             CRIRES_ASTRO_EXTRACT_PROFILE_IMA, 
01056             CRIRES_PROTYPE_PROFILE,
01057             crires_spec_astro_config.period,
01058             NULL,
01059             (const cpl_propertylist **)qclists, 
01060             PACKAGE "/" PACKAGE_VERSION,
01061             filename) ;
01062     cpl_free(filename) ;
01063  
01064     /* Write the background image */
01065     filename = cpl_sprintf("%s_bgmap_set%02d.fits", recipe_name, setting) ;
01066     crires_image_save(set,
01067             parlist,
01068             cur_set, 
01069             bgmap, 
01070             recipe_name,
01071             CRIRES_ASTRO_EXTRACT_BGMAP_IMA, 
01072             CRIRES_PROTYPE_BGD_MAP,
01073             crires_spec_astro_config.period,
01074             NULL,
01075             (const cpl_propertylist **)qclists, 
01076             PACKAGE "/" PACKAGE_VERSION,
01077             filename) ;
01078     cpl_free(filename) ;
01079 
01080     /* Write the map */
01081     filename = cpl_sprintf("%s_wlmap_set%02d.fits", recipe_name, setting) ;
01082     crires_image_save(set,
01083             parlist,
01084             cur_set,
01085             wl_map,
01086             recipe_name,
01087             CRIRES_ASTRO_WL_MAP_IMA,
01088             CRIRES_PROTYPE_WL_MAP,
01089             crires_spec_astro_config.period,
01090             NULL,
01091             (const cpl_propertylist **)qclists,
01092             PACKAGE "/" PACKAGE_VERSION,
01093             filename) ;
01094     cpl_free(filename) ;
01095 
01096     if (wl_map_model != NULL) {
01097         /* Write the model map */
01098         filename = cpl_sprintf("%s_wlmap_model_set%02d.fits", recipe_name, 
01099                 setting) ;
01100         crires_image_save(set,
01101                 parlist,
01102                 cur_set,
01103                 wl_map_model,
01104                 recipe_name,
01105                 CRIRES_ASTRO_WL_MAP_MODEL_IMA,
01106                 CRIRES_PROTYPE_WL_MAP,
01107                 crires_spec_astro_config.period,
01108                 NULL,
01109                 (const cpl_propertylist **)qclists,
01110                 PACKAGE "/" PACKAGE_VERSION,
01111                 filename) ;
01112         cpl_free(filename) ;
01113     }
01114     
01115     /* Write the extracted spectra */
01116     filename = cpl_sprintf("%s_extracted_set%02d.fits", recipe_name, setting);
01117     crires_table_save(set, 
01118             parlist, 
01119             cur_set, 
01120             extr_tab, 
01121             recipe_name, 
01122             CRIRES_ASTRO_EXTRACT_WL_TAB, 
01123             CRIRES_PROTYPE_SPEC_WL,
01124             NULL, 
01125             (const cpl_propertylist **)qclists,
01126             PACKAGE "/" PACKAGE_VERSION,
01127             filename) ;
01128     cpl_free(filename) ;
01129 
01130     /* Free and return */
01131     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
01132         cpl_propertylist_delete(qclists[i]) ;
01133     }
01134     cpl_free(qclists) ;
01135     return  0;
01136 }
01137 
01138 /*----------------------------------------------------------------------------*/
01145 /*----------------------------------------------------------------------------*/
01146 static int crires_spec_astro_compare(
01147         const cpl_frame   *   frame1,
01148         const cpl_frame   *   frame2)
01149 {
01150     int                     comparison ;
01151     cpl_propertylist    *   plist1 ;
01152     cpl_propertylist    *   plist2 ;
01153     double                  dval1, dval2 ;
01154     int                     ival1, ival2 ;
01155 
01156     /* Test entries */
01157     if (frame1==NULL || frame2==NULL) return -1 ;
01158 
01159     /* Get property lists */
01160     if ((plist1=cpl_propertylist_load(cpl_frame_get_filename(frame1),0))==NULL){
01161         cpl_msg_error(__func__, "getting header from reference frame");
01162         return -1 ;
01163     }
01164     if ((plist2=cpl_propertylist_load(cpl_frame_get_filename(frame2),0))==NULL){
01165         cpl_msg_error(__func__, "getting header from reference frame");
01166         cpl_propertylist_delete(plist1) ;
01167         return -1 ;
01168     }
01169 
01170     /* Test status */
01171     if (cpl_error_get_code()) {
01172         cpl_propertylist_delete(plist1) ;
01173         cpl_propertylist_delete(plist2) ;
01174         return -1 ;
01175     }
01176 
01177     comparison = 1 ;
01178 
01179     /* Compare the DROT POSANG used */
01180     dval1 = crires_pfits_get_dit(plist1) ;
01181     dval2 = crires_pfits_get_dit(plist2) ;
01182     if (cpl_error_get_code()) {
01183         cpl_msg_error(__func__, "Cannot get the POSANG");
01184         cpl_propertylist_delete(plist1) ;
01185         cpl_propertylist_delete(plist2) ;
01186         return -1 ;
01187     }
01188     if (fabs(dval1-dval2) > 1e-3) comparison = 0 ;
01189 
01190     /* Check if the frames are consecutive */
01191     ival1 = crires_pfits_get_expno(plist1) ;
01192     ival2 = crires_pfits_get_expno(plist2) ;
01193     if (cpl_error_get_code()) {
01194         cpl_msg_error(__func__, "Cannot get the EXPNO");
01195         cpl_propertylist_delete(plist1) ;
01196         cpl_propertylist_delete(plist2) ;
01197         return -1 ;
01198     }
01199     if (fabs(ival2-ival1) != 1.0) comparison = 0 ;
01200 
01201     cpl_propertylist_delete(plist1) ;
01202     cpl_propertylist_delete(plist2) ;
01203     return comparison ;
01204 }

Generated on 30 Jan 2012 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1