visir_spc_wcal.c

00001 /* $Id: visir_spc_wcal.c,v 1.70 2010/03/09 12:52:18 llundin Exp $
00002  *
00003  * This file is part of the VISIR Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2010/03/09 12:52:18 $
00024  * $Revision: 1.70 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "visir_recipe.h"
00037 #include "visir_spectro.h"
00038 #include "visir_spc_distortion.h"
00039 
00040 
00041 /*-----------------------------------------------------------------------------
00042                                 Defines
00043  -----------------------------------------------------------------------------*/
00044 
00045 #define RECIPE_STRING   "visir_spc_wcal"
00046 
00047 /*-----------------------------------------------------------------------------
00048                             Private Functions prototypes
00049  -----------------------------------------------------------------------------*/
00050 static cpl_error_code visir_spc_wcal_save(cpl_frameset *,
00051                                           const cpl_parameterlist *,
00052                                           const cpl_propertylist *,
00053                                           const cpl_propertylist *,
00054                                           const cpl_table *);
00055 
00056 VISIR_RECIPE_DEFINE(visir_spc_wcal,
00057                     VISIR_PARAM_AUTOBPM |
00058                     VISIR_PARAM_PLOT   | VISIR_PARAM_SLITSKEW |
00059                     VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
00060                     VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
00061                     VISIR_PARAM_HORIARC,
00062                     "Spectroscopic wavelength calibration recipe",
00063                     "This recipe estimates the dispersion relation using the "
00064                     "atmospheric spectrum\n"
00065                     "in a long-slit spectroscopy half-cycle frame.\n"
00066                     "The files listed in the Set Of Frames (sof-file) "
00067                     "must be tagged:\n"
00068                     "VISIR-Long-Slit-Spectroscopy-file.fits "
00069                     VISIR_SPC_WCAL_RAW "\n"
00070                     "VISIR-Quantum-Efficiency-Calibration-file.fits "
00071                     VISIR_CALIB_QEFF_SPC "\n"
00072                     "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
00073                     VISIR_CALIB_LINES_SPC
00074                     "\n"
00075                     MAN_VISIR_CALIB_BPM_SPC);
00076 
00077 /*-----------------------------------------------------------------------------
00078                             Static variables
00079  -----------------------------------------------------------------------------*/
00080 
00081 static struct {
00082     /* Inputs */
00083     int         auto_bpm;
00084     int         plot;
00085     double      phi;
00086     double      ksi;
00087     double      eps;
00088     double      delta;
00089 
00090 } visir_spc_wcal_config;
00091 
00092 /*----------------------------------------------------------------------------*/
00096 /*----------------------------------------------------------------------------*/
00097 
00098 /*-----------------------------------------------------------------------------
00099                                 Functions code
00100  -----------------------------------------------------------------------------*/
00101 
00102 /*----------------------------------------------------------------------------*/
00109 /*----------------------------------------------------------------------------*/
00110 static int visir_spc_wcal(cpl_frameset            * framelist,
00111                           const cpl_parameterlist * parlist)
00112 {
00113     irplib_framelist * allframes = NULL;
00114     irplib_framelist * rawframes = NULL;
00115     cpl_propertylist * qclist    = cpl_propertylist_new();
00116     cpl_propertylist * paflist   = cpl_propertylist_new();
00117     const char      *   badpix;
00118     const char      *   spc_cal_qeff;
00119     const char      *   spc_cal_lines;
00120     const char      *   flat;
00121     cpl_imagelist   *   hcycle = NULL;
00122     cpl_image       *   imhcycle = NULL;
00123     cpl_image       *   imhcyclenarrow = NULL;
00124     cpl_table       *   spc_table = NULL;
00125     double              wlen, slitw, temp, fwhm;
00126     visir_spc_resol     resol;
00127     int                icol1, icol2;
00128     int                jcol1, jcol2;
00129 
00130 
00131     visir_spc_wcal_config.auto_bpm = 
00132         visir_parameterlist_get_bool(parlist, RECIPE_STRING, VISIR_PARAM_AUTOBPM);
00133     visir_spc_wcal_config.plot = visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_PLOT);
00134 
00135     visir_spc_wcal_config.phi = 
00136         visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SLITSKEW);
00137     visir_spc_wcal_config.ksi = 
00138         visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SPECSKEW);
00139     visir_spc_wcal_config.eps = 
00140         visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_VERTARC);
00141     visir_spc_wcal_config.delta = 
00142         visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_HORIARC);
00143 
00144     jcol1 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
00145                                         VISIR_PARAM_REJLEFT);
00146     jcol2 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
00147                                         VISIR_PARAM_REJRIGHT);
00148 
00149     skip_if (0);
00150 
00151     /* Identify the RAW and CALIB frames in the input frameset */
00152     skip_if (visir_dfs_set_groups(framelist));
00153 
00154     /* Objects observation */
00155     allframes = irplib_framelist_cast(framelist);
00156     skip_if(allframes == NULL);
00157     rawframes = irplib_framelist_extract(allframes, VISIR_SPC_WCAL_RAW);
00158     skip_if (rawframes == NULL);
00159 
00160     irplib_framelist_empty(allframes);
00161 
00162     skip_if(irplib_framelist_load_propertylist(rawframes, 0, 0, "^("
00163                                                VISIR_PFITS_REGEXP_LOAD_IMAGELIST
00164                                                "|"
00165                                                VISIR_PFITS_REGEXP_SPC_GET_RES_WL
00166                                                "|" VISIR_PFITS_REGEXP_CAPA "|"
00167                                                VISIR_PFITS_REGEXP_SPC_WCAL_PAF
00168                                                ")$", CPL_FALSE));
00169 
00170     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
00171                                                VISIR_PFITS_REGEXP_LOAD_IMAGELIST
00172                                                "|"
00173                                                VISIR_PFITS_REGEXP_SPC_GET_RES_WL
00174                                                ")$", CPL_FALSE));
00175 
00176     skip_if(visir_dfs_check_framelist_tag(rawframes));
00177     
00178     /* Quantum efficiency file */
00179     spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00180 
00181     /* Spectral lines calibration file */
00182     spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00183 
00184     /* Bad pixels calibration file */
00185     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00186 
00187     /* Flatfield calibration file */
00188     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00189 
00190     /* Get Resolution and Central Wavelength */
00191     resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm);
00192 
00193     skip_if (0);
00194 
00195     if (resol == VISIR_SPC_R_GHR) {
00196         cpl_msg_error(cpl_func, "This recipe cannot reduce HR Grism data");
00197         visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00198         skip_if(1);
00199     }
00200 
00201     skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
00202 
00203     /* Get the hcycle image from the reference file */
00204     hcycle = visir_load_hcycle(rawframes, 0);
00205     skip_if (0);
00206 
00207     imhcycle = cpl_imagelist_get(hcycle, 0);
00208 
00209     skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
00210                                wlen, resol,
00211                                visir_spc_wcal_config.phi,
00212                                visir_spc_wcal_config.ksi,
00213                                visir_spc_wcal_config.eps,
00214                                visir_spc_wcal_config.delta, 
00215                                visir_spc_wcal_config.plot));
00216 
00217     icol1 = 1;
00218     icol2 = cpl_image_get_size_x(imhcycle);
00219 
00220     if (jcol1 != 0) {
00221         cpl_msg_info(cpl_func, "Ignoring %d leftmost columns", jcol1);
00222         icol1 += jcol1;
00223     }
00224     if (jcol2 != 0) {
00225         cpl_msg_info(cpl_func, "Ignoring %d rightmost columns", jcol2);
00226         icol2 -= jcol2;
00227     }
00228 
00229     if (jcol1 != 0 || jcol2 != 0) {
00230         imhcyclenarrow = visir_spc_column_extract(imhcycle, icol1, icol2,
00231                                           visir_spc_wcal_config.plot);
00232         skip_if (0);
00233 
00234     } else {
00235         imhcyclenarrow = imhcycle;
00236     }
00237 
00238     skip_if (visir_spc_wavecal(imhcyclenarrow, qclist, wlen, slitw, temp, fwhm,
00239                                resol, 0,
00240                                spc_cal_lines, spc_cal_qeff,
00241                                &spc_table, visir_spc_wcal_config.plot));
00242 
00243     /* This column is not part of the product */
00244     skip_if (cpl_table_erase_column(spc_table, "SPC_EMISSIVITY"));
00245 
00246     bug_if (visir_spectro_qc(qclist, paflist, CPL_FALSE, rawframes, NULL,
00247                              "^(" VISIR_PFITS_REGEXP_SPC_WCAL_PAF ")$"));
00248     irplib_framelist_empty(rawframes);
00249    
00250     /* Save the spectrum */
00251     cpl_msg_info(cpl_func, "Saving the produced spectrum");
00252 
00253     /* PRO.CATG */
00254     bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00255                                            VISIR_SPC_WCAL_TAB_PROCATG));
00256 
00257     skip_if (visir_spc_wcal_save(framelist, parlist, qclist, paflist, spc_table));
00258 
00259     end_skip;
00260 
00261     cpl_propertylist_delete(qclist);
00262     cpl_propertylist_delete(paflist);
00263     irplib_framelist_delete(allframes);
00264     irplib_framelist_delete(rawframes);
00265     cpl_table_delete(spc_table);
00266     cpl_imagelist_delete(hcycle);
00267     if (imhcyclenarrow != imhcycle) cpl_image_delete(imhcyclenarrow);
00268 
00269 
00270     return cpl_error_get_code();
00271 }
00272  
00273 /*----------------------------------------------------------------------------*/
00283 /*----------------------------------------------------------------------------*/
00284 static cpl_error_code visir_spc_wcal_save(cpl_frameset            * set, 
00285                                           const cpl_parameterlist * parlist,
00286                                           const cpl_propertylist  * qclist,
00287                                           const cpl_propertylist  * paflist,
00288                                           const cpl_table         * table)
00289 {
00290     bug_if (0);
00291 
00292     /* THE TABLE */
00293     skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
00294                                VISIR_SPC_WCAL_TAB_PROCATG,
00295                                qclist, NULL, visir_pipe_id,
00296                                RECIPE_STRING "_spectrum_tab" CPL_DFS_FITS));
00297 
00298 #ifdef VISIR_SAVE_PAF
00299     /* THE PAF FILE FOR QC PARAMETERS */
00300     skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
00301                              RECIPE_STRING CPL_DFS_PAF));
00302 #else
00303     bug_if(paflist == NULL);
00304 #endif
00305 
00306     end_skip;
00307 
00308     return cpl_error_get_code();
00309 }

Generated on Thu Mar 24 11:59:39 2011 for VISIR Pipeline Reference Manual by  doxygen 1.5.8