crires_util_extract.c

00001 /* $Id: crires_util_extract.c,v 1.69 2012/09/20 12:15:03 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/20 12:15:03 $
00024  * $Revision: 1.69 $
00025  * $Name: crire-2_3_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_extract.h"
00039 
00040 /*-----------------------------------------------------------------------------
00041                                 Define
00042  -----------------------------------------------------------------------------*/
00043 
00044 #define RECIPE_STRING "crires_util_extract"
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Functions prototypes
00048  -----------------------------------------------------------------------------*/
00049 
00050 static int crires_util_extract_save(const cpl_table **, const cpl_imagelist *, 
00051         const cpl_imagelist *, const cpl_parameterlist *, cpl_frameset *) ;
00052 
00053 static char crires_util_extract_description[] = 
00054 "This recipe accepts 1 parameter (optionally a 2nd one):\n"
00055 "First parameter:   the combined image.\n" 
00056 "                   (PRO TYPE = "CRIRES_PROTYPE_COMBINED")\n"
00057 "Second parameter (optional):   the contribution map.\n" 
00058 "                   (PRO TYPE = "CRIRES_PROTYPE_CONTRIB")\n"
00059 "\n"
00060 "This recipe produces 3 files:\n"
00061 "First product:     the image of the profile of the spectrum\n"
00062 "                   (PRO TYPE = "CRIRES_PROTYPE_PROFILE")\n"
00063 "Second product:    the table of the extracted spectrum in pixels\n"
00064 "                   (PRO TYPE = "CRIRES_PROTYPE_SPEC_PIX")\n"
00065 "Third product:     the maps of the background\n"
00066 "                   (PRO TYPE = "CRIRES_PROTYPE_BGD_MAP")\n" ;
00067 
00068 CRIRES_RECIPE_DEFINE(crires_util_extract,
00069         CRIRES_PARAM_DISPLAY        |
00070         CRIRES_PARAM_EXTR_MODE      |
00071         CRIRES_PARAM_HOR_SIZE       |
00072         CRIRES_PARAM_SPEC_HSIZE     |
00073         CRIRES_PARAM_KAPPA          |
00074         CRIRES_PARAM_CLOSING_HSIZE  |
00075         CRIRES_PARAM_CLEAN_RATE     |
00076         CRIRES_PARAM_TOT_NDIT       |
00077         CRIRES_PARAM_Y_POS_CHIP1    |
00078         CRIRES_PARAM_Y_POS_CHIP2    |
00079         CRIRES_PARAM_Y_POS_CHIP3    |
00080         CRIRES_PARAM_Y_POS_CHIP4    |
00081         CRIRES_PARAM_Y_WIDTH        |
00082         CRIRES_PARAM_SPEC_ZONE      |
00083         CRIRES_PARAM_REJECT,
00084         "Spectrum extraction routine",
00085         crires_util_extract_description) ;
00086 
00087 /*-----------------------------------------------------------------------------
00088                             Static variables
00089  -----------------------------------------------------------------------------*/
00090 
00091 static struct {
00092     /* Inputs */
00093     /* mode : 1=spec ; 2=lines */
00094     int                 mode ;
00095     double              kappa ;         /* mode = 1 */
00096     int                 closing_hs ;    /* mode = 1 */
00097     double              clean_rate ;    /* mode = 1 */
00098     double              tot_ndit ;      /* mode = 1 */
00099     int                 box_hor_size ;  /* mode = 1 */
00100     int                 spec_hsize ;    /* mode = 1 */
00101     const char      *   y_pos_c1 ;      /* mode = 2 */
00102     const char      *   y_pos_c2 ;      /* mode = 2 */
00103     const char      *   y_pos_c3 ;      /* mode = 2 */
00104     const char      *   y_pos_c4 ;      /* mode = 2 */
00105     int                 y_width ;       /* mode = 2 */
00106     int                 extr_spec_starty ;  /* mode = 1 */
00107     int                 extr_spec_stopy ;   /* mode = 1 */
00108     int                 rej_right ;     /* All modes */
00109     int                 rej_left ;      /* All modes */
00110     int                 display ;       /* All modes */
00111     /* Outputs */
00112     int                 win_mode ;
00113     crires_illum_period period ;
00114     int                 qc_specpos[CRIRES_NB_DETECTORS] ;
00115     int                 qc_specwrec[CRIRES_NB_DETECTORS] ;
00116     int                 qc_specwopt[CRIRES_NB_DETECTORS] ;
00117     double              qc_specoptmed[CRIRES_NB_DETECTORS] ;
00118     double              qc_s2nmed[CRIRES_NB_DETECTORS] ;
00119     double              qc_fwhm_comb_pix[CRIRES_NB_DETECTORS] ;
00120     double              qc_fwhm_comb_as[CRIRES_NB_DETECTORS] ;
00121     double              qc_fwhm_prof_pix[CRIRES_NB_DETECTORS] ;
00122     double              qc_fwhm_prof_as[CRIRES_NB_DETECTORS] ;
00123     double              qc_fwhm_diff[CRIRES_NB_DETECTORS] ;
00124 } crires_util_extract_config ;
00125 
00126 /*-----------------------------------------------------------------------------
00127                                 Functions code
00128  -----------------------------------------------------------------------------*/
00129 
00130 /*----------------------------------------------------------------------------*/
00137 /*----------------------------------------------------------------------------*/
00138 static int crires_util_extract(
00139         cpl_frameset            *   frameset,
00140         const cpl_parameterlist *   parlist) 
00141 {
00142     const char          *   sval ;
00143     const char          *   y_pos ;
00144     cpl_imagelist       *   imlist ;
00145     cpl_propertylist    *   plist ;
00146     cpl_imagelist       *   contrib ;
00147     cpl_image           *   contribution_map ;
00148     cpl_frame           *   fr ;
00149     cpl_imagelist       *   profile ;
00150     cpl_image           *   prof[CRIRES_NB_DETECTORS] ;
00151     cpl_imagelist       *   bgmaps ;
00152     cpl_image           *   bgmap[CRIRES_NB_DETECTORS] ;
00153     cpl_table           *   ext[CRIRES_NB_DETECTORS] ;
00154     cpl_vector          *   ext_vec ;
00155     cpl_vector          *   ypos_vec ;
00156     int                     ly, nexp, starty, stopy ;
00157     int                     i, j ;
00158     
00159     /* Initialise */
00160     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00161         crires_util_extract_config.qc_specpos[i] = -1 ;
00162         crires_util_extract_config.qc_specwrec[i] = -1 ;
00163         crires_util_extract_config.qc_specwopt[i] = -1 ;
00164         crires_util_extract_config.qc_specoptmed[i] = -1.0 ;
00165         crires_util_extract_config.qc_s2nmed[i] = -1.0 ;
00166         crires_util_extract_config.qc_fwhm_comb_pix[i] = -1.0 ;
00167         crires_util_extract_config.qc_fwhm_comb_as[i] = -1.0 ;
00168         crires_util_extract_config.qc_fwhm_prof_pix[i] = -1.0 ;
00169         crires_util_extract_config.qc_fwhm_prof_as[i] = -1.0 ;
00170         crires_util_extract_config.qc_fwhm_diff[i] = -1.0 ;
00171         ext[i] = NULL ;
00172         prof[i] = NULL ;
00173         bgmap[i] = NULL ;
00174     }
00175     ly = -1 ;
00176     
00177     /* Retrieve input parameters */
00178     crires_util_extract_config.display = crires_parameterlist_get_int(parlist,
00179             RECIPE_STRING, CRIRES_PARAM_DISPLAY) ;
00180     crires_util_extract_config.mode = crires_parameterlist_get_int(parlist,
00181             RECIPE_STRING, CRIRES_PARAM_EXTR_MODE) ;
00182     crires_util_extract_config.box_hor_size = crires_parameterlist_get_int(
00183             parlist, RECIPE_STRING, CRIRES_PARAM_HOR_SIZE) ;
00184     crires_util_extract_config.spec_hsize = crires_parameterlist_get_int(
00185             parlist, RECIPE_STRING, CRIRES_PARAM_SPEC_HSIZE) ;
00186     crires_util_extract_config.kappa = crires_parameterlist_get_double(
00187             parlist, RECIPE_STRING, CRIRES_PARAM_KAPPA) ;
00188     crires_util_extract_config.closing_hs = crires_parameterlist_get_int(
00189             parlist, RECIPE_STRING, CRIRES_PARAM_CLOSING_HSIZE) ;
00190     crires_util_extract_config.clean_rate = crires_parameterlist_get_double(
00191             parlist, RECIPE_STRING, CRIRES_PARAM_CLEAN_RATE) ;
00192     crires_util_extract_config.tot_ndit = crires_parameterlist_get_double(
00193             parlist, RECIPE_STRING, CRIRES_PARAM_TOT_NDIT) ;
00194     crires_util_extract_config.y_pos_c1=crires_parameterlist_get_string(parlist,
00195             RECIPE_STRING, CRIRES_PARAM_Y_POS_CHIP1) ;
00196     crires_util_extract_config.y_pos_c2=crires_parameterlist_get_string(parlist,
00197             RECIPE_STRING, CRIRES_PARAM_Y_POS_CHIP2) ;
00198     crires_util_extract_config.y_pos_c3=crires_parameterlist_get_string(parlist,
00199             RECIPE_STRING, CRIRES_PARAM_Y_POS_CHIP3) ;
00200     crires_util_extract_config.y_pos_c4=crires_parameterlist_get_string(parlist,
00201             RECIPE_STRING, CRIRES_PARAM_Y_POS_CHIP4) ;
00202     crires_util_extract_config.y_width= crires_parameterlist_get_int(parlist,
00203             RECIPE_STRING, CRIRES_PARAM_Y_WIDTH) ;
00204     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00205             CRIRES_PARAM_SPEC_ZONE) ;
00206     if (sscanf(sval, "%d,%d",
00207                     &crires_util_extract_config.extr_spec_starty,
00208                     &crires_util_extract_config.extr_spec_stopy)!=2) {
00209         return -1 ;
00210     }
00211     sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00212             CRIRES_PARAM_REJECT) ;
00213     if (sscanf(sval, "%d,%d",
00214                     &crires_util_extract_config.rej_left,
00215                     &crires_util_extract_config.rej_right)!=2) {
00216         return -1 ;
00217     }
00218  
00219     /* Identify the RAW and CALIB frames in the input frameset */
00220     if (crires_dfs_set_groups(frameset, NULL)) {
00221         cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00222         return -1 ;
00223     }
00224   
00225     /* Get the detector illumination period */
00226     fr = cpl_frameset_get_frame(frameset, 0);
00227     crires_util_extract_config.period =
00228         crires_get_detector_illum_period(cpl_frame_get_filename(fr)) ;
00229     if (crires_util_extract_config.period == CRIRES_ILLUM_UNKNOWN) {
00230         cpl_msg_error(__func__, 
00231                 "Cannot determine the detector illumination period") ;
00232         return -1 ;
00233     }
00234  
00235     /* Windowing mode ? */
00236     if ((plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0)) == NULL) 
00237         return -1 ;
00238     sval = crires_pfits_get_ncorrs(plist) ;
00239     if (!strcmp(sval, "FowlerNsampGRstWin")) {
00240         crires_util_extract_config.period = CRIRES_ILLUM_FULL_DETECTOR ;
00241         crires_util_extract_config.win_mode = 1 ;
00242     } else {
00243         crires_util_extract_config.win_mode = 0 ;
00244     }
00245     cpl_propertylist_delete(plist) ;
00246     
00247     /* Display the Detector illumination */
00248     crires_display_detector_illum(crires_util_extract_config.period) ;
00249 
00250     /* Load the first file in an image list */
00251     cpl_msg_info(__func__, "Input image loading") ;
00252     imlist = crires_load_file(cpl_frame_get_filename(fr), 
00253             crires_util_extract_config.period, CPL_TYPE_FLOAT) ;
00254   
00255     /* Load the optional contribution map */
00256     if (cpl_frameset_get_size(frameset) >= 2) {
00257         fr = cpl_frameset_get_frame(frameset, 1);
00258         cpl_msg_info(__func__, "Contribution map specified: %s",
00259                 cpl_frame_get_filename(fr)) ;
00260         contrib = crires_load_file(cpl_frame_get_filename(fr), 
00261                 crires_util_extract_config.period, CPL_TYPE_INT) ;
00262     } else contrib = NULL ;
00263    
00264     if (crires_util_extract_config.mode == 1) {
00265         /* Get the total ndit */
00266         fr = cpl_frameset_get_frame(frameset, 0);
00267         if (crires_util_extract_config.tot_ndit < 0) {
00268             crires_util_extract_config.tot_ndit = 
00269                 crires_get_totndit(cpl_frame_get_filename(fr)) ;
00270             if (crires_util_extract_config.tot_ndit < 0) {
00271                 cpl_msg_error(__func__, 
00272                         "Cannot get the total number of NDIT*DIT") ;
00273                 cpl_imagelist_delete(imlist) ;
00274                 if (contrib != NULL) cpl_imagelist_delete(contrib) ;
00275                 return -1 ;
00276             }
00277         }
00278         /* Get the number of exposures used for the frames combination */
00279         plist=cpl_propertylist_load(cpl_frame_get_filename(fr), 0) ;
00280         nexp = crires_pfits_get_datancom(plist) ;
00281         cpl_propertylist_delete(plist) ;
00282         if (cpl_error_get_code() != CPL_ERROR_NONE) {
00283             cpl_msg_error(__func__,"No PRO DATANCOM information in the header");
00284             cpl_imagelist_delete(imlist) ;
00285             if (contrib != NULL) cpl_imagelist_delete(contrib) ;
00286             return -1 ;
00287         }
00288         crires_util_extract_config.tot_ndit *= nexp ;
00289     }
00290     
00291     /* Extract chips */
00292     cpl_msg_info(__func__, "Spectrum extraction") ;
00293     cpl_msg_indent_more() ;
00294     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00295         /* Skip some detectors in windowing mode */
00296         if ((i==0 || i==CRIRES_NB_DETECTORS-1) && 
00297                 (crires_util_extract_config.win_mode == 1)) {
00298             continue ;
00299         }
00300         cpl_msg_info(__func__, "Spectrum extraction from chip number %d", i+1) ;
00301         cpl_msg_indent_more() ;
00302         if (crires_util_extract_config.mode == 1) {
00303             /* Extract spectrum */
00304             if (contrib != NULL) 
00305                 contribution_map = cpl_imagelist_get(contrib, i) ;
00306             else
00307                 contribution_map = NULL ;
00308             ext[i] = crires_extract_spectrum(cpl_imagelist_get(imlist, i),
00309                     contribution_map,
00310                     crires_util_extract_config.box_hor_size, 
00311                     crires_util_extract_config.spec_hsize, 
00312                     crires_util_extract_config.kappa,
00313                     crires_util_extract_config.closing_hs,
00314                     crires_util_extract_config.clean_rate,
00315                     crires_util_extract_config.rej_left,
00316                     crires_util_extract_config.rej_right,
00317                     crires_util_extract_config.extr_spec_starty,
00318                     crires_util_extract_config.extr_spec_stopy,
00319                     i+1,
00320                     crires_util_extract_config.tot_ndit,
00321                     crires_util_extract_config.period,
00322                     &(crires_util_extract_config.qc_specpos[i]), 
00323                     &(crires_util_extract_config.qc_specwrec[i]), 
00324                     &(crires_util_extract_config.qc_specwopt[i]), 
00325                     &(crires_util_extract_config.qc_specoptmed[i]),
00326                     &(crires_util_extract_config.qc_s2nmed[i]),
00327                     &(prof[i]),
00328                     &(bgmap[i])) ;
00329             cpl_msg_info(__func__, "Chip number %d FWHM Computation", i+1) ;
00330             if (crires_extract_qc_fwhm(cpl_imagelist_get(imlist, i),
00331                         prof[i],
00332                         &(crires_util_extract_config.qc_fwhm_comb_pix[i]),
00333                         &(crires_util_extract_config.qc_fwhm_comb_as[i]),
00334                         &(crires_util_extract_config.qc_fwhm_prof_pix[i]),
00335                         &(crires_util_extract_config.qc_fwhm_prof_as[i]),
00336                         &(crires_util_extract_config.qc_fwhm_diff[i])) == -1) {
00337                 cpl_msg_warning(__func__, "Failed for FWHM computation") ;
00338                 crires_util_extract_config.qc_fwhm_comb_pix[i] = -1.0 ;
00339                 crires_util_extract_config.qc_fwhm_comb_as[i] = -1.0 ;
00340                 crires_util_extract_config.qc_fwhm_prof_pix[i] = -1.0 ;
00341                 crires_util_extract_config.qc_fwhm_prof_as[i] = -1.0 ;
00342                 crires_util_extract_config.qc_fwhm_diff[i] = -1.0 ;
00343             }
00344         } else if (crires_util_extract_config.mode == 2) {
00345             /* Get the correction for the position */
00346             if (i+1 == 1) 
00347                 ly = crires_get_detector_ly1(crires_util_extract_config.period);
00348             if (i+1 == 2) 
00349                 ly = crires_get_detector_ly2(crires_util_extract_config.period);
00350             if (i+1 == 3) 
00351                 ly = crires_get_detector_ly3(crires_util_extract_config.period);
00352             if (i+1 == 4) 
00353                 ly = crires_get_detector_ly4(crires_util_extract_config.period);
00354             /* If ly not found, use the bottom of the image */
00355             if (ly < 0) ly = 1 ;
00356 
00357             /* Where is the Y extraction zone ? */
00358             y_pos = "" ;
00359             if (i+1 == 1) y_pos = crires_util_extract_config.y_pos_c1 ;
00360             if (i+1 == 2) y_pos = crires_util_extract_config.y_pos_c2 ;
00361             if (i+1 == 3) y_pos = crires_util_extract_config.y_pos_c3 ;
00362             if (i+1 == 4) y_pos = crires_util_extract_config.y_pos_c4 ;
00363 
00364             /* Try to parse the user specified positions */
00365             if (!strcmp(y_pos, "")) {
00366                 ypos_vec = NULL ;
00367             } else {
00368                 if ((ypos_vec = crires_parse_y_positions(y_pos)) == NULL) {
00369                     cpl_msg_warning(__func__,
00370                             "Cannot parse the y_positions value : %s", y_pos) ;
00371                     ypos_vec = NULL ;
00372                 }
00373             } 
00374             
00375             /* TODO : Only the first passed value is used */
00376             if (ypos_vec == NULL) {
00377                 starty = 1 ;
00378                 stopy = cpl_image_get_size_y(cpl_imagelist_get(imlist, i)) ;
00379             } else {
00380                 starty = (int)cpl_vector_get(ypos_vec, 0) - 
00381                     (int)(crires_util_extract_config.y_width/2.0) ;
00382                 stopy = (int)cpl_vector_get(ypos_vec, 0) +
00383                     (int)(crires_util_extract_config.y_width/2.0) ;
00384                 cpl_vector_delete(ypos_vec) ;
00385                 starty -= ly-1 ;
00386                 stopy -= ly-1 ;
00387             }
00388 
00389             /* Extract lines */
00390             ext_vec = crires_extract_lines(cpl_imagelist_get(imlist, i),
00391                     starty, stopy,
00392                     crires_util_extract_config.rej_left,
00393                     crires_util_extract_config.rej_right) ;
00394             if (ext_vec != NULL) {
00395                 ext[i] = crires_extract_gen_tab(ext_vec, NULL, NULL, NULL, 
00396                         NULL, NULL, NULL) ;     
00397                 crires_util_extract_config.qc_specpos[i] =
00398                     (starty+stopy)/2 + (ly-1) ;
00399                 crires_util_extract_config.qc_specwrec[i] = starty-stopy+1;
00400                 cpl_vector_delete(ext_vec) ;
00401             }
00402         } else {
00403             cpl_msg_warning(__func__, "Unsuported mode: %d", 
00404                     crires_util_extract_config.mode) ;
00405         }
00406 
00407         /* Plot the result if required */
00408         if ((ext[i] != NULL) && (crires_util_extract_config.display == i+1)) {
00409             ext_vec = cpl_vector_wrap(cpl_table_get_nrow(ext[i]),
00410             cpl_table_get_data_double(ext[i], CRIRES_COL_EXTRACT_INT_RECT));
00411             cpl_plot_vector(
00412 "set grid;set xlabel 'Position (pixels)';set ylabel 'Intensity RECT (ADU/sec)';",
00413             "t 'Extracted Spectrum RECT' w lines", "", ext_vec) ;
00414             cpl_vector_unwrap(ext_vec) ;
00415             ext_vec = cpl_vector_wrap(cpl_table_get_nrow(ext[i]),
00416             cpl_table_get_data_double(ext[i], CRIRES_COL_EXTRACT_INT_OPT)) ;
00417             cpl_plot_vector(
00418 "set grid;set xlabel 'Position (pixels)';set ylabel 'Intensity OPT (ADU/sec)';",
00419             "t 'Extracted Spectrum OPT' w lines", "", ext_vec) ;
00420             cpl_vector_unwrap(ext_vec) ;
00421         }
00422         cpl_msg_indent_less() ;
00423     }
00424     cpl_imagelist_delete(imlist) ;
00425     if (contrib != NULL) cpl_imagelist_delete(contrib) ;
00426     cpl_msg_indent_less() ;
00427 
00428     /* Reconstruct chips in windowing mode using detector 2 */
00429     if (crires_util_extract_config.win_mode == 1) {
00430         if (ext[1] != NULL) {
00431             ext[0] = cpl_table_duplicate(ext[1]) ;
00432             cpl_table_set_size(ext[0], 0) ;
00433             ext[CRIRES_NB_DETECTORS-1] = cpl_table_duplicate(ext[0]) ;
00434         }
00435         if (prof[1] != NULL) {
00436             prof[0] = cpl_image_duplicate(prof[1]) ;
00437             cpl_image_multiply_scalar(prof[0], 0.0) ;
00438             prof[CRIRES_NB_DETECTORS-1] = cpl_image_duplicate(prof[0]) ;
00439         }
00440         if (bgmap[1] != NULL) {
00441             bgmap[0] = cpl_image_duplicate(bgmap[1]) ;
00442             cpl_image_multiply_scalar(bgmap[0], 0.0) ;
00443             bgmap[CRIRES_NB_DETECTORS-1] = cpl_image_duplicate(bgmap[0]) ;
00444         }
00445     }
00446 
00447     /* Test that the spectrum is at the same place in all detectors */
00448     for (i=1 ; i<CRIRES_NB_DETECTORS ; i++) {
00449         /* Skip some detectors in windowing mode */
00450         if ((i==1 || i==CRIRES_NB_DETECTORS-1) && 
00451                 (crires_util_extract_config.win_mode == 1)) {
00452             continue ;
00453         }
00454         if (crires_util_extract_config.qc_specpos[i-1] > 0 && 
00455                 crires_util_extract_config.qc_specpos[i] > 0 &&
00456                 fabs(crires_util_extract_config.qc_specpos[i-1] - 
00457                     crires_util_extract_config.qc_specpos[i]) > 
00458                 CRIRES_SPEC_POS_TOLERANCE) {
00459             cpl_msg_warning(__func__, 
00460     "The spectrum positions in chip %d and chip %d are too different: %d -> %d",
00461                     i, i+1, crires_util_extract_config.qc_specpos[i-1], 
00462                     crires_util_extract_config.qc_specpos[i]) ;
00463         }
00464     }
00465  
00466     /* Create the profile */
00467     profile = cpl_imagelist_new() ;
00468     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00469         if (prof[i] != NULL) cpl_imagelist_set(profile, prof[i], i) ;
00470         else {
00471             cpl_imagelist_delete(profile) ;
00472             profile = NULL ;
00473             for (j=i+1 ; j<CRIRES_NB_DETECTORS ; j++) 
00474                 if (prof[j] != NULL) cpl_image_delete(prof[j]) ;
00475             break ;
00476         }
00477     }
00478    
00479     /* Create the bg maps */
00480     bgmaps = cpl_imagelist_new() ;
00481     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00482         if (bgmap[i] != NULL) cpl_imagelist_set(bgmaps, bgmap[i], i) ;
00483         else {
00484             cpl_imagelist_delete(bgmaps) ;
00485             bgmaps = NULL ;
00486             for (j=i+1 ; j<CRIRES_NB_DETECTORS ; j++) 
00487                 if (bgmap[j] != NULL) cpl_image_delete(bgmap[j]) ;
00488             break ;
00489         }
00490     }
00491         
00492     /* Save the result */
00493     cpl_msg_info(__func__, "Save the products") ;
00494     cpl_msg_indent_more() ;
00495     if (crires_util_extract_save((const cpl_table **)ext, profile, bgmaps, 
00496                 parlist, frameset) == -1) {
00497         cpl_msg_error(__func__, "Cannot save products") ;
00498         cpl_msg_indent_less() ;
00499         for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00500             if (ext[i] != NULL) cpl_table_delete(ext[i]) ;
00501         }
00502         if (profile != NULL) cpl_imagelist_delete(profile) ;
00503         if (bgmaps != NULL) cpl_imagelist_delete(bgmaps) ;
00504         return -1 ;
00505     }
00506     cpl_msg_indent_less() ;
00507    
00508     /* Deallocate */
00509     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00510         if (ext[i] != NULL) cpl_table_delete(ext[i]) ;
00511     }
00512     if (profile != NULL) cpl_imagelist_delete(profile) ;
00513     if (bgmaps != NULL) cpl_imagelist_delete(bgmaps) ;
00514 
00515     /* Return */
00516     if (cpl_error_get_code()) return -1 ;
00517     else return 0 ;
00518 }
00519 
00520 /*----------------------------------------------------------------------------*/
00530 /*----------------------------------------------------------------------------*/
00531 static int crires_util_extract_save(
00532         const cpl_table             **  ext,
00533         const cpl_imagelist         *   profile,
00534         const cpl_imagelist         *   bgmaps,
00535         const cpl_parameterlist     *   parlist,
00536         cpl_frameset                *   set)
00537 {
00538     cpl_propertylist    **  qclists ;
00539     const cpl_frame     *   ref_frame ;
00540     cpl_propertylist    *   inputlist ;
00541     const char          *   recipe_name = "crires_util_extract" ;
00542     int                     i ;
00543 
00544     /* Get the reference frame */
00545     ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00546 
00547     /* Create the QC lists */
00548     qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00549     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00550         qclists[i] = cpl_propertylist_new() ;
00551         cpl_propertylist_append_int(qclists[i], "ESO QC SPECPOS",
00552                 crires_util_extract_config.qc_specpos[i]) ;
00553         cpl_propertylist_append_int(qclists[i], "ESO QC SPECWREC",
00554                 crires_util_extract_config.qc_specwrec[i]) ;
00555         cpl_propertylist_append_int(qclists[i], "ESO QC SPECWOPT",
00556                 crires_util_extract_config.qc_specwopt[i]) ;
00557         cpl_propertylist_append_double(qclists[i], "ESO QC SIGNAL MED",
00558                 crires_util_extract_config.qc_specoptmed[i]) ;
00559         cpl_propertylist_append_double(qclists[i], "ESO QC S2NMED",
00560                 crires_util_extract_config.qc_s2nmed[i]) ;
00561         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMPIX COMBINED",
00562                 crires_util_extract_config.qc_fwhm_comb_pix[i]) ;
00563         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMARC COMBINED",
00564                 crires_util_extract_config.qc_fwhm_comb_as[i]) ;
00565         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMPIX PROFILE",
00566                 crires_util_extract_config.qc_fwhm_prof_pix[i]) ;
00567         cpl_propertylist_append_double(qclists[i], "ESO QC FWHMARC PROFILE",
00568                 crires_util_extract_config.qc_fwhm_prof_as[i]) ;
00569         cpl_propertylist_append_double(qclists[i], "ESO QC FWHM DIFF",
00570                 crires_util_extract_config.qc_fwhm_diff[i]) ;
00571 
00572         /* Propagate some keywords from input raw frame extensions */
00573         inputlist = cpl_propertylist_load_regexp(
00574                 cpl_frame_get_filename(ref_frame), i+1,
00575                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00576         cpl_propertylist_copy_property_regexp(qclists[i], inputlist, 
00577                 CRIRES_HEADER_EXT_FORWARD, 0) ;
00578         cpl_propertylist_delete(inputlist) ;
00579     }
00580 
00581     if (profile != NULL) {
00582         /* Write the image */
00583         crires_image_save(set,
00584                 parlist,
00585                 set,
00586                 profile,
00587                 recipe_name,
00588                 CRIRES_OBS_EXTRACT_PROFILE_IMA,
00589                 CRIRES_PROTYPE_PROFILE,
00590                 crires_util_extract_config.period,
00591                 NULL,
00592                 (const cpl_propertylist**)qclists,
00593                 PACKAGE "/" PACKAGE_VERSION,
00594                 "crires_util_extract_profile.fits") ;
00595     }
00596 
00597     if (bgmaps!= NULL) {
00598         /* Write the image */
00599         crires_image_save(set,
00600                 parlist,
00601                 set,
00602                 bgmaps,
00603                 recipe_name,
00604                 CRIRES_OBS_EXTRACT_BGMAP_IMA,
00605                 CRIRES_PROTYPE_BGD_MAP,
00606                 crires_util_extract_config.period,
00607                 NULL,
00608                 (const cpl_propertylist**)qclists,
00609                 PACKAGE "/" PACKAGE_VERSION,
00610                 "crires_util_extract_bgmap.fits") ;
00611     }
00612 
00613     /* Write the table */
00614     crires_table_save(set,
00615             parlist,
00616             set,
00617             ext,
00618             recipe_name,
00619             CRIRES_EXTRACT_PIX_TAB,
00620             CRIRES_PROTYPE_SPEC_PIX,
00621             NULL,
00622             (const cpl_propertylist**)qclists,
00623             PACKAGE "/" PACKAGE_VERSION,
00624             "crires_util_extract_extracted.fits") ;
00625 
00626     /* Free and return */
00627     for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00628         cpl_propertylist_delete(qclists[i]) ;
00629     }
00630     cpl_free(qclists) ;
00631     return  0;
00632 }
00633 

Generated on 25 Sep 2012 for CRIRES Pipeline Reference Manual by  doxygen 1.6.1