uves_reduce_scired.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019 
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2010/12/13 08:21:50 $
00023  * $Revision: 1.58 $
00024  * $Name: uves-4_9_1 $
00025  *
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 /*----------------------------------------------------------------------------*/
00037 /*----------------------------------------------------------------------------*/
00041 /*-----------------------------------------------------------------------------
00042                                 Includes
00043  -----------------------------------------------------------------------------*/
00044 #include <uves_reduce_scired.h>
00045 
00046 #include <uves_reduce.h>
00047 #include <uves_reduce_utils.h>
00048 #include <uves_corrbadpix.h>
00049 
00050 #include <uves_chip.h>
00051 #include <uves_plot.h>
00052 #include <uves_dfs.h>
00053 #include <uves_pfits.h>
00054 #include <uves_parameters.h>
00055 #include <uves_msg.h>
00056 #include <uves_utils.h>
00057 #include <uves_utils_wrappers.h>
00058 #include <uves_qclog.h>
00059 #include <uves_error.h>
00060 #include <uves_merge.h>
00061 #include <uves.h>
00062 
00063 #include <cpl.h>
00064 #include <string.h>
00065 /*-----------------------------------------------------------------------------
00066                             Functions prototypes
00067  -----------------------------------------------------------------------------*/
00068 
00069 static void
00070 scired_qclog(const cpl_table* info_tbl, 
00071          const uves_propertylist *raw_header,
00072          const cpl_image *raw_image,
00073          double slit,
00074          cpl_table* qclog);
00075 
00076 static void
00077 tflat_qclog(const cpl_image* ima,
00078         const uves_propertylist *raw_header,
00079         cpl_table* qclog);
00080 
00081 
00082 /*-----------------------------------------------------------------------------
00083                             Implementation
00084  -----------------------------------------------------------------------------*/
00085 const char * const uves_scired_desc_short = "Reduces a science frame";
00086 const char * const uves_scired_desc =
00087 "This recipe reduces a science frame (SCIENCE_BLUE or SCIENCE_RED, or\n"
00088 "UVES_SCI_POINT_BLUE or UVES_SCI_POINT_RED, or \n"
00089 "UVES_SCI_EXTND_BLUE or UVES_SCI_EXTND_RED or \n"
00090 "UVES_SCI_SLICER_BLUE or UVES_SCI_SLICER_RED) using "
00091 "a combination (depending on recipe parameters and provided input frames) of "
00092 "the steps:\n"
00093 " - bias subtraction,\n"
00094 " - dark subtraction,\n"
00095 " - background subtraction,\n"
00096 " - extraction/cosmic ray removal,\n"
00097 " - flat field correction,\n"
00098 " - wavelength rebinning,\n"
00099 " - sky subtraction,\n"
00100 " - order merging,\n"
00101 " - response correction (if response curve is provided).\n"
00102 "\n"
00103 "Additional input for this recipe are: \n"
00104 "order table(s) for each chip, ORDER_TABLE_xxxx (where xxxx=BLUE, REDL, REDU),\n"
00105 "line table(s) for each chip, LINE_TABLE_xxxx, a master bias frame,\n"
00106 "MASTER_BIAS_xxxx, a master flat, MASTER_FLAT_xxxx, \n"
00107 "optionally an instrument  response table, INSTR_RESPONSE_xxx\n"
00108 "optionally a table describing the atmospheric extintion,\n"
00109 "EXTCOEFF_TABLE. \n"
00110 "For each chip (xxxx = BLUE, REDL, REDU) the recipe produces a combination of "
00111 "the products:\n"
00112 " 'RED_SCIENCE_xxxx'        Reduced science spectrum\n"
00113 " 'MERGED_SCIENCE_xxxx'     Merged spectrum, no sky subtraction\n"
00114 " 'WCALIB_SCIENCE_xxxx'     Extracted, wavelength calibrated frame in\n"
00115 "                           (wavelength, order) space\n"
00116 " 'WCALIB_FF_SCIENCE_xxxx'  Extracted, flat-fielded, wave.cal. frame in\n"
00117 "                           (wavelength, order) space\n"
00118 "                           (Only if flatfielding done)\n"
00119 " 'WCALIB_FLAT_OBJ_xxxx'    Extracted, wavelength calibrated flat field\n"
00120 "                           in (wavelength, order) space\n"
00121 "                           (Only if flatfielding done)\n"
00122 " 'ERRORBAR_SCIENCE_xxxx'   Error bars of 'RED_SCIENCE_xxxx'\n"
00123 " 'VARIANCE_SCIENCE_xxxx'   Variance of extracted, flatfielded object in\n"
00124 "                           (pixel, order) space\n"
00125 " 'ORDER_TRACE_xxxx'        Table describing the spatial profile\n"
00126 " 'FLUXCAL_SCIENCE_xxxx'    Flux-calibrated science spectrum\n"
00127 " 'FLUXCAL_ERROR_xxxx'      Error bars of 'FLUXCAL_SCIENCE_xxxx'\n"
00128 " 'BKG_SCI_xxxx'            The subtracted background image\n"
00129 " 'CRMASK_xxxx'             List of cosmic ray hits\n"
00130 " 'MERGED_SKY_xxxx'         The merged sky spectrum\n"
00131 " 'EXT_2D_SCIENCE_xxxx'     The 2d extracted spectrum\n"
00132 " 'FF2D_SCIENCE_xxxx'       The 2d extracted, flat-fielded spectrum\n"
00133 " 'WCAL2D_SCIENCE_xxxx'     The 2d extracted, flat-fielded, wave.cal. spectrum\n"
00134 " 'MER2D_SCIENCE_xxxx'      The 2d reduced, flux-calibrated (if possible) \n"
00135 "                           science spectrum\n";
00136 
00137 /*----------------------------------------------------------------------------*/
00144 /*----------------------------------------------------------------------------*/
00145 int uves_scired_define_parameters_body(cpl_parameterlist *parameters,
00146                        const char *recipe_id)
00147 {
00148 
00149     /*****************
00150      *    General    *
00151      *****************/
00152     if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00153         {
00154             return -1;
00155         }
00156 
00157     /**************************************
00158      *  detector's trap correction        *
00159      **************************************/
00160  
00161     if (uves_corr_traps_define_parameters(parameters,recipe_id) 
00162         != CPL_ERROR_NONE)
00163         {
00164             return -1;
00165         }
00166 
00167     /*******************
00168      *  Reduce.        *
00169      ******************/
00170     if (uves_propagate_parameters_step(UVES_REDUCE_ID, parameters,
00171                                        recipe_id, NULL) != 0)
00172         {
00173             return -1;
00174         }
00175     
00176     return (cpl_error_get_code() != CPL_ERROR_NONE);
00177 }
00178 
00179 
00180 const char*
00181 uves_get_pro_catg_special(bool extract_is_2d, merge_method m_method) {
00182    const char* result=NULL;
00183    if(extract_is_2d && m_method == MERGE_NOAPPEND) {
00184       result="";
00185    } else if (!extract_is_2d && 
00186               m_method == MERGE_NOAPPEND) {
00187       result="_NONMERGED";
00188    } else {
00189       result="";
00190    }
00191 
00192    return result;
00193 }
00194 
00195 /*----------------------------------------------------------------------------*/
00201 /*----------------------------------------------------------------------------*/
00202 static cpl_frame **
00203 set_all_raw_none(cpl_frameset *frames)
00204 {
00205     cpl_frame **result = NULL;
00206     cpl_frame *f;
00207     int i;
00208 
00209     result = cpl_calloc( cpl_frameset_get_size(frames) + 1,
00210                          sizeof(*result) );
00211     
00212     for (f = cpl_frameset_get_first(frames), i = 0;
00213          f != NULL;
00214          f = cpl_frameset_get_next(frames))
00215         {
00216             if (cpl_frame_get_group(f) == CPL_FRAME_GROUP_RAW)
00217                 {
00218                     /* Change + remember this frame */
00219                     cpl_frame_set_group(f, CPL_FRAME_GROUP_NONE);
00220                     result[i] = f;
00221                     i++;
00222                 }
00223         }
00224 
00225     /* 'result' is now a NULL-terminated array of the frames that were changed */
00226 
00227     return result;
00228 }
00229 
00230 /*----------------------------------------------------------------------------*/
00300 /*----------------------------------------------------------------------------*/
00301 
00302 static cpl_error_code
00303 uves_scired_process_chip(const cpl_image *raw_image, 
00304                          const uves_propertylist *raw_header,
00305                          const uves_propertylist *rotated_header,
00306                          const cpl_image *master_bias,
00307                          const cpl_image *master_dark, 
00308                          const uves_propertylist *mdark_header,
00309                          const cpl_image *master_flat, 
00310                          const uves_propertylist *mflat_header,
00311                          const cpl_table *ordertable, 
00312                          const polynomial *order_locations,
00313                          const cpl_table *linetable[3], 
00314                          const uves_propertylist *linetable_header[3], 
00315                          const polynomial *dispersion_relation[3],
00316                          const cpl_image *response_curve, 
00317              const cpl_table *master_response,
00318                          const uves_propertylist *response_curve_header,
00319                          const cpl_table *atm_extinction,
00320                          enum uves_chip chip,
00321                          /* General */
00322                          bool   DEBUG,
00323                          /* Backsub */
00324                          /* Flat fielding */
00325                          /* Extraction */
00326                          /* Rebinning  */
00327                          const cpl_parameterlist *parameters,
00328              const char *recipe_id,
00329                          /* Output     */
00330                          cpl_image **x2d, uves_propertylist **x2d_header,
00331                          cpl_image **fx2d,
00332                          cpl_image **background,
00333                          cpl_image **flatfielded_variance,
00334                          uves_propertylist **flatfielded_variance_header,
00335                          cpl_image **resampled_science,
00336                          cpl_image **resampled_mf,
00337                          cpl_image **rebinned_science, 
00338                          uves_propertylist **rebinned_header,
00339                          cpl_image **merged_sky,
00340                          cpl_image **merged_science, 
00341                          uves_propertylist **merged_header,
00342                          cpl_image **reduced_science,
00343                          cpl_image **reduced_science_error,
00344                          cpl_table **cosmic_mask,
00345                          cpl_image **wave_map,
00346                          cpl_image **fluxcal_science,
00347                          cpl_image **fluxcal_error,
00348                          uves_propertylist **fluxcal_header,
00349                          cpl_table **info_tbl,
00350              double *extraction_slit,
00351                          cpl_table **order_trace)
00352 {
00353     cpl_image *rebinned_noise = NULL;
00354     cpl_image *merged_noise   = NULL;
00355 
00356     cpl_image *reduced_rebinned = NULL;
00357     cpl_image *reduced_rebinned_noise = NULL;
00358 
00359     cpl_table *response_table = NULL;
00360 
00361     /* Do the science reduction. Produces wave.cal. spectra. */
00362     uves_msg("Reducing science object");
00363 
00364     check( uves_reduce(raw_image, 
00365                        raw_header, 
00366                        rotated_header,
00367                        master_bias,
00368                        master_dark, 
00369                        mdark_header,
00370                        master_flat, 
00371                        mflat_header, 
00372                        ordertable, 
00373                        order_locations,
00374                        linetable, 
00375                        linetable_header, 
00376                        dispersion_relation,
00377                        chip,
00378                        DEBUG, 
00379                        parameters, 
00380                        recipe_id,
00381                        "",
00382                        /* Output */
00383                        x2d, 
00384                        x2d_header, 
00385                        fx2d,
00386                       cosmic_mask,
00387                        wave_map,
00388                         background,
00389                        flatfielded_variance, 
00390                        flatfielded_variance_header,
00391                        resampled_science,
00392                        resampled_mf,
00393                        merged_sky,
00394                        rebinned_science, 
00395                        &rebinned_noise, 
00396                        rebinned_header,
00397                        merged_science, 
00398                        &merged_noise, 
00399                        merged_header,
00400                        &reduced_rebinned, 
00401                        &reduced_rebinned_noise,
00402                        reduced_science, 
00403                        reduced_science_error,
00404                        info_tbl,
00405                extraction_slit,
00406                        order_trace),
00407            "Could not reduce frame");
00408 
00409     /* Plot middle row */
00410     check( uves_plot_image_rows(*reduced_science, 
00411                 1 + cpl_image_get_size_y(*reduced_science)/2,
00412                 1 + cpl_image_get_size_y(*reduced_science)/2, 1,
00413                 "Wavelength (arbitrary units)", "Relative flux",
00414                 "Reduced science spectrum"),
00415        "Plotting failed");
00416     
00417 
00418     /*
00419      * Flux calibrate the reduced spectrum
00420      * (which is an image of height 1, or more if extract.method=2d)
00421      */
00422 
00423     if (response_curve != NULL || master_response != NULL)
00424         {
00425             double lambda_start;
00426             double dlambda;
00427             int bin;
00428 
00429             /* Number of spatial traces (> 1 for 2d extraction) */
00430             int n_traces = cpl_image_get_size_y(*reduced_science);
00431             
00432             uves_msg("Flux calibrating spectrum");
00433 
00434             /* We cannot be certain that the formats (wavelength start, bin width)
00435                of the science spectrum and the response curve are identical.
00436                Therefore we interpolate the response curve at the wavelengths 
00437                defined by the bins of the science spectrum. */
00438                 
00439 
00440 
00441             /* If the response curve is an image, convert to table.
00442            This is needed for the interpolation */
00443         if (response_curve != NULL) {
00444                 response_table = cpl_table_new(cpl_image_get_size_x(response_curve));
00445                 cpl_table_new_column(response_table, "LAMBDA", CPL_TYPE_DOUBLE);
00446                 cpl_table_new_column(response_table, "FLUX_CONV", CPL_TYPE_DOUBLE);
00447                 
00448                 check( lambda_start = uves_pfits_get_crval1(response_curve_header),
00449                        "Error reading response curve start wavelength from header");
00450                 
00451                 check( dlambda = uves_pfits_get_cdelt1(response_curve_header),
00452                        "Error reading bin width from header");
00453                 
00454                 for (bin = 1; bin <= cpl_image_get_size_x(response_curve); bin++) {
00455                     double lambda;
00456                     double response;
00457                     int pis_rejected;
00458                     
00459                     lambda = lambda_start + (bin-1) * dlambda;
00460                     
00461                     check( response = cpl_image_get(response_curve, bin, 1, &pis_rejected),
00462                            "Error reading response curve bin = %d", bin);
00463                     
00464                     check((cpl_table_set_double(response_table, "LAMBDA", bin - 1, lambda),
00465                            cpl_table_set_double(response_table, "FLUX_CONV", bin - 1, response)),
00466                           "Error updating response table at row %d", bin - 1);
00467                 }
00468             } 
00469         else {
00470         response_table = cpl_table_duplicate( master_response );
00471         } /* Response table created */        
00472             
00473             /*
00474              * Correct for exposure time, gain, binning, atm. ext.
00475              */
00476             
00477             check( *fluxcal_science = uves_normalize_spectrum(*reduced_science,
00478                                                               *reduced_science_error,
00479                                                               *merged_header,
00480                                                               raw_header,
00481                                                               n_traces,
00482                                                               chip,
00483                                                               atm_extinction,
00484                                                               true,    /* Divide by binning? */
00485                                                               fluxcal_error), 
00486                    "Error normalizing reduced spectrum");
00487             
00488             /* 
00489              * Flux calibrate reduced spectrum 
00490              *  flux := flux * response
00491              */
00492             uves_msg("Multiplying by response function");
00493             {
00494                 int nbins   = cpl_image_get_size_x(*fluxcal_science);
00495                 int ntraces = cpl_image_get_size_y(*fluxcal_science);
00496                 double *fluxcal_science_data  = cpl_image_get_data_double(*fluxcal_science);
00497                 double *fluxcal_science_noise = cpl_image_get_data_double(*fluxcal_error);
00498                 
00499                 check( lambda_start = uves_pfits_get_crval1(*merged_header),
00500                        "Error reading start wavelength from reduced science header");
00501                 
00502                 check( dlambda = uves_pfits_get_cdelt1(*merged_header),
00503                        "Error reading bin width from header");
00504                 
00505                 for (bin = 1; bin <= nbins; bin++)
00506                     {
00507                         double lambda;
00508                         double response;
00509                         int trace;       /* Spatial traces (for 2d extracted spectra) */
00510                         int istart = 0;
00511 
00512                         lambda = lambda_start + (bin-1) * dlambda;
00513                         
00514                         check( response = 
00515                                uves_spline_hermite_table(lambda, response_table, 
00516                            "LAMBDA", "FLUX_CONV", &istart),
00517                                "Error interpolating response curve at lambda = %f wlu", lambda);
00518                         
00519                         for (trace = 1; trace <= ntraces; trace++)
00520                             {
00521                 /* Don't check for bad pixels here, also correct those.
00522                  * The fluxcal image has the same bad pixels as the reduced_science
00523                  * image */
00524 
00525                 fluxcal_science_data [(bin-1) + (trace-1)*nbins] *= response;
00526                 fluxcal_science_noise[(bin-1) + (trace-1)*nbins] *= response;
00527 
00528                 /* Do not propagate the error of the response 
00529                    curve which is negligibly small (and unknown at this point!).
00530                 */
00531                 }
00532             }
00533                 
00534                 /* Plot middle row */
00535                 check( uves_plot_image_rows(*fluxcal_science, 
00536                                             1 + cpl_image_get_size_y(*fluxcal_science)/2,
00537                                             1 + cpl_image_get_size_y(*fluxcal_science)/2, 1,
00538                                             "Wavelength (arbitrary units)", 
00539                                             "Flux (10^-16 erg/cm^2/A/sec)", 
00540                                             "Flux calibrated science spectrum"),
00541                        "Plotting failed");
00542                 
00543                 check( *fluxcal_header = uves_initialize_image_header("WAVELENGTH (A)", " ", 
00544                                                                       "FLUX (10^-16 erg/cm^2/A/sec)",
00545                                                                       lambda_start, 1.0,
00546                                                                       1.0, 1.0,
00547                                                                       dlambda, 1.0),
00548                        "Error initializing flux calibrated spectrum header");
00549             } /* Done multiplying by response curve */
00550         }
00551     else
00552         {
00553             uves_msg("Skipping absolute flux calibration");
00554         }
00555     
00556   cleanup:
00557     uves_free_image(&rebinned_noise);
00558     uves_free_image(&merged_noise);
00559     uves_free_image(&reduced_rebinned_noise);
00560     uves_free_image(&reduced_rebinned);
00561     uves_free_table(&response_table);
00562 
00563     if (cpl_error_get_code() != CPL_ERROR_NONE)
00564         {
00565         }
00566 
00567     return cpl_error_get_code();
00568 }
00569 
00570 /*----------------------------------------------------------------------------*/
00579 /*----------------------------------------------------------------------------*/
00580 void uves_reduce_scired(cpl_frameset *frames, const cpl_parameterlist *parameters, 
00581             const char *recipe_id, const char *starttime)
00582 {
00583     /* Recipe parameters */
00584     bool DEBUG;
00585     bool CLEAN_TRAPS;
00586     bool extract_is_2d = false;     /* Are we doing a 2d reduction? */
00587 
00588     /* Input, raw */
00589     cpl_image        *raw_image[2]      = {NULL, NULL};
00590     uves_propertylist *raw_header[2]     = {NULL, NULL};
00591     uves_propertylist *rotated_header[2] = {NULL, NULL};
00592 
00593     /* Input, calib */
00594     cpl_image        *master_bias        = NULL;
00595     uves_propertylist *master_bias_header = NULL;
00596 
00597     cpl_image        *master_flat        = NULL;
00598     uves_propertylist *master_flat_header = NULL;
00599 
00600     cpl_image        *master_dark        = NULL;
00601     uves_propertylist *master_dark_header = NULL;
00602 
00603     cpl_table        *ordertable       = NULL;
00604     uves_propertylist *ordertable_header= NULL;
00605     polynomial       *order_locations  = NULL;
00606     cpl_table        *traces           = NULL;
00607 
00608     /* Line tables for sky, object, sky (UVES specific) */
00609     const cpl_table        *linetable[3]           = {NULL, NULL, NULL};
00610     const uves_propertylist *linetable_header[3]    = {NULL, NULL, NULL};
00611     const polynomial       *dispersion_relation[3] = {NULL, NULL, NULL};
00612 
00613     cpl_image        *response_curve        = NULL;
00614     uves_propertylist *response_curve_header = NULL;
00615     cpl_table        *master_response       = NULL;
00616 
00617     cpl_table        *atm_extinction        = NULL;
00618 
00619     /* Output */
00620     /*  BKG_SCI           */
00621     cpl_image        *background            = NULL;
00622 
00623     /*  VARIANCE_SCIENCE                 */  
00624     cpl_image        *flatfielded_variance  = NULL;
00625     uves_propertylist *flatfielded_variance_header = NULL;
00626 
00627     /*  WCALIB_SCIENCE    */
00628     cpl_image        *resampled_science     = NULL;     /* extracted -> rebinned */
00629     /*  WCALIB_FLAT_OBJ   */
00630     cpl_image        *resampled_mf          = NULL;
00631     /*  WCALIB_FF_SCIENCE */
00632     cpl_image        *rebinned_science      = NULL;     /* extracted -> ff -> rebinned */
00633 
00634     uves_propertylist *rebinned_header       = NULL;
00635 
00636     /*  MERGED_SKY        */
00637     cpl_image        *merged_sky            = NULL;
00638 
00639     /*  MERGED_SCIENCE / MER2D_SCIENCE   */
00640     /*  RED_SCIENCE                      */
00641     /*  ERRORBAR_SCIENCE                 */
00642     cpl_image        *merged_science        = NULL;
00643     uves_propertylist *merged_header         = NULL;
00644     cpl_image        *reduced_science       = NULL;
00645     cpl_image        *reduced_science_error = NULL;
00646   
00647 
00648     /*  FLUXCAL_SCIENCE / FLUXCAL_ERROR */
00649     cpl_image        *fluxcal_science       = NULL;
00650     cpl_image        *fluxcal_error         = NULL;
00651     uves_propertylist *fluxcal_header       = NULL;
00652 
00653     /*  ORDER_TRACE       */
00654     cpl_table         *order_trace        = NULL;
00655     uves_propertylist *order_trace_header = NULL;
00656 
00657 
00658     /* EXT_2D_SCIENCE */
00659     cpl_image        *x2d        = NULL;
00660     uves_propertylist *x2d_header = NULL;
00661     /* FF2D_SCIENCE */
00662     cpl_image        *fx2d       = NULL;
00663 
00664     /*  CRMASK            */
00665     cpl_table        *cosmic_mask           = NULL;
00666     uves_propertylist *cosmic_mask_header    = NULL;
00667 
00668     /* QC */
00669     cpl_table* qclog[2] = {NULL, NULL};
00670     cpl_table *qclog_tflat = NULL;
00671 
00672     /* Local variables */
00673     const char *raw_filename = "";
00674     const char *atm_ext_filename = "";
00675     const char *sci_type = "";
00676     cpl_frame **raw_frames = NULL;  /* Array of cpl_frame pointers */
00677     char *product_tag = NULL;
00678     char *product_filename = NULL;
00679     char *context = NULL;
00680     double extraction_slit;
00681 
00682     bool blue  = false;
00683     enum uves_chip chip;
00684     int binx = 0;
00685     int biny = 0;
00686     
00687     cpl_table* info_tbl = NULL;
00688     const char* PROCESS_CHIP=NULL;
00689     bool red_ccd_is_new=0;
00690     merge_method m_method;
00691     const char* catg_is_noappend=NULL;
00692     cpl_image* wave_map=NULL;
00693     uves_propertylist* wave_map_header=NULL;
00694 
00695     /* Read recipe parameters */
00696     {
00697         const char *ex_method = "";
00698 
00699         /* General */
00700         check( uves_get_parameter(parameters, NULL, "uves", "debug", CPL_TYPE_BOOL, &DEBUG),
00701                "Could not read parameter");
00702 
00703 
00704 
00705         check( uves_get_parameter(parameters, NULL, "uves", "debug", CPL_TYPE_BOOL, &DEBUG),
00706                "Could not read parameter");
00707 
00708 
00709 
00710     check( uves_get_parameter(parameters, NULL, "uves", "process_chip", CPL_TYPE_STRING, &PROCESS_CHIP),
00711                "Could not read parameter");
00712     uves_string_toupper((char*)PROCESS_CHIP);
00713 
00714 
00715     check( uves_get_parameter(parameters,NULL,recipe_id, "clean_traps", CPL_TYPE_BOOL, &CLEAN_TRAPS),
00716                "Could not read parameter");
00717 
00718 
00719         
00720         /* Reduction method */
00721     context = uves_sprintf("%s.%s.%s", recipe_id, UVES_REDUCE_ID, UVES_EXTRACT_ID);
00722 
00723         check( uves_get_parameter(parameters, NULL, 
00724                                   context, "method",
00725                                   CPL_TYPE_STRING, &ex_method),
00726                "Could not read parameter");
00727 
00728         extract_is_2d = (strcmp(ex_method, "2d") == 0); 
00729     
00730     /* Load raw image and header, and identify input frame as red or blue */
00731     check( uves_load_science(frames, &raw_filename, raw_image, raw_header, rotated_header,
00732                  &blue, &sci_type),
00733            "Error loading raw frame");
00734     
00735     if ((strcmp(sci_type, "SCI_SLICER") == 0 ||
00736          strcmp(sci_type, "SCI_EXTND" ) == 0) &&
00737         strcmp(ex_method, "optimal") == 0)
00738         {
00739         uves_msg_warning("Going to optimally extract an extended object (%s). "
00740                  "This may not work because the sky cannot be "
00741                  "reliably determined",
00742                  sci_type);
00743         }
00744     }
00745     
00746     /* Load atmospheric extinction table if present */
00747     if (cpl_frameset_find(frames, UVES_EXTCOEFF_TABLE) != NULL)
00748         {
00749             check( uves_load_atmo_ext(frames, &atm_ext_filename, &atm_extinction),
00750                    "Error loading atm. extinction coefficients");
00751             
00752             uves_msg_low("Using atmospheric extinction table in '%s'", atm_ext_filename);
00753         }
00754     else
00755         {
00756             uves_msg_low("No atmospheric extinction table. Flux calibration not done");
00757         }
00758 
00759     check( m_method = uves_get_merge_method(parameters, recipe_id, "reduce"),
00760            "Could not get merging method");
00761     
00762     /* Adjust parameters according to binning 
00763      * (note that x- and y-directions are swapped later by uves_crop_and_rotate()) 
00764      */
00765     check (binx = uves_pfits_get_binx(raw_header[0]), 
00766            "Could not read x binning factor from input header");
00767     check (biny = uves_pfits_get_biny(raw_header[0]), 
00768            "Could not read y binning factor from input header");
00769 
00770     check_nomsg(red_ccd_is_new=uves_ccd_is_new(raw_header[0]));
00771     /* Loop over one or two chips, over traces and
00772        over extraction windows */
00773     for (chip = uves_chip_get_first(blue);
00774          chip != UVES_CHIP_INVALID; 
00775          chip = uves_chip_get_next(chip))
00776         {
00777 
00778 
00779 
00780       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00781     chip = uves_chip_get_next(chip);
00782       }
00783 
00784             cpl_frame *mflat_frame = NULL;
00785             const char *ordertable_filename = "";
00786             const char *linetable_filename = "";
00787             const char *master_bias_filename = "";
00788             const char *master_dark_filename = "";
00789             const char *master_flat_filename = "";
00790             const char *response_curve_filename = "";
00791             const char *chip_name = "";
00792             /* const char *drs_filename        = "";    not used */
00793             /* Do this to skip REDL chip: chip = uves_chip_get_next(chip); */
00794             int raw_index = uves_chip_get_index(chip);
00795             int tracerow;                      /* Index of table row */
00796             
00797             uves_msg("Processing %s chip in '%s'",
00798                      uves_chip_tostring_upper(chip), raw_filename);
00799             
00800             check_nomsg( chip_name = uves_pfits_get_chipid(raw_header[raw_index], chip));
00801 
00802             uves_msg_debug("Binning = %dx%d", binx, biny);
00803             
00804             
00805             /* Load master bias, set pointer to NULL if not present */
00806             uves_free_image(&master_bias);
00807             uves_free_propertylist(&master_bias_header);
00808             if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
00809                 {
00810                     uves_free_image(&master_bias);
00811                     uves_free_propertylist(&master_bias_header);
00812 
00813                     check( uves_load_mbias(frames, chip_name, &master_bias_filename,
00814                                            &master_bias, &master_bias_header,
00815                                            chip),
00816                            "Error loading master bias");
00817                     
00818                     uves_msg_low("Using master bias in '%s'", master_bias_filename);
00819                 }
00820             else
00821                 {
00822                     uves_msg_low("No master bias in SOF. Bias subtraction not done");
00823                 }
00824 
00825             /* Load master dark, set pointer to NULL if not present */
00826             uves_free_image(&master_dark);
00827             uves_free_propertylist(&master_dark_header);
00828             if (cpl_frameset_find(frames, UVES_MASTER_DARK(chip))  != NULL ||
00829                 cpl_frameset_find(frames, UVES_MASTER_PDARK(chip)) != NULL)
00830 
00831                 {
00832                     check( uves_load_mdark(frames, chip_name, &master_dark_filename,
00833                                            &master_dark, &master_dark_header, chip),
00834                            "Error loading master dark");
00835                     
00836                     uves_msg_low("Using master dark in '%s'", master_dark_filename);
00837                 }
00838             else
00839                 {
00840                     uves_msg_low("No master dark in SOF. Dark subtraction not done");
00841                 }
00842             
00843             /* Load master flat */
00844             uves_free_image(&master_flat);
00845             uves_free_propertylist(&master_flat_header);
00846             check( uves_load_mflat(frames, chip_name, &master_flat_filename, &master_flat, 
00847                                    &master_flat_header, chip, &mflat_frame),
00848                    "Error loading master flat");
00849             
00850             uves_msg_low("Using master flat in '%s'", master_flat_filename);
00851             
00852             
00853             /* Load the order table for this chip */
00854             uves_free_table       (&ordertable);
00855             uves_free_propertylist(&ordertable_header);
00856             uves_polynomial_delete(&order_locations);
00857             uves_free_table       (&traces);
00858             
00859             check( uves_load_ordertable(frames, 
00860                     false,  /* FLAMES? */
00861                     chip_name, 
00862                                         &ordertable_filename, 
00863                                         &ordertable, 
00864                                         &ordertable_header,
00865                                         NULL, 
00866                                         &order_locations, 
00867                                         &traces,
00868                     NULL, NULL,
00869                                        NULL, NULL, /* fibre_pos,fibre_mask */
00870                                         chip, false),
00871                    "Could not load order table");
00872             uves_msg_low("Using order table in '%s'", ordertable_filename);
00873 
00874             /* Load response curve, if present. 
00875                Only if atm. extinction table was present. */
00876             if (atm_extinction != NULL)
00877                 {
00878                     if (cpl_frameset_find(frames, UVES_INSTR_RESPONSE(chip)) != NULL ||
00879             cpl_frameset_find(frames, UVES_MASTER_RESPONSE(chip)) != NULL)
00880                         {
00881                             uves_free_image(&response_curve);
00882                             uves_free_table(&master_response);
00883                             uves_free_propertylist(&response_curve_header);
00884                             check( uves_load_response_curve(frames, chip_name,
00885                                                             &response_curve_filename,
00886                                                             &response_curve, 
00887                                 &master_response,
00888                                                             &response_curve_header,
00889                                                             chip),
00890                                    "Error loading response curve");
00891                             
00892                 uves_msg_low("Using %sresponse curve in '%s'", 
00893                      master_response != NULL ? "master " : "",
00894                      response_curve_filename);
00895                         }
00896                     else
00897                         {
00898                             uves_msg_low("No response curve in SOF. "
00899                                          "Flux calibration not done");
00900                         }
00901                 }
00902             else
00903                 {
00904                     uves_msg_debug("There is no atmospheric extinction table. "
00905                                    "Do not look for response curve");
00906                 }
00907             
00908             /* Loop over all traces (1 trace for UVES) */
00909             for(tracerow = 0; tracerow < cpl_table_get_nrow(traces); tracerow++)
00910                 {
00911                     double trace_offset;
00912                     int trace_number;
00913                     int trace_enabled;
00914                     int badpixels_cleaned;
00915                     trace_offset  = cpl_table_get_double(traces, "Offset"    , tracerow, NULL);
00916                     trace_number  = cpl_table_get_int   (traces, "TraceID"   , tracerow, NULL);
00917                     trace_enabled = cpl_table_get_int   (traces, "Tracemask" , tracerow, NULL);
00918                     
00919                     if (trace_enabled != 0)
00920                         {
00921                             int window;          /* window number */
00922                             
00923                             if (cpl_table_get_nrow(traces) > 1) {
00924                                 uves_msg("Processing trace %d", trace_number);
00925                             }
00926                             
00927                             /* This is UVES specific. Load linetable for the 
00928                                two sky windows (number 1, 3) and for the object
00929                                window (number 2) */
00930                             
00931                             for (window = 1; window <= 3; window ++) {
00932                                 uves_free_table_const ( &(linetable[window-1]) );
00933                                 uves_free_propertylist_const( &(linetable_header[window-1]) );
00934                                 uves_polynomial_delete_const( &(dispersion_relation[window-1]) );
00935                                 check( uves_load_linetable_const(frames,
00936                                false,  /* FLAMES? */
00937                                chip_name,
00938                                                            order_locations,
00939                                                            cpl_table_get_column_min(
00940                                                                ordertable, "Order"),
00941                                                            cpl_table_get_column_max(
00942                                                                ordertable, "Order"),
00943                                                            &linetable_filename,
00944                                                            &(linetable          [window-1]),
00945                                                            &(linetable_header   [window-1]),
00946                                                            &(dispersion_relation[window-1]),
00947                                                            NULL,
00948                                                            chip,
00949                                                            trace_number,
00950                                                            window),
00951                                        "Could not load line table, window #%d", window);
00952                                 
00953                                 uves_msg_low("Using line table(s) in '%s'", linetable_filename);
00954                             }
00955                             
00956                             /* end, UVES specific */
00957                             
00958                             /* Do the science reduction + flux calibration */
00959                             uves_free_image(&x2d);
00960                             uves_free_image(&fx2d);
00961                             uves_free_propertylist(&x2d_header);
00962                             uves_free_image(&background);
00963                             uves_free_image(&flatfielded_variance);
00964                             uves_free_propertylist(&flatfielded_variance_header);
00965                             uves_free_image(&resampled_science);
00966                             uves_free_image(&resampled_mf);
00967                             uves_free_image(&rebinned_science);
00968                             uves_free_propertylist(&rebinned_header);
00969                             uves_free_image(&merged_sky);
00970                             uves_free_image(&merged_science);
00971                             uves_free_propertylist(&merged_header);
00972                             uves_free_image(&reduced_science);
00973                             uves_free_image(&reduced_science_error);
00974                             uves_free_table(&cosmic_mask);
00975                             uves_free_image(&fluxcal_science);
00976                             uves_free_image(&fluxcal_error);
00977                             uves_free_propertylist(&fluxcal_header);
00978                             uves_free_table(&info_tbl);
00979                             uves_free_table(&order_trace);                            
00980 
00981                             if(CLEAN_TRAPS) {
00982 
00983                             check( badpixels_cleaned = 
00984                                    uves_correct_badpix_all(raw_image[raw_index],
00985                                                            raw_header[raw_index], 
00986                                                            chip, binx, biny, 
00987                                                            false,red_ccd_is_new),
00988                                    "Error replacing bad pixels");
00989 
00990                             uves_msg("%d bad pixels replaced", 
00991                                      badpixels_cleaned);
00992                             }
00993 
00994 
00995                             check( uves_scired_process_chip(
00996                                        raw_image[raw_index], 
00997                                        raw_header[raw_index],           /* Raw         */
00998                                        rotated_header[raw_index],
00999                                        master_bias,           /* Calibration */
01000                                        master_dark, 
01001                                        master_dark_header,
01002                                        master_flat, 
01003                                        master_flat_header,
01004                                        ordertable, 
01005                                        order_locations,
01006                                        linetable, 
01007                                        linetable_header, 
01008                                        dispersion_relation,
01009                                        response_curve, 
01010                        master_response,
01011                                        response_curve_header,
01012                                        atm_extinction,
01013                                        chip,
01014                                        DEBUG, 
01015                                        parameters,
01016                        recipe_id,
01017                                        &x2d, 
01018                                        &x2d_header, 
01019                                        &fx2d,            /* Products      */
01020                                        &background,
01021                                        &flatfielded_variance,
01022                                        &flatfielded_variance_header,
01023                                        &resampled_science,
01024                                        &resampled_mf,
01025                                        &rebinned_science, 
01026                                        &rebinned_header,
01027                                        &merged_sky,
01028                                        &merged_science, 
01029                                        &merged_header,
01030                                        &reduced_science,
01031                                        &reduced_science_error,
01032                                        &cosmic_mask,
01033                        &wave_map,
01034                                        &fluxcal_science,
01035                                        &fluxcal_error,
01036                                        &fluxcal_header,
01037                                        &info_tbl,
01038                        &extraction_slit,
01039                                        &order_trace),
01040                                    "Science reduction failed");
01041                             
01042                             if (!extract_is_2d)
01043                                 {
01044                                     uves_qclog_delete(&qclog[0]);
01045                                     qclog[0] = uves_qclog_init(raw_header[raw_index], chip);
01046                                     
01047                                     check( scired_qclog(info_tbl, 
01048                             raw_header[raw_index],
01049                             raw_image[raw_index],
01050                             extraction_slit,
01051                             qclog[0]),
01052                                            "Could not compute QC parameters");
01053                                 }
01054                             
01055                             uves_msg("Saving products...");
01056                             /* It is important to save products in the correct
01057                              * order, because users want to identify products depending on
01058                              * their number rather than the PRO-CATG (which would perhaps
01059                              * make more sense).
01060                              */
01061     
01062                             /* Save RED_SCIENCE / RED2D_SCIENCE = 
01063                                (reduced_science, merged_header) */
01064                             cpl_free(product_filename);
01065                             check( product_filename = 
01066                    (extract_is_2d) ? 
01067                                    uves_scired_red_2d_science_filename(chip) : 
01068                                    ((m_method == MERGE_NOAPPEND) ? 
01069                                    uves_scired_red_noappend_science_filename(chip): uves_scired_red_science_filename(chip)),
01070                                    "Error getting filename");   
01071 
01072                             cpl_free(product_tag);
01073                             catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
01074                             product_tag = uves_sprintf(
01075                                "RED%s%s_%s_%s",
01076                                 catg_is_noappend,
01077                 (extract_is_2d) ? "_2D" : "",
01078                 sci_type, uves_chip_tostring_upper(chip));
01079                             
01080                             
01081 
01082                             check( uves_frameset_insert(frames,
01083                                                         reduced_science,
01084                                                         CPL_FRAME_GROUP_PRODUCT,
01085                                                         CPL_FRAME_TYPE_IMAGE,
01086                                                         CPL_FRAME_LEVEL_FINAL,
01087                                                         product_filename,
01088                                                         product_tag,
01089                                                         raw_header[raw_index],
01090                                                         merged_header,
01091                                                         NULL,
01092                                                         parameters,
01093                                                         recipe_id,
01094                                                         PACKAGE "/" PACKAGE_VERSION,
01095                                                         qclog,
01096                                                         starttime,
01097                             false,    /* Do not create QC log */
01098                                                         UVES_ALL_STATS),
01099                                    "Could not add reduced science spectrum '%s' (%s) to frameset",
01100                                    product_filename, product_tag);
01101                             
01102                             uves_msg("Reduced science spectrum '%s' (%s) added to frameset",
01103                                      product_filename, product_tag);
01104                             
01105                             if (extract_is_2d)
01106                                 {
01107                                     /* Save EXT_2D_SCIENCE_xxxx = (x2d, x2d_header) */
01108                                     cpl_free(product_filename);
01109                                     check( product_filename = uves_scired_ext2d_filename(chip), 
01110                                            "Error getting filename");
01111                                     
01112                                     cpl_free(product_tag);
01113                                     product_tag = 
01114                                         uves_sprintf("EXT_2D_%s_%s", sci_type,
01115                              uves_chip_tostring_upper(chip));
01116                                     
01117                                     check( uves_frameset_insert(frames,
01118                                                                 x2d,
01119                                                                 CPL_FRAME_GROUP_PRODUCT,
01120                                                                 CPL_FRAME_TYPE_IMAGE,
01121                                                                 CPL_FRAME_LEVEL_FINAL,
01122                                                                 product_filename,
01123                                                                 product_tag,
01124                                                                 raw_header[raw_index],
01125                                                                 x2d_header,
01126                                                                 NULL,
01127                                                                 parameters,
01128                                                                 recipe_id,
01129                                                                 PACKAGE "/" PACKAGE_VERSION, 
01130                                                                 qclog,
01131                                                                 starttime, false, 
01132                                                                 UVES_ALL_STATS),
01133                                            "Could not add 2d extracted "
01134                                            "spectrum '%s' (%s) to frameset",
01135                                            product_filename, product_tag);
01136                                     
01137                                     uves_msg("2d extracted spectrum '%s' (%s) added to frameset",
01138                                              product_filename, product_tag);
01139 
01140                                 }
01141                             
01142                             /* Save MERGED_SCIENCE / MER2D_SCIENCE = 
01143                                (merged_science, merged_header) */
01144                             cpl_free(product_filename);
01145                             check( product_filename = (extract_is_2d) ? 
01146                                  uves_scired_merged_2d_science_filename(chip) :
01147                                  uves_scired_merged_science_filename(chip),
01148                                    "Error getting filename");
01149                             cpl_free(product_tag);
01150                             product_tag = uves_sprintf(
01151                 "%s_%s_%s",
01152                 (extract_is_2d) ? "MER_2D" : "MERGED",
01153                                 sci_type, uves_chip_tostring_upper(chip));
01154 
01155                             check( uves_frameset_insert(frames,
01156                                                         merged_science,
01157                                                         CPL_FRAME_GROUP_PRODUCT,
01158                                                         CPL_FRAME_TYPE_IMAGE,
01159                                                         CPL_FRAME_LEVEL_FINAL,
01160                                                         product_filename,
01161                                                         product_tag,
01162                                                         raw_header[raw_index],
01163                                                         merged_header,
01164                                                         NULL,
01165                                                         parameters,
01166                                                         recipe_id,
01167                                                         PACKAGE "/" PACKAGE_VERSION,
01168                                                         qclog,
01169                                                         starttime, false, 
01170                                                         UVES_ALL_STATS),
01171                                    "Could not add merged science spectrum '%s' (%s) to frameset",
01172                                    product_filename, product_tag);
01173                             
01174                             uves_msg("Merged science spectrum '%s' (%s) added to frameset", 
01175                                      product_filename, product_tag);
01176                             
01177                             if (!extract_is_2d)
01178                                 {
01179                                     /* Save WCALIB_SCIENCE = (resampled_science, rebinned_header) 
01180                                      * If ff_method = extract, this product was not flat-fielded
01181                                      */
01182                                     cpl_free(product_filename);
01183                                     check( product_filename = (extract_is_2d) ?
01184                                            uves_scired_resampled_2d_filename(chip) :
01185                                            uves_scired_resampled_filename(chip), 
01186                                            "Error getting filename");   
01187 
01188                                     cpl_free(product_tag);
01189                                     product_tag = 
01190                                         uves_sprintf("WCALIB_%s_%s", sci_type,
01191                              uves_chip_tostring_upper(chip));
01192                             
01193                                     check( uves_frameset_insert(frames,
01194                                                                 resampled_science,
01195                                                                 CPL_FRAME_GROUP_PRODUCT,
01196                                                                 CPL_FRAME_TYPE_IMAGE,
01197                                                                 CPL_FRAME_LEVEL_FINAL,
01198                                                                 product_filename,
01199                                                                 product_tag,
01200                                                                 raw_header[raw_index],
01201                                                                 rebinned_header,
01202                                                                 NULL,
01203                                                                 parameters,
01204                                                                 recipe_id,
01205                                                                 PACKAGE "/" PACKAGE_VERSION,
01206                                                                 qclog,
01207                                                                 starttime, false, 
01208                                                                 UVES_ALL_STATS),
01209                                            "Could not add wavelength calibrated science "
01210                                            "spectrum '%s' (%s) to frameset", product_filename, 
01211                                            product_tag);
01212                                     
01213                                     uves_msg("Wavelength calibrated science spectrum '%s' "
01214                                              "(%s) added to frameset", product_filename, 
01215                                              product_tag);
01216 
01217 
01218                                     cpl_free(product_filename);
01219                               
01220                                     check( product_filename = 
01221                                            uves_order_extract_qc_standard_filename(chip),
01222                                            "Error getting filename");   
01223 
01224                                     check( uves_frameset_insert(frames,
01225                                                                 info_tbl,
01226                                                                 CPL_FRAME_GROUP_PRODUCT,
01227                                                                 CPL_FRAME_TYPE_TABLE,
01228                                                                 CPL_FRAME_LEVEL_INTERMEDIATE,
01229                                                                 product_filename,
01230                                                                 UVES_ORDER_EXTRACT_QC(chip),
01231                                                                 raw_header[raw_index],
01232                                                                 rotated_header[raw_index],
01233                                                                 NULL,
01234                                                                 parameters,
01235                                                                 recipe_id,
01236                                                                 PACKAGE "/" PACKAGE_VERSION,
01237                                                                 NULL,
01238                                                                 starttime, true, 
01239                                                                 0),
01240                                            "Could not add extraction quality table %s (%s)"
01241                                            , product_filename, 
01242                                            UVES_ORDER_EXTRACT_QC(chip));
01243 
01244                                     uves_msg("Extraction quality table '%s' "
01245                                              "(%s) added to frameset", product_filename, 
01246                                              UVES_ORDER_EXTRACT_QC(chip));
01247 
01248 
01249                                 } /* if not 2d extracted */
01250                             
01251 
01252                             {
01253                                 const char *ff = "";
01254                                 
01255                                 /* Read uves_scired.reduce.ffmethd */
01256                 cpl_free(context);
01257                 context = uves_sprintf("%s.%s", recipe_id, UVES_REDUCE_ID);
01258                                 check( uves_get_parameter(parameters, NULL, 
01259                                                           context,
01260                                                           "ffmethod",
01261                                                           CPL_TYPE_STRING, &ff),
01262                                        "Could not read parameter");
01263                                 
01264                                 /* If flat-fielding was done */
01265                                 if (strcmp(ff, "no") != 0)
01266                                     {
01267                                         /* Save WCALIB_FF_SCIENCE / WCAL2D_SCIENCE =
01268                                            (rebinned_science, rebinned_header) */
01269                     cpl_table *qc_tabs[] = {NULL, NULL, NULL};
01270 
01271                     /* QC consists of usual science QC and
01272                        optionally TFLAT QC
01273                     */
01274                     
01275                     if ( strcmp(recipe_id, make_str(UVES_TFLAT_ID)) == 0 )
01276                         {
01277                         uves_qclog_delete(&qclog_tflat);
01278                         qclog_tflat = 
01279                             uves_qclog_init(raw_header[raw_index], chip);
01280                         
01281                         check( tflat_qclog(resampled_science,
01282                                    raw_header[raw_index],
01283                                    qclog_tflat),
01284                                "Could not compute QC parameters");
01285 
01286                         qc_tabs[0] = qclog_tflat;
01287                         qc_tabs[1] = qclog[0];
01288                         }
01289                     else
01290                         {
01291                         qc_tabs[0] = qclog[0];
01292                         qc_tabs[1] = NULL;
01293                         }
01294                     
01295                                         cpl_free(product_filename);
01296                                         check( product_filename = 
01297                                                (extract_is_2d) ? 
01298                                                uves_scired_rebinned_2d_filename(chip) :
01299                                                uves_scired_rebinned_filename(chip), 
01300                                                "Error getting filename");
01301 
01302                                         cpl_free(product_tag);
01303                                         product_tag = uves_sprintf(
01304                         "%s_%s_%s",
01305                                             (extract_is_2d) ? "WCAL_2D" : "WCALIB_FF",
01306                                             sci_type, uves_chip_tostring_upper(chip));
01307                                         
01308                                         check( uves_frameset_insert(frames,
01309                                                                     rebinned_science,
01310                                                                     CPL_FRAME_GROUP_PRODUCT,
01311                                                                     CPL_FRAME_TYPE_IMAGE,
01312                                                                     CPL_FRAME_LEVEL_FINAL,
01313                                                                     product_filename,
01314                                                                     product_tag,
01315                                                                     raw_header[raw_index],
01316                                                                     rebinned_header,
01317                                                                     NULL,
01318                                                                     parameters,
01319                                                                     recipe_id,
01320                                                                     PACKAGE "/" 
01321                                                                     PACKAGE_VERSION,
01322                                                                     qc_tabs,
01323                                                                     starttime, true,
01324                                                                     UVES_ALL_STATS),
01325                                                "Could not add wavelength calibrated flat-fielded "
01326                                                "science spectrum '%s' (%s) to frameset", 
01327                                                product_filename, product_tag);
01328                                     
01329                                         uves_msg("Wavelength calibrated flat-fielded science "
01330                                                  "spectrum '%s' (%s) added to frameset",
01331                                                  product_filename, product_tag);
01332                                         
01333                                         if (!extract_is_2d)
01334                                             {
01335                                                 /* Save WCALIB_FLAT_OBJ_xxxx =
01336                                                    (resampled_mf, rebinned_header) */
01337                                                 cpl_free(product_filename);
01338                                                 check( product_filename = 
01339                                                        uves_scired_resampledmf_filename(chip),
01340                                                        "Error getting filename");
01341                                                 
01342                                                 cpl_free(product_tag);
01343                                                 product_tag = 
01344                                                     uves_sprintf(
01345                                                         "WCALIB_FLAT_OBJ_%s",
01346                                                         uves_chip_tostring_upper(chip));
01347                                                 /* Independent of sci_type */
01348 
01349 
01350                                                 /* !!!Exception!!!
01351                                                  *
01352                                                  * For this reduced master flat frame we
01353                                                  * want to propagate the keywords *not*
01354                                                  * from the first raw input frame but
01355                                                  * from the master flat field itself.
01356                                                  *
01357                                                  * For that to work we temporarily set
01358                                                  * 
01359                                                  * all raw frames    := NONE
01360                                                  * master.flat frame := RAW
01361                                                  *
01362                                                  * This will make cpl_dfs_setup_product_header()
01363                                                  * find the proper "raw" frame (i.e. the mf)
01364                                                  * Also the required 'raw_header' must be
01365                                                  * that of the master flat frame, not science.
01366                                                  * After propagating keywords, we change back
01367                                                  * to normal:
01368                                                  *
01369                                                  * all raw frames    := RAW
01370                                                  * master.flat frame := CALIB
01371                                                  *
01372                                                  * (Since there could be more than 1 raw frame,
01373                                                  *  simply changing the first raw frame would
01374                                                  *  not work)
01375                                                  */                             
01376                                                 
01377                                                 cpl_free(raw_frames);
01378                                                 check_nomsg( raw_frames =
01379                                                              set_all_raw_none(frames) );
01380 
01381                                                 cpl_frame_set_group(mflat_frame, 
01382                                                                     CPL_FRAME_GROUP_RAW);
01383                                                 
01384                                                 check( uves_frameset_insert(
01385                                                            frames,
01386                                                            resampled_mf,
01387                                                            CPL_FRAME_GROUP_PRODUCT,
01388                                                            CPL_FRAME_TYPE_IMAGE,
01389                                                            CPL_FRAME_LEVEL_FINAL,
01390                                                            product_filename,
01391                                                            product_tag,
01392                                                            master_flat_header,  /* Note! */
01393                                                            rebinned_header,
01394                                                            NULL,
01395                                                            parameters,
01396                                                            recipe_id,
01397                                                            PACKAGE "/" 
01398                                                            PACKAGE_VERSION,
01399                                                            NULL, /* No QC: qclog */
01400                                                            starttime, false, 
01401                                                            CPL_STATS_MIN | CPL_STATS_MAX),
01402                                                        "Could not add wavelength calibrated "
01403                                                        "flat-field '%s' (%s) to frameset", 
01404                                                        product_filename, product_tag);
01405                                                 
01406                                                 uves_msg("Wavelength calibrated flat-field "
01407                                                          "spectrum '%s' (%s) added to frameset", 
01408                                                          product_filename, product_tag);
01409 
01410                                                 /* Change frames groups back to normal */
01411                                                 {
01412                                                     int i;
01413                                                     for (i = 0;
01414                                                          raw_frames[i] != NULL;
01415                                                          i++)
01416                                                         {
01417                                                             cpl_frame_set_group(
01418                                                                 raw_frames[i], 
01419                                                                 CPL_FRAME_GROUP_RAW);
01420                                                         }
01421                                                 }
01422                                                 cpl_frame_set_group(mflat_frame, 
01423                                                                     CPL_FRAME_GROUP_CALIB);
01424                                             }
01425 
01426                                         if (extract_is_2d)
01427                                             {
01428                                                 /* Save FF2D_SCIENCE_xxxx = (fx2d, x2d_header) */
01429                                                 cpl_free(product_filename);
01430                                                 check( product_filename = 
01431                                                        uves_scired_ff2d_filename(chip), 
01432                                                        "Error getting filename");
01433 
01434                                                 cpl_free(product_tag);
01435                                                 product_tag = 
01436                                                     uves_sprintf(
01437                             "FF_2D_%s_%s", sci_type,
01438                                                         uves_chip_tostring_upper(chip));
01439                                                 
01440                                                 check( uves_frameset_insert(
01441                                                            frames,
01442                                                            fx2d,
01443                                                            CPL_FRAME_GROUP_PRODUCT,
01444                                                            CPL_FRAME_TYPE_IMAGE,
01445                                                            CPL_FRAME_LEVEL_FINAL,
01446                                                            product_filename,
01447                                                            product_tag,
01448                                                            raw_header[raw_index],
01449                                                            x2d_header,
01450                                                            NULL,
01451                                                            parameters,
01452                                                            recipe_id,
01453                                                            PACKAGE "/" 
01454                                                            PACKAGE_VERSION,
01455                                                            qclog,
01456                                                            starttime, false, 
01457                                                            UVES_ALL_STATS),
01458                                                        "Could not add 2d extracted, flat-fielded "
01459                                                        "spectrum '%s' (%s) to frameset", 
01460                                                        product_filename, product_tag);
01461                                                 
01462                                                 uves_msg("2d extracted, flat-fielded spectrum "
01463                                                          "'%s' (%s) added to frameset", 
01464                                                          product_filename, product_tag);
01465 
01466                                             }
01467                                         
01468                                     }/* If flat-fielding != no */
01469 
01470                                     check( uves_pfits_set_bunit(merged_header, "REL. FLUX."),
01471                                            "Error writing error spectrum header");
01472                                     
01473                                     /* Save ERRORBAR_SCIENCE_xxxx =
01474                                        (reduced_science_error, merged_header) */
01475                                     cpl_free(product_filename);
01476 
01477                                    check( product_filename = 
01478                    (extract_is_2d) ? 
01479                                    uves_scired_red_2d_error_filename(chip) : 
01480                                    ((m_method == MERGE_NOAPPEND) ? 
01481                                     uves_scired_red_noappend_error_filename(chip): uves_scired_red_error_filename(chip)),
01482                                    "Error getting filename");   
01483 
01484 
01485                                     cpl_free(product_tag);
01486                                     catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
01487                                     product_tag = uves_sprintf("%s%s_%s_%s",
01488                                                                (extract_is_2d) ? "ERR_2D" : "ERRORBAR",catg_is_noappend,
01489                                             sci_type, uves_chip_tostring_upper(chip));
01490 
01491 /*
01492                                     product_tag = uves_sprintf(
01493                                        "%s%s_%s_%s",
01494                                        (m_method == MERGE_NOAPPEND) ? "ERRORBAR_NONMERGED" : "ERRORBAR",
01495                                        (extract_is_2d) ? "_2D" : "",
01496                                        sci_type, uves_chip_tostring_upper(chip));
01497 
01498 */
01499                                     check( uves_frameset_insert(
01500                                                frames,
01501                                                reduced_science_error,
01502                                                CPL_FRAME_GROUP_PRODUCT,
01503                                                CPL_FRAME_TYPE_IMAGE,
01504                                                CPL_FRAME_LEVEL_FINAL,
01505                                                product_filename,
01506                                                product_tag,
01507                                                raw_header[raw_index],
01508                                                merged_header,
01509                                                NULL,
01510                                                parameters,
01511                                                recipe_id,
01512                                                PACKAGE "/" PACKAGE_VERSION,
01513                                                qclog,
01514                                                starttime, false, 
01515                                                CPL_STATS_MIN | CPL_STATS_MAX),
01516                                            "Could not add error bars '%s' (%s) to frameset", 
01517                                            product_filename, product_tag);
01518                                     
01519                                     uves_msg("Science spectrum error '%s' (%s) "
01520                                              "added to frameset", 
01521                                              product_filename, product_tag);
01522                                     
01523 
01524                             if (!extract_is_2d)
01525                                 {
01526 
01527 
01528                                     /* Save VARIANCE_SCIENCE_xxxx = 
01529                                        (flatfielded_variance, flatfielded_variance_header) */
01530                                     cpl_free(product_filename);
01531                                     check( product_filename = 
01532                                            uves_scired_ff_variance_filename(chip),
01533                                            "Error getting filename");
01534                                     
01535                                     cpl_free(product_tag);
01536                                     product_tag = 
01537                                         uves_sprintf("VARIANCE_%s_%s", sci_type,
01538                              uves_chip_tostring_upper(chip));
01539                                     
01540 
01541                                     check( uves_frameset_insert(frames,
01542                                                                 flatfielded_variance,
01543                                                                 CPL_FRAME_GROUP_PRODUCT,
01544                                                                 CPL_FRAME_TYPE_IMAGE,
01545                                                                 CPL_FRAME_LEVEL_FINAL,
01546                                                                 product_filename,
01547                                                                 product_tag,
01548                                                                 raw_header[raw_index],
01549                                                                 flatfielded_variance_header,
01550                                                                 NULL,
01551                                                                 parameters,
01552                                                                 recipe_id,
01553                                                                 PACKAGE "/" PACKAGE_VERSION,
01554                                                                 qclog,
01555                                                                 starttime, false, 
01556                                                                 CPL_STATS_MIN | CPL_STATS_MAX),
01557                                            "Could not add flat-fielded spectrum variance "
01558                                            "'%s' (%s) to frameset", 
01559                                            product_filename, product_tag);
01560                                     
01561                                     uves_msg("Flat-fielded spectrum variance '%s' (%s) "
01562                                              "added to frameset", 
01563                                              product_filename, product_tag);
01564                                     
01565                                 } /* if not 2d extraction */
01566                             }
01567 
01568                             if (!extract_is_2d)
01569                                 {
01570                                     /* Save BKG_SCI_xxxx = (background, rotated_header) */
01571                                     cpl_free(product_filename);
01572                                     check( product_filename = 
01573                                            uves_scired_background_filename(chip),
01574                                            "Error getting filename");   
01575 
01576                                     cpl_free(product_tag);
01577                                     product_tag = 
01578                                         uves_sprintf("BKG_SCI_%s",
01579                              uves_chip_tostring_upper(chip));
01580                                     
01581                                     check( uves_frameset_insert(frames,
01582                                                                 background,
01583                                                                 CPL_FRAME_GROUP_PRODUCT,
01584                                                                 CPL_FRAME_TYPE_IMAGE,
01585                                                                 CPL_FRAME_LEVEL_FINAL,
01586                                                                 product_filename,
01587                                                                 product_tag,
01588                                                                 raw_header[raw_index],
01589                                                                 rotated_header[raw_index],
01590                                                                 NULL,
01591                                                                 parameters,
01592                                                                 recipe_id,
01593                                                                 PACKAGE "/" PACKAGE_VERSION,
01594                                                                 NULL, /* QC */
01595                                                                 starttime, false, 
01596                                                                 CPL_STATS_MIN | CPL_STATS_MAX),
01597                                            "Could not add background image '%s' (%s) "
01598                                            "to frameset", product_filename, product_tag);
01599                                     
01600                                     uves_msg("Background image '%s' (%s) added to frameset", 
01601                                              product_filename, product_tag);
01602                                     
01603                                     /* If optimal extraction, also save
01604                                        cosmic_mask, order_trace */
01605                                     if (order_trace != NULL)
01606                                         {
01607                                             /* Save ORDER_TRACE_xxxx  */
01608                                             uves_free_propertylist(&order_trace_header);
01609                                             order_trace_header = uves_propertylist_new();
01610 
01611 
01612                                             /* !WARNING!: Duplicate code follows, be careful if/when
01613                                                changing. These parameters should be calculated
01614                                                the same way as in uves_qclog_add_sci().
01615 
01616                                                The MIDAS pipeline wrote these parameters only
01617                                                in this product, and for backwards compatibility
01618                                                do the same here.
01619                                             */
01620 
01621                                             uves_propertylist_update_int(
01622                                                 order_trace_header, "ESO QC OPTEX NORD",
01623                                                 uves_round_double(
01624                                                     cpl_table_get_column_max(ordertable, "Order")-
01625                                                     cpl_table_get_column_min(ordertable, "Order")+1));
01626 
01627                                             uves_propertylist_update_int(
01628                                                 order_trace_header, "ESO QC OPTEX XSIZE",
01629                                                 cpl_image_get_size_x(raw_image[raw_index]));
01630 
01631                                             uves_propertylist_update_int(
01632                                                 order_trace_header, "ESO QC OPTEX YSIZE",
01633                                                 uves_round_double(extraction_slit));
01634 
01635 
01636                                             cpl_free(product_filename);
01637                                             check( product_filename =
01638                                                    uves_scired_ordertrace_filename(chip),
01639                                                    "Error getting filename");
01640 
01641                                             cpl_free(product_tag);
01642                                             product_tag =
01643                                                 uves_sprintf("ORDER_TRACE_%s",
01644                                                              uves_chip_tostring_upper(chip));
01645                                              
01646                                             check( uves_frameset_insert(frames,
01647                                                                         order_trace,
01648                                                                         CPL_FRAME_GROUP_PRODUCT,
01649                                                                         CPL_FRAME_TYPE_TABLE,
01650                                                                         CPL_FRAME_LEVEL_FINAL,
01651                                                                         product_filename,
01652                                                                         product_tag,
01653                                                                         raw_header[raw_index],
01654                                                                         order_trace_header,
01655                                                                         NULL,
01656                                                                         parameters,
01657                                                                         recipe_id,
01658                                                                         PACKAGE "/"
01659                                                                         PACKAGE_VERSION,
01660                                                                         qclog,
01661                                                                         starttime, false,
01662                                                                         0),
01663                                                    "Could not add sky spectrum '%s' (%s) "
01664                                                    "to frameset",
01665                                                    product_filename, product_tag);
01666 
01667                                             uves_msg("Order trace table '%s' (%s) "
01668                                                      "added to frameset",
01669                                                      product_filename, product_tag);
01670                                         }
01671 
01672 
01673 
01674                                     if (cosmic_mask != NULL)
01675                                         {
01676                                             /* Save CRMASK_xxxx  */
01677                                             uves_free_propertylist(&cosmic_mask_header);
01678                                             cosmic_mask_header = uves_propertylist_new();
01679                                             
01680                                             cpl_free(product_filename);
01681                                             check( product_filename = 
01682                                                    uves_scired_crmask_filename(chip), 
01683                                                    "Error getting filename");
01684 
01685                                             cpl_free(product_tag);
01686                                             product_tag = 
01687                                                 uves_sprintf("CRMASK_%s",
01688                                  uves_chip_tostring_upper(chip));
01689                                             
01690                                             check( uves_frameset_insert(frames,
01691                                                                         cosmic_mask,
01692                                                                         CPL_FRAME_GROUP_PRODUCT,
01693                                                                         CPL_FRAME_TYPE_TABLE,
01694                                                                         CPL_FRAME_LEVEL_FINAL,
01695                                                                         product_filename,
01696                                                                         product_tag,
01697                                                                         raw_header[raw_index],
01698                                                                         cosmic_mask_header,
01699                                                                         NULL,
01700                                                                         parameters,
01701                                                                         recipe_id,
01702                                                                         PACKAGE "/" 
01703                                                                         PACKAGE_VERSION,
01704                                                                         NULL, /* qc */
01705                                                                         starttime, false,
01706                                                                         0),
01707                                                    "Could not add cosmic ray table "
01708                                                    "'%s' (%s) to frameset", 
01709                                                    product_filename, product_tag);
01710                                             
01711                                             uves_msg("Cosmic ray table '%s' (%s) "
01712                                                      "added to frameset",
01713                                                      product_filename, product_tag);
01714                                         }
01715 
01716 
01717 
01718 
01719 
01720 
01721 
01722 
01723                                     if (wave_map != NULL)
01724                                         {
01725                                             /* Save WAVE_MAP_xxxx  */
01726                                             uves_free_propertylist(&wave_map_header);
01727                                             wave_map_header = uves_propertylist_new();
01728                                             
01729                                             cpl_free(product_filename);
01730                                             check( product_filename = 
01731                                                    uves_scired_wmap_filename(chip), 
01732                                                    "Error getting filename");
01733 
01734                                             cpl_free(product_tag);
01735                                             product_tag = 
01736                                                 uves_sprintf("WAVE_MAP_%s",
01737                                  uves_chip_tostring_upper(chip));
01738                                             
01739                                             check( uves_frameset_insert(frames,
01740                                                                         wave_map,
01741                                                                         CPL_FRAME_GROUP_PRODUCT,
01742                                                                         CPL_FRAME_TYPE_IMAGE,
01743                                                                         CPL_FRAME_LEVEL_FINAL,
01744                                                                         product_filename,
01745                                                                         product_tag,
01746                                                                         raw_header[raw_index],
01747                                                                         wave_map_header,
01748                                                                         NULL,
01749                                                                         parameters,
01750                                                                         recipe_id,
01751                                                                         PACKAGE "/" 
01752                                                                         PACKAGE_VERSION,
01753                                                                         NULL, /* qc */
01754                                                                         starttime, false,
01755                                                                         0),
01756                                                    "Could not add wave map "
01757                                                    "'%s' (%s) to frameset", 
01758                                                    product_filename, product_tag);
01759                                             
01760                                             uves_msg("Wave map '%s' (%s) "
01761                                                      "added to frameset",
01762                                                      product_filename, product_tag);
01763                                         } else {
01764                       uves_msg("no wave map!!!!!!!!!");
01765                     }
01766                             uves_free_image(&wave_map);
01767 
01768 
01769                                     if (merged_sky != NULL)     
01770                                         /* In slicer mode / 2d mode, no sky 
01771                                            spectrum is extracted */
01772                                         {
01773                                             /* Save MERGED_SKY_xxxx = 
01774                                                (merged_sky, merged_header) */
01775                                             cpl_free(product_filename);
01776                                             check( product_filename = 
01777                                                    uves_scired_merged_sky_filename(chip),
01778                                                    "Error getting filename");
01779                                             
01780                                             cpl_free(product_tag);
01781                                             product_tag = 
01782                                                 uves_sprintf("MERGED_SKY_%s",
01783                                  uves_chip_tostring_upper(chip));
01784 
01785                                             check( uves_frameset_insert(
01786                                frames,
01787                                merged_sky,
01788                                CPL_FRAME_GROUP_PRODUCT,
01789                                CPL_FRAME_TYPE_IMAGE,
01790                                CPL_FRAME_LEVEL_FINAL,
01791                                product_filename,
01792                                product_tag,
01793                                raw_header[raw_index],
01794                                merged_header,
01795                                NULL,
01796                                parameters,
01797                                recipe_id,
01798                                PACKAGE "/" 
01799                                PACKAGE_VERSION,
01800                                NULL, /* QC */
01801                                starttime, false, 
01802                                CPL_STATS_MIN | CPL_STATS_MAX),
01803                                                    "Could not add sky spectrum "
01804                                                    "'%s' (%s) to frameset", 
01805                                                    product_filename, product_tag);
01806                                             
01807                                             uves_msg("Sky spectrum '%s' (%s) added to frameset", 
01808                                                      product_filename, product_tag);
01809                                         }
01810                                     else
01811                                         {
01812                                             uves_msg_low("No sky spectrum to save");
01813                                         }
01814                                     
01815                                 }/* if extract is 2d */
01816 
01817                             if (fluxcal_science != NULL)
01818                                 {
01819                                     /* Save FLUXCAL_SCIENCE = 
01820                                        (fluxcal_science, fluxcal_header) */
01821                                     cpl_free(product_filename);
01822 
01823                                    check( product_filename = 
01824                    (extract_is_2d) ? 
01825                                    uves_scired_fluxcal_science_2d_filename(chip) : 
01826                                    ((m_method == MERGE_NOAPPEND) ? 
01827                                     uves_scired_fluxcal_science_noappend_filename(chip): uves_scired_fluxcal_science_filename(chip)),
01828                                    "Error getting filename");   
01829 
01830 
01831                                     cpl_free(product_tag);
01832 
01833 
01834 
01835                                     catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
01836                                     product_tag = uves_sprintf("FLUXCAL%s%s_%s_%s",
01837                                                                (extract_is_2d) ? "_2D" : "",catg_is_noappend,
01838                                             sci_type, uves_chip_tostring_upper(chip));
01839 
01840 
01841                                     /* Always _SCIENCE_, independent of sci_type */
01842 
01843                                     check( uves_frameset_insert(frames,
01844                                                                 fluxcal_science,
01845                                                                 CPL_FRAME_GROUP_PRODUCT,
01846                                                                 CPL_FRAME_TYPE_IMAGE,
01847                                                                 CPL_FRAME_LEVEL_FINAL,
01848                                                                 product_filename,
01849                                                                 product_tag,
01850                                                                 raw_header[raw_index],
01851                                                                 fluxcal_header,
01852                                                                 NULL,
01853                                                                 parameters,
01854                                                                 recipe_id,
01855                                                                 PACKAGE "/"
01856                                                                 PACKAGE_VERSION,
01857                                                                 qclog,
01858                                                                 starttime, false,
01859                                                                 CPL_STATS_MIN | CPL_STATS_MAX),
01860                                            "Could not add flux-calibrated science "
01861                                            "spectrum '%s' (%s) to frameset", 
01862                                            product_filename, product_tag);
01863 
01864                                     uves_msg("Flux-calibrated science spectrum "
01865                                              "'%s' (%s) added to frameset", 
01866                                              product_filename, product_tag);
01867 
01868                                     /* Save FLUXCAL_ERRORBAR = (fluxcal_error, fluxcal_header) */
01869                                     check( uves_pfits_set_bunit(fluxcal_header, 
01870                                                                 "ERROR (10^-16 erg/cm^2/A/sec)"),
01871                                            "Error writing error spectrum header");
01872 
01873                                     cpl_free(product_filename);
01874 
01875                                    check( product_filename = 
01876                    (extract_is_2d) ? 
01877                                    uves_scired_fluxcal_error_2d_filename(chip) : 
01878                                    ((m_method == MERGE_NOAPPEND) ? 
01879                                     uves_scired_fluxcal_error_noappend_filename(chip): uves_scired_fluxcal_error_filename(chip)),
01880                                    "Error getting filename");   
01881 
01882 
01883                                     cpl_free(product_tag);
01884 
01885 
01886                                     catg_is_noappend=uves_get_pro_catg_special(extract_is_2d,m_method);
01887                                     product_tag = uves_sprintf("FLUXCAL_ERRORBAR%s%s_%s_%s",
01888                                                                (extract_is_2d) ? "_2D" : "",catg_is_noappend,
01889                                             sci_type, uves_chip_tostring_upper(chip));
01890 
01891                                     check( uves_frameset_insert(frames,
01892                                                                 fluxcal_error,
01893                                                                 CPL_FRAME_GROUP_PRODUCT,
01894                                                                 CPL_FRAME_TYPE_IMAGE,
01895                                                                 CPL_FRAME_LEVEL_FINAL,
01896                                                                 product_filename,
01897                                                                 product_tag,
01898                                                                 raw_header[raw_index],
01899                                                                 fluxcal_header,
01900                                                                 NULL,
01901                                                                 parameters,
01902                                                                 recipe_id,
01903                                                                 PACKAGE "/" 
01904                                                                 PACKAGE_VERSION,
01905                                                                 qclog,
01906                                                                 starttime, false,
01907                                                                 CPL_STATS_MIN | CPL_STATS_MAX),
01908                                            "Could not add flux-calibrated science "
01909                                            "spectrum error '%s' (%s) to frameset",
01910                                            product_filename, product_tag);
01911                                     
01912                                     uves_msg("Flux-calibrated science spectrum error "
01913                                              "'%s' (%s) added to frameset", 
01914                                              product_filename, product_tag);
01915                                     
01916                                 } /* If flux calibration done */
01917                             
01918                         }/* if trace is enabled */
01919                     else
01920                         {
01921                             uves_msg("Skipping trace number %d", trace_number);
01922                         }
01923                 
01924 
01925                 }/* for each trace */
01926 
01927 
01928       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
01929     chip = uves_chip_get_next(chip);
01930       }
01931 
01932         
01933         }/* For each chip */
01934        
01935   cleanup:   
01936     /* Input */
01937     uves_free_table(&info_tbl);
01938     uves_free_image(&raw_image[0]);
01939     uves_free_image(&raw_image[1]);
01940     uves_free_propertylist(&raw_header[0]);
01941     uves_free_propertylist(&raw_header[1]);
01942     uves_free_propertylist(&rotated_header[0]);
01943     uves_free_propertylist(&rotated_header[1]);
01944     uves_free_propertylist(&wave_map_header);
01945     
01946     /* Input, calib */
01947     uves_free_image(&master_bias);
01948     uves_free_propertylist(&master_bias_header);
01949     
01950     uves_free_image(&master_dark);
01951     uves_free_propertylist(&master_dark_header);
01952 
01953     uves_free_image(&master_flat);
01954     uves_free_propertylist(&master_flat_header);
01955     
01956     uves_free_table(&ordertable);
01957     uves_free_propertylist(&ordertable_header);
01958     uves_polynomial_delete(&order_locations);
01959     uves_free_table(&traces);
01960     
01961     uves_free_table_const( &(linetable[0]) );
01962     uves_free_table_const( &(linetable[1]) );
01963     uves_free_table_const( &(linetable[2]) );
01964     uves_free_propertylist_const( &(linetable_header[0]) );
01965     uves_free_propertylist_const( &(linetable_header[1]) );
01966     uves_free_propertylist_const( &(linetable_header[2]) );
01967     uves_polynomial_delete_const( &(dispersion_relation[0]) );
01968     uves_polynomial_delete_const( &(dispersion_relation[1]) );
01969     uves_polynomial_delete_const( &(dispersion_relation[2]) );
01970 
01971     uves_free_image(&response_curve);
01972     uves_free_propertylist(&response_curve_header);
01973     uves_free_table(&master_response);
01974     
01975     uves_free_table(&atm_extinction);
01976 
01977     /* Output */
01978     uves_qclog_delete(&qclog[0]);
01979     uves_qclog_delete(&qclog_tflat);
01980     uves_free_image(&background);
01981     uves_free_image(&flatfielded_variance);
01982     uves_free_propertylist(&flatfielded_variance_header);
01983     uves_free_image(&rebinned_science);
01984     uves_free_propertylist(&rebinned_header);
01985     uves_free_image(&resampled_science);
01986     uves_free_image(&resampled_mf);
01987     uves_free_image(&merged_sky);
01988 
01989     uves_free_image(&merged_science);
01990     uves_free_propertylist(&merged_header);
01991     uves_free_image(&reduced_science);
01992     uves_free_image(&reduced_science_error);
01993     uves_free_image(&fluxcal_science);
01994     uves_free_image(&fluxcal_error);
01995     uves_free_propertylist(&fluxcal_header);
01996     uves_free_table(&cosmic_mask);
01997     uves_free_propertylist(&cosmic_mask_header);
01998 
01999     uves_free_table(&order_trace);
02000     uves_free_propertylist(&order_trace_header);
02001 
02002     uves_free_image(&x2d);
02003     uves_free_image(&fx2d);
02004     uves_free_propertylist(&x2d_header);
02005     
02006     cpl_free(raw_frames);
02007     cpl_free(product_filename);
02008     cpl_free(context);
02009     cpl_free(product_tag);
02010     
02011     return;
02012 }
02013 
02014 /*----------------------------------------------------------------------------*/
02023 /*----------------------------------------------------------------------------*/
02024 static void
02025 scired_qclog(const cpl_table* info_tbl, 
02026          const uves_propertylist *raw_header,
02027          const cpl_image *raw_image,
02028          double slit,
02029          cpl_table* qclog)
02030 {
02031   /* This test does not exist as an official QC-TEST in the MIDAS pipeline. But
02032      the QC parameters are written to the product header */
02033 
02034   check_nomsg(uves_qclog_add_string(qclog,                
02035                                     "QC TEST1 ID",
02036                                     "Science-Reduction-Test-Results",
02037                                     "Name of QC test",
02038                                     "%s"));
02039   
02040   check_nomsg( uves_qclog_add_sci(qclog,
02041                   raw_header, 
02042                   raw_image,
02043                   slit,
02044                   info_tbl) );
02045 
02046   cleanup:
02047   return;
02048 
02049 }
02050 
02051 /*----------------------------------------------------------------------------*/
02058 /*----------------------------------------------------------------------------*/
02059 static void
02060 tflat_qclog(const cpl_image* ima,
02061         const uves_propertylist *raw_header,
02062         cpl_table* qclog)
02063 {
02064   char key_name[80];
02065   cpl_image *window = NULL;
02066 
02067   double exptime;
02068   int nx;
02069   int ny;
02070   int i;
02071   
02072   check_nomsg(uves_qclog_add_string(qclog,
02073                     "QC TEST1 ID",
02074                     "TFLAT-QC",
02075                     "Name of QC test",
02076                     "%s"));
02077   
02078 
02079   check_nomsg(uves_qclog_add_string(qclog,
02080                     uves_remove_string_prefix(UVES_INSMODE, "ESO "),
02081                     uves_pfits_get_insmode(raw_header),
02082                     "Instrument mode used.",
02083                     "%s"));
02084   
02085   check_nomsg(uves_qclog_add_string(qclog,
02086                     uves_remove_string_prefix(UVES_INSPATH, "ESO "),
02087                     uves_pfits_get_inspath(raw_header),
02088                     "Optical path used.",
02089                     "%s"));
02090 
02091     check_nomsg(uves_qclog_add_string(qclog,
02092                     uves_remove_string_prefix(UVES_SLIT1NAME, "ESO "),
02093                     uves_pfits_get_slit1_name(raw_header),
02094                     "Slit common name.",
02095                     "%s"));
02096 
02097   check( exptime = uves_pfits_get_exptime(raw_header),
02098      "Error reading exposure time");
02099   
02100   nx = cpl_image_get_size_x(ima);
02101   ny = cpl_image_get_size_y(ima);
02102 
02103   for (i = 1; i <= ny; i++) 
02104       /* Always count order numbers from 1, like MIDAS */
02105       {
02106       int size = 100;
02107       int xlo = uves_max_int(1 , (nx+1)/2 - size);
02108       int xhi = uves_min_int(nx, (nx+1)/2 + size);
02109           
02110       double min, max, avg, rms, med;
02111 
02112       uves_free_image(&window);
02113       window = cpl_image_extract(ima, xlo, i, xhi, i);
02114       assure_mem( window );
02115 
02116       if (cpl_image_count_rejected(window) >= cpl_image_get_size_x(window) - 2)
02117           {
02118           min = max = avg = rms = med = 0;
02119           }
02120       else
02121           {
02122           min = cpl_image_get_min   (window) / exptime;
02123           max = cpl_image_get_max   (window) / exptime;
02124           avg = cpl_image_get_mean  (window) / exptime;
02125           rms = cpl_image_get_stdev (window) / exptime;
02126           med = cpl_image_get_median(window) / exptime;
02127           }
02128                        
02129       sprintf(key_name, "QC ORD%d DATAMIN", i);
02130       check_nomsg(uves_qclog_add_double(qclog,
02131                         key_name,
02132                         min,
02133                         "extracted order datamin",
02134                         "%f"));
02135                   
02136       sprintf(key_name, "QC ORD%d DATAMAX", i);
02137       check_nomsg(uves_qclog_add_double(qclog,
02138                         key_name,
02139                         max,
02140                         "extracted order datamax",
02141                         "%f"));
02142                   
02143       sprintf(key_name, "QC ORD%d DATAAVG", i);
02144       check_nomsg(uves_qclog_add_double(qclog,
02145                         key_name,
02146                         avg,
02147                         "extracted order datamean",
02148                         "%f"));
02149                   
02150       sprintf(key_name, "QC ORD%d DATARMS", i);
02151       check_nomsg(uves_qclog_add_double(qclog,
02152                         key_name,
02153                         rms,
02154                         "extracted order datarms",
02155                         "%f"));
02156 
02157       sprintf(key_name, "QC ORD%d DATAMED", i);
02158       check_nomsg(uves_qclog_add_double(qclog,
02159                         key_name,
02160                         med,
02161                         "extracted order datamed",
02162                         "%f"));
02163       }
02164   
02165   cleanup:
02166   uves_free_image(&window);
02167   return;
02168 
02169 }
02170 

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1