MIDI Pipeline Reference Manual  2.8.3
procKappa.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: procKappa.c
7 * Description: Contains routines for Transfer Ratios computations
8 *
9 * History:
10 * 03-Dec-05 (csabet) Created
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /******************************************************************************
15 * Compiler directives
16 ******************************************************************************/
17 
18 /******************************************************************************
19 * Include files
20 ******************************************************************************/
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <cpl.h>
26 #include <stdlib.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include "midiGlobal.h"
30 #include "fft.h"
31 #include "midiLib.h"
32 #include "memoryHandling.h"
33 #include "errorHandling.h"
34 #include "diagnostics.h"
35 #include "imageProcessing.h"
36 #include "calibration.h"
37 #include "statistics.h"
38 #include "fitsAnalysisKappa.h"
39 #include "preProcKappa.h"
40 #include "procKappa.h"
41 #include "procFrgHS.h"
42 #include "createProdKappa.h"
43 
44 /**********************************************************
45 * Constant definitions
46 **********************************************************/
47 
48 /**********************************************************
49 * Global Variables
50 **********************************************************/
51 
52 /*============================ C O D E A R E A ===========================*/
53 
54 
55 
56 
57 /******************************************************************************
58 * European Southern Observatory
59 * VLTI MIDI Data Reduction Software
60 *
61 * Module name: procKappa
62 * Input/Output: See function arguments to avoid duplication
63 * Description: This is the main routine for calculating Transfer Ratios
64 * If options->drsKap = 1, this process creates the Transfer Ratios file
65 * in current directory.
66 * If options->drsKap = 2, this process updates the Transfer Ratios file
67 * in the database as well as creating a file in the current directory
68 *
69 * The relevant file structures are:
70 *
71 * Photometry A files have the following configurations:
72 * DATA1 = Photometry A data
73 * DATA2 = Interferometry data
74 * DATA3 = Interferometry data
75 * DATA4 = NULL
76 *
77 * Photometry B files have the following configurations:
78 * DATA1 = NULL
79 * DATA2 = Interferometry data
80 * DATA3 = Interferometry data
81 * DATA4 = Photometry B data
82 *
83 * History:
84 * 05-Dec-05 (csabet) Created
85 ******************************************************************************/
86 void procKappa (
87  MidiFiles *fileNames, // In: Pointer to midi files structure
88  UserOptions *options, // In: Pointer to user options
89  int *error) // Ou: Error status
90 {
91 
92  // Local Declarations
93  // ------------------
94  const char routine[] = "procKappa";
95  ImageFormat *formatInterfA=NULL, *formatInterfB=NULL,
96  *formatPhotomA=NULL, *formatPhotomB=NULL;
97  CompressedData *compressedInterfA=NULL, *compressedInterfB=NULL,
98  *compressedPhotomA=NULL, *compressedPhotomB=NULL;
99  TransferRatios *trr;
100  FilterData *filterInfo=NULL;
101 
102  // Algorithm
103  // ---------
104  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
105  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
106 
107  // Reset status
108  *error = 0;
109  if (options->processing == UNDISPERSED)
110  {
111  *error = 1;
112  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
113  "'UNDISPERSED' is an invalid processing request for SCI_PHOT data");
114  return;
115  }
116 
117  // Allocate memory
118  formatInterfA = callocImageFormat ();
119  formatInterfB = callocImageFormat ();
120  formatPhotomA = callocImageFormat ();
121  formatPhotomB = callocImageFormat ();
122 
123  // Analyse Input files
124  // -------------------
125  analyseFitsKappa (fileNames, formatInterfA, formatInterfB,
126  formatPhotomA, formatPhotomB, error);
127  if (*error)
128  {
129  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse FITS files");
130  freeImageFormat (formatInterfA);
131  freeImageFormat (formatInterfB);
132  freeImageFormat (formatPhotomA);
133  freeImageFormat (formatPhotomB);
134  return;
135  }
136 
137  // Reduce Data
138  // -----------
139  // Allocate memory for data compression
140  compressedInterfA = callocMidiCompressed (formatInterfA);
141  compressedInterfB = callocMidiCompressed (formatInterfB);
142  compressedPhotomA = callocMidiCompressed (formatPhotomA);
143  compressedPhotomB = callocMidiCompressed (formatPhotomB);
144  badChannelList = (int *) calloc (formatInterfA->iXWidth, sizeof (int));
145  filterInfo = callocFilterInfo();
146 
147  // Compress data
148  preProcKappa (options, filterInfo, fileNames, compressedInterfA, compressedInterfB,
149  compressedPhotomA, compressedPhotomB, formatInterfA, formatInterfB, formatPhotomA,
150  formatPhotomB, error);
151  if (*error)
152  {
153  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress FITS files");
154  freeCompressedData (formatInterfA, compressedInterfA);
155  freeCompressedData (formatInterfB, compressedInterfB);
156  freeCompressedData (formatPhotomA, compressedPhotomA);
157  freeCompressedData (formatPhotomB, compressedPhotomB);
158  free (badChannelList);
159  freeImageFormat (formatInterfA);
160  freeImageFormat (formatInterfB);
161  freeImageFormat (formatPhotomA);
162  freeImageFormat (formatPhotomB);
163  freeFilterInfo (filterInfo);
164  return;
165  }
166 
167  // Check Delay Lines for jumps within scans
168  checkDelayLineJumps ("INTERFA", formatInterfA, compressedInterfA);
169  checkDelayLineJumps ("INTERFB", formatInterfB, compressedInterfB);
170  checkDelayLineJumps ("PHOTOMA", formatPhotomA, compressedPhotomA);
171  checkDelayLineJumps ("PHOTOMB", formatPhotomB, compressedPhotomB);
172 
173  // Check Delay Line Consistencies
174  checkDelayLineConsistencies ("INTERFA", "PHOTOMA", compressedInterfA, compressedPhotomA,
175  formatInterfA, formatPhotomA, error);
176  checkDelayLineConsistencies ("INTERFB", "PHOTOMB", compressedInterfB, compressedPhotomB,
177  formatInterfB, formatPhotomB, error);
178  if (*error)
179  {
180  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Kappa calculation abandonned");
181  freeCompressedData (formatInterfA, compressedInterfA);
182  freeCompressedData (formatInterfB, compressedInterfB);
183  freeCompressedData (formatPhotomA, compressedPhotomA);
184  freeCompressedData (formatPhotomB, compressedPhotomB);
185  free (badChannelList);
186  freeImageFormat (formatInterfA);
187  freeImageFormat (formatInterfB);
188  freeImageFormat (formatPhotomA);
189  freeImageFormat (formatPhotomB);
190  freeFilterInfo (filterInfo);
191  return;
192  }
193 
194  // Check Chopping Statistics. For QCLOG diagnostics
195  // ------------------------------------------------
196  reportInterfChopping ("INTERFA", formatInterfA, compressedInterfA);
197  reportInterfChopping ("INTERFB", formatInterfB, compressedInterfB);
198  reportPhotomChopping ("PHOTOMA", formatPhotomA, compressedPhotomA);
199  reportPhotomChopping ("PHOTOMB", formatPhotomB, compressedPhotomB);
200  displayInterfChoppingDisp ("INTERFA", formatInterfA, compressedInterfA);
201  displayInterfChoppingDisp ("INTERFB", formatInterfB, compressedInterfB);
202  displayPhotomChoppingDisp ("PHOTOMA", formatPhotomA, compressedPhotomA);
203  displayPhotomChoppingDisp ("PHOTOMB", formatPhotomB, compressedPhotomB);
204 
205  // Remove Sky Background and display Target with Sky removed
206  cleanupKappa (options->processing, formatInterfA, formatInterfB, formatPhotomA, formatPhotomB,
207  compressedInterfA, compressedInterfB, compressedPhotomA, compressedPhotomB, error);
208  if (*error)
209  {
210  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Kappa calculation abandonned");
211  freeCompressedData (formatInterfA, compressedInterfA);
212  freeCompressedData (formatInterfB, compressedInterfB);
213  freeCompressedData (formatPhotomA, compressedPhotomA);
214  freeCompressedData (formatPhotomB, compressedPhotomB);
215  free (badChannelList);
216  freeImageFormat (formatInterfA);
217  freeImageFormat (formatInterfB);
218  freeImageFormat (formatPhotomA);
219  freeImageFormat (formatPhotomB);
220  freeFilterInfo (filterInfo);
221  return;
222  }
223 
224  // Compute the Transfer Ratios using dispersed data
225  trr = callocTransferRatios (formatInterfA->iXWidth);
226  midiComputeKappaCoeff (compressedInterfA, compressedInterfB, compressedPhotomA,
227  compressedPhotomB, formatInterfA, formatInterfB, formatPhotomA, formatPhotomB, trr, error);
228  if (*error > 1)
229  {
230  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Kappa calculation abandonned");
231  freeCompressedData (formatInterfA, compressedInterfA);
232  freeCompressedData (formatInterfB, compressedInterfB);
233  freeCompressedData (formatPhotomA, compressedPhotomA);
234  freeCompressedData (formatPhotomB, compressedPhotomB);
235  free (badChannelList);
236  freeImageFormat (formatInterfA);
237  freeImageFormat (formatInterfB);
238  freeImageFormat (formatPhotomA);
239  freeImageFormat (formatPhotomB);
240  freeTransferRatios (trr);
241  freeFilterInfo (filterInfo);
242  return;
243  }
244 
245  // Create product Transfer Ratios files
246  // ------------------------------------
247  reportRejectList (formatInterfA, compressedInterfA);
248 
249 /* At the moment the Kappa matrix calculation is done by midi_kappamatrix */
250 /* In order to avoid an empty dummy file MIDI_b1_spk.qc.dummy the next function */
251 /* is not executed any more */
252 
253 /* createKappaProd (options->drsKap, fileNames, formatInterfA, trr, error); */
254 
255  // Release memory used in this batch
256  freeCompressedData (formatInterfA, compressedInterfA);
257  freeCompressedData (formatInterfB, compressedInterfB);
258  freeCompressedData (formatPhotomA, compressedPhotomA);
259  freeCompressedData (formatPhotomB, compressedPhotomB);
260  free (badChannelList);
261  freeImageFormat (formatInterfA);
262  freeImageFormat (formatInterfB);
263  freeImageFormat (formatPhotomA);
264  freeImageFormat (formatPhotomB);
265  freeTransferRatios (trr);
266  freeFilterInfo (filterInfo);
267 
268  return;
269 }
270 /*****************************************************************************/
271 
272 
273 /******************************************************************************
274 * European Southern Observatory
275 * VLTI MIDI Data Reduction Software
276 *
277 * Module name: cleanupKappa
278 * Input/Output: See function arguments to avoid duplication
279 * Description: Removes sky etc from the data
280 *
281 * History:
282 * 06-Dec-05 (csabet) Created
283 ******************************************************************************/
284 void cleanupKappa (
285  enum ProcessingMode processing,
286  ImageFormat *formatInterfA, // In: Interf size parameters
287  ImageFormat *formatInterfB, // In: Interf size parameters
288  ImageFormat *formatPhotomA, // In: PhotA size parameters
289  ImageFormat *formatPhotomB, // In: PhotB size parameters
290  CompressedData *compressedInterfA, // Ou: Pointer to the compressed interferometry data structure
291  CompressedData *compressedInterfB, // Ou: Pointer to the compressed interferometry data structure
292  CompressedData *compressedPhotomA, // Ou: Pointer to the compressed photom A data structure
293  CompressedData *compressedPhotomB, // Ou: Pointer to the compressed photom B data structure
294  int *error) // Ou: Error status
295 {
296 
297  // Local Declarations
298  // ------------------
299  const char routine[] = "cleanupKappa";
300  int transitions, f, X, R, n, localError;
301  double *array, fluxAve;
302  float *arrayF;
303  char *fileName, *title;
304 
305  // Algorithm
306  // ---------
307  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
308  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
309 
310  cpl_msg_info(cpl_func,"\nCleaning up KAPPA files for batch %d \n", batchNumber);
311  cpl_msg_info(cpl_func,"--------------------------------- \n");
312  fprintf (midiReportPtr, "\nCleaning up KAPPA files for batch %d \n", batchNumber);
313  fprintf (midiReportPtr, "--------------------------------- \n");
314 
315  // Initialise
316  localError = 0;
317  *error = 0;
318  transitions = 0;
319 
320  // Check Interferometry A chopping cycle
321  // -------------------------------------
322  transitions = countTransitions ('T', formatInterfA->numOfFrames, compressedInterfA->tarType);
323  sprintf (midiMessage, "Target Transitions for Interf A = %d", transitions);
324  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
325  if (transitions < 10)
326  {
327  sprintf (midiMessage, "Interferometry A data is not chopped");
328  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
329  *error = 1;
330  }
331 
332  // Check Interferometry B chopping cycle
333  // -------------------------------------
334  transitions = countTransitions ('T', formatInterfB->numOfFrames, compressedInterfB->tarType);
335  sprintf (midiMessage, "Target Transitions for Interf B = %d", transitions);
336  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
337  if (transitions < 10)
338  {
339  sprintf (midiMessage, "Interferometry B data is not chopped");
340  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
341  *error = 1;
342  }
343 
344  // Check PhotomA chopping cycle
345  // ----------------------------
346  transitions = countTransitions ('T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
347  sprintf (midiMessage, "Target Transitions for Photom A = %d", transitions);
348  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
349  if (transitions < 10)
350  {
351  sprintf (midiMessage, "Photometry A data is not chopped");
352  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
353  *error = 1;
354  }
355 
356  // Check PhotomB chopping cycle
357  // ----------------------------
358  transitions = countTransitions ('T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
359  sprintf (midiMessage, "Target Transitions for Photom B = %d", transitions);
360  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
361  if (transitions < 10)
362  {
363  sprintf (midiMessage, "Photometry B data is not chopped");
364  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
365  *error = 1;
366  }
367 
368  // if No error so far
369  if (!(*error))
370  {
371  formatInterfA->chopped = 1;
372  formatInterfB->chopped = 1;
373  formatPhotomA->chopped = 1;
374  formatPhotomB->chopped = 1;
375  }
376  else
377  {
378  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot cleanup KAPPA files");
379  return;
380  }
381 
382  // Correct benign tarType inconsistencies
383  // --------------------------------------
384  correctTarType ("INTERFA", compressedInterfA->tarType, compressedInterfA->time, formatInterfA->numOfFrames, &localError);
385  if (localError) *error = 1;
386  correctTarType ("INTERFB", compressedInterfB->tarType, compressedInterfB->time, formatInterfB->numOfFrames, &localError);
387  if (localError) *error = 1;
388  correctTarType ("PHOTOMA", compressedPhotomA->tarType, compressedPhotomA->time, formatPhotomA->numOfFrames, &localError);
389  if (localError) *error = 1;
390  correctTarType ("PHOTOMB", compressedPhotomB->tarType, compressedPhotomB->time, formatPhotomB->numOfFrames, &localError);
391  if (*error || localError)
392  {
393  *error = 1;
394  return;
395  }
396 
397  // Remove sky background
398  // ---------------------
399  removeSkyBackground ("INTERFA", processing, formatInterfA, compressedInterfA, &localError);
400  if (localError) *error = 1;
401  removeSkyBackground ("INTERFB", processing, formatInterfB, compressedInterfB, &localError);
402  if (localError) *error = 1;
403  removeSkyBackground ("PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
404  if (localError) *error = 1;
405  removeSkyBackground ("PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
406  if (formatInterfA->numOfFrames != formatPhotomA->numOfFrames ||
407  formatInterfA->numOfFrames != formatPhotomB->numOfFrames ||
408  formatInterfA->numOfFrames != formatInterfB->numOfFrames || *error || localError)
409  {
410  *error = 1;
411  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unequal numOfFrames");
412  return;
413  }
414 
415  // Reject frames whose flux are <= 0 or are not 'T'
416  for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
417  {
418  for (X = 0; X < formatInterfA->iXWidth; X++)
419  {
420  if (badChannelList[X])
421  continue;
422 
423  for (f = 0; f < formatInterfA->numOfFrames; f++)
424  {
425  if ((compressedInterfA->tarType[f] == 'T') &&
426  (((compressedInterfA->iDispFringe)[R])[X])[f] <= 0.0)
427  {
428  compressedInterfA->rejectList[X][f] |= BSL_DATA_ERROR;
429  if (diagnostic > 3)
430  {
431  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d InterfA %10.2f",
432  R, X, f, (((compressedInterfA->iDispFringe)[R])[X])[f]);
433  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
434  }
435  }
436  // Reject frames which are not 'T'
437  if (compressedInterfA->tarType[f] != 'T')
438  compressedInterfA->rejectList[X][f] |= BSL_SKY_SCAN;
439  }
440  }
441  }
442 
443  for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
444  {
445  for (X = 0; X < formatInterfB->iXWidth; X++)
446  {
447  if (badChannelList[X])
448  continue;
449 
450  for (f = 0; f < formatInterfB->numOfFrames; f++)
451  {
452  if ((compressedInterfB->tarType[f] == 'T') &&
453  (((compressedInterfB->iDispFringe)[R])[X])[f] <= 0.0)
454  {
455  compressedInterfB->rejectList[X][f] |= BSL_DATA_ERROR;
456  if (diagnostic > 3)
457  {
458  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d InterfB %10.2f",
459  R, X, f, (((compressedInterfB->iDispFringe)[R])[X])[f]);
460  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
461  }
462  }
463  if (compressedInterfB->tarType[f] != 'T')
464  compressedInterfB->rejectList[X][f] |= BSL_SKY_SCAN;
465  }
466  }
467  }
468 
469  for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
470  {
471  for (X = 0; X < formatPhotomA->iXWidth; X++)
472  {
473  if (badChannelList[X])
474  continue;
475 
476  for (f = 0; f < formatPhotomA->numOfFrames; f++)
477  {
478  if ((compressedPhotomA->tarType[f] == 'T') &&
479  (((compressedPhotomA->iDispFringe)[R])[X])[f] <= 0.0)
480  {
481  compressedPhotomA->rejectList[X][f] |= BSL_DATA_ERROR;
482  if (diagnostic > 3)
483  {
484  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d PhotomA %10.2f",
485  R, X, f, (((compressedPhotomA->iDispFringe)[R])[X])[f]);
486  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
487  }
488  }
489  if (compressedPhotomA->tarType[f] != 'T')
490  compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
491  }
492  }
493  }
494 
495  for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
496  {
497  for (X = 0; X < formatPhotomB->iXWidth; X++)
498  {
499  if (badChannelList[X])
500  continue;
501 
502  for (f = 0; f < formatPhotomB->numOfFrames; f++)
503  {
504  if ((compressedPhotomB->tarType[f] == 'T') &&
505  (((compressedPhotomB->iDispFringe)[R])[X])[f] <= 0.0)
506  {
507  compressedPhotomB->rejectList[X][f] |= BSL_DATA_ERROR;
508  if (diagnostic > 3)
509  {
510  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d PhotomB %10.2f",
511  R, X, f, (((compressedPhotomB->iDispFringe)[R])[X])[f]);
512  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
513  }
514  }
515  if (compressedPhotomB->tarType[f] != 'T')
516  compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
517  }
518  }
519  }
520 
521  // Display data with background removed
522  if (plotFile && diagnostic > 1)
523  {
524  array = (double *) calloc (formatInterfA->numOfFrames, sizeof (double));
525 
526  fluxAve = 0.0;
527  n = 0;
528  for (f = 0; f < formatInterfA->numOfFrames; f++)
529  {
530  if (compressedInterfA->tarType[f] == 'T')
531  {
532  array[n] = compressedInterfA->iFringe1[f];
533  fluxAve += array[n];
534  n++;
535  }
536  }
537  fluxAve /= n;
538  cpl_msg_info(cpl_func,"InterfA DATA2 Average Flux = %f \n", fluxAve);
539  midiCreatePlotFileDouble2D ("InterfACleanDATA2", "Interferometry A DATA2 (Sky Removed)",
540  "Frame", "Flux", 0, array, 0, n, 1);
541 
542  fluxAve = 0.0;
543  n = 0;
544  for (f = 0; f < formatInterfA->numOfFrames; f++)
545  {
546  if (compressedInterfA->tarType[f] == 'T')
547  {
548  array[n] = compressedInterfA->iFringe2[f];
549  fluxAve += array[n];
550  n++;
551  }
552  }
553  fluxAve /= n;
554  cpl_msg_info(cpl_func,"InterfA DATA3 Average Flux = %f \n", fluxAve);
555  midiCreatePlotFileDouble2D ("InterfACleanDATA3", "Interferometry A DATA3 (Sky Removed)",
556  "Frame", "Flux", 0, array, 0, n, 1);
557 
558  n = 0;
559  for (f = 0; f < formatInterfA->numOfFrames; f++)
560  {
561  if (compressedInterfA->tarType[f] == 'T')
562  {
563  array[n] = compressedInterfA->iFringe[f];
564  n++;
565  }
566  }
567  midiCreatePlotFileDouble2D ("InterfACleanDATA2_DATA3", "Interferometry A DATA2-DATA3 (Sky Removed)",
568  "Frame", "Flux", 0, array, 0, n, 1);
569 
570  fluxAve = 0.0;
571  n = 0;
572  for (f = 0; f < formatInterfB->numOfFrames; f++)
573  {
574  if (compressedInterfB->tarType[f] == 'T')
575  {
576  array[n] = compressedInterfB->iFringe1[f];
577  fluxAve += array[n];
578  n++;
579  }
580  }
581  fluxAve /= n;
582  cpl_msg_info(cpl_func,"InterfB DATA2 Average Flux = %f \n", fluxAve);
583  midiCreatePlotFileDouble2D ("InterfBCleanDATA2", "Interferometry B DATA2 (Sky Removed)",
584  "Frame", "Flux", 0, array, 0, n, 1);
585 
586  fluxAve = 0.0;
587  n = 0;
588  for (f = 0; f < formatInterfB->numOfFrames; f++)
589  {
590  if (compressedInterfB->tarType[f] == 'T')
591  {
592  array[n] = compressedInterfB->iFringe2[f];
593  fluxAve += array[n];
594  n++;
595  }
596  }
597  fluxAve /= n;
598  cpl_msg_info(cpl_func,"InterfB DATA3 Average Flux = %f \n", fluxAve);
599  midiCreatePlotFileDouble2D ("InterfBCleanDATA3", "Interferometry B DATA3 (Sky Removed)",
600  "Frame", "Flux", 0, array, 0, n, 1);
601 
602  n = 0;
603  for (f = 0; f < formatInterfB->numOfFrames; f++)
604  {
605  if (compressedInterfB->tarType[f] == 'T')
606  {
607  array[n] = compressedInterfB->iFringe[f];
608  n++;
609  }
610  }
611  midiCreatePlotFileDouble2D ("InterfBCleanDATA2_DATA3", "Interferometry B DATA2-DATA3 (Sky Removed)",
612  "Frame", "Flux", 0, array, 0, n, 1);
613 
614  fluxAve = 0.0;
615  n = 0;
616  for (f = 0; f < formatPhotomA->numOfFrames; f++)
617  {
618  if (compressedPhotomA->tarType[f] == 'T')
619  {
620  array[n] = compressedPhotomA->iFringe1[f];
621  fluxAve += array[n];
622  n++;
623  }
624  }
625  fluxAve /= n;
626  cpl_msg_info(cpl_func,"PhotomA DATA1 Average Flux = %f \n", fluxAve);
627  midiCreatePlotFileDouble2D ("PhotomACleanDATA1", "Photometry A DATA1 (Sky Removed)",
628  "Frame", "Flux", 0, array, 0, n, 1);
629 
630  fluxAve = 0.0;
631  n = 0;
632  for (f = 0; f < formatPhotomB->numOfFrames; f++)
633  {
634  if (compressedPhotomB->tarType[f] == 'T')
635  {
636  array[n] = compressedPhotomB->iFringe1[f];
637  fluxAve += array[n];
638  n++;
639  }
640  }
641  fluxAve /= n;
642  cpl_msg_info(cpl_func,"PhotomB DATA4 Average Flux = %f \n", fluxAve);
643  midiCreatePlotFileDouble2D ("PhotomBCleanDATA4", "Photometry B DATA4 (Sky Removed)",
644  "Frame", "Flux", 0, array, 0, n, 1);
645 
646  free (array);
647  }
648 
649  // Display for each channel
650  if (plotFile && diagnostic > 4)
651  {
652  arrayF = (float *) calloc (formatInterfA->numOfFrames, sizeof (float));
653  fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
654  title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
655 
656  for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
657  {
658  for (X = 0; X < formatInterfA->iXWidth; X++)
659  {
660  if (badChannelList[X])
661  continue;
662 
663  n = 0;
664  for (f = 0; f < formatInterfA->numOfFrames; f++)
665  {
666  if (compressedInterfA->tarType[f] == 'T' && !(compressedInterfA->rejectList[X][f]))
667  {
668  arrayF[n] = compressedInterfA->iDispFringe[R][X][f];
669  n++;
670  }
671  }
672  sprintf (fileName, "InterfACleanDATA%d_X%d", R+2, X+1);
673  sprintf (title, "Interferometry A DATA%d for Channel %d (Sky Removed)", R+2, X+1);
674  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
675  }
676  }
677  for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
678  {
679  for (X = 0; X < formatInterfB->iXWidth; X++)
680  {
681  if (badChannelList[X])
682  continue;
683 
684  n = 0;
685  for (f = 0; f < formatInterfB->numOfFrames; f++)
686  {
687  if (compressedInterfB->tarType[f] == 'T' && !(compressedInterfB->rejectList[X][f]))
688  {
689  arrayF[n] = compressedInterfB->iDispFringe[R][X][f];
690  n++;
691  }
692  }
693  sprintf (fileName, "InterfBCleanDATA%d_X%d", R+2, X+1);
694  sprintf (title, "Interferometry B DATA%d for Channel %d (Sky Removed)", R+2, X+1);
695  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
696  }
697  }
698  for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
699  {
700  for (X = 0; X < formatPhotomA->iXWidth; X++)
701  {
702  if (badChannelList[X])
703  continue;
704 
705  n = 0;
706  for (f = 0; f < formatPhotomA->numOfFrames; f++)
707  {
708  if (compressedPhotomA->tarType[f] == 'T' && !(compressedPhotomA->rejectList[X][f]))
709  {
710  arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
711  n++;
712  }
713  }
714  sprintf (fileName, "PhotomACleanDATA1_X%d", X+1);
715  sprintf (title, "Photometry A DATA1 for Channel %d (Sky Removed)", X+1);
716  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
717  }
718  }
719  for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
720  {
721  for (X = 0; X < formatPhotomB->iXWidth; X++)
722  {
723  if (badChannelList[X])
724  continue;
725 
726  n = 0;
727  for (f = 0; f < formatPhotomB->numOfFrames; f++)
728  {
729  if (compressedPhotomB->tarType[f] == 'T' && !(compressedPhotomB->rejectList[X][f]))
730  {
731  arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
732  n++;
733  }
734  }
735  sprintf (fileName, "PhotomBCleanDATA4_X%d", X+1);
736  sprintf (title, "Photometry B DATA4 for Channel %d (Sky Removed)", X+1);
737  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
738  }
739  }
740  free (fileName);
741  free (title);
742  free (arrayF);
743  }
744 
745  // Finalise rejection list
746  finaliseRejectListPhotom ("Interferometry A", formatInterfA, compressedInterfA, error);
747  if (*error) return;
748  finaliseRejectListPhotom ("Interferometry B", formatInterfB, compressedInterfB, error);
749  if (*error) return;
750  finaliseRejectListPhotom ("Photometry A", formatPhotomA, compressedPhotomA, error);
751  if (*error) return;
752  finaliseRejectListPhotom ("Photometry B", formatPhotomB, compressedPhotomB, error);
753  if (*error) return;
754 
755  return;
756 }
757 /*****************************************************************************/
758 
759 
760 /******************************************************************************
761 * European Southern Observatory
762 * VLTI MIDI Data Reduction Software
763 *
764 * Module name: midiComputeKappaCoeff
765 * Input/Output: See function arguments to avoid duplication
766 * Description: Computes the transfer matrix. The FITS files have the following
767 * configuration:
768 *
769 * Photometry A files (AOPEN) have the following configurations:
770 * DATA1 = Photometry A PA data
771 * DATA2 = Interferometry IA1 data
772 * DATA3 = Interferometry IA2 data
773 * DATA4 = NULL
774 *
775 * Photometry B files (BOPEN) have the following configurations:
776 * DATA1 = NULL
777 * DATA2 = Interferometry IB1 data
778 * DATA3 = Interferometry IB2 data
779 * DATA4 = Photometry B PB data
780 *
781 * In general we have:
782 * PA = k11 * TFA + k12 * TFB
783 * I1 = k21 * TFA + k22 * TFB
784 * I2 = k31 * TFA + k32 * TFB
785 * PB = k41 * TFA + k42 * TFB
786 *
787 * Where
788 * TFA and TFB, the total flux through the telescopes are given by:
789 * TFA = PA + IA1 + IA2
790 * TFB = PB + IB1 + IB2
791 *
792 * When AOPEN then B = 0. Hence
793 * k11 = PA / TFA
794 * k12 = 0
795 * k21 = IA1 / TFA
796 * k31 = IA2 / TFA
797 *
798 * When BOPEN then TFA = 0. Hence
799 * k22 = IB1 / TFB
800 * k32 = IB2 / TFB
801 * k41 = 0
802 * k42 = PB / TFB
803 *
804 * And the ratios are:
805 * krA1 = k21/k11
806 * krA2 = k31/k11
807 * krB1 = k22/k42
808 * krB2 = k32/k42
809 *
810 * History:
811 * 06-Oct-05 (csabet) Created
812 ******************************************************************************/
813 void midiComputeKappaCoeff (
814  CompressedData *compressedIA, // In: Compressed interferometry data
815  CompressedData *compressedIB, // In: Compressed interferometry data
816  CompressedData *compressedPA, // In: Photometry data
817  CompressedData *compressedPB, // In: Photometry data
818  ImageFormat *formatIA, // In: Image size of interferometry data
819  ImageFormat *formatIB, // In: Image size of interferometry data
820  ImageFormat *formatPA, // In: Applies to A photom files
821  ImageFormat *formatPB, // In: Applies to B photom files
822  TransferRatios *trr, // Ou: Transfer Ratios
823  int *error) // Ou: Status
824 {
825 
826  // Local Declarations
827  // ------------------
828  const char routine[] = "midiComputeKappaCoeff";
829  int f, x, numOfFrames, nzc, minFrames;
830  float *kappArray, *totalFluxA, *totalFluxB, variance;
831  KappaCoefficients *kc;
832  char *string;
833 
834  // Algorithm
835  // ---------
836  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
837  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
838 
839  cpl_msg_info(cpl_func,"\nComputing the Kappa Coefficients for batch %d \n", batchNumber);
840  cpl_msg_info(cpl_func,"------------------------------------------ \n");
841  fprintf (midiReportPtr, "\nComputing the Kappa Coefficients for batch %d \n", batchNumber);
842  fprintf (midiReportPtr, "------------------------------------------ \n");
843 
844  // Reset status
845  *error = 0;
846 
847  // Exception
848  if ((formatIA->numOfFrames != formatIB->numOfFrames) ||
849  (formatIA->numOfFrames != formatPA->numOfFrames) ||
850  (formatIA->numOfFrames != formatPB->numOfFrames) ||
851  (formatPA->numOfFrames != formatPB->numOfFrames))
852  {
853  *error = 2;
854  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent frame counts");
855  return;
856  }
857  numOfFrames = formatIA->numOfFrames;
858  minFrames = MIN_FRAMES_PER_SCAN * compressedIA->numOfTargetChops;
859 
860 
861  // Allocate memory
862  kappArray = (float *) calloc (numOfFrames, sizeof (float));
863  totalFluxA = (float *) calloc (numOfFrames, sizeof (float));
864  totalFluxB = (float *) calloc (numOfFrames, sizeof (float));
865  string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
866  kc = callocKappaCoefficients (formatIA->iXWidth);
867 
868  // Compute kappa coefficients for each wavelength
869  // ----------------------------------------------
870  cpl_msg_info(cpl_func,"Channel k11 sig11 k21 sig21 k31 sig31 k22 sig22 k32 sig32 k42 sig42 \n");
871  fprintf (midiReportPtr, "Channel k11 sig11 k21 sig21 k31 sig31 k22 sig22 k32 sig32 k42 sig42 \n");
872  for (x = 0; x < formatIA->iXWidth; x++)
873  {
874  if (badChannelList[x])
875  {
876  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
877  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
878  continue;
879  }
880 
881  // Compute total flux through each telescope
882  for (f = 0; f < numOfFrames; f++)
883  {
884  if ((compressedIA->tarType[f] == 'T') &&
885  (compressedPA->tarType[f] == 'T') &&
886  !(compressedIA->rejectList[x][f]) &&
887  !(compressedPA->rejectList[x][f]))
888  {
889  totalFluxA[f] = compressedIA->iDispFringe[0][x][f] + compressedIA->iDispFringe[1][x][f] +
890  compressedPA->iDispFringe[0][x][f];
891 
892  }
893  if ((compressedIB->tarType[f] == 'T') &&
894  (compressedPB->tarType[f] == 'T') &&
895  !(compressedIB->rejectList[x][f]) &&
896  !(compressedPB->rejectList[x][f]))
897  {
898  totalFluxB[f] = compressedIB->iDispFringe[0][x][f] + compressedIB->iDispFringe[1][x][f] +
899  compressedPB->iDispFringe[0][x][f];
900  }
901  }
902 
903  // kappa11 computation
904  nzc = -1;
905  for (f = 0; f < numOfFrames; f++)
906  {
907  if ((compressedPA->tarType[f] == 'T') &&
908  (((compressedPA->iDispFringe[0])[x])[f] > 0.0) &&
909  (totalFluxA[f] > 0.0) &&
910  !(compressedPA->rejectList[x][f]))
911  {
912  nzc++;
913  kappArray[nzc] = ((compressedPA->iDispFringe[0])[x])[f] / totalFluxA[f];
914  }
915  }
916  if (nzc > minFrames)
917  {
918  kc->k11[x] = signalMean (kappArray, 0, nzc);
919  variance = signalVariance (kappArray, 0, nzc, &(kc->sig11[x]));
920  if (plotFile && diagnostic > 2)
921  {
922  sprintf (string, "Kappa11 Array, Channel %d", x);
923  midiCreatePlotFile2D ("Kappa11Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
924  }
925  }
926  else
927  {
928  badChannelList[x] |= BSL_DATA_ERROR;
929  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
930  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
931  continue;
932  }
933 
934  // kappa21 computation
935  nzc = -1;
936  for (f = 0; f < numOfFrames; f++)
937  {
938  if ((compressedIA->tarType[f] == 'T') &&
939  (((compressedIA->iDispFringe[0])[x])[f] > 0.0) &&
940  (totalFluxA[f] > 0.0) &&
941  !(compressedIA->rejectList[x][f]))
942  {
943  nzc++;
944  kappArray[nzc] = ((compressedIA->iDispFringe[0])[x])[f] / totalFluxA[f];
945  }
946  }
947  if (nzc > minFrames)
948  {
949  kc->k21[x] = signalMean (kappArray, 0, nzc);
950  variance = signalVariance (kappArray, 0, nzc, &(kc->sig21[x]));
951  if (plotFile && diagnostic > 2)
952  {
953  sprintf (string, "Kappa21 Array, Channel %d", x);
954  midiCreatePlotFile2D ("Kappa21Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
955  }
956  }
957  else
958  {
959  badChannelList[x] |= BSL_DATA_ERROR;
960  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
961  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
962  continue;
963  }
964 
965  // kappa31 computation
966  nzc = -1;
967  for (f = 0; f < numOfFrames; f++)
968  {
969  if ((compressedIA->tarType[f] == 'T') &&
970  (((compressedIA->iDispFringe[1])[x])[f] > 0.0) &&
971  (totalFluxA[f] > 0.0) &&
972  !(compressedIA->rejectList[x][f]))
973  {
974  nzc++;
975  kappArray[nzc] = ((compressedIA->iDispFringe[1])[x])[f] / totalFluxA[f];
976  }
977  }
978  if (nzc > minFrames)
979  {
980  kc->k31[x] = signalMean (kappArray, 0, nzc);
981  variance = signalVariance (kappArray, 0, nzc, &(kc->sig31[x]));
982  if (plotFile && diagnostic > 2)
983  {
984  sprintf (string, "Kappa31 Array, Channel %d", x);
985  midiCreatePlotFile2D ("Kappa31Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
986  }
987  }
988  else
989  {
990  badChannelList[x] |= BSL_DATA_ERROR;
991  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
992  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
993  continue;
994  }
995 
996  // kappa22 computation
997  nzc = -1;
998  for (f = 0; f < numOfFrames; f++)
999  {
1000  if ((compressedIB->tarType[f] == 'T') &&
1001  (((compressedIB->iDispFringe[0])[x])[f] > 0.0) &&
1002  (totalFluxB[f] > 0.0) &&
1003  !(compressedIB->rejectList[x][f]))
1004  {
1005  nzc++;
1006  kappArray[nzc] = ((compressedIB->iDispFringe[0])[x])[f] / totalFluxB[f];
1007  }
1008  }
1009  if (nzc > minFrames)
1010  {
1011  kc->k22[x] = signalMean (kappArray, 0, nzc);
1012  variance = signalVariance (kappArray, 0, nzc, &(kc->sig22[x]));
1013  if (plotFile && diagnostic > 2)
1014  {
1015  sprintf (string, "Kappa22 Array, Channel %d", x);
1016  midiCreatePlotFile2D ("Kappa22Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
1017  }
1018  }
1019  else
1020  {
1021  badChannelList[x] |= BSL_DATA_ERROR;
1022  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
1023  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
1024  continue;
1025  }
1026 
1027  // kappa32 computation
1028  nzc = -1;
1029  for (f = 0; f < numOfFrames; f++)
1030  {
1031  if ((compressedIB->tarType[f] == 'T') &&
1032  (((compressedIB->iDispFringe[1])[x])[f] > 0.0) &&
1033  (totalFluxB[f] > 0.0) &&
1034  !(compressedIB->rejectList[x][f]))
1035  {
1036  nzc++;
1037  kappArray[nzc] = ((compressedIB->iDispFringe[1])[x])[f] / totalFluxB[f];
1038  }
1039  }
1040  if (nzc > minFrames)
1041  {
1042  kc->k32[x] = signalMean (kappArray, 0, nzc);
1043  variance = signalVariance (kappArray, 0, nzc, &(kc->sig32[x]));
1044  if (plotFile && diagnostic > 2)
1045  {
1046  sprintf (string, "Kappa32 Array, Channel %d", x);
1047  midiCreatePlotFile2D ("Kappa32Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
1048  }
1049  }
1050  else
1051  {
1052  badChannelList[x] |= BSL_DATA_ERROR;
1053  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
1054  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
1055  continue;
1056  }
1057 
1058  // kappa42 computation
1059  nzc = -1;
1060  for (f = 0; f < numOfFrames; f++)
1061  {
1062  if ((compressedPB->tarType[f] == 'T') &&
1063  (((compressedPB->iDispFringe[0])[x])[f] > 0.0) &&
1064  (totalFluxB[f] > 0.0) &&
1065  !(compressedPB->rejectList[x][f]))
1066  {
1067  nzc++;
1068  kappArray[nzc] = ((compressedPB->iDispFringe[0])[x])[f] / totalFluxB[f];
1069  }
1070  }
1071  if (nzc > minFrames)
1072  {
1073  kc->k42[x] = signalMean (kappArray, 0, nzc);
1074  variance = signalVariance (kappArray, 0, nzc, &(kc->sig42[x]));
1075  if (plotFile && diagnostic > 2)
1076  {
1077  sprintf (string, "Kappa42 Array, Channel %d", x);
1078  midiCreatePlotFile2D ("Kappa42Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
1079  }
1080  }
1081  else
1082  {
1083  badChannelList[x] |= BSL_DATA_ERROR;
1084  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
1085  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
1086  continue;
1087  }
1088  cpl_msg_info(cpl_func,"%3d %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f \n",
1089  x, kc->k11[x], kc->sig11[x], kc->k21[x], kc->sig21[x], kc->k31[x], kc->sig31[x], kc->k22[x], kc->sig22[x],
1090  kc->k32[x], kc->sig32[x], kc->k42[x], kc->sig42[x]);
1091  fprintf (midiReportPtr, "%3d %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f \n",
1092  x, kc->k11[x], kc->sig11[x], kc->k21[x], kc->sig21[x], kc->k31[x], kc->sig31[x], kc->k22[x], kc->sig22[x],
1093  kc->k32[x], kc->sig32[x], kc->k42[x], kc->sig42[x]);
1094  }
1095 
1096  // Smooth the coefficients. Not sure if this is the right moment for filtering. A
1097  // more suitable place would probably be filtering "kappArray[nzc]" at frame ensemble level.
1098  smoothKappaCoefficiets (formatIA, kc, error);
1099  if (*error)
1100  {
1101  freeKappaCoefficients (kc);
1102  free (kappArray);
1103  free (totalFluxA);
1104  free (totalFluxB);
1105  free (string);
1106  return;
1107  }
1108 
1109  // Now compute the ratios
1110  computeTransferRatios (formatIA, kc, trr, error);
1111 
1112  // Release memory
1113  freeKappaCoefficients (kc);
1114  free (kappArray);
1115  free (totalFluxA);
1116  free (totalFluxB);
1117  free (string);
1118 
1119  return;
1120 }
1121 /*****************************************************************************/
1122 
1123 
1124 
1125 /******************************************************************************
1126 * European Southern Observatory
1127 * VLTI MIDI Data Reduction Software
1128 *
1129 * Module name: computeTransferRatios
1130 * Input/Output: See function arguments to avoid duplication
1131 * Description: Computes the transfer ratios and the associated errors as follows:
1132 *
1133 * krA1 = k21/k11
1134 * krA2 = k31/k11
1135 * krB1 = k22/k42
1136 * krB2 = k32/k42
1137 *
1138 * History:
1139 * 02-Feb-06 (csabet) Created
1140 ******************************************************************************/
1141 void computeTransferRatios (
1142  ImageFormat *format, // In: Image size of interferometry data
1143  KappaCoefficients *kc, // In: Kappa Coefficients
1144  TransferRatios *tr, // Ou: Transfer Ratios
1145  int *error) // Ou: Status
1146 {
1147 
1148  // Local Declarations
1149  // ------------------
1150  const char routine[] = "computeTransferRatios";
1151  int x, n;
1152 
1153  // Algorithm
1154  // ---------
1155  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1156  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
1157 
1158  // Reset
1159  *error = 0;
1160  tr->exists = 0;
1161 
1162  // Check sanity. Kappa coefficients must lie between 0.0 and 1.0 exclusively
1163  cpl_msg_info(cpl_func,"Channel ka1 siga1 ka2 siga2 kb1 sigb1 kb2 sigb2 \n");
1164  fprintf (midiReportPtr, "Channel ka1 siga1 ka2 siga2 kb1 sigb1 kb2 sigb2 \n");
1165  n = 0;
1166  for (x = 0; x < format->iXWidth; x++)
1167  {
1168  if (badChannelList[x])
1169  {
1170  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
1171  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
1172  continue;
1173  }
1174 
1175  if (((kc->k11[x] <= 0.0) || (kc->k11[x] >= 1.0)) ||
1176  ((kc->k21[x] <= 0.0) || (kc->k21[x] >= 1.0)) ||
1177  ((kc->k31[x] <= 0.0) || (kc->k31[x] >= 1.0)) ||
1178  ((kc->k22[x] <= 0.0) || (kc->k22[x] >= 1.0)) ||
1179  ((kc->k32[x] <= 0.0) || (kc->k32[x] >= 1.0)) ||
1180  ((kc->k42[x] <= 0.0) || (kc->k42[x] >= 1.0)))
1181  {
1182  badChannelList[x] |= BSL_DATA_ERROR;
1183  if (diagnostic > 1)
1184  {
1185  sprintf (midiMessage,
1186  "Cannot compute Transfer Ratios for Channel %3d. Values of Kappa Matrix are unreasonable", x);
1187  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1188  }
1189  cpl_msg_info(cpl_func,"%3d <-- %s --> \n", x, UNAV);
1190  fprintf (midiReportPtr, "%3d <-- %s --> \n", x, UNAV);
1191  }
1192  else
1193  {
1194  n++;
1195  tr->ka1[x] = kc->k21[x] / kc->k11[x];
1196  tr->siga1[x] = kc->sig21[x] / fabs(kc->k11[x]) + kc->sig11[x] * (fabs(kc->k21[x]) / (kc->k11[x] * kc->k11[x]));
1197  tr->ka2[x] = kc->k31[x] / kc->k11[x];
1198  tr->siga2[x] = kc->sig31[x] / fabs(kc->k11[x]) + kc->sig11[x] * (fabs(kc->k31[x]) / (kc->k11[x] * kc->k11[x]));
1199  tr->kb1[x] = kc->k22[x] / kc->k42[x];
1200  tr->sigb1[x] = kc->sig22[x] / fabs(kc->k42[x]) + kc->sig42[x] * (fabs(kc->k22[x]) / (kc->k42[x] * kc->k42[x]));
1201  tr->kb2[x] = kc->k32[x] / kc->k42[x];
1202  tr->sigb2[x] = kc->sig32[x] / fabs(kc->k42[x]) + kc->sig42[x] * (fabs(kc->k32[x]) / (kc->k42[x] * kc->k42[x]));
1203 
1204  cpl_msg_info(cpl_func,"%3d %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f \n",
1205  x, tr->ka1[x], tr->siga1[x], tr->ka2[x], tr->siga2[x], tr->kb1[x], tr->sigb1[x], tr->kb2[x], tr->sigb2[x]);
1206  fprintf (midiReportPtr, "%3d %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f \n",
1207  x, tr->ka1[x], tr->siga1[x], tr->ka2[x], tr->siga2[x], tr->kb1[x], tr->sigb1[x], tr->kb2[x], tr->sigb2[x]);
1208  }
1209  }
1210 
1211  if (!n)
1212  {
1213  tr->exists = 0;
1214  *error = 1;
1215  return;
1216  }
1217 
1218  if (plotFile && diagnostic > 1)
1219  {
1220  midiCreatePlotFile2D ("Ka1Ratio", "Ka1 Ratio", "Channel", "Flux", 1, tr->ka1, 0, format->iXWidth, 1, 0);
1221  midiCreatePlotFile2D ("Ka1Sig", "Ka1 Error", "Channel", "Flux", 1, tr->siga1, 0, format->iXWidth, 1, 0);
1222 
1223  midiCreatePlotFile2D ("Ka2Ratio", "Ka2 Ratio", "Channel", "Flux", 1, tr->ka2, 0, format->iXWidth, 1, 0);
1224  midiCreatePlotFile2D ("Ka2Sig", "Ka2 Error", "Channel", "Flux", 1, tr->siga2, 0, format->iXWidth, 1, 0);
1225 
1226  midiCreatePlotFile2D ("Kb1Ratio", "Kb1 Ratio", "Channel", "Flux", 1, tr->kb1, 0, format->iXWidth, 1, 0);
1227  midiCreatePlotFile2D ("Kb1Sig", "Kb1 Error", "Channel", "Flux", 1, tr->sigb1, 0, format->iXWidth, 1, 0);
1228 
1229  midiCreatePlotFile2D ("Kb2Ratio", "Kb2 Ratio", "Channel", "Flux", 1, tr->kb2, 0, format->iXWidth, 1, 0);
1230  midiCreatePlotFile2D ("Kb2Sig", "Kb2 Error", "Channel", "Flux", 1, tr->sigb2, 0, format->iXWidth, 1, 0);
1231  }
1232 
1233  tr->exists = 1;
1234 
1235  return;
1236 }
1237 /*****************************************************************************/
1238 
1239 
1240 /******************************************************************************
1241 * European Southern Observatory
1242 * VLTI MIDI Data Reduction Software
1243 *
1244 * Module name: smoothKappaCoefficiets
1245 * Input/Output: See function arguments to avoid duplication
1246 * Description: Not sure if this routine is required. A better method would
1247 * probably be to filter the kappa elements at frame enseble stage.
1248 * Then compute the associated errors and propagate the errors to
1249 * the ratios.
1250 *
1251 * History:
1252 * 07-Mar-06 (csabet) Created
1253 ******************************************************************************/
1254 void smoothKappaCoefficiets (
1255  ImageFormat *format, // In: Image size of interferometry data
1256  KappaCoefficients *kc, // In: Kappa Coefficients
1257  int *error)
1258 {
1259 
1260  // Local Declarations
1261  // ------------------
1262  const char routine[] = "smoothKappaCoefficiets";
1263  int x, n;
1264 
1265  // Algorithm
1266  // ---------
1267  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1268  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
1269 
1270  // Reset
1271  *error = 0;
1272 
1273  // First reject noisy values
1274  n = 0;
1275  for (x = 0; x < format->iXWidth; x++)
1276  {
1277  if (!(badChannelList[x]))
1278  {
1279  if ((kc->sig11[x] > KAPPA_NOISE_LIMIT * kc->k11[x]) || (kc->sig21[x] > KAPPA_NOISE_LIMIT * kc->k21[x]) ||
1280  (kc->sig31[x] > KAPPA_NOISE_LIMIT * kc->k31[x]) || (kc->sig22[x] > KAPPA_NOISE_LIMIT * kc->k22[x]) ||
1281  (kc->sig32[x] > KAPPA_NOISE_LIMIT * kc->k32[x]) || (kc->sig42[x] > KAPPA_NOISE_LIMIT * kc->k42[x]))
1282  badChannelList[x] |= BSL_DATA_ERROR;
1283  else
1284  n++;
1285  }
1286  }
1287 
1288  // Plot
1289  if (plotFile && diagnostic > 1 && n)
1290  {
1291  midiCreatePlotFile2D ("Kappa11VersX", "k11 Versus X", "X", "k11", 1, kc->k11, 0, format->iXWidth, 1, 0);
1292  midiCreatePlotFile2D ("Kappa21VersX", "k21 Versus X", "X", "k21", 1, kc->k21, 0, format->iXWidth, 1, 0);
1293  midiCreatePlotFile2D ("Kappa31VersX", "k31 Versus X", "X", "k31", 1, kc->k31, 0, format->iXWidth, 1, 0);
1294  midiCreatePlotFile2D ("Kappa22VersX", "k22 Versus X", "X", "k22", 1, kc->k22, 0, format->iXWidth, 1, 0);
1295  midiCreatePlotFile2D ("Kappa32VersX", "k32 Versus X", "X", "k32", 1, kc->k32, 0, format->iXWidth, 1, 0);
1296  midiCreatePlotFile2D ("Kappa42VersX", "k42 Versus X", "X", "k42", 1, kc->k42, 0, format->iXWidth, 1, 0);
1297  }
1298 
1299  if (!n)
1300  {
1301  *error = 1;
1302  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "High error bars on all channels");
1303  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Computation of the Kappa Coefficients is abandonned");
1304  return;
1305  }
1306 
1307  for (x = 0; x < format->iXWidth; x++)
1308  {
1309  // If it is other than a masked error then no kappa matrix should be computed
1310  if (badChannelList[x] & 1023)
1311  {
1312  *error = 1;
1313  return;
1314  }
1315  }
1316 
1317  // Now smooth the result. (Not sure yet if filtering is wise)
1318  midiReportTbd (midiReportPtr, routine, __FILE__, __LINE__, "Routine TBD");
1319 
1320  return;
1321 }
1322 /*****************************************************************************/