Main Page   File List  

visir_spc_wcal.c

00001 /* $Id: visir_spc_wcal.c,v 1.34 2006/01/22 10:31:05 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: 2006/01/22 10:31:05 $
00024  * $Revision: 1.34 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <assert.h>
00038 #include <float.h>
00039 
00040 #include <cpl.h>
00041 
00042 #include "irplib_utils.h"
00043 
00044 #include "visir_utils.h"
00045 #include "visir_pfits.h"
00046 #include "visir_dfs.h"
00047 #include "visir_inputs.h"
00048 #include "visir_spectro.h"
00049 #include "visir_spc_distortion.h"
00050 
00051 /*-----------------------------------------------------------------------------
00052                             Functions prototypes
00053  -----------------------------------------------------------------------------*/
00054 
00055 static int visir_spc_wcal_create(cpl_plugin *);
00056 static int visir_spc_wcal_exec(cpl_plugin *);
00057 static int visir_spc_wcal_destroy(cpl_plugin *);
00058 static int visir_spc_wcal(cpl_parameterlist *, cpl_frameset *);
00059 static int visir_spc_wcal_save(const cpl_table *, const cpl_parameterlist *, 
00060                                cpl_frameset *);
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Static variables
00064  -----------------------------------------------------------------------------*/
00065 
00066 static const char * recipename = "visir_spc_wcal";
00067 
00068 static struct {
00069     /* Inputs */
00070     int         auto_bpm;
00071     int         plot;
00072     double      phi;
00073     double      ksi;
00074     double      eps;
00075     double      delta;
00076 
00077     /* Outputs */
00078     double bg_mean;
00079     visir_output_wcal wcal;
00080 
00081 } visir_spc_wcal_config;
00082 
00083 static char visir_spc_wcal_description[] =
00084 "This recipe estimates the dispersion relation using the atmospheric spectrum\n"
00085 "in a long-slit spectroscopy half-cycle frame.\n"
00086 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00087 "VISIR-Long-Slit-Spectroscopy-file.fits " VISIR_SPC_WCAL_RAW "\n"
00088 "VISIR-Quantum-Efficiency-Calibration-file.fits " VISIR_CALIB_QEFF_SPC "\n"
00089 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits " VISIR_CALIB_LINES_SPC
00090 "\n";
00091 
00092 /*-----------------------------------------------------------------------------
00093                                 Functions code
00094  -----------------------------------------------------------------------------*/
00095 
00096 /*----------------------------------------------------------------------------*/
00105 /*----------------------------------------------------------------------------*/
00106 int cpl_plugin_get_info(cpl_pluginlist * list)
00107 {
00108     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00109     cpl_plugin  *   plugin = &recipe->interface;
00110 
00111 
00112     if (cpl_plugin_init(plugin,
00113                     CPL_PLUGIN_API,
00114                     VISIR_BINARY_VERSION,
00115                     CPL_PLUGIN_TYPE_RECIPE,
00116                     recipename,
00117                     "Spectroscopic wavelength calibration recipe",
00118                     visir_spc_wcal_description,
00119                     "Lars Lundin",
00120                     PACKAGE_BUGREPORT,
00121                     visir_get_license(),
00122                     visir_spc_wcal_create,
00123                     visir_spc_wcal_exec,
00124                     visir_spc_wcal_destroy)) return 1;
00125 
00126     if (cpl_pluginlist_append(list, plugin)) return 1;
00127     
00128     return 0;
00129 }
00130 
00131 /*----------------------------------------------------------------------------*/
00140 /*----------------------------------------------------------------------------*/
00141 static int visir_spc_wcal_create(cpl_plugin * plugin)
00142 {
00143 
00144     cpl_recipe * recipe = (cpl_recipe *)plugin;
00145 
00146     /* Verify plugin type */
00147     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00148 
00149     /* Create the parameters list in the cpl_recipe object */
00150     recipe->parameters = cpl_parameterlist_new();
00151 
00152     /* Fill the parameters list */
00153     return visir_parameter_set(recipe->parameters, recipename,
00154                                VISIR_PARAM_AUTOBPM |
00155                                VISIR_PARAM_PLOT   | VISIR_PARAM_SLITSKEW |
00156                                VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
00157                                VISIR_PARAM_HORIARC );
00158 
00159 }
00160 
00161 /*----------------------------------------------------------------------------*/
00167 /*----------------------------------------------------------------------------*/
00168 static int visir_spc_wcal_exec(cpl_plugin * plugin)
00169 {
00170     cpl_recipe * recipe = (cpl_recipe *)plugin;
00171 
00172     /* Verify plugin type */
00173     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00174 
00175     return visir_spc_wcal(recipe->parameters, recipe->frames);
00176 }
00177 
00178 /*----------------------------------------------------------------------------*/
00184 /*----------------------------------------------------------------------------*/
00185 static int visir_spc_wcal_destroy(cpl_plugin * plugin)
00186 {
00187     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00188 
00189     /* Verify plugin type */
00190     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00191     cpl_parameterlist_delete(recipe->parameters);
00192     return  0;
00193 }
00194 
00195 /*----------------------------------------------------------------------------*/
00202 /*----------------------------------------------------------------------------*/
00203 static int visir_spc_wcal(cpl_parameterlist  *   parlist,
00204                               cpl_frameset * framelist)
00205 {
00206     cpl_parameter   *   par;
00207     const char      *   badpix;
00208     const char      *   spc_cal_qeff;
00209     const char      *   spc_cal_lines;
00210     const char      *   flat;
00211     cpl_frameset    *   rawframes = NULL;
00212     cpl_imagelist   *   hcycle = NULL;
00213     cpl_image       *   imhcycle;
00214     cpl_polynomial  *   phdisp = NULL;
00215     cpl_polynomial  *   xcdisp = NULL;
00216     cpl_table       *   spc_table = NULL;
00217     double              wlen, slitw, temp, fwhm;
00218     visir_spc_resol     resol;
00219 
00220 
00221     if (cpl_error_get_code()) return cpl_error_get_code();
00222 
00223     visir_spc_wcal_config.bg_mean = -1.0;
00224     memset( &visir_spc_wcal_config.wcal, 0, sizeof(visir_output_wcal));
00225 
00226     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal.auto_bpm");
00227     visir_spc_wcal_config.auto_bpm = cpl_parameter_get_bool(par);
00228     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal.plot");
00229     visir_spc_wcal_config.plot = cpl_parameter_get_int(par);
00230 
00231     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal.phi");
00232     visir_spc_wcal_config.phi = cpl_parameter_get_double(par);
00233     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal.ksi");
00234     visir_spc_wcal_config.ksi = cpl_parameter_get_double(par);
00235     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal.eps");
00236     visir_spc_wcal_config.eps = cpl_parameter_get_double(par);
00237     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal.delta");
00238     visir_spc_wcal_config.delta = cpl_parameter_get_double(par);
00239 
00240 
00241     /* Identify the RAW and CALIB frames in the input frameset */
00242     skip_if (visir_dfs_set_groups(framelist));
00243 
00244     /* Objects observation */
00245     rawframes = irplib_frameset_extract(framelist, VISIR_SPC_WCAL_RAW);
00246     skip_if (rawframes == NULL);
00247 
00248     skip_if(visir_dfs_check_frameset_tag(rawframes));
00249     
00250     /* Quantum efficiency file */
00251     spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00252 
00253     /* Spectral lines calibration file */
00254     spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00255 
00256     /* Bad pixels calibration file */
00257     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00258 
00259     /* Flatfield calibration file */
00260     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00261 
00262 
00263     /* Get Resolution and Central Wavelength */
00264     resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm);
00265    
00266 
00267     skip_if (cpl_error_get_code());
00268 
00269     if (resol == VISIR_SPC_R_GHR) {
00270         cpl_msg_error(__func__, "This recipe cannot reduce HR Grism data");
00271         visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00272         skip_if(1);
00273     }
00274 
00275     /* Get the hcycle image from the reference file */
00276     hcycle = visir_load_hcycle(cpl_frameset_get_first(rawframes));
00277     skip_if (cpl_error_get_code());
00278 
00279     imhcycle = cpl_imagelist_get(hcycle, 0);
00280 
00281     /* Compute the background values of the HCYCLE frames */
00282     visir_spc_wcal_config.bg_mean = visir_hcycle_background(rawframes, 0, 0);
00283 
00284     skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
00285                                wlen, resol,
00286                                visir_spc_wcal_config.phi,
00287                                visir_spc_wcal_config.ksi,
00288                                visir_spc_wcal_config.eps,
00289                                visir_spc_wcal_config.delta, 
00290                                visir_spc_wcal_config.plot));
00291 
00292     skip_if (visir_spc_wavecal(imhcycle, wlen, slitw, temp, fwhm, resol,
00293                                0,
00294                                spc_cal_lines, spc_cal_qeff, &phdisp, &xcdisp,
00295                                &(visir_spc_wcal_config.wcal.xc),
00296                                &(visir_spc_wcal_config.wcal.subshift),
00297                                &spc_table, visir_spc_wcal_config.plot));
00298 
00299     /* Copy disp-pointers to output structure */
00300     visir_spc_wcal_config.wcal.phdisp = phdisp;
00301     visir_spc_wcal_config.wcal.xcdisp = xcdisp;
00302 
00303     /* Save the spectrum */
00304     cpl_msg_info(__func__, "Saving the produced spectrum");
00305     skip_if (visir_spc_wcal_save(spc_table, parlist, framelist));
00306 
00307     end_skip;
00308 
00309     cpl_frameset_delete(rawframes);
00310     cpl_polynomial_delete(phdisp);
00311     cpl_polynomial_delete(xcdisp);
00312     cpl_table_delete(spc_table);
00313     cpl_imagelist_delete(hcycle);
00314 
00315 
00316     return cpl_error_get_code();
00317 }
00318 
00319 /*----------------------------------------------------------------------------*/
00327 /*----------------------------------------------------------------------------*/
00328 static int visir_spc_wcal_save(
00329         const cpl_table         * table, 
00330         const cpl_parameterlist * parlist,
00331         cpl_frameset            * set)
00332 {
00333     /* The filename of the reference frame */
00334     const char       * ref_file =
00335         cpl_frame_get_filename(irplib_frameset_get_first_from_group(set,
00336                                                          CPL_FRAME_GROUP_RAW));
00337     cpl_propertylist * plist = cpl_propertylist_load(ref_file, 0);
00338     cpl_propertylist * qclist = cpl_propertylist_new();
00339     const char       * capa = visir_get_capa(plist);
00340     const char       * pafcopy = "^(DATE-OBS|ARCFILE|ESO INS GRAT1 NAME)$";
00341     FILE             * paf = NULL;
00342     /* Will still create .paf even if table does not exist */
00343     const int          nrows = table != NULL ? cpl_table_get_nrow(table) : 0;
00344 
00345 
00346     /* This will catch ref_file == NULL or plist == NULL */
00347     skip_if (cpl_error_get_code());
00348 
00349     /* FIXME */
00350     visir_spc_wcal_config.wcal.npix = nrows;
00351 
00352     skip_if (cpl_propertylist_copy_property_regexp(qclist, plist, pafcopy));
00353 
00354     /* THE PAF FILE FOR QC PARAMETERS */
00355     skip_if (!(paf = visir_paf_init(recipename)));
00356 
00357     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00358 
00359     /* Reuse the propertylist for QC parameters */
00360     cpl_propertylist_empty(qclist);
00361 
00362     cpl_propertylist_empty(plist); /* Just to reduce number of pointers */
00363 
00364     /* QC.BACKGD.MEAN */
00365     skip_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN",
00366                                             visir_spc_wcal_config.bg_mean));
00367 
00368     /* QC.XC-related variables */
00369     skip_if (visir_propertylist_append_wcal(qclist,
00370                                            &visir_spc_wcal_config.wcal));
00371 
00372     /* QC.CAPA */
00373     skip_if (capa != NULL && 
00374              cpl_propertylist_append_string(qclist, "ESO QC CAPA", capa));
00375 
00376     /* THE TABLE */
00377 
00378     skip_if (visir_table_save(parlist, set, table, recipename,
00379                               VISIR_SPC_WCAL_TAB_PROCATG,
00380                               qclist, "_spectrum_tab"));
00381 
00382     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00383 
00384     end_skip;
00385 
00386     visir_paf_end(paf);
00387 
00388     cpl_propertylist_delete(qclist);
00389     cpl_propertylist_delete(plist);
00390 
00391     return cpl_error_get_code();
00392 
00393 }

Generated on Mon Jan 23 12:16:37 2006 for VISIR Pipeline Reference Manual by doxygen1.2.18