33#include <cr2res_dfs.h>
34#include <cr2res_splice.h>
36#define CR2RES_DETECTOR_SIZE 2048
42static void cr2res_splice_orders_test(
void);
43static void cr2res_splice_test(
void);
54cpl_table * make_trace_wave(){
55 int wave_poly_size = 3;
57 cpl_array * wave = cpl_array_new(wave_poly_size, CPL_TYPE_DOUBLE);
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);
64 cpl_array_set_double(wave, 0, 2000);
65 cpl_array_set_double(wave, 1, 1);
66 cpl_array_set_double(wave, 2, 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);
77 cpl_table_set_int(trace_wave, CR2RES_COL_ORDER, i, 2);
78 cpl_table_set_int(trace_wave, CR2RES_COL_TRACENB, i, 1);
80 cpl_array_set(wave, 0, 500.5);
81 cpl_table_set_array(trace_wave, CR2RES_COL_WAVELENGTH, i, wave);
83 cpl_array_delete(wave);
88cpl_table * make_spectra(cpl_table ** blaze){
90 char * cn1, * cn2, * cn3, * cn4;
91 cpl_table * spectra = cpl_table_new(CR2RES_DETECTOR_SIZE);
92 *blaze = cpl_table_new(CR2RES_DETECTOR_SIZE);
99 cpl_table_new_column(spectra, cn1, CPL_TYPE_DOUBLE);
100 cpl_table_new_column(*blaze, cn1, CPL_TYPE_DOUBLE);
102 cpl_table_new_column(spectra, cn2, CPL_TYPE_DOUBLE);
104 cpl_table_new_column(spectra, cn3, CPL_TYPE_DOUBLE);
105 cpl_table_new_column(*blaze, cn3, CPL_TYPE_DOUBLE);
107 cpl_table_new_column(spectra, cn4, CPL_TYPE_DOUBLE);
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);
113 cpl_table_set_double(spectra, cn3, i, 2);
114 cpl_table_set_double(spectra, cn4, i, 0.1);
116 cpl_table_set_double(*blaze, cn1, i, 1.);
117 cpl_table_set_double(*blaze, cn3, i, 2.);
128static void cr2res_splice_orders_test(){
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*));
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);
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);
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);
159 cpl_bivector ** spliced, ** spliced_err;
160 cpl_vector * spectrum_order;
161 cpl_bivector * first, *last;
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));
177 cpl_test_eq(0, cr2res_splice_orders(wave, spec, uncs, cont, ntotal, &spliced, &spliced_err, &spectrum_order, &first, &last));
181 tmp = cpl_vector_new(CR2RES_DETECTOR_SIZE);
182 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++)
184 cpl_vector_set(tmp, i, 1);
186 for (i = 0; i < ntotal; i++){
187 cpl_test_vector_abs(tmp, cpl_bivector_get_y(spliced[i]), DBL_EPSILON);
189 cpl_vector_delete(tmp);
192 cpl_vector_delete(spectrum_order);
193 cpl_bivector_delete(first);
194 cpl_bivector_delete(last);
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]);
211 cpl_free(spliced_err);
216static void cr2res_splice_test(){
218 cpl_table * trace_wave = make_trace_wave();
220 cpl_table * spectra = make_spectra(&blaze);
221 cpl_bivector * spliced, * spliced_err;
224 tmp = cpl_vector_new(CR2RES_DETECTOR_SIZE);
225 for (i = 0; i < CR2RES_DETECTOR_SIZE; i++)
227 cpl_vector_set(tmp, i, 1);
230 cpl_test_eq(0, cr2res_splice(&spectra, &blaze, &trace_wave, 1, &spliced, &spliced_err));
232 for (i = 0; i < CR2RES_DETECTOR_SIZE - 1; i++){
234 cpl_test(cpl_bivector_get_x_data(spliced)[i] < cpl_bivector_get_x_data(spliced)[i+1]);
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);
241 cpl_test(cpl_bivector_get_y_data(spliced_err)[i] <= 0.1);
244 cpl_bivector_dump(spliced, NULL);
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);
262 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_DEBUG);
264 cr2res_splice_orders_test();
265 cr2res_splice_test();
267 return cpl_test_end(0);
char * cr2res_dfs_SPEC_ERR_colname(int order_idx, int trace)
Get the ERR column name for a given order/trace.
char * cr2res_dfs_SPEC_colname(int order_idx, int trace)
Get the SPEC column name for a given order/trace.
int main(void)
Run the Unit tests.