X-shooter Pipeline Reference Manual 3.8.15
test-xsh_bspline.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-09-14 07:16:32 $
23 * $Revision: 1.2 $
24 */
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*-------------------------------------------------------------------------*/
36/*-------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------
40 Includes
41 --------------------------------------------------------------------------*/
42
43#include <tests.h>
44
45#include <xsh_error.h>
46#include <xsh_msg.h>
47
48/*
49#include <xsh_data_pre.h>
50#include <xsh_data_instrument.h>
51#include <xsh_data_rec.h>
52#include <xsh_data_localization.h>
53#include <xsh_drl.h>
54#include <xsh_pfits.h>
55
56#include <xsh_badpixelmap.h>
57#include <getopt.h>
58*/
59#include <cpl.h>
60
61
62#include <stdio.h>
63#include <stdlib.h>
64#include <math.h>
65#include <gsl/gsl_bspline.h>
66#include <gsl/gsl_multifit.h>
67#include <gsl/gsl_rng.h>
68#include <gsl/gsl_randist.h>
69#include <gsl/gsl_statistics.h>
70
71/*--------------------------------------------------------------------------
72 Defines
73 --------------------------------------------------------------------------*/
74
75#define MODULE_ID "XSH_BSPLINE"
76
77/* number of data points to fit */
78#define N 200
79
80/* number of fit coefficients */
81#define NCOEFFS 12
82
83/* nbreak = ncoeffs +2 -k = ncoeffs -2 since k = 4 */
84#define NBREAK (NCOEFFS -2)
85
86
87int
88test_gsl_table(const char* fname) {
89
90 cpl_table* t=NULL;
91 double* w=NULL;
92 double* f=NULL;
93 double* e=NULL;
94
95 t=cpl_table_load(fname,1,0);
96
97 w=cpl_table_get_data_double(t,"WAVE");
98 f=cpl_table_get_data_double(t,"FLUX");
99 e=cpl_table_get_data_double(t,"ERR");
100
101
102
103
104}
105
107
108 const size_t n = N;
109 const size_t ncoeffs = NCOEFFS;
110 const size_t nbreak = NBREAK;
111 size_t i, j;
112 gsl_bspline_workspace *bw;
113 gsl_vector *B;
114 double dy;
115 gsl_rng *r;
116 gsl_vector *c, *w;
117 gsl_vector *x, *y;
118 gsl_matrix *X, *cov;
119 gsl_multifit_linear_workspace *mw;
120 double chisq, dof;
121 //double tss, Rsq;
122
123
124 gsl_rng_env_setup();
125 r = gsl_rng_alloc(gsl_rng_default);
126
127 /* allocate a cubic bspline workspace (k = 4) */
128 bw = gsl_bspline_alloc(4, nbreak);
129 B = gsl_vector_alloc(ncoeffs);
130
131 x = gsl_vector_alloc(n);
132 y = gsl_vector_alloc(n);
133 X = gsl_matrix_alloc(n, ncoeffs);
134 c = gsl_vector_alloc(ncoeffs);
135 w = gsl_vector_alloc(n);
136 cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
137
138 printf("#m=0,S=0\n");
139 /* this is the data to be fitted */
140 for (i = 0; i < n; ++i)
141 {
142 double sigma;
143 double xi = (15.0 / (N - 1)) * i;
144 double yi = cos(xi) * exp(-0.1 * xi);
145
146 sigma = 0.1 * yi;
147 dy = gsl_ran_gaussian(r, sigma);
148 yi += dy;
149
150 gsl_vector_set(x, i, xi);
151 gsl_vector_set(y, i, yi);
152 gsl_vector_set(w, i, 1.0 / (sigma * sigma));
153
154 //printf("%f %f\n", xi, yi);
155 }
156
157 /* use uniform breakpoints on [0, 15] */
158 gsl_bspline_knots_uniform(0.0, 15.0, bw);
159
160 /* construct the fit matrix X */
161 for (i = 0; i < n; ++i)
162 {
163 double xi = gsl_vector_get(x, i);
164
165 /* compute B_j(xi) for all j */
166 gsl_bspline_eval(xi, B, bw);
167
168 /* fill in row i of X */
169 for (j = 0; j < ncoeffs; ++j)
170 {
171 double Bj = gsl_vector_get(B, j);
172 gsl_matrix_set(X, i, j, Bj);
173 }
174 }
175 mw = gsl_multifit_linear_alloc(n, ncoeffs);
176
177 /* do the fit */
178 printf("ok2 n=%d mw->n=%d\n",n,mw->n);
179 mw->n=X->size1;
180 printf("ok2 n=%d mw->n=%d\n",n,mw->n);
181 printf("ok3 p=%d mw->p=%d\n",ncoeffs,mw->p);
182 mw->p=X->size2;
183 printf("ok3 p=%d mw->p=%d\n",ncoeffs,mw->p);
184
185 gsl_multifit_wlinear(X, w, y, c, cov, &chisq, mw);
186
187
188 dof = n - ncoeffs;
189 /*
190 tss = gsl_stats_wtss(w->data, 1, y->data, 1, y->size);
191 Rsq = 1.0 - chisq / tss;
192
193 fprintf(stderr, "chisq/dof = %e, Rsq = %f\n",
194 chisq / dof, Rsq);
195 */
196 /* output the smoothed curve */
197 {
198 double xi, yi, yerr;
199
200 printf("#m=1,S=0\n");
201 for (xi = 0.0; xi < 15.0; xi += 0.1)
202 {
203 gsl_bspline_eval(xi, B, bw);
204 gsl_multifit_linear_est(B, c, cov, &yi, &yerr);
205 //printf("%f %f\n", xi, yi);
206 }
207 }
208
209 gsl_rng_free(r);
210 gsl_bspline_free(bw);
211 gsl_vector_free(B);
212 gsl_vector_free(x);
213 gsl_vector_free(y);
214 gsl_matrix_free(X);
215 gsl_vector_free(c);
216 gsl_vector_free(w);
217 gsl_matrix_free(cov);
218 gsl_multifit_linear_free(mw);
219
220}
221
229int main(int argc, char * const argv[])
230{
231 /* Declarations */
232 int ret =0;
233 const char* file_on = NULL ;
234 /* Initialize libraries */
235 cpl_test_init("amodigli@eso.org", CPL_MSG_WARNING);
236
237 if (argc == 1)
238 {
240 }
241 else if(argc == 2)
242 {
243 file_on = argv[1];
244
245 } else {
246 fprintf(stderr, "usage: %s table.fits\n", argv[0]);
247 return 0;
248 }
249
250
251 if (cpl_error_get_code() != CPL_ERROR_NONE) {
252 xsh_error_dump(CPL_MSG_ERROR);
253 ret= 1;
254 }
255 cpl_test_end(0);
256 return ret ;
257}
258
#define NBREAK
#define N
#define NCOEFFS
int test_gsl_table(const char *fname)
int test_gsl_example()
int main()
Unit test of xsh_bspline_interpol.
static double sigma
#define xsh_error_dump(level)
Definition: xsh_error.h:92
int * y
int * x
int n
Definition: xsh_detmon_lg.c:92