Main Page   File List  

visir_spc_wcal_ech.c

00001 /* $Id: visir_spc_wcal_ech.c,v 1.31 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.31 $
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_ech_create(cpl_plugin *);
00056 static int visir_spc_wcal_ech_exec(cpl_plugin *);
00057 static int visir_spc_wcal_ech_destroy(cpl_plugin *);
00058 static int visir_spc_wcal_ech(cpl_parameterlist *, cpl_frameset *);
00059 static int visir_spc_wcal_ech_save(const cpl_table *,
00060                                    const cpl_parameterlist *, cpl_frameset *);
00061 
00062 /*-----------------------------------------------------------------------------
00063                             Static variables
00064  -----------------------------------------------------------------------------*/
00065 
00066 static const char * recipename = "visir_spc_wcal_ech";
00067 
00068 static struct {
00069     /* Inputs */
00070     int         auto_bpm;
00071     int         plot;
00072     int         orderoffset;
00073     double      phi;
00074     double      ksi;
00075     double      eps;
00076     double      delta;
00077 
00078     /* Outputs */
00079     double bg_mean;
00080     visir_output_wcal wcal;
00081 
00082 } visir_spc_wcal_ech_config;
00083 
00084 static char visir_spc_wcal_ech_description[] =
00085 "This recipe estimates the dispersion relation using the atmospheric spectrum\n"
00086 "in a grism spectroscopy half-cycle frame.\n"
00087 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00088 "VISIR-Echelle-Spectroscopy-file.fits " VISIR_SPC_WCAL_ECH_RAW "\n"
00089 "VISIR-Quantum-Efficiency-Calibration-file.fits " VISIR_CALIB_QEFF_SPC "\n"
00090 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits " VISIR_CALIB_LINES_SPC
00091 "\n";
00092 
00093 
00094 /*-----------------------------------------------------------------------------
00095                                 Functions code
00096  -----------------------------------------------------------------------------*/
00097 
00098 /*----------------------------------------------------------------------------*/
00107 /*----------------------------------------------------------------------------*/
00108 int cpl_plugin_get_info(cpl_pluginlist * list)
00109 {
00110     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00111     cpl_plugin  *   plugin = &recipe->interface;
00112 
00113 
00114     if (cpl_plugin_init(plugin,
00115                     CPL_PLUGIN_API,
00116                     VISIR_BINARY_VERSION,
00117                     CPL_PLUGIN_TYPE_RECIPE,
00118                     recipename,
00119                     "Spectroscopic wavelength calibration recipe in Echelle",
00120                     visir_spc_wcal_ech_description,
00121                     "Lars Lundin",
00122                     PACKAGE_BUGREPORT,
00123                     visir_get_license(),
00124                     visir_spc_wcal_ech_create,
00125                     visir_spc_wcal_ech_exec,
00126                     visir_spc_wcal_ech_destroy)) return 1;
00127 
00128     if (cpl_pluginlist_append(list, plugin)) return 1;
00129     
00130     return 0;
00131 }
00132 
00133 /*----------------------------------------------------------------------------*/
00142 /*----------------------------------------------------------------------------*/
00143 static int visir_spc_wcal_ech_create(cpl_plugin * plugin)
00144 {
00145     cpl_recipe * recipe = (cpl_recipe *)plugin;
00146 
00147     /* Verify plugin type */
00148     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00149 
00150     /* Create the parameters list in the cpl_recipe object */
00151     recipe->parameters = cpl_parameterlist_new();
00152 
00153     /* Fill the parameters list */
00154     visir_parameter_set_double(recipe->parameters, recipename,
00155                                VISIR_PARAM_SLITSKEW | VISIR_PARAM_SPECSKEW |
00156                                VISIR_PARAM_VERTARC  | VISIR_PARAM_HORIARC, 0);
00157 
00158     return visir_parameter_set(recipe->parameters, recipename,
00159                                VISIR_PARAM_AUTOBPM |  VISIR_PARAM_PLOT |
00160                                VISIR_PARAM_ORDEROFF);
00161 }
00162 
00163 /*----------------------------------------------------------------------------*/
00169 /*----------------------------------------------------------------------------*/
00170 static int visir_spc_wcal_ech_exec(cpl_plugin * plugin)
00171 {
00172     cpl_recipe * recipe = (cpl_recipe *)plugin;
00173 
00174     /* Verify plugin type */
00175     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00176 
00177     return visir_spc_wcal_ech(recipe->parameters, recipe->frames);
00178 }
00179 
00180 /*----------------------------------------------------------------------------*/
00186 /*----------------------------------------------------------------------------*/
00187 static int visir_spc_wcal_ech_destroy(cpl_plugin * plugin)
00188 {
00189     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00190 
00191     /* Verify plugin type */
00192     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00193     cpl_parameterlist_delete(recipe->parameters);
00194     return  0;
00195 }
00196 
00197 /*----------------------------------------------------------------------------*/
00204 /*----------------------------------------------------------------------------*/
00205 static int visir_spc_wcal_ech(cpl_parameterlist  *   parlist,
00206                               cpl_frameset * framelist)
00207 {
00208     cpl_parameter   *   par;
00209     const char      *   badpix;
00210     const char      *   spc_cal_qeff;
00211     const char      *   spc_cal_lines;
00212     const char      *   flat;
00213     cpl_frameset    *   rawframes = NULL;
00214     cpl_imagelist   *   hcycle = NULL;
00215     cpl_image       *   imhcycle;
00216     cpl_polynomial  *   phdisp = NULL;
00217     cpl_polynomial  *   xcdisp = NULL;
00218     cpl_table       *   spc_table = NULL;
00219     cpl_image       *   order = NULL;
00220     double              wlen, slitw, temp, fwhm;
00221     int                 icol1, icol2;
00222     visir_spc_resol     resol;
00223 
00224 
00225     if (cpl_error_get_code()) return cpl_error_get_code();
00226 
00227     visir_spc_wcal_ech_config.bg_mean = -1.0;
00228     memset( &visir_spc_wcal_ech_config.wcal, 0, sizeof(visir_output_wcal));
00229     
00230     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.auto_bpm");
00231     visir_spc_wcal_ech_config.auto_bpm = cpl_parameter_get_bool(par);
00232     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.plot");
00233     visir_spc_wcal_ech_config.plot = cpl_parameter_get_int(par);
00234 
00235     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.phi");
00236     visir_spc_wcal_ech_config.phi = cpl_parameter_get_double(par);
00237     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.ksi");
00238     visir_spc_wcal_ech_config.ksi = cpl_parameter_get_double(par);
00239     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.eps");
00240     visir_spc_wcal_ech_config.eps = cpl_parameter_get_double(par);
00241     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.delta");
00242     visir_spc_wcal_ech_config.delta = cpl_parameter_get_double(par);
00243 
00244     par = cpl_parameterlist_find(parlist, "visir.visir_spc_wcal_ech.orderoffset");
00245     visir_spc_wcal_ech_config.orderoffset = cpl_parameter_get_int(par);
00246 
00247 
00248     /* Identify the RAW and CALIB frames in the input frameset */
00249     skip_if (visir_dfs_set_groups(framelist));
00250 
00251     /* Objects observation */
00252     rawframes = irplib_frameset_extract(framelist, VISIR_SPC_WCAL_ECH_RAW);
00253     skip_if (rawframes == NULL);
00254 
00255     skip_if(visir_dfs_check_frameset_tag(rawframes));
00256     
00257     /* Quantum efficiency file */
00258     spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00259 
00260     /* Spectral lines calibration file */
00261     spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00262 
00263     /* Bad pixels calibration file */
00264     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00265 
00266     /* Flatfield calibration file */
00267     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00268 
00269 
00270     /* Get Resolution and Central Wavelength */
00271     resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm);
00272 
00273    
00274     skip_if (cpl_error_get_code());
00275 
00276     if (resol != VISIR_SPC_R_GHR) {
00277         if (visir_spc_wcal_ech_config.orderoffset == 0) {
00278             cpl_msg_warning(__func__,"Reducing non-HR Grism data as main order");
00279         } else {
00280             cpl_msg_error(__func__, "This recipe cannot reduce non-HR Grism "
00281                           "data with an order-offset of %d",
00282                           visir_spc_wcal_ech_config.orderoffset);
00283             visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00284             skip_if(1);
00285         }
00286     }
00287    
00288     /* Get the hcycle image from the reference file */
00289     hcycle = visir_load_hcycle(cpl_frameset_get_first(rawframes));
00290     skip_if (cpl_error_get_code());
00291 
00292     imhcycle = cpl_imagelist_get(hcycle, 0);
00293 
00294     skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
00295                                wlen, resol,
00296                                visir_spc_wcal_ech_config.phi,
00297                                visir_spc_wcal_ech_config.ksi,
00298                                visir_spc_wcal_ech_config.eps,
00299                                visir_spc_wcal_ech_config.delta, 
00300                                visir_spc_wcal_ech_config.plot));
00301 
00302     skip_if (visir_spc_echelle_limit(&icol1, &icol2, wlen,
00303                                      visir_spc_wcal_ech_config.orderoffset, 1,
00304                            cpl_image_get_size_y(imhcycle)));
00305 
00306     /* Compute the background values of the HCYCLE frames */
00307     visir_spc_wcal_ech_config.bg_mean = visir_hcycle_background(rawframes,
00308                                                                 icol1, icol2);
00309     skip_if (cpl_error_get_code());
00310 
00311     order = visir_spc_echelle_extract(imhcycle, icol1, icol2,
00312                                       visir_spc_wcal_ech_config.plot);
00313     skip_if (cpl_error_get_code());
00314 
00315     skip_if (visir_spc_wavecal(order, wlen, slitw, temp, fwhm, resol,
00316                                visir_spc_wcal_ech_config.orderoffset,
00317                                spc_cal_lines, spc_cal_qeff, &phdisp, &xcdisp,
00318                                &(visir_spc_wcal_ech_config.wcal.xc),
00319                                &(visir_spc_wcal_ech_config.wcal.subshift),
00320                                &spc_table, visir_spc_wcal_ech_config.plot));
00321 
00322     /* Copy disp-pointers to output structure */
00323     visir_spc_wcal_ech_config.wcal.phdisp = phdisp;
00324     visir_spc_wcal_ech_config.wcal.xcdisp = xcdisp;
00325 
00326     /* Save the spectrum */
00327     cpl_msg_info(__func__, "Saving the produced spectrum");
00328     skip_if (visir_spc_wcal_ech_save(spc_table, parlist, framelist));
00329 
00330     end_skip;
00331 
00332     cpl_frameset_delete(rawframes);
00333     cpl_polynomial_delete(phdisp);
00334     cpl_polynomial_delete(xcdisp);
00335     cpl_table_delete(spc_table);
00336     cpl_image_delete(order);
00337     cpl_imagelist_delete(hcycle);
00338 
00339     return cpl_error_get_code();
00340 }
00341 
00342 /*----------------------------------------------------------------------------*/
00350 /*----------------------------------------------------------------------------*/
00351 static int visir_spc_wcal_ech_save(
00352         const cpl_table         * table, 
00353         const cpl_parameterlist * parlist,
00354         cpl_frameset            * set)
00355 {
00356     /* The filename of the reference frame */
00357     const char       * ref_file =
00358         cpl_frame_get_filename(irplib_frameset_get_first_from_group(set,
00359                                                          CPL_FRAME_GROUP_RAW));
00360     cpl_propertylist * plist = cpl_propertylist_load(ref_file, 0);
00361     cpl_propertylist * qclist = cpl_propertylist_new();
00362     const char       * capa = visir_get_capa(plist);
00363     const char       * pafcopy = "^(DATE-OBS|ARCFILE|ESO INS GRAT1 NAME)$";
00364     FILE             * paf = NULL;
00365     /* Will still create .paf even if table does not exist */
00366     const int          nrows = table != NULL ? cpl_table_get_nrow(table) : 0;
00367 
00368 
00369     /* This will catch ref_file == NULL or plist == NULL */
00370     skip_if (cpl_error_get_code());
00371 
00372     /* FIXME */
00373     visir_spc_wcal_ech_config.wcal.npix = nrows;
00374 
00375     skip_if (cpl_propertylist_copy_property_regexp(qclist, plist, pafcopy));
00376 
00377     /* THE PAF FILE FOR QC PARAMETERS */
00378     skip_if (!(paf = visir_paf_init(recipename)));
00379 
00380     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00381 
00382     /* Reuse the propertylist for QC parameters */
00383     cpl_propertylist_empty(qclist);
00384 
00385     cpl_propertylist_empty(plist); /* Just to reduce number of pointers */
00386 
00387     /* QC.BACKGD.MEAN */
00388     skip_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN",
00389                                             visir_spc_wcal_ech_config.bg_mean));
00390 
00391     /* QC.XC-related variables */
00392     skip_if (visir_propertylist_append_wcal(qclist,
00393                                            &visir_spc_wcal_ech_config.wcal));
00394 
00395     /* QC.CAPA */
00396     skip_if (capa != NULL && 
00397              cpl_propertylist_append_string(qclist, "ESO QC CAPA", capa));
00398 
00399     /* THE TABLE */
00400 
00401     skip_if (visir_table_save(parlist, set, table, recipename,
00402                               VISIR_SPC_WCAL_ECH_TAB_PROCATG,
00403                               qclist, "_spectrum_tab"));
00404 
00405     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00406 
00407     end_skip;
00408 
00409     visir_paf_end(paf);
00410 
00411     cpl_propertylist_delete(qclist);
00412     cpl_propertylist_delete(plist);
00413 
00414     return cpl_error_get_code();
00415 
00416 }

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