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

Generated on Thu Nov 15 14:32:31 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1