Main Page   File List  

visir_spc_phot.c

00001 /* $Id: visir_spc_phot.c,v 1.97 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.97 $
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 
00038 /**************************************************************/
00039 #include <qfits.h>      /* NEEDED TILL CPL SUPPORTS 3D TABLES */
00040 /**************************************************************/
00041 
00042 #include <cpl.h>
00043 
00044 #include "irplib_utils.h"
00045 
00046 #include "visir_utils.h"
00047 #include "visir_pfits.h"
00048 #include "visir_dfs.h"
00049 #include "visir_inputs.h"
00050 #include "visir_spectro.h"
00051 #include "visir_spc_distortion.h"
00052 #include "visir_spc_photom.h"
00053 
00054 /*-----------------------------------------------------------------------------
00055                             Functions prototypes
00056  -----------------------------------------------------------------------------*/
00057 
00058 static int visir_spc_phot_create(cpl_plugin *);
00059 static int visir_spc_phot_exec(cpl_plugin *);
00060 static int visir_spc_phot_destroy(cpl_plugin *);
00061 static int visir_spc_phot(cpl_parameterlist *, cpl_frameset *);
00062 static int visir_spc_phot_save(const cpl_image *, const cpl_image *,
00063                                const cpl_table *,
00064                                const visir_spc_phot_config *,
00065                                const cpl_parameterlist *, cpl_frameset *);
00066 
00067 /*-----------------------------------------------------------------------------
00068                             Static variables
00069  -----------------------------------------------------------------------------*/
00070 
00071 static const char * recipename = "visir_spc_phot";
00072 
00073 static char visir_spc_phot_description[] =
00074 "This recipe estimates the dispersion relation using the atmospheric spectrum\n"
00075 "in a long-slit spectroscopy half-cycle frame.\n"
00076 "It also extracts the spectrum of an observed object using a combined frame.\n"
00077 "Lastly, it computes the spectral photometry of an observed standard star.\n" 
00078 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00079 "VISIR-Long-Slit-Spectroscopy-file.fits " VISIR_SPC_PHOT_RAW "\n"
00080 "VISIR-Quantum-Efficiency-Calibration-file.fits " VISIR_CALIB_QEFF_SPC "\n"
00081 "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits " VISIR_CALIB_LINES_SPC
00082 "\n"
00083 "VISIR-Spectroscopic-Standard-Star-Catalog.fits " VISIR_CALIB_STDSTAR_SPC "\n";
00084 
00085 /*-----------------------------------------------------------------------------
00086                                 Functions code
00087  -----------------------------------------------------------------------------*/
00088 
00089 /*----------------------------------------------------------------------------*/
00098 /*----------------------------------------------------------------------------*/
00099 int cpl_plugin_get_info(cpl_pluginlist * list)
00100 {
00101     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00102     cpl_plugin  *   plugin = &recipe->interface;
00103 
00104 
00105     if (cpl_plugin_init(plugin,
00106                     CPL_PLUGIN_API,
00107                     VISIR_BINARY_VERSION,
00108                     CPL_PLUGIN_TYPE_RECIPE,
00109                     recipename,
00110                     "Sensitivity computation in spectroscopy",
00111                     visir_spc_phot_description,
00112                     "Lars Lundin",
00113                     PACKAGE_BUGREPORT,
00114                     visir_get_license(),
00115                     visir_spc_phot_create,
00116                     visir_spc_phot_exec,
00117                     visir_spc_phot_destroy)) return 1;
00118 
00119     if (cpl_pluginlist_append(list, plugin)) return 1;
00120     
00121     return 0;
00122 }
00123 
00124 /*----------------------------------------------------------------------------*/
00133 /*----------------------------------------------------------------------------*/
00134 static int visir_spc_phot_create(cpl_plugin * plugin)
00135 {
00136     cpl_recipe * recipe = (cpl_recipe *)plugin;
00137 
00138     /* Verify plugin type */
00139     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00140 
00141     /* Create the parameters list in the cpl_recipe object */
00142     recipe->parameters = cpl_parameterlist_new();
00143 
00144     /* Fill the parameters list */
00145     return visir_parameter_set(recipe->parameters, recipename,
00146                                VISIR_PARAM_OFFSETS |
00147                                VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00148                                VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
00149                                VISIR_PARAM_UNION  | VISIR_PARAM_REJECT |
00150                                VISIR_PARAM_PLOT   | VISIR_PARAM_SLITSKEW |
00151                                VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
00152                                VISIR_PARAM_HORIARC | VISIR_PARAM_FIXCOMBI);
00153 
00154 }
00155 
00156 /*----------------------------------------------------------------------------*/
00162 /*----------------------------------------------------------------------------*/
00163 static int visir_spc_phot_exec(cpl_plugin * plugin)
00164 {
00165     cpl_recipe * recipe = (cpl_recipe *)plugin;
00166 
00167     /* Verify plugin type */
00168     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00169 
00170     return visir_spc_phot(recipe->parameters, recipe->frames);
00171 }
00172 
00173 /*----------------------------------------------------------------------------*/
00179 /*----------------------------------------------------------------------------*/
00180 static int visir_spc_phot_destroy(cpl_plugin * plugin)
00181 {
00182     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00183 
00184     /* Verify plugin type */
00185     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00186     cpl_parameterlist_delete(recipe->parameters);
00187     return  0;
00188 }
00189 
00190 /*----------------------------------------------------------------------------*/
00197 /*----------------------------------------------------------------------------*/
00198 static int visir_spc_phot(cpl_parameterlist   *   parlist, 
00199                           cpl_frameset        *   framelist)
00200 {
00201     cpl_parameter   *   par;
00202     const char      *   badpix;
00203     const char      *   star_cat;
00204     const char      *   spc_cal_qeff;
00205     const char      *   spc_cal_lines;
00206     const char      *   flat;
00207     cpl_frameset    *   rawframes = NULL;
00208     cpl_image       **  combinedpair = NULL;
00209     cpl_image       *   combined = NULL;
00210     cpl_table       *   tab = NULL;
00211     cpl_image       *   weight2d = NULL;
00212     double              wlen, slitw, temp, fwhm;
00213     visir_spc_resol     resol;
00214     visir_spc_phot_config config;
00215 
00216 
00217     if (cpl_error_get_code()) return cpl_error_get_code();
00218 
00219     config.exptime = -1.0;
00220     config.bg_mean = -1.0;
00221     config.star_name[0] = (char)0;
00222 
00223     /* Will still create .paf even if table does not exist,
00224        so e.g. obs.npix must be zero - and all pointer members must be NULL */
00225     memset( &config.obs, 0, sizeof(visir_output_obs));
00226 
00227     /* Retrieve input parameters */
00228     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.offsets");
00229     strcpy(config.offsets, cpl_parameter_get_string(par));
00230     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.nodding");
00231     strcpy(config.nodding, cpl_parameter_get_string(par));
00232     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.auto_bpm");
00233     config.auto_bpm = cpl_parameter_get_bool(par);
00234     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.rem_glitch");
00235     config.rem_glitch = cpl_parameter_get_bool(par);
00236     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.purge_bad");
00237     config.purge_bad = cpl_parameter_get_bool(par);
00238     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.union");
00239     config.union_flag = cpl_parameter_get_bool(par);
00240     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.plot");
00241     config.plot = cpl_parameter_get_int(par);
00242 
00243     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.phi");
00244     config.phi = cpl_parameter_get_double(par);
00245     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.ksi");
00246     config.ksi = cpl_parameter_get_double(par);
00247     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.eps");
00248     config.eps = cpl_parameter_get_double(par);
00249     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.delta");
00250     config.delta = cpl_parameter_get_double(par);
00251 
00252     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.rej");
00253 
00254     skip_if (sscanf(cpl_parameter_get_string(par), "%u %u",
00255                     &config.rej_low,
00256                     &config.rej_high) != 2);
00257    
00258     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot.fixcombi");
00259     config.do_fixcombi = cpl_parameter_get_bool(par);
00260 
00261     /* Identify the RAW and CALIB frames in the input frameset */
00262     skip_if (visir_dfs_set_groups(framelist));
00263 
00264     /* Objects observation */
00265     rawframes = irplib_frameset_extract(framelist, VISIR_SPC_PHOT_RAW);
00266     skip_if (rawframes == NULL);
00267 
00268     skip_if(visir_dfs_check_frameset_tag(rawframes));
00269     
00270     /* Standard star catalog */
00271     star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_SPC);
00272     if (star_cat == NULL) {
00273          cpl_msg_error(__func__, "The file with the star catalog is missing");
00274          skip_if(1);
00275     }
00276 
00277     /* Quantum efficiency file */
00278     spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00279 
00280     /* Spectral lines calibration file */
00281     spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00282 
00283     /* Bad pixels calibration file */
00284     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00285 
00286     /* Flatfield calibration file */
00287     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00288 
00289     /* Get the resolution and central wavelength */
00290     resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm);
00291 
00292     skip_if (cpl_error_get_code());
00293 
00294     if (resol == VISIR_SPC_R_GHR) {
00295         cpl_msg_error(__func__, "This recipe cannot reduce HR Grism data");
00296         visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00297         skip_if(1);
00298     }
00299         
00300     /* Combine the frames */
00301     combinedpair = visir_img_recombine(rawframes, badpix, flat,
00302                                        config.offsets,
00303                                        "",
00304                                        config.nodding,
00305                                        config.auto_bpm,
00306                                        config.rem_glitch,
00307                                        config.purge_bad,
00308                                        0, 0, 0, 0, 0,
00309                                        config.rej_low,
00310                                        config.rej_high,
00311                                        config.union_flag,
00312                                        !config.do_fixcombi, wlen, resol,
00313                                        config.ksi,
00314                                        config.phi,
00315                                        config.eps,
00316                                        config.delta,
00317                                        config.plot);
00318 
00319     if (combinedpair == NULL) {
00320         cpl_msg_error(__func__, "Could not combine the input frames");
00321         skip_if (1);
00322     }
00323 
00324     cpl_image_delete(combinedpair[1]);
00325     combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
00326     cpl_image_delete(combinedpair[0]);
00327 
00328     skip_if (cpl_error_get_code());
00329 
00330     /* Compute here the sensitivity */
00331     cpl_msg_info(__func__, "Compute the sensitivity");
00332     tab = visir_spc_phot_sensit(&combined, rawframes, &config, star_cat,
00333                                 spc_cal_lines, spc_cal_qeff, &weight2d,
00334                                 CPL_FALSE, wlen, slitw, temp, fwhm, resol);
00335 
00336     if (tab == NULL) {
00337         cpl_msg_error(__func__, "Cannot compute sensitivity: '%s' in %s",
00338                         cpl_error_get_message(), cpl_error_get_where());
00339         skip_if(1);
00340     }
00341     
00342     /* Save the results */
00343     cpl_msg_info(__func__, "Save the results");
00344     skip_if (visir_spc_phot_save(combined, weight2d, tab, &config, parlist,
00345                                  framelist));
00346 
00347     end_skip;
00348 
00349     cpl_frameset_delete(rawframes);
00350     cpl_image_delete(combined);
00351     cpl_polynomial_delete(config.obs.phdisp);
00352     cpl_polynomial_delete(config.obs.xcdisp);
00353     cpl_table_delete(tab);
00354     cpl_image_delete(weight2d);
00355 
00356     cpl_free(combinedpair);
00357 
00358     return cpl_error_get_code();
00359 }
00360  
00361 /*----------------------------------------------------------------------------*/
00372 /*----------------------------------------------------------------------------*/
00373 static int visir_spc_phot_save(
00374         const cpl_image         * combined,
00375         const cpl_image         * weight2d,
00376         const cpl_table         * table,
00377         const visir_spc_phot_config * pconfig,
00378         const cpl_parameterlist * parlist,
00379         cpl_frameset            * set)
00380 {
00381     /* The filename of the reference frame */
00382     const char       * ref_file =
00383         cpl_frame_get_filename(irplib_frameset_get_first_from_group(set,
00384                                                          CPL_FRAME_GROUP_RAW));
00385     cpl_propertylist * plist = cpl_propertylist_load(ref_file, 0);
00386     cpl_propertylist * qclist = cpl_propertylist_new();
00387     const char       * capa = visir_get_capa(plist);
00388     const char       * pafcopy = "^(DATE-OBS|ARCFILE|ESO TPL ID|ESO DET DIT"
00389                                   "|MJD-OBS|ESO INS PFOV|ESO TEL AIRM START"
00390                                   "|ESO INS GRAT1 NAME)$";
00391     FILE             * paf = NULL;
00392 
00393 
00394     /* This will catch ref_file == NULL or plist == NULL */
00395     skip_if (cpl_error_get_code());
00396 
00397     skip_if (cpl_propertylist_copy_property_regexp(qclist, plist, pafcopy));
00398 
00399     /* THE PAF FILE FOR QC PARAMETERS */
00400     skip_if (!(paf = visir_paf_init(recipename)));
00401 
00402     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00403 
00404     /* Reuse the propertylist for QC parameters */
00405     cpl_propertylist_empty(qclist);
00406 
00407     cpl_propertylist_empty(plist); /* Just to reduce number of pointers */
00408 
00409     /* QC.BACKGD.MEAN */
00410     skip_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN",
00411                                             pconfig->bg_mean));
00412 
00413     /* QC.XC-related variables */
00414     skip_if (visir_propertylist_append_obs(qclist,
00415                                            &pconfig->obs));
00416 
00417     /* QC.CAPA */
00418     skip_if (capa != NULL && 
00419              cpl_propertylist_append_string(qclist, "ESO QC CAPA", capa));
00420 
00421     /* QC.EXPTIME */
00422     skip_if (cpl_propertylist_append_double(qclist, "ESO QC EXPTIME",
00423                                             pconfig->exptime));
00424     /* QC.SENS.MEAN */
00425     skip_if (cpl_propertylist_append_double(qclist, "ESO QC SENS MEAN",
00426                                             pconfig->sens_mean));
00427     /* QC.SENS.STDEV */
00428     skip_if (cpl_propertylist_append_double(qclist, "ESO QC SENS STDEV",
00429                                             pconfig->sens_stdev));
00430     /* QC.STARNAME */
00431     skip_if (cpl_propertylist_append_string(qclist, "ESO QC STARNAME", 
00432                                             pconfig->star_name));
00433 
00434     /* SAVE THE TABLE */
00435     skip_if (visir_table_save(parlist, set, table, recipename,
00436                          VISIR_SPC_PHOT_TAB_PROCATG,
00437                          qclist, NULL));
00438 
00439     /* THE COMBINED IMAGE */
00440     skip_if (visir_image_save(parlist, set, combined, recipename,
00441                          VISIR_SPC_PHOT_COMBINED_PROCATG,
00442                          qclist, NULL));
00443 
00444     /* THE WEIGHT IMAGE */
00445     skip_if (weight2d && visir_image_save(parlist, set, weight2d, recipename,
00446                          VISIR_SPC_PHOT_WEIGHT_PROCATG,
00447                          qclist, "_weight"));
00448 
00449     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00450 
00451     end_skip;
00452 
00453     visir_paf_end(paf);
00454 
00455     cpl_propertylist_delete(qclist);
00456     cpl_propertylist_delete(plist);
00457 
00458     return cpl_error_get_code();
00459 }

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