visir_spc_obs.c

00001 /* $Id: visir_spc_obs.c,v 1.85 2010/08/09 13:04:22 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: 2010/08/09 13:04:22 $
00024  * $Revision: 1.85 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "visir_recipe.h"
00037 #include "visir_spectro.h"
00038 #include "visir_spc_distortion.h"
00039 
00040 
00041 /*-----------------------------------------------------------------------------
00042                             Defines
00043  -----------------------------------------------------------------------------*/
00044 
00045 #define RECIPE_STRING  "visir_spc_obs"
00046 
00047 /* FITS keys to be loaded for all raw files */
00048 #define RECIPE_KEYS_REGEXP_ALL            \
00049         VISIR_PFITS_REGEXP_IMG_RECOMBINE  \
00050     "|" VISIR_PFITS_REGEXP_SPC_GET_RES_WL
00051 
00052 /* FITS keys to be loaded for first raw file */
00053 #define RECIPE_KEYS_REGEXP               \
00054         RECIPE_KEYS_REGEXP_ALL           \
00055     "|" VISIR_PFITS_REGEXP_CAPA          \
00056     "|" VISIR_PFITS_REGEXP_SPC_WCAL_PAF
00057 
00058 /* FITS keys to be loaded for first raw file, in case WCS is used */
00059 #define RECIPE_KEYS_REGEXP_WCS \
00060         RECIPE_KEYS_REGEXP \
00061     "|" IRPLIB_PFITS_WCS_REGEXP
00062 
00063 /*-----------------------------------------------------------------------------
00064                             Private Functions prototypes
00065  -----------------------------------------------------------------------------*/
00066 static cpl_error_code visir_spc_obs_save(cpl_frameset *,
00067                                          const cpl_parameterlist *,
00068                                          const cpl_propertylist *,
00069                                          const cpl_propertylist *,
00070                                          const cpl_image *,
00071                                          const cpl_image *,
00072                                          const cpl_table *);
00073 
00074 VISIR_RECIPE_DEFINE(visir_spc_obs,
00075                     VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
00076                     VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
00077                     VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
00078                     VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
00079                     VISIR_PARAM_UNION  | VISIR_PARAM_REJECT |
00080                     VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
00081                     VISIR_PARAM_PLOT   | VISIR_PARAM_SLITSKEW |
00082                     VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
00083                     VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
00084                     VISIR_PARAM_HORIARC | VISIR_PARAM_FIXCOMBI,
00085                     "Spectroscopic Observation recipe",
00086                     "This recipe estimates the dispersion relation using the "
00087                     "atmospheric spectrum\n"
00088                     "in a long-slit spectroscopy half-cycle frame.\n"
00089                     "It also extracts the spectrum of an observed object using "
00090                     "a combined frame.\n"
00091                     "The files listed in the Set Of Frames (sof-file) "
00092                     "must be tagged:\n"
00093                     "VISIR-Long-Slit-Spectroscopy-file.fits "
00094                     VISIR_SPC_OBS_RAW "\n"
00095                     "VISIR-Quantum-Efficiency-Calibration-file.fits "
00096                     VISIR_CALIB_QEFF_SPC "\n"
00097                     "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
00098                     VISIR_CALIB_LINES_SPC
00099                     "\n"
00100                     MAN_VISIR_CALIB_BPM_SPC);
00101 
00102 /*-----------------------------------------------------------------------------
00103                             Static variables
00104  -----------------------------------------------------------------------------*/
00105 
00106 static struct {
00107     /* Inputs */
00108     int         plot;
00109     double      phi;
00110     double      ksi;
00111     double      eps;
00112     double      delta;
00113 
00114 } visir_spc_obs_config;
00115 
00116 /*----------------------------------------------------------------------------*/
00120 /*----------------------------------------------------------------------------*/
00121 
00122 /*-----------------------------------------------------------------------------
00123                                 Functions code
00124  -----------------------------------------------------------------------------*/
00125 
00126 /*----------------------------------------------------------------------------*/
00133 /*----------------------------------------------------------------------------*/
00134 static int visir_spc_obs(cpl_frameset            * framelist,
00135                          const cpl_parameterlist * parlist)
00136 {
00137     irplib_framelist * allframes = NULL;
00138     irplib_framelist * rawframes = NULL;
00139     cpl_propertylist * qclist    = cpl_propertylist_new();
00140     cpl_propertylist * paflist   = cpl_propertylist_new();
00141     const char      *   badpix;
00142     const char      *   spc_cal_qeff;
00143     const char      *   spc_cal_lines;
00144     const char      *   flat;
00145     cpl_image       *   combined = NULL;
00146     cpl_image       *   comnarrow = NULL;
00147     cpl_imagelist   *   hcycle = NULL;
00148     cpl_table       *   spc_table = NULL;
00149     cpl_image       *   weight2d  = NULL;
00150     cpl_image       *   imhcycle = NULL;
00151     cpl_image       *   imhcyclenarrow = NULL;
00152     cpl_image      **   combinedpair;
00153     double              wlen, slitw, temp, fwhm;
00154     visir_spc_resol     resol;
00155     cpl_boolean         do_fixcombi;
00156     cpl_boolean        drop_wcs;
00157     int                icol1, icol2;
00158     int                jcol1, jcol2;
00159     const char       * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS ")$";
00160 
00161     visir_spc_obs_config.plot = visir_parameterlist_get_int(parlist, RECIPE_STRING,
00162                                                             VISIR_PARAM_PLOT);
00163 
00164     visir_spc_obs_config.phi = 
00165         visir_parameterlist_get_double(parlist,RECIPE_STRING,VISIR_PARAM_SLITSKEW);
00166     visir_spc_obs_config.ksi = 
00167         visir_parameterlist_get_double(parlist,RECIPE_STRING,VISIR_PARAM_SPECSKEW);
00168     visir_spc_obs_config.eps =
00169         visir_parameterlist_get_double(parlist, RECIPE_STRING,VISIR_PARAM_VERTARC);
00170     visir_spc_obs_config.delta = 
00171         visir_parameterlist_get_double(parlist, RECIPE_STRING,VISIR_PARAM_HORIARC);
00172 
00173     do_fixcombi = visir_parameterlist_get_bool(parlist, RECIPE_STRING,
00174                                                VISIR_PARAM_FIXCOMBI);
00175     jcol1 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
00176                                         VISIR_PARAM_REJLEFT);
00177     jcol2 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
00178                                         VISIR_PARAM_REJRIGHT);
00179 
00180     skip_if (0);
00181 
00182     /* Identify the RAW and CALIB frames in the input frameset */
00183     skip_if (visir_dfs_set_groups(framelist));
00184 
00185     /* Objects observation */
00186     allframes = irplib_framelist_cast(framelist);
00187     skip_if(allframes == NULL);
00188     rawframes = irplib_framelist_extract(allframes, VISIR_SPC_OBS_RAW);
00189     skip_if (rawframes == NULL);
00190 
00191     irplib_framelist_empty(allframes);
00192 
00193     skip_if(irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
00194                                                CPL_FALSE));
00195 
00196     skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
00197                                                    RECIPE_KEYS_REGEXP_ALL
00198                                                    ")$", CPL_FALSE));
00199 
00200     skip_if(visir_dfs_check_framelist_tag(rawframes));
00201     
00202     /* Quantum efficiency file */
00203     spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
00204 
00205     /* Spectral lines calibration file */
00206     spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
00207 
00208     /* Bad pixels calibration file */
00209     badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
00210 
00211     /* Flatfield calibration file */
00212     flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
00213 
00214     /* Get Resolution and Central Wavelength */
00215     resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm);
00216 
00217     skip_if (0);
00218 
00219     if (resol == VISIR_SPC_R_GHR) {
00220         cpl_msg_error(cpl_func, "This recipe cannot reduce HR Grism data");
00221         visir_error_set(CPL_ERROR_TYPE_MISMATCH);
00222         skip_if(1);
00223     }
00224 
00225     /* Combine the frames */
00226     combinedpair = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix,
00227                                        flat, CPL_GEOM_FIRST, &drop_wcs,
00228                                        !do_fixcombi, wlen, resol);
00229 
00230     if (combinedpair == NULL) {
00231         cpl_msg_error(cpl_func, "Could not combine the input frames");
00232         skip_if (1);
00233     }
00234 
00235     cpl_image_delete(combinedpair[1]);
00236     combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
00237     cpl_image_delete(combinedpair[0]);
00238     cpl_free(combinedpair);
00239 
00240     skip_if (0);
00241 
00242     skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
00243 
00244     skip_if (do_fixcombi && visir_spc_det_fix(&combined, 1, CPL_TRUE,
00245                                wlen, resol,
00246                                visir_spc_obs_config.phi,
00247                                visir_spc_obs_config.ksi,
00248                                visir_spc_obs_config.eps,
00249                                visir_spc_obs_config.delta,
00250                                visir_spc_obs_config.plot));
00251 
00252     /* Get the hcycle image from the reference file */
00253     hcycle = visir_load_hcycle(rawframes, 0);
00254     skip_if (0);
00255 
00256     imhcycle = cpl_imagelist_get(hcycle, 0);
00257 
00258     skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
00259                                wlen, resol,
00260                                visir_spc_obs_config.phi,
00261                                visir_spc_obs_config.ksi,
00262                                visir_spc_obs_config.eps,
00263                                visir_spc_obs_config.delta,
00264                                visir_spc_obs_config.plot));
00265 
00266     icol1 = 1;
00267     icol2 = cpl_image_get_size_x(combined);
00268 
00269     if (jcol1 != 0) {
00270         cpl_msg_info(cpl_func, "Ignoring %d leftmost columns", jcol1);
00271         icol1 += jcol1;
00272     }
00273     if (jcol2 != 0) {
00274         cpl_msg_info(cpl_func, "Ignoring %d rightmost columns", jcol2);
00275         icol2 -= jcol2;
00276     }
00277 
00278     if (jcol1 != 0 || jcol2 != 0) {
00279         imhcyclenarrow = visir_spc_column_extract(imhcycle, icol1, icol2,
00280                                           visir_spc_obs_config.plot);
00281         skip_if (0);
00282 
00283         comnarrow = visir_spc_column_extract(combined, icol1, icol2,
00284                                              visir_spc_obs_config.plot);
00285         skip_if (0);
00286 
00287     } else {
00288         imhcyclenarrow = imhcycle;
00289         comnarrow = combined;
00290     }
00291 
00292     skip_if (visir_spc_extract_wcal(comnarrow,
00293                                     imhcyclenarrow, wlen, slitw, temp, fwhm,
00294                                     resol, 0,
00295                                     spc_cal_lines, spc_cal_qeff,
00296                                     &spc_table, &weight2d, qclist,
00297                                     visir_spc_obs_config.plot));
00298 
00299     /* This column is not part of the product */
00300     skip_if (cpl_table_erase_column(spc_table, "SPC_EMISSIVITY"));
00301 
00302     bug_if (visir_spectro_qc(qclist, paflist, drop_wcs, rawframes, NULL,
00303                              "^(" VISIR_PFITS_REGEXP_SPC_WCAL_PAF ")$"));
00304     irplib_framelist_empty(rawframes);
00305    
00306     /* Save the spectrum */
00307     cpl_msg_info(cpl_func, "Saving the produced spectrum");
00308 
00309     /* PRO.CATG */
00310     bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00311                                            VISIR_SPC_OBS_COMBINED_PROCATG));
00312 
00313     skip_if (visir_spc_obs_save(framelist, parlist, qclist, paflist,
00314                                 combined, weight2d, spc_table));
00315 
00316     end_skip;
00317 
00318     cpl_propertylist_delete(qclist);
00319     cpl_propertylist_delete(paflist);
00320     irplib_framelist_delete(allframes);
00321     irplib_framelist_delete(rawframes);
00322     cpl_image_delete(combined);
00323     if (comnarrow != combined) cpl_image_delete(comnarrow);
00324     if (imhcyclenarrow != imhcycle) cpl_image_delete(imhcyclenarrow);
00325     cpl_table_delete(spc_table);
00326     cpl_image_delete(weight2d);
00327     cpl_imagelist_delete(hcycle);
00328 
00329     return cpl_error_get_code();
00330 }
00331 
00332 /*----------------------------------------------------------------------------*/
00344 /*----------------------------------------------------------------------------*/
00345 static cpl_error_code visir_spc_obs_save(cpl_frameset            * set,
00346                                          const cpl_parameterlist * parlist,
00347                                          const cpl_propertylist  * qclist,
00348                                          const cpl_propertylist  * paflist,
00349                                          const cpl_image         * combined,
00350                                          const cpl_image         * weight2d,
00351                                          const cpl_table         * table)
00352 {
00353 
00354     bug_if (0);
00355 
00356     /* THE TABLE */
00357     skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
00358                                VISIR_SPC_OBS_TAB_PROCATG,
00359                                qclist, NULL, visir_pipe_id,
00360                                RECIPE_STRING "_spectrum_tab" CPL_DFS_FITS));
00361 
00362     /* THE COMBINED IMAGE */
00363     skip_if (irplib_dfs_save_image(set, parlist, set, combined, CPL_BPP_IEEE_FLOAT,
00364                                RECIPE_STRING, VISIR_SPC_OBS_COMBINED_PROCATG,
00365                                qclist, NULL, visir_pipe_id,
00366                                RECIPE_STRING CPL_DFS_FITS));
00367 
00368     /* THE WEIGHT IMAGE */
00369     skip_if (irplib_dfs_save_image(set, parlist, set, weight2d, CPL_BPP_IEEE_FLOAT,
00370                                RECIPE_STRING, VISIR_SPC_OBS_WEIGHT_PROCATG,
00371                                qclist, NULL, visir_pipe_id,
00372                                RECIPE_STRING "_weight" CPL_DFS_FITS));
00373 
00374 
00375 #ifdef VISIR_SAVE_PAF
00376     /* THE PAF FILE FOR QC PARAMETERS */
00377     skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
00378                              RECIPE_STRING CPL_DFS_PAF));
00379 #else
00380     bug_if(paflist == NULL);
00381 #endif
00382 
00383     end_skip;
00384 
00385     return cpl_error_get_code();
00386 
00387 }

Generated on Thu Mar 24 11:59:39 2011 for VISIR Pipeline Reference Manual by  doxygen 1.5.8