MIDI Pipeline Reference Manual  2.8.3
midiFitsClassification.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: midiFitsClassification.c
7 * Description: Contains routines for file classification
8 *
9 * History:
10 * 28-Apr-05 (csabet) Created
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /******************************************************************************
15 * Compiler directives
16 ******************************************************************************/
17 // Search for TO_DO_MIDI_DRS
18 #define DEBUG_FITS_FILES_x
19 
20 /******************************************************************************
21 * Include files
22 ******************************************************************************/
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <math.h>
28 #include "midiGlobal.h"
29 #include "midiLib.h"
30 #include "memoryHandling.h"
31 #include "qfits.h"
32 #include "qfits_rw.h"
33 #include "diagnostics.h"
34 #include "errorHandling.h"
35 #include "fileHandling.h"
36 #include "midiFitsClassification.h"
37 #include "midiGlobal.h"
38 #include <cpl.h>
39 /**********************************************************
40 * Constant definitions
41 **********************************************************/
42 
43 
44 /**********************************************************
45 * Global Variables
46 **********************************************************/
47 
48 /*============================ C O D E A R E A ===========================*/
49 
50 
51 
52 /******************************************************************************
53 * European Southern Observatory
54 * VLTI MIDI Data Reduction Software
55 *
56 * Module name: classifyFitsFiles
57 * Input/Output: See function arguments to avoid duplication
58 * Description: It loops through the given list of FITS files and puts the
59 * keyword END_OF_BATCH at the end of a batch of related files. A batch
60 * of files will contain a set of related split Interferometry files
61 * as well as the associated photometry files.
62 *
63 * History:
64 * 10-Oct-03 (csabet) Created
65 ******************************************************************************/
66 void classifyFitsFiles (
67  UserOptions *options, // In: User options
68  MidiFiles *fileNames, // IO: Pointer to midi files structure
69  FILE *filePtr,
70  int *error, // Ou: Error status. 1=Info, 2=Error
71  cpl_parameterlist *parlist,
72  cpl_frameset *frameset)
73 
74 {
75 
76  // Local Declarations
77  // ------------------
78  const char routine[] = "classifyFitsFiles";
79  int stringLength, fileNumInList, batchNum = 0, fileCount, localError;
80  FILE *inFitsClassifiedPtr=NULL, *inFitsTempPtr=NULL;
81  char *inFitsTemp=NULL, *stringTemp=NULL,
82  *fileNameStr=NULL, *classification=NULL;
83  ImageFormat *format=NULL;
84 
85  // Algorithm
86  // ---------
87  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
88  if (diagnostic > 4) fprintf (filePtr, "Invoking routine '%s' \n", routine);
89 
90  cpl_msg_info(cpl_func,"\nClassifying input FITS files \n");
91  cpl_msg_info(cpl_func,"---------------------------- \n");
92  fprintf (filePtr, "\nClassifying input FITS files \n");
93  fprintf (filePtr, "---------------------------- \n");
94 
95  // Reset status
96  *error = 0;
97  localError = 0;
98 
99  // Allocate memory
100  fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
101  classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
102  inFitsTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
103  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
104  format = callocImageFormat ();
105 
106  // Open the list of files for reading
107  if ((inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "r")) == NULL)
108  {
109  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
110  *error = 2;
111  free (stringTemp);
112  free (inFitsTemp);
113  free (classification);
114  free (fileNameStr);
115  freeImageFormat (format);
116  return;
117  }
118 
119 #ifdef DEBUG_FITS_FILES
120  fileNumInList = 0;
121  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
122  {
123  fileNumInList++;
124  sscanf (stringTemp, "%s%s", fileNameStr, classification);
125  getFitsClassification (fileNumInList, filePtr, fileNameStr, format, error);
126  }
127  fclose (filePtr);
128  exit (0);
129 #endif
130 
131  // Open a main temporary file for writing
132  stringLength = strlen (fileNames->inFitsClassified);
133  strncpy (inFitsTemp, fileNames->inFitsClassified, stringLength-4);
134  strcat (inFitsTemp, "Buffer.log");
135  if ((inFitsTempPtr = fopen (inFitsTemp, "w")) == NULL)
136  {
137  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a temporary batch file");
138  *error = 2;
139  fclose (inFitsClassifiedPtr);
140  free (stringTemp);
141  free (inFitsTemp);
142  free (classification);
143  free (fileNameStr);
144  freeImageFormat (format);
145  return;
146  }
147 
148  // Loop through the list of FITS files and classify
149  fileNumInList = 0;
150  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
151  {
152  // Extract name and classification of the input FITS file
153  sprintf (classification, "%s", "");
154  sscanf (stringTemp, "%s%s", fileNameStr, classification);
155 
156  // Increment file number
157  fileNumInList++;
158 
159  // Get FITS file parameters
160  getFitsClassification (fileNumInList, filePtr, fileNameStr, format, &localError);
161  if (localError) continue;
162 
163  // Check template of the subsequent first file
164  if (((strcmp (format->obsCatg, "CALIB") == 0) ||
165  (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
166  ((strcmp (format->obsTech, "INTERFEROMETRY") == 0) ||
167  (strcmp (format->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0)) &&
168  ((strcmp (format->obsType, "TRACK,OBJECT,DISPERSED") == 0) ||
169  (strcmp (format->obsType, "TRACK,OBJECT,DISPERSED,SCIPHOT") == 0) ||
170 
171  (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER") == 0) ||
172  (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER,SCI_PHOT") == 0) ||
173 
174  (strcmp (format->obsType, "PHOTOMETRY,OBJECT,SCIPHOT") == 0) ||
175  (strcmp (format->obsType, "PHOTOMETRY,OBJECT") == 0)) &&
176  (format->fileNumInTemplate == 1))
177  {
178  if (options->drsFrg) getFringeTrackBatch (format, inFitsTempPtr, inFitsClassifiedPtr,
179  fileNameStr, classification, filePtr, &batchNum, &fileNumInList, error, parlist, frameset);
180  }
181  else if (((strcmp (format->obsCatg, "CALIB") == 0) ||
182  (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
183  ((strcmp (format->obsTech, "IMAGE,WINDOW") == 0) ||
184  (strcmp (format->obsType, "COARSE,OBJECT") == 0)))
185  {
186  if (options->drsAcq) getAcquisitionBatch (inFitsTempPtr, fileNameStr, classification,
187  filePtr, &batchNum, error);
188  }
189  else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
190  ((strcmp (format->obsTech, "IMAGE") == 0) ||
191  (strcmp (format->obsTech, "SPECTRUM") == 0)) &&
192  (strcmp (format->obsType, "FLAT") == 0) &&
193  (format->fileNumInTemplate == 1))
194  {
195  if (options->tecLin) getDetLinBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
196  classification, filePtr, &batchNum, &fileNumInList, error);
197  }
198  else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
199  (strcmp (format->obsTech, "IMAGE") == 0) &&
200  (strcmp (format->obsType, "BIAS") == 0) &&
201  (format->fileNumInTemplate == 1))
202  {
203  if (options->tecRon) getDetRonBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
204  classification, filePtr, &batchNum, &fileNumInList, error);
205  }
206  else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
207  (strcmp (format->obsTech, "SPECTRUM") == 0) &&
208  (strcmp (format->obsType, "WAVE") == 0) &&
209  (format->fileNumInTemplate == 1))
210  {
211  if (options->tecTrn) getDspTrnBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
212  classification, filePtr, &batchNum, &fileNumInList, error);
213  }
214  else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
215  (strcmp (format->obsTech, "IMAGE") == 0) &&
216  (strcmp (format->obsType, "FMTCHCK") == 0) &&
217  (format->fileNumInTemplate == 1))
218  {
219  if (options->tecPix) getRefPixBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
220  classification, filePtr, &batchNum, &fileNumInList, error);
221  }
222  else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
223  (strcmp (format->obsTech, "SPECTRUM") == 0) &&
224  (strcmp (format->obsType, "WAVE,SPECTEMPL") == 0) &&
225  (format->fileNumInTemplate == 1))
226  {
227  if (options->tecWav) getWavCalBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
228  classification, filePtr, &batchNum, &fileNumInList, error);
229  }
230  else
231  {
232  continue;
233  }
234  }
235 
236  // Copy the content of the main temporary file into the main list
237  if (inFitsTempPtr) fclose (inFitsTempPtr);
238  if (inFitsClassifiedPtr)
239  {
240  fclose (inFitsClassifiedPtr);
241  inFitsClassifiedPtr = NULL;
242  }
243  if ((inFitsTempPtr = fopen (inFitsTemp, "r")) == NULL)
244  {
245  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list for reading");
246  *error = 2;
247  }
248  else
249  {
250  // Check if the file is empty
251  if (fscanf (inFitsTempPtr, "%s\n", stringTemp) == EOF)
252  {
253  if (options->drsFrg || options->drsAcq || options->tecRon || options->tecLin ||
254  options->tecTrn || options->tecPix || options->tecWav)
255  {
256  *error = 2;
257  sprintf (midiMessage, "%s\n\t %s\n\t\t %s\n\t\t %s",
258  "Cannot find a complete set of FITS files in the given list or directory.",
259  "Check the following log files:", fileNames->inFitsClassified, inFitsTemp);
260  midiReportWarning (filePtr, routine, __FILE__, __LINE__, midiMessage);
261  }
262  else
263  {
264  *error = 1;
265  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, "No Batch Classification was requested");
266  }
267  }
268  else
269  {
270  rewind (inFitsTempPtr);
271  if ((inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "w")) == NULL)
272  {
273  *error = 2;
274  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list for writing");
275  }
276  else
277  {
278  fileCount = 0;
279  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsTempPtr) != NULL)
280  {
281  // Get template name first
282  fileCount++;
283  if (fileCount == 1)
284  {
285  fprintf (inFitsClassifiedPtr, "%s", stringTemp);
286  continue;
287  }
288  if (strcmp (stringTemp, "END_OF_BATCH\n") == 0)
289  fileCount = 0;
290  sprintf (classification, "%s", "");
291  sscanf (stringTemp, "%s%s", fileNameStr, classification);
292  fprintf (inFitsClassifiedPtr, "%s %s\n", fileNameStr, classification);
293  }
294  }
295  }
296  }
297 
298  // Close all files
299  if (inFitsClassifiedPtr) fclose (inFitsClassifiedPtr);
300  if (inFitsTempPtr) fclose (inFitsTempPtr);
301  if (*error < 2) remove (inFitsTemp);
302 
303  // Release memory
304  if (stringTemp) free (stringTemp);
305  if (inFitsTemp) free (inFitsTemp);
306  if (classification) free (classification);
307  if (fileNameStr) free (fileNameStr);
308  if (format) freeImageFormat (format);
309 
310  return;
311 }
312 /******************************************************************************/
313 
314 
315 /******************************************************************************
316 * European Southern Observatory
317 * VLTI MIDI Data Reduction Software
318 *
319 * Module name: getFitsClassification
320 * Input/Output: See function arguments to avoid duplication
321 * Description: A relatively quicker routine to get the fits file classification
322 *
323 * History:
324 * 08-Mar-05 (csabet) Created
325 ******************************************************************************/
326 void getFitsClassification (
327  int fileNum, // In: File counter
328  FILE *reportPtr, // In: Pointer to a temporary report file
329  char *fileName, // In: Name of the input FITS file
330  ImageFormat *format, // Ou: Classification
331  int *error) // Ou: Error status
332 
333 {
334 
335  /* Local Declarations
336  --------------------*/
337  const char routine[] = "getFitsClassification";
338  char *stringQfits, *emptyString, *cleanString;
339  int unknownNumber;
340  struct stat buf;
341 
342  /* Algorithm
343  -----------*/
344  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
345  if (diagnostic > 4) fprintf (reportPtr, "Invoking routine '%s' \n", routine);
346 
347  // Reset status
348  *error = 0;
349  unknownNumber = - 1;
350 
351  // Check if the file exist
352  if (stat (fileName, &buf) == -1)
353  {
354  sprintf (midiMessage, "Cannot find file ... %s", fileName);
355  midiReportWarning (reportPtr, routine, __FILE__, __LINE__, midiMessage);
356  *error = 1;
357  return;
358  }
359  else if ((qfits_is_fits (fileName)) != 1) // Check if this is a FITS file
360  {
361  sprintf (midiMessage, "Not a FITS file ... %s", fileName);
362  midiReportWarning (reportPtr, routine, __FILE__, __LINE__, midiMessage);
363  *error = 1;
364  return;
365  }
366 
367  // Allocate memory
368  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
369  emptyString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
370  strcpy (emptyString, "UNKNOWN");
371 
372  // Get data Observation Category
373  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO DPR CATG");
374  if (stringQfits == NULL) sprintf (format->obsCatg, "%s", emptyString);
375  else
376  {
377  cleanUpString (stringQfits, cleanString);
378  sprintf (format->obsCatg, "%s", cleanString);
379  }
380 
381  // Get data Observation Technique
382  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO DPR TECH");
383  if (stringQfits == NULL) sprintf (format->obsTech, "%s", emptyString);
384  else
385  {
386  cleanUpString (stringQfits, cleanString);
387  sprintf (format->obsTech, "%s", cleanString);
388  }
389 
390  // Get data Observation Type
391  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO DPR TYPE");
392  if (stringQfits == NULL) sprintf (format->obsType, "%s", emptyString);
393  else
394  {
395  cleanUpString (stringQfits, cleanString);
396  sprintf (format->obsType, "%s", cleanString);
397  }
398 
399  // Get target name
400  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TARG NAME");
401  if (stringQfits == NULL) sprintf (format->targetName, "%s", emptyString);
402  else
403  {
404  cleanUpString (stringQfits, cleanString);
405  sprintf (format->targetName, "%s", cleanString);
406  }
407 
408  // Get TPL Start
409  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL START");
410  if (stringQfits == NULL) sprintf (format->tplStart, "%s", emptyString);
411  else
412  {
413  cleanUpString (stringQfits, cleanString);
414  sprintf (format->tplStart, "%s", cleanString);
415  }
416 
417  // Get number of templates
418  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TPLNO");
419  if (stringQfits == NULL) format->numOfTemplates = unknownNumber;
420  else sscanf (stringQfits, "%d", &(format->numOfTemplates));
421 
422  // Get template's number of files
423  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OCS TPL NFILE");
424  if (stringQfits == NULL) format->numOfFilesInTemplate = unknownNumber;
425  else sscanf (stringQfits, "%d", &(format->numOfFilesInTemplate));
426 
427  // Get template's sequential file number
428  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OCS TPL FILENO");
429  if (stringQfits == NULL) format->fileNumInTemplate = unknownNumber;
430  else sscanf (stringQfits, "%d", &(format->fileNumInTemplate));
431 
432  // Get template's Exposure number
433  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL EXPNO");
434  if (stringQfits == NULL) format->exposureNumInTemplate = unknownNumber;
435  else sscanf (stringQfits, "%d", &(format->exposureNumInTemplate));
436 
437  // Get template's number of exposures
438  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL NEXP");
439  if (stringQfits == NULL) format->numOfExposures = unknownNumber;
440  else sscanf (stringQfits, "%d", &(format->numOfExposures));
441 
442  // Get template's file number within exposures
443  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OCS EXPO FILENO");
444  if (stringQfits == NULL) format->fileNumInExposure = unknownNumber;
445  else sscanf (stringQfits, "%d", &(format->fileNumInExposure));
446 
447  // Get template name
448  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL NAME");
449  if (stringQfits == NULL) sprintf (format->tplName, "%s", emptyString);
450  else
451  {
452  cleanUpString (stringQfits, cleanString);
453  sprintf (format->tplName, "%s", cleanString);
454  }
455 
456  // Get beam combiner
457  stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO INS OPT1 ID");
458  if (stringQfits == NULL) sprintf (format->beamCombiner, "%s", emptyString);
459  else
460  {
461  cleanUpString (stringQfits, cleanString);
462  sprintf (format->beamCombiner, "%s", cleanString);
463  }
464 
465  // Get Shutter ID
466  stringQfits = qfits_query_ext (fileName, "HIERARCH ESO INS SHUT ID", 0);
467  if (stringQfits == NULL) sprintf (format->shutterId, "%s", emptyString);
468  else
469  {
470  cleanUpString (stringQfits, cleanString);
471  sprintf (format->shutterId, "%s", cleanString);
472  }
473 
474  if (diagnostic)
475  {
476  cpl_msg_info(cpl_func,"\nfile: %4d\n", fileNum);
477  cpl_msg_info(cpl_func,"fileName: %s\n", fileName);
478  cpl_msg_info(cpl_func,"tplName (TPL NAME): %s\n", format->tplName);
479  cpl_msg_info(cpl_func,"tplStart (TPL START): %s\n", format->tplStart);
480  if (format->fileNumInTemplate == -1)
481  cpl_msg_info(cpl_func,"fileNumInTemplate (TPL FILENO): UNKNOWN\n");
482  else
483  cpl_msg_info(cpl_func,"fileNumInTemplate (TPL FILENO): %d\n", format->fileNumInTemplate);
484 
485  if (format->numOfFilesInTemplate == -1)
486  cpl_msg_info(cpl_func,"numOfFilesInTemplate (TPL NFILE): UNKNOWN\n");
487  else
488  cpl_msg_info(cpl_func,"numOfFilesInTemplate (TPL NFILE): %d\n", format->numOfFilesInTemplate);
489 
490  if (format->numOfTemplates)
491  cpl_msg_info(cpl_func,"numOfTemplates (OBS TPLNO): UNKNOWN\n");
492  else
493  cpl_msg_info(cpl_func,"numOfTemplates (OBS TPLNO): %d\n", format->numOfTemplates);
494 
495  if (format->numOfExposures)
496  cpl_msg_info(cpl_func,"numOfExposures (TPL NEXP): UNKNOWN\n");
497  else
498  cpl_msg_info(cpl_func,"numOfExposures (TPL NEXP): %d\n", format->numOfExposures);
499 
500  if (format->fileNumInExposure)
501  cpl_msg_info(cpl_func,"fileNumInExposure (EXPO FILENO): UNKNOWN\n");
502  else
503  cpl_msg_info(cpl_func,"fileNumInExposure (EXPO FILENO): %d\n", format->fileNumInExposure);
504 
505  if (format->exposureNumInTemplate)
506  cpl_msg_info(cpl_func,"exposureNumInTemplate (TPL EXPNO): UNKNOWN\n");
507  else
508  cpl_msg_info(cpl_func,"exposureNumInTemplate (TPL EXPNO): %d\n", format->exposureNumInTemplate);
509  cpl_msg_info(cpl_func,"targetName (TARG NAME): %s\n", format->targetName);
510  cpl_msg_info(cpl_func,"beamCombiner (INS OPT1 ID): %s\n", format->beamCombiner);
511  cpl_msg_info(cpl_func,"shutterId (INS SHUT ID): %s\n", format->shutterId);
512  cpl_msg_info(cpl_func,"observationCategory (DPR CATG): %s\n", format->obsCatg);
513  cpl_msg_info(cpl_func,"observationType (DPR TYPE): %s\n", format->obsType);
514  cpl_msg_info(cpl_func,"observationTechnique (DPR TECH): %s\n", format->obsTech);
515  }
516 
517  fprintf (reportPtr, "\nfile: %4d\n", fileNum);
518  fprintf (reportPtr, "fileName: %s\n", fileName);
519  fprintf (reportPtr, "tplName (TPL NAME): %s\n", format->tplName);
520  fprintf (reportPtr, "tplStart (TPL START): %s\n", format->tplStart);
521  if (format->fileNumInTemplate == -1)
522  fprintf (reportPtr, "fileNumInTemplate (TPL FILENO): UNKNOWN\n");
523  else
524  fprintf (reportPtr, "fileNumInTemplate (TPL FILENO): %d\n", format->fileNumInTemplate);
525 
526  if (format->numOfFilesInTemplate == -1)
527  fprintf (reportPtr, "numOfFilesInTemplate (TPL NFILE): UNKNOWN\n");
528  else
529  fprintf (reportPtr, "numOfFilesInTemplate (TPL NFILE): %d\n", format->numOfFilesInTemplate);
530 
531  if (format->numOfTemplates)
532  fprintf (reportPtr, "numOfTemplates (OBS TPLNO): UNKNOWN\n");
533  else
534  fprintf (reportPtr, "numOfTemplates (OBS TPLNO): %d\n", format->numOfTemplates);
535 
536  if (format->numOfExposures)
537  fprintf (reportPtr, "numOfExposures (TPL NEXP): UNKNOWN\n");
538  else
539  fprintf (reportPtr, "numOfExposures (TPL NEXP): %d\n", format->numOfExposures);
540 
541  if (format->fileNumInExposure)
542  fprintf (reportPtr, "fileNumInExposure (EXPO FILENO): UNKNOWN\n");
543  else
544  fprintf (reportPtr, "fileNumInExposure (EXPO FILENO): %d\n", format->fileNumInExposure);
545 
546  if (format->exposureNumInTemplate)
547  fprintf (reportPtr, "exposureNumInTemplate (TPL EXPNO): UNKNOWN\n");
548  else
549  fprintf (reportPtr, "exposureNumInTemplate (TPL EXPNO): %d\n", format->exposureNumInTemplate);
550  fprintf (reportPtr, "targetName (TARG NAME): %s\n", format->targetName);
551  fprintf (reportPtr, "beamCombiner (INS OPT1 ID): %s\n", format->beamCombiner);
552  fprintf (reportPtr, "shutterId (INS SHUT ID): %s\n", format->shutterId);
553  fprintf (reportPtr, "observationCategory (DPR CATG): %s\n", format->obsCatg);
554  fprintf (reportPtr, "observationType (DPR TYPE): %s\n", format->obsType);
555  fprintf (reportPtr, "observationTechnique (DPR TECH): %s\n", format->obsTech);
556 
557  /* Release memory */
558  free (emptyString);
559  free (cleanString);
560 
561  return;
562 }
563 /*****************************************************************************/
564 
565 
566 /******************************************************************************
567 * European Southern Observatory
568 * VLTI MIDI Data Reduction Software
569 *
570 * Module name: getFringeTrackBatch
571 * Input/Output: See function arguments to avoid duplication
572 * Description: We know the first file in the given list is a TRACK. We
573 * check to see if we can find a complete batch. If not the file
574 * pointer is returned unchanged.
575 *
576 * History:
577 * 28-Apr-05 (csabet) Created
578 ******************************************************************************/
579 void getFringeTrackBatch (
580  ImageFormat *format, // In: Format structure
581  FILE *inFitsTempPtr, // In: Pointer to the main buffer
582  FILE *inFitsListPtr, // In: Pointer to the list of FITS files
583  char *fileNameStr, // In: Name of the first FITS file
584  char *dummyClassif, // In: Dummy classification
585  FILE *filePtr, // In: Pointer to the log file
586  int *localBatchNum, // IO: Local batch number
587  int *fileNumInList, // IO: pointer to file number
588  int *error, // Ou: Error status
589  cpl_parameterlist *parlist,
590  cpl_frameset *frameset)
591 
592 {
593 
594  // Local Declarations
595  // ------------------
596  const char routine[] = "getFringeTrackBatch";
597  char *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL, *localTplName=NULL,
598  *localBeamCombiner, *localObsCatg, *localObsTech, *localObsType;
599  FILE *inFitsBatchTempPtr=NULL;
600  int i, numOfInterfFiles, numOfPhotAFiles, numOfPhotBFiles, fileNumInTemplate, numOfEmptyFiles,
601  localFileNumInList, numOfFiles;
602  long inFitsListPosition;
603  int checkSof=0;
604  cpl_parameter * cur_param;
605  // Algorithm
606  // ---------
607  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
608  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
609 
610  // Reset status
611  *error = 0;
612  numOfFiles = 0;
613  numOfInterfFiles = 0;
614  numOfPhotAFiles = 0;
615  numOfPhotBFiles = 0;
616  numOfEmptyFiles = 0;
617 
618  /*Get the recipe parameter*/
619  cur_param=cpl_parameterlist_find(parlist,
620  "midi.midi_fringe_all.checkSof");
621  checkSof=cpl_parameter_get_bool(cur_param);
622 
623  // Allocate memory
624  inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
625  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
626  localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
627  localTplName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
628  localBeamCombiner = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
629  localObsCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
630  localObsTech = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
631  localObsType = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
632 
633  // Open a batch temporary file for writing
634  sprintf (inFitsBatchTemp, "localBuffer.log");
635  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
636  {
637  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
638  *error = 1;
639  free (inFitsBatchTemp);
640  free (localTplStart);
641  free (localTplName);
642  free (stringTemp);
643  free (localBeamCombiner);
644  free (localObsCatg);
645  free (localObsTech);
646  free (localObsType);
647  return;
648  }
649 
650  // Save the first file and its parameters
651  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
652  inFitsListPosition = ftell (inFitsListPtr);
653  localFileNumInList = *fileNumInList;
654 
655  if (strcmp (format->obsTech, "INTERFEROMETRY") == 0)
656  numOfInterfFiles++;
657  else if (strcmp (format->shutterId, "AOPEN") == 0)
658  numOfPhotAFiles++;
659  else if (strcmp (format->shutterId, "BOPEN") == 0)
660  numOfPhotBFiles++;
661 
662  fileNumInTemplate = format->fileNumInTemplate;
663 
664  sprintf (localTplStart, "%s", format->tplStart);
665  sprintf (localTplName, "%s", format->tplName);
666  sprintf (localBeamCombiner, "%s", format->beamCombiner);
667  sprintf (localObsCatg, "%s", format->obsCatg);
668  sprintf (localObsTech, "%s", format->obsTech);
669  sprintf (localObsType, "%s", format->obsType);
670 
671  sprintf (batchTemplate, "%s", format->tplName);
672 
673  // Get the rest of the batch files from the list
674  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
675  {
676  sprintf (dummyClassif, "%s", "");
677  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
678 
679  localFileNumInList++;
680  fileNumInTemplate++;
681 
682  // Get FITS file parameters. If ok copy into the batch temporary file
683  getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
684 
685  /*Check template name consistency. For the
686  * calibrator database the tpl Name is "UNKNOWN"*/
687  //if (strcmp (format->tplName, localTplName) != 0) *error = 1;
688  if (strcmp (format->tplName, localTplName) != 0 && strcmp (format->tplName, "UNKNOWN") != 0 )
689  {
690  *error = 1;
691  }
692 
693  /*Checking for TPL START and OCS TPL FILENO (de)activating*/
694  if(checkSof==FALSE)
695  {
696  cpl_msg_warning(cpl_func,"Overruling TPL START homogeneity check");
697  cpl_msg_warning(cpl_func,"Overruling OCS TPL FILENO homogeneity check");
698  format->fileNumInTemplate=fileNumInTemplate;
699  sprintf (localTplStart, "%s", format->tplStart);
700  }
701 
702  // We expect possibly more split interferometry and photometry files and an empty file
703  if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) ||
704  (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
705  (strcmp (format->obsTech, "INTERFEROMETRY") == 0) &&
706  ((strcmp (format->obsType, "TRACK,OBJECT,DISPERSED") == 0) ||
707  (strcmp (format->obsType, "TRACK,OBJECT,DISPERSED,SCIPHOT") == 0) ||
708 
709  (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER") == 0) ||
710  (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER,SCI_PHOT") == 0) ||
711 
712  (strcmp (format->obsType, "TRACK,OBJECT,DISPERSED") == 0)) &&
713  (strcmp (format->tplStart, localTplStart) == 0) &&
714  (format->fileNumInTemplate == fileNumInTemplate))
715  {
716  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
717  numOfInterfFiles++;
718  }
719  else if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) ||
720  (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
721  (strcmp (format->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
722  ((strcmp (format->obsType, "PHOTOMETRY,OBJECT") == 0) ||
723  (strcmp (format->obsType, "PHOTOMETRY,OBJECT,SCIPHOT") == 0)) &&
724  (strcmp (format->tplStart, localTplStart) == 0) &&
725  (strcmp (format->shutterId, "AOPEN") == 0) &&
726  (format->fileNumInTemplate == fileNumInTemplate))
727  {
728  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
729  numOfPhotAFiles++;
730  }
731  else if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) ||
732  (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
733  (strcmp (format->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
734  ((strcmp (format->obsType, "PHOTOMETRY,OBJECT") == 0) ||
735  (strcmp (format->obsType, "PHOTOMETRY,OBJECT,SCIPHOT") == 0)) &&
736  (strcmp (format->tplStart, localTplStart) == 0) &&
737  (strcmp (format->shutterId, "BOPEN") == 0) &&
738  (format->fileNumInTemplate == fileNumInTemplate))
739  {
740  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
741  numOfPhotBFiles++;
742  }
743  else if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) ||
744  (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
745  (strcmp (format->obsTech, "OTHER") == 0) &&
746  (strcmp (format->obsType, "OTHER") == 0) &&
747  (strcmp (format->tplStart, localTplStart) == 0) &&
748  (format->fileNumInTemplate == fileNumInTemplate) &&
749  (numOfEmptyFiles < 1))
750  {
751  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
752  numOfEmptyFiles++;
753  break;
754  }
755  else
756  {
757  // We have not found a complete set. Hence reset the file position
758  fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
759  localFileNumInList--;
760  break;
761  }
762  inFitsListPosition = ftell (inFitsListPtr);
763  }
764  *fileNumInList = localFileNumInList;
765 
766  // If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
767  if (!endOfTemplate) numOfEmptyFiles = 1;
768 
769  /* Now finalise batchTemplate name. If batchTemplate
770  is valid then copy all files into the main temporary file */
771  if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
772  (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
773  (strcmp (localBeamCombiner, "HIGH_SENS") == 0) &&
774  (strcmp (localObsCatg, "CALIB") == 0) &&
775  (numOfInterfFiles > 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) &&
776  (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
777  sprintf (batchTemplate, "%s", "HIGH_SENS_CALIB");
778  else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
779  (strcmp (localTplName, "MIDI STARINTF OBS FRINGE SCI") == 0)) &&
780  (strcmp (localBeamCombiner, "HIGH_SENS") == 0) &&
781  (strcmp (localObsCatg, "SCIENCE") == 0) &&
782  (numOfInterfFiles > 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) &&
783  (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
784  sprintf (batchTemplate, "%s", "HIGH_SENS_SCIENCE");
785  else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
786  (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
787  (strcmp (localBeamCombiner, "SCI_PHOT") == 0) &&
788  (strcmp (localObsCatg, "CALIB") == 0) &&
789  (numOfInterfFiles > 0) && (numOfPhotAFiles+numOfPhotBFiles == 0) && (numOfEmptyFiles == 1))
790  sprintf (batchTemplate, "%s", "SCI_PHOT_CALIB");
791  else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
792  (strcmp (localTplName, "MIDI STARINTF OBS FRINGE SCI") == 0)) &&
793  (strcmp (localBeamCombiner, "SCI_PHOT") == 0) &&
794  (strcmp (localObsCatg, "SCIENCE") == 0) &&
795  (numOfInterfFiles > 0) && (numOfPhotAFiles+numOfPhotBFiles == 0) && (numOfEmptyFiles == 1))
796  sprintf (batchTemplate, "%s", "SCI_PHOT_SCIENCE");
797  else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
798  (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
799  (strcmp (localBeamCombiner, "SCI_PHOT") == 0) &&
800  (numOfInterfFiles == 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) &&
801  (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
802  sprintf (batchTemplate, "%s", "SCI_PHOT_KAPPA");
803  else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
804  (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
805  (strcmp (localBeamCombiner, "SCI_PHOT") == 0) &&
806  (numOfInterfFiles > 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) &&
807  (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
808  {
809  numOfFiles = numOfInterfFiles + numOfPhotAFiles + numOfPhotBFiles + numOfEmptyFiles;
810  sprintf (batchTemplate, "%s", "SCI_PHOT_KAPPA");
811  }
812  else
813  {
814  if (diagnostic > 1)
815  {
816  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Unknown Template Configuration:");
817  cpl_msg_info(cpl_func," Template Name = %s \n", localTplName);
818  cpl_msg_info(cpl_func," Beam Combiner = %s \n", localBeamCombiner);
819  cpl_msg_info(cpl_func," Obs. Category = %s \n", localObsCatg);
820  cpl_msg_info(cpl_func," Number of Interf files = %d \n", numOfInterfFiles);
821  cpl_msg_info(cpl_func," Number of Photom A files = %d \n", numOfPhotAFiles);
822  cpl_msg_info(cpl_func," Number of Photom B files = %d \n", numOfPhotBFiles);
823  fprintf (filePtr, " Template Name = %s \n", localTplName);
824  fprintf (filePtr, " Beam Combiner = %s \n", localBeamCombiner);
825  fprintf (filePtr, " Obs. Category = %s \n", localObsCatg);
826  fprintf (filePtr, " Number of Interf files = %d \n", numOfInterfFiles);
827  fprintf (filePtr, " Number of Photom A files = %d \n", numOfPhotAFiles);
828  fprintf (filePtr, " Number of Photom B files = %d \n", numOfPhotBFiles);
829  }
830  free (inFitsBatchTemp);
831  free (localTplStart);
832  free (localTplName);
833  free (stringTemp);
834  free (localBeamCombiner);
835  free (localObsCatg);
836  free (localObsTech);
837  free (localObsType);
838  if (inFitsBatchTempPtr)
839  {
840  fclose (inFitsBatchTempPtr);
841  inFitsBatchTempPtr = NULL;
842  }
843  return;
844  }
845 
846  // Reopen the batch temporary file for reading
847  fclose (inFitsBatchTempPtr);
848  inFitsBatchTempPtr = NULL;
849  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
850  {
851  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
852  *error = 1;
853  free (inFitsBatchTemp);
854  free (localTplStart);
855  free (localTplName);
856  free (stringTemp);
857  free (localBeamCombiner);
858  free (localObsCatg);
859  free (localObsTech);
860  free (localObsType);
861  return;
862  }
863 
864  // If it is a SCI_PHOT_KAPPA batch with interferometry files, it must be split into two batches
865  if ((strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0) && (numOfInterfFiles > 0))
866  {
867  // First extract the Photometry files for kappa calculation, only if it is a CALIB template
868  if (strcmp (localObsCatg, "CALIB") == 0)
869  {
870  (*localBatchNum)++;
871  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
872  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
873  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
874  for (i = 1; i <= numOfFiles; i++)
875  {
876  fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr);
877  if (i > numOfInterfFiles)
878  {
879  sprintf (dummyClassif, "%s", "");
880  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
881  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
882  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
883  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
884  }
885  }
886  cpl_msg_info(cpl_func,"\n");
887  fprintf (filePtr, "\n");
888 
889  /* We should now have a complete batch of files
890  in the temporary file list. Add an appending keyword */
891  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
892  }
893 
894  // Now rewind the batch list and extract the Interferometry files
895  if (inFitsBatchTempPtr) rewind (inFitsBatchTempPtr);
896  (*localBatchNum)++;
897  if (strcmp (localObsCatg, "CALIB") == 0)
898  {
899  cpl_msg_info(cpl_func,"\nSelected batch %d is SCI_PHOT_CALIB:\n", (*localBatchNum));
900  fprintf (filePtr, "\nSelected batch %d: is SCI_PHOT_CALIB\n", (*localBatchNum));
901  fprintf (inFitsTempPtr, "%s \n", "SCI_PHOT_CALIB");
902  }
903  else
904  {
905  cpl_msg_info(cpl_func,"\nSelected batch %d is SCI_PHOT_SCIENCE:\n", (*localBatchNum));
906  fprintf (filePtr, "\nSelected batch %d: is SCI_PHOT_SCIENCE\n", (*localBatchNum));
907  fprintf (inFitsTempPtr, "%s \n", "SCI_PHOT_SCIENCE");
908  }
909  for (i = 1; i <= numOfFiles; i++)
910  {
911  fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr);
912  if (i <= numOfInterfFiles)
913  {
914  sprintf (dummyClassif, "%s", "");
915  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
916  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
917  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
918  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
919  }
920  }
921  cpl_msg_info(cpl_func,"\n");
922  fprintf (filePtr, "\n");
923 
924  /* We should now have a complete batch of files
925  in the temporary file list. Add an appending keyword */
926  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
927  if (inFitsBatchTempPtr)
928  {
929  fclose (inFitsBatchTempPtr);
930  inFitsBatchTempPtr = NULL;
931  }
932  remove (inFitsBatchTemp);
933  }
934  else
935  {
936  (*localBatchNum)++;
937  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
938  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
939  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
940  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
941  {
942  sprintf (dummyClassif, "%s", "");
943  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
944  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
945  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
946  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
947  }
948  cpl_msg_info(cpl_func,"\n");
949  fprintf (filePtr, "\n");
950 
951  /* We should now have a complete batch of files
952  in the temporary file list. Add an appending keyword */
953  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
954  if (inFitsBatchTempPtr)
955  {
956  fclose (inFitsBatchTempPtr);
957  inFitsBatchTempPtr = NULL;
958  }
959  remove (inFitsBatchTemp);
960  }
961 
962  // Release memory
963  if (inFitsBatchTempPtr)
964  {
965  fclose (inFitsBatchTempPtr);
966  inFitsBatchTempPtr = NULL;
967  }
968  if (inFitsBatchTemp) free (inFitsBatchTemp);
969  if (localTplStart) free (localTplStart);
970  if (localTplName) free (localTplName);
971  if (stringTemp) free (stringTemp);
972  if (localBeamCombiner) free (localBeamCombiner);
973  if (localObsCatg) free (localObsCatg);
974  if (localObsTech) free (localObsTech);
975  if (localObsType) free (localObsType);
976 
977  return;
978 }
979 /*****************************************************************************/
980 
981 
982 /******************************************************************************
983 * European Southern Observatory
984 * VLTI MIDI Data Reduction Software
985 *
986 * Module name: getAcquisitionBatch
987 * Input/Output: See function arguments to avoid duplication
988 * Description: Acquisitio files are individual
989 *
990 * History:
991 * 28-Apr-05 (csabet) Created
992 ******************************************************************************/
993 void getAcquisitionBatch (
994  FILE *inFitsTempPtr, // In: Pointer to the main buffer
995  char *fileNameStr, // In: Name of the first FITS file
996  char *dummyClassif, // In: Dummy classification
997  FILE *filePtr, // In: Pointer to the log file
998  int *localBatchNum, // IO: Local batch number
999  int *error) // Ou: Error status
1000 {
1001 
1002  // Local Declarations
1003  // ------------------
1004  const char routine[] = "getAcquisitionBatch";
1005 
1006  // Algorithm
1007  // ---------
1008  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1009  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
1010 
1011  // Reset status
1012  *error = 0;
1013 
1014  (*localBatchNum)++;
1015  sprintf (batchTemplate, "%s", "ACQ");
1016  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1017  fprintf (filePtr, "\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1018  sprintf (dummyClassif, "%s", "");
1019  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
1020  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
1021  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
1022  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1023  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
1024  cpl_msg_info(cpl_func,"\n");
1025  fprintf (filePtr, "\n");
1026 
1027  return;
1028 }
1029 /*****************************************************************************/
1030 
1031 
1032 
1033 
1034 /******************************************************************************
1035 * European Southern Observatory
1036 * VLTI MIDI Maintenance Templates Software
1037 *
1038 * Module name: getDetLinBatch
1039 * Input/Output: See function arguments to avoid duplication
1040 * Description: It loops through the given list of FITS files and searches for files
1041 * that relate to Detector Linearity template. It then creates
1042 * a batch file with the following format:
1043 *
1044 * <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC DETLIN
1045 * <FITS File 1>
1046 * <FITS File 2>
1047 * <.>
1048 * <.>
1049 * <FITS File n>
1050 * END_OF_BATCH
1051 *
1052 * History:
1053 * 14-Jun-04 (csabet) created
1054 ******************************************************************************/
1055 void getDetLinBatch (
1056  ImageFormat *format, // In: Format structure
1057  FILE *inFitsTempPtr, // In: Pointer to the main buffer
1058  FILE *inFitsListPtr, // In: Pointer to the list of FITS files
1059  char *fileNameStr, // In: Name of the first FITS file
1060  char *dummyClassif, // In: Dummy classification
1061  FILE *filePtr, // In: Pointer to the log file
1062  int *localBatchNum, // IO: Local batch number
1063  int *fileNumInList, // IO: pointer to file number
1064  int *error) // Ou: Error status
1065 {
1066 
1067  // Local Declarations
1068  // ------------------
1069  const char routine[] = "getDetLinBatch";
1070  char *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
1071  FILE *inFitsBatchTempPtr=NULL;
1072  int numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
1073  localFileNumInList;
1074  long inFitsListPosition;
1075 
1076  // Algorithm
1077  // ---------
1078  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1079  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
1080 
1081  // Reset status
1082  *error = 0;
1083  numOfDataFiles = 0;
1084  numOfEmptyFiles = 0;
1085 
1086  // Allocate memory
1087  inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1088  localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1089  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1090 
1091  // Open a batch temporary file for writing
1092  sprintf (inFitsBatchTemp, "localBuffer.log");
1093  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
1094  {
1095  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1096  *error = 1;
1097  free (inFitsBatchTemp);
1098  free (localTplStart);
1099  free (stringTemp);
1100  return;
1101  }
1102 
1103  // Save the first file and its parameters
1104  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1105  fileNumInTemplate = format->fileNumInTemplate;
1106  sscanf (format->tplStart, "%s", localTplStart);
1107  numOfDataFiles++;
1108  localFileNumInList = *fileNumInList;
1109  inFitsListPosition = ftell (inFitsListPtr);
1110  sprintf (batchTemplate, "%s", "DETLIN");
1111 
1112  // Get the rest of the batch files from the list
1113  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
1114  {
1115  sprintf (dummyClassif, "%s", "");
1116  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1117 
1118  localFileNumInList++;
1119  fileNumInTemplate++;
1120 
1121  // Get FITS file parameters. If ok copy into the batch temporary file
1122  getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
1123 
1124  // We expect possibly more split files
1125  if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1126  ((strcmp (format->obsTech, "IMAGE") == 0) ||
1127  (strcmp (format->obsTech, "SPECTRUM") == 0)) &&
1128  (strcmp (format->obsType, "FLAT") == 0) &&
1129  (strcmp (format->tplStart, localTplStart) == 0) &&
1130  (format->fileNumInTemplate == fileNumInTemplate))
1131  {
1132  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1133  numOfDataFiles++;
1134  }
1135  else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1136  (strcmp (format->obsTech, "OTHER") == 0) &&
1137  (strcmp (format->obsType, "OTHER") == 0) &&
1138  (strcmp (format->tplStart, localTplStart) == 0) &&
1139  (format->fileNumInTemplate == fileNumInTemplate) &&
1140  (numOfEmptyFiles < 1))
1141  {
1142  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1143  numOfEmptyFiles++;
1144  break;
1145  }
1146  else
1147  {
1148  // We have not found a complete set. Hence reset the file position
1149  fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
1150  localFileNumInList--;
1151  break;
1152  }
1153  inFitsListPosition = ftell (inFitsListPtr);
1154  }
1155 
1156  // If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
1157  if (!endOfTemplate) numOfEmptyFiles = 1;
1158 
1159  // We expect at least one data file and one empty file. If there has been
1160  // a complete set of files then copy all files into the main temporary file
1161  if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
1162  {
1163  // Reopen the batch temporary file for reading
1164  if (inFitsBatchTempPtr)
1165  {
1166  fclose (inFitsBatchTempPtr);
1167  inFitsBatchTempPtr = NULL;
1168  }
1169  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
1170  {
1171  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1172  *error = 1;
1173  free (inFitsBatchTemp);
1174  free (localTplStart);
1175  free (stringTemp);
1176  return;
1177  }
1178 
1179  (*localBatchNum)++;
1180  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1181  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
1182  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
1183  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
1184  {
1185  sprintf (dummyClassif, "%s", "");
1186  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1187  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
1188  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
1189  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1190  }
1191  cpl_msg_info(cpl_func,"\n");
1192  fprintf (filePtr, "\n");
1193 
1194  // We should now have a complete set of files
1195  // in the temporary file list. Add an appending keyword
1196  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
1197  }
1198  *fileNumInList = localFileNumInList;
1199  if (inFitsBatchTempPtr)
1200  {
1201  fclose (inFitsBatchTempPtr);
1202  inFitsBatchTempPtr = NULL;
1203  }
1204  remove (inFitsBatchTemp);
1205 
1206  // Release memory
1207  if (inFitsBatchTemp) free (inFitsBatchTemp);
1208  if (localTplStart) free (localTplStart);
1209  if (stringTemp) free (stringTemp);
1210 
1211  return;
1212 }
1213 /*****************************************************************************/
1214 
1215 
1216 /******************************************************************************
1217 * European Southern Observatory
1218 * VLTI MIDI Maintenance Templates Software
1219 *
1220 * Module name: getDetRonBatch
1221 * Input/Output: See function arguments to avoid duplication
1222 * Description: It loops through the given list of FITS files and searches for files
1223 * that relate to Detector Readout Noise template. It then creates
1224 * a batch file with the following format:
1225 *
1226 * <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC DETRON
1227 * <FITS File 1>
1228 * <FITS File 2>
1229 * <.>
1230 * <.>
1231 * <FITS File n>
1232 * END_OF_BATCH
1233 *
1234 * History:
1235 * 14-Jun-04 (csabet) created
1236 ******************************************************************************/
1237 void getDetRonBatch (
1238  ImageFormat *format, // In: Format structure
1239  FILE *inFitsTempPtr, // In: Pointer to the main buffer
1240  FILE *inFitsListPtr, // In: Pointer to the list of FITS files
1241  char *fileNameStr, // In: Name of the first FITS file
1242  char *dummyClassif, // In: Dummy classification
1243  FILE *filePtr, // In: Pointer to the log file
1244  int *localBatchNum, // IO: Local batch number
1245  int *fileNumInList, // IO: pointer to file number
1246  int *error) // Ou: Error status
1247 {
1248 
1249  // Local Declarations
1250  // ------------------
1251  const char routine[] = "getDetRonBatch";
1252  char *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
1253  FILE *inFitsBatchTempPtr=NULL;
1254  int numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
1255  localFileNumInList;
1256  long inFitsListPosition;
1257 
1258  // Algorithm
1259  // ---------
1260  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1261  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
1262 
1263  // Reset status
1264  *error = 0;
1265  numOfDataFiles = 0;
1266  numOfEmptyFiles = 0;
1267 
1268  // Allocate memory
1269  inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1270  localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1271  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1272 
1273  // Open a batch temporary file for writing
1274  sprintf (inFitsBatchTemp, "localBuffer.log");
1275  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
1276  {
1277  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1278  *error = 1;
1279  free (inFitsBatchTemp);
1280  free (localTplStart);
1281  free (stringTemp);
1282  return;
1283  }
1284 
1285  // Save the first file and its parameters
1286  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1287  fileNumInTemplate = format->fileNumInTemplate;
1288  sscanf (format->tplStart, "%s", localTplStart);
1289  numOfDataFiles++;
1290  localFileNumInList = *fileNumInList;
1291  inFitsListPosition = ftell (inFitsListPtr);
1292  sprintf (batchTemplate, "%s", "DETRON");
1293 
1294  // Get the rest of the batch files from the list
1295  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
1296  {
1297  sprintf (dummyClassif, "%s", "");
1298  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1299 
1300  localFileNumInList++;
1301  fileNumInTemplate++;
1302 
1303  // Get FITS file parameters. If ok copy into the batch temporary file
1304  getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
1305 
1306  // We expect possibly more split files
1307  if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1308  (strcmp (format->obsTech, "IMAGE") == 0) &&
1309  (strcmp (format->obsType, "BIAS") == 0) &&
1310  (strcmp (format->tplStart, localTplStart) == 0) &&
1311  (format->fileNumInTemplate == fileNumInTemplate))
1312  {
1313  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1314  numOfDataFiles++;
1315  }
1316  else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1317  (strcmp (format->obsTech, "OTHER") == 0) &&
1318  (strcmp (format->obsType, "OTHER") == 0) &&
1319  (strcmp (format->tplStart, localTplStart) == 0) &&
1320  (format->fileNumInTemplate == fileNumInTemplate) &&
1321  (numOfEmptyFiles < 1))
1322  {
1323  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1324  numOfEmptyFiles++;
1325  break;
1326  }
1327  else
1328  {
1329  // We have not found a complete set. Hence reset the file position
1330  fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
1331  localFileNumInList--;
1332  break;
1333  }
1334  inFitsListPosition = ftell (inFitsListPtr);
1335  }
1336 
1337  // If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
1338  if (!endOfTemplate) numOfEmptyFiles = 1;
1339 
1340  // We expect at least one data file and one empty file. If there has been
1341  // a complete set of files then copy all files into the main temporary file
1342  if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
1343  {
1344  // Reopen the batch temporary file for reading
1345  if (inFitsBatchTempPtr)
1346  {
1347  fclose (inFitsBatchTempPtr);
1348  inFitsBatchTempPtr = NULL;
1349  }
1350  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
1351  {
1352  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1353  *error = 1;
1354  free (inFitsBatchTemp);
1355  free (localTplStart);
1356  free (stringTemp);
1357  return;
1358  }
1359 
1360  (*localBatchNum)++;
1361  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1362  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
1363  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
1364  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
1365  {
1366  sprintf (dummyClassif, "%s", "");
1367  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1368  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
1369  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
1370  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1371  }
1372  cpl_msg_info(cpl_func,"\n");
1373  fprintf (filePtr, "\n");
1374 
1375  // We should now have a complete set of files in the
1376  // temporary file list. Add an appending keyword
1377  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
1378  }
1379  *fileNumInList = localFileNumInList;
1380  if (inFitsBatchTempPtr)
1381  {
1382  fclose (inFitsBatchTempPtr);
1383  inFitsBatchTempPtr = NULL;
1384  }
1385  remove (inFitsBatchTemp);
1386 
1387  // Release memory
1388  if (inFitsBatchTemp) free (inFitsBatchTemp);
1389  if (localTplStart) free (localTplStart);
1390  if (stringTemp) free (stringTemp);
1391 
1392  return;
1393 }
1394 /*****************************************************************************/
1395 
1396 
1397 
1398 /******************************************************************************
1399 * European Southern Observatory
1400 * VLTI MIDI Maintenance Templates Software
1401 *
1402 * Module name: getDspTrnBatch
1403 * Input/Output: See function arguments to avoid duplication
1404 * Description: It loops through the given list of FITS files and searches for files
1405 * that relate to Detector Readout Noise template. It then creates
1406 * a batch file with the following format:
1407 *
1408 * <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC DSPTRN
1409 * <FITS File 1>
1410 * <FITS File 2>
1411 * <.>
1412 * <.>
1413 * <FITS File n>
1414 * END_OF_BATCH
1415 *
1416 * History:
1417 * 14-Jun-04 (csabet) created
1418 ******************************************************************************/
1419 void getDspTrnBatch (
1420  ImageFormat *format, // In: Format structure
1421  FILE *inFitsTempPtr, // In: Pointer to the main buffer
1422  FILE *inFitsListPtr, // In: Pointer to the list of FITS files
1423  char *fileNameStr, // In: Name of the first FITS file
1424  char *dummyClassif, // In: Dummy classification
1425  FILE *filePtr, // In: Pointer to the log file
1426  int *localBatchNum, // IO: Local batch number
1427  int *fileNumInList, // IO: pointer to file number
1428  int *error) // Ou: Error status
1429 {
1430 
1431  // Local Declarations
1432  // ------------------
1433  const char routine[] = "getDspTrnBatch";
1434  char *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
1435  FILE *inFitsBatchTempPtr=NULL;
1436  int numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
1437  localFileNumInList;
1438  long inFitsListPosition;
1439 
1440  // Algorithm
1441  // ---------
1442  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1443  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
1444 
1445  // Reset status
1446  *error = 0;
1447  numOfDataFiles = 0;
1448  numOfEmptyFiles = 0;
1449 
1450  // Allocate memory
1451  inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1452  localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1453  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1454 
1455  // Open a batch temporary file for writing
1456  sprintf (inFitsBatchTemp, "localBuffer.log");
1457  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
1458  {
1459  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1460  *error = 1;
1461  free (inFitsBatchTemp);
1462  free (localTplStart);
1463  free (stringTemp);
1464  return;
1465  }
1466 
1467  // Save the first file and its parameters
1468  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1469  fileNumInTemplate = format->fileNumInTemplate;
1470  sscanf (format->tplStart, "%s", localTplStart);
1471  numOfDataFiles++;
1472  localFileNumInList = *fileNumInList;
1473  inFitsListPosition = ftell (inFitsListPtr);
1474  sprintf (batchTemplate, "%s", "DSPTRN");
1475 
1476  // Get the rest of the batch files from the list
1477  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
1478  {
1479  sprintf (dummyClassif, "%s", "");
1480  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1481 
1482  localFileNumInList++;
1483  fileNumInTemplate++;
1484 
1485  // Get FITS file parameters. If ok copy into the batch temporary file
1486  getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
1487 
1488  // We expect possibly more split files
1489  if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1490  (strcmp (format->obsTech, "SPECTRUM") == 0) &&
1491  (strcmp (format->obsType, "WAVE") == 0) &&
1492  (strcmp (format->tplStart, localTplStart) == 0) &&
1493  (format->fileNumInTemplate == fileNumInTemplate))
1494  {
1495  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1496  numOfDataFiles++;
1497  }
1498  else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1499  (strcmp (format->obsTech, "OTHER") == 0) &&
1500  (strcmp (format->obsType, "OTHER") == 0) &&
1501  (strcmp (format->tplStart, localTplStart) == 0) &&
1502  (format->fileNumInTemplate == fileNumInTemplate) &&
1503  (numOfEmptyFiles < 1))
1504  {
1505  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1506  numOfEmptyFiles++;
1507  break;
1508  }
1509  else
1510  {
1511  // We have not found a complete set. Hence reset the file position
1512  fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
1513  localFileNumInList--;
1514  break;
1515  }
1516  inFitsListPosition = ftell (inFitsListPtr);
1517  }
1518 
1519  // If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
1520  if (!endOfTemplate) numOfEmptyFiles = 1;
1521 
1522  // We expect at least one data file and one empty file. If there has been
1523  // a complete set of files then copy all files into the main temporary file
1524  if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
1525  {
1526  // Reopen the batch temporary file for reading
1527  if (inFitsBatchTempPtr)
1528  {
1529  fclose (inFitsBatchTempPtr);
1530  inFitsBatchTempPtr = NULL;
1531  }
1532  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
1533  {
1534  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1535  *error = 1;
1536  free (inFitsBatchTemp);
1537  free (localTplStart);
1538  free (stringTemp);
1539  return;
1540  }
1541 
1542  (*localBatchNum)++;
1543  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1544  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
1545  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
1546  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
1547  {
1548  sprintf (dummyClassif, "%s", "");
1549  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1550  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
1551  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
1552  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1553  }
1554  cpl_msg_info(cpl_func,"\n");
1555  fprintf (filePtr, "\n");
1556 
1557  // We should now have a complete set of files
1558  // in the temporary file list. Add an appending keyword
1559  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
1560  }
1561  *fileNumInList = localFileNumInList;
1562  if (inFitsBatchTempPtr)
1563  {
1564  fclose (inFitsBatchTempPtr);
1565  inFitsBatchTempPtr = NULL;
1566  }
1567  remove (inFitsBatchTemp);
1568 
1569  // Release memory
1570  if (inFitsBatchTemp) free (inFitsBatchTemp);
1571  if (localTplStart) free (localTplStart);
1572  if (stringTemp) free (stringTemp);
1573 
1574  return;
1575 }
1576 /*****************************************************************************/
1577 
1578 
1579 /******************************************************************************
1580 * European Southern Observatory
1581 * VLTI MIDI Maintenance Templates Software
1582 *
1583 * Module name: getRefPixBatch
1584 * Input/Output: See function arguments to avoid duplication
1585 * Description: It loops through the given list of FITS files and searches for files
1586 * that relate to the Reference Pixels Alignment template. It then creates
1587 * a batch file with the following format:
1588 *
1589 * <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC REFPIX
1590 * <FITS File 1>
1591 * <FITS File 2>
1592 * <.>
1593 * <.>
1594 * <FITS File n>
1595 * END_OF_BATCH
1596 *
1597 * History:
1598 * 14-Jun-04 (csabet) created
1599 ******************************************************************************/
1600 void getRefPixBatch (
1601  ImageFormat *format, // In: Format structure
1602  FILE *inFitsTempPtr, // In: Pointer to the main buffer
1603  FILE *inFitsListPtr, // In: Pointer to the list of FITS files
1604  char *fileNameStr, // In: Name of the first FITS file
1605  char *dummyClassif, // In: Dummy classification
1606  FILE *filePtr, // In: Pointer to the log file
1607  int *localBatchNum, // IO: Local batch number
1608  int *fileNumInList, // IO: pointer to file number
1609  int *error) // Ou: Error status
1610 {
1611 
1612  // Local Declarations
1613  // ------------------
1614  const char routine[] = "getRefPixBatch";
1615  char *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
1616  FILE *inFitsBatchTempPtr=NULL;
1617  int numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
1618  localFileNumInList;
1619  long inFitsListPosition;
1620 
1621  // Algorithm
1622  // ---------
1623  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1624  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
1625 
1626  // Reset status
1627  *error = 0;
1628  numOfDataFiles = 0;
1629  numOfEmptyFiles = 0;
1630 
1631  // Allocate memory
1632  inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1633  localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1634  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1635 
1636  // Open a batch temporary file for writing
1637  sprintf (inFitsBatchTemp, "localBuffer.log");
1638  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
1639  {
1640  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1641  *error = 1;
1642  free (inFitsBatchTemp);
1643  free (localTplStart);
1644  free (stringTemp);
1645  return;
1646  }
1647 
1648  // Save the first file and its parameters
1649  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1650  fileNumInTemplate = format->fileNumInTemplate;
1651  sscanf (format->tplStart, "%s", localTplStart);
1652  numOfDataFiles++;
1653  localFileNumInList = *fileNumInList;
1654  inFitsListPosition = ftell (inFitsListPtr);
1655  sprintf (batchTemplate, "%s", "REFPIX");
1656 
1657  // Get the rest of the batch files from the list
1658  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
1659  {
1660  sprintf (dummyClassif, "%s", "");
1661  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1662 
1663  localFileNumInList++;
1664  fileNumInTemplate++;
1665 
1666  // Get FITS file parameters. If ok copy into the batch temporary file
1667  getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
1668 
1669  // We expect possibly more split files
1670  if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1671  (strcmp (format->obsTech, "IMAGE") == 0) &&
1672  (strcmp (format->obsType, "FMTCHCK") == 0) &&
1673  (strcmp (format->tplStart, localTplStart) == 0) &&
1674  (format->fileNumInTemplate == fileNumInTemplate))
1675  {
1676  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1677  numOfDataFiles++;
1678  }
1679  else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1680  (strcmp (format->obsTech, "OTHER") == 0) &&
1681  (strcmp (format->obsType, "OTHER") == 0) &&
1682  (strcmp (format->tplStart, localTplStart) == 0) &&
1683  (format->fileNumInTemplate == fileNumInTemplate) &&
1684  (numOfEmptyFiles < 1))
1685  {
1686  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1687  numOfEmptyFiles++;
1688  break;
1689  }
1690  else
1691  {
1692  // We have not found a complete set. Hence reset the file position
1693  fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
1694  localFileNumInList--;
1695  break;
1696  }
1697  inFitsListPosition = ftell (inFitsListPtr);
1698  }
1699 
1700  // If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
1701  if (!endOfTemplate) numOfEmptyFiles = 1;
1702 
1703  // We expect at least one data file and one empty file. If there has been
1704  // a complete set of files then copy all files into the main temporary file
1705  if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
1706  {
1707  // Reopen the batch temporary file for reading
1708  if (inFitsBatchTempPtr)
1709  {
1710  fclose (inFitsBatchTempPtr);
1711  inFitsBatchTempPtr = NULL;
1712  }
1713  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
1714  {
1715  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1716  *error = 1;
1717  free (inFitsBatchTemp);
1718  free (localTplStart);
1719  free (stringTemp);
1720  return;
1721  }
1722 
1723  (*localBatchNum)++;
1724  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1725  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
1726  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
1727  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
1728  {
1729  sprintf (dummyClassif, "%s", "");
1730  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1731  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
1732  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
1733  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1734  }
1735  cpl_msg_info(cpl_func,"\n");
1736  fprintf (filePtr, "\n");
1737 
1738  // We should now have a complete set of files
1739  // in the temporary file list. Add an appending keyword
1740  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
1741  }
1742  *fileNumInList = localFileNumInList;
1743  if (inFitsBatchTempPtr)
1744  {
1745  fclose (inFitsBatchTempPtr);
1746  inFitsBatchTempPtr = NULL;
1747  }
1748  remove (inFitsBatchTemp);
1749 
1750  // Release memory
1751  if (inFitsBatchTemp) free (inFitsBatchTemp);
1752  if (localTplStart) free (localTplStart);
1753  if (stringTemp) free (stringTemp);
1754 
1755  return;
1756 }
1757 /*****************************************************************************/
1758 
1759 
1760 /******************************************************************************
1761 * European Southern Observatory
1762 * VLTI MIDI Maintenance Templates Software
1763 *
1764 * Module name: getWavCalBatch
1765 * Input/Output: See function arguments to avoid duplication
1766 * Description: It loops through the given list of FITS files and searches for files
1767 * that relate to the Wavelength Calibration template. It then creates
1768 * a batch file with the following format:
1769 *
1770 * <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC WAVECAL
1771 * <FITS File 1>
1772 * <FITS File 2>
1773 * <.>
1774 * <.>
1775 * <FITS File n>
1776 * END_OF_BATCH
1777 *
1778 * History:
1779 * 02-Jun-05 (csabet) created
1780 ******************************************************************************/
1781 void getWavCalBatch (
1782  ImageFormat *format, // In: Format structure
1783  FILE *inFitsTempPtr, // In: Pointer to the main buffer
1784  FILE *inFitsListPtr, // In: Pointer to the list of FITS files
1785  char *fileNameStr, // In: Name of the first FITS file
1786  char *dummyClassif, // In: Dummy classification
1787  FILE *filePtr, // In: Pointer to the log file
1788  int *localBatchNum, // IO: Local batch number
1789  int *fileNumInList, // IO: pointer to file number
1790  int *error) // Ou: Error status
1791 {
1792 
1793  // Local Declarations
1794  // ------------------
1795  const char routine[] = "getWavCalBatch";
1796  char *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
1797  FILE *inFitsBatchTempPtr=NULL;
1798  int numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
1799  localFileNumInList;
1800  long inFitsListPosition;
1801 
1802  // Algorithm
1803  // ---------
1804  if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1805  if (diagnostic > 4) fprintf(filePtr, "Invoking routine '%s' \n", routine);
1806 
1807  // Reset status
1808  *error = 0;
1809  numOfDataFiles = 0;
1810  numOfEmptyFiles = 0;
1811 
1812  // Allocate memory
1813  inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1814  localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1815  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1816 
1817  // Open a batch temporary file for writing
1818  sprintf (inFitsBatchTemp, "localBuffer.log");
1819  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
1820  {
1821  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1822  *error = 1;
1823  free (inFitsBatchTemp);
1824  free (localTplStart);
1825  free (stringTemp);
1826  return;
1827  }
1828 
1829  // Save the first file and its parameters
1830  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1831  fileNumInTemplate = format->fileNumInTemplate;
1832  sscanf (format->tplStart, "%s", localTplStart);
1833  numOfDataFiles++;
1834  localFileNumInList = *fileNumInList;
1835  inFitsListPosition = ftell (inFitsListPtr);
1836  sprintf (batchTemplate, "%s", "WAVECAL");
1837 
1838  // Get the rest of the batch files from the list
1839  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
1840  {
1841  sprintf (dummyClassif, "%s", "");
1842  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1843 
1844  localFileNumInList++;
1845  fileNumInTemplate++;
1846 
1847  // Get FITS file parameters. If ok copy into the batch temporary file
1848  getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
1849 
1850  // We expect possibly more split files
1851  if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1852  (strcmp (format->obsTech, "SPECTRUM") == 0) &&
1853  (strcmp (format->obsType, "WAVE,SPECTEMPL") == 0) &&
1854  (strcmp (format->tplStart, localTplStart) == 0) &&
1855  (format->fileNumInTemplate == fileNumInTemplate))
1856  {
1857  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1858  numOfDataFiles++;
1859  }
1860  else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
1861  (strcmp (format->obsTech, "OTHER") == 0) &&
1862  (strcmp (format->obsType, "OTHER") == 0) &&
1863  (strcmp (format->tplStart, localTplStart) == 0) &&
1864  (format->fileNumInTemplate == fileNumInTemplate) &&
1865  (numOfEmptyFiles < 1))
1866  {
1867  fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1868  numOfEmptyFiles++;
1869  break;
1870  }
1871  else
1872  {
1873  // We have not found a complete set. Hence reset the file position
1874  fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
1875  localFileNumInList--;
1876  break;
1877  }
1878  inFitsListPosition = ftell (inFitsListPtr);
1879  }
1880 
1881  // If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
1882  if (!endOfTemplate) numOfEmptyFiles = 1;
1883 
1884  // We expect at least one data file and one empty file. If there has been
1885  // a complete set of files then copy all files into the main temporary file
1886  if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
1887  {
1888  // Reopen the batch temporary file for reading
1889  if (inFitsBatchTempPtr)
1890  {
1891  fclose (inFitsBatchTempPtr);
1892  inFitsBatchTempPtr = NULL;
1893  }
1894  if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
1895  {
1896  midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
1897  *error = 1;
1898  free (inFitsBatchTemp);
1899  free (localTplStart);
1900  free (stringTemp);
1901  return;
1902  }
1903 
1904  (*localBatchNum)++;
1905  cpl_msg_info(cpl_func,"\nSelected batch %d is %s:\n", (*localBatchNum), batchTemplate);
1906  fprintf (filePtr, "\nSelected batch %d: is %s\n", (*localBatchNum), batchTemplate);
1907  fprintf (inFitsTempPtr, "%s \n", batchTemplate);
1908  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
1909  {
1910  sprintf (dummyClassif, "%s", "");
1911  sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
1912  cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
1913  fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
1914  fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
1915  }
1916  cpl_msg_info(cpl_func,"\n");
1917  fprintf (filePtr, "\n");
1918 
1919  // We should now have a complete set of files
1920  // in the temporary file list. Add an appending keyword
1921  fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
1922  }
1923  *fileNumInList = localFileNumInList;
1924  if (inFitsBatchTempPtr)
1925  {
1926  fclose (inFitsBatchTempPtr);
1927  inFitsBatchTempPtr = NULL;
1928  }
1929  remove (inFitsBatchTemp);
1930 
1931  // Release memory
1932  if (inFitsBatchTemp) free (inFitsBatchTemp);
1933  if (localTplStart) free (localTplStart);
1934  if (stringTemp) free (stringTemp);
1935 
1936  return;
1937 }
1938 /*****************************************************************************/