MIDI Pipeline Reference Manual  2.8.3
fitsAnalysisFrgHS.c
1 
2 /******************************************************************************
3 *******************************************************************************
4 * European Southern Observatory
5 * VLTI MIDI Maintenance Templates Software
6 *
7 * Module name: fitsAnalysisFrgHS.c
8 * Description: Contains all routines for FITS format analysis
9 *
10 * History:
11 * 14-Jun-02 (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 "midiGlobal.h"
25 #include "midiLib.h"
26 #include "fft.h"
27 #include "errorHandling.h"
28 #include "memoryHandling.h"
29 #include "midiFitsUtility.h"
30 #include "fitsAnalysisFrgHS.h"
31 
32 /**********************************************************
33 * Global Variables
34 **********************************************************/
35 
36 
37 
38 /*============================ C O D E A R E A ===========================*/
39 
40 
41 /******************************************************************************
42 * European Southern Observatory
43 * VLTI MIDI Data Reduction Software
44 *
45 * Module name: analyseFitsFrgHS
46 * Input/Output: See function arguments to avoid duplication
47 * Description: Analyses all the FITS files in the given list. This routine
48 * determines if the FITS list contains useful data files. If
49 * not it will discard the list. This routine is used in GIGH_SENS
50 * mode
51 *
52 * History:
53 * 22-Jul-03 (csabet) Created
54 ******************************************************************************/
55 void analyseFitsFrgHS (
56  MidiFiles *fileNames, // IO: Pointer to midi files structure
57  ImageFormat *formatInterf, // Ou: Size for Interferometry file
58  ImageFormat *formatPhotomA, // Ou: Size for Photometry A file
59  ImageFormat *formatPhotomB, // Ou: Size for Photometry B file
60  int *error) // Ou: Error status
61 {
62 
63  // Local Declarations
64  // ------------------
65  const char routine[] = "analyseFitsFrgHS";
66  char *fileTemp, *classification;
67  FILE *inFitsBatchPtr=NULL;
68  int extNumOfImagingDataFile, aprioriDataIsDisp=0;
69  ImageFormat *localFormat=NULL;
70  char *cleanString=NULL, *aprioriCatg=NULL;
71  unsigned int numOfInterf, numOfPhotomA, numOfPhotomB, numOfAcq;
72  ImageFormat *formatAcq;
73 
74  // Algorithm
75  // ---------
76  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
77  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
78 
79  cpl_msg_info(cpl_func,"\nAnalysing batch %d \n", batchNumber);
80  cpl_msg_info(cpl_func,"--------------- \n");
81  fprintf (midiReportPtr, "\nAnalysing batch %d \n", batchNumber);
82  fprintf (midiReportPtr, "--------------- \n");
83 
84  // Reset status
85  *error = 0;
86 
87  // Allocate memory for a temporary file name buffer
88  classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
89  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
90  fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
91  aprioriCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
92  localFormat = callocImageFormat ();
93  formatAcq = 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  freeImageFormat (formatAcq);
104  free (classification);
105  *error = 1;
106  return;
107  }
108 
109  // Reset image format
110  numOfInterf = 0;
111  midiInitFormat (formatInterf);
112  numOfPhotomA = 0;
113  midiInitFormat (formatPhotomA);
114  numOfPhotomB = 0;
115  midiInitFormat (formatPhotomB);
116  numOfAcq = 0;
117  midiInitFormat (formatAcq);
118 
119  // Loop through the files and analyse
120  while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
121  {
122  sprintf (classification, "%s", "");
123  sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
124  if (diagnostic)cpl_msg_info(cpl_func,"\nAnalysing file %s \n", fileNames->inFitsName);
125  fprintf(midiReportPtr, "\nAnalysing file %s \n", fileNames->inFitsName);
126 
127  // Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
128  extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
129  if (*error) break;
130 
131  // Get Image Format parameters
132  if (extNumOfImagingDataFile > 0)
133  {
134  getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
135  if (*error) break;
136  }
137  else localFormat->hasData = 0;
138 
139  // Check if the file has data
140  if (localFormat->hasData)
141  {
142  // If it is an aquisition save parameters and exit
143  if (((strcmp (localFormat->obsCatg, "CALIB") == 0) ||
144  (strcmp (localFormat->obsCatg, "SCIENCE") == 0)) &&
145  ((strcmp (localFormat->obsTech, "IMAGE,WINDOW") == 0) ||
146  (strcmp (localFormat->obsType, "COARSE,OBJECT") == 0)))
147  {
148  // Save local parameters
149  formatAcq->hasData = 1;
150  formatAcq->chopped = 0;
151  midiCopyFormat (localFormat, formatAcq);
152 
153  // Get cummulative results
154  formatAcq->numOfFrames += localFormat->numOfFrames;
155 
156  // Increment file counter
157  numOfAcq++;
158  break;
159  }
160 
161  // Save initial Beam Combiner and Category
162  if (numOfPhotomA == 0 && numOfPhotomA == 0 && numOfInterf == 0)
163  {
164  aprioriDataIsDisp = localFormat->isDisp;
165  sprintf (aprioriCatg, "%s", localFormat->obsCatg);
166  }
167 
168  // Check Beam Combiner consistency
169  if (localFormat->isDisp != aprioriDataIsDisp)
170  {
171  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent Beam Combiner");
172  *error = 1;
173  break;
174  }
175  else
176  aprioriDataIsDisp = localFormat->isDisp;
177 
178  // Check Category consistency
179  if (strcmp (localFormat->obsCatg, aprioriCatg) != 0)
180  {
181  sprintf (midiMessage, "Inconsistent Observation Category. Expected: %s", aprioriCatg);
182  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
183  *error = 1;
184  break;
185  }
186  else
187  sprintf (aprioriCatg, "%s", localFormat->obsCatg);
188 
189  // Check Categ, Tech and Type. If INTERFEROMETRY then compute the image size
190  if (strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0)
191  {
192  // Save local parameters
193  if (numOfInterf == 0)
194  {
195  formatInterf->hasData = 1;
196  formatInterf->chopped = 0;
197  midiCopyFormat (localFormat, formatInterf);
198  }
199 
200  // Get cummulative results
201  formatInterf->numOfFrames += localFormat->numOfFrames;
202 
203  // Increment file counter
204  numOfInterf++;
205  }
206  // If it is a Photometry file, it could either be HIGH_SENS or SCI_PHOT_KAPPA
207  else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
208  (strcmp(localFormat->shutterId,"AOPEN") == 0))
209  {
210  // Save local parameters
211  if (numOfPhotomA == 0)
212  {
213  formatPhotomA->hasData = 1;
214  formatPhotomA->chopped = 1;
215  midiCopyFormat (localFormat, formatPhotomA);
216  }
217 
218  // Get cummulative results
219  formatPhotomA->numOfFrames += localFormat->numOfFrames;
220 
221  // Increment file counter
222  numOfPhotomA++;
223  }
224  else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
225  (strcmp(localFormat->shutterId,"BOPEN") == 0))
226  {
227  // Save local parameters
228  if (numOfPhotomB == 0)
229  {
230  formatPhotomB->hasData = 1;
231  formatPhotomB->chopped = 1;
232  midiCopyFormat (localFormat, formatPhotomB);
233  }
234 
235  // Get cummulative results
236  formatPhotomB->numOfFrames += localFormat->numOfFrames;
237 
238  // Increment file counter
239  numOfPhotomB++;
240  }
241  else
242  {
243  sprintf (midiMessage, "Unknown Category, Tech or Type in ... %s. Not processed", fileNames->inFitsName);
244  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
245  }
246  }
247  else
248  {
249  if (diagnostic)
250  {
251  sprintf (midiMessage, "No data tables in ... %s. Not processed", fileNames->inFitsName);
252  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
253  }
254  }
255  }
256 
257  // Close the file list
258  if (fclose (inFitsBatchPtr))
259  midiReportError (midiReportPtr, routine, __FILE__, __LINE__, "Cannot close batch file");
260 
261  // Check if the FITS files contained useful data
262  if (!(*error) && (numOfPhotomA > 0) &&
263  (numOfPhotomB > 0) && (numOfInterf > 0) &&
264  (numOfPhotomA == numOfPhotomB))
265  {
266  // Compute number of scans
267  formatInterf->numOfScans = formatInterf->numOfFrames / formatInterf->framesPerScan;
268 
269  // Compute default FFT size
270  formatInterf->fftsize = determineFFTsize (formatInterf->framesPerScan);
271 
272  // Compute length of allSpectrum
273  formatInterf->allSpectrumLength = REGIONS_UNDISPERSED * formatInterf->numOfScans * (formatInterf->fftsize/2);
274 
275  // Log results
276  midiReportFormat ("INTERFEROMETRY", aprioriDataIsDisp, formatInterf);
277  midiReportFormat ("PHOTOMETRY A", aprioriDataIsDisp, formatPhotomA);
278  midiReportFormat ("PHOTOMETRY B", aprioriDataIsDisp, formatPhotomB);
279 
280  sprintf (midiMessage, "%d useful Photometry and %d useful Interferometry files detected",
281  numOfPhotomA+numOfPhotomB, numOfInterf);
282  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
283  }
284  else if (!(*error) && (numOfAcq > 0))
285  {
286  // Compute number of scans
287  formatAcq->numOfScans = formatAcq->numOfFrames / formatAcq->framesPerScan;
288 
289  // Log results
290  midiReportAcqFormat (formatAcq);
291  }
292  else
293  {
294  sprintf (fileTemp, "%d", batchNumber);
295  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent batch files");
296  *error = 1;
297  }
298 
299  // Check if requested processing is valid. For now just report warning.
300  if (!(*error))
301  {
302  if (!numOfAcq)
303  {
304  if (!aprioriDataIsDisp)
305  {
306  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
307  "Data is UNDISPERSED\n Algorithm for processing this type of data is not ready");
308  *error = 1;
309  }
310  }
311  }
312 
313  // Release memory
314  if (fileTemp) free (fileTemp);
315  if (localFormat) freeImageFormat (localFormat);
316  if (formatAcq) freeImageFormat (formatAcq);
317  if (cleanString) free (cleanString);
318  if (classification) free (classification);
319  if (aprioriCatg) free (aprioriCatg);
320 
321  return;
322 }
323 /******************************************************************************/
324 
325 
326 
327 /******************************************************************************
328 * European Southern Observatory
329 * VLTI MIDI Maintenance Templates Software
330 *
331 * Module name: midiReportAcqFormat
332 * Input/Output: See function arguments to avoid duplication
333 * Description: Reports image format
334 *
335 * History:
336 * 10-Jul-05 (csabet) created
337 ******************************************************************************/
338 void midiReportAcqFormat (
339  ImageFormat *formatAcq) // In: Pointer to the image format structure
340 {
341 
342  // Local Declarations
343  // ------------------
344  const char routine[] = "midiReportAcqFormat";
345 
346  // Algorithm
347  // ---------
348  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
349  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
350 
351  cpl_msg_info(cpl_func,"ACQUISITION FORMAT \n");
352  cpl_msg_info(cpl_func,"------------------ \n");
353  cpl_msg_info(cpl_func,"FITS Template Name = %s \n", formatAcq->tplName);
354  cpl_msg_info(cpl_func,"Batch Template Name = %s \n", batchTemplate);
355  cpl_msg_info(cpl_func,"Observation Category = %s \n", formatAcq->obsCatg);
356  cpl_msg_info(cpl_func,"Observation Technique = %s \n", formatAcq->obsTech);
357  cpl_msg_info(cpl_func,"Observation Type = %s \n", formatAcq->obsType);
358  cpl_msg_info(cpl_func,"Camera ID = %s \n", formatAcq->cameraId);
359  cpl_msg_info(cpl_func,"Beam Combiner = %s \n", formatAcq->beamCombiner);
360  cpl_msg_info(cpl_func,"Filter ID = %s \n", formatAcq->filterName);
361  cpl_msg_info(cpl_func,"Shutter ID = %s \n", formatAcq->shutterId);
362  cpl_msg_info(cpl_func,"Grism ID = %s \n", formatAcq->grismId);
363  cpl_msg_info(cpl_func,"Slit Name = %s \n", formatAcq->slitName);
364  cpl_msg_info(cpl_func,"Target Name = %s \n", formatAcq->targetName);
365  cpl_msg_info(cpl_func,"TPL Start = %s \n", formatAcq->tplStart);
366  cpl_msg_info(cpl_func,"Number of Frames = %d \n", formatAcq->numOfFrames);
367  cpl_msg_info(cpl_func,"Frames per scan = %d \n", formatAcq->framesPerScan);
368  cpl_msg_info(cpl_func,"Number of Scans = %d \n", formatAcq->numOfScans);
369  cpl_msg_info(cpl_func,"X, Column dimension = %d \n", formatAcq->iXWidth);
370  cpl_msg_info(cpl_func,"Y, Column dimension = %d \n", formatAcq->iYWidth);
371  cpl_msg_info(cpl_func,"Sub-window size = %d \n", formatAcq->subWindowSize);
372  cpl_msg_info(cpl_func,"Number of Pinholes = %d \n", formatAcq->numOfPinholes);
373  cpl_msg_info(cpl_func,"Number of Detector Regions = %d \n", formatAcq->numOfDetectorRegions);
374  cpl_msg_info(cpl_func,"\n");
375 
376  fprintf (midiReportPtr, "ACQUISITION FORMAT \n");
377  fprintf (midiReportPtr, "------------------ \n");
378  fprintf (midiReportPtr, "FITS Template Name = %s \n", formatAcq->tplName);
379  fprintf (midiReportPtr, "Batch Template Name = %s \n", batchTemplate);
380  fprintf (midiReportPtr, "Observation Category = %s \n", formatAcq->obsCatg);
381  fprintf (midiReportPtr, "Observation Technique = %s \n", formatAcq->obsTech);
382  fprintf (midiReportPtr, "Observation Type = %s \n", formatAcq->obsType);
383  fprintf (midiReportPtr, "Camera ID = %s \n", formatAcq->cameraId);
384  fprintf (midiReportPtr, "Beam Combiner = %s \n", formatAcq->beamCombiner);
385  fprintf (midiReportPtr, "Filter ID = %s \n", formatAcq->filterName);
386  fprintf (midiReportPtr, "Shutter ID = %s \n", formatAcq->shutterId);
387  fprintf (midiReportPtr, "Grism ID = %s \n", formatAcq->grismId);
388  fprintf (midiReportPtr, "Slit Name = %s \n", formatAcq->slitName);
389  fprintf (midiReportPtr, "Target Name = %s \n", formatAcq->targetName);
390  fprintf (midiReportPtr, "TPL Start = %s \n", formatAcq->tplStart);
391  fprintf (midiReportPtr, "Number of Frames = %d \n", formatAcq->numOfFrames);
392  fprintf (midiReportPtr, "Frames per scan = %d \n", formatAcq->framesPerScan);
393  fprintf (midiReportPtr, "Number of Scans = %d \n", formatAcq->numOfScans);
394  fprintf (midiReportPtr, "X, Column dimension = %d \n", formatAcq->iXWidth);
395  fprintf (midiReportPtr, "Y, Column dimension = %d \n", formatAcq->iYWidth);
396  fprintf (midiReportPtr, "Sub-window size = %d \n", formatAcq->subWindowSize);
397  fprintf (midiReportPtr, "Number of Pinholes = %d \n", formatAcq->numOfPinholes);
398  fprintf (midiReportPtr, "Number of Detector Regions = %d \n", formatAcq->numOfDetectorRegions);
399  fprintf (midiReportPtr, "\n");
400 
401  return;
402 }
403 /******************************************************************************/
404 
405