uves_physmod_body.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/09/27 08:00:41 $
00023  * $Revision: 1.57 $
00024  * $Name: uves-4_9_1 $
00025  * $Log: uves_physmod_body.c,v $
00026  * Revision 1.57  2010/09/27 08:00:41  amodigli
00027  * removed x_axis_scale and y_axis_scal params from interface as always kept constant
00028  *
00029  * Revision 1.56  2010/09/24 09:32:06  amodigli
00030  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00031  *
00032  * Revision 1.54  2010/08/30 14:17:20  amodigli
00033  * moved declaration product_filename variable up to eventually used to dump an extra QC product
00034  *
00035  * Revision 1.53  2010/06/15 15:46:26  amodigli
00036  * removed useless second call to uves_save_table_local of the same temporary product already saved
00037  *
00038  * Revision 1.52  2010/05/06 14:21:06  amodigli
00039  * increase mbox_x/y min to 10 to improve recipe robustness
00040  *
00041  * Revision 1.51  2010/05/01 16:01:27  amodigli
00042  * clearer error message
00043  *
00044  * Revision 1.50  2010/03/09 15:27:49  amodigli
00045  * removed generation of tmp product 'pippo4.fits'
00046  *
00047  * Revision 1.49  2010/03/01 18:17:22  amodigli
00048  * now model_prediction prediction table is generated only in debug mode
00049  *
00050  * Revision 1.48  2009/08/03 12:48:10  amodigli
00051  * some messaging to monitor physmodel shifts
00052  *
00053  * Revision 1.47  2009/07/13 07:56:21  amodigli
00054  * insert proper offsets for new CCD  echelle standard modes
00055  *
00056  * Revision 1.46  2009/07/13 06:38:41  amodigli
00057  * moved functions to get new CCD shifts in uves_physmod_create_table.c
00058  *
00059  * Revision 1.45  2009/04/27 08:32:59  amodigli
00060  * fixed trans_x/y setting in FIBER mode
00061  *
00062  * Revision 1.44  2009/04/14 07:02:07  amodigli
00063  * set explicit filename to distingush if physmod is executed on a normal formatcheck or on a reference one
00064  *
00065  * Revision 1.43  2008/09/29 06:58:03  amodigli
00066  * add #include <string.h>
00067  *
00068  * Revision 1.42  2008/05/01 09:57:03  amodigli
00069  * fixed compiler warnings
00070  *
00071  * Revision 1.41  2008/05/01 09:46:06  amodigli
00072  * modify x-y shift (if flames) if trans_x/y are set
00073  *
00074  * Revision 1.40  2008/02/15 12:43:49  amodigli
00075  * allow lower/upper chip for parameter process_chip
00076  *
00077  * Revision 1.39  2007/12/05 14:12:43  amodigli
00078  * if stability_cnt=0 does not do certain steps as in MIDAS
00079  *
00080  * Revision 1.38  2007/12/03 17:00:14  amodigli
00081  * removed dependency from flames and fixed a warning
00082  *
00083  * Revision 1.37  2007/12/03 10:43:00  amodigli
00084  * added flames_get_physmod_shift and set itermax to 1 to fix problems on flames-uves
00085  *
00086  * Revision 1.36  2007/10/23 07:37:43  amodigli
00087  * if provided subtract MASTER_BIAS from input raw and reference formatchecks
00088  *
00089  * Revision 1.35  2007/10/05 16:01:45  amodigli
00090  * using proces_chip parameter to process or not a given RED chip
00091  *
00092  * Revision 1.34  2007/08/21 13:08:26  jmlarsen
00093  * Removed irplib_access module, largely deprecated by CPL-4
00094  *
00095  * Revision 1.33  2007/08/10 15:24:11  amodigli
00096  * fixed seg fault with flames-uves
00097  *
00098  * Revision 1.32  2007/06/26 15:08:17  amodigli
00099  * changes to make work flames and uves physmod in simple way
00100  *
00101  * Revision 1.31  2007/06/22 09:30:22  jmlarsen
00102  * Changed interface of uves_save_image
00103  *
00104  * Revision 1.30  2007/06/13 15:13:48  amodigli
00105  * fixed mem leaks in case of fiber mode data
00106  *
00107  * Revision 1.29  2007/06/06 08:17:33  amodigli
00108  * replace tab with 4 spaces
00109  *
00110  * Revision 1.28  2007/05/22 14:34:32  jmlarsen
00111  * Removed unnecessary includes
00112  *
00113  * Revision 1.27  2007/05/14 09:38:38  amodigli
00114  * removed call to irplib_error_dump
00115  *
00116  * Revision 1.26  2007/04/25 08:38:40  amodigli
00117  * chnaged interface uves_physmod_plotmod
00118  *
00119  * Revision 1.25  2007/04/24 14:08:53  jmlarsen
00120  * Log more QC parameters (DFS03805)
00121  *
00122  * Revision 1.24  2007/04/24 12:50:29  jmlarsen
00123  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00124  *
00125  * Revision 1.23  2007/04/14 05:55:19  amodigli
00126  * warning of flames,iter demoted to debug message
00127  *
00128  * Revision 1.22  2007/04/12 12:03:10  jmlarsen
00129  * Fixed doc. typo
00130  *
00131  * Revision 1.21  2007/03/05 10:18:55  jmlarsen
00132  * Do QC-log INS.SLITi.WID
00133  *
00134  * Revision 1.20  2007/02/26 10:16:22  jmlarsen
00135  * Do not QC log slit width
00136  *
00137  * Revision 1.19  2007/01/15 14:28:44  jmlarsen
00138  * Write default stability QC-parameters, only when master frame is provided
00139  *
00140  * Revision 1.18  2007/01/15 08:43:15  jmlarsen
00141  * Fixed missing plots
00142  *
00143  * Revision 1.17  2007/01/13 09:52:22  amodigli
00144  * fixed some problems on flames QC log
00145  *
00146  * Revision 1.16  2007/01/09 17:46:03  amodigli
00147  * fixed a problem in echelle mode
00148  *
00149  * Revision 1.15  2007/01/08 16:59:18  amodigli
00150  * changes to make flames-uves iterations to recover physical model
00151  *
00152  * Revision 1.14  2006/12/07 08:26:04  jmlarsen
00153  * Added uves_pfits_get_readspeed
00154  *
00155  * Revision 1.13  2006/12/01 12:29:21  jmlarsen
00156  * Factored out FLAMES plate-id code
00157  *
00158  * Revision 1.12  2006/11/22 08:42:20  jmlarsen
00159  * Set traceid = 1, not 0 for UVES
00160  *
00161  * Revision 1.10  2006/11/22 08:22:29  jmlarsen
00162  * Set message level according to preprocessor symbol
00163  *
00164  * Revision 1.9  2006/11/16 14:12:21  jmlarsen
00165  * Changed undefined trace number from 0 to -1, to support zero as an actual trace number
00166  *
00167  * Revision 1.8  2006/11/16 09:49:25  jmlarsen
00168  * Fixed doxygen bug
00169  *
00170  * Revision 1.7  2006/11/15 15:02:14  jmlarsen
00171  * Implemented const safe workarounds for CPL functions
00172  *
00173  * Revision 1.5  2006/11/15 14:04:08  jmlarsen
00174  * Removed non-const version of parameterlist_get_first/last/next which is already
00175  * in CPL, added const-safe wrapper, unwrapper and deallocator functions
00176  *
00177  * Revision 1.4  2006/11/06 15:19:41  jmlarsen
00178  * Removed unused include directives
00179  *
00180  * Revision 1.3  2006/10/26 14:03:48  jmlarsen
00181  * Fixed position of const modifier
00182  *
00183  * Revision 1.2  2006/10/25 11:43:32  amodigli
00184  * fixed problem running physmod in debug mode
00185  *
00186  * Revision 1.1  2006/10/24 14:09:56  jmlarsen
00187  * Factored out common UVES/FLAMES code
00188  *
00189  * Revision 1.30  2006/10/19 13:53:25  jmlarsen
00190  * Changed guess line table tag to LINE_GUESS_TAB
00191  *
00192  * Revision 1.29  2006/10/17 12:33:02  jmlarsen
00193  * Added semicolon at UVES_RECIPE_DEFINE invocation
00194  *
00195  * Revision 1.28  2006/10/11 12:22:36  amodigli
00196  * now the stability check consist only i the msrawxy and the table comparison, as in MIDAS
00197  *
00198  * Revision 1.27  2006/10/09 13:01:13  jmlarsen
00199  * Use macro to define recipe interface functions
00200  *
00201  * Revision 1.26  2006/09/19 14:31:38  jmlarsen
00202  * uves_insert_frame(): use bitmap to specify which image statistics keywords must be computed
00203  *
00204  * Revision 1.25  2006/09/19 06:55:39  jmlarsen
00205  * Changed interface of uves_frameset to optionally write image statistics kewwords
00206  *
00207  * Revision 1.24  2006/08/24 11:36:37  jmlarsen
00208  * Write recipe start/stop time to header
00209  *
00210  * Revision 1.23  2006/08/18 13:35:42  jmlarsen
00211  * Fixed/changed QC parameter formats
00212  *
00213  * Revision 1.22  2006/08/17 13:56:53  jmlarsen
00214  * Reduced max line length
00215  *
00216  * Revision 1.21  2006/08/11 14:56:05  amodigli
00217  * removed Doxygen warnings
00218  *
00219  * Revision 1.20  2006/08/07 11:35:35  jmlarsen
00220  * Disabled parameter environment variable mode
00221  *
00222  * Revision 1.19  2006/08/01 14:42:34  amodigli
00223  * fixed bugs getting raw header from master formatcheck
00224  *
00225  * Revision 1.18  2006/07/31 06:29:26  amodigli
00226  * added QC on stability test
00227  *
00228  * Revision 1.17  2006/07/28 14:51:26  amodigli
00229  * fixed some bugs on improper table selection
00230  *
00231  * Revision 1.16  2006/07/14 12:19:28  jmlarsen
00232  * Support multiple QC tests per product
00233  *
00234  * Revision 1.15  2006/07/03 12:46:34  amodigli
00235  * updated description
00236  *
00237  * Revision 1.14  2006/06/28 13:28:29  amodigli
00238  * improved output
00239  *
00240  * Revision 1.13  2006/06/20 09:06:39  amodigli
00241  * correct input tag info in man page
00242  *
00243  * Revision 1.12  2006/06/16 08:25:45  jmlarsen
00244  * Manually propagate ESO.DET. keywords from 1st/2nd input header
00245  *
00246  * Revision 1.11  2006/06/13 11:57:02  jmlarsen
00247  * Check that calibration frames are from the same chip ID
00248  *
00249  * Revision 1.10  2006/06/07 13:06:28  jmlarsen
00250  * Changed doxygen tag addtogroup -> defgroup
00251  *
00252  * Revision 1.9  2006/06/07 09:01:28  amodigli
00253  * added some doc
00254  *
00255  * Revision 1.8  2006/05/08 15:42:16  amodigli
00256  * allow to specify order column label
00257  *
00258  * Revision 1.7  2006/04/20 10:47:39  amodigli
00259  * added qclog
00260  *
00261  * Revision 1.6  2006/04/07 07:11:12  jmlarsen
00262  * Minor doc. fix
00263  *
00264  * Revision 1.5  2006/04/06 09:48:15  amodigli
00265  * changed uves_frameset_insert interface to have QC log
00266  *
00267  * Revision 1.4  2006/04/06 08:42:19  jmlarsen
00268  * Changed indentation
00269  *
00270  * Revision 1.3  2006/03/03 13:54:11  jmlarsen
00271  * Changed syntax of check macro
00272  *
00273  * Revision 1.2  2006/02/28 09:15:22  jmlarsen
00274  * Minor update
00275  *
00276  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00277  * Moved recipe implementations to ./uves directory
00278  *
00279  * Revision 1.66  2006/01/25 10:09:18  jmlarsen
00280  * Added doxygen end marker
00281  *
00282  * Revision 1.65  2006/01/20 10:36:25  amodigli
00283  *
00284  * Fixed warings from doxigen
00285  *
00286  * Revision 1.64  2006/01/19 10:03:06  amodigli
00287  * Fixed leaks
00288  *
00289  * Revision 1.62  2006/01/16 13:52:58  jmlarsen
00290  * Removed memory leak
00291  *
00292  * Revision 1.61  2006/01/16 08:01:57  amodigli
00293  *
00294  * Added stability check
00295  *
00296  * Revision 1.60  2006/01/13 13:43:15  jmlarsen
00297  * Removed memory leak
00298  *
00299  * Revision 1.59  2006/01/13 09:54:42  amodigli
00300  * Fixed some bugs: improved agreement with MIDAS version
00301  *
00302  * Revision 1.58  2006/01/09 15:23:06  jmlarsen
00303  * Removed some warnings
00304  *
00305  * Revision 1.57  2006/01/09 14:05:42  amodigli
00306  * Fixed doxigen warnings
00307  *
00308  * Revision 1.56  2006/01/03 16:57:13  amodigli
00309  * Fixed bug
00310  *
00311  * Revision 1.55  2006/01/03 14:47:53  amodigli
00312  *
00313  * Added uves_physmod_chop_otab.h .c to match MIDAS
00314  *
00315  * Revision 1.54  2005/12/19 16:17:55  jmlarsen
00316  * Replaced bool -> int
00317  *
00318  */
00319 #ifdef HAVE_CONFIG_H
00320 #  include <config.h>
00321 #endif
00322 
00323 /*----------------------------------------------------------------------------*/
00327 /*----------------------------------------------------------------------------*/
00328 
00329 /*-----------------------------------------------------------------------------
00330                                 Includes
00331  -----------------------------------------------------------------------------*/
00332 
00333 /* Self */
00334 #include <uves_physmod_body.h>
00335 
00336 /* called related functions */
00337 #include <uves_physmod_plotmod.h>
00338 #include <uves_physmod_create_table.h>
00339 
00340 #include <uves_physmod_qc1pmtbl.h>
00341 #include <uves_physmod_calmap.h>
00342 #include <uves_physmod_msrawxy.h>
00343 #include <uves_physmod_chop_otab.h>
00344 #include <uves_physmod_stability_check.h>
00345 
00346 /* Utility functions */
00347 #include <uves_utils.h>
00348 #include <uves_utils_polynomial.h>
00349 #include <uves_utils_wrappers.h>
00350 #include <uves_pfits.h>
00351 #include <uves_dfs.h>
00352 #include <uves_parameters.h>
00353 #include <uves_qclog.h>
00354 #include <uves_recipe.h>
00355 #include <uves_error.h>
00356 #include <uves_msg.h>
00357 /* Library */
00358 #include <cpl.h>
00359 #include <math.h>
00360 #include <stdbool.h>
00361 #include <string.h>
00362 /*-----------------------------------------------------------------------------
00363                                 Defines
00364  -----------------------------------------------------------------------------*/
00365 
00366 #define UVES_PHYSMOD_ITER_MAX 5
00367 /*-----------------------------------------------------------------------------
00368                             Functions prototypes
00369  ----------------------------------------------------------------------------*/
00370 
00371 static int 
00372 uves_physmod_qclog(cpl_table* line_table,
00373            cpl_table* order_table,
00374            cpl_table* qclog,
00375            const uves_propertylist *raw_header,
00376            enum uves_chip chip,
00377                    bool flames,
00378                    const int iter,
00379                    const int plate_no);
00380 
00381 static int 
00382 uves_physmod_qclog_sc(const double med_dx, 
00383               const double med_dy,
00384               const double avg_dx,
00385                       const double avg_dy,
00386                       const uves_propertylist *raw_header,
00387                       const uves_propertylist *ref_header,
00388                       enum  uves_chip chip,
00389               bool flames,
00390               const int iter,
00391               cpl_table* qclog);
00392 
00393 
00394 
00395 static int
00396 flames_get_physmod_shift(const int plate_no,
00397                          const int wavec,
00398                          enum uves_chip chip,
00399              double* physmod_shift_x,
00400                          double* physmod_shift_y,
00401              double* rot_1,
00402                          double* rot_2,
00403                          double* rot_3);
00404 
00405 
00406 /*-----------------------------------------------------------------------------
00407                             Recipe standard code
00408  -----------------------------------------------------------------------------*/
00409 
00410 const char * const uves_physmod_desc_short = "Implements the UVES physical model";
00411 const char * const uves_physmod_desc =
00412 "This recipe implements the UVES physical model\n"
00413 "Input files are BLUE or RED arm formatcheck frames identified by the tag\n"
00414 "ARC_LAMP_FORM_xxxx, xxxx=BLUE or RED and a ThAr line reference table\n"
00415 "identified by the tag LINE_REFER_TABLE\n"
00416 "The recipe extracts from the input files FITS header data indicating the\n"
00417 "instrument setting and ambiental atmospheric conditions, then using the\n"
00418 "model predicts X,Y position of the lines listed in the LINE_REFER_TABLE\n"
00419 "table which are imaging on the detector and stores this information in an\n"
00420 "guess order and a guess line table.\n"
00421 "Output are a guess order table and a guess line table per chip.\n"
00422 "If the user provides in input also master format checks having tag\n"
00423 "MASTER_FORM_xxxx, xxxx=BLUE or REDL and REDU the recipe performs also a\n"
00424 "stability check\n";
00425 
00427 /*-----------------------------------------------------------------------------
00428                             Functions code
00429  -----------------------------------------------------------------------------*/
00430 
00431 /*----------------------------------------------------------------------------*/
00437 /*----------------------------------------------------------------------------*/
00438 int
00439 uves_physmod_define_parameters_body(cpl_parameterlist *parameters, 
00440                                     const char *recipe_id)
00441 {
00442     const char *subcontext = NULL;
00443 
00444     /*****************
00445      *    General    *
00446      *****************/
00447 
00448     if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00449     {
00450         return -1;
00451     }
00452     
00453     uves_par_new_range("mbox_x",
00454                  CPL_TYPE_INT,
00455                  "Match box X size",
00456                  40,10,100);
00457 
00458     uves_par_new_range("mbox_y",
00459                  CPL_TYPE_INT,
00460                  "Match box Y size",
00461                  40,10,100);
00462 
00463     uves_par_new_value("trans_x",
00464                  CPL_TYPE_DOUBLE,
00465                  "Detector translation along X",
00466                  0.);
00467 
00468     uves_par_new_value("trans_y",
00469                  CPL_TYPE_DOUBLE,
00470                  "Detector translation along Y",
00471                  0.);
00472 
00473     uves_par_new_value("ech_angle_off",
00474                  CPL_TYPE_DOUBLE,
00475                  "Offset on echelle angle",
00476                  0.);
00477 
00478     uves_par_new_value("cd_angle_off",
00479                  CPL_TYPE_DOUBLE,
00480                  "Offset on cross disperser angle",
00481                  0.);
00482 
00483     uves_par_new_value("ccd_rot_angle_off",
00484                  CPL_TYPE_DOUBLE,
00485                  "Offset on CCD rotation angle",
00486                  0.);
00487 
00488     uves_par_new_value("compute_regression_sw",
00489                  CPL_TYPE_BOOL,
00490                  "Compute regression?",
00491                  true);
00492 
00493 /*  we decided to remove those params as we always have them set to 0
00494     uves_par_new_value("x_axis_scale",
00495                  CPL_TYPE_DOUBLE,
00496                  "Scale X axis",
00497                  0.);
00498 
00499     uves_par_new_value("y_axis_scale",
00500                  CPL_TYPE_DOUBLE,
00501                  "Scale Y axis",
00502                  0.);
00503 */
00504     uves_par_new_value("def_pol1",
00505                  CPL_TYPE_INT,
00506                  "Polynomial X deg",
00507                  4);
00508 
00509     uves_par_new_value("def_pol2",
00510                  CPL_TYPE_INT,
00511                  "Polynomial Y deg",
00512                  5);
00513 
00514     uves_par_new_value("kappa",
00515                  CPL_TYPE_DOUBLE,
00516                  "Kappa value in kappa sigma clipping "
00517                  "on RESIDUAL between YFIT and Y columns",
00518                  4.5);
00519 
00520     uves_par_new_value("tol",
00521                  CPL_TYPE_DOUBLE,
00522                  "Tolerance in kappa sigma clipping "
00523                  "on RESIDUAL between YFIT and Y columns",
00524                  2.0);
00525 
00526     return (int) cpl_error_get_code();
00527 }
00528 /*----------------------------------------------------------------------------*/
00556 /*----------------------------------------------------------------------------*/
00557 
00558 static cpl_table *
00559 uves_physmod_process_chip(const cpl_image *raw_image, 
00560                           const uves_propertylist *raw_header, 
00561                           const char *raw_filename, 
00562                           enum uves_chip chip,
00563               bool flames,
00564                           const char *recipe_id,
00565                           const int DEBUG, 
00566                           const cpl_parameterlist* parameters, 
00567                           cpl_table* line_refer,
00568                           const double physmod_shift_x,
00569                           const double physmod_shift_y,
00570                           cpl_table** tmp_mod_tbl,
00571                           cpl_table** lin_tbl,
00572                           cpl_table** ord_tbl,
00573                           cpl_table**  mline_tbl,
00574                           int* abs_ord_min,
00575                           int* abs_ord_max,
00576                           polynomial** absolute_order_poly2d,
00577                           const int stability_cnt)
00578 {
00579 
00580     cpl_table *tmp_fsr_tbl = NULL;
00581     cpl_table *tmp_m_tbl = NULL;
00582     cpl_table *tmp_p_tbl = NULL;
00583     cpl_table *tmp_w_tbl = NULL;
00584     cpl_table *tmp_s_tbl = NULL;
00585     cpl_table *tmp_rline_tbl = NULL;
00586     cpl_table *tmp_npline_tbl = NULL;
00587 
00588 
00589     cpl_table * result=NULL;
00590     const char *product_filename = NULL;
00591 
00592     /* Start processing this chip */
00593     uves_msg("stability counter=%d",stability_cnt);
00594     if(stability_cnt == 0) {
00595        uves_msg("offsetx=%f offsety=%f",physmod_shift_x,physmod_shift_y);
00596        check( uves_physmod_create_table(raw_header, 
00597                                         chip,
00598                                         flames,
00599                                         recipe_id,
00600                                         parameters, 
00601                                         line_refer, 
00602                                         physmod_shift_x,
00603                                         physmod_shift_y,
00604                                         tmp_mod_tbl,
00605                                         &tmp_fsr_tbl), 
00606               "Could not run UVES physical model on the raw image %s", 
00607               raw_filename);
00608 
00609        if (DEBUG) {
00610 
00611           const char *product_filename = NULL;
00612           product_filename = "model_prediction";
00613           check( uves_save_table_local("Physical model table", 
00614                                        product_filename, *tmp_mod_tbl, chip, -1, -1, raw_header, NULL),
00615                  "Error saving physical model table");
00616 
00617    
00618        }
00619     }
00620 
00621 
00622 
00623 
00624     uves_free_table(&tmp_rline_tbl);
00625     check( uves_physmod_msrawxy(raw_image, 
00626                 raw_header, 
00627                 recipe_id,
00628                 parameters, 
00629                 *tmp_mod_tbl,
00630                 lin_tbl,
00631                 &tmp_m_tbl,
00632                 &tmp_p_tbl,
00633                 &tmp_rline_tbl,
00634                 mline_tbl,
00635                 &tmp_npline_tbl), 
00636        "Could not run uves_msrawxy to measure arc line position on raw image %s", 
00637        raw_filename);
00638 
00639 /*
00640     product_filename = "tmp_mod_tbl";
00641         check( uves_save_table_local("tmp_mod_tbl table", 
00642                product_filename, *tmp_mod_tbl, chip, -1, -1, raw_header, NULL),
00643               "Error saving tmp_mod_tbl table");
00644 */
00645 
00646     check(uves_physmod_plotmod(tmp_rline_tbl,raw_header,recipe_id,
00647                                parameters,chip),
00648           "Could not run uves_physmod_plotmod");
00649   
00650     if(stability_cnt == 0) {
00651       uves_free_table(&tmp_w_tbl);
00652       uves_free_table(&tmp_s_tbl);
00653       check(uves_physmod_calmap(raw_header,
00654                     chip,
00655                     recipe_id,
00656                     parameters, 
00657                     tmp_npline_tbl,
00658                     ord_tbl,
00659                     lin_tbl,
00660                     &tmp_w_tbl,
00661                     &tmp_s_tbl,
00662                     abs_ord_min,
00663                     abs_ord_max,
00664                     absolute_order_poly2d), 
00665         "Could not run uves_calmap on raw image %s", raw_filename);
00666 
00667       //cpl_table_save(*lin_tbl, NULL, NULL, "pippo4.fits", CPL_IO_DEFAULT);
00668    
00669 
00670       check(uves_physmod_chop_otab(raw_header,chip,lin_tbl,"Order",
00671                    abs_ord_min,abs_ord_max),
00672             "Could not run uves_physmod_chop_otab on raw image %s",
00673             raw_filename);
00674 
00675 
00676       check(uves_physmod_qc1pmtbl(&tmp_rline_tbl,lin_tbl), 
00677        "Could not run uves_qc1pmtbl on raw image %s", raw_filename);
00678 
00679     
00680       check(uves_physmod_chop_otab(raw_header,chip,ord_tbl,"ORDER",
00681                    abs_ord_min,abs_ord_max),
00682             "Could not run uves_physmod_chop_otab on raw image %s",
00683             raw_filename);
00684     }
00685 
00686     if (DEBUG) {
00687         /* Temporary products (to DEBUG) */
00688 
00689     /* Basic info about orders */
00690     if(stability_cnt==0) {
00691       product_filename = "arclampform";
00692     } else {
00693       product_filename = "ref_arclampform";
00694     }
00695         check( uves_save_image_local("Physical model table", 
00696                product_filename, raw_image, chip, -1, -1, raw_header, true),
00697               "Error saving arc lamp form image");
00698 
00699     product_filename = "pline";
00700         check( uves_save_table_local("Physical model table", 
00701                product_filename, *tmp_mod_tbl, chip, -1, -1, raw_header, NULL),
00702               "Error saving physical model table");
00703 
00704       
00705      if(stability_cnt == 0) {
00706       product_filename = "free_spectral_range";
00707       check( uves_save_table_local("FSR table",
00708                        product_filename, tmp_fsr_tbl,
00709                                        chip, -1, -1, raw_header, NULL),
00710          "Error saving free spectral range table");
00711 
00712     }
00713         
00714     product_filename = "midduml";
00715         check( uves_save_table_local("midduml table", 
00716                product_filename, *lin_tbl, chip, -1, -1, raw_header, NULL),
00717               "Error saving midduml table");
00718 
00719 
00720  
00721     product_filename = "middumm";
00722         check( uves_save_table_local("middumm table", 
00723                product_filename, tmp_m_tbl, chip, -1, -1, raw_header, NULL),
00724               "Error saving middumm table");
00725 
00726 
00727     
00728     product_filename = "middumrline";
00729         check( uves_save_table_local("middumrline table", 
00730                product_filename, tmp_rline_tbl, chip, -1, -1, raw_header, NULL),
00731               "Error saving middumrline table");
00732 
00733 
00734     product_filename = "middummline";
00735         check( uves_save_table_local("middummline table", 
00736                product_filename, *mline_tbl, chip, -1, -1, raw_header, NULL),
00737               "Error saving middummline table");
00738 
00739 
00740     product_filename = "middump";
00741         check( uves_save_table_local("middump table", 
00742                product_filename, tmp_p_tbl, chip, -1, -1, raw_header, NULL),
00743               "Error saving middump table");
00744 
00745        
00746     product_filename = "middumnpline";
00747         check( uves_save_table_local("middumnpline table", 
00748                product_filename, tmp_npline_tbl, chip, -1, -1, raw_header, NULL),
00749               "Error saving middumnpline table");
00750       
00751        
00752     /* Calmap */
00753     if(stability_cnt== 0) {
00754       product_filename = "middumw";
00755       check( uves_save_table_local("middumw table", 
00756                        product_filename, tmp_w_tbl, 
00757                                        chip, -1, -1, raw_header, NULL),
00758          "Error saving middumw table");
00759      
00760       product_filename = "middums";
00761       check( uves_save_table_local("middums table", 
00762                        product_filename, tmp_s_tbl, 
00763                                        chip, -1, -1, raw_header, NULL),
00764          "Error saving middums table");
00765      
00766        
00767       product_filename = "order";
00768       check( uves_save_table_local("order table", 
00769                        product_filename, *ord_tbl, 
00770                                        chip, -1, -1, raw_header, NULL),
00771          "Error saving order table");
00772       uves_msg("Order table saved to file %s", 
00773            product_filename);
00774 
00775     }
00776     product_filename = "line";
00777         check( uves_save_table_local("line table", 
00778                product_filename, *lin_tbl, chip, -1, -1, raw_header, NULL),
00779               "Error saving uves_line table");
00780         uves_msg("Line table saved to file %s", 
00781                                 product_filename);
00782     
00783      
00784     
00785     }
00786     
00787   cleanup:
00788     uves_free_table(&tmp_fsr_tbl);
00789     uves_free_table(&tmp_m_tbl);
00790     uves_free_table(&tmp_p_tbl);
00791     uves_free_table(&tmp_w_tbl);
00792     uves_free_table(&tmp_s_tbl);
00793     uves_free_table(&tmp_rline_tbl);
00794      uves_free_table(&tmp_npline_tbl);
00795      
00796     uves_msg_debug("end %s",__func__);
00797     return result;
00798 }
00799 
00800 
00801 
00802 /*---------------------------------------------------------------------------*/
00811 /*---------------------------------------------------------------------------*/
00812 void
00813 uves_physmod_exe_body(cpl_frameset *frames,
00814               bool flames,
00815               const char *recipe_id,
00816               const cpl_parameterlist *parameters,
00817               const char *starttime)
00818 {
00819   /* Input image */
00820   cpl_image        *raw_image[2]      = {NULL, NULL};
00821   uves_propertylist *raw_header[2]     = {NULL, NULL};
00822   uves_propertylist *rotated_header[2] = {NULL, NULL};
00823   uves_propertylist *master_formatcheck_header[2] = {NULL, NULL};
00824 
00825   /* Physical Model guess table products */
00826   cpl_table        *line_table     = NULL;
00827   cpl_table        *order_table     = NULL;
00828   cpl_table        *mline_table     = NULL;
00829   cpl_table        *m_mline_table     = NULL;
00830   cpl_table        *model_table = NULL;
00831 
00832   cpl_table        *r_mline_table     = NULL;
00833   cpl_table        *mst_line_table     = NULL;
00834   cpl_table        *mst_mline_table     = NULL;
00835   cpl_table        *mst_order_table     = NULL;
00836 
00837 
00838   uves_propertylist *product_header = NULL;
00839   uves_propertylist *table_header = NULL;
00840   cpl_table        *line_refer            = NULL;
00841   polynomial       *absolute_order_poly2d=NULL;      
00842   polynomial       *mst_absolute_order_poly2d=NULL;      
00843 
00844 
00845   /* Local variables */
00846   int      DEBUG=0;
00847   int abs_ord_min=0;
00848   int abs_ord_max=0;
00849   int mst_abs_ord_min=0;
00850   int mst_abs_ord_max=0;
00851   double avg_dx=0;
00852   double avg_dy=0;
00853 
00854   const char *raw_filename = "";
00855   char *product_filename = NULL;
00856     
00857   bool blue = false;
00858   enum uves_chip chip;    
00859   const char *line_refer_filename = "";
00860   const char *master_formatcheck_filename = "";
00861   cpl_image        *master_formatcheck    = NULL;
00862   int nordpred=0;
00863   int plate_no=0;
00864   int iter=1;
00865   int it=0;
00866 
00867   int iter_max=UVES_PHYSMOD_ITER_MAX;
00868   /* QC tables for iter_max*(TEST1+TEST2) and a NULL pointer */
00869   cpl_table* qclog[iter_max*2+1];
00870 
00871 
00872   double physmod_shift_x=0;
00873   double physmod_shift_y=0;
00874   double ref_frame_physmod_shift_x=0;
00875   double ref_frame_physmod_shift_y=0;
00876   const double max_shift_x=0.4;
00877   const double max_shift_y=0.2;
00878   double med_dx=2*max_shift_x;
00879   double med_dy=2*max_shift_y;
00880   const char* PROCESS_CHIP=NULL;
00881   /* Master bias */
00882   cpl_image *master_bias               = NULL;
00883   uves_propertylist *master_bias_header = NULL;
00884 
00885   double rot_1=0;
00886   double rot_2=0;
00887   double rot_3=0;
00888   double wlen=0;
00889   int wavec=0;
00890   int stab_check=0;
00891   int stability_cnt=1;
00892   const char *chip_name = "";
00893   int raw_index = 0;
00894   const char *master_bias_filename = "";
00895 
00896   double trans_x=0;
00897   double trans_y=0;
00898 
00899   for( iter=0 ; iter < 2*iter_max+1 ; iter++) {
00900     qclog[iter]=NULL;
00901   }
00902 
00903   if(flames) {
00904     iter_max=1;  //Temporally changed to 1
00905   } else {
00906     iter_max=1;
00907   }
00908   check( uves_get_parameter(parameters, NULL, "uves", "debug",  
00909                 CPL_TYPE_BOOL, &DEBUG),
00910      "Could not read parameter");
00911   check( uves_get_parameter(parameters, NULL, "uves", "process_chip", 
00912                 CPL_TYPE_STRING, &PROCESS_CHIP),
00913      "Could not read parameter");
00914   uves_string_toupper((char*)PROCESS_CHIP);
00915   /* Load raw image and header, and identify input frame as red or blue */
00916   check( uves_load_formatcheck(frames, flames, &raw_filename, raw_image, 
00917                    raw_header, rotated_header, &blue), 
00918      "Error loading raw frame");
00919 
00920   /* Load reference line table */
00921   check( uves_load_linerefertable(frames, &line_refer_filename, 
00922                   &line_refer, NULL), 
00923      "Could not load line reference table");
00924   uves_msg("Using line reference table in '%s'", line_refer_filename);
00925 
00926   check( uves_get_parameter(parameters, NULL, recipe_id,
00927                 "trans_x", CPL_TYPE_DOUBLE, &trans_x )  ,
00928      "Could not read parameter");
00929 
00930   check( uves_get_parameter(parameters, NULL, recipe_id, 
00931                 "trans_y", CPL_TYPE_DOUBLE, &trans_y )  ,
00932      "Could not read parameter");
00933 
00934 
00935 
00936   /* Loop over one or two chips */
00937   for (chip = uves_chip_get_first(blue); 
00938        chip != UVES_CHIP_INVALID; 
00939        chip = uves_chip_get_next(chip))
00940     {
00941  
00942       if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00943     chip = uves_chip_get_next(chip);
00944       }
00945 
00946       stab_check=0;
00947       stability_cnt=1;
00948       raw_index = uves_chip_get_index(chip);
00949         
00950 
00951       uves_msg("Processing %s chip in '%s'", 
00952            uves_chip_tostring_upper(chip), raw_filename);
00953 
00954       check_nomsg( chip_name = uves_pfits_get_chipid(raw_header[raw_index], chip));
00955 
00956 
00957       physmod_shift_x=0;
00958       physmod_shift_y=0;
00959       if(flames) {
00960     check(plate_no = uves_flames_pfits_get_plateid(raw_header[raw_index]),
00961           "Error reading plate id");
00962         check_nomsg(wlen=uves_pfits_get_gratwlen(raw_header[raw_index],chip));
00963     wavec=(int)wlen;
00964         
00965 
00966     ck0_nomsg(flames_get_physmod_shift(plate_no,wavec,chip,
00967                        &physmod_shift_x,&physmod_shift_y,
00968                        &rot_1,&rot_2,&rot_3));
00969 
00970         ref_frame_physmod_shift_x=physmod_shift_x;
00971         ref_frame_physmod_shift_y=physmod_shift_y;
00972 
00973 
00974 
00975     uves_msg("shift_x=%f shift_y=%f",physmod_shift_x,physmod_shift_y);
00976     if( trans_x != 0 ) {
00977       physmod_shift_x+=trans_x;
00978     }
00979 
00980     if( trans_y != 0 ) {
00981       physmod_shift_y+=trans_y;
00982     }
00983     uves_msg("shift_x=%f shift_y=%f",physmod_shift_x,physmod_shift_y);
00984 
00985       } else {
00986 
00987     if( trans_x != 0 ) {
00988       physmod_shift_x=trans_x;
00989     }
00990 
00991     if( trans_y != 0 ) {
00992       physmod_shift_y=trans_y;
00993     }
00994 
00995       }
00996       uves_msg("Using physmod shifts: %g %g",physmod_shift_x,physmod_shift_y);
00997       /* Check if a stability check can be done */
00998       if (cpl_frameset_find(frames, UVES_MASTER_ARC_FORM(chip)) != NULL) {
00999       
01000     uves_free_image (&master_formatcheck);
01001     uves_free_propertylist(&master_formatcheck_header[raw_index]);
01002     check( uves_load_master_formatcheck(frames, 
01003                         chip_name,
01004                         &master_formatcheck_filename, 
01005                         &master_formatcheck,
01006                         &master_formatcheck_header[raw_index],                  
01007                         chip),
01008            "Could not load master formatcheck frm");
01009             
01010 
01011     stab_check=1;
01012 
01013       } else {
01014     uves_msg_low("No master format check frm in SOF.");
01015     uves_msg_low("Stability check not done");
01016       }
01017 
01018       /* Load master bias, set pointer to NULL if not present */
01019       uves_free_image(&master_bias);
01020       uves_free_propertylist(&master_bias_header);
01021       if (cpl_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
01022         {
01023       uves_free_image(&master_bias);
01024       uves_free_propertylist(&master_bias_header);
01025       check( uves_load_mbias(frames,
01026                  chip_name,
01027                  &master_bias_filename, &master_bias,
01028                  &master_bias_header, chip), 
01029          "Error loading master bias");
01030             
01031       uves_msg_low("Using master bias in '%s' and '%s'", 
01032                master_bias_filename,master_formatcheck_filename);
01033       check_nomsg(cpl_image_subtract(raw_image[raw_index],master_bias));
01034       if(stab_check==1) {
01035         check_nomsg(cpl_image_subtract(master_formatcheck,master_bias));
01036       }     
01037 
01038         }
01039       else
01040         {
01041       uves_msg_low("No master bias in SOF. Bias subtraction not done");
01042         }
01043 
01044       /* Init QC-Log: before starting the stability check as in 
01045      FLAMES-UVES one may have several iterations to be dumped 
01046      on the different tables
01047       */
01048  
01049       for(iter=1,it=0;
01050       iter<=iter_max &&
01051         (fabs(med_dx) > max_shift_x || 
01052          fabs(med_dy) > max_shift_y ) ;
01053       iter++,it+=2) {
01054     uves_msg("iter=%d it=%d",iter,it);
01055 
01056     uves_qclog_delete(&qclog[it]);
01057     uves_qclog_delete(&qclog[it+1]);
01058     qclog[it]   = uves_qclog_init(raw_header[raw_index], chip);
01059     qclog[it+1] = uves_qclog_init(raw_header[raw_index], chip);
01060 
01061   
01062     //Run the physical model on the raw frame
01063     uves_free_table(&model_table);
01064     uves_free_table(&line_table);
01065     uves_free_table(&order_table);
01066     uves_free_table(&mline_table);
01067 
01068     uves_polynomial_delete(&absolute_order_poly2d);
01069     uves_msg("Run the physical model on the raw frame");
01070         uves_msg("physmod shift x %f y %f",physmod_shift_x,physmod_shift_y);
01071     check( uves_physmod_process_chip(raw_image[raw_index], 
01072                      raw_header[raw_index],
01073                      raw_filename,
01074                      chip,
01075                      flames,
01076                      recipe_id,
01077                      DEBUG,
01078                      parameters,
01079                      line_refer,
01080                      physmod_shift_x,
01081                      physmod_shift_y,
01082                      &model_table,
01083                      &line_table,
01084                      &order_table,
01085                      &mline_table,
01086                      &abs_ord_min,
01087                      &abs_ord_max,
01088                      &absolute_order_poly2d,0),
01089            "Error processing chip");
01090 
01091     nordpred=abs_ord_max-abs_ord_min+1;
01092     uves_msg("No of predicted orders %d",nordpred);
01093 
01094 
01095     /* add QC log */
01096     uves_msg("add QC log on raw frame");
01097     ck0(uves_physmod_qclog(line_table,order_table,qclog[it],
01098                    raw_header[raw_index], chip,
01099                    flames,iter,plate_no),"qc-log dump");
01100 
01101 
01102        
01103     uves_msg("iter_max=%d",iter_max);
01104     if(stab_check) {
01105       uves_free_table(&mst_line_table);
01106       uves_free_table(&mst_order_table);
01107       uves_free_table(&mst_mline_table);
01108       uves_polynomial_delete(&mst_absolute_order_poly2d);
01109 
01110       uves_msg("Run the physical model on the Master formatcheck frame");
01111           uves_msg("ref physmod shift x %f y %f",
01112            ref_frame_physmod_shift_x,ref_frame_physmod_shift_y);
01113 
01114       uves_msg("Stability counter=%d",stability_cnt);
01115       check(uves_physmod_process_chip(master_formatcheck,
01116                       master_formatcheck_header[raw_index], 
01117                       master_formatcheck_filename,
01118                       chip,
01119                       flames,
01120                       recipe_id,
01121                       DEBUG,
01122                       parameters,
01123                       line_refer,
01124                       ref_frame_physmod_shift_x,
01125                       ref_frame_physmod_shift_y,
01126                       &model_table,
01127                       &mst_line_table,
01128                       &mst_order_table,
01129                       &mst_mline_table,
01130                       &mst_abs_ord_min,
01131                       &mst_abs_ord_max,
01132                       &mst_absolute_order_poly2d,
01133                       stability_cnt),
01134         "Error processing chip");
01135       stability_cnt+=1;            
01136       uves_msg("Stability check");
01137       uves_msg("Using master format check frm '%s'", 
01138            master_formatcheck_filename);
01139       check_nomsg( uves_physmod_stability_check(
01140                             mline_table,
01141                             mst_mline_table,
01142                             &med_dx,
01143                             &med_dy,
01144                             &avg_dx,
01145                             &avg_dy) );
01146 
01147 
01148             
01149       uves_physmod_qclog_sc(med_dx,med_dy,avg_dx,avg_dy, 
01150                 raw_header[raw_index],
01151                 master_formatcheck_header[raw_index],
01152                 chip,flames,iter,
01153                 qclog[it+1]);
01154       uves_msg("iter=%d med_dx=%g med_dy=%g",
01155            iter,fabs(med_dx),fabs(med_dy));
01156       uves_msg("iter=%d max_shift_x=%g max_shift_y=%g",
01157            iter,max_shift_x,max_shift_y);
01158 
01159 
01160       nordpred=abs_ord_max-abs_ord_min+1;
01161       physmod_shift_x-=med_dx;
01162       physmod_shift_y-=med_dy;
01163 
01164     } // end of stability check
01165       
01166       } // end of iterations
01167          
01168         
01169         /* Finished. Now save the products */
01170       uves_msg("Saving products...");
01171        
01172         
01173       /* QC parameters should go here.
01174      Other mandatory keywords (FITS + dfs) are
01175      automatically added. */
01176 
01177       uves_free_propertylist(&product_header);
01178       uves_free_propertylist(&table_header);
01179       product_header = uves_propertylist_new();
01180       table_header = uves_propertylist_new();
01181       check( uves_pfits_set_traceid     ( table_header, 0),
01182          "Error writing trace ID to product header");
01183       check( uves_pfits_set_windownumber( table_header, 2),
01184          "Error window number to product header");
01185 
01186       check( uves_pfits_set_firstabsorder(table_header, abs_ord_min),
01187          "Error window number to product header");
01188 
01189       check( uves_pfits_set_lastabsorder(table_header, abs_ord_max),
01190          "Error window number to product header");
01191 
01192       check_nomsg(uves_pfits_set_ordpred(product_header,nordpred));
01193 
01194 
01195       if (flames) {
01196             
01197     check_nomsg(uves_flames_pfits_set_newplateid(product_header, 
01198                              plate_no));
01199       }
01200 
01201       cpl_free(product_filename);
01202       check(( product_filename = uves_guess_line_table_filename(chip),
01203           uves_frameset_insert(frames,
01204                    line_table,
01205                    CPL_FRAME_GROUP_PRODUCT,
01206                    CPL_FRAME_TYPE_TABLE,
01207                    CPL_FRAME_LEVEL_INTERMEDIATE,
01208                    product_filename,
01209                    UVES_GUESS_LINE_TABLE(flames, chip),
01210                    raw_header[raw_index], 
01211                    product_header,
01212                    table_header,
01213                    parameters,
01214                    recipe_id,
01215                    PACKAGE "/" PACKAGE_VERSION,
01216                    qclog,
01217                    starttime, 
01218                    true, 
01219                    0)),
01220         "Could not add line guess table %s to frameset", 
01221         product_filename);
01222                 
01223       /* Save in next extension */
01224         
01225       check( uves_save_polynomial(absolute_order_poly2d, 
01226                   product_filename, table_header),
01227          "Could not write polynomial to file '%s'", product_filename);
01228                
01229         
01230       check( uves_save_polynomial(absolute_order_poly2d, 
01231                   product_filename, table_header), 
01232          "Could not write polynomial to file '%s'", product_filename);
01233             
01234 
01235 
01236       uves_msg("Line table %s added to frameset", product_filename);
01237         
01238       /* Save guess order table, re-use product header */
01239 
01240       cpl_free(product_filename);        
01241       check(( product_filename = uves_guess_order_table_filename(chip),
01242           uves_frameset_insert(frames,
01243                    order_table,
01244                    CPL_FRAME_GROUP_PRODUCT,
01245                    CPL_FRAME_TYPE_TABLE,
01246                    CPL_FRAME_LEVEL_INTERMEDIATE,
01247                    product_filename,
01248                    UVES_GUESS_ORDER_TABLE(flames, chip),
01249                    raw_header[raw_index],
01250                    product_header,
01251                    NULL,
01252                    parameters,
01253                    recipe_id,
01254                    PACKAGE "/" PACKAGE_VERSION,
01255                    NULL,
01256                    starttime, 
01257                    false, 
01258                    0)), 
01259         "Could not add order guess table %s to frameset", 
01260         product_filename);
01261         
01262       uves_msg("Order guess table %s added to frameset", 
01263            product_filename);
01264 
01265       for(it=0;it<2*iter_max+1;it++) {
01266     uves_qclog_delete(&qclog[it]);
01267       }
01268 
01269       if(strcmp(PROCESS_CHIP,"REDL") == 0) {
01270     chip = uves_chip_get_next(chip);
01271       }
01272 
01273     }/* For each chip */
01274 
01275  cleanup:
01276   for(it=0;it<2*iter_max+1;it++) {
01277     uves_qclog_delete(&qclog[it]);
01278   }
01279    
01280   /* Master bias */
01281   uves_free_image(&master_bias);
01282   uves_free_propertylist(&master_bias_header);
01283 
01284   uves_free_image (&raw_image[0]);
01285   uves_free_image (&raw_image[1]);
01286   uves_free_propertylist(&raw_header[0]);
01287   uves_free_propertylist(&raw_header[1]);
01288   uves_free_propertylist(&rotated_header[0]);
01289   uves_free_propertylist(&rotated_header[1]);
01290 
01291   uves_free_image (&master_formatcheck);
01292   uves_free_propertylist(&master_formatcheck_header[0]);
01293   uves_free_propertylist(&master_formatcheck_header[1]);
01294 
01295   uves_free_table(&model_table);
01296   uves_free_table(&line_table);
01297   uves_free_table(&mst_line_table);
01298   uves_free_table(&order_table);
01299   uves_free_table(&mst_order_table);
01300   uves_free_table(&line_refer);
01301 
01302   uves_free_table(&mline_table);
01303   uves_free_table(&m_mline_table);
01304   uves_free_table(&r_mline_table);
01305   uves_free_table(&mst_mline_table);
01306 
01307   uves_free_propertylist(&product_header);
01308   uves_free_propertylist(&table_header);
01309   uves_polynomial_delete(&absolute_order_poly2d);
01310   uves_polynomial_delete(&mst_absolute_order_poly2d);
01311   cpl_free(product_filename); 
01312           
01313   uves_msg_debug("end %s",__func__);
01314   return;
01315 }
01316 
01317 
01327 static int 
01328 uves_physmod_qclog(cpl_table* line_table,
01329                    cpl_table* order_table,
01330                    cpl_table* qclog,
01331                    const uves_propertylist *raw_header,
01332                    enum uves_chip chip,
01333                    bool flames,
01334                    const int iter,
01335                    const int plate_no)
01336 {
01337   int nlinsel=0;
01338   cpl_table *xline_table=NULL;
01339   char key_value[25];
01340 
01341 
01342   check_nomsg( uves_qclog_add_common_wave(raw_header,
01343                                           chip, qclog) );
01344 
01345 
01346   if(flames) {
01347      ck0_nomsg(uves_qclog_add_string(qclog,
01348                      "QC TEST1 ID",
01349                      "Fibre-Physical-Model-Prediction-Results",
01350                      "Name of QC test",
01351                      "%s"));
01352   } else {
01353      ck0_nomsg(uves_qclog_add_string(qclog,
01354                      "QC TEST1 ID",
01355                      "Physical-Model-Prediction-Results",
01356                      "Name of QC test",
01357                      "%s"));
01358   }
01359   ck0_nomsg(uves_qclog_add_string(qclog,
01360                   "QC MODEL ID",
01361                   "UVES_phys_mod/1.1.0",
01362                   "Physmod Model Id",
01363                   "%s"));
01364 
01365   ck0_nomsg(uves_qclog_add_string(qclog,
01366                   "QC MODEL DATE",
01367                   "2000:03:18T00:00:00.000",
01368                   "Physmod Model Parameters Last Change",
01369                   "%s"));
01370 
01371   if(flames) {
01372 
01373     sprintf(key_value,"%s%d","QC MODEL ITER",iter);
01374     ck0_nomsg(uves_qclog_add_int(qclog,
01375                  key_value,
01376                  iter,
01377                  "Model iteration",
01378                  "%d"));
01379 
01380     ck0_nomsg(uves_qclog_add_int(qclog,
01381                  "QC MODEL ORDMIN",
01382                  cpl_table_get_column_min(line_table,"Order"),
01383                  "minimum predicted order value",
01384                  "%d"));
01385 
01386     ck0_nomsg(uves_qclog_add_int(qclog,
01387                  "QC MODEL ORDMAX",
01388                  cpl_table_get_column_max(line_table,"Order"),
01389                  "maximum predicted order value",
01390                  "%d"));
01391 
01392 
01393     ck0_nomsg(uves_qclog_add_double(qclog,
01394                  "QC MODEL WLENMIN",
01395                  cpl_table_get_column_min(line_table,"WAVEC")/10.,
01396                  "minimum predicted order value",
01397                  "%f"));
01398 
01399     ck0_nomsg(uves_qclog_add_double(qclog,
01400                  "QC MODEL WLENMAX",
01401                  cpl_table_get_column_max(line_table,"WAVEC")/10.,
01402                  "maximum predicted order value",
01403                  "%f"));
01404 
01405 
01406   } /* in case of flames-uves */
01407 
01408   ck0_nomsg(uves_qclog_add_int(qclog,
01409                    "QC MODEL NLINALL",
01410                    cpl_table_get_nrow(line_table),
01411                    "Number of predicted lines",
01412                    "%d"));
01413 
01414   check_nomsg(nlinsel=cpl_table_and_selected_int(line_table,"SELPLOT",
01415                                                  CPL_EQUAL_TO,1));
01416   check_nomsg(xline_table=cpl_table_extract_selected(line_table));
01417 
01418   ck0_nomsg(uves_qclog_add_int(qclog,
01419                    "QC MODEL NLINSEL",
01420                    nlinsel,
01421                    "Number of lines selected",
01422                    "%d"));
01423 
01424   if(iter == 1) {
01425      ck0_nomsg(uves_qclog_add_double(qclog,
01426                      "QC MODEL DIFFXRMS",
01427                      cpl_table_get_column_stdev(xline_table,"XDIF"),
01428                      "Std dev of X difference to physical model",
01429                      "%8.4f"));
01430 
01431      ck0_nomsg(uves_qclog_add_double(qclog,
01432                      "QC MODEL DIFFXAVG",
01433                      cpl_table_get_column_mean(xline_table,"XDIF"),
01434                      "Average of X difference to physical model",
01435                      "%8.4f"));
01436 
01437      ck0_nomsg(uves_qclog_add_double(qclog,"QC MODEL DIFFXMED",
01438                      cpl_table_get_column_median(xline_table,"XDIF"),
01439                      "Median of X difference to physical model",
01440                      "%8.4f"));
01441 
01442      ck0_nomsg(uves_qclog_add_double(qclog,
01443                      "QC MODEL DIFFYRMS",
01444                      cpl_table_get_column_stdev(xline_table,"YDIF"),
01445                      "Std dev of Y difference to physical model",
01446                      "%8.4f"));
01447 
01448      ck0_nomsg(uves_qclog_add_double(qclog,
01449                      "QC MODEL DIFFYAVG",
01450                      cpl_table_get_column_mean(xline_table,"YDIF"),
01451                      "Average of Y difference to physical model",
01452                      "%8.4f"));
01453 
01454      ck0_nomsg(uves_qclog_add_double(qclog,"QC MODEL DIFFYMED",
01455                      cpl_table_get_column_median(xline_table,"YDIF"),
01456                      "Median of Y difference to physical model",
01457                      "%8.4f"));
01458   }
01459 
01460   if ( flames == 1 ) {
01461 
01462 
01463        sprintf(key_value,"%s%d","QC MODEL RESXRMS",iter);
01464        ck0_nomsg(uves_qclog_add_double(qclog,
01465                     key_value,
01466                     cpl_table_get_column_stdev(xline_table,"XDIF"),
01467                     "Std dev of X difference to physical model",
01468                     "%8.4f"));
01469 
01470 
01471        sprintf(key_value,"%s%d","QC MODEL RESXAVG",iter);
01472        ck0_nomsg(uves_qclog_add_double(qclog,
01473                     key_value,
01474                     cpl_table_get_column_mean(xline_table,"XDIF"),
01475                     "Average of X difference to physical model",
01476                     "%8.4f"));
01477 
01478        sprintf(key_value,"%s%d","QC MODEL RESXMED",iter);
01479        ck0_nomsg(uves_qclog_add_double(qclog,
01480                     key_value,
01481                     cpl_table_get_column_median(xline_table,"XDIF"),
01482                     "Median of X difference to physical model",
01483                     "%8.4f"));
01484 
01485 
01486 
01487        sprintf(key_value,"%s%d","QC MODEL RESYRMS",iter);
01488        ck0_nomsg(uves_qclog_add_double(qclog,
01489                     key_value,
01490                     cpl_table_get_column_stdev(xline_table,"YDIF"),
01491                     "Std dev of Y difference to physical model",
01492                     "%8.4f"));
01493 
01494 
01495        sprintf(key_value,"%s%d","QC MODEL RESYAVG",iter);
01496        ck0_nomsg(uves_qclog_add_double(qclog,
01497                     key_value,
01498                     cpl_table_get_column_mean(xline_table,"YDIF"),
01499                     "Average of Y difference to physical model",
01500                     "%8.4f"));
01501 
01502        sprintf(key_value,"%s%d","QC MODEL RESYMED",iter);
01503        ck0_nomsg(uves_qclog_add_double(qclog,
01504                     key_value,
01505                     cpl_table_get_column_median(xline_table,"YDIF"),
01506                     "Median of Y difference to physical model",
01507                     "%8.4f"));
01508 
01509 
01510   }
01511 
01512 
01513   cpl_table_unselect_all(line_table);
01514 
01515   /* we divide by 10 as line_table contains values in Angstrom and we want
01516      nanometers */
01517   ck0_nomsg(uves_qclog_add_double(qclog,
01518                   "QC MODEL WLENMIN",
01519                   cpl_table_get_column_min(xline_table,"WAVEC")/10.,
01520                   "minimum predicted wavelength value",
01521                   "%8.4f"));
01522 
01523  ck0_nomsg(uves_qclog_add_double(qclog,
01524                  "QC MODEL WLENMAX",
01525                  cpl_table_get_column_max(xline_table,"WAVEC")/10.,
01526                  "maximum predicted wavelength value",
01527                  "%8.4f"));
01528 
01529 
01530   ck0_nomsg(uves_qclog_add_int(qclog,
01531                    "QC MODEL ORDMIN",
01532                    (int)cpl_table_get_column_min(xline_table,"Order"),
01533                    "minimum predicted order value",
01534                    "%d"));
01535 
01536   ck0_nomsg(uves_qclog_add_int(qclog,
01537                    "QC MODEL ORDMAX",
01538                    (int)cpl_table_get_column_max(xline_table,"Order"),
01539                    "maximum predicted order value",
01540                    "%d"));
01541 
01542 
01543  
01544   /* we divide by 10 as line_table contains values in Angstrom and we want
01545      nanometers */
01546   ck0_nomsg(uves_qclog_add_double(qclog,
01547                   "QC WLENMIN",
01548                   cpl_table_get_column_min(line_table,"WAVEC")/10.,
01549                   "minimum wavelength",
01550                   "%8.4f"));
01551 
01552   ck0_nomsg(uves_qclog_add_double(qclog,
01553                   "QC WLENMAX",
01554                   cpl_table_get_column_max(line_table,"WAVEC")/10.,
01555                   "maximum wavelength",
01556                   "%8.4f"));
01557   
01558   ck0_nomsg(uves_qclog_add_int(qclog,
01559                    "QC ORDMIN",
01560                    cpl_table_get_column_min(order_table,"ORDER"),
01561                    "minimum order number",
01562                    "%d"));
01563 
01564   ck0_nomsg(uves_qclog_add_int(qclog,
01565                    "QC ORDMAX",
01566                    cpl_table_get_column_max(order_table,"ORDER"),
01567                    "maximum order number",
01568                    "%d"));
01569 
01570 
01571   if (flames) {
01572     /*
01573 stat/ima {wlc} {SESSOUTV}
01574 tot_int = outputr(7)
01575 exp_time = {{wlc},{h_dit1}}
01576 rel_int = tot_int / exp_time
01577     */
01578 
01579      ck0_nomsg(uves_qclog_add_double(qclog,
01580                   "QC FIB1 ABSTRANS",
01581                   cpl_table_get_column_max(order_table,"ORDER"),
01582                   "abs. trans. countrate",
01583                   "%f"));
01584      /*
01585 @p flames_check_sat_lev {wlc} {DRS_PTHRE_MIN} {DRS_PTHRE_MAX}
01586 sat_lev = {q1}
01587 n_hpix  = {q2}
01588      */
01589 
01590     ck0_nomsg(uves_qclog_add_int(qclog,
01591                  "QC NHOTPIX",
01592                  cpl_table_get_column_max(order_table,"ORDER"),
01593                  "no. of hot pixels",
01594                  "%d"));
01595 
01596 
01597     ck0_nomsg(uves_qclog_add_int(qclog,
01598                  "QC PLATENO",
01599                  plate_no,
01600                  "Plate Id.",
01601                  "%d"));
01602   }
01603 
01604   /* The number of orders predicted is already written to another
01605      keyword, and this is not need for QC logging, so it is commented out
01606   uves_qclog_add_int(qclog,"QC NORDGUE",???,"No of predicted orders","%d");
01607   */
01608 
01609  cleanup:
01610   uves_free_table(&xline_table);
01611 
01612   if (cpl_error_get_code() != CPL_ERROR_NONE) {
01613     return -1;
01614 
01615   } else {
01616 
01617     return 0;
01618   }
01619 
01620 
01621 
01622 }
01623 
01624 
01625 
01626 
01636 static int 
01637 uves_physmod_qclog_sc(const double med_dx, 
01638                       const double med_dy,
01639                       const double avg_dx,
01640                       const double avg_dy,
01641                       const uves_propertylist *raw_header,
01642                       const uves_propertylist *ref_header,
01643                       enum  uves_chip chip,
01644                       bool flames,
01645                       const int iter,
01646                       cpl_table* qclog)
01647 {
01648 
01649   char key_value[25];
01650 
01651     if(flames) {
01652         ck0_nomsg(uves_qclog_add_string(qclog,
01653                     "QC TEST2 ID",
01654                     "Fibre-Stability-Check-Results",
01655                     "Name of QC test",
01656                         "%s"));
01657     } else {
01658         ck0_nomsg(uves_qclog_add_string(qclog,
01659                     "QC TEST2 ID",
01660                     "Stability-Check-Results",
01661                     "Name of QC test",
01662                     "%s"));
01663     }
01664     
01665     ck0_nomsg(uves_qclog_add_string(qclog,
01666               "QC MODEL ID",
01667               "UVES_phys_mod/1.1.0",
01668               "Physmod Model Id",
01669               "%s"));
01670     
01671     ck0_nomsg(uves_qclog_add_string(qclog,
01672               "QC MODEL DATE",
01673               "2000:03:18T00:00:00.000",
01674               "Physmod Model Parameters Last Change",
01675               "%s"));
01676 
01677     check_nomsg( uves_qclog_add_common_wave(raw_header,
01678                                             chip, qclog) );
01679     
01680 
01681     ck0_nomsg(uves_qclog_add_string(qclog,
01682               "QC REF PNAME",
01683               uves_pfits_get_arcfile(ref_header),
01684               "Reference file name",
01685               "%s"));
01686 
01687     ck0_nomsg(uves_qclog_add_double(qclog,
01688               "QC AMBI PRES",
01689               uves_pfits_get_ambipress(raw_header),
01690               "Ambient pressure [mm] Hg.",
01691               "%8.4f"));
01692 
01693 
01694     if(flames) {
01695 
01696        sprintf(key_value,"%s%d","QC MODEL ITER",iter);
01697        ck0_nomsg(uves_qclog_add_int(qclog,
01698                  key_value,
01699                  iter,
01700                  "Model iteration",
01701                  "%d"));
01702 
01703 
01704     }
01705 
01706   if(iter == 1) {
01707     ck0_nomsg(uves_qclog_add_double(qclog,
01708               "QC SHFTXAVG",
01709               avg_dx,
01710               "mean shift in x",
01711               "%8.4f"));
01712 
01713     ck0_nomsg(uves_qclog_add_double(qclog,
01714               "QC SHFTXMED",
01715               med_dx,
01716               "median shift in x",
01717               "%8.4f"));
01718 
01719     ck0_nomsg(uves_qclog_add_double(qclog,
01720               "QC SHFTYAVG",
01721               avg_dy,
01722               "mean shift in y",
01723               "%8.4f"));
01724 
01725     ck0_nomsg(uves_qclog_add_double(qclog,
01726               "QC SHFTYMED",
01727               med_dy,
01728               "median shift in y",
01729               "%8.4f"));
01730 
01731   }
01732  cleanup:
01733 
01734   if (cpl_error_get_code() != CPL_ERROR_NONE) {
01735     return -1;
01736   } else {
01737     return 0;
01738   }
01739 }
01740 
01741 
01742 /*---------------------------------------------------------------------------*/
01754 /*---------------------------------------------------------------------------*/
01755 
01756 
01757 static int
01758 flames_get_physmod_shift(const int plate_no,
01759                          const int wavec,
01760                          enum uves_chip chip,
01761                          double* trans_x,
01762                          double* trans_y,
01763                          double* rot_1,
01764                          double* rot_2,
01765                          double* rot_3)
01766 {
01767 
01768   //For the moment rot angle default is assumed always 0,0,0
01769   *rot_1=0;
01770   *rot_2=0;
01771   *rot_3=0;
01772 
01773   uves_msg("plate_no=%d,wavec=%d,chip=%d",plate_no,wavec,chip);
01774   switch(plate_no){
01775 
01776   case 1:
01777     if(chip==UVES_CHIP_REDL) {
01778       switch(wavec){
01779 
01780       case 520:
01781     *trans_x=-15.330;
01782     *trans_y=-40.461;
01783     uves_msg("case 520 REDL pt1");
01784     break;
01785 
01786       case 580:
01787     *trans_x=-17.972;
01788     *trans_y=-39.200;
01789     uves_msg("case 580 REDL pt1");
01790     break;
01791 
01792       case 860:
01793     *trans_x=-12.212;
01794     *trans_y=-49.370;
01795     uves_msg("case 860 REDL pt1");
01796     break;
01797 
01798       }
01799     } else {
01800 
01801       switch(wavec){
01802 
01803       case 520:
01804     *trans_x=-14.237;
01805     *trans_y=-40.337;
01806     uves_msg("case 520 REDU pt1");
01807     break;
01808 
01809       case 580:
01810     *trans_x=-14.738;
01811     *trans_y=-38.831;
01812     uves_msg("case 580 REDU pt1");
01813     break;
01814 
01815       case 860:
01816     *trans_x=-08.253;
01817     *trans_y=-45.385;
01818     uves_msg("case 860 REDU pt1");
01819     break;
01820 
01821       }
01822       break;
01823   
01824     }
01825     break;
01826 
01827 
01828   case 2:
01829     if(chip==UVES_CHIP_REDL) {
01830 
01831     case UVES_CHIP_REDL:
01832       switch(wavec){
01833 
01834       case 520:
01835     *trans_x=+10.136;
01836     *trans_y=-41.420;
01837     uves_msg("case 520 REDL pt2");
01838     break;
01839 
01840       case 580:
01841     *trans_x=+09.000;
01842     *trans_y=-38.289;
01843     uves_msg("case 580 REDL pt2");
01844     break;
01845 
01846       case 860:
01847     *trans_x=+16.386;
01848     *trans_y=-47.519;
01849     uves_msg("case 860 REDL pt2");
01850     break;
01851 
01852       }
01853       break;
01854 
01855     } else {
01856 
01857       switch(wavec){
01858 
01859       case 520:
01860     *trans_x=+12.244;
01861     *trans_y=-41.970;
01862     uves_msg("case 520 REDU pt2");
01863     break;
01864 
01865       case 580:
01866     *trans_x=+12.023;
01867     *trans_y=-38.165;
01868     uves_msg("case 580 REDU pt2");
01869     break;
01870 
01871       case 860:
01872     *trans_x=+18.241;
01873     *trans_y=-43.889;
01874     uves_msg("case 860 REDU pt2");
01875     break;
01876 
01877       }
01878       break;
01879 
01880     }
01881     break;
01882 
01883 
01884   default:
01885     *trans_x=0;
01886     *trans_y=0;
01887   
01888 
01889 
01890   }
01891 
01892 
01893   uves_msg("Physical Model shifts trans=%f,%f rot=%f,%f,%f",
01894        *trans_x,*trans_y,*rot_1,*rot_2,*rot_3);
01895   return 0;
01896 }
01897 
01898 
01899 

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