CR2RE Pipeline Reference Manual 1.6.8
cr2res_splice-test.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <stdlib.h>
29#include <string.h>
30#include <math.h>
31#include <cpl.h>
32#include <hdrl.h>
33#include <cr2res_dfs.h>
34#include <cr2res_splice.h>
35
36#define CR2RES_DETECTOR_SIZE 2048
37
38/*-----------------------------------------------------------------------------
39 Functions prototypes
40 -----------------------------------------------------------------------------*/
41
42static void cr2res_splice_orders_test(void);
43static void cr2res_splice_test(void);
44
45
46/*----------------------------------------------------------------------------*/
51/*----------------------------------------------------------------------------*/
54cpl_table * make_trace_wave(){
55 int wave_poly_size = 3;
56 int i;
57 cpl_array * wave = cpl_array_new(wave_poly_size, CPL_TYPE_DOUBLE);
58 // columns: order, trace, wavelength
59 cpl_table * trace_wave = cpl_table_new(2);
60 cpl_table_new_column(trace_wave, CR2RES_COL_ORDER, CPL_TYPE_INT);
61 cpl_table_new_column(trace_wave, CR2RES_COL_TRACENB, CPL_TYPE_INT);
62 cpl_table_new_column_array(trace_wave, CR2RES_COL_WAVELENGTH, CPL_TYPE_DOUBLE, wave_poly_size);
63
64 cpl_array_set_double(wave, 0, 2000);
65 cpl_array_set_double(wave, 1, 1);
66 cpl_array_set_double(wave, 2, 0.);
67
68 // first order
69 i = 0;
70 cpl_table_set_int(trace_wave, CR2RES_COL_ORDER, i, 1);
71 cpl_table_set_int(trace_wave, CR2RES_COL_TRACENB, i, 1);
72 cpl_table_set_array(trace_wave, CR2RES_COL_WAVELENGTH, i, wave);
73
74
75 // second order
76 i = 1;
77 cpl_table_set_int(trace_wave, CR2RES_COL_ORDER, i, 2);
78 cpl_table_set_int(trace_wave, CR2RES_COL_TRACENB, i, 1);
79
80 cpl_array_set(wave, 0, 500.5);
81 cpl_table_set_array(trace_wave, CR2RES_COL_WAVELENGTH, i, wave);
82
83 cpl_array_delete(wave);
84
85 return trace_wave;
86}
87
88cpl_table * make_spectra(cpl_table ** blaze){
89 cpl_size i;
90 char * cn1, * cn2, * cn3, * cn4; // column names
91 cpl_table * spectra = cpl_table_new(CR2RES_DETECTOR_SIZE);
92 *blaze = cpl_table_new(CR2RES_DETECTOR_SIZE);
93
94 cn1 = cr2res_dfs_SPEC_colname(1, 1);
96 cn3 = cr2res_dfs_SPEC_colname(2, 1);
98
99 cpl_table_new_column(spectra, cn1, CPL_TYPE_DOUBLE);
100 cpl_table_new_column(*blaze, cn1, CPL_TYPE_DOUBLE);
101
102 cpl_table_new_column(spectra, cn2, CPL_TYPE_DOUBLE);
103
104 cpl_table_new_column(spectra, cn3, CPL_TYPE_DOUBLE);
105 cpl_table_new_column(*blaze, cn3, CPL_TYPE_DOUBLE);
106
107 cpl_table_new_column(spectra, cn4, CPL_TYPE_DOUBLE);
108
109 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++){
110 cpl_table_set_double(spectra, cn1, i, 1);
111 cpl_table_set_double(spectra, cn2, i, 0.1);
112
113 cpl_table_set_double(spectra, cn3, i, 2);
114 cpl_table_set_double(spectra, cn4, i, 0.1);
115
116 cpl_table_set_double(*blaze, cn1, i, 1.);
117 cpl_table_set_double(*blaze, cn3, i, 2.);
118 }
119
120 cpl_free(cn1);
121 cpl_free(cn2);
122 cpl_free(cn3);
123 cpl_free(cn4);
124
125 return spectra;
126}
127
128static void cr2res_splice_orders_test(){
129
130 int i = 0, j = 0;
131 int ntotal = 2;
132
133 cpl_vector ** wave = cpl_malloc(ntotal * sizeof(cpl_vector*));
134 cpl_vector ** spec = cpl_malloc(ntotal * sizeof(cpl_vector*));
135 cpl_vector ** uncs = cpl_malloc(ntotal * sizeof(cpl_vector*));
136 cpl_vector ** cont = cpl_malloc(ntotal * sizeof(cpl_vector*));
137 cpl_vector * tmp;
138
139 for (j = 0; j < ntotal; j++){
140 wave[j] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
141 spec[j] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
142 uncs[j] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
143 cont[j] = cpl_vector_new(CR2RES_DETECTOR_SIZE);
144 }
145
146 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++){
147 cpl_vector_set(wave[0], i, 2000 + i);
148 cpl_vector_set(spec[0], i, 1);
149 cpl_vector_set(uncs[0], i, 0.1);
150 cpl_vector_set(cont[0], i, 1);
151
152
153 cpl_vector_set(wave[1], i, 500.5 + i);
154 cpl_vector_set(spec[1], i, 1);
155 cpl_vector_set(uncs[1], i, 0.1);
156 cpl_vector_set(cont[1], i, 1);
157 }
158
159 cpl_bivector ** spliced, ** spliced_err;
160 cpl_vector * spectrum_order;
161 cpl_bivector * first, *last;
162
163 // Test NULL input
164 cpl_test_eq(-1, cr2res_splice_orders(NULL, spec, uncs, cont, ntotal, &spliced, &spliced_err, &spectrum_order, &first, &last));
165 cpl_test_eq(-1, cr2res_splice_orders(wave, NULL, uncs, cont, ntotal, &spliced, &spliced_err, &spectrum_order, &first, &last));
166 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, NULL, cont, ntotal, &spliced, &spliced_err, &spectrum_order, &first, &last));
167 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, NULL, ntotal, &spliced, &spliced_err, &spectrum_order, &first, &last));
168 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, cont, 0, &spliced, &spliced_err, &spectrum_order, &first, &last));
169 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, NULL, &spliced_err, &spectrum_order, &first, &last));
170 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, &spliced, NULL, &spectrum_order, &first, &last));
171 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, &spliced, &spliced_err, NULL, &first, &last));
172 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, &spliced, &spliced_err, &spectrum_order, NULL, &last));
173 cpl_test_eq(-1, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, &spliced, &spliced_err, &spectrum_order, &first, NULL));
174
175
176 // Test proper values
177 cpl_test_eq(0, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, &spliced, &spliced_err, &spectrum_order, &first, &last));
178
179 // Check results, this depends on the initial values of course
180 // Here the spectrum was equal to the wavelength, this should still be the case after splicing
181 tmp = cpl_vector_new(CR2RES_DETECTOR_SIZE);
182 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++)
183 {
184 cpl_vector_set(tmp, i, 1);
185 }
186 for (i = 0; i < ntotal; i++){
187 cpl_test_vector_abs(tmp, cpl_bivector_get_y(spliced[i]), DBL_EPSILON);
188 }
189 cpl_vector_delete(tmp);
190
191 // Delete all memory
192 cpl_vector_delete(spectrum_order);
193 cpl_bivector_delete(first);
194 cpl_bivector_delete(last);
195
196 for (i = 0; i < ntotal; i++){
197 cpl_bivector_delete(spliced[i]);
198 cpl_bivector_delete(spliced_err[i]);
199 cpl_vector_delete(wave[i]);
200 cpl_vector_delete(spec[i]);
201 cpl_vector_delete(uncs[i]);
202 cpl_vector_delete(cont[i]);
203 }
204
205 cpl_free(wave);
206 cpl_free(spec);
207 cpl_free(uncs);
208 cpl_free(cont);
209
210 cpl_free(spliced);
211 cpl_free(spliced_err);
212
213 return;
214}
215
216static void cr2res_splice_test(){
217 int i = 0;
218 cpl_table * trace_wave = make_trace_wave();
219 cpl_table * blaze;
220 cpl_table * spectra = make_spectra(&blaze);
221 cpl_bivector * spliced, * spliced_err;
222
223 cpl_vector * tmp;
224 tmp = cpl_vector_new(CR2RES_DETECTOR_SIZE);
225 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++)
226 {
227 cpl_vector_set(tmp, i, 1);
228 }
229
230 cpl_test_eq(0, cr2res_splice(&spectra, &blaze, &trace_wave, 1, &spliced, &spliced_err));
231
232 for (i = 0; i < CR2RES_DETECTOR_SIZE - 1; i++){
233 // Wavelength grid is increasing
234 cpl_test(cpl_bivector_get_x_data(spliced)[i] < cpl_bivector_get_x_data(spliced)[i+1]);
235
236 // Here: spectrum == wavelength grid, i.e. also increasing
237 cpl_test(cpl_bivector_get_y_data(spliced)[i] == cpl_bivector_get_y_data(spliced)[i+1]);
238 cpl_test_abs(cpl_vector_get(tmp, i), cpl_bivector_get_y_data(spliced)[i], DBL_EPSILON);
239
240 // Here: Error <= 0.1, initial error
241 cpl_test(cpl_bivector_get_y_data(spliced_err)[i] <= 0.1);
242 }
243
244 cpl_bivector_dump(spliced, NULL);
245
246 cpl_table_delete(trace_wave);
247 cpl_table_delete(blaze);
248 cpl_table_delete(spectra);
249 cpl_bivector_delete(spliced);
250 cpl_bivector_delete(spliced_err);
251 cpl_vector_delete(tmp);
252
253}
254
255/*----------------------------------------------------------------------------*/
259/*----------------------------------------------------------------------------*/
260int main(void)
261{
262 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_DEBUG);
263
264 cr2res_splice_orders_test();
265 cr2res_splice_test();
266
267 return cpl_test_end(0);
268}
269
char * cr2res_dfs_SPEC_ERR_colname(int order_idx, int trace)
Get the ERR column name for a given order/trace.
Definition: cr2res_dfs.c:414
char * cr2res_dfs_SPEC_colname(int order_idx, int trace)
Get the SPEC column name for a given order/trace.
Definition: cr2res_dfs.c:378
int main(void)
Run the Unit tests.