46 #include "gravi_data.h" 47 #include "gravi_dfs.h" 48 #include "gravi_pfits.h" 49 #include "gravi_cpl.h" 51 #include "gravi_utils.h" 53 #include "gravi_calib.h" 54 #include "gravi_vis.h" 56 #include "gravi_eop.h" 62 double pythag(
double ,
double );
64 cpl_matrix * svdcmp(cpl_matrix * , cpl_vector * , cpl_matrix * );
71 int gravi_array_threshold_min (cpl_array * array,
double lo_cut,
74 cpl_ensure (array, CPL_ERROR_NULL_INPUT, -1);
76 cpl_size size = cpl_array_get_size (array);
79 for (cpl_size s = 0; s < size ; s++) {
80 if (cpl_array_get (array, s, NULL) < lo_cut) {
81 cpl_array_set (array, s, assign_lo_cut);
96 cpl_ensure (n>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
97 cpl_ensure (size>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
100 output = cpl_malloc (n *
sizeof(cpl_array *));
103 if ( type == CPL_TYPE_DOUBLE )
104 for ( out1 = 0 ; out1 < n ; out1++ ) {
105 output[out1] = cpl_array_new (size, type);
106 cpl_array_fill_window_double (output[out1], 0, size, 0.0);
108 else if ( type == CPL_TYPE_DOUBLE_COMPLEX )
109 for ( out1 = 0 ; out1 < n ; out1++ ) {
110 output[out1] = cpl_array_new (size, type);
111 cpl_array_fill_window_double_complex (output[out1], 0, size, 0.0 * I*0.0);
114 cpl_error_set_message (cpl_func,CPL_ERROR_ILLEGAL_INPUT,
"This type is not supported.");
115 FREE (cpl_free, output);
122 cpl_error_code gravi_table_interpolate_column (cpl_table * to_table,
125 const cpl_table * from_table,
129 gravi_msg_function_start(1);
130 cpl_ensure_code (to_table, CPL_ERROR_NULL_INPUT);
131 cpl_ensure_code (to_x, CPL_ERROR_NULL_INPUT);
132 cpl_ensure_code (to_y, CPL_ERROR_NULL_INPUT);
133 cpl_ensure_code (from_table, CPL_ERROR_NULL_INPUT);
134 cpl_ensure_code (from_x, CPL_ERROR_NULL_INPUT);
135 cpl_ensure_code (from_y, CPL_ERROR_NULL_INPUT);
138 cpl_size nxref = cpl_table_get_nrow (from_table);
139 cpl_size nxout = cpl_table_get_nrow (to_table);
142 cpl_vector * xref = cpl_vector_new (nxref);
143 for (cpl_size row = 0; row < nxref; row++) {
144 cpl_vector_set (xref, row, cpl_table_get (from_table, from_x, row, NULL));
145 CPLCHECK_MSG (
"Cannot get x data");
149 cpl_vector * xout = cpl_vector_new (nxout);
150 for (cpl_size row = 0; row < nxout; row++) {
151 cpl_vector_set (xout, row, cpl_table_get (to_table, to_x, row, NULL));
152 CPLCHECK_MSG (
"Cannot get x data");
156 cpl_vector * yref = cpl_vector_new (nxref);
157 cpl_vector * yout = cpl_vector_new (nxout);
158 cpl_bivector * fref = cpl_bivector_wrap_vectors (xref, yref);
159 cpl_bivector * fout = cpl_bivector_wrap_vectors (xout, yout);
162 cpl_size depth = cpl_table_get_column_depth (from_table, from_y);
164 cpl_error_set_message (cpl_func,CPL_ERROR_INVALID_TYPE ,
165 "Report this error to gravity.drs");
168 const char * unit = cpl_table_get_column_unit (from_table, from_y);
169 gravi_table_init_column_array (to_table, to_y, unit, CPL_TYPE_DOUBLE, depth);
172 for (cpl_size size = 0; size < depth; size++) {
175 for (cpl_size row = 0; row < nxref; row++) {
176 double value = gravi_table_get_value (from_table, from_y, row, size);
177 cpl_vector_set (yref, row, value);
178 CPLCHECK_MSG (
"Cannot get y data");
182 cpl_bivector_interpolate_linear (fout, fref);
183 CPLCHECK_MSG (
"Cannot interpolate");
186 for (cpl_size row = 0; row < nxout; row++) {
187 double value = cpl_vector_get (yout, row);
188 gravi_table_set_value (to_table, to_y, row, size, value);
189 CPLCHECK_MSG (
"Cannot set y data");
195 FREE (cpl_bivector_delete, fref);
196 FREE (cpl_bivector_delete, fout);
198 gravi_msg_function_exit(1);
199 return CPL_ERROR_NONE;
202 double gravi_table_get_column_mean (cpl_table * table,
const char * name,
int base,
int nbase)
204 cpl_ensure (table, CPL_ERROR_NULL_INPUT, 0.0);
205 cpl_ensure (name, CPL_ERROR_NULL_INPUT, 0.0);
208 cpl_size nrow = cpl_table_get_nrow (table) / nbase;
209 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, 0.0);
211 cpl_type type = cpl_table_get_column_type (table, name);
212 cpl_size depth = cpl_table_get_column_depth (table, name);
214 if (depth == 0 && type == CPL_TYPE_DOUBLE) {
215 double * data = cpl_table_get_data_double (table, name);
216 cpl_ensure (data, CPL_ERROR_ILLEGAL_INPUT, 0.0);
217 for (cpl_size r=0; r<nrow;r++) mean += data[r*nbase+base];
219 else if (depth == 0 && type == CPL_TYPE_FLOAT) {
220 float * data = cpl_table_get_data_float (table, name);
221 cpl_ensure (data, CPL_ERROR_ILLEGAL_INPUT, 0.0);
222 for (cpl_size r=0; r<nrow;r++) mean += data[r*nbase+base];
224 else if (depth == 0 && type == CPL_TYPE_INT) {
225 int * data = cpl_table_get_data_int (table, name);
226 cpl_ensure (data, CPL_ERROR_ILLEGAL_INPUT, 0.0);
227 for (cpl_size r=0; r<nrow;r++) mean += data[r*nbase+base];
229 else if (depth > 0) {
230 cpl_array ** arrays = cpl_table_get_data_array (table, name);
231 cpl_ensure (arrays, CPL_ERROR_ILLEGAL_INPUT, 0.0);
232 cpl_array * output = cpl_array_duplicate (arrays[base]);
233 cpl_ensure (output, CPL_ERROR_ILLEGAL_INPUT, 0.0);
234 for (cpl_size r=1; r<nrow;r++)
235 cpl_array_add (output, arrays[r*nbase+base]);
236 mean = cpl_array_get_mean (output) / nrow;
237 FREE (cpl_array_delete, output);
240 cpl_error_set_message (cpl_func,CPL_ERROR_ILLEGAL_INPUT,
"unknow type");
247 double gravi_table_get_column_std (cpl_table * table,
const char * name,
int base,
int nbase)
249 cpl_ensure (table, CPL_ERROR_NULL_INPUT, 0.0);
250 cpl_ensure (name, CPL_ERROR_NULL_INPUT, 0.0);
254 cpl_size nrow = cpl_table_get_nrow (table) / nbase;
255 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, 0.0);
257 cpl_type type = cpl_table_get_column_type (table, name);
258 cpl_size depth = cpl_table_get_column_depth (table, name);
260 if (depth == 0 && type == CPL_TYPE_DOUBLE) {
261 double * data = cpl_table_get_data_double (table, name);
262 cpl_ensure (data, CPL_ERROR_ILLEGAL_INPUT, 0.0);
263 for (cpl_size r=0; r<nrow;r++) mean += data[r*nbase+base];
264 for (cpl_size r=0; r<nrow;r++) mean2 += data[r*nbase+base] * data[r*nbase+base];
266 else if (depth == 0 && type == CPL_TYPE_FLOAT) {
267 float * data = cpl_table_get_data_float (table, name);
268 cpl_ensure (data, CPL_ERROR_ILLEGAL_INPUT, 0.0);
269 for (cpl_size r=0; r<nrow;r++) mean += data[r*nbase+base];
270 for (cpl_size r=0; r<nrow;r++) mean2 += data[r*nbase+base] * data[r*nbase+base];
272 else if (depth == 0 && type == CPL_TYPE_INT) {
273 int * data = cpl_table_get_data_int (table, name);
274 cpl_ensure (data, CPL_ERROR_ILLEGAL_INPUT, 0.0);
275 for (cpl_size r=0; r<nrow;r++) mean += data[r*nbase+base];
276 for (cpl_size r=0; r<nrow;r++) mean2 += data[r*nbase+base] * data[r*nbase+base];
279 cpl_error_set_message (cpl_func,CPL_ERROR_ILLEGAL_INPUT,
"unknow type");
283 return sqrt (mean2 / nrow - mean*mean / nrow / nrow);
287 cpl_array * gravi_table_get_column_mean_array (cpl_table * table,
const char * name,
int base,
int nbase)
289 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
290 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
292 cpl_size nrow = cpl_table_get_nrow (table) / nbase;
293 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, NULL);
296 cpl_array ** arrays = cpl_table_get_data_array (table, name);
297 cpl_ensure (arrays, CPL_ERROR_ILLEGAL_INPUT, NULL);
300 cpl_array * output = cpl_array_duplicate (arrays[base]);
301 for (cpl_size r=1; r<nrow;r++)
302 cpl_array_add (output, arrays[r*nbase+base]);
304 cpl_array_divide_scalar (output, nrow);
309 double ** gravi_table_get_data_array_double(cpl_table * table,
const char * name)
311 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
312 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
314 cpl_size nrow = cpl_table_get_nrow (table);
315 cpl_array ** pdata = cpl_table_get_data_array (table, name);
317 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, NULL);
318 cpl_ensure (pdata, CPL_ERROR_ILLEGAL_INPUT, NULL);
321 double ** data = cpl_malloc (
sizeof(
double*) * nrow);
324 for (cpl_size row=0; row<nrow; row++) {
325 data[row] = cpl_array_get_data_double (pdata[row]);
328 CPLCHECK_NUL(
"Cannot load the requested arrays");
333 float ** gravi_table_get_data_array_float(cpl_table * table,
const char * name)
335 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
336 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
338 cpl_size nrow = cpl_table_get_nrow (table);
339 cpl_array ** pdata = cpl_table_get_data_array (table, name);
341 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, NULL);
342 cpl_ensure (pdata, CPL_ERROR_ILLEGAL_INPUT, NULL);
345 float ** data = cpl_malloc (
sizeof(
float*) * nrow);
348 for (cpl_size row=0; row<nrow; row++) {
349 data[row] = cpl_array_get_data_float (pdata[row]);
352 CPLCHECK_NUL(
"Cannot load the requested arrays");
357 float complex ** gravi_table_get_data_array_float_complex (cpl_table * table,
const char * name)
359 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
360 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
362 cpl_size nrow = cpl_table_get_nrow (table);
363 cpl_array ** pdata = cpl_table_get_data_array (table, name);
365 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, NULL);
366 cpl_ensure (pdata, CPL_ERROR_ILLEGAL_INPUT, NULL);
369 float complex ** data = cpl_malloc (
sizeof(
float complex*) * nrow);
372 for (cpl_size row=0; row<nrow; row++) {
373 data[row] = cpl_array_get_data_float_complex (pdata[row]);
376 CPLCHECK_NUL (
"Cannot load the requested arrays");
382 double complex ** gravi_table_get_data_array_double_complex (cpl_table * table,
const char * name)
384 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
385 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
387 cpl_size nrow = cpl_table_get_nrow (table);
388 cpl_array ** pdata = cpl_table_get_data_array (table, name);
390 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, NULL);
391 cpl_ensure (pdata, CPL_ERROR_ILLEGAL_INPUT, NULL);
394 double complex ** data = cpl_malloc (
sizeof(
double complex*) * nrow);
397 for (cpl_size row=0; row<nrow; row++) {
398 data[row] = cpl_array_get_data_double_complex (pdata[row]);
401 CPLCHECK_NUL (
"Cannot load the requested arrays");
406 int ** gravi_table_get_data_array_int(cpl_table * table,
const char * name)
408 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
409 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
411 cpl_size nrow = cpl_table_get_nrow (table);
412 cpl_array ** pdata = cpl_table_get_data_array (table, name);
414 cpl_ensure (nrow, CPL_ERROR_ILLEGAL_INPUT, NULL);
415 cpl_ensure (pdata, CPL_ERROR_ILLEGAL_INPUT, NULL);
418 int ** data = cpl_malloc (
sizeof(
int*) * nrow);
421 for (cpl_size row=0; row<nrow; row++) {
422 data[row] = cpl_array_get_data_int (pdata[row]);
425 CPLCHECK_NUL(
"Cannot load the requested arrays");
433 cpl_array * gravi_array_init_double (
long n ,
double value)
435 cpl_ensure (n>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
436 cpl_array * output = cpl_array_new (n, CPL_TYPE_DOUBLE);
437 cpl_array_fill_window_double (output, 0, n, value);
444 cpl_array * gravi_array_init_int (
long n,
int value)
446 cpl_ensure (n>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
447 cpl_array * output = cpl_array_new (n, CPL_TYPE_INT);
448 cpl_array_fill_window_int (output, 0, n, value);
455 cpl_array * gravi_array_init_double_complex (
long n,
double complex value)
457 cpl_ensure (n>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
458 cpl_array * output = cpl_array_new (n, CPL_TYPE_DOUBLE_COMPLEX);
459 cpl_array_fill_window_double_complex (output, 0, n, value);
466 cpl_array * gravi_array_init_float_complex (
long n,
float complex value)
468 cpl_ensure (n>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
469 cpl_array * output = cpl_array_new (n, CPL_TYPE_FLOAT_COMPLEX);
470 cpl_array_fill_window_float_complex (output, 0, n, value);
477 cpl_array * gravi_array_wrap_complex (cpl_array * input_re, cpl_array * input_im)
479 cpl_ensure (input_re, CPL_ERROR_NULL_INPUT, NULL);
480 cpl_ensure (input_im, CPL_ERROR_NULL_INPUT, NULL);
482 cpl_size size_re = cpl_array_get_size (input_re);
483 cpl_size size_im = cpl_array_get_size (input_im);
485 cpl_ensure (size_re == size_im, CPL_ERROR_ILLEGAL_INPUT, NULL);
487 cpl_array * output = cpl_array_new (size_re, CPL_TYPE_DOUBLE_COMPLEX);
489 for (cpl_size n = 0; n < size_re; n ++) {
490 cpl_array_set_complex (output, n, 1.* I * cpl_array_get (input_im, n, NULL) +
491 cpl_array_get (input_re, n, NULL));
500 cpl_array * gravi_array_wrap_float_complex (cpl_array * input_re, cpl_array * input_im)
502 cpl_ensure (input_re, CPL_ERROR_NULL_INPUT, NULL);
503 cpl_ensure (input_im, CPL_ERROR_NULL_INPUT, NULL);
505 cpl_size size_re = cpl_array_get_size (input_re);
506 cpl_size size_im = cpl_array_get_size (input_im);
508 cpl_ensure (size_re == size_im, CPL_ERROR_ILLEGAL_INPUT, NULL);
510 cpl_array * output = cpl_array_new (size_re, CPL_TYPE_FLOAT_COMPLEX);
513 for (cpl_size n = 0; n < size_re; n ++) {
514 cpl_array_set_float_complex (output, n, 1.* I * cpl_array_get (input_im, n, &nv) +
515 cpl_array_get (input_re, n, &nv));
524 cpl_array * gravi_array_compute_norm2 (cpl_array * input_re, cpl_array * input_im)
526 cpl_ensure (input_re, CPL_ERROR_NULL_INPUT, NULL);
527 cpl_ensure (input_im, CPL_ERROR_NULL_INPUT, NULL);
529 cpl_size size_re = cpl_array_get_size (input_re);
530 cpl_size size_im = cpl_array_get_size (input_im);
532 cpl_ensure (size_re == size_im, CPL_ERROR_ILLEGAL_INPUT, NULL);
534 cpl_array * output = cpl_array_new (size_re, CPL_TYPE_DOUBLE);
535 cpl_array_fill_window_double (output, 0, size_re, 0.0);
538 for (cpl_size n = 0; n < size_re; n ++) {
539 cpl_array_set_double (output, n, pow(cpl_array_get (input_im, n, &nv),2) +
540 pow(cpl_array_get (input_re, n, &nv),2) );
550 cpl_error_code gravi_table_set_array_double_complex (cpl_table * table,
556 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
557 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
558 cpl_ensure_code (visR, CPL_ERROR_NULL_INPUT);
559 cpl_ensure_code (visI, CPL_ERROR_NULL_INPUT);
561 cpl_array * tmp_cast = gravi_array_wrap_complex (visR, visI);
562 cpl_table_set_array (table, name, row, tmp_cast);
563 cpl_array_delete (tmp_cast);
565 CPLCHECK_MSG(
"Cannot set float_complex array");
567 return CPL_ERROR_NONE;
574 cpl_error_code gravi_table_set_array_phase (cpl_table * table,
const char * name, cpl_size row, cpl_array * phase)
576 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
577 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
578 cpl_ensure_code (phase, CPL_ERROR_NULL_INPUT);
580 cpl_array * tmp_cast;
581 if (cpl_array_get_type (phase) == CPL_TYPE_FLOAT_COMPLEX ||
582 cpl_array_get_type (phase) == CPL_TYPE_DOUBLE_COMPLEX ) {
583 tmp_cast = cpl_array_cast (phase, CPL_TYPE_DOUBLE_COMPLEX);
584 cpl_array_arg (tmp_cast);
586 tmp_cast = cpl_array_cast (phase, CPL_TYPE_DOUBLE);
588 cpl_array_multiply_scalar (tmp_cast, 180.0/ CPL_MATH_PI);
589 cpl_table_set_array (table, name, row, tmp_cast);
590 cpl_array_delete (tmp_cast);
592 CPLCHECK_MSG(
"Cannot set phase array");
594 return CPL_ERROR_NONE;
607 gravi_msg_function_start(0);
608 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
609 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
610 cpl_ensure_code (row>-1, CPL_ERROR_ILLEGAL_INPUT);
611 cpl_ensure_code (len>0, CPL_ERROR_ILLEGAL_INPUT);
613 char * str = cpl_sprintf (
"%-*.*s", len, len, value);
614 cpl_table_set_string (table, name, row, str);
616 FREE (cpl_free, str);
617 CPLCHECK_MSG(
"Cannot set string");
619 gravi_msg_function_exit(0);
620 return CPL_ERROR_NONE;
628 cpl_array * gravi_array_rebin (
const cpl_array * input,
const cpl_array * errs,
629 cpl_table * oi_wave_sc, cpl_table * oi_wave_ft)
631 gravi_msg_function_start(0);
632 cpl_ensure (input, CPL_ERROR_NULL_INPUT, NULL);
633 cpl_ensure (errs, CPL_ERROR_NULL_INPUT, NULL);
634 cpl_ensure (oi_wave_sc, CPL_ERROR_NULL_INPUT, NULL);
635 cpl_ensure (oi_wave_ft, CPL_ERROR_NULL_INPUT, NULL);
638 float *effWave_sc = cpl_table_get_data_float (oi_wave_sc,
"EFF_WAVE");
639 float *effWave_ft = cpl_table_get_data_float (oi_wave_ft,
"EFF_WAVE");
640 float *effBand_ft = cpl_table_get_data_float (oi_wave_ft,
"EFF_BAND");
641 cpl_size nwave_ft = cpl_table_get_nrow (oi_wave_ft);
642 cpl_size nwave_sc = cpl_table_get_nrow (oi_wave_sc);
644 CPLCHECK_NUL (
"Cannot get data");
646 cpl_array * output = cpl_array_new (nwave_ft, CPL_TYPE_DOUBLE);
647 cpl_array * weight = cpl_array_new (nwave_ft, CPL_TYPE_DOUBLE);
650 for (cpl_size wft = 0; wft < nwave_ft; wft++) {
651 for (cpl_size wsc = 0; wsc < nwave_sc; wsc++)
652 if ( fabs (effWave_sc[wsc] - effWave_ft[wft]) < 0.5 * effBand_ft[wft] ) {
654 double v = cpl_array_get (input, wsc, NULL);
655 double w = cpl_array_get (errs, wsc, NULL);
658 cpl_array_set (output, wft, cpl_array_get (output, wft, NULL) + v * w);
659 cpl_array_set (weight, wft, cpl_array_get (weight, wft, NULL) + w);
661 CPLCHECK_NUL(
"Cannot reduce the spectral resolution");
665 cpl_array_divide (output, weight);
666 cpl_array_delete (weight);
667 gravi_msg_function_exit(0);
676 cpl_error_code gravi_table_add_columns (cpl_table * oi_vis1,
const char *name1,
677 cpl_table * oi_vis2,
const char *name2)
679 gravi_msg_function_start(0);
680 cpl_ensure_code (oi_vis1, CPL_ERROR_NULL_INPUT);
681 cpl_ensure_code (oi_vis2, CPL_ERROR_NULL_INPUT);
682 cpl_ensure_code (name1, CPL_ERROR_NULL_INPUT);
683 cpl_ensure_code (name2, CPL_ERROR_NULL_INPUT);
686 cpl_msg_debug (cpl_func,
"Colname (%s + %s) ",name1,name2);
688 cpl_type type1 = cpl_table_get_column_type (oi_vis1, name1);
689 cpl_type type2 = cpl_table_get_column_type (oi_vis2, name2);
690 cpl_size nrow1 = cpl_table_get_nrow (oi_vis1);
691 cpl_size nrow2 = cpl_table_get_nrow (oi_vis2);
692 CPLCHECK_MSG(
"Cannot get type or nrow");
694 if ( type1 != type2 || nrow1 != nrow2) {
695 return cpl_error_set_message (cpl_func,CPL_ERROR_ILLEGAL_INPUT,
696 "input columns not conformables");
699 if ( type1 == CPL_TYPE_DOUBLE ) {
700 double * data1 = cpl_table_get_data_double (oi_vis1, name1);
701 double * data2 = cpl_table_get_data_double (oi_vis2, name2);
702 CPLCHECK_MSG(
"Cannot load data");
704 for (row=0 ; row<nrow1 ; row++) data1[row] += data2[row];
706 else if ( type1 == CPL_TYPE_FLOAT_COMPLEX ) {
707 float complex * data1 = cpl_table_get_data_float_complex (oi_vis1, name1);
708 float complex * data2 = cpl_table_get_data_float_complex (oi_vis2, name2);
709 CPLCHECK_MSG(
"Cannot load data");
711 for (row=0 ; row<nrow1; row++) data1[row] += data2[row];
713 else if ( type1 == CPL_TYPE_DOUBLE_COMPLEX ) {
714 double complex * data1 = cpl_table_get_data_double_complex (oi_vis1, name1);
715 double complex * data2 = cpl_table_get_data_double_complex (oi_vis2, name2);
716 CPLCHECK_MSG(
"Cannot load data");
718 for (row=0 ; row<nrow1; row++) data1[row] += data2[row];
720 else if ( type1 & CPL_TYPE_POINTER ) {
721 cpl_array ** data1 = cpl_table_get_data_array (oi_vis1, name1);
722 cpl_array ** data2 = cpl_table_get_data_array (oi_vis2, name2);
723 CPLCHECK_MSG(
"Cannot load data");
725 for (row=0 ; row<nrow1; row++) {
726 cpl_array_add (data1[row], data2[row]);
727 CPLCHECK_MSG(
"Cannot add data");
730 return cpl_error_set_message (cpl_func,CPL_ERROR_ILLEGAL_INPUT,
731 "unknow type -- report to DRS team");
735 gravi_msg_function_exit(0);
736 return CPL_ERROR_NONE;
747 cpl_error_code gravi_table_runint_column (cpl_table * oi_vis,
748 const char *input_name,
749 const char *output_name,
750 int nsmooth,
int nbase)
752 gravi_msg_function_start(0);
753 cpl_ensure_code (oi_vis, CPL_ERROR_NULL_INPUT);
754 cpl_ensure_code (input_name, CPL_ERROR_NULL_INPUT);
755 cpl_ensure_code (output_name, CPL_ERROR_NULL_INPUT);
756 cpl_ensure_code (nsmooth>=0, CPL_ERROR_ILLEGAL_INPUT);
757 cpl_ensure_code (nbase>0, CPL_ERROR_ILLEGAL_INPUT);
760 int row_add, row_sub;
762 cpl_type type = cpl_table_get_column_type (oi_vis, input_name);
763 cpl_size nrow = cpl_table_get_nrow (oi_vis) / nbase;
765 if ( type == CPL_TYPE_DOUBLE ) {
766 double * data = cpl_table_get_data_double (oi_vis, input_name);
767 double * output = cpl_malloc (
sizeof(
double) * nrow * nbase);
770 CPLCHECK_MSG(
"Cannot load data");
773 for ( base=0 ; base<nbase ; base++) {
776 for ( row=-nsmooth ; row<nrow ; row++) {
777 row_add = row+nsmooth;
778 row_sub = row-nsmooth-1;
779 if ( row_add < nrow ) buffer += data[row_add * nbase + base];
780 if ( row_sub >= 0 ) buffer -= data[row_sub * nbase + base];
781 if( row>=0 && row<nrow ) output[row * nbase + base] = (double)(buffer);
788 if ( !strcmp (input_name, output_name)) {
789 for (row = 0; row < nrow * nbase; row++) data[row] = output[row];
792 if (cpl_table_has_column (oi_vis, output_name))
793 cpl_table_erase_column (oi_vis, output_name);
794 cpl_table_wrap_double (oi_vis, output, output_name);
796 CPLCHECK_MSG(
"Cannot Wrap");
798 else if ( type == CPL_TYPE_DOUBLE_COMPLEX ) {
799 double complex * data = cpl_table_get_data_double_complex (oi_vis, input_name);
800 double complex * output = cpl_malloc (
sizeof(
double complex) * nrow * nbase);
801 double complex buffer = 0.0;
803 CPLCHECK_MSG(
"Cannot load data");
806 for ( base=0 ; base<nbase ; base++) {
809 for ( row=-nsmooth ; row<nrow ; row++) {
810 row_add = row+nsmooth;
811 row_sub = row-nsmooth-1;
812 if ( row_add < nrow ) buffer += data[row_add * nbase + base];
813 if ( row_sub >= 0 ) buffer -= data[row_sub * nbase + base];
814 if( row>=0 && row<nrow ) output[row * nbase + base] = (
double complex)(buffer);
820 if ( !strcmp (input_name, output_name)) {
821 for (row = 0; row < nrow * nbase; row++) data[row] = output[row];
824 if (cpl_table_has_column (oi_vis, output_name))
825 cpl_table_erase_column (oi_vis, output_name);
826 cpl_table_wrap_double_complex (oi_vis, output, output_name);
828 CPLCHECK_MSG (
"Cannot Wrap");
831 return cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
832 "This type is not supported..." 833 "report this error to DRS team !!");
836 gravi_msg_function_exit(0);
837 return CPL_ERROR_NONE;
840 cpl_error_code gravi_table_smooth_column (cpl_table * oi_vis,
841 const char *input_name,
842 const char *output_name,
843 int nsmooth,
int nbase)
845 gravi_msg_function_start(0);
846 cpl_ensure_code (oi_vis, CPL_ERROR_NULL_INPUT);
847 cpl_ensure_code (input_name, CPL_ERROR_NULL_INPUT);
848 cpl_ensure_code (output_name, CPL_ERROR_NULL_INPUT);
849 cpl_ensure_code (nsmooth>0, CPL_ERROR_ILLEGAL_INPUT);
850 cpl_ensure_code (nbase>0, CPL_ERROR_ILLEGAL_INPUT);
853 gravi_table_runint_column (oi_vis, input_name, output_name, nsmooth, nbase);
854 cpl_table_divide_scalar (oi_vis, output_name, 2.0*(
double)nsmooth+1.0);
856 gravi_msg_function_exit(0);
857 return CPL_ERROR_NONE;
860 cpl_array * gravi_table_create_sigma_array (cpl_table * oi_wave)
863 cpl_size size = cpl_table_get_nrow (oi_wave);
864 cpl_array * sigma = cpl_array_new (size, CPL_TYPE_DOUBLE);
866 for (cpl_size w = 0; w<size; w++ )
867 cpl_array_set (sigma, w, 1./cpl_table_get (oi_wave,
"EFF_WAVE", w, &nv));
869 CPLCHECK_NUL(
"Cannot compute the sigma array from EFF_WAVE");
873 cpl_array * gravi_table_create_wave_array (cpl_table * oi_wave)
875 cpl_ensure (oi_wave, CPL_ERROR_NULL_INPUT, NULL);
878 cpl_size size = cpl_table_get_nrow (oi_wave);
879 cpl_array * wave = cpl_array_new (size, CPL_TYPE_FLOAT);
881 for (cpl_size w = 0; w<size; w++ )
882 cpl_array_set (wave, w, cpl_table_get (oi_wave,
"EFF_WAVE", w, &nv));
884 CPLCHECK_NUL(
"Cannot compute the wave array from EFF_WAVE");
889 cpl_error_code gravi_array_normalize_complex (cpl_array * input)
891 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
894 cpl_size size = cpl_array_get_size (input);
896 double complex cpx = 0.0 * I + 0.0;
897 for (cpl_size wave = 0; wave<size; wave++ ) {
898 cpx = cpl_array_get_complex (input,wave,&nv);
899 cpl_array_set_complex (input, wave, cpx / cabs (cpx));
900 if (nv) {cpl_array_set_invalid (input, wave); nv=0;}
903 CPLCHECK_MSG(
"Cannot normalize complex");
904 return CPL_ERROR_NONE;
907 cpl_array * gravi_array_create_inverse (cpl_array *input)
909 cpl_ensure (input, CPL_ERROR_NULL_INPUT, NULL);
911 cpl_size size = cpl_array_get_size (input);
913 cpl_array * inv = cpl_array_new (size, CPL_TYPE_DOUBLE);
914 cpl_array_fill_window (inv, 0, size, 1.0);
915 cpl_array_divide (inv, input);
917 CPLCHECK_NUL(
"Cannot compute the sigma array from wave");
924 cpl_error_code gravi_array_phase_unwrap (cpl_array * input)
926 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
928 cpl_size size_x = cpl_array_get_size (input);
929 double phi_i, phi_ii, d_phi;
932 for (cpl_size i_data = 1; i_data < size_x; i_data++){
937 phi_i = cpl_array_get(input, i_data, NULL);
938 phi_ii = cpl_array_get(input, i_data-1, NULL);
940 d_phi = phi_i + k_wrap * 2 * M_PI - phi_ii;
942 if (d_phi > M_PI) k_wrap ++;
943 if (d_phi < - M_PI) k_wrap --;
945 cpl_array_set(input, i_data, cpl_array_get(input, i_data, NULL) + k_wrap * 2 * M_PI);
948 CPLCHECK_INT(
"Cannot unwrap the phase array");
949 return CPL_ERROR_NONE;
955 cpl_error_code gravi_array_phase_wrap (cpl_array * input)
957 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
959 cpl_size size = cpl_array_get_size (input);
961 for (cpl_size wave = 0; wave<size; wave++ ) {
962 cpl_array_set (input, wave, carg (cexp ( 1.0 * I * cpl_array_get (input, wave, NULL))));
965 CPLCHECK_MSG(
"Cannot wrap phase array");
966 return CPL_ERROR_NONE;
975 cpl_ensure (input, CPL_ERROR_NULL_INPUT, NULL);
977 cpl_size size = cpl_array_get_size (input);
979 cpl_array * output = cpl_array_new (size, CPL_TYPE_DOUBLE_COMPLEX);
980 cpl_array_fill_window_complex (output, 0, size, (
double complex)(0.0 + 0.0 * I));
984 for (n = 0; n < size; n ++) {
985 cpl_array_set_complex (output, n, cexp (factor * cpl_array_get (input, n, &nv) ) );
997 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
998 cpl_ensure_code (phase, CPL_ERROR_NULL_INPUT);
1000 cpl_size size_in = cpl_array_get_size (input);
1001 cpl_size size_ph = cpl_array_get_size (phase);
1003 cpl_ensure_code (size_in == size_ph, CPL_ERROR_ILLEGAL_INPUT);
1007 for (n = 0; n < size_in; n ++) {
1008 cpl_array_set_complex( input, n,
1009 cpl_array_get_complex( input, n, &nv) *
1010 cexp ( factor * cpl_array_get( phase, n, &nv) ) );
1013 CPLCHECK_MSG (
"Cannot multiply phasor");
1014 return CPL_ERROR_NONE;
1023 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
1024 cpl_ensure_code (phase, CPL_ERROR_NULL_INPUT);
1026 cpl_size size_in = cpl_array_get_size (input);
1027 cpl_size size_ph = cpl_array_get_size (phase);
1029 cpl_ensure_code (size_in == size_ph, CPL_ERROR_ILLEGAL_INPUT);
1033 for (n = 0; n < size_in; n ++) {
1034 cpl_array_set_complex( input, n,
1035 cpl_array_get_complex( input, n, &nv) +
1036 cexp ( factor * cpl_array_get( phase, n, &nv) ) );
1037 CPLCHECK_MSG (
"Cannot add phasor");
1040 return CPL_ERROR_NONE;
1049 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
1050 cpl_ensure_code (add, CPL_ERROR_NULL_INPUT);
1051 cpl_ensure_code (sub, CPL_ERROR_NULL_INPUT);
1053 cpl_size size_in = cpl_array_get_size (input);
1054 cpl_size size_add = cpl_array_get_size (add);
1055 cpl_size size_sub = cpl_array_get_size (sub);
1057 cpl_ensure_code (size_in == size_add, CPL_ERROR_ILLEGAL_INPUT);
1058 cpl_ensure_code (size_in == size_sub, CPL_ERROR_ILLEGAL_INPUT);
1062 for (n = 0; n < size_in; n ++) {
1063 cpl_array_set_complex (input, n,
1064 cpl_array_get_complex (input, n, &nv) +
1065 cpl_array_get_complex (add, n, &nv) *
1066 conj (cpl_array_get_complex (sub, n, &nv)));
1067 CPLCHECK_MSG (
"Cannot add phasors");
1070 return CPL_ERROR_NONE;
1079 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
1080 cpl_ensure_code (phase, CPL_ERROR_NULL_INPUT);
1082 cpl_size size_in = cpl_array_get_size (input);
1083 cpl_size size_ph = cpl_array_get_size (phase);
1085 cpl_ensure_code (size_in == size_ph, CPL_ERROR_ILLEGAL_INPUT);
1087 cpl_array * tmp = cpl_array_duplicate (phase);
1089 cpl_array_multiply_scalar (tmp, factor);
1090 cpl_array_add (input, tmp);
1092 cpl_array_delete (tmp);
1094 CPLCHECK_MSG(
"Cannot add phase");
1095 return CPL_ERROR_NONE;
1098 cpl_error_code gravi_array_multiply_conj (cpl_array * input1, cpl_array * input2)
1100 cpl_ensure_code (input1, CPL_ERROR_NULL_INPUT);
1101 cpl_ensure_code (input2, CPL_ERROR_NULL_INPUT);
1103 cpl_size size_in1 = cpl_array_get_size (input1);
1104 cpl_size size_in2 = cpl_array_get_size (input2);
1106 cpl_ensure_code (size_in1 == size_in2, CPL_ERROR_ILLEGAL_INPUT);
1108 for (cpl_size w = 0 ; w < size_in1 ; w++)
1109 cpl_array_set_complex (input1, w, cpl_array_get_complex (input1, w, NULL) *
1110 conj(cpl_array_get_complex (input2, w, NULL)));
1112 CPLCHECK_MSG (
"Cannot multiply by conj");
1113 return CPL_ERROR_NONE;
1121 cpl_array * gravi_array_smooth (cpl_array * input,
int nsmooth)
1123 cpl_ensure (nsmooth>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1124 cpl_ensure (input, CPL_ERROR_NULL_INPUT, NULL);
1126 cpl_type type = cpl_array_get_type (input);
1127 cpl_size row, n_row = cpl_array_get_size ( input );
1128 int row_add, row_sub;
1131 cpl_array * output = cpl_array_duplicate (input);
1133 if ( type == CPL_TYPE_DOUBLE ) {
1134 double * data_input = cpl_array_get_data_double (input);
1135 double * data_output = cpl_array_get_data_double (output);
1136 double buffer = 0.0;
1140 for ( row=-nsmooth ; row<n_row ; row++) {
1141 row_add = row+nsmooth;
1142 row_sub = row-nsmooth-1;
1143 if ( row_add < n_row ) {
1144 buffer += data_input[row_add];
1147 if ( row_sub >= 0 ) {
1148 buffer -= data_input[row_sub];
1151 if( row>=0 && row<n_row ) data_output[row] = (double)(buffer)/norm;
1156 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1157 "This type is not supported... report this error to DRS team !!");
1188 double * gd, cpl_size nrow,
1192 gravi_msg_function_start(verbose);
1193 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
1194 cpl_ensure_code (sigma, CPL_ERROR_NULL_INPUT);
1195 cpl_ensure_code (gd, CPL_ERROR_ILLEGAL_OUTPUT);
1198 cpl_size nsigma = cpl_array_get_size (sigma);
1199 double width1, step1, width2, step2, width3, step3;
1200 cpl_size w, s, nstep1, nstep2, nstep3;
1201 double x, gd0, gd1, gd2, gd3, current_max = -1.0;
1202 double lbd = 1.0 / cpl_array_get (sigma,nsigma/2,&nv);
1205 double coherence = 0.5 * nsigma / fabs (cpl_array_get (sigma,0,&nv) - cpl_array_get (sigma,nsigma-1,&nv));
1208 width1 = CPL_MIN (coherence, max_width);
1210 nstep1 = (cpl_size)(width1/step1);
1213 width2 = 3.0 * step1;
1215 nstep2 = (cpl_size)(width2/step2);
1218 width3 = 3.0 * step2;
1220 nstep3 = (cpl_size)(width3/step3);
1224 double * sigdata = cpl_malloc (
sizeof(
double complex) * nsigma);
1225 double complex * visdata = cpl_malloc (
sizeof(
double complex) * nsigma);
1226 double complex * waveform1 = cpl_malloc (
sizeof(
double complex) * (nstep1+2) * nsigma);
1227 double complex * waveform2 = cpl_malloc (
sizeof(
double complex) * (nstep2+2) * nsigma);
1228 double complex * waveform3 = cpl_malloc (
sizeof(
double complex) * (nstep3+2) * nsigma);
1231 for (w=0; w<nsigma; w++) sigdata[w] = cpl_array_get (sigma, w, &nv);
1235 for (w=1; w<nsigma; w++) ds += sigdata[w] - sigdata[w-1];
1239 cpl_msg_debug (cpl_func,
"Build waveform for 3 pass -- %lli %lli %lli steps", nstep1, nstep2, nstep3);
1241 for (s=0, x = -width1/2.0; x < +width1/2.0; x+=step1)
1242 for (w=0; w<nsigma; w++) { waveform1[s] = cexp (-2.*I*CPL_MATH_PI * x * sigdata[w]); s++;}
1243 for (s=0, x = -width2/2.0; x < +width2/2.0; x+=step2)
1244 for (w=0; w<nsigma; w++) { waveform2[s] = cexp (-2.*I*CPL_MATH_PI * x * sigdata[w]); s++;}
1245 for (s=0, x = -width3/2.0; x < +width3/2.0; x+=step3)
1246 for (w=0; w<nsigma; w++) { waveform3[s] = cexp (-2.*I*CPL_MATH_PI * x * sigdata[w]); s++;}
1248 cpl_msg_debug (cpl_func,
"Loop on %lli rows to compute gdelay", nrow);
1251 for (cpl_size row = 0; row<nrow; row++) {
1255 for (w=0; w<nsigma; w++) visdata[w] = cpl_array_get_complex (input[row], w, &nv);
1259 double complex is = 0.0 + I * 0.0;
1260 for (w=1; w<nsigma; w++) {
1261 is += visdata[w] * conj(visdata[w-1]) / CPL_MAX(cabs(visdata[w]) * cabs(visdata[w-1]), 1e-15);
1263 gd0 = carg (is) / ds / CPL_MATH_2PI;
1266 for (w=0; w<nsigma; w++) visdata[w] *= cexp (-2.*I*CPL_MATH_PI*gd0*sigdata[w]);
1269 for (current_max = -1.0, s = 0, x = -width1/2; x < +width1/2; x+=step1) {
1270 double complex tmp = 0.0 * I + 0.0;
1271 for (w=0; w<nsigma; w++) {tmp += visdata[w] * waveform1[s]; s++;}
1273 if ( P > current_max) { current_max = P; gd1 = x; }
1277 for (w=0; w<nsigma; w++) visdata[w] *= cexp (-2.*I*CPL_MATH_PI*gd1*sigdata[w]);
1280 for (current_max = -1.0, s = 0, x = -width2/2; x < +width2/2; x+=step2) {
1281 double complex tmp = 0.0 * I + 0.0;
1282 for (w=0; w<nsigma; w++) {tmp += visdata[w] * waveform2[s]; s++;}
1284 if ( P > current_max) { current_max = P; gd2 = x; }
1288 for (w=0; w<nsigma; w++) visdata[w] *= cexp (-2.*I*CPL_MATH_PI*gd2*sigdata[w]);
1291 for (current_max = -1.0, s = 0, x = -width3/2; x < +width3/2; x+=step3) {
1292 double complex tmp = 0.0 * I + 0.0;
1293 for (w=0; w<nsigma; w++) {tmp += visdata[w] * waveform3[s]; s++;}
1295 if ( P > current_max) { current_max = P; gd3 = x; }
1298 gd[row] = gd0 + gd1 + gd2 + gd3;
1300 CPLCHECK_MSG(
"Cannot compute GD");
1304 FREE (cpl_free, visdata);
1305 FREE (cpl_free, sigdata);
1306 FREE (cpl_free, waveform1);
1307 FREE (cpl_free, waveform2);
1308 FREE (cpl_free, waveform3);
1310 gravi_msg_function_exit(verbose);
1311 return CPL_ERROR_NONE;
1314 cpl_error_code gravi_table_compute_group_delay (cpl_table * table,
const char *input,
1315 const char *output, cpl_table * oi_wave)
1317 gravi_msg_function_start(0);
1318 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
1319 cpl_ensure_code (input, CPL_ERROR_NULL_INPUT);
1320 cpl_ensure_code (output, CPL_ERROR_NULL_INPUT);
1321 cpl_ensure_code (oi_wave, CPL_ERROR_NULL_INPUT);
1324 cpl_size nwave = cpl_table_get_nrow (oi_wave);
1325 cpl_array * sigma = cpl_array_new (nwave, CPL_TYPE_DOUBLE);
1326 for (cpl_size wave = 0; wave < nwave ; wave ++)
1327 cpl_array_set (sigma, wave, 1./cpl_table_get (oi_wave,
"EFF_WAVE", wave, NULL));
1330 gravi_table_new_column (table, output,
"m", CPL_TYPE_DOUBLE);
1331 double * gdelay = cpl_table_get_data_double (table, output);
1334 cpl_array ** input_arrays = cpl_table_get_data_array (table, input);
1335 cpl_size nrow = cpl_table_get_nrow (table);
1337 CPLCHECK_MSG (
"Cannot get data");
1341 nrow, 1.e-3, CPL_TRUE);
1342 FREE (cpl_array_delete, sigma);
1344 gravi_msg_function_exit(0);
1345 return CPL_ERROR_NONE;
1360 gravi_msg_function_start(0);
1361 cpl_ensure_code (first, CPL_ERROR_NULL_INPUT);
1362 cpl_ensure_code (second, CPL_ERROR_NULL_INPUT);
1364 cpl_size nrow = cpl_table_get_nrow (first);
1365 cpl_size ncol = cpl_table_get_ncol (first);
1368 if (nrow != cpl_table_get_nrow (second)) {cpl_msg_info(cpl_func,
"Different rows");
return 0;}
1369 if (ncol != cpl_table_get_ncol (second)) {cpl_msg_info(cpl_func,
"Different cols");
return 0;}
1370 if (cpl_table_compare_structure (first, second)) {cpl_msg_info(cpl_func,
"Different structure");
return 0;}
1372 cpl_array *names = cpl_table_get_column_names (first);
1375 for (cpl_size c = 0; c<ncol; c++) {
1376 const char * name = cpl_array_get_string (names, c);
1377 cpl_msg_debug (cpl_func,
"Now test %s", name);
1380 int type = cpl_table_get_column_type (first, name);
1383 case CPL_TYPE_STRING:
1384 for (cpl_size r = 0; r<nrow; r++)
1385 if (strcmp (cpl_table_get_string (first, name, r),
1386 cpl_table_get_string (second, name, r) ) )
1387 {cpl_msg_info (cpl_func,
"Different values in column %s, row %lli", name,r);
return 0;}
1389 case CPL_TYPE_DOUBLE:
1390 case CPL_TYPE_FLOAT:
1392 for (cpl_size r = 0; r<nrow; r++)
1393 if (cpl_table_get (first, name, r, &nv) !=
1394 cpl_table_get (second, name, r, &nv) )
1395 {cpl_msg_info (cpl_func,
"Different values in column %s, row %lli", name,r);
return 0;}
1397 case CPL_TYPE_POINTER|CPL_TYPE_STRING:
1398 for (cpl_size r = 0; r<nrow; r++)
1399 for (cpl_size p = 0; p<cpl_table_get_column_depth (first,name); p++)
1400 if ( strcmp (cpl_array_get_string (cpl_table_get_array (first, name, r), p),
1401 cpl_array_get_string (cpl_table_get_array (second, name, r), p)) )
1402 {cpl_msg_info (cpl_func,
"Different values in column %s, row %lli", name,r);
return 0;}
1404 case CPL_TYPE_POINTER|CPL_TYPE_DOUBLE:
1405 case CPL_TYPE_POINTER|CPL_TYPE_FLOAT:
1406 case CPL_TYPE_POINTER|CPL_TYPE_INT:
1407 for (cpl_size r = 0; r<nrow; r++)
1408 for (cpl_size p = 0; p<cpl_table_get_column_depth (first,name); p++) {
1409 if (cpl_array_get (cpl_table_get_array (first, name, r), p, NULL) !=
1410 cpl_array_get (cpl_table_get_array (second, name, r), p, NULL) )
1411 {cpl_msg_info (cpl_func,
"Different values in column %s, row %lli", name,r);
return 0;}
1415 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Cannot compare these tables (TBD, FIXME)");
1421 gravi_msg_function_exit(0);
1425 cpl_error_code gravi_table_new_column (cpl_table * table,
const char * name,
const char * unit, cpl_type type)
1427 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
1428 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
1430 if ( cpl_table_has_column (table, name) &&
1431 cpl_table_get_column_type (table, name) == type) {
1432 cpl_msg_info (cpl_func,
"Column %s already exists", name);
1434 cpl_table_new_column (table, name, type);
1437 if (type == CPL_TYPE_DOUBLE_COMPLEX || type == CPL_TYPE_FLOAT_COMPLEX)
1438 cpl_table_fill_column_window_complex (table, name, 0, cpl_table_get_nrow (table), 0.0 + I*0.0);
1440 cpl_table_fill_column_window (table, name, 0, cpl_table_get_nrow (table), 0.0);
1442 if (unit) cpl_table_set_column_unit (table, name, unit);
1444 return CPL_ERROR_NONE;
1447 cpl_error_code gravi_table_new_column_array (cpl_table * table,
const char * name,
const char * unit, cpl_type type, cpl_size size)
1449 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
1450 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
1452 if ( cpl_table_has_column (table, name) )
1453 cpl_table_erase_column (table, name);
1455 cpl_table_new_column_array (table, name, type, size);
1456 if (unit) cpl_table_set_column_unit (table, name, unit);
1458 return CPL_ERROR_NONE;
1461 cpl_error_code gravi_table_init_column_array (cpl_table * table,
const char * name,
const char * unit, cpl_type type, cpl_size size)
1463 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
1464 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
1466 if ( cpl_table_has_column (table, name) )
1467 cpl_table_erase_column (table, name);
1469 cpl_table_new_column_array (table, name, type, size);
1470 if (unit) cpl_table_set_column_unit (table, name, unit);
1472 cpl_array * array = cpl_array_new (size, type);
1473 cpl_array_fill_window (array, 0, size, 0.0);
1475 cpl_size nrow = cpl_table_get_nrow (table);
1476 for (cpl_size row = 0; row < nrow; row++)
1477 cpl_table_set_array (table, name, row, array);
1479 FREE (cpl_array_delete, array);
1481 return CPL_ERROR_NONE;
1498 cpl_ensure_code (imglist, CPL_ERROR_NULL_INPUT);
1500 cpl_size nrow = cpl_imagelist_get_size (imglist);
1502 for (cpl_size i = nrow-1; i>=0 ; i--) {
1503 cpl_image_unwrap (cpl_imagelist_unset (imglist, i));
1504 CPLCHECK_MSG(
"Cannot unset image");
1507 cpl_imagelist_delete (imglist);
1508 CPLCHECK_MSG(
"Cannot delete imagelist");
1510 return CPL_ERROR_NONE;
1528 gravi_msg_function_start(0);
1529 cpl_ensure (table_data, CPL_ERROR_NULL_INPUT, NULL);
1530 cpl_ensure (data_x, CPL_ERROR_NULL_INPUT, NULL);
1534 int type_column = cpl_table_get_column_type (table_data, data_x);
1537 cpl_size nrow = cpl_table_get_nrow (table_data);
1538 cpl_array ** array = cpl_table_get_data_array (table_data, data_x);
1539 cpl_ensure (array, CPL_ERROR_ILLEGAL_INPUT, NULL);
1543 if (cpl_table_get_column_dimensions (table_data, data_x)<2) {
1544 nx = cpl_table_get_column_depth (table_data, data_x);
1547 nx = cpl_table_get_column_dimension (table_data, data_x, 0);
1548 ny = cpl_table_get_column_dimension (table_data, data_x, 1);
1550 CPLCHECK_NUL (
"Cannot get dimension");
1554 cpl_imagelist * imglist = cpl_imagelist_new();
1557 switch (type_column)
1559 case CPL_TYPE_POINTER|CPL_TYPE_DOUBLE :
1561 for (cpl_size j = 0; j < nrow ; j++)
1563 img = cpl_image_wrap_double (nx, ny, cpl_array_get_data_double(array[j]));
1564 cpl_imagelist_set (imglist, img, j);
1569 case CPL_TYPE_POINTER|CPL_TYPE_INT :
1571 for (cpl_size j = 0; j < nrow ; j++)
1573 img = cpl_image_wrap_int (nx, ny, cpl_array_get_data_int(array[j]));
1574 cpl_imagelist_set (imglist, img,j);
1579 case CPL_TYPE_POINTER|CPL_TYPE_FLOAT :
1580 for (cpl_size j = 0; j < nrow ; j++)
1582 img = cpl_image_wrap_float (nx, ny, cpl_array_get_data_float(array[j]));
1583 cpl_imagelist_set (imglist, img, j);
1590 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
1591 "invalid type of image coming from %s", data_x);
1592 cpl_imagelist_delete (imglist);
1597 gravi_msg_function_exit(0);
1609 cpl_size llx, cpl_size lly,
1610 cpl_size urx, cpl_size ury)
1612 gravi_msg_function_start(0);
1613 cpl_ensure (img, CPL_ERROR_NULL_INPUT, -1);
1617 cpl_vector * flux = cpl_vector_new ((urx-llx+1)*(ury-lly+1));
1619 for (cpl_size v = 0, x = llx; x <= urx; x++) {
1620 for (cpl_size y = lly; y <= ury; y++) {
1621 cpl_vector_set (flux, v, cpl_image_get (img, x, y, &nv));
1623 CPLCHECK_MSG (
"Cannot fill vector");
1631 cpl_vector_multiply (flux, flux);
1633 double RMS = sqrt (cpl_vector_get_median (flux));
1634 FREE (cpl_vector_delete, flux);
1636 gravi_msg_function_exit(0);
1642 cpl_error_code gravi_image_subtract_window (cpl_image * img1,
const cpl_image * img2,
1643 cpl_size llx, cpl_size lly,
1644 cpl_size urx, cpl_size ury,
1645 cpl_size llx2, cpl_size lly2)
1647 gravi_msg_function_start(0);
1648 cpl_ensure_code (img1, CPL_ERROR_NULL_INPUT);
1649 cpl_ensure_code (img2, CPL_ERROR_NULL_INPUT);
1652 cpl_size nx = cpl_image_get_size_x (img1);
1653 cpl_size ny = cpl_image_get_size_y (img1);
1654 urx = CPL_MIN (urx, nx);
1655 ury = CPL_MIN (ury, ny);
1660 for (cpl_size x=llx; x<=urx; x++) {
1661 for (cpl_size y=lly; y<=ury; y++) {
1662 cpl_image_set (img1, x, y,
1663 cpl_image_get (img1,x,y,&nv) -
1664 cpl_image_get (img2,x+llx2,y+lly2,&nv));
1668 gravi_msg_function_exit(0);
1669 return CPL_ERROR_NONE;
1689 gravi_msg_function_start(0);
1690 cpl_ensure (table_data, CPL_ERROR_NULL_INPUT, NULL);
1691 cpl_ensure (data_x, CPL_ERROR_NULL_INPUT, NULL);
1692 cpl_ensure (row < cpl_table_get_nrow (table_data), CPL_ERROR_ILLEGAL_INPUT, NULL);
1695 cpl_ensure (wrap_imglist, CPL_ERROR_ILLEGAL_INPUT, NULL);
1697 cpl_image * out_img = cpl_image_duplicate (cpl_imagelist_get (wrap_imglist, row));
1700 gravi_msg_function_exit(0);
1721 gravi_msg_function_start(0);
1722 cpl_ensure (table_data, CPL_ERROR_NULL_INPUT, NULL);
1723 cpl_ensure (data_x, CPL_ERROR_NULL_INPUT, NULL);
1726 cpl_ensure (wrap_imglist, CPL_ERROR_ILLEGAL_INPUT, NULL);
1728 cpl_imagelist * out_imglist = cpl_imagelist_duplicate (wrap_imglist);
1731 gravi_msg_function_exit(0);
1751 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
1752 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
1754 cpl_size ndim = cpl_table_get_column_dimensions (table, name);
1755 cpl_array * dimension = cpl_array_new (ndim, CPL_TYPE_INT);
1756 for (cpl_size dim = 0; dim < ndim; dim++) {
1757 int value = cpl_table_get_column_dimension (table, name, dim);
1758 cpl_array_set (dimension, dim, value);
1778 cpl_ensure (img, CPL_ERROR_NULL_INPUT, NULL);
1780 cpl_type type_img = cpl_image_get_type (img);
1781 int x = cpl_image_get_size_x (img);
1782 int y = cpl_image_get_size_y (img);
1784 CPLCHECK_NUL (
"Cannot get data");
1786 cpl_array * array = NULL;
1788 case CPL_TYPE_FLOAT :
1789 array = cpl_array_wrap_float (cpl_image_get_data_float(img), x*y);
1791 case CPL_TYPE_DOUBLE :
1792 array = cpl_array_wrap_double (cpl_image_get_data_double(img), x*y);
1795 array = cpl_array_wrap_int (cpl_image_get_data_int(img), x*y);
1798 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
1799 "invalid type of image");
1821 cpl_vector * vector2)
1823 cpl_ensure (vector1, CPL_ERROR_NULL_INPUT, NULL);
1825 cpl_matrix * matrix, * matrix_wrap;
1826 int size = cpl_vector_get_size(vector1);
1827 double * data1, * data2;
1829 if( vector2 != NULL ){
1830 int size2 = cpl_vector_get_size(vector2);
1831 cpl_ensure (size == size2, CPL_ERROR_ILLEGAL_INPUT, NULL);
1833 data1 = cpl_malloc(2 * size *
sizeof(
double));
1834 memcpy(data1, cpl_vector_get_data(vector1), size *
sizeof(
double));
1836 data2 = cpl_vector_get_data(vector2);
1837 memcpy(data1 + size, data2, size *
sizeof(
double));
1839 matrix_wrap = cpl_matrix_wrap(2, size, data1);
1840 matrix = cpl_matrix_transpose_create(matrix_wrap);
1842 cpl_matrix_unwrap(matrix_wrap);
1845 data1 = cpl_malloc(size *
sizeof(
double));
1846 memcpy(data1, cpl_vector_get_data(vector1), size *
sizeof(
double));
1848 matrix = cpl_matrix_wrap(size, 1, data1);
1872 const char * regname)
1875 cpl_ensure (spectrum_data, CPL_ERROR_NULL_INPUT, NULL);
1876 cpl_ensure (regname, CPL_ERROR_NULL_INPUT, NULL);
1877 cpl_ensure (index>=0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1880 cpl_size size = cpl_table_get_column_depth (spectrum_data, regname);
1881 cpl_ensure (index<size, CPL_ERROR_ILLEGAL_INPUT, NULL);
1884 cpl_size nrow = cpl_table_get_nrow (spectrum_data);
1885 cpl_vector * data_value = cpl_vector_new (nrow);
1890 cpl_array ** column_array = cpl_table_get_data_array (spectrum_data, regname);
1891 cpl_ensure (column_array, CPL_ERROR_ILLEGAL_INPUT, NULL);
1895 for (cpl_size row = 0; row < nrow; row ++){
1896 value = cpl_array_get (column_array[row], index, NULL);
1897 cpl_vector_set (data_value, row, value);
1899 }
else if (cpl_table_get_column_type (spectrum_data, regname)
1900 == CPL_TYPE_DOUBLE) {
1902 double * data = cpl_table_get_data_double (spectrum_data, regname);
1903 for (cpl_size row = 0; row < nrow; row++) {
1904 cpl_vector_set (data_value, row, data[row]);
1907 }
else if (cpl_table_get_column_type (spectrum_data, regname)
1910 int * data = cpl_table_get_data_int (spectrum_data, regname);
1911 for (cpl_size row = 0; row < nrow; row++) {
1912 cpl_vector_set (data_value, row, data[row]);
1916 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1917 "This type is not supported" 1918 "(report to DRS team).");
1919 FREE (cpl_vector_delete, data_value);
1923 return (data_value);
1926 cpl_vector * gravi_table_get_vector_scalar (cpl_table * table,
1932 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
1933 cpl_ensure (name, CPL_ERROR_NULL_INPUT, NULL);
1934 cpl_ensure (base>=0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1935 cpl_ensure (nbase>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1938 cpl_size size = cpl_table_get_column_depth (table, name);
1939 cpl_ensure (size==0, CPL_ERROR_ILLEGAL_INPUT, NULL);
1942 cpl_size nrow = cpl_table_get_nrow (table) / nbase;
1943 cpl_vector * vector = cpl_vector_new (nrow);
1946 cpl_type type = cpl_table_get_column_type (table, name);
1948 if (type == CPL_TYPE_DOUBLE) {
1949 double * data = cpl_table_get_data_double (table, name);
1950 for (cpl_size row = 0; row < nrow; row++)
1951 cpl_vector_set (vector, row, data[row*nbase+base]);
1953 else if (type == CPL_TYPE_FLOAT) {
1954 float * data = cpl_table_get_data_float (table, name);
1955 for (cpl_size row = 0; row < nrow; row++)
1956 cpl_vector_set (vector, row, data[row*nbase+base]);
1958 else if (type == CPL_TYPE_INT) {
1959 int * data = cpl_table_get_data_int (table, name);
1960 for (cpl_size row = 0; row < nrow; row++)
1961 cpl_vector_set (vector, row, data[row*nbase+base]);
1964 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1965 "This type is not supported" 1966 "(report to DRS team).");
1967 FREE (cpl_vector_delete, vector);
1992 const char * regname1,
1993 const char * regname2)
1996 cpl_ensure (spectrum_data, CPL_ERROR_NULL_INPUT, NULL);
1997 cpl_ensure (regname1, CPL_ERROR_NULL_INPUT, NULL);
1998 cpl_ensure (regname2, CPL_ERROR_NULL_INPUT, NULL);
1999 cpl_ensure (index>=0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2002 cpl_size size1 = cpl_table_get_column_depth (spectrum_data, regname1);
2003 cpl_size size2 = cpl_table_get_column_depth (spectrum_data, regname2);
2004 cpl_ensure (index<size1, CPL_ERROR_ILLEGAL_INPUT, NULL);
2005 cpl_ensure (index<size2, CPL_ERROR_ILLEGAL_INPUT, NULL);
2008 cpl_array ** column_array1 = cpl_table_get_data_array (spectrum_data, regname1);
2009 cpl_array ** column_array2 = cpl_table_get_data_array (spectrum_data, regname2);
2010 cpl_ensure (column_array1, CPL_ERROR_ILLEGAL_INPUT, NULL);
2011 cpl_ensure (column_array2, CPL_ERROR_ILLEGAL_INPUT, NULL);
2014 cpl_size nrow = cpl_table_get_nrow (spectrum_data);
2015 cpl_vector * data_value = cpl_vector_new (nrow);
2019 for (cpl_size row = 0; row < nrow; row++){
2020 value = cpl_array_get (column_array1[row], index, NULL) - cpl_array_get (column_array2[row], index, NULL);
2021 cpl_vector_set (data_value, row, value);
2024 return (data_value);
2035 cpl_ensure_code (img, CPL_ERROR_NULL_INPUT);
2037 cpl_size nx = cpl_image_get_size_x (img);
2038 cpl_size ny = cpl_image_get_size_y (img);
2039 cpl_image_fill_window (img, 1,1,nx,ny, value);
2041 return CPL_ERROR_NONE;
2052 cpl_ensure (matrix, CPL_ERROR_NULL_INPUT, NULL);
2053 cpl_size nx = cpl_matrix_get_ncol (matrix);
2054 cpl_size ny = cpl_matrix_get_nrow (matrix);
2055 return cpl_image_wrap_double (nx, ny, cpl_matrix_get_data (matrix));
2058 cpl_image * gravi_image_from_matrix (cpl_matrix * matrix)
2060 cpl_ensure (matrix, CPL_ERROR_NULL_INPUT, NULL);
2061 cpl_size nx = cpl_matrix_get_ncol (matrix);
2062 cpl_size ny = cpl_matrix_get_nrow (matrix);
2064 cpl_image * image = cpl_image_new (nx,ny,CPL_TYPE_DOUBLE);
2065 for (cpl_size i = 0; i < nx; i++)
2066 for (cpl_size j = 0; j < ny; j++)
2067 cpl_image_set (image, i+1, j+1, cpl_matrix_get (matrix,j,i));
2080 cpl_ensure (vector, CPL_ERROR_NULL_INPUT, NULL);
2081 cpl_size nx = cpl_vector_get_size (vector);
2083 return cpl_image_wrap_double (nx, ny, cpl_vector_get_data (vector));
2086 cpl_image * gravi_image_from_vector (cpl_vector * vector)
2088 cpl_ensure (vector, CPL_ERROR_NULL_INPUT, NULL);
2089 cpl_size nx = cpl_vector_get_size (vector);
2092 cpl_image * image = cpl_image_new (nx,ny,CPL_TYPE_DOUBLE);
2093 for (cpl_size i = 0; i < nx; i++)
2094 cpl_image_set (image, i+1, 1, cpl_vector_get (vector,i));
2114 cpl_ensure (img, CPL_ERROR_NULL_INPUT, -1);
2115 cpl_ensure (thr>0 && thr<1, CPL_ERROR_ILLEGAL_INPUT, -1);
2117 cpl_size nx = cpl_image_get_size_x (img);
2118 cpl_size ny = cpl_image_get_size_y (img);
2119 cpl_size nq = (cpl_size)(thr * nx * ny);
2121 cpl_ensure (nq>=0 && nq<=nx*ny, CPL_ERROR_ILLEGAL_INPUT, -1);
2125 cpl_vector * vect = cpl_vector_new (nx*ny);
2126 for (cpl_size ix = 0; ix < nx ; ix++)
2127 for (cpl_size iy = 0; iy < ny ; iy++)
2128 cpl_vector_set (vect, ix * ny + iy, cpl_image_get (img, ix+1, iy+1, &nv));
2131 cpl_vector_sort (vect, CPL_SORT_ASCENDING);
2132 double value = cpl_vector_get (vect, nq);
2133 cpl_vector_delete (vect);
2148 cpl_ensure (arr, CPL_ERROR_NULL_INPUT, -1);
2149 cpl_ensure (thr>0 && thr<1, CPL_ERROR_ILLEGAL_INPUT, -1);
2151 cpl_size nx = cpl_array_get_size (arr);
2152 cpl_size nq = (cpl_size)(thr * nx);
2154 cpl_ensure (nq>=0 && nq<=nx, CPL_ERROR_ILLEGAL_INPUT, -1);
2158 cpl_vector * vect = cpl_vector_new (nx);
2159 for (cpl_size ix = 0; ix < nx ; ix++)
2160 cpl_vector_set (vect, ix, cpl_array_get (arr, ix, &nv));
2163 cpl_vector_sort (vect, CPL_SORT_ASCENDING);
2164 double value = cpl_vector_get (vect, nq);
2165 cpl_vector_delete (vect);
2181 cpl_ensure (vector, CPL_ERROR_NULL_INPUT, -1);
2182 cpl_size size = cpl_vector_get_size (vector);
2185 double value = cpl_vector_get (vector, 0);
2186 for (
int s = 1; s < size; s++) {
2187 if (cpl_vector_get (vector, s) > value) {
2189 value = cpl_vector_get (vector, s);
2211 cpl_ensure (vector_in, CPL_ERROR_NULL_INPUT, 0.0);
2212 cpl_ensure (percent > 0, CPL_ERROR_ILLEGAL_INPUT, 0.0);
2213 cpl_ensure (percent < 0.5, CPL_ERROR_ILLEGAL_INPUT, 0.0);
2214 cpl_ensure (nsigma > 0, CPL_ERROR_ILLEGAL_INPUT, 0.0);
2217 cpl_vector * sort_vector = cpl_vector_duplicate (vector_in);
2218 cpl_vector_sort (sort_vector, CPL_SORT_ASCENDING);
2221 cpl_size size = cpl_vector_get_size (vector_in);
2222 cpl_size sizeout = size*(1-percent*2);
2223 cpl_size start = (size-sizeout)/2;
2225 cpl_vector * vector = cpl_vector_new (sizeout);
2226 for (cpl_size i = 0 ; i < sizeout ; i++)
2227 cpl_vector_set (vector, i, cpl_vector_get (sort_vector, i+start));
2230 cpl_vector * vector_med = cpl_vector_new (sizeout);
2231 double med = cpl_vector_get_median (vector);
2232 double rms = nsigma * cpl_vector_get_stdev (vector);
2234 cpl_size size_med = 0;
2235 for (cpl_size i = 0 ; i < cpl_vector_get_size (vector) ; i++)
2236 if ( (cpl_vector_get (vector, i) > med-rms) &&
2237 (cpl_vector_get (vector, i) < med+rms) ) {
2238 cpl_vector_set (vector_med, size_med, cpl_vector_get (vector, i));
2241 cpl_vector_set_size (vector_med, size_med);
2244 double output = cpl_vector_get_mean (vector_med);
2246 FREE (cpl_vector_delete, vector_med);
2247 FREE (cpl_vector_delete, sort_vector);
2263 cpl_size size = cpl_vector_get_size (vector);
2264 cpl_size newsize = size / step;
2265 cpl_vector * out = cpl_vector_new (newsize);
2267 for (
int s = 0; s < newsize; s++)
2268 cpl_vector_set (out, s, cpl_vector_get (vector, s*step+start));
2286 cpl_ensure_code (vector, CPL_ERROR_NULL_INPUT);
2287 cpl_ensure_code (ref, CPL_ERROR_NULL_INPUT);
2289 cpl_size nrow = cpl_vector_get_size (vector);
2291 double referenced, referenced_prev = 0.0;
2294 for (cpl_size row = 0 ; row < nrow; row ++) {
2295 double phase_ref = ref_to_phase * cpl_vector_get (ref, row);
2298 referenced = (cpl_vector_get (vector, row) - phase_ref);
2299 referenced = fmod (referenced, CPL_MATH_2PI);
2300 if (referenced < 0) referenced += CPL_MATH_2PI;
2303 if ( referenced - referenced_prev > CPL_MATH_PI ) wrap --;
2304 if ( referenced - referenced_prev < -CPL_MATH_PI ) wrap ++;
2305 referenced_prev = referenced;
2308 cpl_vector_set (vector, row, phase_ref + referenced + wrap * CPL_MATH_2PI);
2311 CPLCHECK_MSG (
"Cannot unwrap with guess");
2313 return CPL_ERROR_NONE;
2332 int base,
int nbase,
double value)
2334 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
2335 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
2336 cpl_ensure_code (nbase==1 || nbase==4 || nbase==6, CPL_ERROR_ILLEGAL_INPUT);
2337 cpl_ensure_code (base>=0 && base <nbase, CPL_ERROR_ILLEGAL_INPUT);
2339 cpl_size nrow = cpl_table_get_nrow (table) / nbase;
2341 if (cpl_table_get_column_depth (table, name) > 0) {
2342 cpl_array ** array = cpl_table_get_data_array (table, name);
2343 for (cpl_size row = 0 ; row < nrow ; row ++) {
2344 cpl_array_multiply_scalar (array[row*nbase+base], value);
2345 CPLCHECK_MSG (
"Cannot multiply (array may not be numerical)");
2347 }
else if(cpl_table_get_column_type (table, name) == CPL_TYPE_DOUBLE) {
2348 double * array = cpl_table_get_data_double (table, name);
2349 for (cpl_size row = 0 ; row < nrow ; row ++) {
2350 array[row*nbase+base] *= value;
2352 }
else if(cpl_table_get_column_type (table, name) == CPL_TYPE_INT) {
2353 int * array = cpl_table_get_data_int (table, name);
2354 for (cpl_size row = 0 ; row < nrow ; row ++) {
2355 array[row*nbase+base] *= value;
2358 return cpl_error_set_message (cpl_func, CPL_ERROR_INVALID_TYPE,
2359 "Column type is not supported");
2362 CPLCHECK_MSG (
"Cannot multiply");
2363 return CPL_ERROR_NONE;
2382 int base,
int nbase,
double value)
2384 cpl_ensure_code (table, CPL_ERROR_NULL_INPUT);
2385 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
2386 cpl_ensure_code (nbase==1 || nbase==4 || nbase==6, CPL_ERROR_ILLEGAL_INPUT);
2387 cpl_ensure_code (base>=0 && base <nbase, CPL_ERROR_ILLEGAL_INPUT);
2389 cpl_size nrow = cpl_table_get_nrow (table) / nbase;
2391 if (cpl_table_get_column_depth (table, name) > 0) {
2392 cpl_array ** array = cpl_table_get_data_array (table, name);
2393 for (cpl_size row = 0 ; row < nrow ; row ++) {
2394 cpl_array_add_scalar (array[row*nbase+base], value);
2395 CPLCHECK_MSG (
"Cannot multiply (array may not be numerical)");
2397 }
else if(cpl_table_get_column_type (table, name) == CPL_TYPE_DOUBLE) {
2398 double * array = cpl_table_get_data_double (table, name);
2399 for (cpl_size row = 0 ; row < nrow ; row ++) {
2400 array[row*nbase+base] += value;
2402 }
else if(cpl_table_get_column_type (table, name) == CPL_TYPE_INT) {
2403 int * array = cpl_table_get_data_int (table, name);
2404 for (cpl_size row = 0 ; row < nrow ; row ++) {
2405 array[row*nbase+base] += value;
2408 return cpl_error_set_message (cpl_func, CPL_ERROR_INVALID_TYPE,
2409 "Column type is not supported");
2412 CPLCHECK_MSG (
"Cannot multiply");
2413 return CPL_ERROR_NONE;
2436 gravi_msg_function_start(0);
2438 cpl_ensure (matrix, CPL_ERROR_NULL_INPUT, NULL);
2439 cpl_ensure (xref, CPL_ERROR_NULL_INPUT, NULL);
2440 cpl_ensure (xout, CPL_ERROR_NULL_INPUT, NULL);
2442 cpl_size nrow = cpl_matrix_get_nrow (matrix);
2443 cpl_size ncol = cpl_matrix_get_ncol (matrix);
2444 cpl_size nxref = cpl_vector_get_size (xref);
2445 cpl_size nxout = cpl_vector_get_size (xout);
2447 cpl_ensure (ncol == nxref, CPL_ERROR_ILLEGAL_INPUT, NULL);
2448 cpl_ensure (nxout > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
2451 cpl_matrix * outmatrix = cpl_matrix_new (nrow, nxout);
2452 cpl_vector * yref = cpl_vector_new (nxref);
2453 cpl_vector * yout = cpl_vector_new (nxout);
2454 cpl_bivector * fref = cpl_bivector_wrap_vectors (xref, yref);
2455 cpl_bivector * fout = cpl_bivector_wrap_vectors (xout, yout);
2458 for (cpl_size row = 0; row < nrow; row++) {
2459 for (cpl_size x = 0; x < nxref; x++)
2460 cpl_vector_set (yref, x, cpl_matrix_get (matrix, row, x));
2461 cpl_bivector_interpolate_linear (fout, fref);
2462 for (cpl_size x = 0; x < nxout; x++)
2463 cpl_matrix_set (outmatrix, row, x, cpl_vector_get (yout, x));
2464 CPLCHECK_NUL (
"Cannot interpolate matrix");
2467 FREE (cpl_bivector_unwrap_vectors, fref);
2468 FREE (cpl_bivector_unwrap_vectors, fout);
2469 FREE (cpl_vector_delete, yref);
2470 FREE (cpl_vector_delete, yout);
2472 gravi_msg_function_exit(0);
2488 gravi_msg_function_start(0);
2490 cpl_ensure (a_in, CPL_ERROR_NULL_INPUT, NULL);
2492 cpl_size m = cpl_matrix_get_nrow (a_in);
2493 cpl_size n = cpl_matrix_get_ncol (a_in);
2495 cpl_vector * w = cpl_vector_new (n);
2496 cpl_matrix * v = cpl_matrix_new (n, n);
2502 cpl_matrix * a_out = svdcmp (a_in, w, v);
2503 CPLCHECK_NUL (
"Error in inverse SV");
2506 for (cpl_size ii = 0; ii < n; ii++) {
2507 if (cpl_vector_get (w, ii) < 0.1)
2508 cpl_msg_warning (cpl_func,
"Singular Value %lld = %e",
2509 ii, cpl_vector_get (w, ii));
2515 cpl_matrix * a_inv = cpl_matrix_new (n,m);
2516 double * a_inv_data = cpl_matrix_get_data (a_inv);
2518 for (cpl_size j = 0; j < m; j++) {
2519 for (cpl_size i = 0; i < n; i++){
2522 for (cpl_size ii = 0; ii < n; ii++)
2523 if (cpl_vector_get (w, ii) > 1e-15)
2524 wv_at += cpl_matrix_get (v, i, ii) /
2525 cpl_vector_get (w, ii) *
2526 cpl_matrix_get (a_out, j, ii);
2528 a_inv_data[j + i * m] = wv_at;
2533 cpl_vector_delete (w);
2534 cpl_matrix_delete (v);
2535 cpl_matrix_delete (a_out);
2537 gravi_msg_function_exit(0);
2545 double pythag(
double a,
double b)
2547 double absa=fabs(a);
2548 double absb=fabs(b);
2550 return absa*sqrt(1.0+pow(absb/absa, 2));
2552 return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+pow(absa/absb, 2)));
2560 cpl_matrix * svdcmp (cpl_matrix * a_in, cpl_vector * w, cpl_matrix * v)
2562 gravi_msg_function_start(0);
2563 cpl_ensure (a_in, CPL_ERROR_NULL_INPUT, NULL);
2565 int flag, i, its, j, jj, k, l, nm, n, m;
2566 double anorm, c, f, g, h, s, scale, x, y, z;
2570 a = cpl_matrix_duplicate(a_in);
2571 m = cpl_matrix_get_nrow (a_in);
2572 n = cpl_matrix_get_ncol (a_in);
2573 rv1 = cpl_vector_new(n);
2575 g = scale = anorm = 0.0;
2576 for (i = 0; i < n; i++) {
2578 cpl_vector_set(rv1, i, scale*g);
2579 g = s = scale = 0.0;
2581 for (k = i; k < m; k++)
2582 scale += fabs(cpl_matrix_get(a, k, i));
2585 for (k = i; k < m; k++) {
2587 cpl_matrix_set (a, k, i, cpl_matrix_get(a, k, i)/scale);
2588 s += cpl_matrix_get(a, k, i) * cpl_matrix_get(a, k, i);
2590 f = cpl_matrix_get(a, i, i);
2591 g = -SIGN(sqrt(s),f);
2593 cpl_matrix_set (a, i, i, f - g);
2594 for (j = l; j < n; j++) {
2595 for (s = 0.0, k = i; k < m; k++)
2596 s += cpl_matrix_get(a, k, i) * cpl_matrix_get(a, k, j);
2599 for (k = i; k < m; k++)
2600 cpl_matrix_set (a, k, j, cpl_matrix_get(a, k, j) +
2601 f * cpl_matrix_get(a, k, i));
2604 for (k = i; k < m; k++)
2605 cpl_matrix_set (a, k, i, cpl_matrix_get(a, k, i) * scale);
2610 cpl_vector_set(w, i, scale *g);
2612 g = s = scale = 0.0;
2613 if (i < m && i != (n - 1)) {
2614 for (k = l; k < n; k++)
2615 scale += fabs(cpl_matrix_get(a, i, k));;
2617 for (k = l; k < n; k++) {
2618 cpl_matrix_set (a, i, k, cpl_matrix_get(a, i, k) / scale);
2619 s += cpl_matrix_get(a, i, k) * cpl_matrix_get(a, i, k);
2621 f = cpl_matrix_get(a, i, l);
2622 g = -SIGN(sqrt(s),f);
2624 cpl_matrix_set (a, i, l, f - g);
2625 for (k = l; k < n; k++)
2626 cpl_vector_set(rv1, k, cpl_matrix_get(a, i, k)/h);
2627 for (j = l; j < m; j++) {
2628 for (s = 0.0, k = l; k < n; k++)
2629 s += cpl_matrix_get(a, j, k) * cpl_matrix_get(a, i, k);
2630 for (k = l; k < n; k++)
2631 cpl_matrix_set (a, j, k, cpl_matrix_get(a, j, k) + s * cpl_vector_get(rv1, k));
2633 for (k = l; k < n; k++)
2634 cpl_matrix_set (a, i, k, cpl_matrix_get(a, i, k) * scale);
2638 anorm = fmax(anorm,(fabs(cpl_vector_get(w, i)) +
2639 fabs(cpl_vector_get(rv1, i))));
2643 for (i = (n - 1); i >= 0; i--) {
2646 for (j = l; j < n; j++)
2647 cpl_matrix_set(v, j, i, (cpl_matrix_get(a, i, j) /
2648 cpl_matrix_get(a, i, l)) / g);
2650 for (j = l;j < n; j++) {
2651 for (s = 0.0, k = l; k < n; k++)
2652 s += cpl_matrix_get(a, i, k) * cpl_matrix_get(v, k, j);
2654 for (k = l; k < n; k++)
2655 cpl_matrix_set(v, k, j, cpl_matrix_get(v, k, j) + s *
2656 cpl_matrix_get(v, k, i));
2660 for (j = l; j < n; j++) {
2661 cpl_matrix_set(v, i, j, 0.0);
2662 cpl_matrix_set(v, j, i, 0.0);
2665 cpl_matrix_set(v, i, i, 1.0);
2667 g = cpl_vector_get(rv1, i);
2671 for (i = (IMIN(m,n) - 1); i >= 0; i--) {
2673 g = cpl_vector_get(w, i);
2674 for (j = l; j < n; j++)
2675 cpl_matrix_set(a, i, j, 0.0);
2679 for (j = l; j < n; j++) {
2680 for (s = 0.0, k = l; k < m; k++)
2681 s += cpl_matrix_get(a, k, i) * cpl_matrix_get(a, k, j);
2683 f = (s / cpl_matrix_get(a, i, i)) * g;
2685 for (k = i; k < m; k++)
2686 cpl_matrix_set(a, k, j, cpl_matrix_get(a, k, j) + f *
2687 cpl_matrix_get(a, k, i));
2690 for (j = i; j < m; j++)
2691 cpl_matrix_set(a, j, i, cpl_matrix_get(a, j, i) * g);
2695 for (j = i; j < m; j++)
2696 cpl_matrix_set(a, j, i, 0.0);
2698 cpl_matrix_set(a, i, i, cpl_matrix_get(a, i, i) + 1);
2702 for (k = (n - 1); k >= 0; k--) {
2703 for (its = 1; its <= 60; its ++) {
2706 for (l = k; l >= 0; l--) {
2708 if ((fabs(cpl_vector_get(rv1, l)) + anorm) == anorm) {
2712 if ((fabs(cpl_vector_get(w, nm)) + anorm) == anorm)
2719 for (i = l; i <= k; i++) {
2720 f = s * cpl_vector_get(rv1, i);
2721 cpl_vector_set(rv1, i, c * cpl_vector_get(rv1, i));
2722 if ((fabs(f) + anorm) == anorm)
2724 g = cpl_vector_get(w, i);
2726 cpl_vector_set(w, i, h);
2730 for (j = 0; j < m; j++) {
2731 y = cpl_matrix_get(a, j, nm);
2732 z = cpl_matrix_get(a, j, i);
2733 cpl_matrix_set(a, j, nm, y*c+z*s);
2734 cpl_matrix_set(a, j, i, z*c-y*s);
2738 z = cpl_vector_get(w, k);
2741 cpl_vector_set(w, k, -z);
2742 for (j = 0; j < n; j++)
2743 cpl_matrix_set(v, j, k, -cpl_matrix_get(v, j, k));
2750 cpl_error_set_message(cpl_func,
2751 CPL_ERROR_ILLEGAL_INPUT,
2752 "no convergence in 120 svdcmp iterations");
2753 cpl_vector_delete(rv1);
2754 cpl_matrix_delete(a);
2757 x = cpl_vector_get(w, l);
2759 y = cpl_vector_get(w, nm); ;
2760 g = cpl_vector_get(rv1, nm);
2761 h = cpl_vector_get(rv1, k);
2762 f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
2764 f = ((x - z) * (x + z) + h * ((y / (f + SIGN(g, f))) - h)) / x;
2766 for (j = l; j < nm; j++) {
2768 g = cpl_vector_get(rv1, i);
2769 y = cpl_vector_get(w, i);
2773 cpl_vector_set(rv1, j, z);
2780 for (jj = 0; jj < n; jj++) {
2781 x = cpl_matrix_get(v, jj, j);
2782 z=cpl_matrix_get(v, jj, i);
2783 cpl_matrix_set(v, jj, j, x*c+z*s);
2784 cpl_matrix_set(v, jj, i, z*c-x*s);
2787 cpl_vector_set(w, j, z);
2796 for (jj = 0; jj < m; jj++) {
2797 y = cpl_matrix_get(a, jj, j);
2798 z = cpl_matrix_get(a, jj, i);
2799 cpl_matrix_set(a, jj, j, y*c+z*s);
2800 cpl_matrix_set(a, jj, i, z*c-y*s);
2803 cpl_vector_set(rv1, l, 0.0);
2804 cpl_vector_set(rv1, k, f);
2805 cpl_vector_set(w, k, x);
2809 cpl_vector_delete(rv1);
2811 gravi_msg_function_exit(0);
2828 gravi_msg_function_start(1);
2829 cpl_ensure (table, CPL_ERROR_NULL_INPUT, NULL);
2832 cpl_table_select_all (table);
2833 cpl_table_and_selected_double (table,
"TIME", CPL_NOT_LESS_THAN, start);
2834 cpl_table_and_selected_double (table,
"TIME", CPL_LESS_THAN, end);
2835 cpl_table * out = cpl_table_extract_selected (table);
2837 gravi_msg_function_exit(1);
double gravi_image_get_quantile(const cpl_image *img, double thr)
Compute the quantile of an image.
cpl_array * gravi_table_get_column_dimension(const cpl_table *table, const char *name)
Return an array ready for cpl_table_set_column_dimension.
cpl_vector * gravi_table_get_vector(cpl_table *spectrum_data, cpl_size index, const char *regname)
Create a vector from the row index of the column regname.
cpl_imagelist * gravi_imagelist_from_column(cpl_table *table_data, const char *data_x)
Create an imagelist from a column array in table.
cpl_vector * gravi_vector_extract(const cpl_vector *vector, int start, int step)
Extract part of a vector.
cpl_error_code gravi_array_add_phasor(cpl_array *input, double complex factor, cpl_array *phase)
Add a REAL phase to a COMPLEX array, in-place: input = input + cexp (factor * phase) ...
cpl_array * gravi_array_cexp(double complex factor, const cpl_array *input)
Compute the complex exponention of an array: cexp (factor * input)
cpl_error_code gravi_table_multiply_scalar(cpl_table *table, const char *name, int base, int nbase, double value)
Multiply scalar or array column by scalar.
cpl_matrix * gravi_matrix_interpolate_col(cpl_matrix *matrix, cpl_vector *xref, cpl_vector *xout)
Linear interpolation of matrix column.
int gravi_table_are_equal(cpl_table *first, cpl_table *second)
Check if two tables have the same content.
cpl_table * gravi_table_extract_time_interval(cpl_table *table, double start, double end)
Extract rows from table based on the TIME column.
cpl_array ** gravi_array_new_list(int n, cpl_type type, int size)
Allocate a list of arrays, pre-filled with 0.0.
cpl_error_code gravi_array_multiply_phasor(cpl_array *input, double complex factor, cpl_array *phase)
Multiply a REAL phase to a COMPLEX array, in-place: input = input * cexp (factor * phase) ...
cpl_error_code gravi_array_get_group_delay_loop(cpl_array **input, cpl_array *sigma, double *gd, cpl_size nrow, double max_width, int verbose)
Optimized computation of GDELAY for a list of arrays.
cpl_error_code gravi_array_add_phase(cpl_array *input, double factor, cpl_array *phase)
Add a REAL phase to a REAL phase array, in-place: input = input + factor * phase. ...
cpl_error_code gravi_image_fill(cpl_image *img, double value)
Fill entire image with value.
cpl_matrix * gravi_matrix_invertSV_create(cpl_matrix *a_in)
Invers a matrix with singular value decomposition.
cpl_array * gravi_array_wrap_image(cpl_image *img)
Wrap the data of na image into an array.
cpl_error_code gravi_table_set_string_fixlen(cpl_table *table, const char *name, int row, const char *value, int len)
Set string in table, ensuring fixed length (right space padding). see cpl_table_set_string.
cpl_error_code gravi_imagelist_unwrap_images(cpl_imagelist *imglist)
Unwrap an imagelist an all its images.
double gravi_image_get_noise_window(cpl_image *img, cpl_size llx, cpl_size lly, cpl_size urx, cpl_size ury)
Estimate the median noise in a window. This noise is estimated as the median{img**2}**0.5, so that it is a robust estimate.
cpl_image * gravi_image_from_column(cpl_table *table_data, const char *data_x, cpl_size row)
Create an image from a column array in table.
double gravi_vector_get_mean_clip(cpl_vector *vector_in, double percent, double nsigma)
Return the mean of a vector after extrema and RMS clipping.
cpl_vector * gravi_table_get_vector_diff(cpl_table *spectrum_data, int index, const char *regname1, const char *regname2)
Create a vector from the row index of the column regname.
double gravi_array_get_quantile(cpl_array *arr, double thr)
Compute the value of the vector corresponding to the quantile 'thr' (0 < thr < 1) ...
cpl_error_code gravi_vector_unwrap_with_guess(cpl_vector *vector, cpl_vector *ref, double ref_to_phase)
Unwrap a phase vector following a guess vector. The difference is actually unwrap and shall thus be s...
cpl_error_code gravi_table_add_scalar(cpl_table *table, const char *name, int base, int nbase, double value)
Multply scalar or array column by scalar.
cpl_size gravi_vector_get_maxpos(cpl_vector *vector)
Return the index of maximum in a vector. If several indexes exists with the maximum value...
cpl_error_code gravi_array_add_phasors(cpl_array *input, cpl_array *add, cpl_array *sub)
Add a pair of COMPLEX arrays to a COMPLEX array, in-place: input = input + add*conj(sub) ...
cpl_image * gravi_image_wrap_matrix(cpl_matrix *matrix)
Wrap matrix into an image (data not duplicated)
cpl_image * gravi_image_wrap_vector(cpl_vector *vector)
Wrap vector into an image (data not duplicated)
cpl_imagelist * gravi_imagelist_wrap_column(cpl_table *table_data, const char *data_x)
Wrap a column array of a table into an imagelist.
cpl_matrix * get_matrix_from_vector(cpl_vector *vector1, cpl_vector *vector2)
Copy the content of two vector into a newly allocated 2D matrix.