38#include <gsl/gsl_matrix.h>
39#include <gsl/gsl_vector.h>
40#include <gsl/gsl_blas.h>
41#include <gsl/gsl_complex.h>
42#include <gsl/gsl_complex_math.h>
43#include <gsl/gsl_statistics.h>
44#include <gsl/gsl_multimin.h>
46#define PI 3.14159265359
47#define TWOPI 6.28318530718
48#define MAS_TO_RAD (2.0 * PI / (3600 * 360 * 1000))
95static gsl_vector *
load_vector(cpl_table *table,
const char *name) CPL_ATTR_ALLOC;
96static gsl_matrix *
load_matrix(cpl_table *table,
const char *name) CPL_ATTR_ALLOC;
97static gsl_matrix_complex *
load_matrix_complex(cpl_table *table,
const char *name) CPL_ATTR_ALLOC;
106static gsl_matrix *
average_matrix_over_dits(gsl_matrix *m,
int ndit,
int nchannel,
int nwave, gsl_matrix_int *flag) CPL_ATTR_ALLOC;
128 const size_t N = a->size;
129 const size_t stride = a->stride;
133 for (i = 0; i < N; i++)
134 sum += a->data[i * stride];
143 cpl_ensure(table != NULL, CPL_ERROR_NULL_INPUT, NULL);
144 cpl_ensure(name != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
146 cpl_size depth = cpl_table_get_column_depth(table, name);
148 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
"Field %s not found", name);
150 }
else if (depth > 0) {
151 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
"Field %s not of scalar type", name);
155 cpl_size nelem = cpl_table_get_nrow(table);
156 double * cpl_vec = cpl_table_get_data_double(table, name);
158 gsl_vector *gsl_vec = gsl_vector_alloc(nelem);
159 memcpy(gsl_vec->data, cpl_vec, nelem *
sizeof(
double));
169 cpl_ensure(table != NULL, CPL_ERROR_NULL_INPUT, NULL);
170 cpl_ensure(name != NULL, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
172 cpl_size depth = cpl_table_get_column_depth(table, name);
174 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
"Field %s not found", name);
176 }
else if (depth == 0) {
177 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
"Field %s not of 2D array type", name);
181 cpl_size nrow = cpl_table_get_nrow(table);
182 cpl_size ncol = cpl_table_get_column_dimension(table, name, 0);
183 const cpl_array ** cpl_matrix = cpl_table_get_data_array_const(table, name);
185 gsl_matrix *gsl_matrix = gsl_matrix_alloc(nrow, ncol);
186 for (
int i = 0; i < nrow; i++) {
187 gsl_vector_const_view row_view = gsl_vector_const_view_array(
188 cpl_array_get_data_double_const(cpl_matrix[i]), ncol);
189 gsl_matrix_set_row(gsl_matrix, i, &row_view.vector);
200 cpl_ensure(table != NULL, CPL_ERROR_NULL_INPUT, NULL);
201 cpl_ensure(name != NULL, CPL_ERROR_ACCESS_OUT_OF_RANGE, NULL);
203 cpl_size depth = cpl_table_get_column_depth(table, name);
205 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
"Field %s not found", name);
207 }
else if (depth == 0) {
208 cpl_error_set_message(cpl_func, CPL_ERROR_INVALID_TYPE,
"Field %s not of 2D array type", name);
212 cpl_size nrow = cpl_table_get_nrow(table);
213 cpl_size ncol = cpl_table_get_column_dimension(table, name, 0);
214 const cpl_array ** cpl_matrix = cpl_table_get_data_array_const(table, name);
216 gsl_matrix_complex *gsl_matrix = gsl_matrix_complex_alloc(nrow, ncol);
217 for (
int i = 0; i < nrow; i++) {
218 for (
int j = 0; j < ncol; j++) {
219 double complex cpl_matrix_val = cpl_array_get_complex(cpl_matrix[i], j, NULL);
220 gsl_complex gsl_matrix_val = gsl_complex_rect(creal(cpl_matrix_val), cimag(cpl_matrix_val));
221 gsl_matrix_complex_set(gsl_matrix, i, j, gsl_matrix_val);
236 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
238 gsl_complex complex_factor = gsl_complex_rect(factor, 0);
239 gsl_matrix_complex_scale(self->
visdata, complex_factor);
240 gsl_matrix_complex_scale(self->
visdata_ft, complex_factor);
241 gsl_matrix_complex_scale(self->
vis_ref, complex_factor);
243 double factor2 = factor * factor;
246 gsl_vector_complex_scale(self->
vis_ref_pcov[i], gsl_complex_rect(factor2, 0));
249 return CPL_ERROR_NONE;
260 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
261 cpl_ensure_code(factor, CPL_ERROR_NULL_INPUT);
262 cpl_ensure_code((
size_t) (self->
ndit * self->
nchannel) == factor->size, CPL_ERROR_INCOMPATIBLE_INPUT);
264 gsl_matrix_complex *complex_factor = gsl_matrix_complex_alloc(self->
ndit * self->
nchannel, self->
nwave);
265 gsl_matrix_complex *complex_factor_ft = gsl_matrix_complex_alloc(self->
ndit * self->
nchannel, self->
nwave_ft);
267 gsl_complex cf = gsl_complex_rect(gsl_vector_get(factor, i), 0);
268 for (
int j = 0; j < self->
nwave; j++)
269 gsl_matrix_complex_set(complex_factor, i, j, cf);
270 for (
int j = 0; j < self->
nwave_ft; j++)
271 gsl_matrix_complex_set(complex_factor_ft, i, j, cf);
274 gsl_matrix_complex_mul_elements(self->
visdata, complex_factor);
275 gsl_matrix_complex_mul_elements(self->
vis_ref, complex_factor);
276 gsl_matrix_complex_mul_elements(self->
visdata_ft, complex_factor_ft);
279 double factor2 = gsl_vector_get(factor, i);
282 gsl_vector_complex_scale(self->
vis_ref_pcov[i], gsl_complex_rect(factor2, 0));
285 FREE(gsl_matrix_complex_free, complex_factor);
286 FREE(gsl_matrix_complex_free, complex_factor_ft);
288 return CPL_ERROR_NONE;
296 gsl_matrix_complex *phi = gsl_matrix_complex_alloc(self->
ndit * self->
nchannel, self->
nwave);
298 for (
int j = 0; j < self->
nwave; j++) {
299 gsl_complex phi_val = gsl_complex_polar(1.0, gsl_matrix_get(phase, i, j));
300 gsl_matrix_complex_set(phi, i, j, phi_val);
304 gsl_matrix_complex_mul_elements(self->
vis_ref, phi);
306 gsl_vector_complex *cov = gsl_vector_complex_alloc(self->
nwave);
307 gsl_vector_complex *pcov = gsl_vector_complex_alloc(self->
nwave);
308 gsl_vector_complex *tmp = gsl_vector_complex_alloc(self->
nwave);
311 gsl_vector_complex_set_zero(cov);
312 gsl_vector_complex_set_zero(pcov);
313 gsl_vector_complex_set_zero(tmp);
316 gsl_vector_complex_const_view phi_row = gsl_matrix_complex_const_row(phi, i);
317 gsl_vector_complex_memcpy(tmp, &phi_row.vector);
318 gsl_vector_view phi_imag = gsl_vector_complex_imag(tmp);
319 gsl_vector_scale(&phi_imag.vector, -1);
322 gsl_vector_view cov_real = gsl_vector_complex_real(cov);
323 gsl_vector_memcpy(&cov_real.vector, self->
vis_ref_cov[i]);
326 gsl_vector_complex_mul(tmp, cov);
327 gsl_vector_complex_mul(tmp, &phi_row.vector);
328 cov_real = gsl_vector_complex_real(tmp);
329 gsl_vector_memcpy(self->
vis_ref_cov[i], &cov_real.vector);
333 gsl_vector_complex_mul(pcov, &phi_row.vector);
334 gsl_vector_complex_mul(pcov, &phi_row.vector);
338 FREE(gsl_vector_complex_free, cov);
339 FREE(gsl_vector_complex_free, pcov);
340 FREE(gsl_vector_complex_free, tmp);
341 FREE(gsl_matrix_complex_free, phi);
343 return CPL_ERROR_NONE;
352 gsl_matrix_memcpy(this_u, self->
ucoord);
353 gsl_matrix_scale(this_u, xvalue);
356 gsl_matrix_memcpy(this_v, self->
vcoord);
357 gsl_matrix_scale(this_v, yvalue);
359 gsl_matrix_add(this_u, this_v);
364 FREE(gsl_matrix_free, this_u);
365 FREE(gsl_matrix_free, this_v);
367 return CPL_ERROR_NONE;
375 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
379 gsl_vector_complex_const_view visft_row = gsl_matrix_complex_const_row(self->
visdata_ft, i);
380 double row_abs_sum = 0.0;
381 for (
int j = 0; j < self->
nwave_ft; j++)
382 row_abs_sum += gsl_complex_abs(gsl_vector_complex_get(&visft_row.vector, j));
385 if (row_abs_sum < threshold) {
387 for (
int k = 0; k < self->
nwave; k++) {
388 gsl_matrix_int_set(self->
flag, i, k, 1);
389 gsl_matrix_complex_set(self->
visdata, i, k, GSL_COMPLEX_ZERO);
414 cpl_msg_debug(cpl_func,
"Flagging %d points below FT threshold %.3e", npoints, threshold);
415 self->
nflag += npoints;
417 return CPL_ERROR_NONE;
425 gsl_vector *ftflux_scale = gsl_vector_alloc(self->
ndit * self->
nchannel);
428 for (
int j = 0; j < self->
nwave_ft; j++)
429 scale += gsl_complex_abs(gsl_matrix_complex_get(self->
visdata_ft, i, j));
430 gsl_vector_set(ftflux_scale, i, self->
nwave / scale);
434 FREE(gsl_vector_free, ftflux_scale);
435 return CPL_ERROR_NONE;
443 cpl_ensure(data != NULL, CPL_ERROR_NULL_INPUT, NULL);
449 const char *filename = cpl_propertylist_get_string(hdr,
"PIPEFILE");
450 self->
filename = cpl_strdup(filename);
452 const char *insname = cpl_propertylist_get_string(hdr,
"TELESCOP");
453 self->
insname = cpl_strdup(insname);
456 if (cpl_propertylist_has(hdr,
"ESO TPL NDIT OBJECT"))
457 self->
ndit = cpl_propertylist_get_int(hdr,
"ESO TPL NDIT OBJECT");
459 self->
ndit = cpl_propertylist_get_int(hdr,
"ESO TPL NDIT SKY");
460 self->
dit = cpl_propertylist_get_double(hdr,
"ESO DET2 SEQ1 DIT");
462 self->
swap = (!strcmp(cpl_propertylist_get_string(hdr,
"ESO INS SOBJ SWAP"),
"YES"));
463 cpl_msg_debug(cpl_func,
"SWAP keyword is: %s", cpl_propertylist_get_string(hdr,
"ESO INS SOBJ SWAP"));
467 self->
sobj_x = cpl_propertylist_get_double(hdr,
"ESO INS SOBJ X");
468 self->
sobj_y = cpl_propertylist_get_double(hdr,
"ESO INS SOBJ Y");
471 self->
mjd = cpl_propertylist_get_double(hdr,
"MJD-OBS");
479 self->
nwave = cpl_propertylist_get_int(wave_plist,
"NWAVE");
482 self->
nwave_ft = cpl_propertylist_get_int(wave_plist,
"NWAVE");
486 cpl_table_cast_column(wave_table,
"EFF_WAVE",
"EFF_WAVE", CPL_TYPE_DOUBLE);
498 for (
int j = 0; j < self->
nwave; j++) {
499 gsl_vector_view col_view;
500 col_view = gsl_matrix_column(self->
ucoord, j);
501 gsl_vector_memcpy(&col_view.vector, self->
u);
502 col_view = gsl_matrix_column(self->
vcoord, j);
503 gsl_vector_memcpy(&col_view.vector, self->
v);
507 gsl_vector_view row_view;
508 row_view = gsl_matrix_row(self->
ucoord, i);
509 gsl_vector_div(&row_view.vector, self->
wave);
510 row_view = gsl_matrix_row(self->
vcoord, i);
511 gsl_vector_div(&row_view.vector, self->
wave);
523 const cpl_array ** cpl_flag = cpl_table_get_data_array_const(oivis_table,
"FLAG");
524 const int* cpl_rej = NULL;
525 if (cpl_table_has_column(oivis_table,
"REJECTION_FLAG"))
526 cpl_rej = cpl_table_get_data_int_const(oivis_table,
"REJECTION_FLAG");
532 int rej_val = (cpl_rej ? cpl_rej[i] : 0) > 0;
533 for (
int j = 0; j < self->
nwave; j++) {
534 int flag_val = cpl_array_get_int(cpl_flag[i], j, NULL);
536 gsl_matrix_int_set(self->
flag, i, j, flag_val);
537 self->
nflag += flag_val;
544 for (
int j = 0; j < self->
nwave; j++) {
545 if (gsl_matrix_int_get(self->
flag, i, j)) {
547 gsl_matrix_complex_set(self->
visdata, i, j, GSL_COMPLEX_ZERO);
551 cpl_msg_debug(cpl_func,
"Cleaned %d flagged values in VISDATA", ncleaned);
570 for (
int j = 0; j < self->
nwave; j++) {
571 gsl_complex vis_ref_val = gsl_matrix_complex_get(self->
visdata, i, j);
572 gsl_complex pha = gsl_complex_polar(1.0, gsl_matrix_get(self->
phase_ref, i, j));
573 vis_ref_val = gsl_complex_mul(vis_ref_val, pha);
574 gsl_matrix_complex_set(self->
vis_ref, i, j, vis_ref_val);
582 gsl_vector *vreal = gsl_vector_alloc(self->
nwave);
583 gsl_vector *vimag = gsl_vector_alloc(self->
nwave);
586 gsl_vector_complex_const_view err_vals = gsl_matrix_complex_const_row(self->
viserr, i);
587 gsl_vector_const_view real_vals = gsl_vector_complex_const_real(&err_vals.vector);
588 gsl_vector_const_view imag_vals = gsl_vector_complex_const_imag(&err_vals.vector);
590 gsl_vector_memcpy(vreal, &real_vals.vector);
591 gsl_vector_mul(vreal, vreal);
593 gsl_vector_memcpy(vimag, &imag_vals.vector);
594 gsl_vector_mul(vimag, vimag);
597 gsl_vector *cov_diag = gsl_vector_alloc(self->
nwave);
598 gsl_vector_memcpy(cov_diag, vreal);
599 gsl_vector_add(cov_diag, vimag);
603 gsl_vector_complex *pcov_diag = gsl_vector_complex_calloc(self->
nwave);
604 gsl_vector_view pcov_diag_real = gsl_vector_complex_real(pcov_diag);
605 gsl_vector_memcpy(&pcov_diag_real.vector, vreal);
606 gsl_vector_sub(&pcov_diag_real.vector, vimag);
608 for (
int j = 0; j < self->
nwave; j++) {
610 gsl_complex pcov_val = gsl_complex_polar(1.0, 2 * gsl_matrix_get(self->
phase_ref, i, j));
611 pcov_val = gsl_complex_mul(gsl_vector_complex_get(pcov_diag, j), pcov_val);
612 gsl_vector_complex_set(pcov_diag, j, pcov_val);
616 FREE(gsl_vector_free, vreal);
617 FREE(gsl_vector_free, vimag);
623 gsl_matrix *wave_opd_disp = gsl_matrix_alloc(self->
ndit * self->
nchannel, self->
nwave);
624 gsl_matrix_memcpy(wave_opd_disp, self->
opd_disp);
626 gsl_vector *wavenumber = gsl_vector_alloc(self->
nwave);
627 gsl_vector_set_all(wavenumber,
TWOPI);
628 gsl_vector_div(wavenumber, self->
wave);
632 gsl_vector_view wave_opd_dist_row = gsl_matrix_row(wave_opd_disp, i);
633 gsl_vector_mul(&wave_opd_dist_row.vector, wavenumber);
638 gsl_matrix_scale(wave_opd_disp, -1);
641 FREE(gsl_vector_free, wavenumber);
642 FREE(gsl_matrix_free, wave_opd_disp);
649 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
650 cpl_ensure_code(handle, CPL_ERROR_NULL_INPUT);
652 fprintf(handle,
"%s\n", self->
filename);
653 fprintf(handle,
"%s\n", self->
insname);
657 gsl_matrix_fprintf(handle, self->
ucoord,
"%f");
658 gsl_matrix_fprintf(handle, self->
vcoord,
"%f");
659 gsl_matrix_int_fprintf(handle, self->
flag,
"%d");
660 gsl_matrix_complex_fprintf(handle, self->
visdata,
"%f");
661 gsl_matrix_complex_fprintf(handle, self->
vis_ref,
"%f");
663 return CPL_ERROR_NONE;
671 FREE(gsl_vector_free, self->
u);
672 FREE(gsl_vector_free, self->
v);
681 FREE(gsl_matrix_int_free, self->
flag);
697 cpl_ensure(self, CPL_ERROR_NULL_INPUT, -1);
701 for (
int j = 0; j < self->
nwave_ft; j++) {
702 abs_val += gsl_complex_abs(gsl_matrix_complex_get(self->
visdata_ft, i, j));
721 cpl_ensure(self, CPL_ERROR_NULL_INPUT, -1);
722 cpl_ensure(others, CPL_ERROR_NULL_INPUT, -1);
724 cpl_boolean any_before = FALSE, any_after = FALSE;
725 for (
int i = 0; i < n_other; i++) {
726 double delta = self->
mjd - others[i]->
mjd;
728 any_before = CPL_TRUE;
730 any_after = CPL_TRUE;
734 double min_delta = INFINITY;
736 for (
int i = 0; i < n_other; i++) {
737 double delta = self->
mjd - others[i]->
mjd;
740 if (any_before && delta >= 0 && delta < min_delta) {
746 if (any_after && delta <= 0 && -delta < min_delta) {
752 if (delta <= 0 && fabs(delta) < min_delta) {
774 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
775 cpl_ensure_code(phase_refs, CPL_ERROR_NULL_INPUT);
777 cpl_ensure_code(swaps, CPL_ERROR_NULL_INPUT);
778 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
780 gsl_matrix *amp_ref = NULL, *amp_ref_tmp = NULL;
781 gsl_matrix_complex *vis_ref = NULL, *vis_ref_tmp = NULL;
784 char *calib_strategy = cpl_strdup(cpl_parameter_get_string(
785 cpl_parameterlist_find(parlist,
"gravity.astrometry.calib-strategy")));
787 for(
int i = 0; calib_strategy[i]; i++)
788 calib_strategy[i] = toupper(calib_strategy[i]);
792 cpl_msg_debug(cpl_func,
"Calibrating amplitude reference with '%s' strategy", calib_strategy);
794 if (!strcmp(calib_strategy,
"NONE")) {
796 vis_ref = gsl_matrix_complex_alloc(self->
nchannel, self->
nwave);
797 gsl_matrix_set_all(amp_ref, 1.0);
798 gsl_matrix_complex_set_all(vis_ref, GSL_COMPLEX_ONE);
799 }
else if (!strcmp(calib_strategy,
"SELF")) {
803 for (
int j = 0; j < self->
nwave; j++) {
804 gsl_complex vis_ref_val = gsl_matrix_complex_get(self->
vis_ref, i, j);
805 gsl_matrix_set(amp_ref_tmp, i, j, gsl_complex_abs(vis_ref_val));
809 FREE(gsl_matrix_free, amp_ref_tmp);
811 vis_ref = gsl_matrix_complex_alloc(self->
nchannel, self->
nwave);
812 for (
int i = 0; i < self->
nchannel; i++) {
813 for (
int j = 0; j < self->
nwave; j++) {
814 double amp_ref_val = gsl_matrix_get(amp_ref, i, j);
815 gsl_matrix_complex_set(vis_ref, i, j, gsl_complex_rect(amp_ref_val, 0));
820 gsl_vector_int *phase_indices;
822 if (!strcmp(calib_strategy,
"ALL")) {
823 nphase_used = nphase;
824 phase_indices = gsl_vector_int_alloc(nphase_used);
825 for (
int n = 0; n < nphase_used; n++)
826 gsl_vector_int_set(phase_indices, n, n);
827 }
else if (!strcmp(calib_strategy,
"NEAREST")) {
829 phase_indices = gsl_vector_int_alloc(nphase_used);
835 if (idx_before == -1 || idx_after == -1) {
836 cpl_error_set_message(cpl_func, CPL_ERROR_ACCESS_OUT_OF_RANGE,
"Cannot find time-bracketing phase references");
837 return CPL_ERROR_ACCESS_OUT_OF_RANGE;
839 gsl_vector_int_set(phase_indices, 0, idx_before);
840 gsl_vector_int_set(phase_indices, 1, idx_after);
842 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Unknown calibration strategy %s", calib_strategy);
843 return CPL_ERROR_ILLEGAL_INPUT;
847 vis_ref = gsl_matrix_complex_calloc(self->
nchannel, self->
nwave);
850 for (
int n = 0; n < nphase_used; n++) {
851 cpl_size idx = gsl_vector_int_get(phase_indices, n);
858 for (
int i = 0; i < soi->
nchannel; i++) {
859 for (
int j = 0; j < soi->
nwave; j++) {
860 gsl_complex vis_ref_val = gsl_matrix_complex_get(vis_ref_tmp, i, j);
861 gsl_matrix_set(amp_ref_tmp, i, j, gsl_complex_abs(vis_ref_val));
865 gsl_matrix_add(amp_ref, amp_ref_tmp);
866 gsl_matrix_complex_add(vis_ref, vis_ref_tmp);
867 FREE(gsl_matrix_complex_free, vis_ref_tmp);
870 gsl_matrix_scale(amp_ref, 1.0 / nphase_used);
871 gsl_matrix_complex_scale(vis_ref, gsl_complex_rect(1.0 / nphase_used, 0.0));
873 for (
int i = 0; i < self->
nchannel; i++) {
874 for (
int j = 0; j < self->
nwave; j++) {
875 double theta = gsl_complex_arg(gsl_matrix_complex_get(vis_ref, i, j));
876 double r = gsl_matrix_get(amp_ref, i, j);
877 gsl_matrix_complex_set(vis_ref, i, j, gsl_complex_polar(r, theta));
880 FREE(gsl_matrix_free, amp_ref_tmp);
881 FREE(gsl_vector_int_free, phase_indices);
889 cpl_msg_debug(cpl_func,
"Extract phase reference from swaps");
895 if (!strcmp(calib_strategy,
"SWAP")) {
896 cpl_size n_swapon = 0;
898 gsl_matrix_set_zero(amp_ref);
899 gsl_matrix_complex_set_zero(vis_ref);
901 for (
int n = 0; n < nswap; n++) {
909 for (
int j = 0; j < soi->
nwave; j++) {
910 gsl_complex vis_ref_val = gsl_matrix_complex_get(soi->
vis_ref, i, j);
911 gsl_matrix_set(amp_ref_tmp, i, j, gsl_complex_abs(vis_ref_val));
915 gsl_matrix_add(amp_ref, amp_ref_avg);
917 FREE(gsl_matrix_free, amp_ref_tmp);
918 FREE(gsl_matrix_free, amp_ref_avg);
922 gsl_matrix_scale(amp_ref, 1.0 / n_swapon);
923 for (
int i = 0; i < self->
nchannel; i++) {
924 for (
int j = 0; j < self->
nwave; j++) {
926 double amp_ref_val = gsl_matrix_get(amp_ref, i, j);
927 gsl_matrix_complex_set(vis_ref, i, j, gsl_complex_rect(0.5 * amp_ref_val, 0));
934 for (
int i = 0; i < self->
nchannel; i++) {
935 for (
int j = 0; j < self->
nwave; j++) {
936 gsl_complex phi = gsl_matrix_complex_get(swap_ref, i, j);
937 double argphi = gsl_complex_arg(phi);
939 gsl_complex vis_ref_val = gsl_matrix_complex_get(vis_ref, i, j);
940 double vis_ref_abs = gsl_complex_abs(vis_ref_val);
942 gsl_matrix_complex_set(vis_ref, i, j, gsl_complex_polar(2 * vis_ref_abs, argphi));
947 cpl_free(calib_strategy);
948 return CPL_ERROR_NONE;
956 cpl_table *table = cpl_table_new(self->
nchannel);
957 cpl_table_new_column_array(table,
"ASTRO_VISREF", CPL_TYPE_DOUBLE_COMPLEX, self->
nwave);
958 cpl_table_new_column_array(table,
"ASTRO_AMPREF", CPL_TYPE_DOUBLE, self->
nwave);
959 cpl_table_new_column_array(table,
"ASTRO_COV", CPL_TYPE_DOUBLE, self->
ndit * self->
nwave);
960 cpl_table_new_column_array(table,
"ASTRO_PCOV", CPL_TYPE_DOUBLE_COMPLEX, self->
ndit * self->
nwave);
962 cpl_array *tmp_arr = cpl_array_new(self->
nwave, CPL_TYPE_DOUBLE_COMPLEX);
963 for (
int i = 0; i < self->
nchannel; i++) {
964 for (
int j = 0; j < self->
nwave; j++) {
966 cpl_array_set_complex(tmp_arr, j, GSL_REAL(val) + I * GSL_IMAG(val));
968 cpl_table_set_array(table,
"ASTRO_VISREF", i, tmp_arr);
970 FREE(cpl_array_delete, tmp_arr);
972 tmp_arr = cpl_array_new(self->
nwave, CPL_TYPE_DOUBLE);
973 for (
int i = 0; i < self->
nchannel; i++) {
974 for (
int j = 0; j < self->
nwave; j++) {
976 cpl_array_set(tmp_arr, j, val);
978 cpl_table_set_array(table,
"ASTRO_AMPREF", i, tmp_arr);
980 FREE(cpl_array_delete, tmp_arr);
983 tmp_arr = cpl_array_new(self->
ndit * self->
nwave, CPL_TYPE_DOUBLE);
984 for (
int i = 0; i < self->
nchannel; i++) {
985 for (
int j = 0; j < self->
ndit; j++) {
986 for (
int k = 0; k < self->
nwave; k++) {
988 cpl_array_set(tmp_arr, k + j * self->
nwave, val);
991 cpl_table_set_array(table,
"ASTRO_COV", i, tmp_arr);
994 FREE(cpl_array_delete, tmp_arr);
997 tmp_arr = cpl_array_new(self->
ndit * self->
nwave, CPL_TYPE_DOUBLE_COMPLEX);
998 for (
int i = 0; i < self->
nchannel; i++) {
999 for (
int j = 0; j < self->
ndit; j++) {
1000 for (
int k = 0; k < self->
nwave; k++) {
1002 cpl_array_set_complex(tmp_arr, k + j * self->
nwave, GSL_REAL(val) + I * GSL_IMAG(val));
1005 cpl_table_set_array(table,
"ASTRO_PCOV", i, tmp_arr);
1008 FREE(cpl_array_delete, tmp_arr);
1015 gsl_vector *vavg = gsl_vector_alloc(nchannel);
1017 for (
int i = 0; i < nchannel; i++) {
1018 double avg = gsl_stats_mean(v->data + i, nchannel, ndit);
1019 gsl_vector_set(vavg, i, avg);
1027 gsl_matrix *mavg = gsl_matrix_alloc(nchannel, nwave);
1029 for (
int i = 0; i < nchannel; i++) {
1030 for (
int j = 0; j < nwave; j++) {
1031 cpl_size start = i * nwave + j;
1032 gsl_vector_const_view dit_view = gsl_vector_const_view_array_with_stride(
1033 m->data + start, nwave * nchannel, ndit);
1034 gsl_vector_int_const_view flag_view = flag ?
1035 gsl_vector_int_const_view_array_with_stride(flag->data + start, nwave * nchannel, ndit) :
1036 (gsl_vector_int_const_view){};
1039 cpl_size Nvalid = 0;
1040 for (
int k = 0; k < ndit; k++) {
1041 if (!flag || gsl_vector_int_get(&flag_view.vector, k) == 0) {
1042 avg += gsl_vector_get(&dit_view.vector, k);
1048 gsl_matrix_set(mavg, i, j, avg);
1050 gsl_matrix_set(mavg, i, j, 0.0);
1060 gsl_matrix_complex *mavg = gsl_matrix_complex_alloc(nchannel, nwave);
1062 for (
int i = 0; i < nchannel; i++) {
1063 for (
int j = 0; j < nwave; j++) {
1064 cpl_size start = i * nwave + j;
1065 gsl_vector_complex_const_view dit_view = gsl_vector_complex_const_view_array_with_stride(
1066 m->data + 2 * start, nwave * nchannel, ndit);
1067 gsl_vector_int_const_view flag_view = flag ?
1068 gsl_vector_int_const_view_array_with_stride(flag->data + start, nwave * nchannel, ndit) :
1069 (gsl_vector_int_const_view){};
1071 gsl_complex avg = GSL_COMPLEX_ZERO;
1072 cpl_size Nvalid = 0;
1073 for (
int k = 0; k < ndit; k++) {
1074 if (!flag || gsl_vector_int_get(&flag_view.vector, k) == 0) {
1075 avg = gsl_complex_add(avg, gsl_vector_complex_get(&dit_view.vector, k));
1080 avg = gsl_complex_div_real(avg, Nvalid);
1081 gsl_matrix_complex_set(mavg, i, j, avg);
1083 gsl_matrix_complex_set(mavg, i, j, GSL_COMPLEX_ZERO);
1096 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1117 flat->
wave = gsl_vector_alloc(flat->
nwave);
1118 gsl_vector_memcpy(flat->
wave, self->
wave);
1136 for (
int i = 0; i < self->
nchannel; i++) {
1140 for (
int j = 0; j < self->
ndit; j++) {
1141 cpl_size idx = j * self->
nchannel + i;
1146 gsl_vector_complex_scale(flat->
vis_ref_pcov[i], gsl_complex_rect(1.0 / (self->
ndit * self->
ndit), 0));
1151 for (
int i = 0; i < self->
nchannel; i++) {
1152 for (
int j = 0; j < self->
nwave; j++) {
1153 cpl_size start = i * self->
nwave + j;
1154 gsl_vector_int_const_view flag_view = gsl_vector_int_const_view_array_with_stride(
1158 gsl_matrix_int_set(flat->
flag, i, j, 1);
1174 gsl_matrix_complex *visref = gsl_matrix_complex_calloc(data[0]->nchannel, data[0]->nwave);
1175 gsl_matrix_complex *visref_ditsum = gsl_matrix_complex_alloc(data[0]->nchannel, data[0]->nwave);
1177 gsl_matrix_int *ngood = gsl_matrix_int_calloc(data[0]->nchannel, data[0]->nwave);
1178 gsl_matrix_int *ngood_ditsum = gsl_matrix_int_alloc(data[0]->nchannel, data[0]->nwave);
1180 for (
int n = 0; n < ndata; n++) {
1183 for (
int j = 0; j < self->
nchannel; j++) {
1184 for (
int k = 0; k < self->
nwave; k++) {
1186 gsl_complex visref_jk = GSL_COMPLEX_ZERO;
1188 for (
int i = 0; i < self->
ndit; i++) {
1189 cpl_size idx = i * self->
nchannel + j;
1190 double uv = gsl_matrix_get(self->
ucoord, idx, k) * ra + gsl_matrix_get(self->
vcoord, idx, k) * dec;
1193 gsl_complex phi = gsl_complex_polar(1.0, phase);
1195 phi = gsl_complex_conjugate(phi);
1196 gsl_complex visref_ijk = gsl_complex_mul(phi, gsl_matrix_complex_get(self->
vis_ref, idx, k));
1198 int flag = gsl_matrix_int_get(self->
flag, idx, k);
1201 visref_jk = gsl_complex_add(visref_jk, visref_ijk);
1204 gsl_matrix_complex_set(visref_ditsum, j, k, visref_jk);
1205 gsl_matrix_int_set(ngood_ditsum, j, k, ngood_jk);
1208 gsl_matrix_complex_add(visref, visref_ditsum);
1209 gsl_matrix_int_add(ngood, ngood_ditsum);
1212 for (
int j = 0; j < data[0]->
nchannel; j++) {
1213 for (
int k = 0; k < data[0]->
nwave; k++) {
1214 int ngood_jk = gsl_matrix_int_get(ngood, j, k);
1216 gsl_complex visref_jk = gsl_matrix_complex_get(visref, j, k);
1217 visref_jk = gsl_complex_div_real(visref_jk, ngood_jk);
1218 gsl_matrix_complex_set(visref, j, k, visref_jk);
1223 FREE(gsl_matrix_complex_free, visref_ditsum);
1239 cpl_size nchannel = s1[0]->
nchannel;
1240 cpl_size nwave = s1[0]->
nwave;
1242 double ra = gsl_vector_get(X, 0);
1243 double dec = gsl_vector_get(X, 1);
1249 for (
int i = 0; i < nchannel; i++) {
1250 double chi2_channel = 0;
1251 for (
int k = 0; k < nwave; k++) {
1252 gsl_complex visref_swap = gsl_complex_mul(
1253 gsl_matrix_complex_get(visref_s1, i, k),
1254 gsl_complex_conjugate(gsl_matrix_complex_get(visref_s2, i, k))
1256 visref_swap = gsl_complex_sqrt(visref_swap);
1257 chi2_channel += GSL_IMAG(visref_swap) * GSL_IMAG(visref_swap);
1259 chi2 += chi2_channel;
1262 FREE(gsl_matrix_complex_free, visref_s1);
1263 FREE(gsl_matrix_complex_free, visref_s2);
1270 cpl_ensure_code(ra_grid, CPL_ERROR_NULL_INPUT);
1271 cpl_ensure_code(dec_grid, CPL_ERROR_NULL_INPUT);
1272 cpl_ensure_code(ra_dec_out, CPL_ERROR_NULL_INPUT);
1274 cpl_size n_ra = ra_grid->size;
1275 cpl_size n_dec = dec_grid->size;
1276 double ra_dec[2] = {0};
1277 gsl_vector_const_view ra_dec_view = gsl_vector_const_view_array(ra_dec, 2);
1278 double best_ra, best_dec, best_chi2 = INFINITY;
1280 for (
int i = 0; i < n_ra; i++) {
1281 ra_dec[0] = gsl_vector_get(ra_grid, i);
1282 cpl_msg_debug(cpl_func,
"calculating chi2 for ra=%f", ra_dec[0]);
1283 for (
int j = 0; j < n_dec; j++) {
1284 ra_dec[1] = gsl_vector_get(dec_grid, j);
1287 &ra_dec_view.vector, params
1291 gsl_matrix_set(chi2_map, i, j, chi2);
1293 if (chi2 < best_chi2) {
1294 best_ra = ra_dec[0];
1295 best_dec = ra_dec[1];
1302 best_chi2, best_ra, best_dec);
1304 gsl_vector_set(ra_dec_out, 0, best_ra);
1305 gsl_vector_set(ra_dec_out, 1, best_dec);
1306 return CPL_ERROR_NONE;
1314 const int MAX_ITERATIONS = 1000;
1315 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex2rand;
1316 gsl_multimin_fminimizer *mini = gsl_multimin_fminimizer_alloc(T, dim);
1320 gsl_multimin_function func = {
1326 double initial_guess[] = {ra_guess, dec_guess};
1327 gsl_vector_view guess_view = gsl_vector_view_array(initial_guess, dim);
1328 double step_size[] = {0.1, 0.1};
1329 gsl_vector_view step_view = gsl_vector_view_array(step_size, dim);
1330 gsl_multimin_fminimizer_set(mini, &func, &guess_view.vector, &step_view.vector);
1335 status = gsl_multimin_fminimizer_iterate(mini);
1341 size = gsl_multimin_fminimizer_size(mini);
1342 status = gsl_multimin_test_size(size, 1e-3);
1343 }
while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS);
1345 if (status == GSL_SUCCESS)
1346 gsl_vector_memcpy(Xsolve, mini->x);
1348 if (status == GSL_CONTINUE)
1349 cpl_msg_warning(cpl_func,
"model-fitting did not converge");
1350 else if (status != GSL_SUCCESS)
1351 cpl_msg_error(cpl_func,
"model-fitting failed with error code %d (%s)", status, gsl_strerror(status));
1353 cpl_msg_debug(cpl_func,
"converged with best-fit:\nra\tdec\tchi2\n%.3f\t%.3f\t%.4f", mini->x->data[0], mini->x->data[1], mini->fval);
1355 FREE(gsl_multimin_fminimizer_free, mini);
1356 return status ? CPL_ERROR_ILLEGAL_OUTPUT : CPL_ERROR_NONE;
1361 cpl_ensure_code(swap_data, CPL_ERROR_NULL_INPUT);
1362 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
1365 cpl_size n1 = 0, n2 = 0;
1367 gsl_vector *ra_grid = NULL, *dec_grid = NULL;
1369 gsl_vector *ra_dec_fit = NULL;
1370 gsl_matrix *chi2_map = NULL;
1373 cpl_boolean use_swap_fiber_pos = cpl_parameter_get_bool(
1374 cpl_parameterlist_find(parlist,
"gravity.astrometry.use-swap-fiber-pos"));
1376 cpl_boolean go_fast = cpl_parameter_get_bool(
1377 cpl_parameterlist_find(parlist,
"gravity.astrometry.average-over-dits"));
1379 double ra_lim = cpl_parameter_get_double(
1380 cpl_parameterlist_find(parlist,
"gravity.astrometry.ra-lim-swap"));
1381 int n_ra = cpl_parameter_get_int(
1382 cpl_parameterlist_find(parlist,
"gravity.astrometry.nra-swap"));
1384 double dec_lim = cpl_parameter_get_double(
1385 cpl_parameterlist_find(parlist,
"gravity.astrometry.ra-lim-swap"));
1386 int n_dec = cpl_parameter_get_int(
1387 cpl_parameterlist_find(parlist,
"gravity.astrometry.ndec-swap"));
1389 double zoom = cpl_parameter_get_double(
1390 cpl_parameterlist_find(parlist,
"gravity.astrometry.zoom-factor"));
1394 if (use_swap_fiber_pos) {
1395 cpl_msg_warning(cpl_func,
"No astrometric solution is computed for swaps. Default to fiber position");
1396 for (
int i = 0; i < nswap; i++) {
1406 return CPL_ERROR_NONE;
1410 for (
int i = 0; i < nswap; i++) {
1411 if (swap_data[i]->swap)
1418 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"No files with SWAP=YES");
1420 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"No files with SWAP=NO");
1423 group1 = cpl_malloc(n1 *
sizeof(
astro_data*));
1424 group2 = cpl_malloc(n2 *
sizeof(
astro_data*));
1425 cpl_size i1 = 0, i2 = 0;
1427 for (
int i = 0; i < nswap; i++) {
1428 if (swap_data[i]->swap) {
1436 .
group1 = group1, .group2 = group2, .n1 = n1, .n2 = n2
1441 if (strstr(swap_data[0]->insname,
"U1234"))
1449 dec_lim = field / 2;
1451 double avg_sx = 0.0, avg_sy = 0.0;
1452 for (
int i = 0; i < nswap; i++) {
1453 avg_sx += swap_data[i]->
sobj_x * (swap_data[i]->
swap ? -1 : 1);
1454 avg_sy += swap_data[i]->
sobj_y * (swap_data[i]->
swap ? -1 : 1);
1458 cpl_msg_debug(cpl_func,
"Centre: [%f, %f] mas", avg_sx, avg_sy);
1460 double ra_fit, dec_fit;
1461 double ra_min = avg_sx - ra_lim, ra_max = avg_sx + ra_lim;
1462 double dec_min = avg_sy - dec_lim, dec_max = avg_sy + dec_lim;
1463 double d_ra = (ra_max - ra_min) / (n_ra - 1);
1464 double d_dec = (dec_max - dec_min) / (n_dec - 1);
1466 ra_grid = gsl_vector_alloc(n_ra);
1467 dec_grid = gsl_vector_alloc(n_dec);
1468 for (
int i = 0; i < n_ra; i++)
1469 gsl_vector_set(ra_grid, i, ra_min + i * d_ra);
1470 for (
int j = 0; j < n_dec; j++)
1471 gsl_vector_set(dec_grid, j, dec_min + j * d_dec);
1473 ra_dec_fit = gsl_vector_alloc(2);
1474 chi2_map = gsl_matrix_alloc(n_ra, n_dec);
1477 cpl_msg_debug(cpl_func,
"RA grid: [%f, %f] with %d points", ra_min, ra_max, n_ra);
1478 cpl_msg_debug(cpl_func,
"dec grid: [%f, %f] with %d points", dec_min, dec_max, n_dec);
1481 ra_fit = gsl_vector_get(ra_dec_fit, 0);
1482 dec_fit = gsl_vector_get(ra_dec_fit, 1);
1483 cpl_msg_debug(cpl_func,
"big chi2 map minimsed for ra=%f, dec=%f", ra_fit, dec_fit);
1489 ra_lim = (ra_max - ra_min) / (4 * zoom);
1490 dec_lim = (dec_max - dec_min) / (4 * zoom);
1492 ra_min = ra_fit - ra_lim;
1493 ra_max = ra_fit + ra_lim;
1494 dec_min = dec_fit - dec_lim;
1495 dec_max = dec_fit + dec_lim;
1497 d_ra = (ra_max - ra_min) / (n_ra - 1);
1498 d_dec = (dec_max - dec_min) / (n_dec - 1);
1500 for (
int i = 0; i < n_ra; i++)
1501 gsl_vector_set(ra_grid, i, ra_min + i * d_ra);
1502 for (
int j = 0; j < n_dec; j++)
1503 gsl_vector_set(dec_grid, j, dec_min + j * d_dec);
1506 cpl_msg_debug(cpl_func,
"RA grid: [%f, %f] with %d points", ra_min, ra_max, n_ra);
1507 cpl_msg_debug(cpl_func,
"dec grid: [%f, %f] with %d points", dec_min, dec_max, n_dec);
1510 ra_fit = gsl_vector_get(ra_dec_fit, 0);
1511 dec_fit = gsl_vector_get(ra_dec_fit, 1);
1512 cpl_msg_debug(cpl_func,
"zoomed chi2 map minimised for ra=%f, dec=%f", ra_fit, dec_fit);
1518 for (
int i = 0; i < nswap; i++) {
1528 ra_fit = gsl_vector_get(ra_dec_fit, 0);
1529 dec_fit = gsl_vector_get(ra_dec_fit, 1);
1530 cpl_msg_debug(cpl_func,
"final astrometric solution after gradient descent is ra=%f, dec=%f", ra_fit, dec_fit);
1532 for (
int i = 0; i < nswap; i++) {
1542 CPLCHECK_MSG(
"Could not extract swap phase reference");
1544 for (
int i = 1; i < nswap; i++)
1545 swap_data[i]->phase_ref_astro = gsl_matrix_complex_alloc_from_matrix(swap_ref, 0, 0, swap_ref->size1, swap_ref->size2);
1552 FREE(cpl_free, group1);
1553 FREE(cpl_free, group2);
1556 FREE(gsl_matrix_free, chi2_map);
1557 FREE(gsl_vector_free, ra_grid);
1558 FREE(gsl_vector_free, dec_grid);
1559 FREE(gsl_vector_free, ra_dec_fit);
1561 return CPL_ERROR_NONE;
1572 cpl_ensure(swap_data, CPL_ERROR_NULL_INPUT, NULL);
1574 int nchannel = swap_data[0]->
nchannel;
1575 int nwave = swap_data[0]->
nwave;
1577 gsl_matrix_complex *phase_ref_s1 = gsl_matrix_complex_calloc(nchannel, nwave);
1578 gsl_matrix_complex *phase_ref_s2 = gsl_matrix_complex_calloc(nchannel, nwave);
1579 gsl_matrix_complex *phase_ref_avg = gsl_matrix_complex_calloc(nchannel, nwave);
1581 for (
int n = 0; n < nswap; n++) {
1585 soi->
nwave == nwave,
1586 CPL_ERROR_INCOMPATIBLE_INPUT,
1600 gsl_matrix_complex_add(phase_ref_s1, vis_ref_avg);
1602 gsl_matrix_complex_add(phase_ref_s2, vis_ref_avg);
1603 FREE(gsl_matrix_complex_free, vis_ref_avg);
1607 gsl_matrix_complex_memcpy(phase_ref_avg, phase_ref_s1);
1608 gsl_matrix_complex_add(phase_ref_avg, phase_ref_s2);
1609 gsl_matrix_complex_scale(phase_ref_avg, gsl_complex_rect(0.5, 0));
1611 FREE(gsl_matrix_complex_free, phase_ref_s1);
1612 FREE(gsl_matrix_complex_free, phase_ref_s2);
1614 return phase_ref_avg;
static cpl_error_code gravi_astrometry_mul_visibilities(astro_data *self, const gsl_vector *factor)
Scale VISDATA, VISREF, errors by vector factor, broadcasting over wavelength axis.
void gravi_astrometry_delete(astro_data *self)
cpl_error_code gravi_astrometry_create_phase_reference(astro_data *self, astro_data **phase_refs, cpl_size nphase, astro_data **swaps, cpl_size nswap, cpl_parameterlist *parlist)
Compute the final astrometric phase reference.
cpl_table * gravi_astrometry_get_phase_reference(astro_data *self)
double gravi_astrometry_get_mean_ftflux(astro_data *self)
static gsl_matrix * load_matrix(cpl_table *table, const char *name) CPL_ATTR_ALLOC
Load data from table into GSL matrix. Each table row is copied into the corresponding matrix row.
static gsl_vector * load_vector(cpl_table *table, const char *name) CPL_ATTR_ALLOC
Load data from table into GSL vector.
static gsl_matrix_complex * load_matrix_complex(cpl_table *table, const char *name) CPL_ATTR_ALLOC
Load data from table into GSL matrix. Each table row is copied into the corresponding matrix row.
static cpl_error_code gravi_astrometry_minimise_chi2_descent(gravi_astrometry_model_params *params, double ra_guess, double dec_guess, gsl_vector *Xsolve)
static gsl_matrix_complex * gravi_astrometry_calculate_visref_swap(astro_data **data, cpl_size ndata, double ra, double dec) CPL_ATTR_ALLOC
static int _gsl_vector_int_sum(const gsl_vector_int *a)
cpl_error_code gravi_astrometry_dump(astro_data *self, FILE *handle)
struct _gravi_astrometry_model_params_ gravi_astrometry_model_params
static astro_data * gravi_astrometry_average_over_dits(astro_data *self) CPL_ATTR_ALLOC
Average input astro_data over DITs and return new flattened astro_data.
cpl_error_code gravi_astrometry_filter_ftflux(astro_data *self, double threshold)
Filter based on FT flux threshold and normalise.
static cpl_error_code gravi_astrometry_minimise_chi2_grid(gravi_astrometry_model_params *params, const gsl_vector *ra_grid, const gsl_vector *dec_grid, gsl_vector *ra_dec_out, gsl_matrix *chi2_map)
static double gravi_astrometry_calculate_chi2(const gsl_vector *X, void *params)
Evaluate chi^2 statistic.
static cpl_error_code gravi_astrometry_recentre_phase(astro_data *self, double xvalue, double yvalue)
Recentre visRef and visRefError on the given position (in mas) by shifting the phases.
cpl_error_code gravi_astrometry_normalise_to_ft(astro_data *self)
Normalise visibilities to average FT flux.
static gsl_matrix_complex * average_matrix_complex_over_dits(gsl_matrix_complex *m, int ndit, int nchannel, int nwave, gsl_matrix_int *flag) CPL_ATTR_ALLOC
static gsl_matrix * average_matrix_over_dits(gsl_matrix *m, int ndit, int nchannel, int nwave, gsl_matrix_int *flag) CPL_ATTR_ALLOC
static cpl_size gravi_astrometry_find_closest_mjd(astro_data *self, astro_data **others, cpl_size n_other, find_closest_mode mode)
Given observation and list of reference observations, find closest in time from the list.
enum _find_closest_mode find_closest_mode
static gsl_matrix_complex * gravi_astrometry_create_swap_reference(astro_data **swap_data, cpl_size nswap) CPL_ATTR_ALLOC
Centre swap data on zero OPD and extract average phase.
astro_data * gravi_astrometry_load(gravi_data *data)
Load data for astrometry from a gravi_data.
static gsl_vector * average_vector_over_dits(gsl_vector *v, int ndit, int nchannel) CPL_ATTR_ALLOC
cpl_error_code gravi_astrometry_reduce_swaps(astro_data **swap_data, cpl_size nswap, cpl_parameterlist *parlist)
static cpl_error_code gravi_astrometry_add_phase(astro_data *self, const gsl_matrix *phase)
Add phase to visRef, updating errors accordingly.
static cpl_error_code gravi_astrometry_scale_visibilities(astro_data *self, double factor)
Scale VISDATA, VISREF, errors by scalar factor.
typedefCPL_BEGIN_DECLS struct _gravi_data_ gravi_data
#define gravi_data_get_header(data)
#define gravi_data_get_oi_wave(data, type, pol, npol)
#define gravi_data_get_oi_vis(data, type, pol, npol)
#define gravi_data_get_oi_wave_plist(data, type, pol, npol)
cpl_msg_debug(cpl_func, "Spectra has <50 pixels -> don't flat")
#define CPLCHECK_INT(msg)
#define CPLCHECK_CLEAN(msg)
#define FREE(function, variable)
#define CPLCHECK_NUL(msg)
#define CPLCHECK_MSG(msg)
#define FREELOOP(function, variable, n)
gsl_matrix_complex * phase_ref_astro
gsl_vector ** vis_ref_cov
gsl_matrix_complex * visdata
gsl_vector_complex ** vis_ref_pcov
gsl_matrix * amp_ref_astro
double swap_astrometry_fit[2]
double swap_astrometry_guess[2]
gsl_matrix_complex * vis_ref
gsl_matrix_complex * viserr
gsl_matrix_complex * visdata_ft
gsl_matrix * phase_met_telfc