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