Main Page   File List  

visir_spc_phot_ech.c

00001 /* $Id: visir_spc_phot_ech.c,v 1.25 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.25 $
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_ech_create(cpl_plugin *);
00059 static int visir_spc_phot_ech_exec(cpl_plugin *);
00060 static int visir_spc_phot_ech_destroy(cpl_plugin *);
00061 static int visir_spc_phot_ech(cpl_parameterlist *, cpl_frameset *);
00062 static int visir_spc_phot_ech_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_ech";
00072 
00073 static char visir_spc_phot_ech_description[] =
00074 "This recipe estimates the dispersion relation using the atmospheric spectrum\n"
00075 "in a echelle 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-Echelle-Spectroscopy-file.fits " VISIR_SPC_PHOT_ECH_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_ech_description,
00112                     "Lars Lundin",
00113                     PACKAGE_BUGREPORT,
00114                     visir_get_license(),
00115                     visir_spc_phot_ech_create,
00116                     visir_spc_phot_ech_exec,
00117                     visir_spc_phot_ech_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_ech_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     visir_parameter_set_double(recipe->parameters, recipename,
00146                                VISIR_PARAM_SLITSKEW | VISIR_PARAM_SPECSKEW |
00147                                VISIR_PARAM_VERTARC  | VISIR_PARAM_HORIARC, 0);
00148 
00149     return visir_parameter_set(recipe->parameters, recipename,
00150                                VISIR_PARAM_OFFSETS |
00151                                VISIR_PARAM_ORDEROFF |
00152                                VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00153                                VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
00154                                VISIR_PARAM_UNION  | VISIR_PARAM_REJECT |
00155                                VISIR_PARAM_PLOT | VISIR_PARAM_FIXCOMBI);
00156 
00157 }
00158 
00159 /*----------------------------------------------------------------------------*/
00165 /*----------------------------------------------------------------------------*/
00166 static int visir_spc_phot_ech_exec(cpl_plugin * plugin)
00167 {
00168     cpl_recipe * recipe = (cpl_recipe *)plugin;
00169 
00170     /* Verify plugin type */
00171     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00172 
00173     return visir_spc_phot_ech(recipe->parameters, recipe->frames);
00174 }
00175 
00176 /*----------------------------------------------------------------------------*/
00182 /*----------------------------------------------------------------------------*/
00183 static int visir_spc_phot_ech_destroy(cpl_plugin * plugin)
00184 {
00185     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00186 
00187     /* Verify plugin type */
00188     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00189     cpl_parameterlist_delete(recipe->parameters);
00190     return  0;
00191 }
00192 
00193 /*----------------------------------------------------------------------------*/
00200 /*----------------------------------------------------------------------------*/
00201 static int visir_spc_phot_ech(cpl_parameterlist   *   parlist, 
00202                           cpl_frameset        *   framelist)
00203 {
00204     cpl_parameter   *   par;
00205     const char      *   badpix;
00206     const char      *   star_cat;
00207     const char      *   spc_cal_qeff;
00208     const char      *   spc_cal_lines;
00209     const char      *   flat;
00210     cpl_frameset    *   rawframes = NULL;
00211     cpl_image       **  combinedpair = NULL;
00212     cpl_image       *   combined = NULL;
00213     cpl_table       *   tab = NULL;
00214     cpl_image       *   weight2d = NULL;
00215     double              wlen, slitw, temp, fwhm;
00216     visir_spc_resol     resol;
00217     visir_spc_phot_config config;
00218 
00219 
00220     if (cpl_error_get_code()) return cpl_error_get_code();
00221 
00222     config.exptime = -1.0;
00223     config.bg_mean = -1.0;
00224     config.star_name[0] = (char)0;
00225 
00226     /* Will still create .paf even if table does not exist,
00227        so e.g. obs.npix must be zero - and all pointer members must be NULL */
00228     memset( &config.obs, 0, sizeof(visir_output_obs));
00229 
00230     /* Retrieve input parameters */
00231     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.offsets");
00232     strcpy(config.offsets, cpl_parameter_get_string(par));
00233     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.nodding");
00234     strcpy(config.nodding, cpl_parameter_get_string(par));
00235     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.auto_bpm");
00236     config.auto_bpm = cpl_parameter_get_bool(par);
00237     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.rem_glitch");
00238     config.rem_glitch = cpl_parameter_get_bool(par);
00239     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.purge_bad");
00240     config.purge_bad = cpl_parameter_get_bool(par);
00241     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.union");
00242     config.union_flag = cpl_parameter_get_bool(par);
00243     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.plot");
00244     config.plot = cpl_parameter_get_int(par);
00245 
00246     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.phi");
00247     config.phi = cpl_parameter_get_double(par);
00248     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.ksi");
00249     config.ksi = cpl_parameter_get_double(par);
00250     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.eps");
00251     config.eps = cpl_parameter_get_double(par);
00252     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.delta");
00253     config.delta = cpl_parameter_get_double(par);
00254 
00255     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.orderoffset");
00256     config.orderoffset = cpl_parameter_get_int(par);
00257 
00258     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.rej");
00259 
00260     skip_if (sscanf(cpl_parameter_get_string(par), "%u %u",
00261                     &config.rej_low,
00262                     &config.rej_high) != 2);
00263    
00264     par = cpl_parameterlist_find(parlist, "visir.visir_spc_phot_ech.fixcombi");
00265     config.do_fixcombi = cpl_parameter_get_bool(par);
00266 
00267     /* Identify the RAW and CALIB frames in the input frameset */
00268     skip_if (visir_dfs_set_groups(framelist));
00269 
00270     /* Objects observation */
00271     rawframes = irplib_frameset_extract(framelist, VISIR_SPC_PHOT_ECH_RAW);
00272     skip_if (rawframes == NULL);
00273 
00274     skip_if(visir_dfs_check_frameset_tag(rawframes));
00275     
00276     /* Standard star catalog */
00277     star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_SPC);
00278     if (star_cat == NULL) {
00279          cpl_msg_error(__func__, "The file with the star catalog is missing");
00280          skip_if(1);
00281     }
00282 
00283     /* Quantum efficiency file */
00284     spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00285 
00286     /* Spectral lines calibration file */
00287     spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00288 
00289     /* Bad pixels calibration file */
00290     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00291 
00292     /* Flatfield calibration file */
00293     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00294 
00295     /* Get the resolution and central wavelength */
00296     resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm);
00297 
00298     skip_if (cpl_error_get_code());
00299 
00300     if (resol != VISIR_SPC_R_GHR) {
00301         if (config.orderoffset == 0) {
00302             cpl_msg_warning(__func__,"Reducing non-HR Grism data as main order");
00303         } else {
00304             cpl_msg_error(__func__, "This recipe cannot reduce non-HR Grism "
00305                           "data with an order-offset of %d",
00306                           config.orderoffset);
00307             visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00308             skip_if(1);
00309         }
00310     }
00311 
00312     /* Combine the frames */
00313     combinedpair = visir_img_recombine(rawframes, badpix, flat,
00314                                        config.offsets,
00315                                        "",
00316                                        config.nodding,
00317                                        config.auto_bpm,
00318                                        config.rem_glitch,
00319                                        config.purge_bad,
00320                                        0, 0, 0, 0, 0,
00321                                        config.rej_low,
00322                                        config.rej_high,
00323                                        config.union_flag,
00324                                        !config.do_fixcombi, wlen, resol,
00325                                        config.ksi,
00326                                        config.phi,
00327                                        config.eps,
00328                                        config.delta,
00329                                        config.plot);
00330 
00331     if (combinedpair == NULL) {
00332         cpl_msg_error(__func__, "Could not combine the input frames");
00333         skip_if (1);
00334     }
00335 
00336     cpl_image_delete(combinedpair[1]);
00337     combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
00338     cpl_image_delete(combinedpair[0]);
00339 
00340     skip_if (cpl_error_get_code());
00341 
00342     /* Compute here the sensitivity */
00343     cpl_msg_info(__func__, "Compute the sensitivity");
00344     tab = visir_spc_phot_sensit(&combined, rawframes, &config, star_cat,
00345                                 spc_cal_lines, spc_cal_qeff, &weight2d,
00346                                 CPL_TRUE, wlen, slitw, temp, fwhm, resol);
00347 
00348     if (tab == NULL) {
00349         cpl_msg_error(__func__, "Cannot compute sensitivity: '%s' in %s",
00350                         cpl_error_get_message(), cpl_error_get_where());
00351         skip_if(1);
00352     }
00353     
00354     /* Save the results */
00355     cpl_msg_info(__func__, "Save the results");
00356     skip_if (visir_spc_phot_ech_save(combined, weight2d, tab, &config, parlist,
00357                                      framelist));
00358 
00359     end_skip;
00360 
00361     cpl_frameset_delete(rawframes);
00362     cpl_image_delete(combined);
00363     cpl_polynomial_delete(config.obs.phdisp);
00364     cpl_polynomial_delete(config.obs.xcdisp);
00365     cpl_table_delete(tab);
00366     cpl_image_delete(weight2d);
00367 
00368     cpl_free(combinedpair);
00369 
00370     return cpl_error_get_code();
00371 }
00372  
00373 /*----------------------------------------------------------------------------*/
00384 /*----------------------------------------------------------------------------*/
00385 static int visir_spc_phot_ech_save(
00386         const cpl_image         * combined,
00387         const cpl_image         * weight2d,
00388         const cpl_table         * table,
00389         const visir_spc_phot_config * pconfig,
00390         const cpl_parameterlist * parlist,
00391         cpl_frameset            * set)
00392 {
00393     /* The filename of the reference frame */
00394     const char       * ref_file =
00395         cpl_frame_get_filename(irplib_frameset_get_first_from_group(set,
00396                                                          CPL_FRAME_GROUP_RAW));
00397     cpl_propertylist * plist = cpl_propertylist_load(ref_file, 0);
00398     cpl_propertylist * qclist = cpl_propertylist_new();
00399     const char       * capa = visir_get_capa(plist);
00400     const char       * pafcopy = "^(DATE-OBS|ARCFILE|ESO TPL ID|ESO DET DIT"
00401                                   "|MJD-OBS|ESO INS PFOV|ESO TEL AIRM START"
00402                                   "|ESO INS GRAT1 NAME)$";
00403     FILE             * paf = NULL;
00404 
00405 
00406     /* This will catch ref_file == NULL or plist == NULL */
00407     skip_if (cpl_error_get_code());
00408 
00409     skip_if (cpl_propertylist_copy_property_regexp(qclist, plist, pafcopy));
00410 
00411     /* THE PAF FILE FOR QC PARAMETERS */
00412     skip_if (!(paf = visir_paf_init(recipename)));
00413 
00414     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00415 
00416     /* Reuse the propertylist for QC parameters */
00417     cpl_propertylist_empty(qclist);
00418 
00419     cpl_propertylist_empty(plist); /* Just to reduce number of pointers */
00420 
00421     /* QC.BACKGD.MEAN */
00422     skip_if (cpl_propertylist_append_double(qclist, "ESO QC BACKGD MEAN",
00423                                             pconfig->bg_mean));
00424 
00425     /* QC.XC-related variables */
00426     skip_if (visir_propertylist_append_obs(qclist,
00427                                            &pconfig->obs));
00428 
00429     /* QC.CAPA */
00430     skip_if (capa != NULL && 
00431              cpl_propertylist_append_string(qclist, "ESO QC CAPA", capa));
00432 
00433     /* QC.EXPTIME */
00434     skip_if (cpl_propertylist_append_double(qclist, "ESO QC EXPTIME",
00435                                             pconfig->exptime));
00436     /* QC.SENS.MEAN */
00437     skip_if (cpl_propertylist_append_double(qclist, "ESO QC SENS MEAN",
00438                                             pconfig->sens_mean));
00439     /* QC.SENS.STDEV */
00440     skip_if (cpl_propertylist_append_double(qclist, "ESO QC SENS STDEV",
00441                                             pconfig->sens_stdev));
00442     /* QC.STARNAME */
00443     skip_if (cpl_propertylist_append_string(qclist, "ESO QC STARNAME", 
00444                                             pconfig->star_name));
00445 
00446     /* SAVE THE TABLE */
00447     skip_if (visir_table_save(parlist, set, table, recipename,
00448                          VISIR_SPC_PHOT_ECH_TAB_PROCATG,
00449                          qclist, NULL));
00450 
00451     /* THE COMBINED IMAGE */
00452     skip_if (visir_image_save(parlist, set, combined, recipename,
00453                          VISIR_SPC_PHOT_ECH_COMBINED_PROCATG,
00454                          qclist, NULL));
00455 
00456     /* THE WEIGHT IMAGE */
00457     skip_if (weight2d && visir_image_save(parlist, set, weight2d, recipename,
00458                          VISIR_SPC_PHOT_ECH_WEIGHT_PROCATG,
00459                          qclist, "_weight"));
00460 
00461     skip_if (irplib_propertylist_dump_paf(qclist, paf));
00462 
00463     end_skip;
00464 
00465     visir_paf_end(paf);
00466 
00467     cpl_propertylist_delete(qclist);
00468     cpl_propertylist_delete(plist);
00469 
00470     return cpl_error_get_code();
00471 }

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