82 double outlier_threshold);
89 double outlier_threshold);
91 cpl_table * oi_vis2_avg,
94 const char * phase_ref,
98 double outlier_threshold);
102cpl_error_code
gravi_vis_average_amp (cpl_table *oi_table,
const char *name,
const char *err,
int nbase);
103cpl_error_code
gravi_vis_average_phi (cpl_table *oi_table,
const char *name,
const char *err,
int nbase);
106 cpl_size nsamp, cpl_size nwave_new);
108 cpl_size nsamp, cpl_size nwave_new);
115 const char * err, cpl_size maxdeg);
118 cpl_table * in_table,
119 const char * name,
int ntel);
121cpl_error_code
gravi_vis_flag_median (cpl_table * oi_table,
const char * data,
const char *flag,
double value);
125 cpl_array * wavenumber,
126 const char * phase_ref,
int* cmin,
int* cmax,
int nrange);
143 double samp[] = {0.97446388, 0.78404357, 2.24226141, 1.85507201, 1.10792943,
144 1.34028771, -0.15399594, 0.07316682, 1.60898976, 0.33111245,
145 0.76767625, -2.1252529 , 0.3898138 , 2.1218198 , 0.51703696,
146 0.38451722, 1.07581416, -0.61435275, 1.91926679, 1.10020069,
147 1.82407999, 1.07367663, 0.46105875, 0.45497282, 1.65549611,
148 1.21647974, -0.32725523, -0.36477508, 0.43947414, 1.0242778 ,
149 2.05617949, 1.06163165, 1.24564147, 2.36249995, 0.20676319,
150 1.30886256, 0.7122533 , 2.28503709, 0.7134141 , -0.19104819,
151 2.9925884 , 0.95761567, 2.11770457, 0.34763896, 0.30040327,
152 2.3535165 , 1.65839907, 1.89819461, 1.67480833, 1.11174145};
155 return samp[rand()%nsamp];
163 cpl_ensure_code (data, CPL_ERROR_NULL_INPUT);
164 cpl_ensure_code (mean, CPL_ERROR_NULL_INPUT);
165 cpl_ensure_code (variance, CPL_ERROR_NULL_INPUT);
166 cpl_ensure_code (n>=0, CPL_ERROR_ILLEGAL_INPUT);
171 double n1 = 1. / ( (double)n + 1.0 );
172 double n2 = (double)n / ( (
double)n + 1.0 );
174 cpl_size size = cpl_array_get_size (data);
182 for (w = 0; w < size; w ++) {
184 rdata = cpl_array_get (data, w, &nv);
185 rmean = cpl_array_get (mean, w, &nv);
186 delta = ( rdata - rmean ) * n1;
188 rmean = rmean + delta;
189 cpl_array_set (mean, w, rmean);
191 cpl_array_set (variance, w,
192 cpl_array_get (variance, w, &nv) * n2 + delta * ( rdata - rmean ));
196 if ( (code=cpl_error_get_code()) ) {
197 return cpl_error_set_message(cpl_func, code,
"Cannot do online variance");
200 return CPL_ERROR_NONE;
225 cpl_ensure_code (data, CPL_ERROR_NULL_INPUT);
226 cpl_ensure_code (n>=0, CPL_ERROR_ILLEGAL_INPUT);
228 cpl_size size = cpl_array_get_size (data[0]);
234 cpl_array_add (data[2], data[0]);
239 for (
int w=0; w<size; w++ ) {
240 cpl_array_set (data[0], w,
241 carg( cexp (1*I* (cpl_array_get(data[0], w, NULL) -
242 cpl_array_get(data[2], w, NULL))) ) );
250 cpl_array_fill_window (data[0], 0, size, 0.0);
255 return CPL_ERROR_NONE;
277 double outlier_threshold)
280 cpl_ensure_code (oi_flux_avg, CPL_ERROR_ILLEGAL_OUTPUT);
281 cpl_ensure_code (oi_flux, CPL_ERROR_NULL_INPUT);
282 cpl_ensure_code (nboot>0, CPL_ERROR_ILLEGAL_INPUT);
285 int nv = 0,
ntel = 4;
286 cpl_size nrow = cpl_table_get_nrow (oi_flux) /
ntel;
287 cpl_size nwave = cpl_table_get_column_depth (oi_flux,
"FLUX");
290 cpl_array ** pFLUX = cpl_table_get_data_array (oi_flux,
"FLUX");
291 cpl_array ** pFLUXERR = cpl_table_get_data_array (oi_flux,
"FLUXERR");
292 cpl_array ** pFLAG = cpl_table_get_data_array (oi_flux,
"FLAG");
293 double * pINTTIME = cpl_table_get_data_double (oi_flux,
"INT_TIME");
294 double * pMJD = cpl_table_get_data_double (oi_flux,
"MJD");
298 for (cpl_size tel = 0; tel <
ntel; tel++) {
306 double total_exptime = 0.0, mjd_avg = 0.0;
313 int * flag = cpl_malloc(
sizeof(
int) * nrow );
314 for (
int row=0 ; row<nrow; row++ ) {
315 flag[row] = 0; nvalid++;
319 cpl_size nrow_per_seg = CPL_MAX(nvalid / CPL_MIN (nrow, 100), 1);
320 cpl_size nseg = nvalid / nrow_per_seg;
324 cpl_size nsamp = 5, nmontecarlo = CPL_MAX (nsamp - nseg, 0);
326 cpl_msg_info (
"Stat",
"%6lld valid frames over %6lld (%5.1f%%), make %4lld seg. of %5lld (miss %lld), add %lld MonteCarlo",
327 nvalid, nrow, (
double)nvalid/(
double)nrow*100.0,
328 nseg, nrow_per_seg, nvalid - nseg*nrow_per_seg, nmontecarlo);
341 for (
int seg = 0 ; seg < nseg + nmontecarlo; seg ++ ) {
342 cpl_msg_debug(cpl_func,
"pre integration of seg %d start with row %lld", seg, row);
346 while ( r < nrow_per_seg ) {
347 row = (row + 1) % nrow;
348 if ( flag[row] ) {
continue;}
else {r++;}
351 cpl_size rtel = row *
ntel + tel;
356 total_exptime += pINTTIME[rtel];
357 mjd_avg += pMJD[rtel] * pINTTIME[rtel];
361 double * tflux = cpl_array_get_data_double (flux[seg]);
364 for (
int w=0; w<nwave; w++ ) {
365 double FLUX = cpl_array_get (pFLUX[rtel], w, NULL);
366 double FLUXERR = cpl_array_get (pFLUXERR[rtel], w, NULL);
367 int outlier_flag = cpl_array_get (pFLAG[rtel], w, NULL);
375 if ( seg > nseg-1 ) {
394 for (
int boot = 0 ; boot < nboot ; boot ++ ) {
395 cpl_msg_debug(cpl_func,
"Bootstrap %d over %d", boot+1, nboot);
399 for (
int rowb = 0; rowb < nseg; rowb ++){
405 if (boot == 0 ) rows = rowb;
406 else rows = rand()%(nseg+nmontecarlo);
409 cpl_array_add (flux_res[0], flux[rows]);
417 CPLCHECK_MSG(
"while computing the variances over the bootstrap");
422 FREELOOP (cpl_array_delete, flux, nseg + nmontecarlo);
426 cpl_array_power (flux_res[3], 0.5);
430 mjd_avg /= total_exptime;
438 cpl_msg_debug (cpl_func,
"Not valid frames, force zero and infinit RMS");
439 cpl_array_fill_window (flux_res[3], 0, nwave, 1e10);
440 mjd_avg = cpl_table_get_column_mean (oi_flux,
"MJD");
445 cpl_array ** pflag = cpl_table_get_data_array (oi_flux_avg,
"FLAG");
449 for (
int wave = 0; wave < nwave; wave++) {
450 double value = cpl_array_get (array, wave, NULL) / nrow;
452 cpl_array_set (flux_res[2], wave, cpl_array_get (flux_res[2], wave, NULL) / (1.0 - value));
454 if (value > outlier_threshold) {
455 cpl_array_set (pflag[tel], wave, 1);
458 FREE (cpl_array_delete, array);
464 cpl_table_set_array (oi_flux_avg,
"FLUX", tel, flux_res[2]);
465 cpl_table_set_array (oi_flux_avg,
"FLUXERR", tel, flux_res[3]);
474 cpl_msg_debug(cpl_func,
"Total integration time = %.3f s", total_exptime);
475 cpl_table_set_double (oi_flux_avg,
"INT_TIME", tel, total_exptime);
476 cpl_table_set_double (oi_flux_avg,
"MJD", tel, mjd_avg);
477 cpl_table_set (oi_flux_avg,
"NVALID", tel, nvalid);
478 cpl_table_set (oi_flux_avg,
"NDIT", tel, nrow);
481 cpl_table_set_int (oi_flux_avg,
"TARGET_ID", tel, cpl_table_get_int (oi_flux,
"TARGET_ID", tel, &nv));
482 cpl_table_set_int (oi_flux_avg,
"STA_INDEX", tel, cpl_table_get_int (oi_flux,
"STA_INDEX", tel, &nv));
484 FREELOOP (cpl_array_delete, flux_res, 4);
490 return CPL_ERROR_NONE;
516 double outlier_threshold)
519 cpl_ensure_code (oi_t3_avg, CPL_ERROR_ILLEGAL_OUTPUT);
520 cpl_ensure_code (oi_vis, CPL_ERROR_NULL_INPUT);
521 cpl_ensure_code (oi_flux, CPL_ERROR_NULL_INPUT);
522 cpl_ensure_code (nboot>0, CPL_ERROR_ILLEGAL_INPUT);
530 int nv = 0,
ntel = 4, nclo = 4;
532 cpl_size nrow = cpl_table_get_nrow (oi_vis) /
GRAVI_NBASE;
533 cpl_size nwave = cpl_table_get_column_depth (oi_vis,
"VISDATA");
536 cpl_array ** pVISDATA = cpl_table_get_data_array (oi_vis,
"VISDATA");
537 cpl_array ** pVISERR = cpl_table_get_data_array (oi_vis,
"VISERR");
538 cpl_array ** pFLUX = cpl_table_get_data_array (oi_flux,
"FLUX");
539 cpl_array ** pFLAG = cpl_table_get_data_array (oi_vis,
"FLAG");
540 double * pINTTIME = cpl_table_get_data_double (oi_vis,
"INT_TIME");
541 double * pMJD = cpl_table_get_data_double (oi_vis,
"MJD");
542 double * pUCOORD = cpl_table_get_data_double (oi_vis,
"UCOORD");
543 double * pVCOORD = cpl_table_get_data_double (oi_vis,
"VCOORD");
544 cpl_array ** pVFACTOR = use_vFactor?cpl_table_get_data_array (oi_vis,
"V_FACTOR"):NULL;
546 double *pPFACTOR = NULL;
547 cpl_array **ppPFACTOR = NULL;
549 if (cpl_table_get_column_depth(oi_vis,
"P_FACTOR") > 0) {
550 ppPFACTOR = cpl_table_get_data_array(oi_vis,
"P_FACTOR");
552 pPFACTOR = cpl_table_get_data_double(oi_vis,
"P_FACTOR");
559 for (cpl_size closure = 0; closure < nclo; closure++) {
573 double total_exptime = 0.0, mjd_avg = 0.0;
574 double u1Coord = 0.0, v1Coord = 0.0, u2Coord = 0.0, v2Coord = 0.0;
581 int * flag = cpl_table_get_data_int (oi_vis,
"REJECTION_FLAG");
582 cpl_ensure_code (flag, CPL_ERROR_ILLEGAL_INPUT);
583 int * flagclo = cpl_malloc(
sizeof(
int) * nrow );
584 for (
int row=0 ; row<nrow; row++ ) {
586 if ( flagclo[row] == 0 ) nvalid++;
590 cpl_size nrow_per_seg = CPL_MAX (nvalid / CPL_MIN (nrow, 100), 1);
591 cpl_size nseg = nvalid / nrow_per_seg;
595 cpl_size nsamp = 5, nmontecarlo = CPL_MAX (nsamp - nseg, 0);
597 cpl_msg_info (
"Stat",
"%6lld valid frames over %6lld (%5.1f%%), make %4lld seg. of %5lld (miss %lld), add %lld MonteCarlo",
598 nvalid, nrow, (
double)nvalid/(
double)nrow*100.0,
599 nseg, nrow_per_seg, nvalid - nseg*nrow_per_seg, nmontecarlo);
613 for (
int seg = 0 ; seg < nseg + nmontecarlo ; seg ++ ) {
614 cpl_msg_debug(cpl_func,
"pre integration of seg %d start with row %lld", seg, row);
618 while ( r < nrow_per_seg ) {
619 row = (row + 1) % nrow;
620 if ( flagclo[row] ) {
continue;}
else {r++;}
630 total_exptime += pINTTIME[rbase0];
631 mjd_avg += pMJD[rbase0] * pINTTIME[rbase0];
632 u1Coord += pUCOORD[rbase0] * pINTTIME[rbase0];
633 v1Coord += pVCOORD[rbase0] * pINTTIME[rbase0];
634 u2Coord += pUCOORD[rbase1] * pINTTIME[rbase0];
635 v2Coord += pVCOORD[rbase1] * pINTTIME[rbase0];
639 double complex * tbisp = cpl_array_get_data_double_complex (bisp[seg]);
640 double *tF012 = cpl_array_get_data_double (F012[seg]);
643 double PFACTOR0 = 1.0, PFACTOR1 = 1.0, PFACTOR2 = 1.0;
644 if (use_pFactor && ppPFACTOR == NULL) {
645 PFACTOR0 = pPFACTOR[rbase0];
646 PFACTOR1 = pPFACTOR[rbase1];
647 PFACTOR2 = pPFACTOR[rbase2];
648 if (use_pFactor == 2) {
649 PFACTOR0 *= PFACTOR0;
650 PFACTOR1 *= PFACTOR1;
651 PFACTOR2 *= PFACTOR2;
657 for (
int w=0; w<nwave; w++ ) {
658 if (use_pFactor && ppPFACTOR) {
659 pPFACTOR = cpl_array_get_data_double(ppPFACTOR[rbase0]);
660 PFACTOR0 = pPFACTOR[w];
661 pPFACTOR = cpl_array_get_data_double(ppPFACTOR[rbase1]);
662 PFACTOR1 = pPFACTOR[w];
663 pPFACTOR = cpl_array_get_data_double(ppPFACTOR[rbase2]);
664 PFACTOR2 = pPFACTOR[w];
665 if (use_pFactor == 2) {
666 PFACTOR0 *= PFACTOR0;
667 PFACTOR1 *= PFACTOR1;
668 PFACTOR2 *= PFACTOR2;
673 double complex Vis0 = cpl_array_get_complex (pVISDATA[rbase0], w, NULL);
674 double complex Vis1 = cpl_array_get_complex (pVISDATA[rbase1], w, NULL);
675 double complex Vis2 = cpl_array_get_complex (pVISDATA[rbase2], w, NULL);
676 double complex VisErr0 = cpl_array_get_complex (pVISERR[rbase0], w, NULL);
677 double complex VisErr1 = cpl_array_get_complex (pVISERR[rbase1], w, NULL);
678 double complex VisErr2 = cpl_array_get_complex (pVISERR[rbase2], w, NULL);
679 double F0 = cpl_array_get (pFLUX[row *
ntel + ctel0], w, NULL);
680 double F1 = cpl_array_get (pFLUX[row *
ntel + ctel1], w, NULL);
681 double F2 = cpl_array_get (pFLUX[row *
ntel + ctel2], w, NULL);
682 double VFACTOR0 = (use_vFactor?cpl_array_get (pVFACTOR[rbase0], w, NULL):1.0);
683 double VFACTOR1 = (use_vFactor?cpl_array_get (pVFACTOR[rbase1], w, NULL):1.0);
684 double VFACTOR2 = (use_vFactor?cpl_array_get (pVFACTOR[rbase2], w, NULL):1.0);
685 int outlier_flag0 = cpl_array_get (pFLAG[rbase0], w, NULL);
686 int outlier_flag1 = cpl_array_get (pFLAG[rbase1], w, NULL);
687 int outlier_flag2 = cpl_array_get (pFLAG[rbase2], w, NULL);
690 if (outlier_flag0 || outlier_flag1 || outlier_flag2) {
704 if ( seg > nseg-1 ) {
714 tbisp[w] += Vis0 * Vis1 * conj (Vis2);
718 tF012[w] += F0 * F1 * F2 *
719 sqrt (CPL_MAX (VFACTOR0 * VFACTOR1 * VFACTOR2 *
720 PFACTOR0 * PFACTOR1 * PFACTOR2, 0.0));
734 for (
int boot = 0 ; boot < nboot ; boot ++ ) {
735 cpl_msg_debug(cpl_func,
"Bootstrap %d over %d", boot+1, nboot);
743 for (
int rowb = 0; rowb < nseg; rowb ++){
749 if (boot == 0 ) rows = rowb;
750 else rows = rand()%(nseg+nmontecarlo);
753 cpl_array_add (bisp_boot, bisp[rows]);
754 cpl_array_add (f012_boot, F012[rows]);
762 FREE (cpl_array_delete, t3Amp_res[0]);
763 t3Amp_res[0] = cpl_array_duplicate(bisp_boot);
764 cpl_array_abs (t3Amp_res[0]);
765 cpl_array_divide (t3Amp_res[0], f012_boot);
767 FREE (cpl_array_delete, t3Phi_res[0]);
768 t3Phi_res[0] = cpl_array_duplicate(bisp_boot);
769 cpl_array_arg (t3Phi_res[0]);
776 FREE (cpl_array_delete, bisp_boot);
777 FREE (cpl_array_delete, f012_boot);
778 CPLCHECK_MSG(
"while computing the variances over the bootstrap");
783 FREELOOP (cpl_array_delete, bisp, nseg + nmontecarlo);
784 FREELOOP (cpl_array_delete, F012, nseg + nmontecarlo);
788 cpl_array_power (t3Phi_res[3], 0.5);
789 cpl_array_power (t3Amp_res[3], 0.5);
793 mjd_avg /= total_exptime;
794 u1Coord /= total_exptime;
795 v1Coord /= total_exptime;
796 u2Coord /= total_exptime;
797 v2Coord /= total_exptime;
805 cpl_msg_debug (cpl_func,
"Not valid frames, force zero and infinit RMS");
806 cpl_array_fill_window (t3Amp_res[3], 0, nwave, 1e10);
807 cpl_array_fill_window (t3Phi_res[3], 0, nwave, 1e10);
808 mjd_avg = cpl_table_get_column_mean (oi_vis,
"MJD");
812 cpl_array ** pflag = cpl_table_get_data_array (oi_t3_avg,
"FLAG");
815 cpl_array * array = cpl_array_new (nwave, CPL_TYPE_INT);
816 cpl_array_fill_window (array, 0, nwave, 0.0);
817 int * sum_flag = cpl_array_get_data_int (array);
819 for (
int row=0; row < nrow; row++) {
820 cpl_array * arr0 = pFLAG[row *
GRAVI_NBASE + base0];
821 cpl_array * arr1 = pFLAG[row *
GRAVI_NBASE + base1];
822 cpl_array * arr2 = pFLAG[row *
GRAVI_NBASE + base2];
823 for (
int wave = 0; wave < nwave; wave++) {
824 if (cpl_array_get (arr0, wave, NULL) ||
825 cpl_array_get (arr1, wave, NULL) ||
826 cpl_array_get (arr2, wave, NULL) )
831 for (
int wave = 0; wave < nwave; wave++) {
832 double value = cpl_array_get (array, wave, NULL) / nrow;
833 if (value > outlier_threshold) {
834 cpl_array_set (pflag[closure], wave, 1);
837 FREE (cpl_array_delete, array);
844 cpl_table_set_array (oi_t3_avg,
"T3AMP", closure, t3Amp_res[2]);
845 cpl_table_set_array (oi_t3_avg,
"T3AMPERR", closure, t3Amp_res[3]);
861 cpl_msg_debug(cpl_func,
"Total integration time = %.3f s", total_exptime);
862 cpl_table_set_double (oi_t3_avg,
"INT_TIME", closure, total_exptime);
863 cpl_table_set_double (oi_t3_avg,
"MJD", closure, mjd_avg);
864 cpl_table_set_double (oi_t3_avg,
"U1COORD", closure, u1Coord);
865 cpl_table_set_double (oi_t3_avg,
"V1COORD", closure, v1Coord);
866 cpl_table_set_double (oi_t3_avg,
"U2COORD", closure, u2Coord);
867 cpl_table_set_double (oi_t3_avg,
"V2COORD", closure, v2Coord);
868 cpl_table_set (oi_t3_avg,
"NVALID", closure, nvalid);
869 cpl_table_set (oi_t3_avg,
"NDIT", closure, nrow);
872 cpl_table_set_int (oi_t3_avg,
"TARGET_ID", closure, cpl_table_get_int (oi_vis,
"TARGET_ID", base0, &nv));
875 cpl_array * sta_index = cpl_array_new (3, CPL_TYPE_INT);
876 cpl_array_set_int (sta_index, 0, cpl_table_get_int (oi_flux,
"STA_INDEX", ctel0, &nv));
877 cpl_array_set_int (sta_index, 1, cpl_table_get_int (oi_flux,
"STA_INDEX", ctel1, &nv));
878 cpl_array_set_int (sta_index, 2, cpl_table_get_int (oi_flux,
"STA_INDEX", ctel2, &nv));
879 cpl_table_set_array (oi_t3_avg,
"STA_INDEX", closure, sta_index);
880 FREE (cpl_array_delete, sta_index);
883 FREELOOP (cpl_array_delete, t3Phi_res, 4);
884 FREELOOP (cpl_array_delete, t3Amp_res, 4);
885 FREE (cpl_free, flagclo);
890 return CPL_ERROR_NONE;
916 cpl_table * oi_vis2_avg,
919 const char * phase_ref,
923 double outlier_threshold)
926 cpl_ensure_code (oi_vis_avg, CPL_ERROR_ILLEGAL_OUTPUT);
927 cpl_ensure_code (oi_vis2_avg, CPL_ERROR_ILLEGAL_OUTPUT);
928 cpl_ensure_code (oi_vis, CPL_ERROR_NULL_INPUT);
929 cpl_ensure_code (nboot>0, CPL_ERROR_ILLEGAL_INPUT);
933 cpl_size nrow = cpl_table_get_nrow (oi_vis) /
GRAVI_NBASE;
934 cpl_size nwave = cpl_table_get_column_depth (oi_vis,
"VISDATA");
937 cpl_array ** pVISDATA = cpl_table_get_data_array (oi_vis,
"VISDATA");
938 cpl_array ** pVISERR = cpl_table_get_data_array (oi_vis,
"VISERR");
939 cpl_array ** pFNORM = cpl_table_get_data_array (oi_vis,
"F1F2");
940 cpl_array ** pFLAG = cpl_table_get_data_array (oi_vis,
"FLAG");
941 double * pINTTIME = cpl_table_get_data_double (oi_vis,
"INT_TIME");
942 double * pMJD = cpl_table_get_data_double (oi_vis,
"MJD");
943 double * pUCOORD = cpl_table_get_data_double (oi_vis,
"UCOORD");
944 double * pVCOORD = cpl_table_get_data_double (oi_vis,
"VCOORD");
945 cpl_array ** pVFACTOR = use_vFactor?cpl_table_get_data_array (oi_vis,
"V_FACTOR"):NULL;
947 double *pPFACTOR = NULL;
948 cpl_array **ppPFACTOR = NULL;
950 if (cpl_table_get_column_depth(oi_vis,
"P_FACTOR") > 0) {
951 ppPFACTOR = cpl_table_get_data_array(oi_vis,
"P_FACTOR");
953 pPFACTOR = cpl_table_get_data_double(oi_vis,
"P_FACTOR");
959 cpl_array ** pPHASEREF = NULL;
960 if (phase_ref && strcmp (phase_ref,
"NONE"))
961 pPHASEREF = cpl_table_get_data_array (oi_vis, phase_ref);
962 CPLCHECK_MSG (
"Cannot get the reference phase data (column missing?)");
965 for (cpl_size base = 0; base <
GRAVI_NBASE; base ++) {
977 double total_exptime = 0.0, mjd_avg = 0.0;
978 double uCoord = 0.0, vCoord = 0.0;
985 int * flag = cpl_table_get_data_int (oi_vis,
"REJECTION_FLAG");
986 cpl_ensure_code (flag, CPL_ERROR_ILLEGAL_INPUT);
987 for (
int row=0 ; row<nrow; row++ )
if ( flag[row *
GRAVI_NBASE + base] == 0 ) nvalid++;
990 cpl_size nrow_per_seg = CPL_MAX (nvalid / CPL_MIN (nrow, 100), 1);
991 cpl_size nseg = nvalid / nrow_per_seg;
995 cpl_size nsamp = 5, nmontecarlo = CPL_MAX (nsamp - nseg, 0);
997 cpl_msg_info (
"Stat",
"%6lld valid frames over %6lld (%5.1f%%), make %4lld seg. of %5lld (miss %lld), add %lld MonteCarlo",
998 nvalid, nrow, (
double)nvalid/(
double)nrow*100.0,
999 nseg, nrow_per_seg, nvalid - nseg*nrow_per_seg, nmontecarlo);
1015 for (
int seg = 0 ; seg < nseg + nmontecarlo ; seg ++ ) {
1016 cpl_msg_debug(cpl_func,
"pre integration of seg %d start with row %lld", seg, row);
1020 while ( r < nrow_per_seg ) {
1021 row = (row + 1) % nrow;
1022 if ( flag[row *
GRAVI_NBASE + base] ) {
continue;}
else {r++;}
1030 total_exptime += pINTTIME[rbase];
1031 mjd_avg += pMJD[rbase] * pINTTIME[rbase];
1032 uCoord += pUCOORD[rbase] * pINTTIME[rbase];
1033 vCoord += pVCOORD[rbase] * pINTTIME[rbase];
1037 double *tR = cpl_array_get_data_double (visR[seg]);
1038 double *tI = cpl_array_get_data_double (visI[seg]);
1039 double *tP = cpl_array_get_data_double (POWER[seg]);
1040 double *tF1F2 = cpl_array_get_data_double (F1F2[seg]);
1041 double *tF12 = cpl_array_get_data_double (F12[seg]);
1044 double PFACTOR = 1.0;
1045 if (use_pFactor && ppPFACTOR == NULL) {
1046 PFACTOR = pPFACTOR[rbase];
1047 if (use_pFactor == 2) PFACTOR *= PFACTOR;
1052 for (
int w = 0; w < nwave; w++) {
1053 if (use_pFactor && ppPFACTOR) {
1054 pPFACTOR = cpl_array_get_data_double(ppPFACTOR[rbase]);
1055 PFACTOR = pPFACTOR[w];
1056 if (use_pFactor == 2) PFACTOR *= PFACTOR;
1059 double VFACTOR = use_vFactor?cpl_array_get (pVFACTOR[rbase], w, NULL):1.0;
1060 double PHASEREF = pPHASEREF?cpl_array_get (pPHASEREF[rbase], w, NULL):0.0;
1061 double FNORM = cpl_array_get (pFNORM[rbase], w, NULL);
1062 double complex Vis = cpl_array_get_complex (pVISDATA[rbase], w, NULL);
1063 double complex VErr = cpl_array_get_complex (pVISERR[rbase], w, NULL);
1064 double mR = creal (Vis);
1065 double mI = cimag (Vis);
1066 double eR = creal (VErr);
1067 double eI = cimag (VErr);
1068 int outlier_flag = cpl_array_get (pFLAG[rbase], w, NULL);
1080 if ( seg > nseg-1 ) {
1086 tR[w] += cos(PHASEREF) * mR - sin(PHASEREF) * mI;
1087 tI[w] += cos(PHASEREF) * mI + sin(PHASEREF) * mR;
1096 tF1F2[w] += FNORM * VFACTOR * PFACTOR;
1097 tF12[w] += sqrt( CPL_MAX (FNORM * VFACTOR * PFACTOR, 0.0) );
1100 if (use_debiasing) {
1101 tP[w] += mR*mR + mI*mI - eR*eR - eI*eI;
1103 tP[w] += mR*mR + mI*mI;
1118 for (
int boot = 0 ; boot < nboot ; boot ++ ) {
1119 cpl_msg_debug (cpl_func,
"Bootstrap %d over %d", boot+1, nboot);
1127 for (
int rowb = 0; rowb < nseg; rowb ++) {
1133 if (boot == 0 ) rows = rowb;
1134 else rows = rand()%(nseg+nmontecarlo);
1137 cpl_array_add (visR_res[0], visR[rows]);
1138 cpl_array_add (visI_res[0], visI[rows]);
1139 cpl_array_add (vis2_res[0], POWER[rows]);
1140 cpl_array_add (F12_boot, F12[rows]);
1141 cpl_array_add (F1F2_boot, F1F2[rows]);
1152 cpl_array_divide (vis2_res[0], F1F2_boot);
1153 CPLCHECK_MSG(
"while computing the energie integration");
1157 FREE (cpl_array_delete, visAmp_res[0]);
1159 cpl_array_power (visAmp_res[0], 0.5);
1160 cpl_array_divide (visAmp_res[0], F12_boot);
1161 CPLCHECK_MSG(
"while computing the norm of the coherent integration");
1165 FREE (cpl_array_delete, visPhi_res[0]);
1167 cpl_array_arg (visPhi_res[0]);
1176 CPLCHECK_MSG(
"while computing the variances over the bootstrap");
1179 FREE (cpl_array_delete, F12_boot);
1180 FREE (cpl_array_delete, F1F2_boot);
1186 FREELOOP (cpl_array_delete, visR, nseg + nmontecarlo);
1187 FREELOOP (cpl_array_delete, visI, nseg + nmontecarlo);
1188 FREELOOP (cpl_array_delete, POWER, nseg + nmontecarlo);
1189 FREELOOP (cpl_array_delete, F12, nseg + nmontecarlo);
1190 FREELOOP (cpl_array_delete, F1F2, nseg + nmontecarlo);
1194 cpl_array_power (vis2_res[3], 0.5);
1195 cpl_array_power (visAmp_res[3], 0.5);
1196 cpl_array_power (visPhi_res[3], 0.5);
1197 cpl_array_power (visR_res[3], 0.5);
1198 cpl_array_power (visI_res[3], 0.5);
1202 mjd_avg /= total_exptime;
1203 uCoord /= total_exptime;
1204 vCoord /= total_exptime;
1211 cpl_msg_debug (cpl_func,
"Not valid frames, force zero and infinit RMS");
1212 cpl_array_fill_window (vis2_res[3], 0, nwave, 1e10);
1213 cpl_array_fill_window (visR_res[3], 0, nwave, 1e10);
1214 cpl_array_fill_window (visI_res[3], 0, nwave, 1e10);
1215 cpl_array_fill_window (visAmp_res[3], 0, nwave, 1e10);
1216 cpl_array_fill_window (visPhi_res[3], 0, nwave, 1e10);
1217 mjd_avg = cpl_table_get_column_mean (oi_vis,
"MJD");
1222 cpl_array ** pflag_vis = cpl_table_get_data_array (oi_vis_avg,
"FLAG");
1223 cpl_array ** pflag_vis2 = cpl_table_get_data_array (oi_vis2_avg,
"FLAG");
1227 for (
int wave = 0; wave < nwave; wave++) {
1228 double value = cpl_array_get (array, wave, NULL) / nrow;
1230 cpl_array_set (visR_res[2], wave, cpl_array_get (visR_res[2], wave, NULL) / (1.0 - value));
1231 cpl_array_set (visI_res[2], wave, cpl_array_get (visI_res[2], wave, NULL) / (1.0 - value));
1233 if (value > outlier_threshold) {
1234 cpl_array_set (pflag_vis[base], wave, 1);
1235 cpl_array_set (pflag_vis2[base], wave, 1);
1238 FREE (cpl_array_delete, array);
1244 cpl_table_set_array (oi_vis2_avg,
"VIS2DATA", base, vis2_res[2]);
1245 cpl_table_set_array (oi_vis2_avg,
"VIS2ERR", base, vis2_res[3]);
1252 cpl_table_set_array (oi_vis_avg,
"RVIS", base, visR_res[2]);
1253 cpl_table_set_array (oi_vis_avg,
"RVISERR", base, visR_res[3]);
1256 cpl_table_set_array (oi_vis_avg,
"IVIS", base, visI_res[2]);
1257 cpl_table_set_array (oi_vis_avg,
"IVISERR", base, visI_res[3]);
1260 cpl_table_set_array (oi_vis_avg,
"VISAMP", base, visAmp_res[2]);
1261 cpl_table_set_array (oi_vis_avg,
"VISAMPERR", base, visAmp_res[3]);
1278 cpl_table_set (oi_vis_avg,
"INT_TIME", base, total_exptime);
1279 cpl_table_set (oi_vis2_avg,
"INT_TIME", base, total_exptime);
1280 cpl_table_set (oi_vis_avg,
"MJD", base, mjd_avg);
1281 cpl_table_set (oi_vis2_avg,
"MJD", base, mjd_avg);
1282 cpl_table_set (oi_vis_avg,
"UCOORD", base, uCoord);
1283 cpl_table_set (oi_vis_avg,
"VCOORD", base, vCoord);
1284 cpl_table_set (oi_vis2_avg,
"UCOORD", base, uCoord);
1285 cpl_table_set (oi_vis2_avg,
"VCOORD", base, vCoord);
1289 cpl_table_set (oi_vis2_avg,
"NVALID", base, nvalid);
1290 cpl_table_set (oi_vis2_avg,
"NDIT", base, nrow);
1291 cpl_table_set (oi_vis_avg,
"NVALID", base, nvalid);
1292 cpl_table_set (oi_vis_avg,
"NDIT", base, nrow);
1296 cpl_table_set_int (oi_vis_avg,
"TARGET_ID", base, cpl_table_get_int (oi_vis,
"TARGET_ID", base, &nv));
1297 cpl_table_set_int (oi_vis2_avg,
"TARGET_ID", base, cpl_table_get_int (oi_vis,
"TARGET_ID", base, &nv));
1298 cpl_table_set_array (oi_vis_avg,
"STA_INDEX", base, cpl_table_get_array (oi_vis,
"STA_INDEX", base));
1299 cpl_table_set_array (oi_vis2_avg,
"STA_INDEX", base, cpl_table_get_array (oi_vis,
"STA_INDEX", base));
1302 FREELOOP (cpl_array_delete, vis2_res, 4);
1303 FREELOOP (cpl_array_delete, visAmp_res, 4);
1304 FREELOOP (cpl_array_delete, visPhi_res, 4);
1305 FREELOOP (cpl_array_delete, visR_res, 4);
1306 FREELOOP (cpl_array_delete, visI_res, 4);
1312 return CPL_ERROR_NONE;
1341 const double Asymptot = CPL_MATH_PI / sqrt(3.0);
1342 double c[8] = {2.7191808010909,
1351 double x2, x3, x4, x5, x6, x7, z;
1356 return (0.691 / (Asymptot - x));
1367 z = c[0] * x7 + c[1] * x6 + c[2] * x5 + c[3] * x4 + c[4] * x3 + c[5] * x2 + c[6] * x + c[7];
1386 cpl_array * wavenumber,
1387 const char * phase_ref,
int* cmin,
int* cmax,
int nrange)
1390 cpl_ensure_code(oi_vis_avg, CPL_ERROR_ILLEGAL_OUTPUT);
1391 cpl_ensure_code(oi_vis, CPL_ERROR_NULL_INPUT);
1394 cpl_size nrow = cpl_table_get_nrow(oi_vis) /
GRAVI_NBASE;
1395 cpl_size nwave = cpl_table_get_column_depth(oi_vis,
"VISDATA");
1397 int use_crange = (nrange > 0);
1399 for (
int i = 0; i < nrange; ++i) {
1400 if (cmax[i] - cmin[i] < 1) {
1404 if (cmax[i] > nwave - 1) {
1414 for (
int i = 0; i < nrange; ++i)
cpl_msg_info(
"Reference Channel",
"Part %02d [%3d:%3d]", i + 1, cmin[i], cmax[i]);
1416 cpl_msg_info(
"Warning (SELF_VISPHI)",
"Invalid Ranges Found, continuing with default Method.");
1421 cpl_array ** pVISDATA = cpl_table_get_data_array(oi_vis,
"VISDATA");
1422 cpl_array ** pVISERR = cpl_table_get_data_array(oi_vis,
"VISERR");
1426 cpl_array ** pPHASEREF = NULL;
1427 if (phase_ref && strcmp(phase_ref,
"NONE"))
1428 pPHASEREF = cpl_table_get_data_array(oi_vis, phase_ref);
1429 CPLCHECK_MSG(
"Cannot get the reference phase data (column missing?)");
1432 for (cpl_size base = 0; base <
GRAVI_NBASE; base++) {
1435 cpl_size nvalid = 0;
1442 int * flag = cpl_table_get_data_int(oi_vis,
"REJECTION_FLAG");
1443 cpl_ensure_code(flag, CPL_ERROR_ILLEGAL_INPUT);
1444 for (
int row = 0; row < nrow; row++)
if (flag[row *
GRAVI_NBASE + base] == 0) nvalid++;
1446 cpl_msg_info(
"Stat (SELF_VISPHI)",
"%6lld valid frames over %6lld (%5.1f%%)",
1447 nvalid, nrow, (
double) nvalid / (
double) nrow * 100.0);
1453 cpl_msg_debug(cpl_func,
"No valid frames, force zero and infinite RMS");
1454 cpl_array_fill_window(visPhi_res[1], 0, nwave, 1e10);
1459 FREELOOP(cpl_array_delete, visPhi_res, 2);
1470 for (cpl_size currentRow = 0, validRowIndex = -1; currentRow < nrow; currentRow++) {
1479 double complex *ptrC = cpl_array_get_data_double_complex(Vis[validRowIndex]);
1480 double complex *ptrEC = cpl_array_get_data_double_complex(EVis[validRowIndex]);
1484 for (
int w = 0; w < nwave; w++) {
1485 double PHASEREF = pPHASEREF ? cpl_array_get(pPHASEREF[rbase], w, NULL) : 0.0;
1486 double complex vis = cpl_array_get_double_complex(pVISDATA[rbase], w, NULL);
1487 double complex viserr = cpl_array_get_double_complex(pVISERR[rbase], w, NULL);
1491 ptrC[w] = (cos(PHASEREF) * creal(vis) - sin(PHASEREF) * cimag(vis)) +
1492 I * (cos(PHASEREF) * cimag(vis) + sin(PHASEREF) * creal(vis));
1502 for (
int irow = 0; irow < nvalid; irow++) {
1512 double mean_delay = 0.0;
1517 double mean_phase = carg(cpl_array_get_mean_complex(Vis[irow]));
1518 cpl_array_multiply_scalar_complex(Vis[irow], cexp(-I * mean_phase));
1521 cpl_array *CRef = cpl_array_new(nwave, CPL_TYPE_DOUBLE_COMPLEX);
1522 cpl_array *ECRef = cpl_array_new(nwave, CPL_TYPE_DOUBLE_COMPLEX);
1523 double complex *pCRef = cpl_array_get_data_double_complex(CRef);
1524 double complex *pECRef = cpl_array_get_data_double_complex(ECRef);
1528 for (
int irow = 0; irow < nvalid; irow++) {
1530 double complex totalVis = 0.0 + I * 0.0;
1531 double complex totalEVis = 0.0 + I * 0.0;
1533 double complex *pVis = cpl_array_get_data_double_complex(Vis[irow]);
1534 double complex *pEVis = cpl_array_get_data_double_complex(EVis[irow]);
1536 double complex *pW1 = cpl_array_get_data_double_complex(W1[irow]);
1537 double complex *pEW1 = cpl_array_get_data_double_complex(EW1[irow]);
1543 for (
int i = 0; i < nrange; ++i) {
1544 for (
int w = cmin[i]; w < cmax[i]; ++w) {
1545 totalVis += pVis[w];
1546 totalEVis += pEVis[w];
1550 for (
int w = 0; w < nwave; w++) {
1551 cpl_array_set_double_complex(CRef, w, totalVis / nchans);
1552 cpl_array_set_double_complex(ECRef, w, totalEVis / nchans);
1556 for (
int w = 0; w < nwave; w++) {
1557 totalVis += pVis[w];
1558 totalEVis += pEVis[w];
1566 int divider = nwave - (2 * radius) - 1;
1567 for (; iw < radius; iw++) {
1568 cpl_array_set_double_complex(CRef, iw, totalVis / nwave);
1569 cpl_array_set_double_complex(ECRef, iw, totalEVis / nwave);
1571 for (; iw < nwave - radius; iw++) {
1572 double complex tmp = 0.0 + I * 0.0;
1573 double complex Etmp = 0.0 + I * 0.0;
1574 for (
int j = iw; j < iw + 2 * radius + 1; ++j) tmp += pVis[iw];
1575 cpl_array_set_double_complex(CRef, iw, (totalVis - tmp) / divider);
1576 for (
int j = iw; j < iw + 2 * radius + 1; ++j) Etmp += pEVis[iw];
1577 cpl_array_set_double_complex(ECRef, iw, (totalEVis - Etmp) / divider);
1579 for (; iw < radius; iw++) {
1580 cpl_array_set_double_complex(CRef, iw, totalVis / nwave);
1581 cpl_array_set_double_complex(ECRef, iw, totalEVis / nwave);
1585 for (
int w = 0; w < nwave; w++) {
1586 pW1[w] = pVis[w] * conj(pCRef[w]);
1591 pEW1[w] = (creal(pEVis[w]) *
gravi_pow2(creal(pCRef[w])) +
1592 creal(pECRef[w]) *
gravi_pow2(creal(pVis[w])) +
1593 cimag(pVis[w]) *
gravi_pow2(cimag(pCRef[w])) +
1594 cimag(pECRef[w]) +
gravi_pow2(cimag(pVis[w]))) +
1597 cimag(pVis[w]) *
gravi_pow2(creal(pCRef[w])) +
1598 cimag(pECRef[w]) *
gravi_pow2(creal(pVis[w])) +
1599 creal(pVis[w]) *
gravi_pow2(cimag(pCRef[w])) +
1600 creal(pECRef[w]) +
gravi_pow2(cimag(pVis[w]))
1605 FREE(cpl_array_delete, CRef);
1606 FREE(cpl_array_delete, ECRef);
1607 FREELOOP(cpl_array_delete, Vis, nvalid);
1608 FREELOOP(cpl_array_delete, EVis, nvalid);
1610 double *pPhi = cpl_array_get_data_double(visPhi_res[0]);
1611 double *pPhiErr = cpl_array_get_data_double(visPhi_res[1]);
1614 for (
int w = 0; w < nwave; w++) {
1615 cpl_array *cpxVisVect = cpl_array_new(nvalid, CPL_TYPE_DOUBLE_COMPLEX);
1618 for (
int irow = 0; irow < nvalid; irow++) {
1619 const double complex *pW1 = cpl_array_get_data_double_complex_const(W1[irow]);
1620 cpl_array_set_double_complex(cpxVisVect, irow, pW1[w]);
1624 double complex w1Avg;
1625 w1Avg = cpl_array_get_mean_complex(cpxVisVect);
1628 pPhi[w] = atan2(cimag(w1Avg), creal(w1Avg));
1630 w1Avg = conj(w1Avg);
1631 cpl_array *Vect = cpl_array_new(nvalid, CPL_TYPE_DOUBLE);
1632 for (
int irow = 0; irow < nvalid; irow++) {
1633 const double complex *tW1 = cpl_array_get_data_double_complex_const(W1[irow]);
1635 cpl_array_set_double(Vect, irow, atan2(cimag(tW1[w] * w1Avg), creal(tW1[w] * w1Avg)));
1637 double x = cpl_array_get_stdev(Vect);
1641 FREE(cpl_array_delete, cpxVisVect);
1642 FREE(cpl_array_delete, Vect);
1650 FREELOOP(cpl_array_delete, visPhi_res, 2);
1652 FREELOOP(cpl_array_delete, EW1 , nvalid);
1653 FREELOOP(cpl_array_delete, W1 , nvalid);
1661 return CPL_ERROR_NONE;
1676 const cpl_parameterlist * parlist,
1677 cpl_size * current_frame)
1680 cpl_ensure (p2vmred_data, CPL_ERROR_NULL_INPUT, NULL);
1681 cpl_ensure (parlist, CPL_ERROR_NULL_INPUT, NULL);
1691 cpl_msg_info (cpl_func,
"Average %lli frames starting from %lli",
1692 max_frame, *current_frame);
1703 cpl_size nrow = cpl_table_get_nrow (vis_SC) /
GRAVI_NBASE;
1706 cpl_size sframe = *current_frame;
1707 cpl_size eframe = CPL_MIN (*current_frame + max_frame - 1, nrow-1);
1710 if (eframe >= nrow-1) *current_frame = -1;
1711 else *current_frame += max_frame;
1714 double start_time, end_time;
1715 start_time = cpl_table_get (vis_SC,
"TIME", sframe*
GRAVI_NBASE, &nv) - dit_sc/2;
1716 end_time = cpl_table_get (vis_SC,
"TIME", eframe*
GRAVI_NBASE, &nv) + dit_sc/2;
1719 cpl_msg_info (cpl_func,
"Integrate frames: first = %lli last = %lli", sframe, eframe);
1720 cpl_msg_info (cpl_func,
"start = %f end = %f [s]", start_time*1e-6, end_time*1e-6);
1727 cpl_msg_info(cpl_func,
"Construction of the averaged output data");
1731 cpl_propertylist_append (vis_header, p2vmred_header);
1746 cpl_msg_info (cpl_func,
"P2VMRED data has no FT extensions");
1750 int v_factor_flag_ft = 0;
1751 int p_factor_flag_ft = 0;
1753 if (!strcmp(
"PFACTOR", p_factor_str_ft))
1754 p_factor_flag_ft = 1;
1755 else if (!strcmp(
"PFACTOR_SQUARED", p_factor_str_ft))
1756 p_factor_flag_ft = 2;
1759 const char * phase_ref_ft =
"SELF_REF";
1760 double outlier_threshold_ft = 0.0;
1761 cpl_msg_info (cpl_func,
"vFactor correction for FT is %s",v_factor_flag_ft?
"ENABLE":
"DISABLE");
1762 cpl_msg_info (cpl_func,
"pFactor correction for FT is %s",(p_factor_flag_ft>0)?p_factor_str_ft:
"DISABLE");
1766 cpl_msg_info (cpl_func,
"Bias subtraction of V2 for FT is %s",debiasing_flag_ft?
"ENABLE":
"DISABLE");
1767 cpl_msg_info (cpl_func,
"Reference phase for FT is %s",phase_ref_ft);
1774 for (
int pol = 0; pol < npol_ft; pol++) {
1775 cpl_msg_info (cpl_func,
"Start FT polarisation %d over %d",pol+1, npol_ft);
1780 int nwave_ft = cpl_table_get_column_depth (vis_FT,
"VISDATA");
1808 cpl_msg_info (cpl_func,
"Compute OIVIS2 and OIVIS for FT");
1816 outlier_threshold_ft);
1828 outlier_threshold_ft);
1838 outlier_threshold_ft);
1845 cpl_propertylist * oivis_plist = cpl_propertylist_new();
1846 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"DATE-OBS");
1847 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"EXTVER");
1848 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"ARRNAME");
1849 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"INSNAME");
1850 cpl_propertylist_update_string (oivis_plist,
"AMPTYP",
"absolute");
1851 cpl_propertylist_update_string (oivis_plist,
"PHITYP",
"differential");
1852 cpl_propertylist_update_int (oivis_plist,
"PHIORDER",1);
1855 cpl_propertylist * oivis2_plist = cpl_propertylist_new();
1856 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"DATE-OBS");
1857 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"EXTVER");
1858 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"ARRNAME");
1859 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"INSNAME");
1862 cpl_propertylist * oit3_plist = cpl_propertylist_new();
1863 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"DATE-OBS");
1864 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"EXTVER");
1865 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"ARRNAME");
1866 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"INSNAME");
1869 cpl_propertylist * oiflux_plist = cpl_propertylist_new();
1870 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"DATE-OBS");
1871 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"EXTVER");
1872 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"ARRNAME");
1873 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"INSNAME");
1874 cpl_propertylist_update_string (oiflux_plist,
"CALSTAT",
"U");
1875 cpl_propertylist_set_comment (oiflux_plist,
"CALSTAT",
"Uncalibrated flux per telescope");
1880 FREE (cpl_table_delete, vis_FT);
1881 FREE (cpl_table_delete, flux_FT);
1891 cpl_msg_info (cpl_func,
"P2VMRED data has no SC extensions");
1899 double SOBJ_R = sqrt(SOBJ_X*SOBJ_X + SOBJ_Y*SOBJ_Y);
1904 if ( !strcmp (phase_ref_sc,
"AUTO") && SOBJ_R > 0.001)
1906 phase_ref_sc =
"IMAGING_REF";
1907 if (cpl_table_has_column (vis_SC, phase_ref_sc) == 0) {
1908 phase_ref_sc =
"PHASE_REF";
1909 cpl_msg_warning (cpl_func,
"No table 'IMAGING_REF', changing mode to phase_ref_sc='PHASE_REF'");
1913 else if ( !strcmp (phase_ref_sc,
"AUTO") && SOBJ_R < 0.001)
1914 phase_ref_sc =
"PHASE_REF";
1916 cpl_msg_info (cpl_func,
"Reference phase for SC is %s",phase_ref_sc);
1921 if ( !strcmp (output_phase_sc,
"AUTO") && SOBJ_R > 0.001)
1922 output_phase_sc =
"ABSOLUTE";
1923 else if ( !strcmp (output_phase_sc,
"AUTO") && SOBJ_R < 0.001)
1924 output_phase_sc =
"DIFFERENTIAL";
1929 if (!strcmp(output_phase_sc,
"SELF_VISPHI")) {
1930 if (strcmp(phase_ref_sc,
"NONE")) {
1931 phase_ref_sc =
"SELF_REF";
1932 cpl_msg_info(cpl_func,
"Reference phase for SC forced to %s due to option SELF_VISPHI", phase_ref_sc);
1933 }
else cpl_msg_info(cpl_func,
"Reference phase for SC is %s", phase_ref_sc);
1934 }
else cpl_msg_info(cpl_func,
"Reference phase for SC is %s", phase_ref_sc);
1936 cpl_msg_info (cpl_func,
"Output phase for SC is %s",output_phase_sc);
1939 int v_factor_flag_sc = strstr (
gravi_param_get_string (parlist,
"gravity.vis.vis-correction-sc"),
"VFACTOR") ? 1 : 0;
1940 int p_factor_flag_sc = strstr (
gravi_param_get_string (parlist,
"gravity.vis.vis-correction-sc"),
"PFACTOR") ? 1 : 0;
1944 double outlier_threshold_sc =
gravi_param_get_double (parlist,
"gravity.vis.outlier-fraction-threshold");
1945 cpl_msg_info (cpl_func,
"Bias subtraction of V2 for SC is %s",debiasing_flag_sc?
"ENABLE":
"DISABLE");
1946 cpl_msg_info (cpl_func,
"Threshold for fraction of outlier is %.3f",outlier_threshold_sc);
1950 if (!strcmp(output_phase_sc,
"SELF_VISPHI")) {
1951 v_factor_flag_sc= 0;
1952 p_factor_flag_sc= 0;
1953 cpl_msg_info(cpl_func,
"vFactor correction for SC is %s due to option SELF_VISPHI", v_factor_flag_sc ?
"ENABLE" :
"DISABLE");
1954 cpl_msg_info(cpl_func,
"pFactor correction for SC is %s due to option SELF_VISPHI", p_factor_flag_sc ?
"ENABLE" :
"DISABLE");
1956 cpl_msg_info (cpl_func,
"vFactor correction for SC is %s",v_factor_flag_sc?
"ENABLE":
"DISABLE");
1957 cpl_msg_info (cpl_func,
"pFactor correction for SC is %s",p_factor_flag_sc?
"ENABLE":
"DISABLE");
1965 for (
int pol = 0; pol < npol_sc; pol++) {
1966 cpl_msg_info (cpl_func,
"Start SC polarisation %d over %d",pol+1, npol_sc);
1975 cpl_array * wavenumber_sc;
1976 int nwave_sc = cpl_table_get_column_depth (vis_SC,
"VISDATA");
1977 wavenumber_sc = cpl_array_new (nwave_sc, CPL_TYPE_DOUBLE);
1978 for (cpl_size wave = 0; wave < nwave_sc; wave ++){
1979 cpl_array_set (wavenumber_sc, wave, 1./cpl_table_get (oi_wavelengthsc,
"EFF_WAVE", wave, &nv));
2004 CPLCHECK_NUL(
"Cannot create columns in averaged OIFITS...");
2013 cpl_msg_info (cpl_func,
"Compute OIVIS2 and OIVIS for SC");
2021 outlier_threshold_sc);
2037 if ( !strcmp (output_phase_sc,
"DIFFERENTIAL") || !strcmp (output_phase_sc,
"ABSOLUTE") ) {
2041 cpl_array * visData_sc, * visErr_sc;
2042 visData_sc = cpl_array_cast (cpl_table_get_array (oi_vis_SC,
"VISDATA", base),
2043 CPL_TYPE_DOUBLE_COMPLEX);
2046 visErr_sc = cpl_array_duplicate (visData_sc);
2047 cpl_array_abs (visErr_sc);
2048 cpl_array_divide (visData_sc, visErr_sc);
2051 cpl_array * flag_sc;
2052 flag_sc = (cpl_array *)cpl_table_get_array (oi_vis_SC,
"FLAG", base);
2055 double mean_delay = 0.0;
2060 cpl_table_set (oi_vis_SC,
"GDELAY", base, mean_delay);
2063 double mean_phase = carg (cpl_array_get_mean_complex (visData_sc));
2064 cpl_array_multiply_scalar_complex (visData_sc, cexp(- I * mean_phase));
2067 cpl_table_set (oi_vis_SC,
"PHASE", base, mean_phase);
2070 if ( !strcmp (output_phase_sc,
"DIFFERENTIAL") ) {
2071 cpl_array_arg (visData_sc);
2074 cpl_array_delete (visData_sc);
2075 cpl_array_delete (visErr_sc);
2081 if (!strcmp(output_phase_sc,
"SELF_VISPHI")) {
2086 if (strcmp(rangeString,
"UNKNOWN")) {
2089 int l=strlen(rangeString)+1;
2092 str=(
char*)malloc(l);
2093 strncpy(str,rangeString,l);
2094 for (i = 0; i<l; ++i)
if (str[i]!=
' ' && str[i]!=
'\t') str[j++]=str[i];
2097 str1=(
char*)malloc(l);
2098 strncpy(str1,str,l);
2101 token = strtok(str,
"[,]");
2104 token = strtok(NULL,
"[,]");
2107 cmin=(
int*) calloc(nrange,
sizeof(
int));
2108 cmax=(
int*) calloc(nrange,
sizeof(
int));
2110 char *str2, *subtoken;
2111 char *saveptr1, *saveptr2;
2112 for (j = 0; ; j++, str1 = NULL) {
2113 token = strtok_r(str1,
"[,]" , &saveptr1);
2116 for (str2 = token, i=0; i<2 ; str2 = NULL, ++i) {
2117 subtoken = strtok_r(str2,
":", &saveptr2);
2118 if (subtoken == NULL)
2137 outlier_threshold_sc);
2146 nboot_sc, outlier_threshold_sc);
2157 for (
int tel = 0; tel <
ntel; tel++){
2159 cpl_table_set (oi_flux_SC,
"LKDT_MET_FC", tel, lockdate);
2166 cpl_propertylist * oivis_plist = cpl_propertylist_new();
2167 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"DATE-OBS");
2168 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"EXTVER");
2169 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"ARRNAME");
2170 cpl_propertylist_copy_property (oivis_plist, vis_plist,
"INSNAME");
2171 cpl_propertylist_update_string (oivis_plist,
"AMPTYP",
"absolute");
2172 if ( !strcmp (output_phase_sc,
"DIFFERENTIAL") )
2173 cpl_propertylist_update_string (oivis_plist,
"PHITYP",
"differential");
2174 if ( !strcmp (output_phase_sc,
"ABSOLUTE") )
2175 cpl_propertylist_update_string (oivis_plist,
"PHITYP",
"absolute");
2176 cpl_propertylist_update_int (oivis_plist,
"PHIORDER",1);
2179 cpl_propertylist * oivis2_plist = cpl_propertylist_new();
2180 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"DATE-OBS");
2181 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"EXTVER");
2182 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"ARRNAME");
2183 cpl_propertylist_copy_property (oivis2_plist, vis_plist,
"INSNAME");
2186 cpl_propertylist * oit3_plist = cpl_propertylist_new();
2187 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"DATE-OBS");
2188 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"EXTVER");
2189 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"ARRNAME");
2190 cpl_propertylist_copy_property (oit3_plist, vis_plist,
"INSNAME");
2193 cpl_propertylist * oiflux_plist = cpl_propertylist_new();
2194 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"DATE-OBS");
2195 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"EXTVER");
2196 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"ARRNAME");
2197 cpl_propertylist_copy_property (oiflux_plist, vis_plist,
"INSNAME");
2198 cpl_propertylist_update_string (oiflux_plist,
"CALSTAT",
"U");
2199 cpl_propertylist_set_comment (oiflux_plist,
"CALSTAT",
"Uncalibrated flux per telescope");
2205 FREE (cpl_array_delete, wavenumber_sc);
2208 FREE (cpl_table_delete, vis_SC);
2209 FREE (cpl_table_delete, flux_SC);
2218 cpl_propertylist_update_int (vis_header,
GRAVI_NIGHT_OBS, (
int)floor(mjd_obs-0.625));
2238 cpl_propertylist **frame_qcs, cpl_size nb_frame,
2239 char * input_data_type)
2242 cpl_ensure_code (vis_data, CPL_ERROR_NULL_INPUT);
2243 cpl_ensure_code (frame_qcs, CPL_ERROR_NULL_INPUT);
2245 int nv,
ntel=4, nclo=4;
2260 cpl_msg_info (cpl_func,
"VIS data has no FT extensions");
2266 for (
int pol = 0; pol < npol_ft; pol++) {
2267 cpl_msg_info (cpl_func,
"Start FT polarisation %d over %d",pol+1, npol_ft);
2271 cpl_msg_info (cpl_func,
"Compute QC OIVIS2 and OIVIS for FT");
2280 sprintf (qc_name,
"ESO QC VIS2_FT%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2282 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2284 sprintf (qc_name,
"ESO QC VIS2ERR_FT%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2286 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2288 sprintf (qc_name,
"ESO QC VISPHI_FT%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2290 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2292 sprintf (qc_name,
"ESO QC VISPHIERR_FT%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2294 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2296 sprintf (qc_name,
"ESO QC VISAMP_FT%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2298 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2300 sprintf (qc_name,
"ESO QC VISAMPERR_FT%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2302 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2304 CPLCHECK_MSG(
"Cannot compute QC parameter for OI_VIS for FT");
2314 for (
int clo = 0; clo < nclo; clo++){
2316 sprintf (qc_name,
"ESO QC T3PHI_FT%s_P%d AVG",
GRAVI_CLO_NAME[clo], pol+1);
2318 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2320 sprintf (qc_name,
"ESO QC T3PHIERR_FT%s_P%d AVG",
GRAVI_CLO_NAME[clo], pol+1);
2322 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2324 CPLCHECK_MSG(
"Cannot compute QC parameter for OI_T3 for FT");
2334 for (
int tel = 0; tel <
ntel; tel++){
2336 sprintf (qc_name,
"ESO QC FLUX_FT%d_P%d AVG", tel+1, pol+1);
2338 cpl_propertylist_set_comment (
plist, qc_name,
"[e/total_int_time] mean over lbd");
2340 sprintf (qc_name,
"ESO QC FLUXERR_FT%d_P%d AVG", tel+1, pol+1);
2342 cpl_propertylist_set_comment (
plist, qc_name,
"[e/total_int_time] mean over lbd");
2344 sprintf (qc_name,
"ESO QC FLUXRATE_FT%d_P%d SUM", tel+1, pol+1);
2345 double flux_rate = cpl_array_get_mean (cpl_table_get_array (oi_flux_FT,
"FLUX", tel)) *
2346 cpl_array_get_size (cpl_table_get_array (oi_flux_FT,
"FLUX", tel)) / cpl_table_get_double (oi_flux_FT,
"INT_TIME", tel, &nv);
2348 cpl_propertylist_set_comment (
plist, qc_name,
"[e/s] sum over lbd");
2350 CPLCHECK_MSG(
"Cannot compute QC parameter for OI_FLUX for FT");
2362 cpl_msg_info (cpl_func,
"VIS data has no SC extensions");
2369 for (
int pol = 0; pol < npol_sc; pol++) {
2374 cpl_msg_info (cpl_func,
"Compute QC OIVIS2 and OIVIS for SC");
2380 double pfactor = 0, vfactor = 0;
2381 cpl_size total_weight = 0;
2382 for (
int frame = 0; frame < nb_frame; frame++) {
2383 cpl_size weight = cpl_propertylist_get_long_long(frame_qcs[frame],
"NROW");
2384 total_weight += weight;
2385 sprintf (qc_name,
"ESO QC VFACTOR%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2386 vfactor += weight * cpl_propertylist_get_double(frame_qcs[frame], qc_name);
2387 sprintf (qc_name,
"ESO QC PFACTOR%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2388 pfactor += weight * cpl_propertylist_get_double(frame_qcs[frame], qc_name);
2390 vfactor /= total_weight;
2391 pfactor /= total_weight;
2393 sprintf (qc_name,
"ESO QC VFACTOR%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2395 cpl_propertylist_set_comment (
plist, qc_name,
"mean v-factor");
2397 sprintf (qc_name,
"ESO QC PFACTOR%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2399 cpl_propertylist_set_comment (
plist, qc_name,
"mean p-factor");
2401 sprintf (qc_name,
"ESO QC GD_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2403 cpl_propertylist_set_comment (
plist, qc_name,
"[m] mean Group-Delay");
2405 sprintf (qc_name,
"ESO QC VIS2_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2407 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2409 sprintf (qc_name,
"ESO QC VIS2ERR_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2411 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2413 sprintf (qc_name,
"ESO QC VISPHI_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2415 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2417 sprintf (qc_name,
"ESO QC VISPHIERR_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2419 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2421 sprintf (qc_name,
"ESO QC VISAMP_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2423 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2425 sprintf (qc_name,
"ESO QC VISAMPERR_SC%s_P%d AVG",
GRAVI_BASE_NAME[base], pol+1);
2427 cpl_propertylist_set_comment (
plist, qc_name,
"mean over lbd");
2430 sprintf (qc_name,
"ESO QC PHASE_REF_COEFF2 SC%s_P%d",
GRAVI_BASE_NAME[base], pol+1);
2432 cpl_propertylist_set_comment (
plist, qc_name,
"[rad] 2sd order of FT phase");
2434 CPLCHECK_MSG(
"Cannot set QC parameter for OI_VIS for SC");
2444 for (
int clo = 0; clo < nclo; clo++) {
2445 sprintf (qc_name,
"ESO QC P3FACTOR%s_P%d AVG",
GRAVI_CLO_NAME[clo], pol+1);
2446 cpl_size total_weight = 0;
2447 double p3factor = 0;
2448 for (
int frame = 0; frame < nb_frame; frame++) {
2449 cpl_size weight = cpl_propertylist_get_long_long(frame_qcs[frame],
"NROW");
2450 total_weight += weight;
2451 p3factor += weight * cpl_propertylist_get_double(frame_qcs[frame], qc_name);
2453 p3factor /= total_weight;
2456 cpl_propertylist_set_comment (
plist, qc_name,
"mean p3-factor");
2458 sprintf (qc_name,
"ESO QC T3PHI_SC%s_P%d AVG",
GRAVI_CLO_NAME[clo], pol+1);
2460 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2462 sprintf (qc_name,
"ESO QC T3PHIERR_SC%s_P%d AVG",
GRAVI_CLO_NAME[clo], pol+1);
2464 cpl_propertylist_set_comment (
plist, qc_name,
"[deg] mean over lbd");
2466 CPLCHECK_MSG(
"Cannot set QC parameter for OI_T3 for SC");
2476 for (
int tel = 0; tel <
ntel; tel++){
2478 sprintf (qc_name,
"ESO QC FLUX_SC%d_P%d AVG", tel+1, pol+1);
2480 cpl_propertylist_set_comment (
plist, qc_name,
"[e/total_int_time] mean over lbd");
2482 sprintf (qc_name,
"ESO QC FLUXERR_SC%d_P%d AVG", tel+1, pol+1);
2484 cpl_propertylist_set_comment (
plist, qc_name,
"[e/total_int_time] mean over lbd");
2486 sprintf (qc_name,
"ESO QC FLUXRATE_SC%d_P%d SUM", tel+1, pol+1);
2487 double flux_rate = cpl_array_get_mean (cpl_table_get_array (oi_flux_SC,
"FLUX", tel)) *
2488 cpl_array_get_size(cpl_table_get_array (oi_flux_SC,
"FLUX", tel)) / cpl_table_get_double (oi_flux_SC,
"INT_TIME", tel, &nv);
2490 cpl_propertylist_set_comment (
plist, qc_name,
"[e/s] sum over lbd");
2492 double ftpos_mean = cpl_table_get (oi_flux_SC,
"FT_POS", tel, NULL);
2493 sprintf (qc_name,
"ESO QC FT_POS SC%d_P%d", tel+1, pol+1);
2495 cpl_propertylist_set_comment (
plist, qc_name,
"[V]");
2497 double oplair_mean = cpl_table_get (oi_flux_SC,
"OPL_AIR", tel, NULL);
2498 sprintf (qc_name,
"ESO QC OPL_AIR SC%d_P%d", tel+1, pol+1);
2500 cpl_propertylist_set_comment (
plist, qc_name,
"[m]");
2502 CPLCHECK_MSG(
"Cannot set QC parameter for OI_FLUX for SC");
2510 cpl_propertylist * idp_hdr =
gravi_idp_compute(vis_data, vis_header, frameset, input_data_type);
2511 cpl_propertylist_append(
plist, idp_hdr);
2512 cpl_propertylist_delete(idp_hdr);
2518 cpl_propertylist_update_string (
plist,
"ESO QC FIELD MODE",
2520 cpl_propertylist_set_comment (
plist,
"ESO QC FIELD MODE",
"Field mode");
2525 return CPL_ERROR_NONE;
2542 double * min_uvcoord,
2543 double * max_uvcoord)
2545 cpl_ensure (oi_vis2, CPL_ERROR_NULL_INPUT, 0.0);
2547 *min_uvcoord = DBL_MAX;
2548 *max_uvcoord = DBL_MIN;
2551 cpl_size nrow = cpl_table_get_nrow (oi_vis2);
2553 const double * ucoord = cpl_table_get_data_double_const(oi_vis2,
"UCOORD");
2554 const double * vcoord = cpl_table_get_data_double_const(oi_vis2,
"VCOORD");
2556 const cpl_array ** flags = cpl_table_get_data_array_const (oi_vis2,
"FLAG");
2559 cpl_size flag_array_size = cpl_array_get_size(flags[0]);
2560 for (cpl_size r=0; r<nrow;r++)
2563 for(cpl_size el=0; el<flag_array_size; el++)
2565 if (cpl_array_get_int(flags[r], el, &valid))
2573 uvcoord = sqrt(ucoord[r]*ucoord[r]+vcoord[r]*vcoord[r]);
2574 if(uvcoord > *max_uvcoord)
2575 *max_uvcoord = uvcoord;
2576 if(uvcoord < *min_uvcoord)
2577 *min_uvcoord = uvcoord;
2580 return CPL_ERROR_NONE;
2599 cpl_ensure_code (vis_data, CPL_ERROR_NULL_INPUT);
2606 return cpl_error_set_message(cpl_func,CPL_ERROR_ILLEGAL_INPUT,
"polarisation of SC and FT shall be compatible");
2611 for ( pol= 0 ; pol < npol ; pol++ ) {
2627 for (cpl_size base = 0; base < cpl_table_get_nrow (oi_vis2_ft); base ++) {
2630 const cpl_array * vis2_ft = cpl_table_get_array (oi_vis2_ft,
"VIS2DATA", base);
2633 cpl_array * vis2_lr =
gravi_array_rebin (cpl_table_get_array (oi_vis2_sc,
"VIS2DATA", base),
2634 cpl_table_get_array (oi_vis2_sc,
"VIS2ERR", base),
2635 oi_wave_sc, oi_wave_ft);
2639 qFactor = cpl_array_get_mean (vis2_lr) / cpl_array_get_mean (vis2_ft);
2640 cpl_msg_info (cpl_func,
"vis2 %lli: qFactor = %f", base, qFactor);
2643 cpl_array_divide_scalar (cpl_table_get_data_array (oi_vis2_sc,
"VIS2DATA")[base], qFactor);
2644 cpl_array_divide_scalar (cpl_table_get_data_array (oi_vis2_sc,
"VIS2ERR")[base], qFactor);
2647 const cpl_array * vis_ft = cpl_table_get_array (oi_vis_ft,
"VISAMP", base);
2650 cpl_array * vis_lr =
gravi_array_rebin (cpl_table_get_array (oi_vis_sc,
"VISAMP", base),
2651 cpl_table_get_array (oi_vis_sc,
"VISAMPERR", base),
2652 oi_wave_sc, oi_wave_ft);
2655 qFactor = cpl_array_get_mean (vis_lr) / cpl_array_get_mean (vis_ft);
2656 cpl_msg_info (cpl_func,
"visAmp %lli: qFactor = %f", base, qFactor);
2659 cpl_array_divide_scalar (cpl_table_get_data_array (oi_vis_sc,
"VISAMP")[base], qFactor);
2660 cpl_array_divide_scalar (cpl_table_get_data_array (oi_vis_sc,
"VISAMPERR")[base], qFactor);
2662 FREE (cpl_array_delete, vis2_lr);
2663 FREE (cpl_array_delete, vis_lr);
2669 return CPL_ERROR_NONE;
2685 cpl_ensure_code (vis_data, CPL_ERROR_NULL_INPUT);
2690 for (
int ext = 0; ext < next; ext ++) {
2693 if (!strcmp (extname,
"OI_VIS") ||
2694 !strcmp (extname,
"OI_VIS2") ||
2695 !strcmp (extname,
"OI_T3") ||
2696 !strcmp (extname,
"OI_FLUX")) {
2700 sprintf (date,
"%.10sT00:00:00.000", cpl_propertylist_get_string (
plist,
"DATE-OBS"));
2704 cpl_msg_debug (cpl_func,
"DATE-OBS = %s -> mjd = %.3f", date, mjd0);
2708 cpl_size nrow = cpl_table_get_nrow (oi_table);
2709 for (cpl_size row = 0; row < nrow; row++) {
2710 double mjd = cpl_table_get (oi_table,
"MJD", row, NULL);
2711 cpl_table_set (oi_table,
"TIME", row, (mjd-mjd0) * 24 * 3600);
2715 cpl_table_set_column_unit (oi_table,
"TIME",
"s");
2720 return CPL_ERROR_NONE;
2739 cpl_ensure_code (vis_data, CPL_ERROR_NULL_INPUT);
2740 cpl_ensure_code (p2vm_map, CPL_ERROR_NULL_INPUT);
2742 int num_used_tf = 1;
2750 for (
int type_data = 0; type_data < ntype_data ; type_data ++) {
2754 for (
int pol= 0 ; pol < npol ; pol++ ) {
2757 double delta_t = 10000.0;
2761 "FLUX",
"FLUXERR", 4, delta_t);
2771 return CPL_ERROR_NONE;
2788 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
2789 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
2790 cpl_ensure_code (err, CPL_ERROR_NULL_INPUT);
2792 cpl_size nwave = cpl_table_get_column_depth (oi_table, name);
2797 int nrow = cpl_table_get_nrow (oi_table) / nbase;
2798 for (cpl_size base = 0; base < nbase ; base++) {
2799 cpl_array_fill_window (weight, 0, nwave, 0.0);
2800 cpl_array_fill_window (value, 0, nwave, 0.0);
2803 for (cpl_size row = 0; row < nrow ; row++) {
2804 const cpl_array * rval = cpl_table_get_array (oi_table, name, base + row*nbase);
2805 const cpl_array * rerr = cpl_table_get_array (oi_table, err, base + row*nbase);
2806 const cpl_array * flag = cpl_table_get_array (oi_table,
"FLAG", base + row*nbase);
2807 for (cpl_size wave = 0; wave < nwave; wave++) {
2808 double w = pow (cpl_array_get (rerr, wave, NULL), -2);
2809 if (cpl_array_get (flag, wave, NULL)) w = 10e-20;
2810 double v = cpl_array_get (rval, wave, NULL);
2811 cpl_array_set (weight, wave, cpl_array_get (weight, wave, NULL) + w);
2812 cpl_array_set (value, wave, cpl_array_get (value, wave, NULL) + v * w);
2818 cpl_array_divide (value, weight);
2819 cpl_table_set_array (oi_table, name, base, value);
2822 cpl_array_power (weight, -0.5);
2823 cpl_table_set_array (oi_table, err, base, weight);
2829 FREE (cpl_array_delete, weight);
2830 FREE (cpl_array_delete, value);
2831 return CPL_ERROR_NONE;
2849 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
2850 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
2851 cpl_ensure_code (err, CPL_ERROR_NULL_INPUT);
2853 cpl_size nwave = cpl_table_get_column_depth (oi_table, name);
2858 int nrow = cpl_table_get_nrow (oi_table) / nbase;
2859 for (cpl_size base = 0; base < nbase ; base++) {
2860 cpl_array_fill_window (weight, 0, nwave, 0.0);
2861 cpl_array_fill_window_complex (value, 0, nwave, 0.0);
2864 for (cpl_size row = 0; row < nrow ; row++) {
2865 const cpl_array * rval = cpl_table_get_array (oi_table, name, base + row*nbase);
2866 const cpl_array * rerr = cpl_table_get_array (oi_table, err, base + row*nbase);
2867 const cpl_array * flag = cpl_table_get_array (oi_table,
"FLAG", base + row*nbase);
2868 for (cpl_size wave = 0; wave < nwave; wave++) {
2869 double w = pow (cpl_array_get (rerr, wave, NULL), -2);
2870 if (cpl_array_get (flag, wave, NULL)) w = 10e-20;
2871 double complex v = cexp (1.*I * cpl_array_get (rval, wave, NULL) * CPL_MATH_RAD_DEG);
2872 cpl_array_set (weight, wave, cpl_array_get (weight, wave, NULL) + w);
2873 cpl_array_set_complex (value, wave, cpl_array_get_complex (value, wave, NULL) + v * w);
2881 cpl_array_power (weight, -0.5);
2882 cpl_table_set_array (oi_table, err, base, weight);
2886 FREE (cpl_array_delete, weight);
2887 FREE (cpl_array_delete, value);
2888 return CPL_ERROR_NONE;
2905 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
2906 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
2910 int nrow = cpl_table_get_nrow (oi_table) / nbase;
2911 for (cpl_size base = 0; base < nbase ; base++) {
2912 double weight = 0.0;
2916 for (cpl_size row = 0; row < nrow ; row++) {
2917 double w = (err!=NULL ? pow (cpl_table_get (oi_table, err, base + row*nbase, &nv), -2.0) : 1.0);
2918 value += cpl_table_get (oi_table, name, base + row*nbase, &nv) * w;
2924 cpl_table_set (oi_table, name, base, value / weight);
2927 return CPL_ERROR_NONE;
2941 const cpl_parameterlist * parlist)
2944 cpl_ensure_code (oi_data, CPL_ERROR_NULL_INPUT);
2945 cpl_ensure_code (parlist, CPL_ERROR_NULL_INPUT);
2955 cpl_msg_info (cpl_func,
"Force VISAMPERR to %.e for all observations", err);
2958 for (
int pol = 0; pol < npol; pol++) {
2960 cpl_array ** array = cpl_table_get_data_array (oi_table,
"VISAMPERR");
2961 for (cpl_size row = 0; row<cpl_table_get_nrow (oi_table); row++) {
2962 cpl_array_fill_window (array[row], 0, CPL_SIZE_MAX, err);
2970 cpl_msg_info (cpl_func,
"Force VISPHIERR to %.e for all observations", err);
2973 for (
int pol = 0; pol < npol; pol++) {
2975 cpl_array ** array = cpl_table_get_data_array (oi_table,
"VISPHIERR");
2976 for (cpl_size row = 0; row<cpl_table_get_nrow (oi_table); row++) {
2977 cpl_array_fill_window (array[row], 0, CPL_SIZE_MAX, err);
2985 cpl_msg_info (cpl_func,
"Force FLUXERR to %.e for all observations", err);
2988 for (
int pol = 0; pol < npol; pol++) {
2990 cpl_array ** array = cpl_table_get_data_array (oi_table,
"FLUXERR");
2991 for (cpl_size row = 0; row<cpl_table_get_nrow (oi_table); row++) {
2992 cpl_array_fill_window (array[row], 0, CPL_SIZE_MAX, err);
3000 cpl_msg_info (cpl_func,
"Force VIS2ERR to %.e for all observations", err);
3003 for (
int pol = 0; pol < npol; pol++) {
3005 cpl_array ** array = cpl_table_get_data_array (oi_table,
"VIS2ERR");
3006 for (cpl_size row = 0; row<cpl_table_get_nrow (oi_table); row++) {
3007 cpl_array_fill_window (array[row], 0, CPL_SIZE_MAX, err);
3013 return CPL_ERROR_NONE;
3033 cpl_ensure_code (oi_data, CPL_ERROR_NULL_INPUT);
3035 gravi_msg_warning (
"FIXME",
"Average the different observation is EXPERIMENTAL");
3037 cpl_msg_warning (cpl_func,
"FIXME: Weightening of UVCOORD and MJD is not done properly yet !");
3038 cpl_msg_warning (cpl_func,
"FIXME: Integration of INT_TIME is not done properly yet !");
3040 cpl_table * oi_table;
3046 for (
int type_data = 0; type_data < 2 ; type_data ++) {
3054 for (
int pol = 0 ; pol < npol ; pol++ ) {
3075 cpl_table_erase_window (oi_table, 6, CPL_SIZE_MAX);
3087 cpl_table_erase_window (oi_table, 6, CPL_SIZE_MAX);
3097 cpl_table_erase_window (oi_table, 4, CPL_SIZE_MAX);
3112 cpl_table_erase_window (oi_table, 4, CPL_SIZE_MAX);
3120 return CPL_ERROR_NONE;
3138 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3139 if (nsamp < 1)
return CPL_ERROR_NONE;
3143 cpl_size nwave = cpl_table_get_column_depth (oi_table, name);
3144 cpl_size nrow = cpl_table_get_nrow (oi_table);
3145 cpl_ensure_code (nrow > 0, CPL_ERROR_ILLEGAL_INPUT);
3148 cpl_array ** v_array = cpl_table_get_data_array (oi_table, name);
3149 cpl_array ** e_array = cpl_table_get_data_array (oi_table, err);
3150 cpl_array ** f_array = cpl_table_get_data_array (oi_table,
"FLAG");
3154 cpl_array * smo_array = cpl_array_duplicate (v_array[0]);
3155 cpl_array * err_array = cpl_array_duplicate (e_array[0]);
3158 for (cpl_size row = 0 ; row < nrow ; row ++) {
3162 cpl_vector * i_vector = cpl_vector_new (nwave);
3163 for (cpl_size wave = 0; wave < nwave; wave++)
3164 cpl_vector_set (i_vector, wave, cpl_array_get (e_array[row],wave,&nv));
3165 cpl_vector * o_vector;
3166 o_vector = cpl_vector_filter_median_create (i_vector, nsamp);
3169 for (cpl_size wave = 0 ; wave < nwave ; wave ++) {
3170 double sum = 0.0, weight = 0.0;
3173 for (cpl_size samp = CPL_MAX(0,wave-nsamp) ; samp < CPL_MIN(nwave,wave+nsamp) ; samp ++) {
3174 if (cpl_array_get (f_array[row],samp,&nv)) {
3178 double w = pow (cpl_vector_get (o_vector,samp), -2);
3179 sum += cpl_array_get (v_array[row],samp,&nv) * w;
3184 cpl_array_set_double (smo_array, wave, sum / weight);
3185 cpl_array_set_double (err_array, wave, pow (weight, -0.5));
3189 cpl_table_set_array (oi_table, name, row, smo_array);
3190 cpl_table_set_array (oi_table, err, row, err_array);
3193 FREE (cpl_vector_delete, i_vector);
3194 FREE (cpl_vector_delete, o_vector);
3197 FREE (cpl_array_delete, smo_array);
3198 FREE (cpl_array_delete, err_array);
3201 return CPL_ERROR_NONE;
3219 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3220 if (nsamp < 1)
return CPL_ERROR_NONE;
3223 cpl_size nwave = cpl_table_get_column_depth (oi_table, name);
3224 cpl_size nrow = cpl_table_get_nrow (oi_table);
3225 cpl_ensure_code (nrow > 0, CPL_ERROR_ILLEGAL_INPUT);
3229 cpl_array ** v_array = cpl_table_get_data_array (oi_table, name);
3230 cpl_array ** e_array = cpl_table_get_data_array (oi_table, err);
3231 cpl_array ** f_array = cpl_table_get_data_array (oi_table,
"FLAG");
3235 cpl_array * smo_array = cpl_array_duplicate (v_array[0]);
3236 cpl_array * err_array = cpl_array_duplicate (e_array[0]);
3239 for (cpl_size row = 0 ; row < nrow ; row ++) {
3243 cpl_vector * i_vector = cpl_vector_new (nwave);
3244 for (cpl_size wave = 0; wave < nwave; wave++)
3245 cpl_vector_set (i_vector, wave, cpl_array_get (e_array[row],wave,&nv));
3246 cpl_vector * o_vector;
3247 o_vector = cpl_vector_filter_median_create (i_vector, nsamp);
3250 for (cpl_size wave = 0 ; wave < nwave ; wave ++) {
3251 double complex sum = 0.0 + I*0.0;
3252 double weight = 0.0;
3255 for (cpl_size samp = CPL_MAX(0,wave-nsamp) ; samp < CPL_MIN(nwave,wave+nsamp) ; samp ++) {
3256 if (cpl_array_get (f_array[row],samp,&nv)) {
3260 double w = pow (cpl_vector_get (o_vector,samp), -2);
3261 sum += cexp (1.*I* cpl_array_get (v_array[row],samp,&nv) * CPL_MATH_RAD_DEG) * w;
3266 cpl_array_set_double (smo_array, wave, carg (sum) * CPL_MATH_DEG_RAD);
3267 cpl_array_set_double (err_array, wave, pow (weight, -0.5));
3271 cpl_table_set_array (oi_table, name, row, smo_array);
3272 cpl_table_set_array (oi_table, err, row, err_array);
3275 FREE (cpl_vector_delete, i_vector);
3276 FREE (cpl_vector_delete, o_vector);
3279 FREE (cpl_array_delete, smo_array);
3280 FREE (cpl_array_delete, err_array);
3283 return CPL_ERROR_NONE;
3298 const char * err, cpl_size maxdeg)
3301 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3302 if (maxdeg < 0)
return CPL_ERROR_NONE;
3305 cpl_size nwave = cpl_table_get_column_depth (oi_table, name);
3306 cpl_size nrow = cpl_table_get_nrow (oi_table);
3307 cpl_ensure_code (nrow > 0, CPL_ERROR_ILLEGAL_INPUT);
3311 cpl_array ** v_array = cpl_table_get_data_array (oi_table, name);
3312 cpl_array ** e_array = cpl_table_get_data_array (oi_table, err);
3313 cpl_array ** f_array = cpl_table_get_data_array (oi_table,
"FLAG");
3317 for (cpl_size row = 0 ; row < nrow ; row ++) {
3320 cpl_matrix * coeff = cpl_matrix_new (nwave,maxdeg+1);
3321 cpl_matrix * rhs = cpl_matrix_new (nwave,1);
3324 for (cpl_size wave = 0 ; wave < nwave ; wave ++) {
3325 double weight = cpl_array_get (f_array[row],wave,&nv) ? 10e-20 :
3326 pow (cpl_array_get (e_array[row],wave,&nv), -2);
3327 double value = cpl_array_get (f_array[row],wave,&nv) ? 0.0 :
3328 cpl_array_get (v_array[row],wave,&nv);
3332 cpl_msg_warning (cpl_func,
"name = %s row = %lli wave = %lli "
3333 "has faulty uncertainty", name, row, wave);
3335 cpl_array_set (f_array[row],wave,1);
3338 cpl_matrix_set (rhs, wave, 0, value * weight);
3339 for (cpl_size deg = 0; deg <= maxdeg; deg++)
3340 cpl_matrix_set (coeff, wave, deg, pow ((
double)wave,(
double)deg) * weight);
3349 cpl_errorstate prev_state = cpl_errorstate_get();
3350 cpl_matrix * solve = cpl_matrix_solve_normal (coeff, rhs);
3353 if ( !cpl_errorstate_is_equal (prev_state))
3355 cpl_errorstate_dump (prev_state, 0, NULL);
3356 cpl_msg_error (cpl_func,
"%s row=%lld",name,row);
3361 for (cpl_size wave = 0 ; wave < nwave ; wave ++) {
3363 for (cpl_size deg = 0; deg <= maxdeg; deg++)
3364 value += cpl_matrix_get (solve, deg, 0) * pow (wave, deg);
3365 cpl_array_set (v_array[row],wave,value);
3369 FREE (cpl_matrix_delete, coeff);
3370 FREE (cpl_matrix_delete, rhs);
3371 FREE (cpl_matrix_delete, solve);
3375 return CPL_ERROR_NONE;
3400 cpl_ensure_code (oi_data, CPL_ERROR_NULL_INPUT);
3402 cpl_table * oi_table;
3410 for (
int pol = 0 ; pol < npol ; pol++ ) {
3416 if (cpl_table_has_column(oi_table,
"FLUXDATA"))
3418 cpl_msg_info(cpl_func,
"Smoothing OIFITS2 FLUXDATA column");
3470 return CPL_ERROR_NONE;
3486 cpl_size nsamp, cpl_size nwave_new)
3489 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3492 cpl_size nrow = cpl_table_get_nrow (oi_table);
3493 cpl_ensure_code (nrow > 0, CPL_ERROR_ILLEGAL_INPUT);
3494 for (cpl_size row = 0 ; row < nrow ; row ++) {
3497 for (cpl_size wave = 0 ; wave < nwave_new ; wave ++) {
3499 double weight = 0.0;
3500 for (cpl_size samp = 0 ; samp < nsamp ; samp ++) {
3512 cpl_table_set_column_depth (oi_table, name, nwave_new);
3513 cpl_table_set_column_depth (oi_table, err, nwave_new);
3516 return CPL_ERROR_NONE;
3532 cpl_size nsamp, cpl_size nwave_new)
3535 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3538 cpl_size nrow = cpl_table_get_nrow (oi_table);
3539 cpl_ensure_code (nrow > 0, CPL_ERROR_ILLEGAL_INPUT);
3540 for (cpl_size row = 0 ; row < nrow ; row ++) {
3543 for (cpl_size wave = 0 ; wave < nwave_new ; wave ++) {
3544 double complex sum = 0.0;
3545 double weight = 0.0;
3546 for (cpl_size samp = 0 ; samp < nsamp ; samp ++) {
3549 sum += cexp (1.*I*
gravi_table_get_value (oi_table,name,row,wave*nsamp+samp) * CPL_MATH_RAD_DEG) * w;
3558 cpl_table_set_column_depth (oi_table, name, nwave_new);
3559 cpl_table_set_column_depth (oi_table, err, nwave_new);
3562 return CPL_ERROR_NONE;
3581 cpl_ensure_code (oi_data, CPL_ERROR_NULL_INPUT);
3582 cpl_ensure_code (nsamp>1, CPL_ERROR_ILLEGAL_INPUT);
3584 cpl_table * oi_table;
3585 cpl_size nwave, nwave_new;
3593 for (
int pol = 0 ; pol < npol ; pol++ ) {
3597 nwave = cpl_table_get_nrow (oi_table);
3600 nwave_new = nwave / nsamp;
3601 cpl_msg_info (cpl_func,
"Resamp the SC data by %lld bins: %lld -> %lld",
3602 nsamp, nwave, nwave_new);
3603 cpl_ensure_code (nwave_new > 1, CPL_ERROR_ILLEGAL_INPUT);
3606 for (cpl_size wave = 0 ; wave < nwave_new ; wave ++) {
3608 cpl_table_set (oi_table,
"EFF_BAND", wave,
3609 cpl_table_get (oi_table,
"EFF_WAVE", wave*nsamp+nsamp-1, &nv) -
3610 cpl_table_get (oi_table,
"EFF_WAVE", wave*nsamp, &nv));
3614 for (cpl_size samp = 0 ; samp < nsamp ; samp++)
3615 mean += cpl_table_get (oi_table,
"EFF_WAVE", wave*nsamp+samp, &nv);
3616 cpl_table_set (oi_table,
"EFF_WAVE", wave, mean / nsamp);
3620 cpl_table_erase_window (oi_table, nwave_new, CPL_SIZE_MAX);
3626 cpl_table_set_column_depth (oi_table,
"FLAG", nwave_new);
3633 cpl_table_set_column_depth (oi_table,
"FLAG", nwave_new);
3643 cpl_table_set_column_depth (oi_table,
"FLAG", nwave_new);
3647 cpl_table_set_column_depth (oi_table,
"VISDATA", nwave_new);
3648 cpl_table_set_column_depth (oi_table,
"VISERR", nwave_new);
3655 cpl_table_set_column_depth (oi_table,
"FLAG", nwave_new);
3662 return CPL_ERROR_NONE;
3678 cpl_ensure_code (oi_data, CPL_ERROR_NULL_INPUT);
3684 for (
int type_data = 0; type_data < 2 ; type_data ++) {
3692 for (
int pol = 0 ; pol < npol ; pol++ ) {
3695 cpl_table * oi_flux;
3699 if (cpl_table_has_column (oi_flux,
"FLUXDATA") )
3700 cpl_table_erase_column (oi_flux,
"FLUXDATA");
3703 cpl_table_duplicate_column (oi_flux,
"FLUXDATA", oi_flux,
"FLUX");
3707 cpl_table_erase_column(oi_flux,
"FLUX");
3713 return CPL_ERROR_NONE;
3727 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3729 cpl_size nrow = cpl_table_get_nrow (oi_table);
3730 cpl_ensure_code (nrow > 0, CPL_ERROR_ILLEGAL_INPUT);
3733 const char * names[] = {
"VIS2DATA",
"VIS2ERR",
"VISAMP",
"VISAMPERR",
3734 "VISPHI",
"VISPHIERR",
"T3PHI",
"T3PHIERR",
3735 "T3AMP",
"T3AMPERR"};
3738 for (
int c = 0; c < ncols; c++) {
3740 if (!cpl_table_has_column (oi_table,names[c]))
continue;
3744 cpl_size nwave = cpl_table_get_column_depth (oi_table, names[c]);
3745 cpl_array ** v_array = cpl_table_get_data_array (oi_table, names[c]);
3746 cpl_array ** f_array = cpl_table_get_data_array (oi_table,
"FLAG");
3750 cpl_size ninvalid = 0;
3751 for (cpl_size row = 0; row < nrow ; row ++) {
3752 for (cpl_size wave = 0 ; wave < nwave ; wave ++) {
3756 double value = cpl_array_get (v_array[row], wave, &nv);
3759 if (nv || isnan (value)) {
3760 cpl_array_set (f_array[row], wave, 1.0);
3761 cpl_array_set (v_array[row], wave, 0.0);
3770 cpl_msg_warning (cpl_func,
"Flag %lld invalid data (NAN or NULL) in %s", ninvalid, names[c]);
3776 return CPL_ERROR_NONE;
3796 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3797 cpl_ensure_code (data, CPL_ERROR_NULL_INPUT);
3798 cpl_ensure_code (flag, CPL_ERROR_ILLEGAL_OUTPUT);
3802 cpl_size nrow = cpl_table_get_nrow (oi_table);
3803 cpl_array ** pdata = cpl_table_get_data_array (oi_table, data);
3804 cpl_array ** pflag = cpl_table_get_data_array (oi_table, flag);
3808 cpl_size size = cpl_array_get_size (pdata[0]);
3811 for ( cpl_size row = 0 ; row < nrow ; row ++ ) {
3812 if (pdata[row]==NULL)
continue;
3814 for ( cpl_size indx = 0 ; indx < size ; indx ++ ) {
3815 if ( cpl_array_get (pdata[row], indx, &nv) > value ) {
3816 cpl_array_set (pflag[row], indx, cpl_array_get (pflag[row], indx, &nv) + 1 );
3822 return CPL_ERROR_NONE;
3842 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3843 cpl_ensure_code (data, CPL_ERROR_NULL_INPUT);
3844 cpl_ensure_code (flag, CPL_ERROR_ILLEGAL_OUTPUT);
3848 cpl_size nrow = cpl_table_get_nrow (oi_table);
3849 cpl_array ** pdata = cpl_table_get_data_array (oi_table, data);
3850 cpl_array ** pflag = cpl_table_get_data_array (oi_table, flag);
3854 cpl_size size = cpl_array_get_size (pdata[0]);
3857 for ( cpl_size row = 0 ; row < nrow ; row ++ ) {
3858 if (pdata[row]==NULL)
continue;
3860 for ( cpl_size indx = 0 ; indx < size ; indx ++ ) {
3861 if ( cpl_array_get (pdata[row], indx, &nv) <= value ) {
3862 cpl_array_set (pflag[row], indx, cpl_array_get (pflag[row], indx, &nv) + 1 );
3868 return CPL_ERROR_NONE;
3888 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3889 cpl_ensure_code (data, CPL_ERROR_NULL_INPUT);
3890 cpl_ensure_code (flag, CPL_ERROR_ILLEGAL_OUTPUT);
3894 cpl_size nrow = cpl_table_get_nrow (oi_table);
3895 cpl_array ** pdata = cpl_table_get_data_array (oi_table, data);
3896 cpl_array ** pflag = cpl_table_get_data_array (oi_table, flag);
3900 cpl_size size = cpl_array_get_size (pdata[0]);
3901 cpl_vector * i_vector = cpl_vector_new (size);
3904 for ( cpl_size row = 0 ; row < nrow ; row ++ ) {
3905 if (pdata[row]==NULL || size<100)
continue;
3908 for (cpl_size indx = 0; indx < size; indx++)
3909 cpl_vector_set (i_vector, indx, cpl_array_get (pdata[0],indx,&nv));
3912 cpl_vector * o_vector = cpl_vector_filter_median_create (i_vector, 4);
3915 for ( cpl_size indx = 0 ; indx < size ; indx ++ ) {
3916 if ( cpl_array_get (pdata[row], indx, &nv) > value * cpl_vector_get (o_vector, indx)) {
3917 cpl_array_set (pflag[row], indx, cpl_array_get (pflag[row], indx, &nv) + 1 );
3921 FREE (cpl_vector_delete, o_vector);
3924 FREE (cpl_vector_delete, i_vector);
3926 return CPL_ERROR_NONE;
3946 const char * data,
const char *flag,
double value)
3949 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3950 cpl_ensure_code (data, CPL_ERROR_NULL_INPUT);
3951 cpl_ensure_code (err, CPL_ERROR_NULL_INPUT);
3952 cpl_ensure_code (flag, CPL_ERROR_ILLEGAL_OUTPUT);
3956 cpl_size row, nrow = cpl_table_get_nrow (oi_table);
3957 cpl_array ** perr = cpl_table_get_data_array (oi_table, err);
3958 cpl_array ** pdata = cpl_table_get_data_array (oi_table, data);
3959 cpl_array ** pflag = cpl_table_get_data_array (oi_table, flag);
3963 cpl_size indx, size = cpl_array_get_size (pdata[0]);
3966 for ( row = 0 ; row < nrow ; row ++ ) {
3967 if (perr[row]==NULL)
continue;
3968 cpl_array * tmp = cpl_array_duplicate (perr[row]);
3969 cpl_array_divide (tmp, pdata[row]);
3970 for ( indx = 0 ; indx < size ; indx ++ ) {
3971 if ( cpl_array_get (tmp, indx, &nv) > value) {
3972 cpl_array_set (pflag[row], indx, cpl_array_get (pflag[row], indx, &nv) + 1 );
3975 cpl_array_delete (tmp);
3979 return CPL_ERROR_NONE;
3998 cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
3999 cpl_ensure_code (flag_array, CPL_ERROR_NULL_INPUT);
4002 cpl_size nrow = cpl_table_get_nrow (oi_table) /
ntel;
4003 cpl_ensure_code (nrow == cpl_array_get_size (flag_array),
4004 CPL_ERROR_ILLEGAL_INPUT);
4007 cpl_table_unselect_all (oi_table);
4008 for (cpl_size row = 0; row < nrow; row++) {
4009 if (cpl_array_get (flag_array, row, NULL) == 0)
continue;
4010 cpl_table_or_selected_window (oi_table, row *
ntel,
ntel);
4014 cpl_table_erase_selected (oi_table);
4018 return CPL_ERROR_NONE;
4044 cpl_table * in_table,
4045 const char * name,
int ntel)
4048 cpl_ensure_code (out_table, CPL_ERROR_NULL_INPUT);
4049 cpl_ensure_code (in_table, CPL_ERROR_NULL_INPUT);
4050 cpl_ensure_code (name, CPL_ERROR_NULL_INPUT);
4051 cpl_ensure_code (
ntel == cpl_table_get_nrow (out_table),
4052 CPL_ERROR_ILLEGAL_OUTPUT);
4055 if ( !cpl_table_has_column (in_table, name)) {
4056 cpl_msg_info (cpl_func,
"Cannot average column %s (not existing)", name);
4057 return CPL_ERROR_NONE;
4061 int type = cpl_table_get_column_type (in_table, name);
4062 cpl_size depth = cpl_table_get_column_depth (in_table, name);
4065 cpl_size nrow = cpl_table_get_nrow (in_table) /
ntel;
4066 cpl_ensure_code (nrow, CPL_ERROR_ILLEGAL_INPUT);
4070 if (cpl_table_has_column (in_table,
"REJECTION_FLAG"))
4071 flag = cpl_table_get_data_int (in_table,
"REJECTION_FLAG");
4073 cpl_msg_info (cpl_func,
"Average column: %s (%s REJECTION_FLAG)",
4074 name, flag ?
"with" :
"without");
4077 case CPL_TYPE_DOUBLE:
4078 case CPL_TYPE_FLOAT:
4081 if (!cpl_table_has_column (out_table, name))
4082 cpl_table_new_column (out_table, name, CPL_TYPE_DOUBLE);
4083 for (
int tel = 0; tel <
ntel; tel++) {
4084 cpl_size nvalid = 0;
4086 for (cpl_size row = 0; row < nrow; row++) {
4087 if (flag && flag[row*
ntel+tel] != 0)
continue;
4089 mean += cpl_table_get (in_table, name, row*
ntel+tel, NULL);
4091 if (nvalid > 0) mean /= nvalid;
4092 cpl_table_set (out_table, name, tel, mean);
4096 case CPL_TYPE_POINTER|CPL_TYPE_DOUBLE:
4097 case CPL_TYPE_POINTER|CPL_TYPE_FLOAT:
4098 case CPL_TYPE_POINTER|CPL_TYPE_INT:
4100 if (!cpl_table_has_column (out_table, name))
4101 cpl_table_new_column_array (out_table, name, CPL_TYPE_DOUBLE, depth);
4102 for (
int tel = 0; tel <
ntel; tel++) {
4103 cpl_size nvalid = 0;
4105 for (cpl_size row = 0; row < nrow; row++) {
4106 if (flag && flag[row*
ntel+tel] != 0)
continue;
4108 cpl_array_add (mean, cpl_table_get_array (in_table, name, row*
ntel+tel));
4111 if (nvalid > 0) cpl_array_divide_scalar (mean, nvalid);
4112 cpl_table_set_array (out_table, name, tel, mean);
4113 FREE (cpl_array_delete, mean);
4117 case CPL_TYPE_POINTER|CPL_TYPE_DOUBLE_COMPLEX:
4118 case CPL_TYPE_POINTER|CPL_TYPE_FLOAT_COMPLEX:
4120 if (!cpl_table_has_column (out_table, name))
4121 cpl_table_new_column_array (out_table, name, CPL_TYPE_DOUBLE_COMPLEX, depth);
4122 for (
int tel = 0; tel <
ntel; tel++) {
4123 cpl_size nvalid = 0;
4125 for (cpl_size row = 0; row < nrow; row++) {
4126 if (flag && flag[row*
ntel+tel] != 0)
continue;
4128 cpl_array_add (mean, cpl_table_get_array (in_table, name, row*
ntel+tel));
4130 if (nvalid > 0) cpl_array_divide_scalar (mean, nvalid);
4131 cpl_table_set_array (out_table, name, tel, mean);
4133 FREE (cpl_array_delete, mean);
4137 cpl_msg_error (cpl_func,
"Type column not yet supported...");
4138 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"This type is not supported.");
4139 return CPL_ERROR_ILLEGAL_INPUT;
4143 cpl_table_set_column_unit (out_table, name, cpl_table_get_column_unit (in_table, name));
4146 return CPL_ERROR_NONE;
4166 cpl_ensure_code (oi_data, CPL_ERROR_NULL_INPUT);
4170 cpl_ensure_code (nb_ext>0, CPL_ERROR_ILLEGAL_INPUT);
4173 double mean_mjd = 0.0;
4174 double mean_time = 0.0;
4178 for (
int ext = 0; ext < nb_ext; ext++) {
4182 if (!strcmp (extname,
"OI_VIS") || !strcmp (extname,
"OI_VIS2") ||
4183 !strcmp (extname,
"OI_T3") || !strcmp (extname,
"OI_FLUX")) {
4187 mean_mjd += cpl_table_get_column_mean (oi_table,
"MJD");
4188 mean_time += cpl_table_get_column_mean (oi_table,
"TIME");
4196 cpl_ensure_code (count>0, CPL_ERROR_ILLEGAL_INPUT);
4201 cpl_msg_info (cpl_func,
"Mean MDJ = %g [mdj]", mean_mjd);
4202 cpl_msg_info (cpl_func,
"Mean TIME = %g [s]", mean_time);
4205 for (
int ext = 0; ext < nb_ext; ext++) {
4209 if (!strcmp (extname,
"OI_VIS") || !strcmp (extname,
"OI_VIS2") ||
4210 !strcmp (extname,
"OI_T3") || !strcmp (extname,
"OI_FLUX")) {
4214 cpl_table_fill_column_window (oi_table,
"MJD", 0, CPL_SIZE_MAX, mean_mjd);
4215 cpl_table_fill_column_window (oi_table,
"TIME", 0, CPL_SIZE_MAX, mean_time);
4222 return CPL_ERROR_NONE;
#define gravi_table_set_value(table, name, row, value, val)
#define gravi_table_get_value(table, name, row, value)
typedefCPL_BEGIN_DECLS struct _gravi_data_ gravi_data
#define gravi_data_get_oi_t3(data, type, pol, npol)
#define gravi_data_get_oi_flux(data, type, pol, npol)
#define gravi_data_get_header(data)
#define gravi_data_get_oi_vis2(data, type, pol, npol)
#define gravi_data_get_oi_wave(data, type, pol, npol)
#define gravi_data_get_oi_vis_plist(data, type, pol, npol)
#define gravi_data_get_oi_vis(data, type, pol, npol)
#define gravi_data_get_extname(data, ext)
cpl_propertylist * gravi_idp_compute(gravi_data *vis_data, cpl_propertylist *header, cpl_frameset *frameset, char *input_data_type)
Create IDP keywords to satisfy standard.
cpl_msg_debug(cpl_func, "Spectra has <50 pixels -> don't flat")
cpl_propertylist * header
cpl_msg_info(cpl_func, "Compute WAVE_SCAN for %s", GRAVI_TYPE(type_data))
cpl_propertylist_update_double(header, "ESO QC MINWAVE SC", cpl_propertylist_get_double(plist, "ESO QC MINWAVE SC"))
#define GRAVI_OI_VIS2_EXT
#define GRAVI_INSNAME(type, pol, npol)
#define GRAVI_OI_TARGET_EXT
#define GRAVI_OI_ARRAY_EXT
#define GRAVI_OI_FLUX_EXT
#define GRAVI_OI_WAVELENGTH_EXT
#define gravi_msg_function_exit(flag)
#define FREE(function, variable)
#define CPLCHECK_NUL(msg)
#define gravi_msg_function_start(flag)
#define CPLCHECK_MSG(msg)
#define FREELOOP(function, variable, n)
cpl_error_code gravi_table_set_array_double_complex(cpl_table *table, const char *name, cpl_size row, cpl_array *visR, cpl_array *visI)
cpl_table * gravi_table_extract_time_interval(cpl_table *table, double start, double end)
Extract rows from table based on the TIME column.
double gravi_table_get_column_mean(cpl_table *table, const char *name, int base, int nbase)
cpl_error_code gravi_table_new_column(cpl_table *table, const char *name, const char *unit, cpl_type type)
cpl_error_code gravi_table_set_array_phase(cpl_table *table, const char *name, cpl_size row, cpl_array *phase)
cpl_array * gravi_array_init_double(long n, double value)
cpl_error_code gravi_array_get_group_delay_loop(cpl_array **input, cpl_array **flag, cpl_array *sigma, double *gd, cpl_size nrow, double max_width, int verbose)
Optimized computation of GDELAY for a list of arrays.
cpl_array * gravi_table_get_column_sum_array(cpl_table *table, const char *name, int base, int nbase)
cpl_array * gravi_array_compute_norm2(cpl_array *input_re, cpl_array *input_im)
int gravi_array_threshold_min(cpl_array *array, double lo_cut)
cpl_array * gravi_array_rebin(const cpl_array *input, const cpl_array *errs, cpl_table *oi_wave_sc, cpl_table *oi_wave_ft)
cpl_array * gravi_array_init_double_complex(long n, double complex value)
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_array * gravi_array_wrap_complex(cpl_array *input_re, cpl_array *input_im)
cpl_propertylist * gravi_data_get_extra_primary_header(gravi_data *self)
Get the propertylist for additional keywords to the primary header.
gravi_data * gravi_data_new(int nb_ext)
Create an empty gravi_data.
cpl_error_code gravi_data_add_table(gravi_data *self, cpl_propertylist *plist, const char *extname, cpl_table *table)
Add a BINTABLE extension in gravi_data.
int gravi_data_has_type(gravi_data *self, const char *type)
Return the number of ext whose EXTNAME and INSNAME match 'type'.
cpl_table * gravi_data_get_table(gravi_data *self, const char *extname)
Return a pointer on a table extension by its EXTNAME.
cpl_propertylist * gravi_data_get_plist_x(gravi_data *self, int i)
Get the propertylist of an extension by position.
int gravi_data_get_size(const gravi_data *self)
Get the number of extension in a gravi_data.
cpl_error_code gravi_data_copy_ext(gravi_data *output, gravi_data *input, const char *name)
Copy extensions from one data to another.
cpl_table * gravi_data_get_table_x(gravi_data *self, int i)
Get the table of an extension by position.
int gravi_param_get_bool(const cpl_parameterlist *parlist, const char *name)
const char * gravi_param_get_string(const cpl_parameterlist *parlist, const char *name)
int gravi_param_get_int(const cpl_parameterlist *parlist, const char *name)
double gravi_param_get_double(const cpl_parameterlist *parlist, const char *name)
double gravi_param_get_double_default(const cpl_parameterlist *parlist, const char *name, double def)
Get the parameter from the parameter list.
int gravi_pfits_get_pola_num(const cpl_propertylist *plist, int type_data)
const char * gravi_pfits_get_mode_name(const cpl_propertylist *plist)
double gravi_pfits_get_sobj_y(const cpl_propertylist *plist)
double gravi_pfits_get_metfc_lockmjd(const cpl_propertylist *plist, int tel)
double gravi_convert_to_mjd(const char *start)
double gravi_pfits_get_dit_sc(const cpl_propertylist *plist)
double gravi_pfits_get_sobj_x(const cpl_propertylist *plist)
cpl_error_code gravi_apply_tf_amp(gravi_data *science, gravi_data *science_tf, gravi_data **used_tf_data, int num_tf_data, const char *extName, const char *insName, const char *ampName, const char *ampErrName, int nbase, double delta_t)
Interpolate the TF at the time of the science observation for an amplitude quantity.
cpl_error_code gravi_msg_warning(const char *component, const char *msg)
char GRAVI_BASE_NAME[6][3]
char GRAVI_CLO_NAME[4][4]
cpl_table * gravi_table_oi_create(int nwave, int nrow, const char *oi_name)
Create the oi table (oi_vis, oi_vis2, oi_t3)
cpl_error_code gravi_vis_average_bootstrap(cpl_table *oi_vis_avg, cpl_table *oi_vis2_avg, cpl_table *oi_vis, int nboot, const char *phase_ref, int use_vFactor, int use_pFactor, int use_debiasing, double outlier_threshold)
Average the visibility of all DITs into a final, averaged value.
cpl_error_code gravi_vis_resamp_amp(cpl_table *oi_table, const char *name, const char *err, cpl_size nsamp, cpl_size nwave_new)
Rebin amplitude column of OIFITS table.
cpl_error_code gravi_normalize_sc_to_ft(gravi_data *vis_data)
Align the SC visibilities on the FT visibilities.
cpl_error_code gravi_force_uncertainties(gravi_data *oi_data, const cpl_parameterlist *parlist)
Force uncertainties.
double gravi_randn(void)
Normal distribution pseudo-random generator.
gravi_data * gravi_compute_vis(gravi_data *p2vmred_data, const cpl_parameterlist *parlist, cpl_size *current_frame)
The function average the individual frames of a P2VMREDUCED file into a final, single observation per...
cpl_error_code gravi_vis_fit_amp(cpl_table *oi_table, const char *name, const char *err, cpl_size maxdeg)
Smooth amp column of OIFITS table.
cpl_error_code gravi_flux_average_bootstrap(cpl_table *oi_flux_avg, cpl_table *oi_flux, int nboot, double outlier_threshold)
Average the flux of all DITs into a final, averaged value.
cpl_error_code gravi_average_self_visphi(cpl_table *oi_vis_avg, cpl_table *oi_vis, cpl_array *wavenumber, const char *phase_ref, int *cmin, int *cmax, int nrange)
Compute Averaged VISPHI in the manner described, e.g., in F. Millour's thesis.
cpl_error_code gravi_vis_flag_nan(cpl_table *oi_table)
Flag samples of OIFITS table which are NAN or NULL.
cpl_error_code gravi_t3_average_bootstrap(cpl_table *oi_t3_avg, cpl_table *oi_vis, cpl_table *oi_flux, int nboot, int use_vFactor, int use_pFactor, double outlier_threshold)
Average the closure-phase of all DITs into a final, averaged value.
cpl_error_code gravi_vis_smooth(gravi_data *oi_data, cpl_size nsamp_vis, cpl_size nsamp_flx, cpl_size maxdeg)
Smooth the SC table by nsamp consecutive spectral bins.
cpl_error_code gravi_vis_force_time(gravi_data *oi_data)
Force all data in OI_TABLE to have the same TIME and MJD.
cpl_error_code gravi_vis_mjd_to_time(gravi_data *vis_data)
Recompute the TIME column of all OIFITS extension from the MJD column, following the OIFITS standard ...
cpl_error_code gravi_vis_compute_column_mean(cpl_table *out_table, cpl_table *in_table, const char *name, int ntel)
Compute the mean of a column in OIFITS table, and save the result in the specified output table.
cpl_error_code gravi_vis_average_phi(cpl_table *oi_table, const char *name, const char *err, int nbase)
Average phases column of a multi-observation OIFITS table Phases are averaged with arg{<exp(i....
double gdAbacusErrPhi(double x)
cpl_error_code gravi_vis_smooth_phi(cpl_table *oi_table, const char *name, const char *err, cpl_size nsamp)
Smooth phase column of OIFITS table.
cpl_error_code gravi_vis_average_amp(cpl_table *oi_table, const char *name, const char *err, int nbase)
Average amplitudes column of a multi-observation OIFITS table The averaged quantities are stored in t...
cpl_error_code gravi_vis_copy_fluxdata(gravi_data *oi_data, int delete_flux)
Duplicate the column FLUX into FLUXDATA, for OIFITS2 compliance.
cpl_error_code gravi_vis_resamp(gravi_data *oi_data, cpl_size nsamp)
Re-bin the SC table by nsamp consecutive spectral bins.
cpl_error_code gravi_vis_resamp_phi(cpl_table *oi_table, const char *name, const char *err, cpl_size nsamp, cpl_size nwave_new)
Rebin phase column of OIFITS table (arg{<exp(i.phi)>})
cpl_error_code gravi_compute_vis_qc(gravi_data *vis_data, cpl_frameset *frameset, cpl_propertylist **frame_qcs, cpl_size nb_frame, char *input_data_type)
Compute the QC parameters for a VIS (averaged) data.
cpl_error_code gravi_data_get_minmax_uvcoord(const cpl_table *oi_vis2, double *min_uvcoord, double *max_uvcoord)
Compute the minimum and maximum values of sqrt(ucoord**2 + vcoord**2)
cpl_error_code gravi_array_online_variance(cpl_array *data, cpl_array *mean, cpl_array *variance, int n)
cpl_error_code gravi_flat_flux(gravi_data *vis_data, gravi_data *p2vm_map)
Divide the OI_FLUX by OI_FLUX from the P2VM (no checks, no time distance...)
cpl_error_code gravi_array_online_variance_res(cpl_array **data, int n, int rephase)
On-line variance of arrays.
cpl_error_code gravi_vis_flag_threshold(cpl_table *oi_table, const char *data, const char *flag, double value)
Flag samples of OIFITS table based on absolute threshold.
cpl_error_code gravi_vis_flag_relative_threshold(cpl_table *oi_table, const char *err, const char *data, const char *flag, double value)
Flag samples of OIFITS table based on relative threshold.
cpl_error_code gravi_vis_flag_lower(cpl_table *oi_table, const char *data, const char *flag, double value)
Flag samples of OIFITS table based on absolute threshold.
cpl_error_code gravi_average_vis(gravi_data *oi_data)
Coadd the observations together.
cpl_error_code gravi_vis_flag_median(cpl_table *oi_table, const char *data, const char *flag, double value)
Flag samples of OIFITS table based on runnning median.
cpl_error_code gravi_vis_smooth_amp(cpl_table *oi_table, const char *name, const char *err, cpl_size nsamp)
Smooth amplitude column of OIFITS table.
cpl_error_code gravi_vis_average_value(cpl_table *oi_table, const char *name, const char *err, int nbase)
Average scalar column of a multi-observation OIFITS table. The averaged quantities are stored in the ...
cpl_error_code gravi_vis_erase_obs(cpl_table *oi_table, cpl_array *flag_array, cpl_size ntel)
Erase observation from an OIFITS table.