fors_resample.c

00001 /* $Id: fors_resample.c,v 1.11 2013-08-14 16:11:42 cgarcia Exp $
00002  *
00003  * This file is part of the FORS Data Reduction Pipeline
00004  * Copyright (C) 2002-2010 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  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2013-08-14 16:11:42 $
00024  * $Revision: 1.11 $
00025  * $Name: not supported by cvs2svn $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <math.h>
00033 #include <cpl.h>
00034 #include <moses.h>
00035 #include <fors_dfs.h>
00036 
00037 static int fors_resample_create(cpl_plugin *);
00038 static int fors_resample_exec(cpl_plugin *);
00039 static int fors_resample_destroy(cpl_plugin *);
00040 static int fors_resample(cpl_parameterlist *, cpl_frameset *);
00041 
00042 static char fors_resample_description[] =
00043 "This recipe is used to resample at constant wavelength step spatially\n"
00044 "rectified spectra. The input frames are produced using either the recipe\n"
00045 "fors_extract_slits in the case of MOS/MXU multi slit exposures, or the\n"
00046 "recipes fors_remove_bias and fors_flatfield in the case of LSS or long-slit\n"
00047 "like MOS/MXU data. Only in case of LSS or LSS-like data the SLIT_LOCATION\n"
00048 "table is required in input. Please refer to the FORS Pipeline User's Manual\n"
00049 "for more details.\n"
00050 "\n"
00051 "In the table below the MXU acronym can also be read as MOS and LSS, SCI\n"
00052 "can be read as STD, and SCIENCE as STANDARD.\n\n"
00053 "Input files:\n\n"
00054 "  DO category:               Type:       Explanation:         Required:\n"
00055 "  LAMP_UNBIAS_MXU\n"
00056 "  or SCIENCE_UNBIAS_MXU\n"
00057 "  or SCIENCE_UNFLAT_MXU\n"
00058 "  or RECTIFIED_LAMP_MXU\n"
00059 "  or RECTIFIED_ALL_SCI_MXU\n"
00060 "  or RECTIFIED_SCI_MXU\n"
00061 "  or RECTIFIED_SKY_SCI_MXU   Calib       Frame to resample       Y\n"
00062 "  DISP_COEFF_MXU\n"
00063 "  or DISP_COEFF_SCI_MXU      Calib       Dispersion coefficients Y\n"
00064 "  SLIT_LOCATION_MXU          Calib       Slit location table     Y\n"
00065 "  GRISM_TABLE                Calib       Grism table             .\n\n"
00066 "Output files:\n\n"
00067 "  DO category:               Data type:  Explanation:\n"
00068 "  MAPPED_LAMP_MXU\n"
00069 "  or MAPPED_ALL_SCI_MXU\n"
00070 "  or MAPPED_SCI_MXU\n"
00071 "  or MAPPED_SKY_SCI_MXU      FITS image  Resampled spectra\n\n";
00072 
00073 #define fors_resample_exit(message)           \
00074 {                                             \
00075 if ((const char *)message != NULL) cpl_msg_error(recipe, message);  \
00076 cpl_image_delete(spectra);                    \
00077 cpl_image_delete(mapped);                     \
00078 cpl_table_delete(grism_table);                \
00079 cpl_table_delete(idscoeff);                   \
00080 cpl_table_delete(slits);                      \
00081 cpl_propertylist_delete(header);              \
00082 cpl_msg_indent_less();                        \
00083 return -1;                                    \
00084 }
00085 
00086 #define fors_resample_exit_memcheck(message)    \
00087 {                                               \
00088 if ((const char *)message != NULL) cpl_msg_info(recipe, message);     \
00089 printf("free spectra (%p)\n", spectra);         \
00090 cpl_image_delete(spectra);                      \
00091 printf("free mapped (%p)\n", mapped);           \
00092 cpl_image_delete(mapped);                       \
00093 printf("free grism_table (%p)\n", grism_table); \
00094 cpl_table_delete(grism_table);                  \
00095 printf("free idscoeff (%p)\n", idscoeff);       \
00096 cpl_table_delete(idscoeff);                     \
00097 printf("free slits (%p)\n", slits);             \
00098 cpl_table_delete(slits);                        \
00099 printf("free header (%p)\n", header);           \
00100 cpl_propertylist_delete(header);                \
00101 cpl_msg_indent_less();                          \
00102 return 0;                                       \
00103 }
00104 
00105 
00117 int cpl_plugin_get_info(cpl_pluginlist *list)
00118 {
00119     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe );
00120     cpl_plugin *plugin = &recipe->interface;
00121 
00122     cpl_plugin_init(plugin,
00123                     CPL_PLUGIN_API,
00124                     FORS_BINARY_VERSION,
00125                     CPL_PLUGIN_TYPE_RECIPE,
00126                     "fors_resample",
00127                     "Resample input spectra at constant wavelength step",
00128                     fors_resample_description,
00129                     "Carlo Izzo",
00130                     PACKAGE_BUGREPORT,
00131     "This file is currently part of the FORS Instrument Pipeline\n"
00132     "Copyright (C) 2002-2010 European Southern Observatory\n\n"
00133     "This program is free software; you can redistribute it and/or modify\n"
00134     "it under the terms of the GNU General Public License as published by\n"
00135     "the Free Software Foundation; either version 2 of the License, or\n"
00136     "(at your option) any later version.\n\n"
00137     "This program is distributed in the hope that it will be useful,\n"
00138     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00139     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
00140     "GNU General Public License for more details.\n\n"
00141     "You should have received a copy of the GNU General Public License\n"
00142     "along with this program; if not, write to the Free Software Foundation,\n"
00143     "Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n",
00144                     fors_resample_create,
00145                     fors_resample_exec,
00146                     fors_resample_destroy);
00147 
00148     cpl_pluginlist_append(list, plugin);
00149     
00150     return 0;
00151 }
00152 
00153 
00164 static int fors_resample_create(cpl_plugin *plugin)
00165 {
00166     cpl_recipe    *recipe;
00167     cpl_parameter *p;
00168 
00169     /* 
00170      * Check that the plugin is part of a valid recipe 
00171      */
00172 
00173     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00174         recipe = (cpl_recipe *)plugin;
00175     else 
00176         return -1;
00177 
00178     /* 
00179      * Create the (empty) parameters list in the cpl_recipe object 
00180      */
00181 
00182     recipe->parameters = cpl_parameterlist_new(); 
00183 
00184     /*
00185      * Dispersion
00186      */
00187 
00188     p = cpl_parameter_new_value("fors.fors_resample.dispersion",
00189                                 CPL_TYPE_DOUBLE,
00190                                 "Expected spectral dispersion (Angstrom/pixel)",
00191                                 "fors.fors_resample",
00192                                 0.0);
00193     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dispersion");
00194     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00195     cpl_parameterlist_append(recipe->parameters, p);
00196 
00197     /*
00198      * Start wavelength for spectral extraction
00199      */
00200 
00201     p = cpl_parameter_new_value("fors.fors_resample.startwavelength",
00202                                 CPL_TYPE_DOUBLE,
00203                                 "Start wavelength in spectral extraction",
00204                                 "fors.fors_resample",
00205                                 0.0);
00206     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "startwavelength");
00207     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00208     cpl_parameterlist_append(recipe->parameters, p);
00209 
00210     /*
00211      * End wavelength for spectral extraction
00212      */
00213 
00214     p = cpl_parameter_new_value("fors.fors_resample.endwavelength",
00215                                 CPL_TYPE_DOUBLE,
00216                                 "End wavelength in spectral extraction",
00217                                 "fors.fors_resample",
00218                                 0.0);
00219     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "endwavelength");
00220     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00221     cpl_parameterlist_append(recipe->parameters, p);
00222 
00223     /*
00224      * Flux conservation
00225      */
00226  
00227     p = cpl_parameter_new_value("fors.fors_resample.flux",
00228                                 CPL_TYPE_BOOL,
00229                                 "Apply flux conservation",
00230                                 "fors.fors_resample",
00231                                 TRUE);
00232     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flux");
00233     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00234     cpl_parameterlist_append(recipe->parameters, p);
00235 
00236     return 0;
00237 }
00238 
00239 
00248 static int fors_resample_exec(cpl_plugin *plugin)
00249 {
00250     cpl_recipe *recipe;
00251     
00252     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00253         recipe = (cpl_recipe *)plugin;
00254     else 
00255         return -1;
00256 
00257     return fors_resample(recipe->parameters, recipe->frames);
00258 }
00259 
00260 
00269 static int fors_resample_destroy(cpl_plugin *plugin)
00270 {
00271     cpl_recipe *recipe;
00272     
00273     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00274         recipe = (cpl_recipe *)plugin;
00275     else 
00276         return -1;
00277 
00278     cpl_parameterlist_delete(recipe->parameters); 
00279 
00280     return 0;
00281 }
00282 
00283 
00293 static int fors_resample(cpl_parameterlist *parlist, 
00294                                cpl_frameset *frameset)
00295 {
00296 
00297     const char *recipe = "fors_resample";
00298 
00299 
00300     /*
00301      * Input parameters
00302      */
00303 
00304     double      dispersion;
00305     double      startwavelength;
00306     double      endwavelength;
00307     int         flux;
00308 
00309     /*
00310      * CPL objects
00311      */
00312 
00313     cpl_image        *spectra     = NULL;
00314     cpl_image        *mapped      = NULL;
00315     cpl_table        *grism_table = NULL;
00316     cpl_table        *maskslits   = NULL;
00317     cpl_table        *slits       = NULL;
00318     cpl_table        *idscoeff    = NULL;
00319     cpl_propertylist *header      = NULL;
00320 
00321     /*
00322      * Auxiliary variables
00323      */
00324 
00325     char        version[80];
00326     const char *disp_coeff_tag;
00327     const char *slit_location_tag;
00328     const char *rectified_tag;
00329     const char *mapped_tag;
00330     int         nframes;
00331     int         rebin;
00332     double      reference;
00333     int         treat_as_lss = 0;
00334     int         mxu, mos, lss;
00335     int         disp;
00336     int         dispsci;
00337     int         dispstd;
00338     int         sciall;
00339     int         stdall;
00340     int         scisky;
00341     int         stdsky;
00342     int         sci;
00343     int         std;
00344     int         lamp;
00345 
00346     char       *instrume = NULL;
00347 
00348 
00349     cpl_msg_set_indentation(2);
00350 
00351     /*
00352      * Get configuration parameters
00353      */
00354 
00355     cpl_msg_info(recipe, "Recipe %s configuration parameters:", recipe);
00356     cpl_msg_indent_more();
00357     
00358     if (cpl_frameset_count_tags(frameset, "GRISM_TABLE") > 1)
00359         fors_resample_exit("Too many in input: GRISM_TABLE"); 
00360 
00361     grism_table = dfs_load_table(frameset, "GRISM_TABLE", 1);
00362 
00363     dispersion = dfs_get_parameter_double(parlist,
00364                     "fors.fors_resample.dispersion", grism_table);
00365 
00366     if (dispersion <= 0.0)
00367         fors_resample_exit("Invalid spectral dispersion value");
00368 
00369     startwavelength = dfs_get_parameter_double(parlist,
00370                     "fors.fors_resample.startwavelength", grism_table);
00371     if (startwavelength > 1.0)
00372         if (startwavelength < 3000.0 || startwavelength > 13000.0)
00373             fors_resample_exit("Invalid wavelength");
00374 
00375     endwavelength = dfs_get_parameter_double(parlist,
00376                     "fors.fors_resample.endwavelength", grism_table);
00377     if (endwavelength > 1.0) {
00378         if (endwavelength < 3000.0 || endwavelength > 13000.0)
00379             fors_resample_exit("Invalid wavelength");
00380         if (startwavelength < 1.0)
00381             fors_resample_exit("Invalid wavelength interval");
00382     }
00383 
00384     if (startwavelength > 1.0)
00385         if (endwavelength - startwavelength <= 0.0)
00386             fors_resample_exit("Invalid wavelength interval");
00387 
00388     flux = dfs_get_parameter_bool(parlist, "fors.fors_resample.flux", NULL);
00389 
00390     cpl_table_delete(grism_table); grism_table = NULL;
00391 
00392     if (cpl_error_get_code())
00393         fors_resample_exit("Failure reading the configuration parameters");
00394 
00395 
00396     cpl_msg_indent_less();
00397     cpl_msg_info(recipe, "Check input set-of-frames:");
00398     cpl_msg_indent_more();
00399 
00400     mxu  = cpl_frameset_count_tags(frameset, "DISP_COEFF_MXU");
00401     mxu += cpl_frameset_count_tags(frameset, "DISP_COEFF_SCI_MXU");
00402     mxu += cpl_frameset_count_tags(frameset, "DISP_COEFF_STD_MXU");
00403     mos  = cpl_frameset_count_tags(frameset, "DISP_COEFF_MOS");
00404     mos += cpl_frameset_count_tags(frameset, "DISP_COEFF_SCI_MOS");
00405     mos += cpl_frameset_count_tags(frameset, "DISP_COEFF_STD_MOS");
00406     lss  = cpl_frameset_count_tags(frameset, "DISP_COEFF_LSS");
00407     lss += cpl_frameset_count_tags(frameset, "DISP_COEFF_SCI_LSS");
00408     lss += cpl_frameset_count_tags(frameset, "DISP_COEFF_STD_LSS");
00409 
00410     nframes = mos + mxu + lss;
00411 
00412     if (nframes == 0) {
00413         fors_resample_exit("Missing dispersion coefficients table");
00414     }
00415     if (nframes > 1) {
00416         cpl_msg_error(recipe, 
00417                       "Too many input dispersion coefficients tables (%d > 1)",
00418                       nframes);
00419         fors_resample_exit(NULL);
00420     }
00421 
00422     disp     = cpl_frameset_count_tags(frameset, "DISP_COEFF_MXU");
00423     disp    += cpl_frameset_count_tags(frameset, "DISP_COEFF_MOS");
00424     disp    += cpl_frameset_count_tags(frameset, "DISP_COEFF_LSS");
00425     dispsci  = cpl_frameset_count_tags(frameset, "DISP_COEFF_SCI_MXU");
00426     dispsci += cpl_frameset_count_tags(frameset, "DISP_COEFF_SCI_MOS");
00427     dispsci += cpl_frameset_count_tags(frameset, "DISP_COEFF_SCI_LSS");
00428     dispstd  = cpl_frameset_count_tags(frameset, "DISP_COEFF_STD_MXU");
00429     dispstd += cpl_frameset_count_tags(frameset, "DISP_COEFF_STD_MOS");
00430     dispstd += cpl_frameset_count_tags(frameset, "DISP_COEFF_STD_LSS");
00431 
00432     if (mxu) {
00433         slit_location_tag = "SLIT_LOCATION_MXU";
00434         if (disp)
00435             disp_coeff_tag = "DISP_COEFF_MXU";
00436         else if (dispsci)
00437             disp_coeff_tag = "DISP_COEFF_SCI_MXU";
00438         else
00439             disp_coeff_tag = "DISP_COEFF_STD_MXU";
00440     }
00441     else if (mos) {
00442         slit_location_tag = "SLIT_LOCATION_MOS";
00443         if (disp)
00444             disp_coeff_tag = "DISP_COEFF_MOS";
00445         else if (dispsci)
00446             disp_coeff_tag = "DISP_COEFF_SCI_MOS";
00447         else
00448             disp_coeff_tag = "DISP_COEFF_STD_MOS";
00449     }
00450     else {
00451         slit_location_tag = "SLIT_LOCATION_LSS";
00452         if (disp)
00453             disp_coeff_tag = "DISP_COEFF_LSS";
00454         else if (dispsci)
00455             disp_coeff_tag = "DISP_COEFF_SCI_LSS";
00456         else
00457             disp_coeff_tag = "DISP_COEFF_STD_LSS";
00458     }
00459 
00460     header = dfs_load_header(frameset, disp_coeff_tag, 0);
00461 
00462     if (header == NULL)
00463         fors_resample_exit("Cannot load dispersion coefficients table header");
00464 
00465     if (mos || mxu) {
00466         int nslits_out_det = 0;
00467 
00468         if (mos)
00469             maskslits = mos_load_slits_fors_mos(header, &nslits_out_det);
00470         else
00471             maskslits = mos_load_slits_fors_mxu(header);
00472 
00473         /*
00474          * Check if all slits have the same X offset.
00475          */
00476 
00477         treat_as_lss = fors_mos_is_lss_like(maskslits, nslits_out_det);
00478 
00479         cpl_table_delete(maskslits); maskslits = NULL;
00480     }
00481 
00482     cpl_propertylist_delete(header); header = NULL;
00483 
00484     if (mxu) {
00485         if (treat_as_lss) {
00486             sciall = cpl_frameset_count_tags(frameset, "SCIENCE_UNFLAT_MXU");
00487             stdall = cpl_frameset_count_tags(frameset, "STANDARD_UNFLAT_MXU");
00488             scisky = 0;
00489             stdsky = 0;
00490             sci    = cpl_frameset_count_tags(frameset, "SCIENCE_UNBIAS_MXU");
00491             std    = cpl_frameset_count_tags(frameset, "STANDARD_UNBIAS_MXU");
00492             lamp   = cpl_frameset_count_tags(frameset, "LAMP_UNBIAS_MXU");
00493         }
00494         else {
00495             sciall = cpl_frameset_count_tags(frameset, "RECTIFIED_ALL_SCI_MXU");
00496             stdall = cpl_frameset_count_tags(frameset, "RECTIFIED_ALL_STD_MXU");
00497             scisky = cpl_frameset_count_tags(frameset, "RECTIFIED_SKY_SCI_MXU");
00498             stdsky = cpl_frameset_count_tags(frameset, "RECTIFIED_SKY_STD_MXU");
00499             sci    = cpl_frameset_count_tags(frameset, "RECTIFIED_SCI_MXU");
00500             std    = cpl_frameset_count_tags(frameset, "RECTIFIED_STD_MXU");
00501             lamp   = cpl_frameset_count_tags(frameset, "RECTIFIED_LAMP_MXU");
00502         }
00503     }
00504     else if (mos) {
00505         if (treat_as_lss) {
00506             sciall = cpl_frameset_count_tags(frameset, "SCIENCE_UNFLAT_MOS");
00507             stdall = cpl_frameset_count_tags(frameset, "STANDARD_UNFLAT_MOS");
00508             scisky = 0;
00509             stdsky = 0;
00510             sci    = cpl_frameset_count_tags(frameset, "SCIENCE_UNBIAS_MOS");
00511             std    = cpl_frameset_count_tags(frameset, "STANDARD_UNBIAS_MOS");
00512             lamp   = cpl_frameset_count_tags(frameset, "LAMP_UNBIAS_MOS");
00513         }
00514         else {
00515             sciall = cpl_frameset_count_tags(frameset, "RECTIFIED_ALL_SCI_MOS");
00516             stdall = cpl_frameset_count_tags(frameset, "RECTIFIED_ALL_STD_MOS");
00517             scisky = cpl_frameset_count_tags(frameset, "RECTIFIED_SKY_SCI_MOS");
00518             stdsky = cpl_frameset_count_tags(frameset, "RECTIFIED_SKY_STD_MOS");
00519             sci    = cpl_frameset_count_tags(frameset, "RECTIFIED_SCI_MOS");
00520             std    = cpl_frameset_count_tags(frameset, "RECTIFIED_STD_MOS");
00521             lamp   = cpl_frameset_count_tags(frameset, "RECTIFIED_LAMP_MOS");
00522         }
00523     }
00524     else {
00525         sciall = cpl_frameset_count_tags(frameset, "SCIENCE_UNFLAT_LSS");
00526         stdall = cpl_frameset_count_tags(frameset, "STANDARD_UNFLAT_LSS");
00527         scisky = 0;
00528         stdsky = 0;
00529         sci    = cpl_frameset_count_tags(frameset, "SCIENCE_UNBIAS_LSS");
00530         std    = cpl_frameset_count_tags(frameset, "STANDARD_UNBIAS_LSS");
00531         lamp   = cpl_frameset_count_tags(frameset, "LAMP_UNBIAS_LSS");
00532     }
00533 
00534     nframes = sciall + stdall + scisky + stdsky + sci + std + lamp;
00535 
00536     if (nframes == 0)
00537         fors_resample_exit("Missing input spectral frame");
00538 
00539     if (nframes > 1) {
00540         cpl_msg_error(recipe, "Too many input spectral frames (%d > 1)", 
00541                       nframes);
00542         fors_resample_exit(NULL);
00543     }
00544 
00545     if (sciall) {
00546         if (mxu) {
00547             if (treat_as_lss) {
00548                 rectified_tag = "SCIENCE_UNFLAT_MXU";
00549                 mapped_tag    = "MAPPED_ALL_SCI_MXU";
00550             }
00551             else {
00552                 rectified_tag = "RECTIFIED_ALL_SCI_MXU";
00553                 mapped_tag    = "MAPPED_ALL_SCI_MXU";
00554             }
00555         }
00556         else if (mos) {
00557             if (treat_as_lss) {
00558                 rectified_tag = "SCIENCE_UNFLAT_MOS";
00559                 mapped_tag    = "MAPPED_ALL_SCI_MOS";
00560             }
00561             else {
00562                 rectified_tag = "RECTIFIED_ALL_SCI_MOS";
00563                 mapped_tag    = "MAPPED_ALL_SCI_MOS";
00564             }
00565         }
00566         else {
00567             rectified_tag = "SCIENCE_UNFLAT_LSS";
00568             mapped_tag    = "MAPPED_ALL_SCI_LSS";
00569         }
00570     }
00571     else if (stdall) {
00572         if (mxu) {
00573             if (treat_as_lss) {
00574                 rectified_tag = "STANDARD_UNFLAT_MXU";
00575                 mapped_tag    = "MAPPED_ALL_STD_MXU";
00576             }
00577             else {
00578                 rectified_tag = "RECTIFIED_ALL_STD_MXU";
00579                 mapped_tag    = "MAPPED_ALL_STD_MXU";
00580             }
00581         }
00582         else if (mos) {
00583             if (treat_as_lss) {
00584                 rectified_tag = "STANDARD_UNFLAT_MOS";
00585                 mapped_tag    = "MAPPED_ALL_STD_MOS";
00586             }
00587             else {
00588                 rectified_tag = "RECTIFIED_ALL_STD_MOS";
00589                 mapped_tag    = "MAPPED_ALL_STD_MOS";
00590             }
00591         }
00592         else {
00593             rectified_tag = "STANDARD_UNFLAT_LSS";
00594             mapped_tag    = "MAPPED_ALL_STD_LSS";
00595         }
00596     }
00597     else if (scisky) {
00598         if (mxu) {
00599             rectified_tag = "RECTIFIED_SKY_SCI_MXU";
00600             mapped_tag    = "MAPPED_SKY_SCI_MXU";
00601         }
00602         else {
00603             rectified_tag = "RECTIFIED_SKY_SCI_MOS";
00604             mapped_tag    = "MAPPED_SKY_SCI_MOS";
00605         }
00606     }
00607     else if (stdsky) {
00608         if (mxu) {
00609             rectified_tag = "RECTIFIED_SKY_STD_MXU";
00610             mapped_tag    = "MAPPED_SKY_STD_MXU";
00611         }
00612         else {
00613             rectified_tag = "RECTIFIED_SKY_STD_MOS";
00614             mapped_tag    = "MAPPED_SKY_STD_MOS";
00615         }
00616     }
00617     else if (sci) {
00618         if (mxu) {
00619             if (treat_as_lss) {
00620                 rectified_tag = "SCIENCE_UNBIAS_MXU";
00621                 mapped_tag    = "MAPPED_ALL_SCI_MXU";
00622             }
00623             else {
00624                 rectified_tag = "RECTIFIED_SCI_MXU";
00625                 mapped_tag    = "MAPPED_SCI_MXU";
00626             }
00627         }
00628         else if (mos) {
00629             if (treat_as_lss) {
00630                 rectified_tag = "SCIENCE_UNBIAS_MOS";
00631                 mapped_tag    = "MAPPED_ALL_SCI_MOS";
00632             }
00633             else {
00634                 rectified_tag = "RECTIFIED_SCI_MOS";
00635                 mapped_tag    = "MAPPED_SCI_MOS";
00636             }
00637         }
00638         else {
00639             rectified_tag = "SCIENCE_UNBIAS_LSS";
00640             mapped_tag    = "MAPPED_ALL_SCI_LSS";
00641         }
00642     }
00643     else if (std) {
00644         if (mxu) {
00645             if (treat_as_lss) {
00646                 rectified_tag = "STANDARD_UNBIAS_MXU";
00647                 mapped_tag    = "MAPPED_ALL_STD_MXU";
00648             }
00649             else {
00650                 rectified_tag = "RECTIFIED_STD_MXU";
00651                 mapped_tag    = "MAPPED_STD_MXU";
00652             }
00653         }
00654         else if (mos) {
00655             if (treat_as_lss) {
00656                 rectified_tag = "STANDARD_UNBIAS_MOS";
00657                 mapped_tag    = "MAPPED_ALL_STD_MOS";
00658             }
00659             else {
00660                 rectified_tag = "RECTIFIED_STD_MOS";
00661                 mapped_tag    = "MAPPED_STD_MOS";
00662             }
00663         }
00664         else {
00665             rectified_tag = "STANDARD_UNBIAS_LSS";
00666             mapped_tag    = "MAPPED_ALL_STD_LSS";
00667         }
00668     }
00669     else if (lamp) {
00670         if (mxu) {
00671             if (treat_as_lss) {
00672                 rectified_tag = "LAMP_UNBIAS_MXU";
00673                 mapped_tag    = "MAPPED_LAMP_MXU";
00674             }
00675             else {
00676                 rectified_tag = "RECTIFIED_LAMP_MXU";
00677                 mapped_tag    = "MAPPED_LAMP_MXU";
00678             }
00679         }
00680         else if (mos) {
00681             if (treat_as_lss) {
00682                 rectified_tag = "LAMP_UNBIAS_MOS";
00683                 mapped_tag    = "MAPPED_LAMP_MOS";
00684             }
00685             else {
00686                 rectified_tag = "RECTIFIED_LAMP_MOS";
00687                 mapped_tag    = "MAPPED_LAMP_MOS";
00688             }
00689         }
00690         else {
00691             rectified_tag = "LAMP_UNBIAS_LSS";
00692             mapped_tag    = "MAPPED_LAMP_LSS";
00693         }
00694     }
00695 
00696     header = dfs_load_header(frameset, rectified_tag, 0);
00697 
00698     if (header == NULL)
00699         fors_resample_exit("Cannot load spectral frame header");
00700 
00701 
00702     if (!dfs_equal_keyword(frameset, "ESO INS GRIS1 ID"))
00703         cpl_msg_warning(cpl_func,"Input frames are not from the same grism");
00704 
00705     if (!dfs_equal_keyword(frameset, "ESO INS FILT1 ID"))
00706         cpl_msg_warning(cpl_func,"Input frames are not from the same filter");
00707 
00708     if (!dfs_equal_keyword(frameset, "ESO DET CHIP1 ID"))
00709         cpl_msg_warning(cpl_func,"Input frames are not from the same chip");
00710 
00711 
00712     /*
00713      * Get the reference wavelength and the rebin factor along the
00714      * dispersion direction from the reference frame
00715      */
00716 
00717     instrume = (char *)cpl_propertylist_get_string(header, "INSTRUME");
00718     if (instrume == NULL)
00719         fors_resample_exit("Missing keyword INSTRUME in reference frame "
00720                             "header");
00721 
00722     if (instrume[4] == '1')
00723         snprintf(version, 80, "%s/%s", "fors1", VERSION);
00724     if (instrume[4] == '2')
00725         snprintf(version, 80, "%s/%s", "fors2", VERSION);
00726 
00727     reference = cpl_propertylist_get_double(header, "ESO INS GRIS1 WLEN");
00728 
00729     if (cpl_error_get_code() != CPL_ERROR_NONE)
00730         fors_resample_exit("Missing keyword ESO INS GRIS1 WLEN "
00731                             "in reference frame header");
00732 
00733     if (reference < 3000.0)   /* Perhaps in nanometers... */
00734         reference *= 10;
00735 
00736     if (reference < 3000.0 || reference > 13000.0) {
00737         cpl_msg_error(recipe, "Invalid central wavelength %.2f read from "
00738                       "keyword ESO INS GRIS1 WLEN in reference frame header",
00739                       reference);
00740         fors_resample_exit(NULL);
00741     }
00742 
00743     cpl_msg_info(recipe, "The central wavelength is: %.2f", reference);
00744 
00745     rebin = cpl_propertylist_get_int(header, "ESO DET WIN1 BINX");
00746 
00747     if (cpl_error_get_code() != CPL_ERROR_NONE)
00748         fors_resample_exit("Missing keyword ESO DET WIN1 BINX "
00749                             "in reference frame header");
00750 
00751     if (rebin != 1) {
00752         dispersion *= rebin;
00753         cpl_msg_warning(recipe, "The rebin factor is %d, and therefore the "
00754                         "working dispersion used is %f A/pixel", rebin,
00755                         dispersion);
00756     }
00757 
00758 
00759     cpl_msg_indent_less();
00760     cpl_msg_info(recipe, "Load input frames...");
00761     cpl_msg_indent_more();
00762 
00763     spectra = dfs_load_image(frameset, rectified_tag, CPL_TYPE_FLOAT, 0, 0);
00764     if (spectra == NULL)
00765         fors_resample_exit("Cannot load input spectral frame");
00766 
00767     idscoeff = dfs_load_table(frameset, disp_coeff_tag, 1);
00768     if (idscoeff == NULL)
00769         fors_resample_exit("Cannot load dispersion solution table");
00770 
00771     if (lss || treat_as_lss) {
00772         int        first_row, last_row, ylow, yhig, nx;
00773         cpl_image *dummy;
00774 
00775         slits = dfs_load_table(frameset, slit_location_tag, 1);
00776         if (slits == NULL)
00777             fors_resample_exit("Cannot load slit location table");
00778 
00779         first_row = cpl_table_get_double(slits, "ybottom", 0, NULL);
00780         last_row = cpl_table_get_double(slits, "ytop", 0, NULL);
00781 
00782         ylow = first_row + 1;
00783         yhig = last_row + 1;
00784 
00785         nx = cpl_image_get_size_x(spectra);
00786 
00787         dummy = cpl_image_extract(spectra, 1, ylow, nx, yhig);
00788         cpl_image_delete(spectra); spectra = dummy;
00789     }
00790 
00791     cpl_msg_indent_less();
00792     cpl_msg_info(recipe, "Spectral resampling...");
00793     cpl_msg_indent_more();
00794 
00795     mapped = mos_wavelength_calibration(spectra, reference,
00796                                         startwavelength, endwavelength,
00797                                         dispersion, idscoeff, flux);
00798 
00799     cpl_table_delete(idscoeff); idscoeff = NULL;
00800     cpl_image_delete(spectra); spectra = NULL;
00801 
00802     cpl_propertylist_update_double(header, "CRPIX1", 1.0);
00803     cpl_propertylist_update_double(header, "CRPIX2", 1.0);
00804     cpl_propertylist_update_double(header, "CRVAL1",
00805                                    startwavelength + dispersion/2);
00806     cpl_propertylist_update_double(header, "CRVAL2", 1.0);
00807     /* cpl_propertylist_update_double(header, "CDELT1", dispersion);
00808     cpl_propertylist_update_double(header, "CDELT2", 1.0); */
00809     cpl_propertylist_update_double(header, "CD1_1", dispersion);
00810     cpl_propertylist_update_double(header, "CD1_2", 0.0);
00811     cpl_propertylist_update_double(header, "CD2_1", 0.0);
00812     cpl_propertylist_update_double(header, "CD2_2", 1.0);
00813     cpl_propertylist_update_string(header, "CTYPE1", "LINEAR");
00814     cpl_propertylist_update_string(header, "CTYPE2", "PIXEL");
00815 
00816     if (dfs_save_image(frameset, mapped, mapped_tag,
00817                        header, parlist, recipe, version))
00818         fors_resample_exit(NULL);
00819 
00820     cpl_image_delete(mapped); mapped = NULL;
00821     cpl_propertylist_delete(header); header = NULL;
00822 
00823     return 0;
00824 }

Generated on 12 Feb 2016 for FORS Pipeline Reference Manual by  doxygen 1.6.1