uves_test_simulate.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/24 09:31:49 $
00023  * $Revision: 1.14 $
00024  * $Name: uves-4_9_1 $
00025  * $Log: uves_test_simulate.c,v $
00026  * Revision 1.14  2010/09/24 09:31:49  amodigli
00027  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00028  *
00029  * Revision 1.12  2007/09/11 17:10:49  amodigli
00030  * added uves_utils_polynomial.h
00031  *
00032  * Revision 1.11  2007/08/30 07:56:05  amodigli
00033  * fixed some doxygen warnings
00034  *
00035  * Revision 1.10  2007/06/26 14:51:00  jmlarsen
00036  * Fixed bug in load_linetable
00037  *
00038  * Revision 1.9  2007/06/20 15:55:37  jmlarsen
00039  * Parametrized assumption about MIDAS format
00040  *
00041  * Revision 1.8  2007/06/20 15:49:59  jmlarsen
00042  * Use MIDAS compatible column names
00043  *
00044  * Revision 1.7  2007/06/20 08:28:53  amodigli
00045  * updated interface to uves_polynomial_convert_from_plist_midas
00046  *
00047  * Revision 1.6  2007/05/22 11:28:14  jmlarsen
00048  * Removed unused variables
00049  *
00050  * Revision 1.5  2007/04/24 12:50:29  jmlarsen
00051  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00052  *
00053  * Revision 1.4  2007/04/10 11:34:48  jmlarsen
00054  * More points per order in order table
00055  *
00056  * Revision 1.3  2007/04/10 07:22:07  jmlarsen
00057  * Changed interface of polynomial_regression_2d()
00058  *
00059  * Revision 1.2  2007/03/15 14:44:51  jmlarsen
00060  * Removed debugging dump
00061  *
00062  * Revision 1.1  2007/03/15 12:47:32  jmlarsen
00063  * Imported sources
00064  *
00065  *
00066  */
00067 
00068 #ifdef HAVE_CONFIG_H
00069 #  include <config.h>
00070 #endif
00071 
00072 /*----------------------------------------------------------------------------*/
00076 /*----------------------------------------------------------------------------*/
00079 /*-----------------------------------------------------------------------------
00080                                 Includes
00081  -----------------------------------------------------------------------------*/
00082 
00083 #include <uves_test_simulate.h>
00084 //#include <uves_utils_polynomial.h>
00085 #include <uves_utils_wrappers.h>
00086 #include <uves_wavecal_utils.h>
00087 #include <uves_error.h>
00088 #include <uves_dfs.h>
00089 
00090 #include <cpl.h>
00091 
00092 /*-----------------------------------------------------------------------------
00093                             Functions prototypes
00094  -----------------------------------------------------------------------------*/
00095 
00096 /*-----------------------------------------------------------------------------
00097                             Implementation
00098  -----------------------------------------------------------------------------*/
00099 
00100 /*----------------------------------------------------------------------------*/
00111 /*----------------------------------------------------------------------------*/
00112 void
00113 create_order_table(cpl_table **ordertable, 
00114                    polynomial **order_locations, 
00115                    cpl_table **tracetable,
00116                    int minorder, 
00117                    int maxorder, 
00118                    int nx)
00119 {
00120     uves_propertylist *header = NULL;
00121     
00122     /* Create polynomial */
00123     assure_nomsg(order_locations != NULL, CPL_ERROR_NULL_INPUT);
00124     
00125     {
00126         const char *data[] = {"",
00127                               "'COEFFI','I*4',1,7,'7I10'",
00128                               "     53889         2         3         2         1         4         5",
00129                               "",
00130                               "'COEFFR','R*4',1,5,'5E14.7'",
00131                               " 4.3300000E+02 4.0880000E+03 1.0000000E+00 2.1000000E+01 0.0000000E+00",
00132                               "",
00133                               "'COEFFD','R*8',1,30,'3E23.15'",
00134                               " -7.097005629698889E+01  4.050908371864904E-02 -2.886756545398909E-06",
00135                               "  5.504345508879626E-10 -5.583004967206025E-14  7.624532125635992E+01",
00136                               " -2.428213567964009E-03  1.819158447566360E-06 -5.090366383338846E-10",
00137                               "  5.198098506055602E-14  3.513177145982783E-01  5.570332137951829E-04",
00138                               " -3.876157463910250E-07  1.113253735718822E-10 -1.132455173423791E-14",
00139                               "  2.977232589499959E-02 -5.389240622889887E-05  3.777456726044612E-08",
00140                               " -1.083863050648735E-11  1.098450510939580E-15 -1.093309039442914E-03",          
00141                               "  2.402609262989674E-06 -1.688416547941747E-09  4.839101712729582E-13",
00142                               " -4.884504488944702E-17  1.919853952642526E-05 -4.004133160220927E-08",
00143                               "  2.816206503824200E-11 -8.051313882805877E-15  8.090579180112579E-19",
00144                               " ",
00145                               "'TAB_IN_OUT_YSHIFT','R*8',1,1,'3E23.15'",
00146                               "  4.180818583555659E+01          ",
00147                               " "};
00148             
00149         header = uves_propertylist_new();
00150         {
00151             unsigned i;
00152             for (i = 0; i < sizeof(data)/sizeof(char *); i++)
00153                 {
00154                     uves_propertylist_append_string(header, "HISTORY",
00155                                                    data[i]);
00156                 }
00157         }
00158             
00159         check_nomsg( *order_locations = uves_polynomial_convert_from_plist_midas(header, 
00160                                                                                  "COEFF",-1));
00161     }
00162 
00163     /* Fill order table */
00164     if (ordertable != NULL)
00165         {
00166             int order;
00167             int row = 0;
00168 
00169             *ordertable = cpl_table_new(5*(maxorder - minorder + 1));
00170             cpl_table_new_column(*ordertable, "Order", CPL_TYPE_INT);
00171             cpl_table_new_column(*ordertable, "X", CPL_TYPE_INT);
00172             cpl_table_new_column(*ordertable, "Yfit", CPL_TYPE_DOUBLE);
00173 
00174             for (order = minorder; order <= maxorder; order++)
00175                 {
00176                     int x[5];
00177                     int i;
00178                     x[0] = (1*nx)/6+1;
00179                     x[1] = (2*nx)/6+1;
00180                     x[2] = (3*nx)/6+1;
00181                     x[3] = (4*nx)/6+1;
00182                     x[4] = (5*nx)/6+1;
00183                     
00184                     for (i = 0; i < 5; i++)
00185                         {
00186                             cpl_table_set_int(*ordertable, "Order", row, order);
00187                             cpl_table_set_int(*ordertable, "X", row, x[i]);
00188                             cpl_table_set_double(*ordertable, "Yfit", row, 
00189                                                  uves_polynomial_evaluate_2d(*order_locations,
00190                                                                              x[i], order));
00191                             row++;
00192                         }
00193                 }
00194         }
00195   
00196   /* Not implemented: */
00197   /* Fill tracetable */
00198     tracetable = tracetable; /* suppress warnings */
00199   
00200  cleanup:
00201   uves_free_propertylist(&header);
00202   return;
00203 }
00204 
00205 /*----------------------------------------------------------------------------*/
00219 /*----------------------------------------------------------------------------*/
00220 void
00221 create_line_table(cpl_table **linetable, 
00222                   polynomial **dispersion, 
00223                   polynomial **abs_orders,
00224                   int *firstabs, 
00225                   int *lastabs,
00226                   int minorder, 
00227                   int maxorder, 
00228                   int nx)
00229 {
00230     polynomial *order_locations = NULL;
00231     cpl_table *ordertable = NULL;
00232     cpl_table *tracetable = NULL;
00233     const bool midas_format = false;
00234 
00235     const char *ORDER = midas_format ? "ORDER" : "Order";
00236     const char *IDENT = midas_format ? "IDENT" : "Ident";
00237     const char *YNEW = midas_format ? "YNEW" : "Ynew";
00238 
00239     create_order_table(&ordertable, &order_locations, &tracetable,
00240                        minorder, maxorder, nx);
00241 
00242     assure_nomsg(linetable != NULL, CPL_ERROR_NULL_INPUT);
00243     
00244     {
00245         int row = 0;
00246         *linetable = cpl_table_new(cpl_table_get_nrow(ordertable));
00247         cpl_table_new_column(*linetable, "X", CPL_TYPE_DOUBLE);
00248         cpl_table_new_column(*linetable, YNEW, CPL_TYPE_DOUBLE);
00249         cpl_table_new_column(*linetable, "Y", CPL_TYPE_INT);
00250         cpl_table_new_column(*linetable, ORDER, CPL_TYPE_INT);
00251         cpl_table_new_column(*linetable, LINETAB_LAMBDAC, CPL_TYPE_DOUBLE);
00252         cpl_table_new_column(*linetable, IDENT, CPL_TYPE_DOUBLE);
00253         cpl_table_new_column(*linetable, "Aux", CPL_TYPE_DOUBLE);
00254         for (row = 0; row < cpl_table_get_nrow(ordertable); row++)
00255             {
00256                 int order = cpl_table_get_int(ordertable, "Order", row, NULL);
00257                 double x  = cpl_table_get_int(ordertable, "X", row, NULL);
00258                 double y  = cpl_table_get_double(ordertable, "Yfit", row, NULL);
00259                 int m = 120 - order; /* absolute order number */
00260                 double lambda = 3000 + 50*(order - minorder) + 80*(x*1.0/nx);
00261                 
00262                 cpl_table_set_double(*linetable, "X", row, x);
00263                 cpl_table_set_double(*linetable, YNEW, row, y);
00264                 cpl_table_set_int(*linetable, "Y", row, order);   /* it's correct! */
00265                 cpl_table_set_int(*linetable, ORDER, row, m);
00266                 cpl_table_set_double(*linetable, LINETAB_LAMBDAC, row, lambda);
00267                 cpl_table_set_double(*linetable, IDENT, row, lambda); /* exact! */
00268                 cpl_table_set_double(*linetable, "Aux", row, lambda*m);
00269             }
00270     }
00271     
00272     if (abs_orders != NULL)
00273         {
00274             /* Create polynomial
00275                absorders(x, y) = m
00276             */
00277             int degree = 2;
00278             check_nomsg(
00279             *abs_orders = uves_polynomial_regression_2d(*linetable,
00280                                                         "X", YNEW, ORDER, NULL,
00281                                                         degree, degree,
00282                                                         NULL, NULL, NULL, /* new columns */
00283                                                         NULL, NULL, /* mse, red_chisq */
00284                                                         NULL, -1, -1)); /* variance, kappa */
00285             if (firstabs != NULL) {
00286                 double x = nx/2;
00287                 double y = 
00288                     uves_polynomial_evaluate_2d(order_locations, x, minorder);
00289                 
00290                 *firstabs = uves_round_double(
00291                     uves_polynomial_evaluate_2d(*abs_orders, x, y));
00292             }
00293             if (lastabs != NULL) {
00294                 double x = nx/2;
00295                 double y = 
00296                     uves_polynomial_evaluate_2d(order_locations, x, maxorder);
00297                 
00298                 *lastabs = uves_round_double(
00299                     uves_polynomial_evaluate_2d(*abs_orders, x, y));
00300             }
00301 
00302         }
00303 
00304     if (dispersion != NULL)
00305         {
00306             /* Create polynomial of this form
00307                f(x, m) = lambda m
00308             */
00309             int degree = 2;
00310             check_nomsg(
00311             *dispersion = uves_polynomial_regression_2d(*linetable,
00312                                                         "X", ORDER, "Aux", NULL,
00313                                                         degree, degree,
00314                                                         NULL, NULL, NULL, /* new columns */
00315                                                         NULL, NULL, /* mse, red_chisq */
00316                                                         NULL, -1, -1)); /* variance, kappa */
00317         }
00318     
00319  cleanup:
00320     uves_polynomial_delete(&order_locations);
00321     uves_free_table(&ordertable);
00322     uves_free_table(&tracetable);
00323     return;
00324 }

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