MIDI Pipeline Reference Manual  2.8.3
procFrgHS.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: procFrgHS.c
7 * Description: Contains routines for Fringe Track data post processing
8 *
9 * History:
10 * 03-Feb-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 <math.h>
27 #include "midiGlobal.h"
28 #include "midiLib.h"
29 #include "memoryHandling.h"
30 #include "calibration.h"
31 #include "errorHandling.h"
32 #include "midiFitsUtility.h"
33 #include "fitsAnalysisFrgHS.h"
34 #include "imageProcessing.h"
35 #include "createProdFrg.h"
36 #include "createProdFrgUndisp.h"
37 #include "transferFunction.h"
38 #include "visibility.h"
39 #include "diagnostics.h"
40 #include "estimationFrg.h"
41 #include "estimationFrgHSUndisp.h"
42 #include "photometry.h"
43 #include "procFrgHS.h"
44 #include "preProcFrgHS.h"
45 #include "qfits.h"
46 
47 /**********************************************************
48 * Constant definitions
49 **********************************************************/
50 
51 /**********************************************************
52 * Global Variables
53 **********************************************************/
54 
55 /*============================ C O D E A R E A ===========================*/
56 
57 
58 
59 
60 /******************************************************************************
61 * European Southern Observatory
62 * VLTI MIDI Data Reduction Software
63 *
64 * Module name: procFrgHS
65 * Input/Output: See function arguments to avoid duplication
66 * Description: This is the main routine for HIGH_SEND fringe track
67 * The memory allocation needs the following considerations:
68 *
69 * Split Interferometry files require "compressedInterf" & "formatInterf"
70 * One Photometry A file requires "compressedPhotomA" & "formatPhotomA"
71 * One Photometry B file requires "compressedPhotomB" & "formatPhotomB"
72 *
73 * History:
74 * 21-Jul-03 (csabet) Created
75 ******************************************************************************/
76 void procFrgHS (
77  MidiFiles *fileNames, // In: Pointer to midi files structure
78  UserOptions *options, // In: Pointer to user options
79  int *error, // Ou: Error status
80  cpl_parameterlist *parlist,
81  cpl_frameset *frameset)
82 {
83 
84  // Local Declarations
85  // ------------------
86  const char routine[] = "procFrgHS";
87  ImageFormat *formatInterf=NULL; // Applies to interf files
88  ImageFormat *formatPhotomA=NULL;// Applies to photom A files
89  ImageFormat *formatPhotomB=NULL;// Applies to photom B files
90  CompressedData *compressedInterf=NULL, *compressedPhotomA=NULL, *compressedPhotomB=NULL;
91  FilterData *filterInfo=NULL;
92  FILE *signaturePtr=NULL;
93  DispersedResult *dispResult=NULL;
94  CorrectedFrames *corrFrames=NULL;
95  // Algorithm
96  // ---------
97  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
98  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
99 
100  // Write a signature. Midas checks to see which template is running
101  if (strcmp (batchTemplate, "HIGH_SENS_CALIB") == 0)
102  signaturePtr = fopen ("MIDI_sig_hsc.log", "w");
103  else if (strcmp (batchTemplate, "HIGH_SENS_SCIENCE") == 0)
104  signaturePtr = fopen ("MIDI_sig_hss.log", "w");
105  else
106  {
107  sprintf (midiMessage, "Invalid template ... %s", batchTemplate);
108  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
109  }
110  fclose (signaturePtr);
111 
112  // Reset status
113  *error = 0;
114 
115  // Analyse Input files
116  // -------------------
117  // Allocate memory
118  formatInterf = callocImageFormat ();
119  formatPhotomA = callocImageFormat ();
120  formatPhotomB = callocImageFormat ();
121  corrFrames = cpl_calloc (1, sizeof (CorrectedFrames));
122 
123  corrFrames->CorrInterf=0;
124  corrFrames->CorrPhotomA=0;
125  corrFrames->CorrPhotomB=0;
126 
127  analyseFitsFrgHS (fileNames, formatInterf, formatPhotomA, formatPhotomB, error);
128  if (*error)
129  {
130  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse FITS files");
131  freeImageFormat (formatInterf);
132  freeImageFormat (formatPhotomA);
133  freeImageFormat (formatPhotomB);
134  return;
135  }
136 
137  // Reduce Data
138  // -----------
139  // Allocate memory for the compressed data, filter data and initialise to zero
140  compressedInterf = callocMidiCompressed (formatInterf);
141  compressedPhotomA = callocMidiCompressed (formatPhotomA);
142  compressedPhotomB = callocMidiCompressed (formatPhotomB);
143  badChannelList = (int *) calloc (formatInterf->iXWidth, sizeof (int));
144  filterInfo = callocFilterInfo();
145 
146  // Preprocessing
147  preProcFrgHS (options, filterInfo, fileNames, compressedInterf, compressedPhotomA,
148  compressedPhotomB, formatInterf, formatPhotomA, formatPhotomB, error);
149 
150  if (*error)
151  {
152  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress FITS files");
153  freeCompressedData (formatInterf, compressedInterf);
154  freeCompressedData (formatPhotomA, compressedPhotomA);
155  freeCompressedData (formatPhotomB, compressedPhotomB);
156  free (badChannelList);
157  freeImageFormat (formatInterf);
158  freeImageFormat (formatPhotomA);
159  freeImageFormat (formatPhotomB);
160  freeFilterInfo (filterInfo);
161  return;
162  }
163 
164  // Check Delay Lines for jumps within scans
165  checkDelayLineJumps ("INTERF", formatInterf, compressedInterf);
166 
167  // Check Delay Line Consistencies
168  checkDelayLineConsistencies ("INTERF", "PHOTOMA", compressedInterf, compressedPhotomA, formatInterf, formatPhotomA, error);
169  checkDelayLineConsistencies ("INTERF", "PHOTOMB", compressedInterf, compressedPhotomB, formatInterf, formatPhotomB, error);
170  if (*error)
171  {
172  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
173  freeCompressedData (formatInterf, compressedInterf);
174  freeCompressedData (formatPhotomA, compressedPhotomA);
175  freeCompressedData (formatPhotomB, compressedPhotomB);
176  free (badChannelList);
177  freeImageFormat (formatInterf);
178  freeImageFormat (formatPhotomA);
179  freeImageFormat (formatPhotomB);
180  freeFilterInfo (filterInfo);
181  return;
182  }
183 
184  // Check Chopping Statistics. for QCLOG diagnostics
185  // ------------------------------------------------
186  reportInterfChopping ("INTERF", formatInterf, compressedInterf);
187  reportPhotomChopping ("PHOTOMA", formatPhotomA, compressedPhotomA);
188  reportPhotomChopping ("PHOTOMB", formatPhotomB, compressedPhotomB);
189  if (options->processing == DISPERSED)
190  {
191  displayInterfChoppingDisp ("INTERF", formatInterf, compressedInterf);
192  displayPhotomChoppingDisp ("PHOTOMA", formatPhotomA, compressedPhotomA);
193  displayPhotomChoppingDisp ("PHOTOMB", formatPhotomB, compressedPhotomB);
194  }
195 
196  // Remove Sky Background and display Target with Sky removed
197  dispResult = callocDispResult (formatInterf->iXWidth);
198  cleanupFrgHS (options->processing, formatInterf, formatPhotomA, formatPhotomB, compressedInterf,
199  compressedPhotomA, compressedPhotomB, dispResult, error, corrFrames);
200  if (*error)
201  {
202  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
203  freeCompressedData (formatInterf, compressedInterf);
204  freeCompressedData (formatPhotomA, compressedPhotomA);
205  freeCompressedData (formatPhotomB, compressedPhotomB);
206  free (badChannelList);
207  freeImageFormat (formatInterf);
208  freeImageFormat (formatPhotomA);
209  freeImageFormat (formatPhotomB);
210  freeFilterInfo (filterInfo);
211  freeDispResult (dispResult);
212  return;
213  }
214  // Display Waterfall data
215  prepareWaterfallDisplay (fileNames, formatInterf, compressedInterf);
216 
217  // Frequency Calibration, if it is a CALIB batch
218  // ---------------------------------------------
219  if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
220  calibrateWaveUsingPolynom (fileNames, formatInterf);
221 
222  // Create UNDISPERSED allSpectrum and dcLevels
223  // -------------------------------------------
224  getUndispersedPowerSpectrum (formatInterf, compressedInterf);
225 
226  // Display Waterpower data
227  prepareWaterpowerDisplay (fileNames, formatInterf, compressedInterf->allSpectrum);
228 
229  // Select processing mode
230  // ----------------------
231  if (options->processing == DISPERSED)
232  {
233  postProcFringe (fileNames, filterInfo, compressedInterf,
234  compressedPhotomA, compressedPhotomB, formatInterf,
235  formatPhotomA, formatPhotomB, dispResult, error,
236  parlist, frameset, corrFrames);
237  if (*error)
238  {
239  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
240  freeCompressedData (formatInterf, compressedInterf);
241  freeCompressedData (formatPhotomA, compressedPhotomA);
242  freeCompressedData (formatPhotomB, compressedPhotomB);
243  free (badChannelList);
244  freeImageFormat (formatInterf);
245  freeImageFormat (formatPhotomA);
246  freeImageFormat (formatPhotomB);
247  freeFilterInfo (filterInfo);
248  freeDispResult (dispResult);
249  return;
250  }
251  }
252  else if (options->processing == UNDISPERSED)
253  {
254  postProcFrgHSUndisp (fileNames, compressedInterf, compressedPhotomA,
255  compressedPhotomB, filterInfo, formatInterf, formatPhotomA, formatPhotomB, error, parlist, frameset);
256  if (*error)
257  {
258  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
259  freeCompressedData (formatInterf, compressedInterf);
260  freeCompressedData (formatPhotomA, compressedPhotomA);
261  freeCompressedData (formatPhotomB, compressedPhotomB);
262  free (badChannelList);
263  freeImageFormat (formatInterf);
264  freeImageFormat (formatPhotomA);
265  freeImageFormat (formatPhotomB);
266  freeFilterInfo (filterInfo);
267  freeDispResult (dispResult);
268  return;
269  }
270  }
271  else
272  {
273  *error = 1;
274  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown processing mode requested");
275  freeCompressedData (formatInterf, compressedInterf);
276  freeCompressedData (formatPhotomA, compressedPhotomA);
277  freeCompressedData (formatPhotomB, compressedPhotomB);
278  free (badChannelList);
279  freeImageFormat (formatInterf);
280  freeImageFormat (formatPhotomA);
281  freeImageFormat (formatPhotomB);
282  freeFilterInfo (filterInfo);
283  freeDispResult (dispResult);
284  return;
285  }
286 
287  // Release memory used in this batch
288  freeCompressedData (formatInterf, compressedInterf);
289  freeCompressedData (formatPhotomA, compressedPhotomA);
290  freeCompressedData (formatPhotomB, compressedPhotomB);
291  free (badChannelList);
292  freeImageFormat (formatInterf);
293  freeImageFormat (formatPhotomA);
294  freeImageFormat (formatPhotomB);
295  freeFilterInfo (filterInfo);
296  freeDispResult (dispResult);
297  cpl_free(corrFrames);
298  return;
299 }
300 
301 
302 
303 /******************************************************************************
304 * European Southern Observatory
305 * VLTI MIDI Data Reduction Software
306 *
307 * Module name: postProcFringe
308 * Input/Output: See function arguments to avoid duplication
309 * Description: Performs complete post processing for the DISPERSED mode
310 *
311 * History:
312 * 03-Feb-05 (csabet) Created
313 ******************************************************************************/
314 void postProcFringe (
315  MidiFiles *fileNames, // In: Pointer to file names
316  FilterData *filterInfo, // In: Filter data
317  CompressedData *compressedInterf, // In: Compressed interferometry data
318  CompressedData *compressedPhotomA, // In: Photometry data
319  CompressedData *compressedPhotomB, // In: Photometry data
320  ImageFormat *formatInterf, // In: Image size of interferometry data
321  ImageFormat *formatPhotomA, // In: Applies to A photom files
322  ImageFormat *formatPhotomB, // In: Applies to B photom files
323  DispersedResult *dispResult, // IO: Output results
324  int *error, // Ou: Status
325  cpl_parameterlist *parlist,
326  cpl_frameset *frameset,
327  CorrectedFrames *corrFrames)
328 {
329 
330  // Local Declarations
331  // ------------------
332  const char routine[] = "postProcFringe";
333  CalibratorParam *calibrator=NULL;
334  float *freqCal=NULL;
335  IauExchange *iauData=NULL;
336 
337  // Algorithm
338  // ---------
339  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
340  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
341 
342  cpl_msg_info(cpl_func,"\nProcessing data in DISPERSED mode for batch %d \n", batchNumber);
343  cpl_msg_info(cpl_func,"------------------------------------------- \n");
344  fprintf (midiReportPtr, "\nProcessing data in DISPERSED mode for batch %d \n", batchNumber);
345  fprintf (midiReportPtr, "------------------------------------------- \n");
346 
347  // Allocate memory
348  formatInterf->fftsize = POST_FFT_SIZE;
349  calibrator = callocCalibrator ();
350  freqCal = (float *) calloc (formatInterf->iXWidth, sizeof (float));
351 
352  // Reset status
353  *error = 0;
354  dispResult->photomAExists = 0;
355  dispResult->photomBExists = 0;
356  dispResult->photomIExists = 0;
357  dispResult->calibVisExists = 0;
358  dispResult->trfExists = 0;
359 
360  // Estimate fringe
361  // ---------------
362  estimateFringe (fileNames, filterInfo, compressedInterf, formatInterf, freqCal,
363  dispResult->rawVis2, dispResult->rawVis2Err, error);
364  if (*error)
365  {
366  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Raw Visibilities");
367  return;
368  }
369 
370 
371  cpl_msg_info(cpl_func,"\nRunning Photometry on photom A data: ");
372  fprintf (midiReportPtr, "\nRunning Photometry on photom A data: ");
373  estimatePhotom ('A', compressedPhotomA, formatPhotomA, dispResult->photomA, dispResult->photomAErr, error);
374  if (*error)
375  {
376  cpl_msg_info(cpl_func,"%s \n", UNAV);
377  fprintf (midiReportPtr, "%s \n", UNAV);
378  }
379  else
380  {
381  dispResult->photomAExists = 1;
382  cpl_msg_info(cpl_func,"\n");
383  fprintf (midiReportPtr, "\n");
384  }
385 
386  cpl_msg_info(cpl_func,"Running Photometry on Photom B data: ");
387  fprintf (midiReportPtr, "Running Photometry on Photom B data: ");
388  estimatePhotom ('B', compressedPhotomB, formatPhotomB, dispResult->photomB, dispResult->photomBErr, error);
389  if (*error)
390  {
391  cpl_msg_info(cpl_func,"%s \n", UNAV);
392  fprintf (midiReportPtr, "%s \n", UNAV);
393  }
394  else
395  {
396  dispResult->photomBExists = 1;
397  cpl_msg_info(cpl_func,"\n");
398  fprintf (midiReportPtr, "\n");
399  }
400 
401  cpl_msg_info(cpl_func,"Running Photometry on Interf data: ");
402  fprintf (midiReportPtr, "Running Photometry on Interf data: ");
403  estimatePhotom ('I', compressedInterf, formatInterf, dispResult->photomI, dispResult->photomIErr, error);
404  if (*error)
405  {
406  cpl_msg_info(cpl_func,"%s \n", UNAV);
407  fprintf (midiReportPtr, "%s \n", UNAV);
408  }
409  else
410  {
411  dispResult->photomIExists = 1;
412  cpl_msg_info(cpl_func,"\n");
413  fprintf (midiReportPtr, "\n");
414  }
415 
416  // Compute DISPERSED Normalized but Uncalibrated Visibility
417  // --------------------------------------------------------
418  computeUncalibVis (formatInterf, freqCal, dispResult, error);
419  if (*error)
420  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Uncalibrated Visibility for this batch");
421 
422  // Compute Transfer Function if observation category is CALIB
423  // ----------------------------------------------------------
424  if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
425  {
426  estimateTransferFunction (formatInterf, fileNames, dispResult, calibrator, error, parlist, frameset);
427  if (*error)
428  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Transfer Function for this batch");
429  }
430 
431  // Compute Calibrated Visibility if observation category is SCIENCE
432  // ----------------------------------------------------------------
433  if (strcmp (formatInterf->obsCatg, "SCIENCE") == 0)
434  {
435  computeCalibVis (fileNames, formatInterf, dispResult, error, parlist, frameset);
436  if (*error)
437  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Calibrated Visibility for this batch");
438  }
439 
440  // Report results
441  if (diagnostic) midiReportResultsFringe (freqCal, formatInterf, dispResult);
442  reportRejectList (formatInterf, compressedInterf);
443 
444  // Create the product files
445 
446  iauData=createFrgProd (fileNames, formatInterf, freqCal, dispResult, calibrator,
447  error, corrFrames);
448 
449  /*Save the correlated flux*/
450  if(iauData != NULL){
451  midiSaveCorrelatedFlux(iauData, dispResult, parlist, frameset);
452  }
453  if (*error)
454  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create product files");
455 
456  /* Release memory */
457  freeCalibrator (calibrator);
458  free (freqCal);
459  if(iauData != NULL){
460  freeIauExchange (iauData);
461  }
462  return;
463 }
464 /*****************************************************************************/
465 
466 
467 
468 /******************************************************************************
469 * European Southern Observatory
470 * VLTI MIDI Data Reduction Software
471 *
472 * Module name: postProcFrgHSUndisp
473 * Input/Output: See function arguments to avoid duplication
474 * Description: Performs complete post processing for the UNDISPERSED mode
475 *
476 * History:
477 * 03-Feb-05 (csabet) Created
478 ******************************************************************************/
479 void postProcFrgHSUndisp (
480  MidiFiles *fileNames, // In: Pointer to file names
481  CompressedData *compressedInterf, // In: Compressed interferometry data
482  CompressedData *compressedPhotomA, // In: Compressed photometry data
483  CompressedData *compressedPhotomB, // In: Compressed photometry data
484  FilterData *filterInfo, // In: Filter information
485  ImageFormat *formatInterf,
486  ImageFormat *formatPhotomA,
487  ImageFormat *formatPhotomB,
488  int *error, // Ou: Status
489  cpl_parameterlist *parlist,
490  cpl_frameset *frameset)
491 {
492 
493  // Local Declarations
494  // ------------------
495  const char routine[] = "postProcFrgHSUndisp";
496  RawVisibility *rawVis=NULL;
497  PhotometryResult *photometry=NULL;
498  CalibratedVisibility *calibVis=NULL;
499  CalibratorParam *calibrator=NULL;
500  TransferFunction *trf=NULL;
501 
502 
503  // Algorithm
504  // ---------
505  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
506  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
507 
508  cpl_msg_info(cpl_func,"\nProcessing HIGH_SENS data in UNDISPERSED mode for batch %d \n", batchNumber);
509  cpl_msg_info(cpl_func,"------------------------------------------------------- \n");
510  fprintf (midiReportPtr, "\nProcessing HIGH_SENS data in UNDISPERSED mode for batch %d \n", batchNumber);
511  fprintf (midiReportPtr, "------------------------------------------------------- \n");
512 
513  // Reset status
514  *error = 0;
515 
516  // Finalise reject list using Signal / Noise
517  getBadScansFromSpectrumUndisp (filterInfo, formatInterf, compressedInterf, error);
518  if (*error) return;
519 
520  // Compute UNDISPERSED fringe
521  // --------------------------
522  rawVis = callocRawVis ();
523  photometry = callocPhotometry ();
524  estimateFrgHSUndisp (compressedInterf, compressedPhotomA, compressedPhotomB, filterInfo,
525  formatInterf, formatPhotomA, formatPhotomB, rawVis, photometry);
526 
527  // Compute UNDISPERSED Uncalibrated Visibility
528  // -------------------------------------------
529  computeUncalibVisHSUndisp (rawVis, photometry);
530 
531  // Compute Transfer Function if observation category is CALIB
532  // ----------------------------------------------------------
533  calibrator = callocCalibrator ();
534  trf = callocTrFunction (6);
535  if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
536  {
537  estimateTransferFunctionUndisp (fileNames, rawVis, trf, calibrator, error,parlist, frameset);
538  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
539  "Cannot compute Transfer Function for this batch");
540  }
541 
542  // Compute Calibrated Visibility if observation category is SCIENCE
543  // ----------------------------------------------------------------
544  calibVis = callocCalibVis ();
545  if (strcmp (formatInterf->obsCatg, "SCIENCE") == 0)
546  {
547  computeCalibVisHSUndisp (fileNames, rawVis, trf, calibVis, error);
548  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
549  "Cannot compute Calibrated Visibility for this batch");
550  }
551 
552  // Create the product files
553  // ------------------------
554  createFrgProdUndisp (fileNames, formatInterf, filterInfo, rawVis, trf,
555  calibrator, calibVis, photometry, error);
556  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create product files");
557 
558  // Release memory
559  freeRawVis (rawVis);
560  freePhotometry (photometry);
561  freeTrFunction (trf);
562  freeCalibrator (calibrator);
563  freeCalibVis (calibVis);
564 
565  return;
566 }
567 /*****************************************************************************/
568 
569 
570 /******************************************************************************
571 * European Southern Observatory
572 * VLTI MIDI Data Reduction Software
573 *
574 * Module name: cleanupFrgHS
575 * Input/Output: See function arguments to avoid duplication
576 * Description: Removes sky etc from the data
577 *
578 * History:
579 * 06-Dec-05 (csabet) Created
580 ******************************************************************************/
581 void cleanupFrgHS (
582  enum ProcessingMode processing,
583  ImageFormat *formatInterf, // In: Interf size parameters
584  ImageFormat *formatPhotomA, // In: PhotA size parameters
585  ImageFormat *formatPhotomB, // In: PhotB size parameters
586  CompressedData *compressedInterf, // Ou: Pointer to the compressed interferometry data structure
587  CompressedData *compressedPhotomA, // Ou: Pointer to the compressed photom A data structure
588  CompressedData *compressedPhotomB, // Ou: Pointer to the compressed photom B data structure
589  DispersedResult *dispResult, // Ou: Output results
590  int *error,
591  CorrectedFrames *corrFrames) // Ou: Error status
592 {
593 
594  // Local Declarations
595  // ------------------
596  const char routine[] = "cleanupFrgHS";
597  int F, s, t, u, n, f, R, X, transitions, localError;
598  double *array, fluxAve;
599  float *arrayF;
600  char *fileName, *title;
601 
602  // Algorithm
603  // ---------
604  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
605  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
606 
607  cpl_msg_info(cpl_func,"\nCleaning up HIGH_SENS files for batch %d \n", batchNumber);
608  cpl_msg_info(cpl_func,"------------------------------------- \n");
609  fprintf (midiReportPtr, "\nCleaning up HIGH_SENS files for batch %d \n", batchNumber);
610  fprintf (midiReportPtr, "------------------------------------- \n");
611 
612  // Initialise
613  localError = 0;
614  *error = 0;
615  transitions = 0;
616 
617  // Check Interf chopping cycle
618  // ---------------------------
619  s = t = u = 0;
620  for (F = 0; F < formatInterf->numOfFrames; F++)
621  {
622  if (compressedInterf->tarType[F] == 'S') s++;
623  else if (compressedInterf->tarType[F] == 'T') t++;
624  else u++;
625  }
626  // Report
627  if (u || s || (t != formatInterf->numOfFrames))
628  {
629  sprintf (midiMessage, "Interferometry data is chopped");
630  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
631  *error = 1;
632  }
633  transitions = countTransitions ('T', formatInterf->numOfFrames, compressedInterf->tarType);
634  sprintf (midiMessage, "Target Transitions for Interf = %d", transitions);
635  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
636  formatInterf->numOfSkyScans = 0;
637 
638  // Check PhotomA chopping cycle
639  // ----------------------------
640  transitions = countTransitions ('T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
641  sprintf (midiMessage, "Target Transitions for Photom A = %d", transitions);
642  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
643  if (transitions < 10)
644  {
645  sprintf (midiMessage, "Photometry A data is not chopped");
646  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
647  *error = 1;
648  }
649 
650  // Check PhotomB chopping cycle
651  // ----------------------------
652  transitions = countTransitions ('T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
653  sprintf (midiMessage, "Target Transitions for Photom B = %d", transitions);
654  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
655  if (transitions < 10)
656  {
657  sprintf (midiMessage, "Photometry B data is not chopped");
658  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
659  *error = 1;
660  }
661 
662  // if No error so far
663  if (!(*error))
664  {
665  formatInterf->chopped = 0;
666  formatPhotomA->chopped = 1;
667  formatPhotomB->chopped = 1;
668  }
669  else
670  {
671  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot cleanup HIGH_SENS files");
672  *error = 1;
673  return;
674  }
675 
676  // Correct benign tarType inconsistencies
677  // --------------------------------------
678  corrFrames->CorrInterf=0;
679  corrFrames->CorrPhotomA = correctTarType ("PHOTOMA",
680  compressedPhotomA->tarType, compressedPhotomA->time,
681  formatPhotomA->numOfFrames, &localError);
682  if (localError) *error = 1;
683 
684  corrFrames->CorrPhotomB = correctTarType ("PHOTOMB",
685  compressedPhotomB->tarType, compressedPhotomB->time,
686  formatPhotomB->numOfFrames, &localError);
687  if (*error || localError)
688  {
689  *error = 1;
690  return;
691  }
692 
693  // If all is well, carry out cleaning
694  // ----------------------------------
695  // First compute binned Target 'T' photometries with background
696  if (processing == DISPERSED && (strcmp(formatPhotomA->beamCombiner, "HIGH_SENS") == 0))
697  {
698  computeBinnedPhotom (compressedPhotomA, compressedPhotomB, formatPhotomA,
699  &(dispResult->photomATotal1), &(dispResult->photomATotal2), &(dispResult->photomATotal3),
700  &(dispResult->photomBTotal1), &(dispResult->photomBTotal2), &(dispResult->photomBTotal3), error);
701 
702  // Report
703  cpl_msg_info(cpl_func,"\nBinned photometric measurements with background \n");
704  cpl_msg_info(cpl_func,"----------------------------------------------- \n");
705  cpl_msg_info(cpl_func,"Grism ID: %s \n", formatPhotomA->grismId);
706  fprintf(midiReportPtr, "\nBinned photometric measurements with background QCLOG \n");
707  fprintf(midiReportPtr, "----------------------------------------------- QCLOG \n");
708  fprintf(midiReportPtr, "Grism ID: %s QCLOG \n", formatPhotomA->grismId);
709  if (strcmp(formatPhotomA->grismId, "PRISM") == 0)
710  {
711  cpl_msg_info(cpl_func,"ArIII (Channel 113 -> 121) Beam A = %0.2f Beam B = %0.2f \n",
712  dispResult->photomATotal1, dispResult->photomBTotal1);
713  cpl_msg_info(cpl_func," SIV (Channel 88 -> 98) Beam A = %0.2f Beam B = %0.2f \n",
714  dispResult->photomATotal2, dispResult->photomBTotal2);
715  cpl_msg_info(cpl_func," NeII (Channel 36 -> 50) Beam A = %0.2f Beam B = %0.2f \n\n",
716  dispResult->photomATotal3, dispResult->photomBTotal3);
717 
718  fprintf(midiReportPtr, "ArIII (Channel 113 -> 121) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
719  dispResult->photomATotal1, dispResult->photomBTotal1);
720  fprintf(midiReportPtr, " SIV (Channel 88 -> 98) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
721  dispResult->photomATotal2, dispResult->photomBTotal2);
722  fprintf(midiReportPtr, " NeII (Channel 36 -> 50) Beam A = %0.2f Beam B = %0.2f QCLOG \n\n",
723  dispResult->photomATotal3, dispResult->photomBTotal3);
724  }
725  else
726  {
727  cpl_msg_info(cpl_func,"ArIII (Channel 52 -> 68) Beam A = %0.2f Beam B = %0.2f \n",
728  dispResult->photomATotal1, dispResult->photomBTotal1);
729  cpl_msg_info(cpl_func," SIV (Channel 116 -> 136) Beam A = %0.2f Beam B = %0.2f \n",
730  dispResult->photomATotal2, dispResult->photomBTotal2);
731  cpl_msg_info(cpl_func," NeII (Channel 217 -> 244) Beam A = %0.2f Beam B = %0.2f \n\n",
732  dispResult->photomATotal3, dispResult->photomBTotal3);
733 
734  fprintf(midiReportPtr, "ArIII (Channel 52 -> 68) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
735  dispResult->photomATotal1, dispResult->photomBTotal1);
736  fprintf(midiReportPtr, " SIV (Channel 116 -> 136) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
737  dispResult->photomATotal2, dispResult->photomBTotal2);
738  fprintf(midiReportPtr, " NeII (Channel 217 -> 244) Beam A = %0.2f Beam B = %0.2f QCLOG \n\n",
739  dispResult->photomATotal3, dispResult->photomBTotal3);
740  }
741  }
742 
743  removeSkyBackground ("PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
744  if (localError) *error = 1;
745  removeSkyBackground ("PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
746  if (formatPhotomA->numOfFrames != formatPhotomA->numOfFrames || *error || localError)
747  {
748  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unequal numOfFrames");
749  *error = 1;
750  return;
751  }
752 
753  // Now compute binned Target 'T' photometries with background removed
754  if (processing == DISPERSED && (strcmp(formatPhotomB->beamCombiner, "HIGH_SENS") == 0))
755  {
756  computeBinnedPhotom (compressedPhotomA, compressedPhotomB, formatPhotomA,
757  &(dispResult->photomATarg1), &(dispResult->photomATarg2), &(dispResult->photomATarg3),
758  &(dispResult->photomBTarg1), &(dispResult->photomBTarg2), &(dispResult->photomBTarg3), error);
759 
760  // Report
761  cpl_msg_info(cpl_func,"\nBinned photometric measurements without background \n");
762  cpl_msg_info(cpl_func,"-------------------------------------------------- \n");
763  cpl_msg_info(cpl_func,"Grism ID: %s \n", formatPhotomA->grismId);
764  fprintf(midiReportPtr, "\nBinned photometric measurements without background QCLOG \n");
765  fprintf(midiReportPtr, "-------------------------------------------------- QCLOG \n");
766  fprintf(midiReportPtr, "Grism ID: %s QCLOG \n", formatPhotomA->grismId);
767  if (strcmp(formatPhotomA->grismId, "PRISM") == 0)
768  {
769  cpl_msg_info(cpl_func,"ArIII (Channel 113 -> 121) Beam A = %0.2f Beam B = %0.2f \n",
770  dispResult->photomATarg1, dispResult->photomBTarg1);
771  cpl_msg_info(cpl_func," SIV (Channel 88 -> 98) Beam A = %0.2f Beam B = %0.2f \n",
772  dispResult->photomATarg2, dispResult->photomBTarg2);
773  cpl_msg_info(cpl_func," NeII (Channel 36 -> 50) Beam A = %0.2f Beam B = %0.2f \n\n",
774  dispResult->photomATarg3, dispResult->photomBTarg3);
775 
776  fprintf(midiReportPtr, "ArIII (Channel 113 -> 121) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
777  dispResult->photomATarg1, dispResult->photomBTarg1);
778  fprintf(midiReportPtr, " SIV (Channel 88 -> 98) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
779  dispResult->photomATarg2, dispResult->photomBTarg2);
780  fprintf(midiReportPtr, " NeII (Channel 36 -> 50) Beam A = %0.2f Beam B = %0.2f QCLOG \n\n",
781  dispResult->photomATarg3, dispResult->photomBTarg3);
782  }
783  else
784  {
785  cpl_msg_info(cpl_func,"ArIII (Channel 52 -> 68) Beam A = %0.2f Beam B = %0.2f \n",
786  dispResult->photomATarg1, dispResult->photomBTarg1);
787  cpl_msg_info(cpl_func," SIV (Channel 116 -> 136) Beam A = %0.2f Beam B = %0.2f \n",
788  dispResult->photomATarg2, dispResult->photomBTarg2);
789  cpl_msg_info(cpl_func," NeII (Channel 217 -> 244) Beam A = %0.2f Beam B = %0.2f \n\n",
790  dispResult->photomATarg3, dispResult->photomBTarg3);
791 
792  fprintf(midiReportPtr, "ArIII (Channel 52 -> 68) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
793  dispResult->photomATarg1, dispResult->photomBTarg1);
794  fprintf(midiReportPtr, " SIV (Channel 116 -> 136) Beam A = %0.2f Beam B = %0.2f QCLOG \n",
795  dispResult->photomATarg2, dispResult->photomBTarg2);
796  fprintf(midiReportPtr, " NeII (Channel 217 -> 244) Beam A = %0.2f Beam B = %0.2f QCLOG \n\n",
797  dispResult->photomATarg3, dispResult->photomBTarg3);
798  }
799  }
800 
801  // Reject frames whose flux are <= 0 or are not 'T'
802  for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
803  {
804  for (X = 0; X < formatInterf->iXWidth; X++)
805  {
806  if (badChannelList[X])
807  continue;
808 
809  for (f = 0; f < formatInterf->numOfFrames; f++)
810  {
811  if ((compressedInterf->tarType[f] == 'T') &&
812  (((compressedInterf->iDispFringe)[R])[X])[f] <= 0.0)
813  {
814  compressedInterf->rejectList[X][f] |= BSL_DATA_ERROR;
815  if (diagnostic > 3)
816  {
817  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d Interf %10.2f",
818  R, X, f, (((compressedInterf->iDispFringe)[R])[X])[f]);
819  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
820  }
821  }
822  // Reject frames which are not 'T'
823  if (compressedInterf->tarType[f] != 'T')
824  compressedInterf->rejectList[X][f] |= BSL_SKY_SCAN;
825  }
826  }
827  }
828 
829  for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
830  {
831  for (X = 0; X < formatPhotomA->iXWidth; X++)
832  {
833  if (badChannelList[X])
834  continue;
835 
836  for (f = 0; f < formatPhotomA->numOfFrames; f++)
837  {
838  if ((compressedPhotomA->tarType[f] == 'T') &&
839  (((compressedPhotomA->iDispFringe)[R])[X])[f] <= 0.0)
840  {
841  compressedPhotomA->rejectList[X][f] |= BSL_DATA_ERROR;
842  if (diagnostic > 3)
843  {
844  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d PhotomA %10.2f",
845  R, X, f, (((compressedPhotomA->iDispFringe)[R])[X])[f]);
846  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
847  }
848  }
849  // Reject frames which are not 'T'
850  if (compressedPhotomA->tarType[f] != 'T')
851  compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
852  }
853  }
854  }
855 
856  for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
857  {
858  for (X = 0; X < formatPhotomB->iXWidth; X++)
859  {
860  if (badChannelList[X])
861  continue;
862 
863  for (f = 0; f < formatPhotomB->numOfFrames; f++)
864  {
865  if ((compressedPhotomB->tarType[f] == 'T') &&
866  (((compressedPhotomB->iDispFringe)[R])[X])[f] <= 0.0)
867  {
868  compressedPhotomB->rejectList[X][f] |= BSL_DATA_ERROR;
869  if (diagnostic > 3)
870  {
871  sprintf (midiMessage, "Negative Flux at: R %d X %3d F %4d PhotomB %10.2f",
872  R, X, f, (((compressedPhotomB->iDispFringe)[R])[X])[f]);
873  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
874  }
875  }
876  // Reject frames which are not 'T'
877  if (compressedPhotomB->tarType[f] != 'T')
878  compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
879  }
880  }
881  }
882 
883  // Display data with background removed
884  if (plotFile)
885  {
886  array = (double *) calloc (formatInterf->numOfFrames, sizeof (double));
887 
888  n = 0;
889  fluxAve = 0.0;
890  for (f = 0; f < formatInterf->numOfFrames; f++)
891  {
892  if (compressedInterf->tarType[f] == 'T')
893  {
894  array[n] = compressedInterf->iFringe1[f];
895  fluxAve += array[n];
896  n++;
897  }
898  }
899  fluxAve /= n;
900  cpl_msg_info(cpl_func,"Interf DATA1 Average Flux = %f \n", fluxAve);
901  midiCreatePlotFileDouble2D ("InterfCleanDATA1", "Interferometry DATA1 (Sky Removed)",
902  "Frame", "Flux", 0, array, 0, n, 1);
903 
904  n = 0;
905  fluxAve = 0.0;
906  for (f = 0; f < formatInterf->numOfFrames; f++)
907  {
908  if (compressedInterf->tarType[f] == 'T')
909  {
910  array[n] = compressedInterf->iFringe2[f];
911  fluxAve += array[n];
912  n++;
913  }
914  }
915  fluxAve /= n;
916  cpl_msg_info(cpl_func,"Interf DATA2 Average Flux = %f \n", fluxAve);
917  midiCreatePlotFileDouble2D ("InterfCleanDATA2", "Interferometry DATA2 (Sky Removed)",
918  "Frame", "Flux", 0, array, 0, n, 1);
919 
920  n = 0;
921  for (f = 0; f < formatInterf->numOfFrames; f++)
922  {
923  if (compressedInterf->tarType[f] == 'T')
924  {
925  array[n] = compressedInterf->iFringe[f];
926  n++;
927  }
928  }
929  midiCreatePlotFileDouble2D ("InterfCleanDATA1_DATA2", "Interferometry DATA1-DATA2 (Sky Removed)",
930  "Frame", "Flux", 0, array, 0, n, 1);
931 
932  n = 0;
933  fluxAve = 0.0;
934  for (f = 0; f < formatPhotomA->numOfFrames; f++)
935  {
936  if (compressedPhotomA->tarType[f] == 'T')
937  {
938  array[n] = compressedPhotomA->iFringe1[f];
939  fluxAve += array[n];
940  n++;
941  }
942  }
943  fluxAve /= n;
944  cpl_msg_info(cpl_func,"PhotomA DATA1 Average Flux = %f \n", fluxAve);
945  midiCreatePlotFileDouble2D ("PhotomACleanDATA1", "Photometry A DATA1 (Sky Removed)",
946  "Frame", "Flux", 0, array, 0, n, 1);
947 
948  n = 0;
949  fluxAve = 0.0;
950  for (f = 0; f < formatPhotomA->numOfFrames; f++)
951  {
952  if (compressedPhotomA->tarType[f] == 'T')
953  {
954  array[n] = compressedPhotomA->iFringe2[f];
955  fluxAve += array[n];
956  n++;
957  }
958  }
959  fluxAve /= n;
960  cpl_msg_info(cpl_func,"PhotomA DATA2 Average Flux = %f \n", fluxAve);
961  midiCreatePlotFileDouble2D ("PhotomACleanDATA2", "Photometry A DATA2 (Sky Removed)",
962  "Frame", "Flux", 0, array, 0, n, 1);
963 
964  n = 0;
965  fluxAve = 0.0;
966  for (f = 0; f < formatPhotomB->numOfFrames; f++)
967  {
968  if (compressedPhotomB->tarType[f] == 'T')
969  {
970  array[n] = compressedPhotomB->iFringe1[f];
971  fluxAve += array[n];
972  n++;
973  }
974  }
975  fluxAve /= n;
976  cpl_msg_info(cpl_func,"PhotomB DATA1 Average Flux = %f \n", fluxAve);
977  midiCreatePlotFileDouble2D ("PhotomBCleanDATA1", "Photometry B DATA1 (Sky Removed)",
978  "Frame", "Flux", 0, array, 0, n, 1);
979 
980  n = 0;
981  fluxAve = 0.0;
982  for (f = 0; f < formatPhotomB->numOfFrames; f++)
983  {
984  if (compressedPhotomB->tarType[f] == 'T')
985  {
986  array[n] = compressedPhotomB->iFringe2[f];
987  fluxAve += array[n];
988  n++;
989  }
990  }
991  fluxAve /= n;
992  cpl_msg_info(cpl_func,"PhotomB DATA2 Average Flux = %f \n", fluxAve);
993  midiCreatePlotFileDouble2D ("PhotomBCleanDATA2", "Photometry B DATA2 (Sky Removed)",
994  "Frame", "Flux", 0, array, 0, n, 1);
995 
996  free (array);
997  }
998 
999  // Display for each channel
1000  if (plotFile && diagnostic > 4)
1001  {
1002  arrayF = (float *) calloc (formatInterf->numOfFrames, sizeof (float));
1003  fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1004  title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1005 
1006  for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
1007  {
1008  for (X = 0; X < formatInterf->iXWidth; X++)
1009  {
1010  if (badChannelList[X])
1011  continue;
1012 
1013  n = 0;
1014  for (f = 0; f < formatInterf->numOfFrames; f++)
1015  {
1016  if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
1017  {
1018  arrayF[n] = compressedInterf->iDispFringe[R][X][f];
1019  n++;
1020  }
1021  }
1022  sprintf (fileName, "InterfCleanDATA%d_X%d", R+1, X+1);
1023  sprintf (title, "Interferometry DATA%d for Channel %d (Sky Removed)", R+1, X+1);
1024  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 0, arrayF, 0, n, 1, 0);
1025  }
1026  }
1027  for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
1028  {
1029  for (X = 0; X < formatPhotomA->iXWidth; X++)
1030  {
1031  if (badChannelList[X])
1032  continue;
1033 
1034  n = 0;
1035  for (f = 0; f < formatPhotomA->numOfFrames; f++)
1036  {
1037  if (compressedPhotomA->tarType[f] == 'T' && !(compressedPhotomA->rejectList[X][f]))
1038  {
1039  arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
1040  n++;
1041  }
1042  }
1043  sprintf (fileName, "PhotomACleanDATA%d_X%d", R+1, X+1);
1044  sprintf (title, "Photometry A DATA%d for Channel %d (Sky Removed)", R+1, X+1);
1045  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 0, arrayF, 0, n, 1, 0);
1046  }
1047  }
1048  for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
1049  {
1050  for (X = 0; X < formatPhotomB->iXWidth; X++)
1051  {
1052  if (badChannelList[X])
1053  continue;
1054 
1055  n = 0;
1056  for (f = 0; f < formatPhotomB->numOfFrames; f++)
1057  {
1058  if (compressedPhotomB->tarType[f] == 'T' && !(compressedPhotomB->rejectList[X][f]))
1059  {
1060  arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
1061  n++;
1062  }
1063  }
1064  sprintf (fileName, "PhotomBCleanDATA%d_X%d", R+1, X+1);
1065  sprintf (title, "Photometry B DATA%d for Channel %d (Sky Removed)", R+1, X+1);
1066  midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 0, arrayF, 0, n, 1, 0);
1067  }
1068  }
1069  free (fileName);
1070  free (title);
1071  free (arrayF);
1072  }
1073 
1074  // Display the mean flux as a function of the channel
1075 
1076  if (plotFile)
1077  {
1078  arrayF = (float *) calloc (formatInterf->iXWidth, sizeof (float));
1079  fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1080  title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1081 
1082  for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
1083  {
1084  for (X = 0; X < formatInterf->iXWidth; X++)
1085  {
1086  arrayF[X]=0;
1087  if (badChannelList[X])
1088  continue;
1089 
1090  n = 0;
1091  for (f = 0; f < formatInterf->numOfFrames; f++)
1092  {
1093  if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
1094  {
1095  arrayF[X] += compressedInterf->iDispFringe[R][X][f];
1096  n++;
1097  }
1098  }
1099  arrayF[X] /=(float)n;
1100  }
1101 
1102  sprintf (fileName, "MeanInterfCleanDATA%d", R+1);
1103  sprintf (title, "Mean Interferometry DATA%d (Sky Removed)", R+1);
1104  midiCreatePlotFile2D (fileName, title, "Channel", "Flux", 0, arrayF, 0, formatInterf->iXWidth, 1, 0);
1105  }
1106  free (fileName);
1107  free (title);
1108  free (arrayF);
1109  }
1110 
1111 
1112  // Finalise rejection list
1113  finaliseRejectListInterf ("Combined Interferometry", formatInterf, compressedInterf, error);
1114  if (*error) return;
1115  finaliseRejectListPhotom ("Combined Photometry A", formatPhotomA, compressedPhotomA, error);
1116  if (*error) return;
1117  finaliseRejectListPhotom ("Combined Photometry B", formatPhotomB, compressedPhotomB, error);
1118  if (*error) return;
1119 
1120  return;
1121 }
1122 /*****************************************************************************/
1123 
1124 
1125 
1126 /******************************************************************************
1127 * European Southern Observatory
1128 * VLTI MIDI Data Reduction Software
1129 *
1130 * Module name: finaliseRejectListInterf
1131 * Input/Output: See function arguments to avoid duplication
1132 * Description: Finalising the badScanList, rejectList and badChannelList
1133 *
1134 * History:
1135 * 14-Mar-06 (csabet) Created
1136 ******************************************************************************/
1137 void finaliseRejectListInterf (
1138  const char *dataKey, // In: Data type flag
1139  ImageFormat *format, // In: Points to the image format
1140  CompressedData *compressed, // IO: Pointer to the compressed data structure
1141  int *error) // Ou: Error status
1142 {
1143  // Local Declarations
1144  //--------------------
1145  const char routine[] = "finaliseRejectListInterf";
1146  int i, f, s, x, numOfGoodFrames, numOfGoodChannels,
1147  numOfBadScans, minFrames;
1148 
1149  // Algorithm
1150  // ---------
1151  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1152  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
1153 
1154  cpl_msg_info(cpl_func,"\nFinalising Rejection List for %s data \n", dataKey);
1155  cpl_msg_info(cpl_func,"-------------------------------------------------- \n");
1156  fprintf (midiReportPtr, "\nFinalising Rejection List for %s data \n", dataKey);
1157  fprintf (midiReportPtr, "-------------------------------------------------- \n");
1158 
1159  // Reset
1160  *error = 0;
1161  minFrames = MIN_FRAMES_PER_SCAN * format->framesPerScan;
1162 
1163  numOfGoodChannels = 0;
1164  for (x = 0; x < format->iXWidth; x++)
1165  {
1166  if (badChannelList[x])
1167  {
1168  for (f = 0; f < format->numOfFrames; f++)
1169  compressed->rejectList[x][f] |= badChannelList[x];
1170  continue;
1171  }
1172 
1173  numOfBadScans = 0;
1174  for (s = 0; s < format->numOfScans; s++)
1175  {
1176  numOfGoodFrames = 0;
1177  for (i = 0; i < format->framesPerScan; i++)
1178  {
1179  f = i + s * format->framesPerScan;
1180  if (!(compressed->rejectList[x][f]))
1181  numOfGoodFrames++;
1182  }
1183  // If number of good frames in a scan is less than a minimum reject scan
1184  if (numOfGoodFrames < minFrames)
1185  numOfBadScans++;
1186  }
1187  if (numOfBadScans == format->numOfScans)
1188  badChannelList[x] |= BSL_DATA_ERROR;
1189  else
1190  numOfGoodChannels++;
1191  }
1192 
1193  // If no good channels return error
1194  if (!numOfGoodChannels)
1195  {
1196  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "All channels have been rejected");
1197  *error = 1;
1198  }
1199 
1200  return;
1201 }
1202 /*****************************************************************************/
1203 
1204 
1205 /******************************************************************************
1206 * European Southern Observatory
1207 * VLTI MIDI Data Reduction Software
1208 *
1209 * Module name: finaliseRejectListPhotom
1210 * Input/Output: See function arguments to avoid duplication
1211 * Description: Finalising the badScanList, rejectList and badChannelList
1212 * This routine is used for the photometry of the HIGH_SENS
1213 * as concept of scan is meaningless
1214 *
1215 * History:
1216 * 14-Mar-06 (csabet) Created
1217 ******************************************************************************/
1218 void finaliseRejectListPhotom (
1219  const char *dataKey, // In: Data type flag
1220  ImageFormat *format, // In: Points to the image format
1221  CompressedData *compressed, // IO: Pointer to the compressed data structure
1222  int *error) // Ou: Error status
1223 {
1224  // Local Declarations
1225  //--------------------
1226  const char routine[] = "finaliseRejectListPhotom";
1227  int f, x, numOfBadFrames, numOfGoodChannels;
1228 
1229  // Algorithm
1230  // ---------
1231  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1232  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
1233 
1234  cpl_msg_info(cpl_func,"\nFinalising Rejection List for %s data \n", dataKey);
1235  cpl_msg_info(cpl_func,"-------------------------------------------------- \n");
1236  fprintf (midiReportPtr, "\nFinalising Rejection List for %s data \n", dataKey);
1237  fprintf (midiReportPtr, "-------------------------------------------------- \n");
1238 
1239  // Reset
1240  *error = 0;
1241 
1242  numOfGoodChannels = 0;
1243  for (x = 0; x < format->iXWidth; x++)
1244  {
1245  if (badChannelList[x])
1246  {
1247  for (f = 0; f < format->numOfFrames; f++)
1248  compressed->rejectList[x][f] |= badChannelList[x];
1249  continue;
1250  }
1251 
1252  numOfBadFrames = 0;
1253  for (f = 0; f < format->numOfFrames; f++)
1254  {
1255  if (compressed->rejectList[x][f])
1256  numOfBadFrames++;
1257  }
1258  if (numOfBadFrames == format->numOfFrames)
1259  badChannelList[x] |= BSL_DATA_ERROR;
1260  else
1261  numOfGoodChannels++;
1262  }
1263 
1264  // If no good channels return error
1265  if (!numOfGoodChannels)
1266  {
1267  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "All channels have been rejected");
1268  *error = 1;
1269  }
1270 
1271  return;
1272 }
1273 /*****************************************************************************/
1274 
1275 /*----------------------------------------------------------------------------*/
1282 /*----------------------------------------------------------------------------*/
1283 
1284 cpl_error_code midiSaveCorrelatedFlux(IauExchange *iauData,
1285  DispersedResult *dispResult, cpl_parameterlist *parlist,
1286  cpl_frameset *frameset)
1287 {
1288  cpl_table * table=NULL;
1289  cpl_propertylist * qclist=NULL;
1290  char * dataname=NULL;
1291 
1292  table=cpl_table_new(iauData->wavelength->nwave);
1293 
1294  cpl_table_new_column(table, "Wavelength", CPL_TYPE_FLOAT);
1295  cpl_table_new_column(table, "CorrelatedFlux", CPL_TYPE_FLOAT);
1296  cpl_table_new_column(table, "CorrelatedFluxError", CPL_TYPE_FLOAT);
1297  cpl_table_new_column(table, "Flag", CPL_TYPE_INT);
1298 
1299  cpl_table_set_column_unit(table ,"Wavelength", "m");
1300  cpl_table_set_column_unit(table, "CorrelatedFlux", "ADU");
1301  cpl_table_set_column_unit(table, "CorrelatedFluxError", "ADU");
1302 
1303  cpl_table_copy_data_float(table,"Wavelength",iauData->wavelength->eff_wave);
1304  cpl_table_copy_data_float(table,"CorrelatedFlux",dispResult->rawVis2);
1305  cpl_table_copy_data_float(table,"CorrelatedFluxError",dispResult->rawVis2Err);
1306  cpl_table_copy_data_int(table,"Flag",badChannelList);
1307  /*
1308  cpl_table_dump_structure(table,NULL);
1309  cpl_table_dump(table,0,200,NULL);
1310  */
1311  qclist=cpl_propertylist_new();
1312 
1313  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "CORRELATED_FLUX");
1314  dataname=cpl_sprintf("MIDI_CorrelatedFlux.fits");
1315 
1316  cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, table,
1317  qclist, "midi_fringe_all",
1318  qclist, NULL,
1319  PACKAGE "/" PACKAGE_VERSION,
1320  dataname);
1321 
1322  cpl_propertylist_delete(qclist);
1323  cpl_free(dataname);
1324  cpl_table_delete(table);
1325 
1326  if(cpl_error_get_code()!=CPL_ERROR_NONE)
1327  {
1328  cpl_msg_error(cpl_func,"Correlated flux can not be saved!");
1329  return cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_CREATED);
1330  }
1331  else {
1332  return CPL_ERROR_NONE;
1333  }
1334 }
1335 
1336 /*****************************************************************************/
1337