midiControl.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  midiControl.c
00007 * Description:  Contains the main program and routines for data reduction
00008 *
00009 * History:
00010 * 10-Jan-05     (csabet) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Compiler directives
00016 ******************************************************************************/
00017 
00018 /******************************************************************************
00019 *   Include files
00020 ******************************************************************************/
00021 
00022 #include <sys/stat.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <time.h>
00027 #include "cpl_init.h"
00028 #include "midiGlobal.h"
00029 #include "midiLib.h"
00030 #include "midiControl.h"
00031 #include "memoryHandling.h"
00032 #include "procFrgHS.h"
00033 #include "procFrgSP.h"
00034 #include "procKappa.h"
00035 #include "procAcq.h"
00036 #include "procDetLin.h"
00037 #include "procDetRon.h"
00038 #include "procRefPix.h"
00039 #include "procDspTrn.h"
00040 #include "procWavCal.h"
00041 #include "errorHandling.h"
00042 #include "fileHandling.h"
00043 #include "midiFitsClassification.h"
00044 #include "calibration.h"
00045 #include "diagnostics.h"
00046 #include <cpl.h>
00047 #include "midi_dfs.h"
00048 
00049 /**********************************************************
00050 *   Global Variables
00051 **********************************************************/
00052 int             rootName = 1;                                           // Output root name is given
00053 int             diagnostic = 0;                                         // Set by command line argument DEBUG   (argumentFile.lst)
00054 int             endOfTemplate = 0;                                      // Set by command line argument EOT   (argumentFile.lst)
00055 int             plotFile = 2;                                           // Set by command line argument PLOT_FILE (argumentFile.lst)
00056 int             plotDuration = 0;                                       // Set by command line argument PLOT_FILE (argumentFile.lst)
00057 int             batchNumber = 0;                                        // It is used everywhere
00058 FILE    *midiReportPtr=NULL;                            // File pointer for the program execution history
00059 char    *midiReportName=NULL;                           // Name of event log file
00060 FILE    *midiQcLogPtr=NULL;                                     // File pointer for the QC log file
00061 char    *midiQcLogName=NULL;                            // Name of QC log file
00062 char    outFileDir[MAX_STRING_LENGTH];          // Product directory name
00063 char    outRootName[MAX_STRING_LENGTH];         // Root name for all output files
00064 char    batchTemplate[MAX_STRING_LENGTH];       // A consistent Batch Template Name
00065 char    givenName[MAX_STRING_LENGTH];           // User given  name for all output files
00066 char    midiMessage[MAX_STRING_LENGTH];         // A global message holder
00067 int             *badChannelList;                                        // An array of numOfChannels
00068 
00069 /*============================ C O D E    A R E A ===========================*/
00070 
00071 
00072 /******************************************************************************
00073 *               European Southern Observatory
00074 *             VLTI MIDI Data Reduction Software
00075 *
00076 * Module name:  executeDataReduction
00077 * Input/Output: See function arguments to avoid duplication
00078 * Description:  This is the main routine for the MIDI Data Reduction. It shall be
00079 *                               invoked by one of the recipes. This rootine and the way user parameters
00080 *                               are processed is not at the moment efficient. It has been adapted from the
00081 *                               stand-alone version of the MIDI quickly to get the recipes running.
00082 *
00083 * History:
00084 * 09-Jan-07     (csabet) Created
00085 ******************************************************************************/
00086 void executeDataReduction (
00087    const char        *calibTempDir,  // In: CalibDb directory (full path-name)
00088    const char        *maskFileDir,   // In: Mask file directory (full path-name)
00089    const char        *productDir,    // In: Name of the product directory
00090    int               givenDispDur,   // In: Given plot duration
00091    const char        *inFitsList,    // In: Name of file containing input FITS files (full path-name)
00092    int               *error,         // Ou: Error status
00093    cpl_parameterlist *parlist,
00094    cpl_frameset      *frameset)
00095 {
00096    
00097    //  Local Declarations
00098     //  ------------------
00099     const char  routine[] = "executeDataReduction";
00100     FILE                *inFitsClassifiedPtr=NULL, *inFitsBatchPtr=NULL, *tempReportPtr=NULL;
00101     char                *stringTemp=NULL, *tempReportName=NULL, *fileNameStr=NULL,
00102                                 *classification=NULL;
00103     MidiFiles   *fileNames=NULL;
00104     UserOptions *options=NULL;
00105     FILE                *signaturePtr=NULL;
00106 
00107     //  Algorithm
00108     //  ---------
00109 //      cpl_init ();
00110         *error = 0;
00111 
00112     //  Allocate memory
00113     tempReportName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00114     options = (UserOptions *) calloc (1, sizeof (UserOptions));
00115     fileNames = callocMidiFiles (MAX_STRING_LENGTH);
00116 
00117     // Initialisation
00118     sprintf (tempReportName, "midiPipelineTempReport.log");
00119     tempReportPtr = fopen (tempReportName, "w");
00120     sprintf (outFileDir, "%s", productDir);
00121         plotDuration = givenDispDur;
00122         if (plotDuration == 0) plotFile = 1;
00123         sprintf (givenName, "%s", "MIDI");
00124 //      cpl_msg_info(cpl_func,"Name of the FITS list = %s \n", inFitsList);
00125         
00126 
00127         getMaskFileFromSof(fileNames,frameset);
00128         
00129         
00130 
00131 
00132     getParameters (calibTempDir, maskFileDir, inFitsList, tempReportPtr, options, fileNames, error);
00133         if (*error)
00134         {
00135                 cpl_msg_info(cpl_func,"\n");
00136                 fprintf (tempReportPtr, "\n");
00137                 fclose (tempReportPtr);
00138                 free (tempReportName);
00139                 free (options);
00140                 free (fileNames);
00141                 return;
00142         }
00143 
00144 
00145 
00146     //  Allocate memory
00147     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00148     midiQcLogName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00149     midiReportName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00150     fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00151     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00152 
00153     //  Classify FITS files
00154     //  -------------------
00155     classifyFitsFiles (options, fileNames, tempReportPtr, error,
00156             parlist, frameset);
00157         if (*error)
00158         {
00159                 if (*error > 1) 
00160                 {
00161                         sprintf (midiMessage, "No FITS files to process. Check log file: %s", tempReportName);
00162                         midiReportWarning (tempReportPtr, routine, __FILE__, __LINE__, midiMessage);
00163                 }
00164                 cpl_msg_info(cpl_func,"\n");
00165                 fprintf (tempReportPtr, "\n");
00166                 fclose (tempReportPtr);
00167                 freeMidiFiles (fileNames);
00168                 free (options);
00169                 free (stringTemp);
00170                 free (tempReportName);
00171                 free (midiQcLogName);
00172                 free (midiReportName);
00173                 free (fileNameStr);
00174                 free (classification);
00175                 return;
00176         }
00177 
00178     /*====================================================================================
00179     At this point a file containing the names of input fits files should be available. Here
00180     the following assumptions are made:
00181 
00182         1.  Interferometry FITS files may be split into more than one file
00183         2.  Data Organizer's list of files are consecutive in types. Mixed split files are 
00184                         not allowed
00185         3.  All file categories are processed separately and thier results are stored in 
00186                         either memory or relevant output FITS files.
00187         4.  A Process is only invoked when necessary data is available
00188         5.  Photometry FITS files can also be split. However the number of Photometry A files
00189                         must be equal to the number of Photometry B files.
00190         6.  The order of Photometry and Interferometry in the above list is not important. 
00191     ====================================================================================*/
00192 
00193     //  Open the classified list of FITS files for reading
00194     if ((inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "r")) == NULL)
00195         midiReportError (tempReportPtr, routine, __FILE__, __LINE__, "Cannot open list of classified input FITS files");
00196 
00197     //  Loop through the list of classified FITS files and process 
00198     //  each batch. The first record  in the file is the name of the template
00199     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
00200     {
00201                 //      Extract cleaned up batchTemplate string and save it
00202                 removeNewLine (stringTemp, batchTemplate);
00203 
00204         //      Create a batch file
00205         sprintf (fileNames->inFitsBatch, "%s", "inputFitsBatch.log");
00206         if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "w")) == NULL)
00207             midiReportError (tempReportPtr, routine, __FILE__, __LINE__, "Cannot open batch FITS files");
00208 
00209         //      Copy the rest of files from the classified-list into the batch-list until the END_OF_BATCH
00210         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
00211         {
00212             sprintf (classification, "%s", "");
00213             sscanf (stringTemp, "%s%s", fileNameStr, classification);
00214             if (strcmp (fileNameStr, "END_OF_BATCH") != 0)
00215                 fprintf (inFitsBatchPtr, "%s %s\n", fileNameStr, classification);
00216             else
00217                 break;
00218         }
00219         fclose (inFitsBatchPtr);
00220 
00221         //      Increment batch counter
00222                 batchNumber=1;
00223                         
00224                 //      Create output file names
00225         createOutputFileNames (fileNames, tempReportPtr, error);
00226                 if (*error)
00227                 {
00228                         midiReportWarning (tempReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
00229                         continue;   //  Abandon the current batch and get the next batch
00230                 }
00231         handoverReport (tempReportName, &tempReportPtr);
00232 
00233         //      Create Mask FITS file
00234         //      ---------------------
00235         if (options->maskMode == COMPUTED)
00236         {
00237             createMaskFile (fileNames, error);
00238             if (*error)
00239             {
00240                                 midiReportWarning (tempReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
00241                 midiRejectBatch ();
00242                 continue;   //  Abandon the current batch and get the next batch
00243             }
00244         }
00245 
00246                 //      Process templates
00247                 //      -----------------
00248                 if ((strcmp (batchTemplate, "HIGH_SENS_CALIB") == 0) ||
00249                         (strcmp (batchTemplate, "HIGH_SENS_SCIENCE") == 0))
00250                 {
00251                    if (options->drsFrg) procFrgHS (fileNames, options, error, parlist, frameset);
00252                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
00253                 }
00254                 else if (strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0)
00255                 {
00256                         if (options->drsKap)
00257                         {
00258                                 //      Write a signature. Midas checks to see which template is running
00259                                 signaturePtr = fopen ("MIDI_sig_spk.log", "w");
00260                             fclose (signaturePtr);
00261                                 procKappa (fileNames, options, error);
00262                                 if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
00263                         }
00264                 }
00265                 else if ((strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0) ||
00266                         (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0))
00267                 {
00268                         if (options->drsFrg)
00269                         {
00270                                 if (strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0)
00271                                     signaturePtr = fopen ("MIDI_sig_spc.log", "w");
00272                                 else
00273                                         signaturePtr = fopen ("MIDI_sig_sps.log", "w");
00274                                 fclose (signaturePtr);
00275                                 procFrgSP (fileNames, options, error, parlist, frameset);
00276                                 if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "This batch is rejected");
00277                         }
00278                 }
00279                 else if (strcmp (batchTemplate, "ACQ") == 0)
00280                 {
00281                         if (options->drsAcq) 
00282                                 procAcq (fileNames, error);
00283                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00284                                 "This batch is rejected");
00285                 }
00286                 else if (strcmp (batchTemplate, "DETLIN") == 0)
00287                 {
00288                         if (options->tecLin) 
00289                                 procDetLin (fileNames, error);
00290                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00291                                 "This batch is rejected");
00292                 }
00293                 else if (strcmp (batchTemplate, "DETRON") == 0)
00294                 {
00295                         if (options->tecRon) 
00296                                 procDetRon (fileNames, error);
00297                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00298                                 "This batch is rejected");
00299                 }
00300                 else if (strcmp (batchTemplate, "DSPTRN") == 0)
00301                 {
00302                         if (options->tecTrn) 
00303                                 procDspTrn (fileNames, error);
00304                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00305                                 "This batch is rejected");
00306                 }
00307                 else if (strcmp (batchTemplate, "REFPIX") == 0)
00308                 {
00309                         if (options->tecPix) 
00310                                 procRefPix (fileNames, error);
00311                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00312                                 "This batch is rejected");
00313                 }
00314                 else if (strcmp (batchTemplate, "WAVECAL") == 0)
00315                 {
00316                         if (options->tecWav) 
00317                                 procWaveCal (options->tecWav, fileNames, error);
00318                         if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00319                                 "This batch is rejected");
00320                 }
00321                 else 
00322                 {
00323                         sprintf (midiMessage, "Cannot process template: %s", batchTemplate);
00324                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00325                         *error = 1;
00326                 }
00327 
00328                 //      Close log files for this batch
00329                 if (*error) midiRejectBatch ();
00330                 else midiCloseBatch ();
00331                 
00332                 if (strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0)
00333                 {
00334                    /* The empty dummy file MIDI_b1_spk.qc.dummy has to be erased  */ 
00335                    remove (midiQcLogName);
00336                    
00337                    continue;
00338                 }
00339                    break;
00340 
00341    }
00342  
00343     //  Close temporary files and release memory
00344     //  ----------------------------------------
00345     if (diagnostic) cpl_msg_info(cpl_func,"\nClosing temporary files \n");
00346     if (diagnostic) cpl_msg_info(cpl_func,"----------------------- \n");
00347     if (diagnostic) cpl_msg_info(cpl_func,"\n\n");
00348     fclose (inFitsClassifiedPtr);
00349     fclose (tempReportPtr);
00350     remove (fileNames->inFitsClassified);
00351     remove (fileNames->inFitsBatch);
00352     remove (tempReportName);
00353  
00354     //  Release remaining memory
00355     freeMidiFiles (fileNames);
00356     free (options);
00357     free (stringTemp);
00358     free (tempReportName);
00359     free (midiQcLogName);
00360         free (midiReportName);
00361     free (fileNameStr);
00362     free (classification);
00363 //      cpl_end ();
00364         
00365     return;
00366 }
00367 /*****************************************************************************/
00368 
00369 
00370 /******************************************************************************
00371 *               European Southern Observatory
00372 *             VLTI MIDI Data Reduction Software
00373 *
00374 * Module name:  midiRejectBatch
00375 * Input/Output: See function arguments to avoid duplication
00376 * Description:  Garbage collector
00377 *
00378 * History:
00379 * 17-May-05     (csabet) Created
00380 ******************************************************************************/
00381 void midiRejectBatch (void)
00382 {
00383     //  Local Declarations
00384     //  ------------------
00385     const char  routine[] = "midiRejectBatch";
00386 
00387     //  Algorithm
00388     //  ---------
00389     if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00390     if (diagnostic > 2) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00391 
00392         //      Close log File and release memory for this batch
00393         //      ------------------------------------------------
00394         if (diagnostic) cpl_msg_info(cpl_func,"Created Log files: %s \n", midiReportName);
00395         fprintf (midiReportPtr, "Created Log files: %s \n", midiReportName);
00396         if (diagnostic) cpl_msg_info(cpl_func,"\n");
00397         fprintf (midiReportPtr, "\n");
00398         if (midiReportPtr)
00399         {
00400                 fclose (midiReportPtr); 
00401                 midiReportPtr = NULL;
00402         }
00403         if (midiQcLogPtr)
00404         {
00405                 fclose (midiQcLogPtr); 
00406                 midiQcLogPtr = NULL;
00407                 remove (midiQcLogName);
00408         }
00409 
00410         return;
00411 }
00412 /*****************************************************************************/
00413 
00414 
00415 /******************************************************************************
00416 *               European Southern Observatory
00417 *             VLTI MIDI Data Reduction Software
00418 *
00419 * Module name:  midiCloseBatch
00420 * Input/Output: See function arguments to avoid duplication
00421 * Description:  Garbage collector
00422 *
00423 * History:
00424 * 17-May-05     (csabet) Created
00425 ******************************************************************************/
00426 void midiCloseBatch (void)
00427 {
00428     //  Local Declarations
00429     //  ------------------
00430     const char  routine[] = "midiCloseBatch";
00431 
00432     //  Algorithm
00433     //  ---------
00434     if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00435     if (diagnostic > 2) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00436 
00437         //      Close log File and release memory for this batch
00438         //      Close files used in this batch
00439         cpl_msg_info(cpl_func,"Created Log files: %s  %s \n", midiReportName, midiQcLogName);
00440         fprintf (midiReportPtr, "Created Log files: %s  %s \n", midiReportName, midiQcLogName);
00441         cpl_msg_info(cpl_func,"\n\n");
00442         fprintf (midiReportPtr, "\n\n");
00443         if (midiReportPtr)
00444         {
00445                 fclose (midiReportPtr); 
00446                 midiReportPtr = NULL;
00447         }
00448         if (midiQcLogPtr)
00449         {
00450                 fclose (midiQcLogPtr); 
00451                 midiQcLogPtr = NULL;
00452         }
00453 
00454         return;
00455 }
00456 /*****************************************************************************/
00457 
00458 
00459 /******************************************************************************
00460 *               European Southern Observatory
00461 *             VLTI MIDI Data Reduction Software
00462 *
00463 * Module name:  getparameters
00464 * Input/Output: See function arguments to avoid duplication
00465 * Description:  Gets user options
00466 *
00467 * History:
00468 * 24-Sep_03     (csabet) Removed OPDstep. It shall be derived from the FITS header
00469 * 03-Mar-03     (csabet) Created
00470 ******************************************************************************/
00471 void getParameters (
00472         const char            *calibTempDir,  // In: CalibDb directory
00473         const char            *maskFileDir,   // In: Mask file directory
00474         const char            *inFitsList,    // In: Name of file containing input FITS files
00475         FILE            *filePtr,               // In: Pointer to a temporary open file
00476         UserOptions     *options,               // IO: Pointer to the user options
00477         MidiFiles       *fileNames,             // IO: Pointer to midi file structure
00478         int                     *error)                 // Ou: Error status
00479 
00480 {
00481 
00482     //  Local Declarations
00483     //  ------------------
00484     const char  routine[] = "getParameters";
00485     FILE        *inFitsListPtr=NULL, *inFitsClassifiedPtr=NULL;
00486     char        *argument=NULL, *value=NULL, *tempStr=NULL;
00487 
00488     //  Algorithm
00489     //  ---------
00490         *error = 0;
00491 
00492     //  Allocate memory
00493     tempStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00494     argument = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00495     value = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00496 
00497     //  Set default variables
00498     //  ---------------------
00499     sprintf (fileNames->inFileDir, "%s", "");
00500     sprintf (fileNames->trfNameRead, "%s", "/tmp/MIDI.trf.dummy");
00501     sprintf (fileNames->trfNameWrite, "%s", "/tmp/MIDI.trf.dummy");
00502     sprintf (fileNames->wclNameRead, "%s", "/tmp/MIDI.wcl.dummy");
00503     sprintf (fileNames->wclNameWrite, "%s", "/tmp/MIDI.wcl.dummy");
00504     sprintf (fileNames->trrNameRead, "%s", "/tmp/MIDI.trr.dummy");
00505     sprintf (fileNames->trrNameWrite, "%s", "/tmp/MIDI.trr.dummy");
00506     sprintf (fileNames->calibDbDir, "%s", calibTempDir);
00507     sprintf (fileNames->maskFileDir, "%s", maskFileDir);
00508     sprintf (fileNames->inFitsList, "%s", inFitsList);
00509         options->processing = DISPERSED;
00510         options->drsFrg = 1;
00511         options->drsAcq = 1;
00512         options->drsKap = 0;
00513         options->tecRon = 1;
00514         options->tecLin = 1;
00515         options->tecPix = 1;
00516         options->tecTrn = 1;
00517         options->tecWav = 1;
00518 /*     options->maskMode = SELECTED; */
00519     options->maskMode = SPECIFIED;
00520         
00521     //  Display banner, time and date
00522     displayBanner (filePtr);
00523 
00524     if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00525 
00526     //  Create names for calibrator data files
00527     sprintf (fileNames->calibDbName, "%scalibrator_database.dat", fileNames->calibDbDir);
00528     sprintf (fileNames->waveCalibName, "%swaveCalib", fileNames->calibDbDir);
00529     sprintf (fileNames->transferRatiosName, "%stransferRatios", fileNames->calibDbDir);
00530     sprintf (fileNames->grismFoilName, "%sgrism_foil_4lines.dat", fileNames->calibDbDir);
00531     sprintf (fileNames->prismFoilName, "%sprism_foil_4lines.dat", fileNames->calibDbDir);
00532 
00533     //  Create a name for the classified file
00534     strcpy (fileNames->inFitsClassified, "inputFitsClassified.log");
00535 
00536 //      cpl_msg_info(cpl_func,"Name of the FITS list = %s \n", fileNames->inFitsList);
00537     //  Check if the input FITS file list exists and it is not empty
00538     if (((inFitsListPtr = fopen(fileNames->inFitsList, "r")) != NULL) &&
00539         (fscanf (inFitsListPtr, "%s\n", argument) != EOF))
00540     {
00541         //      Create a file for the classified list
00542         rewind (inFitsListPtr);
00543         inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "w");
00544 
00545         while (fgets (tempStr, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
00546         {
00547             //  Clear string
00548             sprintf (value, "%s", "");
00549             sscanf (tempStr, "%s%s", argument, value);
00550             fprintf (inFitsClassifiedPtr, "%s %s\n", argument, value);
00551         }
00552         fclose (inFitsListPtr);
00553         fclose (inFitsClassifiedPtr);
00554     }
00555     else
00556     {
00557        cpl_msg_info(cpl_func,"\n<<Error>>: List of FITS file is not given or the list is empty \n");
00558        cpl_msg_info(cpl_func,"\n");
00559         fprintf(filePtr, "\n<<Error>>: List of FITS file is not given or the list is empty\n");
00560         fprintf(filePtr, "\n");
00561                 *error = 1;
00562         if (argument) free (argument);
00563         if (tempStr) free (tempStr);
00564         if (value) free (value);
00565                 return;
00566     }
00567 
00568     //  Report the contents of the list
00569     inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "r");
00570     cpl_msg_info(cpl_func,"\n");
00571     cpl_msg_info(cpl_func,"The FITS list contains the following files:\n");
00572     cpl_msg_info(cpl_func,"-------------------------------------------\n");
00573     fprintf (filePtr, "\n");
00574     fprintf (filePtr, "Given FITS list contains the following files:\n");
00575     fprintf (filePtr, "---------------------------------------------\n");
00576     while (fgets (tempStr, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
00577     {
00578         //      Clear string
00579         sprintf (value, "%s", "");
00580         sscanf (tempStr, "%s%s", argument, value);
00581         cpl_msg_info(cpl_func,"%s %s\n", argument, value);
00582         fprintf (filePtr, "%s %s\n", argument, value);
00583     }
00584     fclose (inFitsClassifiedPtr);
00585 
00586     //  Release memory
00587     if (argument) free (argument);
00588     if (tempStr) free (tempStr);
00589     if (value) free (value);
00590 
00591     return;
00592 }
00593 /******************************************************************************/
00594 
00595 
00596 
00597 
00598 /******************************************************************************
00599 *               European Southern Observatory
00600 *             VLTI MIDI Data Reduction Software
00601 *
00602 * Module name:  createOutputFileNames
00603 * Input/Output: See function arguments to avoid duplication
00604 * Description:  Creates names for the product FITS files
00605 *
00606 * History:
00607 * 03-Mar-03     (csabet) Created
00608 ******************************************************************************/
00609 void createOutputFileNames (
00610     MidiFiles   *fileNames,     // IO: Pointer to midi files structure
00611     FILE        *filePtr,
00612     int                 *error)
00613 
00614 {
00615 
00616     //  Local Declarations
00617     //  ------------------
00618     const char      routine[] = "createOutputFileNames";
00619     FILE            *inFitsBatchPtr=NULL;
00620     size_t          stringLength;
00621     char                *fileTemp=NULL, *classification=NULL, *pipeFilePrefix=NULL, 
00622                                         *fileNameStr=NULL, *suffix=NULL, *pipeRootName=NULL;
00623 
00624     //  Algorithm
00625     //  ---------
00626     if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00627     if (diagnostic > 2) fprintf (filePtr, "Invoking      routine   '%s' \n", routine);
00628     
00629     //  Reset parameters
00630     *error = 0;
00631     
00632     //  Allocate memory
00633     fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00634     suffix = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00635     fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00636     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00637         pipeFilePrefix = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00638         pipeRootName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00639         
00640         //      Create the prefix for the PIPEFILE
00641     sprintf (pipeFilePrefix, "r.");
00642 
00643         //      Get the pipeline root name (pipeRootName) from the first FITS file in the batch
00644         if ((inFitsBatchPtr = fopen(fileNames->inFitsBatch, "r")) == NULL)
00645                 midiReportError (filePtr, routine, __FILE__, __LINE__, "Cannot open FITS file list");
00646 
00647         fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr);
00648         sprintf (classification, "%s", "");
00649         sscanf (fileTemp, "%s%s", fileNameStr, classification);
00650         stringLength = strlen (fileNameStr);
00651         strncpy (pipeRootName, fileNameStr, stringLength-5);
00652 
00653         //      Strip off the path name if any and Create PIPEFILE and 
00654         //      ARCHFILE. Always from the root name of the first FITS file 
00655         removePathName (pipeRootName, filePtr);
00656         if (inFitsBatchPtr) fclose (inFitsBatchPtr);
00657         sprintf (fileNames->archFileName, "%s.fits", pipeRootName);
00658         sprintf (fileNames->pipeFileName, "%s%s_tpl_0000.fits", pipeFilePrefix, pipeRootName);
00659 
00660         //      Create template suffix
00661         if (strcmp (batchTemplate, "HIGH_SENS_CALIB") == 0)
00662         {
00663                 sprintf (suffix, "_b%d_hsc", batchNumber);
00664         }
00665         else if (strcmp (batchTemplate, "HIGH_SENS_SCIENCE") == 0)
00666         {
00667                 sprintf (suffix, "_b%d_hss", batchNumber);
00668         }
00669         else if (strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0)
00670         {
00671                 sprintf (suffix, "_b%d_spc", batchNumber);
00672         }
00673         else if (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0)
00674         {
00675                 sprintf (suffix, "_b%d_sps", batchNumber);
00676         }
00677         else if (strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0)
00678         {
00679                 sprintf (suffix, "_b%d_spk", batchNumber);
00680         }
00681         else if (strcmp (batchTemplate, "ACQ") == 0)
00682         {
00683                 sprintf (suffix, "_b%d_acq", batchNumber);
00684         }
00685         else if (strcmp (batchTemplate, "DETLIN") == 0)
00686         {
00687                 sprintf (suffix, "_b%d_lin", batchNumber);
00688         }
00689         else if (strcmp (batchTemplate, "DETRON") == 0)
00690         {
00691                 sprintf (suffix, "_b%d_ron", batchNumber);
00692         }
00693         else if (strcmp (batchTemplate, "DSPTRN") == 0)
00694         {
00695                 sprintf (suffix, "_b%d_trn", batchNumber);
00696         }
00697         else if (strcmp (batchTemplate, "REFPIX") == 0)
00698         {
00699                 sprintf (suffix, "_b%d_pix", batchNumber);
00700         }
00701         else if (strcmp (batchTemplate, "WAVECAL") == 0)
00702         {
00703                 sprintf (suffix, "_b%d_wav", batchNumber);
00704         }
00705         else
00706         {
00707                 *error = 1;
00708                 sprintf (midiMessage, "Unknown Template: %s", batchTemplate);
00709                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, midiMessage);
00710                 free (fileTemp);
00711                 free (suffix);
00712                 free (classification);
00713                 free (fileNameStr);
00714                 free (pipeFilePrefix);
00715                 free (pipeRootName);
00716                 return;
00717         }
00718         if (!rootName) sprintf (outRootName, "%s%s", pipeRootName, suffix);
00719         else sprintf (outRootName, "%s%s", givenName, suffix);
00720         
00721         sprintf (midiReportName, "%s%s.log", outFileDir, outRootName);
00722         sprintf (midiQcLogName, "%s%s.qc.dummy", outFileDir, outRootName);
00723         sprintf (fileNames->trfHistoryName, "%s%s.trfh.dummy", outFileDir, outRootName);
00724         sprintf (fileNames->wclHistoryName, "%s%s.wclh.dummy", outFileDir, outRootName);
00725         sprintf (fileNames->trrHistoryName, "%s%s.trrh.dummy", outFileDir, outRootName);
00726         sprintf (fileNames->outFitsName, "%s%s.pro.fits", outFileDir, outRootName);
00727         sprintf (fileNames->outWaterfallName, "%s%s.waf.fits", outFileDir, outRootName);
00728         sprintf (fileNames->outWaterpowerName, "%s%s.wap.fits", outFileDir, outRootName);
00729         sprintf (fileNames->outAveImageName, "%s%s.aim.fits", outFileDir, outRootName);
00730         sprintf (fileNames->outChSpectrumName, "%s%s.chs.fits", outFileDir, outRootName);
00731                 
00732     //  Release memory
00733     if (fileTemp) free (fileTemp);
00734     if (suffix) free (suffix);
00735     if (classification) free (classification);
00736     if (fileNameStr) free (fileNameStr);
00737         if (pipeFilePrefix) free (pipeFilePrefix);
00738         if (pipeRootName) free (pipeRootName);
00739 
00740     return;
00741 }
00742 /******************************************************************************/
00743 
00744 
00745 
00746 /******************************************************************************
00747 *               European Southern Observatory
00748 *             VLTI MIDI Data Reduction Software
00749 *
00750 * Module name:  displayBanner
00751 * Input/Output: See function arguments to avoid duplication
00752 * Description:  Displays the ESO Banner
00753 *
00754 * History:
00755 * 23-Jul-03     (csabet) Created
00756 ******************************************************************************/
00757 void displayBanner (
00758     FILE    *filePtr)
00759 
00760 {
00761 
00762     /*  Local Declarations
00763     --------------------*/
00764     const char  routine[] = "displayBanner";
00765     time_t      now;
00766     struct tm   *newTime=NULL;
00767     char        *currentTime=NULL;
00768 
00769     /*  Algorithm
00770     -----------*/
00771     if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00772     if (diagnostic > 2) fprintf (filePtr, "Invoking      routine   '%s' \n", routine);
00773 
00774     /*  Allocate memory */
00775     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00776 
00777     now = time(NULL);
00778     newTime = gmtime (&now);
00779     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00780 
00781     cpl_msg_info(cpl_func,"\n\n\n\n");
00782     cpl_msg_info(cpl_func,"   ,----------------------------------------------.\n");
00783     cpl_msg_info(cpl_func,"   |   *       European Southern Observatory      |\n");
00784     cpl_msg_info(cpl_func,"   | *E S*          VLTI DMD, Garching            |\n");
00785     cpl_msg_info(cpl_func,"   |   O        MIDI Data Reduction Program       |\n");
00786     cpl_msg_info(cpl_func,"   |   *            Version: %s\n", MIDI_PIPE_VERSION);
00787     cpl_msg_info(cpl_func,"   |                                              |\n");
00788     cpl_msg_info(cpl_func,"   |               For support e-mail to:         |\n");
00789     cpl_msg_info(cpl_func,"   |         pballest@eso.org or agabasch@eso.org   |\n");
00790     cpl_msg_info(cpl_func,"   `----------------------------------------------'\n");
00791     cpl_msg_info(cpl_func,"\nMIDI pipeline started on: %s  GMT \n", currentTime);
00792 
00793     fprintf (filePtr, "\n\n\n\n");
00794     fprintf (filePtr, "   ,----------------------------------------------.\n");
00795     fprintf (filePtr, "   |   *       European Southern Observatory      |\n");
00796     fprintf (filePtr, "   | *E S*          VLTI DMD, Garching            |\n");
00797     fprintf (filePtr, "   |   O        MIDI Data Reduction Program       |\n");
00798     fprintf (filePtr, "   |   *            Version: %s\n", MIDI_PIPE_VERSION);
00799     fprintf (filePtr, "   |                                              |\n");
00800     fprintf (filePtr, "   |               For support e-mail to:         |\n");
00801     fprintf (filePtr, "   |         pballest@eso.org or agabasch@eso.org   |\n");
00802     fprintf (filePtr, "   `----------------------------------------------'\n");
00803     fprintf (filePtr, "\nMIDI pipeline started on: %s  GMT \n", currentTime);
00804 
00805     /*  Release memory */
00806     if (currentTime) free (currentTime);
00807 
00808     return;
00809 }
00810 /******************************************************************************/
00811 
00812 
00813 
00814 /******************************************************************************
00815 *               European Southern Observatory
00816 *             VLTI MIDI Data Reduction Software
00817 *
00818 * Module name:  handoverReport
00819 * Input/Output: See function arguments to avoid duplication
00820 * Description:  At the begining of the DRS before user parameters have been
00821 *               processed all reports are logged in a temporary
00822 *               file. Afterwards the content of this file is transfered to an
00823 *               actual report file the name of which is user defined..
00824 *
00825 * History:
00826 * 22-Jul-03     (csabet) Created
00827 ******************************************************************************/
00828 void handoverReport (
00829     char        *fileName,  /*  IO: Pointer to the temporary file */
00830     FILE        **filePtr)
00831 {
00832 
00833     /*  Local Declarations
00834     --------------------*/
00835     const char  routine[] = "handoverReport";
00836     char        *tempStr=NULL;
00837     time_t      now;
00838     struct tm   *newTime=NULL;
00839     char        *currentTime=NULL;
00840     struct stat buf;
00841 
00842     /*  Algorithm
00843     -----------*/
00844     if (diagnostic > 2) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00845     if (diagnostic > 2) fprintf(*filePtr, "Invoking      routine   '%s' \n", routine);
00846         
00847     /*  Allocate memory */
00848     tempStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00849     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00850 
00851         //      Now open the following global files for writing
00852     if (stat (midiReportName, &buf) == 0) remove (midiReportName);       // If the file exist delete it
00853     midiReportPtr = fopen(midiReportName, "w");
00854     if (!midiReportPtr) midiReportError (*filePtr, routine, __FILE__, __LINE__, "Cannot create output event log file");
00855 
00856     if (stat (midiQcLogName, &buf) == 0) remove (midiQcLogName);        // If the file exist delete it
00857     midiQcLogPtr = fopen(midiQcLogName, "w");
00858     if (!midiQcLogPtr) midiReportError (*filePtr, routine, __FILE__, __LINE__, "Cannot create output QC log file");
00859 
00860     fclose (*filePtr);
00861 
00862     *filePtr = fopen (fileName, "r");
00863     while (fgets (tempStr, MAX_STRING_LENGTH, *filePtr) != NULL)
00864         fprintf (midiReportPtr, "%s", tempStr);
00865 
00866     now = time(NULL);
00867     newTime = gmtime (&now);
00868     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00869     cpl_msg_info(cpl_func,"\nMIDI Data Reduction for batch  %d  started on: %s  GMT \n", batchNumber, currentTime);
00870     fprintf (midiReportPtr, "\nMIDI Data Reduction for batch  %d  started on: %s  GMT \n", batchNumber, currentTime);
00871 
00872     fclose (*filePtr);
00873     *filePtr = fopen (fileName, "a");
00874 
00875     /*  Release memory */
00876     if (tempStr) free (tempStr);
00877     if (currentTime) free (currentTime);
00878 
00879     return;
00880 }
00881 /******************************************************************************/
00882 
00883 /*----------------------------------------------------------------------------*/
00889 /*----------------------------------------------------------------------------*/
00890 
00891 void getMaskFileFromSof(MidiFiles * fileNames, cpl_frameset  * frameset)
00892 {
00893    
00894    
00895    cpl_frame             * cur_frame;
00896    char                  * tag_firstframe=NULL;
00897    char                  * tag_mask=NULL;
00898    char                  * tag=NULL;
00899    char                    gris_name[100]= ""; 
00900    cpl_propertylist      * plist;
00901    int                     i;
00902    
00903    /* Select the right profile/mask from the SOF and write the
00904     * variable fileNames->maskFileName accordingly */
00905 
00906 
00907   /* Identify the RAW and CALIB frames in the input frameset */
00908 //   cpl_ensure_code(midi_dfs_set_groups(frameset) == CPL_ERROR_NONE,
00909 //                   cpl_error_get_code());
00910    
00911    if (midi_dfs_set_groups(frameset) != CPL_ERROR_NONE) {
00912       cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00913                             "Identifying calib and/or raw frames failed");
00914       return;
00915    }
00916    
00917    cur_frame = cpl_frameset_get_first(frameset);
00918    if (cur_frame == NULL) {
00919       cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00920                             "SOF does not have any file");
00921       return;
00922    }
00923    
00924 /* Skip the calibration files and use the first raw file  */  
00925    
00926    for(i=0; i<cpl_frameset_get_size(frameset);i++){
00927 /*       cpl_msg_info(cpl_func, "Reading %s",cpl_frame_get_filename(cur_frame)); */
00928 /*       cpl_msg_info(cpl_func,"group: %d",cpl_frame_get_group(cur_frame)); */
00929       if((cpl_frame_get_group(cur_frame) == CPL_FRAME_GROUP_RAW)){
00930          break;
00931       }
00932       else {
00933          cur_frame = cpl_frameset_get_next( frameset );   
00934          
00935       }
00936       
00937    }
00938 
00939    if (cur_frame == NULL) {
00940       cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
00941                             "SOF does not have any raw files");
00942       cpl_msg_error(cpl_func, "SOF does not have any raw files");
00943       return;
00944    }
00945    
00946 
00947    tag_firstframe = (char*)cpl_frame_get_tag(cur_frame);
00948    plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00949    /*       Checking if PRISM or GRISM */
00950    if (cpl_propertylist_has(plist, "ESO INS GRIS NAME") == 1)
00951    {
00952       strcpy(gris_name,cpl_propertylist_get_string(plist, "ESO INS GRIS NAME"));
00953 /*       cpl_msg_info(cpl_func,"gris_name: %s",gris_name); */
00954    }
00955            
00956    /*  Computing the tag for the right mask */
00957 
00958    if ((strcmp(tag_firstframe, MIDI_HIGH_SENS_CALIB)==0 || 
00959         strcmp(tag_firstframe, MIDI_HIGH_SENS_SCIENCE)==0)  && strcmp(gris_name,"GRISM")==0) {
00960       tag_mask = cpl_sprintf("%s", "KAPPA_HS_MASK_GRISM");  
00961    }
00962    
00963    else if ((strcmp(tag_firstframe, MIDI_HIGH_SENS_CALIB)==0 || 
00964              strcmp(tag_firstframe, MIDI_HIGH_SENS_SCIENCE)==0)  && strcmp(gris_name,"PRISM")==0) {
00965       tag_mask = cpl_sprintf("%s", "KAPPA_HS_MASK_PRISM");  
00966    }
00967    
00968    else if ((strcmp(tag_firstframe, MIDI_SCI_PHOT_CALIB)==0 || 
00969              strcmp(tag_firstframe, MIDI_SCI_PHOT_SCIENCE)==0)  && strcmp(gris_name,"GRISM")==0) {
00970       tag_mask = cpl_sprintf("%s", "KAPPA_SP_MASK_GRISM");  
00971    }
00972 
00973    else if ((strcmp(tag_firstframe, MIDI_SCI_PHOT_CALIB)==0 || 
00974              strcmp(tag_firstframe, MIDI_SCI_PHOT_SCIENCE)==0)  && strcmp(gris_name,"PRISM")==0) {
00975       tag_mask = cpl_sprintf("%s", "KAPPA_SP_MASK_PRISM");  
00976    }
00977 
00978    else {
00979       tag_mask = cpl_sprintf("%s", "NO MASK"); 
00980    }
00981 
00982       
00983    cur_frame = cpl_frameset_get_first(frameset);
00984 
00985    while(cur_frame)
00986    {
00987       /*      Check the right tags */
00988       tag = (char*)cpl_frame_get_tag(cur_frame);
00989 /*       cpl_msg_info(cpl_func,"tag: %s tag_mask: %s",tag,tag_mask); */
00990       if (strcmp(tag, tag_mask)==0) {
00991 /*                  fileNames->maskFileName=cpl_sprintf("%s",cpl_frame_get_filename(cur_frame)); */
00992          sprintf(fileNames->maskFileName,"%s",cpl_frame_get_filename(cur_frame));
00993       }
00994 
00995       cur_frame = cpl_frameset_get_next( frameset );
00996    }
00997            
00998 /*    cpl_msg_info(cpl_func,"tag_mask: %s",  tag_mask); */
00999 /*    cpl_msg_info(cpl_func,"fileNames->maskFileName: %s", fileNames->maskFileName); */
01000 
01001    /*  ToDo: Free the memory */
01002    cpl_propertylist_delete(plist);
01003    cpl_free(tag_mask);          
01004    return ;
01005 }

Generated on 5 Mar 2013 for MIDI Pipeline Reference Manual by  doxygen 1.6.1