MIDI Pipeline Reference Manual  2.8.3
fitsAnalysisKappa.c
1 
2 /******************************************************************************
3 *******************************************************************************
4 * European Southern Observatory
5 * VLTI MIDI Maintenance Templates Software
6 *
7 * Module name: fitsAnalysisKappa.c
8 * Description: Contains all the routines for FITS format analysis related to
9 * Kappa computation
10 *
11 * History:
12 * 05-Dec-05 (csabet) created
13 *******************************************************************************
14 ******************************************************************************/
15 
16 /******************************************************************************
17 * Compiler directives
18 ******************************************************************************/
19 
20 /******************************************************************************
21 * Include files
22 ******************************************************************************/
23 #include <stdio.h>
24 #include <cpl.h>
25 #include <math.h>
26 #include "midiGlobal.h"
27 #include "midiLib.h"
28 #include "fft.h"
29 #include "errorHandling.h"
30 #include "memoryHandling.h"
31 #include "midiFitsUtility.h"
32 #include "fitsAnalysisKappa.h"
33 
34 /**********************************************************
35 * Global Variables
36 **********************************************************/
37 
38 
39 
40 /*============================ C O D E A R E A ===========================*/
41 
42 
43 
44 
45 /******************************************************************************
46 * European Southern Observatory
47 * VLTI MIDI Data Reduction Software
48 *
49 * Module name: analyseFitsKappa
50 * Input/Output: See function arguments to avoid duplication
51 * Description: Analyses all the FITS files in the given list. This routine
52 * determines if the FITS list contains useful data files. If
53 * not it will discard the list.
54 *
55 * History:
56 * 05-Dec-05 (csabet) Created
57 ******************************************************************************/
58 void analyseFitsKappa (
59  MidiFiles *fileNames, // IO: Pointer to midi files structure
60  ImageFormat *formatInterfA, // Ou: Size for Interferometry file
61  ImageFormat *formatInterfB, // Ou: Size for Interferometry file
62  ImageFormat *formatPhotomA, // Ou: Size for Photometry A file
63  ImageFormat *formatPhotomB, // Ou: Size for Photometry B file
64  int *error) // Ou: Error status
65 {
66 
67  // Local Declarations
68  // ------------------
69  const char routine[] = "analyseFitsKappa";
70  char *fileTemp, *classification;
71  FILE *inFitsBatchPtr=NULL;
72  int extNumOfImagingDataFile, aprioriDataIsDisp=0;
73  ImageFormat *localFormat=NULL;
74  char *cleanString=NULL, *aprioriCatg=NULL;
75  unsigned int numOfPhotomA, numOfPhotomB;
76 
77  // Algorithm
78  // ---------
79  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
80  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
81 
82  cpl_msg_info(cpl_func,"\nAnalysing batch %d \n", batchNumber);
83  cpl_msg_info(cpl_func,"--------------- \n");
84  fprintf (midiReportPtr, "\nAnalysing batch %d \n", batchNumber);
85  fprintf (midiReportPtr, "--------------- \n");
86 
87  // Reset status
88  *error = 0;
89 
90  // Allocate memory for a temporary file name buffer
91  classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
92  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
93  fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
94  aprioriCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
95  localFormat = callocImageFormat ();
96 
97  // Open the list of files
98  if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
99  {
100  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
101  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "No analysis has been carried out for this batch");
102  free (fileTemp);
103  free (cleanString);
104  freeImageFormat (localFormat);
105  free (classification);
106  *error = 1;
107  return;
108  }
109 
110  // Reset image format
111  numOfPhotomA = 0;
112  midiInitFormat (formatPhotomA);
113  numOfPhotomB = 0;
114  midiInitFormat (formatPhotomB);
115 
116  // Loop through the files and analyse
117  while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
118  {
119  sprintf (classification, "%s", "");
120  sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
121  if (diagnostic)cpl_msg_info(cpl_func,"\nAnalysing file %s \n", fileNames->inFitsName);
122  fprintf(midiReportPtr, "\nAnalysing file %s \n", fileNames->inFitsName);
123 
124  // Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
125  extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
126  if (*error) break;
127 
128  // Get Image Format parameters
129  if (extNumOfImagingDataFile > 0)
130  {
131  getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
132  if (*error) break;
133  }
134  else localFormat->hasData = 0;
135 
136  // Check if the file has data
137  if (localFormat->hasData)
138  {
139  // Save initial Beam Combiner and Category
140  if (numOfPhotomA == 0 && numOfPhotomA == 0)
141  {
142  aprioriDataIsDisp = localFormat->isDisp;
143  sprintf (aprioriCatg, "%s", localFormat->obsCatg);
144  }
145 
146  // Check Beam Combiner consistency
147  if (localFormat->isDisp != aprioriDataIsDisp)
148  {
149  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent Beam Combiner");
150  *error = 1;
151  break;
152  }
153  else
154  aprioriDataIsDisp = localFormat->isDisp;
155 
156  // Check Category consistency
157  if (strcmp (localFormat->obsCatg, aprioriCatg) != 0)
158  {
159  sprintf (midiMessage, "Inconsistent Observation Category. Expected: %s", aprioriCatg);
160  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
161  *error = 1;
162  break;
163  }
164  else
165  sprintf (aprioriCatg, "%s", localFormat->obsCatg);
166 
167  // Check Tech and Shutter ID
168  if (strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0)
169  {
170  if (diagnostic > 1) midiReportInfo (midiReportPtr,
171  routine, __FILE__, __LINE__, "The above Interferometry file will not be processed");
172  }
173  else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
174  (strcmp(localFormat->shutterId,"AOPEN") == 0))
175  {
176  // Save local parameters
177  if (numOfPhotomA == 0)
178  {
179  formatPhotomA->hasData = 1;
180  formatPhotomA->chopped = 1;
181  midiCopyFormat (localFormat, formatPhotomA);
182  formatPhotomA->numOfRegionsToProcess = 1;
183  formatInterfA->hasData = 1;
184  formatInterfA->chopped = 1;
185  midiCopyFormat (localFormat, formatInterfA);
186  }
187 
188  // Get cummulative results
189  formatPhotomA->numOfFrames += localFormat->numOfFrames;
190  formatInterfA->numOfFrames += localFormat->numOfFrames;
191 
192  // Increment file counter
193  numOfPhotomA++;
194  }
195  else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
196  (strcmp(localFormat->shutterId,"BOPEN") == 0))
197  {
198  // Save local parameters
199  if (numOfPhotomB == 0)
200  {
201  formatPhotomB->hasData = 1;
202  formatPhotomB->chopped = 1;
203  midiCopyFormat (localFormat, formatPhotomB);
204  formatPhotomB->numOfRegionsToProcess = 1;
205  formatInterfB->hasData = 1;
206  formatInterfB->chopped = 1;
207  midiCopyFormat (localFormat, formatInterfB);
208  }
209 
210  // Get cummulative results
211  formatPhotomB->numOfFrames += localFormat->numOfFrames;
212  formatInterfB->numOfFrames += localFormat->numOfFrames;
213 
214  // Increment file counter
215  numOfPhotomB++;
216  }
217  else
218  {
219  sprintf (midiMessage, "Unknown Category, Tech or Type in ... %s. Not processed",
220  fileNames->inFitsName);
221  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
222  }
223  }
224  else
225  {
226  if (diagnostic)
227  {
228  sprintf (midiMessage, "No data tables in ... %s. Not processed", fileNames->inFitsName);
229  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
230  }
231  }
232  }
233 
234  // Close the file list
235  if (fclose (inFitsBatchPtr))
236  midiReportError (midiReportPtr, routine, __FILE__, __LINE__, "Cannot close batch file");
237 
238  // Check if the FITS files contained useful data
239  if (!(*error) && (numOfPhotomA > 0) && (numOfPhotomB > 0) && (numOfPhotomA == numOfPhotomB))
240  {
241  // Check potential singularities
242  if ((formatInterfA->framesPerScan == 0) || (formatInterfB->framesPerScan == 0) ||
243  (formatPhotomA->framesPerScan == 0) || (formatPhotomB->framesPerScan == 0))
244  {
245  if (fileTemp) free (fileTemp);
246  if (localFormat) freeImageFormat (localFormat);
247  if (cleanString) free (cleanString);
248  if (classification) free (classification);
249  if (aprioriCatg) free (aprioriCatg);
250  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Potential singularity detected");
251  *error = 1;
252  return;
253  }
254 
255  // Compute number of scans
256  formatInterfA->numOfScans = formatInterfA->numOfFrames / formatInterfA->framesPerScan;
257  formatInterfB->numOfScans = formatInterfB->numOfFrames / formatInterfB->framesPerScan;
258  formatPhotomA->numOfScans = formatPhotomA->numOfFrames / formatPhotomA->framesPerScan;
259  formatPhotomB->numOfScans = formatPhotomB->numOfFrames / formatPhotomB->framesPerScan;
260 
261  // Make sure numberOfFrames is a muliple number of the framesPerScan
262  formatInterfA->numOfFrames = formatInterfA->numOfScans * formatInterfA->framesPerScan;
263  formatInterfB->numOfFrames = formatInterfB->numOfScans * formatInterfB->framesPerScan;
264  formatPhotomA->numOfFrames = formatPhotomA->numOfScans * formatPhotomA->framesPerScan;
265  formatPhotomB->numOfFrames = formatPhotomB->numOfScans * formatPhotomB->framesPerScan;
266 
267  // Compute default FFT size
268  formatInterfA->fftsize = determineFFTsize (formatInterfA->framesPerScan);
269  formatInterfB->fftsize = determineFFTsize (formatInterfB->framesPerScan);
270 
271  // Compute length of allSpectrum
272  formatInterfA->allSpectrumLength = REGIONS_UNDISPERSED * formatInterfA->numOfScans * (formatInterfA->fftsize/2);
273  formatInterfB->allSpectrumLength = REGIONS_UNDISPERSED * formatInterfB->numOfScans * (formatInterfB->fftsize/2);
274 
275  // Log results
276  midiReportFormat ("INTERFEROMETRY A", aprioriDataIsDisp, formatInterfA);
277  midiReportFormat ("INTERFEROMETRY B", aprioriDataIsDisp, formatInterfB);
278  midiReportFormat ("PHOTOMETRY A", aprioriDataIsDisp, formatPhotomA);
279  midiReportFormat ("PHOTOMETRY B", aprioriDataIsDisp, formatPhotomB);
280 
281  sprintf (midiMessage, "%d useful Photometry A and %d useful Photometry B files detected",
282  numOfPhotomA, numOfPhotomB);
283  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
284  }
285  else
286  {
287  sprintf (fileTemp, "%d", batchNumber);
288  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent batch files");
289  *error = 1;
290  }
291 
292  // Check if requested processing is valid. For now just report warning.
293  if (!(*error))
294  {
295  if (!aprioriDataIsDisp)
296  {
297  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
298  "Data is UNDISPERSED\n Algorithm for processing this type of data is not ready");
299  *error = 1;
300  }
301  }
302 
303  // Release memory
304  if (fileTemp) free (fileTemp);
305  if (localFormat) freeImageFormat (localFormat);
306  if (cleanString) free (cleanString);
307  if (classification) free (classification);
308  if (aprioriCatg) free (aprioriCatg);
309 
310  return;
311 }
312 /******************************************************************************/
313