MIDI Pipeline Reference Manual  2.8.3
midiControl.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: midiControl.c
7 * Description: Contains the main program and routines for data reduction
8 *
9 * History:
10 * 10-Jan-05 (csabet) Created
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /******************************************************************************
15 * Compiler directives
16 ******************************************************************************/
17 
18 /******************************************************************************
19 * Include files
20 ******************************************************************************/
21 
22 #include <sys/stat.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include "cpl_init.h"
28 #include "midiGlobal.h"
29 #include "midiLib.h"
30 #include "midiControl.h"
31 #include "memoryHandling.h"
32 #include "procFrgHS.h"
33 #include "procFrgSP.h"
34 #include "procKappa.h"
35 #include "procAcq.h"
36 #include "procDetLin.h"
37 #include "procDetRon.h"
38 #include "procRefPix.h"
39 #include "procDspTrn.h"
40 #include "procWavCal.h"
41 #include "errorHandling.h"
42 #include "fileHandling.h"
43 #include "midiFitsClassification.h"
44 #include "calibration.h"
45 #include "diagnostics.h"
46 #include <cpl.h>
47 #include "midi_dfs.h"
48 
49 /**********************************************************
50 * Global Variables
51 **********************************************************/
52 int rootName = 1; // Output root name is given
53 int diagnostic = 0; // Set by command line argument DEBUG (argumentFile.lst)
54 int endOfTemplate = 0; // Set by command line argument EOT (argumentFile.lst)
55 int plotFile = 2; // Set by command line argument PLOT_FILE (argumentFile.lst)
56 int plotDuration = 0; // Set by command line argument PLOT_FILE (argumentFile.lst)
57 int batchNumber = 0; // It is used everywhere
58 FILE *midiReportPtr=NULL; // File pointer for the program execution history
59 char *midiReportName=NULL; // Name of event log file
60 FILE *midiQcLogPtr=NULL; // File pointer for the QC log file
61 char *midiQcLogName=NULL; // Name of QC log file
62 char outFileDir[MAX_STRING_LENGTH]; // Product directory name
63 char outRootName[MAX_STRING_LENGTH]; // Root name for all output files
64 char batchTemplate[MAX_STRING_LENGTH]; // A consistent Batch Template Name
65 char givenName[MAX_STRING_LENGTH]; // User given name for all output files
66 char midiMessage[MAX_STRING_LENGTH]; // A global message holder
67 int *badChannelList; // An array of numOfChannels
68 
69 /*============================ C O D E A R E A ===========================*/
70 
71 
72 /******************************************************************************
73 * European Southern Observatory
74 * VLTI MIDI Data Reduction Software
75 *
76 * Module name: executeDataReduction
77 * Input/Output: See function arguments to avoid duplication
78 * Description: This is the main routine for the MIDI Data Reduction. It shall be
79 * invoked by one of the recipes. This rootine and the way user parameters
80 * are processed is not at the moment efficient. It has been adapted from the
81 * stand-alone version of the MIDI quickly to get the recipes running.
82 *
83 * History:
84 * 09-Jan-07 (csabet) Created
85 ******************************************************************************/
86 void executeDataReduction (
87  const char *calibTempDir, // In: CalibDb directory (full path-name)
88  const char *maskFileDir, // In: Mask file directory (full path-name)
89  const char *productDir, // In: Name of the product directory
90  int givenDispDur, // In: Given plot duration
91  const char *inFitsList, // In: Name of file containing input FITS files (full path-name)
92  int *error, // Ou: Error status
93  cpl_parameterlist *parlist,
94  cpl_frameset *frameset)
95 {
96 
97  // Local Declarations
98  // ------------------
99  const char routine[] = "executeDataReduction";
100  FILE *inFitsClassifiedPtr=NULL, *inFitsBatchPtr=NULL, *tempReportPtr=NULL;
101  char *stringTemp=NULL, *tempReportName=NULL, *fileNameStr=NULL,
102  *classification=NULL;
103  MidiFiles *fileNames=NULL;
104  UserOptions *options=NULL;
105  FILE *signaturePtr=NULL;
106 
107  // Algorithm
108  // ---------
109 // cpl_init ();
110  *error = 0;
111 
112  // Allocate memory
113  tempReportName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
114  options = (UserOptions *) calloc (1, sizeof (UserOptions));
115  fileNames = callocMidiFiles (MAX_STRING_LENGTH);
116 
117  // Initialisation
118  sprintf (tempReportName, "midiPipelineTempReport.log");
119  tempReportPtr = fopen (tempReportName, "w");
120  sprintf (outFileDir, "%s", productDir);
121  plotDuration = givenDispDur;
122  if (plotDuration == 0) plotFile = 1;
123  sprintf (givenName, "%s", "MIDI");
124 // cpl_msg_info(cpl_func,"Name of the FITS list = %s \n", inFitsList);
125 
126 
127  getMaskFileFromSof(fileNames,frameset);
128 
129 
130 
131 
132  getParameters (calibTempDir, maskFileDir, inFitsList, tempReportPtr, options, fileNames, error);
133  if (*error)
134  {
135  cpl_msg_info(cpl_func,"\n");
136  fprintf (tempReportPtr, "\n");
137  fclose (tempReportPtr);
138  free (tempReportName);
139  free (options);
140  free (fileNames);
141  return;
142  }
143 
144 
145 
146  // Allocate memory
147  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
148  midiQcLogName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
149  midiReportName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
150  fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
151  classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
152 
153  // Classify FITS files
154  // -------------------
155  classifyFitsFiles (options, fileNames, tempReportPtr, error,
156  parlist, frameset);
157  if (*error)
158  {
159  if (*error > 1)
160  {
161  sprintf (midiMessage, "No FITS files to process. Check log file: %s", tempReportName);
162  midiReportWarning (tempReportPtr, routine, __FILE__, __LINE__, midiMessage);
163  }
164  cpl_msg_info(cpl_func,"\n");
165  fprintf (tempReportPtr, "\n");
166  fclose (tempReportPtr);
167  freeMidiFiles (fileNames);
168  free (options);
169  free (stringTemp);
170  free (tempReportName);
171  free (midiQcLogName);
172  free (midiReportName);
173  free (fileNameStr);
174  free (classification);
175  return;
176  }
177 
178  /*====================================================================================
179  At this point a file containing the names of input fits files should be available. Here
180  the following assumptions are made:
181 
182  1. Interferometry FITS files may be split into more than one file
183  2. Data Organizer's list of files are consecutive in types. Mixed split files are
184  not allowed
185  3. All file categories are processed separately and thier results are stored in
186  either memory or relevant output FITS files.
187  4. A Process is only invoked when necessary data is available
188  5. Photometry FITS files can also be split. However the number of Photometry A files
189  must be equal to the number of Photometry B files.
190  6. The order of Photometry and Interferometry in the above list is not important.
191  ====================================================================================*/
192 
193  // Open the classified list of FITS files for reading
194  if ((inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "r")) == NULL)
195  midiReportError (tempReportPtr, routine, __FILE__, __LINE__, "Cannot open list of classified input FITS files");
196 
197  // Loop through the list of classified FITS files and process
198  // each batch. The first record in the file is the name of the template
199  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
200  {
201  // Extract cleaned up batchTemplate string and save it
202  removeNewLine (stringTemp, batchTemplate);
203 
204  // Create a batch file
205  sprintf (fileNames->inFitsBatch, "%s", "inputFitsBatch.log");
206  if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "w")) == NULL)
207  midiReportError (tempReportPtr, routine, __FILE__, __LINE__, "Cannot open batch FITS files");
208 
209  // Copy the rest of files from the classified-list into the batch-list until the END_OF_BATCH
210  while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
211  {
212  sprintf (classification, "%s", "");
213  sscanf (stringTemp, "%s%s", fileNameStr, classification);
214  if (strcmp (fileNameStr, "END_OF_BATCH") != 0)
215  fprintf (inFitsBatchPtr, "%s %s\n", fileNameStr, classification);
216  else
217  break;
218  }
219  fclose (inFitsBatchPtr);
220 
221  // Increment batch counter
222  batchNumber=1;
223 
224  // Create output file names
225  createOutputFileNames (fileNames, tempReportPtr, error);
226  if (*error)
227  {
228  midiReportWarning (tempReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
229  continue; // Abandon the current batch and get the next batch
230  }
231  handoverReport (tempReportName, &tempReportPtr);
232 
233  // Create Mask FITS file
234  // ---------------------
235  if (options->maskMode == COMPUTED)
236  {
237  createMaskFile (fileNames, error);
238  if (*error)
239  {
240  midiReportWarning (tempReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
241  midiRejectBatch ();
242  continue; // Abandon the current batch and get the next batch
243  }
244  }
245 
246  // Process templates
247  // -----------------
248  if ((strcmp (batchTemplate, "HIGH_SENS_CALIB") == 0) ||
249  (strcmp (batchTemplate, "HIGH_SENS_SCIENCE") == 0))
250  {
251  if (options->drsFrg) procFrgHS (fileNames, options, error, parlist, frameset);
252  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
253  }
254  else if (strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0)
255  {
256  if (options->drsKap)
257  {
258  // Write a signature. Midas checks to see which template is running
259  signaturePtr = fopen ("MIDI_sig_spk.log", "w");
260  fclose (signaturePtr);
261  procKappa (fileNames, options, error);
262  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
263  }
264  }
265  else if ((strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0) ||
266  (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0))
267  {
268  if (options->drsFrg)
269  {
270  if (strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0)
271  signaturePtr = fopen ("MIDI_sig_spc.log", "w");
272  else
273  signaturePtr = fopen ("MIDI_sig_sps.log", "w");
274  fclose (signaturePtr);
275  procFrgSP (fileNames, options, error, parlist, frameset);
276  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
277  }
278  }
279  else if (strcmp (batchTemplate, "ACQ") == 0)
280  {
281  if (options->drsAcq)
282  procAcq (fileNames, error);
283  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
284  "This batch is rejected");
285  }
286  else if (strcmp (batchTemplate, "DETLIN") == 0)
287  {
288  if (options->tecLin)
289  procDetLin (fileNames, error);
290  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
291  "This batch is rejected");
292  }
293  else if (strcmp (batchTemplate, "DETRON") == 0)
294  {
295  if (options->tecRon)
296  procDetRon (fileNames, error);
297  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
298  "This batch is rejected");
299  }
300  else if (strcmp (batchTemplate, "DSPTRN") == 0)
301  {
302  if (options->tecTrn)
303  procDspTrn (fileNames, error);
304  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
305  "This batch is rejected");
306  }
307  else if (strcmp (batchTemplate, "REFPIX") == 0)
308  {
309  if (options->tecPix)
310  procRefPix (fileNames, error);
311  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
312  "This batch is rejected");
313  }
314  else if (strcmp (batchTemplate, "WAVECAL") == 0)
315  {
316  if (options->tecWav)
317  procWaveCal (options->tecWav, fileNames, error);
318  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
319  "This batch is rejected");
320  }
321  else
322  {
323  sprintf (midiMessage, "Cannot process template: %s", batchTemplate);
324  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
325  *error = 1;
326  }
327 
328  // Close log files for this batch
329  if (*error) midiRejectBatch ();
330  else midiCloseBatch ();
331 
332  if (strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0)
333  {
334  /* The empty dummy file MIDI_b1_spk.qc.dummy has to be erased */
335  remove (midiQcLogName);
336 
337  continue;
338  }
339  break;
340 
341  }
342 
343  // Close temporary files and release memory
344  // ----------------------------------------
345  if (diagnostic) cpl_msg_info(cpl_func,"\nClosing temporary files \n");
346  if (diagnostic) cpl_msg_info(cpl_func,"----------------------- \n");
347  if (diagnostic) cpl_msg_info(cpl_func,"\n\n");
348  fclose (inFitsClassifiedPtr);
349  fclose (tempReportPtr);
350  remove (fileNames->inFitsClassified);
351  remove (fileNames->inFitsBatch);
352  remove (tempReportName);
353 
354  // Release remaining memory
355  freeMidiFiles (fileNames);
356  free (options);
357  free (stringTemp);
358  free (tempReportName);
359  free (midiQcLogName);
360  free (midiReportName);
361  free (fileNameStr);
362  free (classification);
363 // cpl_end ();
364 
365  return;
366 }
367 /*****************************************************************************/
368 
369 
370 /******************************************************************************
371 * European Southern Observatory
372 * VLTI MIDI Data Reduction Software
373 *
374 * Module name: midiRejectBatch
375 * Input/Output: See function arguments to avoid duplication
376 * Description: Garbage collector
377 *
378 * History:
379 * 17-May-05 (csabet) Created
380 ******************************************************************************/
381 void midiRejectBatch (void)
382 {
383  // Local Declarations
384  // ------------------
385  const char routine[] = "midiRejectBatch";
386 
387  // Algorithm
388  // ---------
389  if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
390  if (diagnostic > 2) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
391 
392  // Close log File and release memory for this batch
393  // ------------------------------------------------
394  if (diagnostic) cpl_msg_info(cpl_func,"Created Log files: %s \n", midiReportName);
395  fprintf (midiReportPtr, "Created Log files: %s \n", midiReportName);
396  if (diagnostic) cpl_msg_info(cpl_func,"\n");
397  fprintf (midiReportPtr, "\n");
398  if (midiReportPtr)
399  {
400  fclose (midiReportPtr);
401  midiReportPtr = NULL;
402  }
403  if (midiQcLogPtr)
404  {
405  fclose (midiQcLogPtr);
406  midiQcLogPtr = NULL;
407  remove (midiQcLogName);
408  }
409 
410  return;
411 }
412 /*****************************************************************************/
413 
414 
415 /******************************************************************************
416 * European Southern Observatory
417 * VLTI MIDI Data Reduction Software
418 *
419 * Module name: midiCloseBatch
420 * Input/Output: See function arguments to avoid duplication
421 * Description: Garbage collector
422 *
423 * History:
424 * 17-May-05 (csabet) Created
425 ******************************************************************************/
426 void midiCloseBatch (void)
427 {
428  // Local Declarations
429  // ------------------
430  const char routine[] = "midiCloseBatch";
431 
432  // Algorithm
433  // ---------
434  if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
435  if (diagnostic > 2) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
436 
437  // Close log File and release memory for this batch
438  // Close files used in this batch
439  cpl_msg_info(cpl_func,"Created Log files: %s %s \n", midiReportName, midiQcLogName);
440  fprintf (midiReportPtr, "Created Log files: %s %s \n", midiReportName, midiQcLogName);
441  cpl_msg_info(cpl_func,"\n\n");
442  fprintf (midiReportPtr, "\n\n");
443  if (midiReportPtr)
444  {
445  fclose (midiReportPtr);
446  midiReportPtr = NULL;
447  }
448  if (midiQcLogPtr)
449  {
450  fclose (midiQcLogPtr);
451  midiQcLogPtr = NULL;
452  }
453 
454  return;
455 }
456 /*****************************************************************************/
457 
458 
459 /******************************************************************************
460 * European Southern Observatory
461 * VLTI MIDI Data Reduction Software
462 *
463 * Module name: getparameters
464 * Input/Output: See function arguments to avoid duplication
465 * Description: Gets user options
466 *
467 * History:
468 * 24-Sep_03 (csabet) Removed OPDstep. It shall be derived from the FITS header
469 * 03-Mar-03 (csabet) Created
470 ******************************************************************************/
471 void getParameters (
472  const char *calibTempDir, // In: CalibDb directory
473  const char *maskFileDir, // In: Mask file directory
474  const char *inFitsList, // In: Name of file containing input FITS files
475  FILE *filePtr, // In: Pointer to a temporary open file
476  UserOptions *options, // IO: Pointer to the user options
477  MidiFiles *fileNames, // IO: Pointer to midi file structure
478  int *error) // Ou: Error status
479 
480 {
481 
482  // Local Declarations
483  // ------------------
484  const char routine[] = "getParameters";
485  FILE *inFitsListPtr=NULL, *inFitsClassifiedPtr=NULL;
486  char *argument=NULL, *value=NULL, *tempStr=NULL;
487 
488  // Algorithm
489  // ---------
490  *error = 0;
491 
492  // Allocate memory
493  tempStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
494  argument = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
495  value = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
496 
497  // Set default variables
498  // ---------------------
499  sprintf (fileNames->inFileDir, "%s", "");
500  sprintf (fileNames->trfNameRead, "%s", "/tmp/MIDI.trf.dummy");
501  sprintf (fileNames->trfNameWrite, "%s", "/tmp/MIDI.trf.dummy");
502  sprintf (fileNames->wclNameRead, "%s", "/tmp/MIDI.wcl.dummy");
503  sprintf (fileNames->wclNameWrite, "%s", "/tmp/MIDI.wcl.dummy");
504  sprintf (fileNames->trrNameRead, "%s", "/tmp/MIDI.trr.dummy");
505  sprintf (fileNames->trrNameWrite, "%s", "/tmp/MIDI.trr.dummy");
506  sprintf (fileNames->calibDbDir, "%s", calibTempDir);
507  sprintf (fileNames->maskFileDir, "%s", maskFileDir);
508  sprintf (fileNames->inFitsList, "%s", inFitsList);
509  options->processing = DISPERSED;
510  options->drsFrg = 1;
511  options->drsAcq = 1;
512  options->drsKap = 0;
513  options->tecRon = 1;
514  options->tecLin = 1;
515  options->tecPix = 1;
516  options->tecTrn = 1;
517  options->tecWav = 1;
518 /* options->maskMode = SELECTED; */
519  options->maskMode = SPECIFIED;
520 
521  // Display banner, time and date
522  displayBanner (filePtr);
523 
524  if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
525 
526  // Create names for calibrator data files
527  sprintf (fileNames->calibDbName, "%scalibrator_database.dat", fileNames->calibDbDir);
528  sprintf (fileNames->waveCalibName, "%swaveCalib", fileNames->calibDbDir);
529  sprintf (fileNames->transferRatiosName, "%stransferRatios", fileNames->calibDbDir);
530  sprintf (fileNames->grismFoilName, "%sgrism_foil_4lines.dat", fileNames->calibDbDir);
531  sprintf (fileNames->prismFoilName, "%sprism_foil_4lines.dat", fileNames->calibDbDir);
532 
533  // Create a name for the classified file
534  strcpy (fileNames->inFitsClassified, "inputFitsClassified.log");
535 
536 // cpl_msg_info(cpl_func,"Name of the FITS list = %s \n", fileNames->inFitsList);
537  // Check if the input FITS file list exists and it is not empty
538  if (((inFitsListPtr = fopen(fileNames->inFitsList, "r")) != NULL) &&
539  (fscanf (inFitsListPtr, "%s\n", argument) != EOF))
540  {
541  // Create a file for the classified list
542  rewind (inFitsListPtr);
543  inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "w");
544 
545  while (fgets (tempStr, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
546  {
547  // Clear string
548  sprintf (value, "%s", "");
549  sscanf (tempStr, "%s%s", argument, value);
550  fprintf (inFitsClassifiedPtr, "%s %s\n", argument, value);
551  }
552  fclose (inFitsListPtr);
553  fclose (inFitsClassifiedPtr);
554  }
555  else
556  {
557  cpl_msg_info(cpl_func,"\n<<Error>>: List of FITS file is not given or the list is empty \n");
558  cpl_msg_info(cpl_func,"\n");
559  fprintf(filePtr, "\n<<Error>>: List of FITS file is not given or the list is empty\n");
560  fprintf(filePtr, "\n");
561  *error = 1;
562  if (argument) free (argument);
563  if (tempStr) free (tempStr);
564  if (value) free (value);
565  return;
566  }
567 
568  // Report the contents of the list
569  inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "r");
570  cpl_msg_info(cpl_func,"\n");
571  cpl_msg_info(cpl_func,"The FITS list contains the following files:\n");
572  cpl_msg_info(cpl_func,"-------------------------------------------\n");
573  fprintf (filePtr, "\n");
574  fprintf (filePtr, "Given FITS list contains the following files:\n");
575  fprintf (filePtr, "---------------------------------------------\n");
576  while (fgets (tempStr, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
577  {
578  // Clear string
579  sprintf (value, "%s", "");
580  sscanf (tempStr, "%s%s", argument, value);
581  cpl_msg_info(cpl_func,"%s %s\n", argument, value);
582  fprintf (filePtr, "%s %s\n", argument, value);
583  }
584  fclose (inFitsClassifiedPtr);
585 
586  // Release memory
587  if (argument) free (argument);
588  if (tempStr) free (tempStr);
589  if (value) free (value);
590 
591  return;
592 }
593 /******************************************************************************/
594 
595 
596 
597 
598 /******************************************************************************
599 * European Southern Observatory
600 * VLTI MIDI Data Reduction Software
601 *
602 * Module name: createOutputFileNames
603 * Input/Output: See function arguments to avoid duplication
604 * Description: Creates names for the product FITS files
605 *
606 * History:
607 * 03-Mar-03 (csabet) Created
608 ******************************************************************************/
609 void createOutputFileNames (
610  MidiFiles *fileNames, // IO: Pointer to midi files structure
611  FILE *filePtr,
612  int *error)
613 
614 {
615 
616  // Local Declarations
617  // ------------------
618  const char routine[] = "createOutputFileNames";
619  FILE *inFitsBatchPtr=NULL;
620  size_t stringLength;
621  char *fileTemp=NULL, *classification=NULL, *pipeFilePrefix=NULL,
622  *fileNameStr=NULL, *suffix=NULL, *pipeRootName=NULL;
623 
624  // Algorithm
625  // ---------
626  if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
627  if (diagnostic > 2) fprintf (filePtr, "Invoking routine '%s' \n", routine);
628 
629  // Reset parameters
630  *error = 0;
631 
632  // Allocate memory
633  fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
634  suffix = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
635  fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
636  classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
637  pipeFilePrefix = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
638  pipeRootName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
639 
640  // Create the prefix for the PIPEFILE
641  sprintf (pipeFilePrefix, "r.");
642 
643  // Get the pipeline root name (pipeRootName) from the first FITS file in the batch
644  if ((inFitsBatchPtr = fopen(fileNames->inFitsBatch, "r")) == NULL)
645  midiReportError (filePtr, routine, __FILE__, __LINE__, "Cannot open FITS file list");
646 
647  fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr);
648  sprintf (classification, "%s", "");
649  sscanf (fileTemp, "%s%s", fileNameStr, classification);
650  stringLength = strlen (fileNameStr);
651  strncpy (pipeRootName, fileNameStr, stringLength-5);
652 
653  // Strip off the path name if any and Create PIPEFILE and
654  // ARCHFILE. Always from the root name of the first FITS file
655  removePathName (pipeRootName, filePtr);
656  if (inFitsBatchPtr) fclose (inFitsBatchPtr);
657  sprintf (fileNames->archFileName, "%s.fits", pipeRootName);
658  sprintf (fileNames->pipeFileName, "%s%s_tpl_0000.fits", pipeFilePrefix, pipeRootName);
659 
660  // Create template suffix
661  if (strcmp (batchTemplate, "HIGH_SENS_CALIB") == 0)
662  {
663  sprintf (suffix, "_b%d_hsc", batchNumber);
664  }
665  else if (strcmp (batchTemplate, "HIGH_SENS_SCIENCE") == 0)
666  {
667  sprintf (suffix, "_b%d_hss", batchNumber);
668  }
669  else if (strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0)
670  {
671  sprintf (suffix, "_b%d_spc", batchNumber);
672  }
673  else if (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0)
674  {
675  sprintf (suffix, "_b%d_sps", batchNumber);
676  }
677  else if (strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0)
678  {
679  sprintf (suffix, "_b%d_spk", batchNumber);
680  }
681  else if (strcmp (batchTemplate, "ACQ") == 0)
682  {
683  sprintf (suffix, "_b%d_acq", batchNumber);
684  }
685  else if (strcmp (batchTemplate, "DETLIN") == 0)
686  {
687  sprintf (suffix, "_b%d_lin", batchNumber);
688  }
689  else if (strcmp (batchTemplate, "DETRON") == 0)
690  {
691  sprintf (suffix, "_b%d_ron", batchNumber);
692  }
693  else if (strcmp (batchTemplate, "DSPTRN") == 0)
694  {
695  sprintf (suffix, "_b%d_trn", batchNumber);
696  }
697  else if (strcmp (batchTemplate, "REFPIX") == 0)
698  {
699  sprintf (suffix, "_b%d_pix", batchNumber);
700  }
701  else if (strcmp (batchTemplate, "WAVECAL") == 0)
702  {
703  sprintf (suffix, "_b%d_wav", batchNumber);
704  }
705  else
706  {
707  *error = 1;
708  sprintf (midiMessage, "Unknown Template: %s", batchTemplate);
709  midiReportWarning (filePtr, routine, __FILE__, __LINE__, midiMessage);
710  free (fileTemp);
711  free (suffix);
712  free (classification);
713  free (fileNameStr);
714  free (pipeFilePrefix);
715  free (pipeRootName);
716  return;
717  }
718  if (!rootName) sprintf (outRootName, "%s%s", pipeRootName, suffix);
719  else sprintf (outRootName, "%s%s", givenName, suffix);
720 
721  sprintf (midiReportName, "%s%s.log", outFileDir, outRootName);
722  sprintf (midiQcLogName, "%s%s.qc.dummy", outFileDir, outRootName);
723  sprintf (fileNames->trfHistoryName, "%s%s.trfh.dummy", outFileDir, outRootName);
724  sprintf (fileNames->wclHistoryName, "%s%s.wclh.dummy", outFileDir, outRootName);
725  sprintf (fileNames->trrHistoryName, "%s%s.trrh.dummy", outFileDir, outRootName);
726  sprintf (fileNames->outFitsName, "%s%s.pro.fits", outFileDir, outRootName);
727  sprintf (fileNames->outWaterfallName, "%s%s.waf.fits", outFileDir, outRootName);
728  sprintf (fileNames->outWaterpowerName, "%s%s.wap.fits", outFileDir, outRootName);
729  sprintf (fileNames->outAveImageName, "%s%s.aim.fits", outFileDir, outRootName);
730  sprintf (fileNames->outChSpectrumName, "%s%s.chs.fits", outFileDir, outRootName);
731 
732  // Release memory
733  if (fileTemp) free (fileTemp);
734  if (suffix) free (suffix);
735  if (classification) free (classification);
736  if (fileNameStr) free (fileNameStr);
737  if (pipeFilePrefix) free (pipeFilePrefix);
738  if (pipeRootName) free (pipeRootName);
739 
740  return;
741 }
742 /******************************************************************************/
743 
744 
745 
746 /******************************************************************************
747 * European Southern Observatory
748 * VLTI MIDI Data Reduction Software
749 *
750 * Module name: displayBanner
751 * Input/Output: See function arguments to avoid duplication
752 * Description: Displays the ESO Banner
753 *
754 * History:
755 * 23-Jul-03 (csabet) Created
756 ******************************************************************************/
757 void displayBanner (
758  FILE *filePtr)
759 
760 {
761 
762  /* Local Declarations
763  --------------------*/
764  const char routine[] = "displayBanner";
765  time_t now;
766  struct tm *newTime=NULL;
767  char *currentTime=NULL;
768 
769  /* Algorithm
770  -----------*/
771  if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
772  if (diagnostic > 2) fprintf (filePtr, "Invoking routine '%s' \n", routine);
773 
774  /* Allocate memory */
775  currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
776 
777  now = time(NULL);
778  newTime = gmtime (&now);
779  strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
780 
781  cpl_msg_info(cpl_func,"\n\n\n\n");
782  cpl_msg_info(cpl_func," ,----------------------------------------------.\n");
783  cpl_msg_info(cpl_func," | * European Southern Observatory |\n");
784  cpl_msg_info(cpl_func," | *E S* VLTI DMD, Garching |\n");
785  cpl_msg_info(cpl_func," | O MIDI Data Reduction Program |\n");
786  cpl_msg_info(cpl_func," | * Version: %s\n", MIDI_PIPE_VERSION);
787  cpl_msg_info(cpl_func," | |\n");
788  cpl_msg_info(cpl_func," | For support e-mail to: |\n");
789  cpl_msg_info(cpl_func," | pballest@eso.org or agabasch@eso.org |\n");
790  cpl_msg_info(cpl_func," `----------------------------------------------'\n");
791  cpl_msg_info(cpl_func,"\nMIDI pipeline started on: %s GMT \n", currentTime);
792 
793  fprintf (filePtr, "\n\n\n\n");
794  fprintf (filePtr, " ,----------------------------------------------.\n");
795  fprintf (filePtr, " | * European Southern Observatory |\n");
796  fprintf (filePtr, " | *E S* VLTI DMD, Garching |\n");
797  fprintf (filePtr, " | O MIDI Data Reduction Program |\n");
798  fprintf (filePtr, " | * Version: %s\n", MIDI_PIPE_VERSION);
799  fprintf (filePtr, " | |\n");
800  fprintf (filePtr, " | For support e-mail to: |\n");
801  fprintf (filePtr, " | pballest@eso.org or agabasch@eso.org |\n");
802  fprintf (filePtr, " `----------------------------------------------'\n");
803  fprintf (filePtr, "\nMIDI pipeline started on: %s GMT \n", currentTime);
804 
805  /* Release memory */
806  if (currentTime) free (currentTime);
807 
808  return;
809 }
810 /******************************************************************************/
811 
812 
813 
814 /******************************************************************************
815 * European Southern Observatory
816 * VLTI MIDI Data Reduction Software
817 *
818 * Module name: handoverReport
819 * Input/Output: See function arguments to avoid duplication
820 * Description: At the begining of the DRS before user parameters have been
821 * processed all reports are logged in a temporary
822 * file. Afterwards the content of this file is transfered to an
823 * actual report file the name of which is user defined..
824 *
825 * History:
826 * 22-Jul-03 (csabet) Created
827 ******************************************************************************/
828 void handoverReport (
829  char *fileName, /* IO: Pointer to the temporary file */
830  FILE **filePtr)
831 {
832 
833  /* Local Declarations
834  --------------------*/
835  const char routine[] = "handoverReport";
836  char *tempStr=NULL;
837  time_t now;
838  struct tm *newTime=NULL;
839  char *currentTime=NULL;
840  struct stat buf;
841 
842  /* Algorithm
843  -----------*/
844  if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
845  if (diagnostic > 2) fprintf(*filePtr, "Invoking routine '%s' \n", routine);
846 
847  /* Allocate memory */
848  tempStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
849  currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
850 
851  // Now open the following global files for writing
852  if (stat (midiReportName, &buf) == 0) remove (midiReportName); // If the file exist delete it
853  midiReportPtr = fopen(midiReportName, "w");
854  if (!midiReportPtr) midiReportError (*filePtr, routine, __FILE__, __LINE__, "Cannot create output event log file");
855 
856  if (stat (midiQcLogName, &buf) == 0) remove (midiQcLogName); // If the file exist delete it
857  midiQcLogPtr = fopen(midiQcLogName, "w");
858  if (!midiQcLogPtr) midiReportError (*filePtr, routine, __FILE__, __LINE__, "Cannot create output QC log file");
859 
860  fclose (*filePtr);
861 
862  *filePtr = fopen (fileName, "r");
863  while (fgets (tempStr, MAX_STRING_LENGTH, *filePtr) != NULL)
864  fprintf (midiReportPtr, "%s", tempStr);
865 
866  now = time(NULL);
867  newTime = gmtime (&now);
868  strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
869  cpl_msg_info(cpl_func,"\nMIDI Data Reduction for batch %d started on: %s GMT \n", batchNumber, currentTime);
870  fprintf (midiReportPtr, "\nMIDI Data Reduction for batch %d started on: %s GMT \n", batchNumber, currentTime);
871 
872  fclose (*filePtr);
873  *filePtr = fopen (fileName, "a");
874 
875  /* Release memory */
876  if (tempStr) free (tempStr);
877  if (currentTime) free (currentTime);
878 
879  return;
880 }
881 /******************************************************************************/
882 
883 /*----------------------------------------------------------------------------*/
889 /*----------------------------------------------------------------------------*/
890 
891 void getMaskFileFromSof(MidiFiles * fileNames, cpl_frameset * frameset)
892 {
893 
894 
895  cpl_frame * cur_frame;
896  char * tag_firstframe=NULL;
897  char * tag_mask=NULL;
898  char * tag=NULL;
899  char gris_name[100]= "";
900  cpl_propertylist * plist;
901  int i;
902 
903  /* Select the right profile/mask from the SOF and write the
904  * variable fileNames->maskFileName accordingly */
905 
906 
907  /* Identify the RAW and CALIB frames in the input frameset */
908 // cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
909 // cpl_error_get_code());
910 
911  if (midi_dfs_set_groups(frameset) != CPL_ERROR_NONE) {
912  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
913  "Identifying calib and/or raw frames failed");
914  return;
915  }
916 
917  cur_frame = cpl_frameset_get_first(frameset);
918  if (cur_frame == NULL) {
919  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
920  "SOF does not have any file");
921  return;
922  }
923 
924 /* Skip the calibration files and use the first raw file */
925 
926  for(i=0; i<cpl_frameset_get_size(frameset);i++){
927 /* cpl_msg_info(cpl_func, "Reading %s",cpl_frame_get_filename(cur_frame)); */
928 /* cpl_msg_info(cpl_func,"group: %d",cpl_frame_get_group(cur_frame)); */
929  if((cpl_frame_get_group(cur_frame) == CPL_FRAME_GROUP_RAW)){
930  break;
931  }
932  else {
933  cur_frame = cpl_frameset_get_next( frameset );
934 
935  }
936 
937  }
938 
939  if (cur_frame == NULL) {
940  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
941  "SOF does not have any raw files");
942  cpl_msg_error(cpl_func, "SOF does not have any raw files");
943  return;
944  }
945 
946 
947  tag_firstframe = (char*)cpl_frame_get_tag(cur_frame);
948  plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
949  /* Checking if PRISM or GRISM */
950  if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
951  {
952  strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
953 /* cpl_msg_info(cpl_func,"gris_name: %s",gris_name); */
954  }
955 
956  /* Computing the tag for the right mask */
957 
958  if ((strcmp(tag_firstframe, MIDI_HIGH_SENS_CALIB)==0 ||
959  strcmp(tag_firstframe, MIDI_HIGH_SENS_SCIENCE)==0) && strcmp(gris_name,"GRISM")==0) {
960  tag_mask = cpl_sprintf("%s", "KAPPA_HS_MASK_GRISM");
961  }
962 
963  else if ((strcmp(tag_firstframe, MIDI_HIGH_SENS_CALIB)==0 ||
964  strcmp(tag_firstframe, MIDI_HIGH_SENS_SCIENCE)==0) && strcmp(gris_name,"PRISM")==0) {
965  tag_mask = cpl_sprintf("%s", "KAPPA_HS_MASK_PRISM");
966  }
967 
968  else if ((strcmp(tag_firstframe, MIDI_SCI_PHOT_CALIB)==0 ||
969  strcmp(tag_firstframe, MIDI_SCI_PHOT_SCIENCE)==0) && strcmp(gris_name,"GRISM")==0) {
970  tag_mask = cpl_sprintf("%s", "KAPPA_SP_MASK_GRISM");
971  }
972 
973  else if ((strcmp(tag_firstframe, MIDI_SCI_PHOT_CALIB)==0 ||
974  strcmp(tag_firstframe, MIDI_SCI_PHOT_SCIENCE)==0) && strcmp(gris_name,"PRISM")==0) {
975  tag_mask = cpl_sprintf("%s", "KAPPA_SP_MASK_PRISM");
976  }
977 
978  else {
979  tag_mask = cpl_sprintf("%s", "NO MASK");
980  }
981 
982 
983  cur_frame = cpl_frameset_get_first(frameset);
984 
985  while(cur_frame)
986  {
987  /* Check the right tags */
988  tag = (char*)cpl_frame_get_tag(cur_frame);
989 /* cpl_msg_info(cpl_func,"tag: %s tag_mask: %s",tag,tag_mask); */
990  if (strcmp(tag, tag_mask)==0) {
991 /* fileNames->maskFileName=cpl_sprintf("%s",cpl_frame_get_filename(cur_frame)); */
992  sprintf(fileNames->maskFileName,"%s",cpl_frame_get_filename(cur_frame));
993  }
994 
995  cur_frame = cpl_frameset_get_next( frameset );
996  }
997 
998 /* cpl_msg_info(cpl_func,"tag_mask: %s", tag_mask); */
999 /* cpl_msg_info(cpl_func,"fileNames->maskFileName: %s", fileNames->maskFileName); */
1000 
1001  /* ToDo: Free the memory */
1002  cpl_propertylist_delete(plist);
1003  cpl_free(tag_mask);
1004  return ;
1005 }