midiFitsClassification.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  midiFitsClassification.c
00007 * Description:  Contains routines for file classification
00008 *
00009 * History:
00010 * 28-Apr-05     (csabet) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Compiler directives
00016 ******************************************************************************/
00017 //  Search for TO_DO_MIDI_DRS
00018 #define DEBUG_FITS_FILES_x
00019 
00020 /******************************************************************************
00021 *   Include files
00022 ******************************************************************************/
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <unistd.h>
00026 #include <stdio.h>
00027 #include <math.h>
00028 #include "midiGlobal.h"
00029 #include "midiLib.h"
00030 #include "memoryHandling.h"
00031 #include "qfits.h"
00032 #include "qfits_rw.h"
00033 #include "diagnostics.h"
00034 #include "errorHandling.h"
00035 #include "fileHandling.h"
00036 #include "midiFitsClassification.h"
00037 #include "midiGlobal.h"
00038 #include <cpl.h>
00039 /**********************************************************
00040 *   Constant definitions
00041 **********************************************************/
00042 
00043 
00044 /**********************************************************
00045 *   Global Variables
00046 **********************************************************/
00047 
00048 /*============================ C O D E    A R E A ===========================*/
00049 
00050 
00051 
00052 /******************************************************************************
00053 *               European Southern Observatory
00054 *             VLTI MIDI Data Reduction Software
00055 *
00056 * Module name:  classifyFitsFiles
00057 * Input/Output: See function arguments to avoid duplication
00058 * Description:  It loops through the given list of FITS files and puts the
00059 *               keyword END_OF_BATCH at the end of a batch of related files. A batch
00060 *               of files will contain a set of related split Interferometry files
00061 *               as well as the associated photometry files.
00062 *
00063 * History:
00064 * 10-Oct-03     (csabet) Created
00065 ******************************************************************************/
00066 void classifyFitsFiles (
00067     UserOptions       *options,       // In: User options
00068     MidiFiles         *fileNames,     // IO: Pointer to midi files structure
00069     FILE              *filePtr,
00070     int               *error,         // Ou: Error status. 1=Info, 2=Error
00071     cpl_parameterlist *parlist,
00072     cpl_frameset      *frameset)
00073 
00074 {
00075 
00076     //  Local Declarations
00077         //      ------------------
00078     const char  routine[] = "classifyFitsFiles";
00079     int                 stringLength, fileNumInList, batchNum = 0, fileCount, localError;
00080     FILE                *inFitsClassifiedPtr=NULL, *inFitsTempPtr=NULL;
00081     char                *inFitsTemp=NULL, *stringTemp=NULL, 
00082                                 *fileNameStr=NULL, *classification=NULL;
00083     ImageFormat *format=NULL;
00084 
00085     //  Algorithm
00086     //  ---------
00087     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00088     if (diagnostic > 4) fprintf (filePtr, "Invoking      routine   '%s' \n", routine);
00089 
00090     cpl_msg_info(cpl_func,"\nClassifying input FITS files \n");
00091     cpl_msg_info(cpl_func,"---------------------------- \n");
00092     fprintf (filePtr, "\nClassifying input FITS files \n");
00093     fprintf (filePtr, "---------------------------- \n");
00094 
00095         //      Reset status
00096         *error = 0;
00097         localError = 0;
00098         
00099         //      Allocate memory
00100     fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00101     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00102     inFitsTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00103     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00104         format = callocImageFormat ();
00105 
00106     //  Open the list of files for reading
00107     if ((inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "r")) == NULL)
00108     {
00109         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00110                 *error = 2;
00111                 free (stringTemp);
00112                 free (inFitsTemp);
00113                 free (classification);
00114                 free (fileNameStr);
00115                 freeImageFormat (format);
00116                 return;
00117         }
00118 
00119 #ifdef DEBUG_FITS_FILES
00120     fileNumInList = 0;
00121     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
00122     {
00123         fileNumInList++;
00124         sscanf (stringTemp, "%s%s", fileNameStr, classification);
00125         getFitsClassification (fileNumInList, filePtr, fileNameStr, format, error);
00126     }
00127     fclose (filePtr);
00128     exit (0);
00129 #endif
00130 
00131     //  Open a main temporary file for writing
00132     stringLength = strlen (fileNames->inFitsClassified);
00133     strncpy (inFitsTemp, fileNames->inFitsClassified, stringLength-4);
00134     strcat (inFitsTemp, "Buffer.log");
00135     if ((inFitsTempPtr = fopen (inFitsTemp, "w")) == NULL)
00136     {
00137         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a temporary batch file");
00138                 *error = 2;
00139                 fclose (inFitsClassifiedPtr);
00140                 free (stringTemp);
00141                 free (inFitsTemp);
00142                 free (classification);
00143                 free (fileNameStr);
00144                 freeImageFormat (format);
00145                 return;
00146         }
00147 
00148     //  Loop through the list of FITS files and classify
00149     fileNumInList = 0;
00150     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsClassifiedPtr) != NULL)
00151     {
00152                 //      Extract name and classification of the input FITS file
00153         sprintf (classification, "%s", "");
00154         sscanf (stringTemp, "%s%s", fileNameStr, classification);
00155 
00156         //      Increment file number
00157         fileNumInList++;
00158 
00159         //      Get FITS file parameters
00160         getFitsClassification (fileNumInList, filePtr, fileNameStr, format, &localError);
00161                 if (localError) continue;
00162 
00163                 //      Check template of the subsequent first file
00164                 if (((strcmp (format->obsCatg, "CALIB") == 0) || 
00165                         (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00166                         ((strcmp (format->obsTech, "INTERFEROMETRY") == 0) || 
00167                         (strcmp (format->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0)) &&
00168                         ((strcmp (format->obsType, "TRACK,OBJECT,DISPERSED") == 0) || 
00169                         (strcmp (format->obsType, "TRACK,OBJECT,DISPERSED,SCIPHOT") == 0) || 
00170 
00171                         (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER") == 0) ||
00172                         (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER,SCI_PHOT") == 0) || 
00173 
00174                         (strcmp (format->obsType, "PHOTOMETRY,OBJECT,SCIPHOT") == 0) ||
00175                         (strcmp (format->obsType, "PHOTOMETRY,OBJECT") == 0))  &&
00176                         (format->fileNumInTemplate == 1))
00177                 {
00178                         if (options->drsFrg) getFringeTrackBatch (format, inFitsTempPtr, inFitsClassifiedPtr, 
00179                                 fileNameStr, classification, filePtr, &batchNum, &fileNumInList, error, parlist, frameset);
00180                 }
00181                 else if (((strcmp (format->obsCatg, "CALIB") == 0) || 
00182                         (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00183                         ((strcmp (format->obsTech, "IMAGE,WINDOW") == 0) || 
00184                         (strcmp (format->obsType, "COARSE,OBJECT") == 0)))
00185                 {
00186                         if (options->drsAcq) getAcquisitionBatch (inFitsTempPtr, fileNameStr, classification, 
00187                                 filePtr, &batchNum, error);
00188                 }
00189                 else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
00190                         ((strcmp (format->obsTech, "IMAGE") == 0) || 
00191                         (strcmp (format->obsTech, "SPECTRUM") == 0)) &&
00192                         (strcmp (format->obsType, "FLAT") == 0) &&
00193                         (format->fileNumInTemplate == 1))
00194                 {
00195                         if (options->tecLin) getDetLinBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
00196                                 classification, filePtr, &batchNum, &fileNumInList, error);
00197                 }
00198                 else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
00199                         (strcmp (format->obsTech, "IMAGE") == 0) &&
00200                         (strcmp (format->obsType, "BIAS") == 0) &&
00201                         (format->fileNumInTemplate == 1))
00202                 {
00203                         if (options->tecRon) getDetRonBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
00204                                 classification, filePtr, &batchNum, &fileNumInList, error);
00205                 }
00206                 else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
00207                         (strcmp (format->obsTech, "SPECTRUM") == 0) &&
00208                         (strcmp (format->obsType, "WAVE") == 0) &&
00209                         (format->fileNumInTemplate == 1))
00210                 {
00211                         if (options->tecTrn) getDspTrnBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
00212                                 classification, filePtr, &batchNum, &fileNumInList, error);
00213                 }
00214                 else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
00215                         (strcmp (format->obsTech, "IMAGE") == 0) &&
00216                         (strcmp (format->obsType, "FMTCHCK") == 0) &&
00217                         (format->fileNumInTemplate == 1))
00218                 {
00219                         if (options->tecPix) getRefPixBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
00220                                 classification, filePtr, &batchNum, &fileNumInList, error);
00221                 }
00222                 else if ((strcmp (format->obsCatg, "CALIB") == 0) &&
00223                         (strcmp (format->obsTech, "SPECTRUM") == 0) &&
00224                         (strcmp (format->obsType, "WAVE,SPECTEMPL") == 0) &&
00225                         (format->fileNumInTemplate == 1))
00226                 {
00227                         if (options->tecWav) getWavCalBatch (format, inFitsTempPtr, inFitsClassifiedPtr, fileNameStr,
00228                                 classification, filePtr, &batchNum, &fileNumInList, error);
00229                 }
00230                 else
00231                 {       
00232                 continue;
00233                 }
00234         }
00235 
00236     //  Copy the content of the main temporary file into the main list
00237     if (inFitsTempPtr) fclose (inFitsTempPtr);
00238     if (inFitsClassifiedPtr) 
00239     {
00240                 fclose (inFitsClassifiedPtr);
00241                 inFitsClassifiedPtr = NULL;
00242         }
00243     if ((inFitsTempPtr = fopen (inFitsTemp, "r")) == NULL)
00244     {
00245         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list for reading");
00246         *error = 2;
00247         }
00248         else
00249         {
00250                 //      Check if the file is empty
00251                 if (fscanf (inFitsTempPtr, "%s\n", stringTemp) == EOF)
00252             {
00253                         if (options->drsFrg || options->drsAcq || options->tecRon || options->tecLin || 
00254                                 options->tecTrn || options->tecPix || options->tecWav)
00255                         {
00256                                 *error = 2;
00257                                 sprintf (midiMessage, "%s\n\t     %s\n\t\t     %s\n\t\t     %s", 
00258                                         "Cannot find a complete set of FITS files in the given list or directory.",
00259                                         "Check the following log files:", fileNames->inFitsClassified, inFitsTemp);
00260                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, midiMessage);
00261                         }
00262                         else
00263                         {
00264                                 *error = 1;
00265                                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, "No Batch Classification was requested");
00266                         }
00267                 }
00268                 else
00269                 {
00270                         rewind (inFitsTempPtr);
00271                         if ((inFitsClassifiedPtr = fopen (fileNames->inFitsClassified, "w")) == NULL)
00272                         {
00273                                 *error = 2;
00274                                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list for writing");
00275                         }
00276                         else
00277                         {
00278                                 fileCount = 0;
00279                                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsTempPtr) != NULL)
00280                                 {
00281                                         //      Get template name first
00282                                         fileCount++;
00283                                         if (fileCount == 1)
00284                                         {
00285                                                 fprintf (inFitsClassifiedPtr, "%s", stringTemp);
00286                                                 continue;
00287                                         }
00288                                         if (strcmp (stringTemp, "END_OF_BATCH\n") == 0)
00289                                                 fileCount = 0;
00290                                         sprintf (classification, "%s", "");
00291                                         sscanf (stringTemp, "%s%s", fileNameStr, classification);
00292                                         fprintf (inFitsClassifiedPtr, "%s %s\n", fileNameStr, classification);
00293                                 }
00294                         }
00295                 }
00296         }
00297 
00298     //  Close all files
00299     if (inFitsClassifiedPtr) fclose (inFitsClassifiedPtr);
00300     if (inFitsTempPtr) fclose (inFitsTempPtr);
00301     if (*error < 2) remove (inFitsTemp);
00302 
00303     //  Release memory
00304     if (stringTemp) free (stringTemp);
00305     if (inFitsTemp) free (inFitsTemp);
00306     if (classification) free (classification);
00307     if (fileNameStr) free (fileNameStr);
00308         if (format) freeImageFormat (format);
00309 
00310     return;
00311 }
00312 /******************************************************************************/
00313 
00314 
00315 /******************************************************************************
00316 *               European Southern Observatory
00317 *            VLTI MIDI Data Reduction Software
00318 *
00319 * Module name:  getFitsClassification
00320 * Input/Output: See function arguments to avoid duplication
00321 * Description:  A relatively quicker routine to get the fits file classification
00322 *
00323 * History:
00324 * 08-Mar-05     (csabet) Created
00325 ******************************************************************************/
00326 void getFitsClassification (
00327     int                 fileNum,        // In: File counter
00328     FILE                *reportPtr,     // In: Pointer to a temporary report file
00329     char                *fileName,      // In: Name of the input FITS file
00330     ImageFormat *format,        // Ou: Classification
00331     int                 *error)         // Ou: Error status
00332 
00333 {
00334 
00335     /*  Local Declarations
00336     --------------------*/
00337     const char  routine[] = "getFitsClassification";
00338     char                *stringQfits, *emptyString, *cleanString;
00339     int                 unknownNumber;
00340     struct stat buf;
00341 
00342     /*  Algorithm
00343     -----------*/
00344     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00345     if (diagnostic > 4) fprintf (reportPtr, "Invoking      routine   '%s' \n", routine);
00346 
00347         //      Reset status
00348     *error = 0;
00349     unknownNumber = - 1;
00350 
00351     //  Check if the file exist
00352     if (stat (fileName, &buf) == -1)
00353     {
00354                 sprintf (midiMessage, "Cannot find file ... %s", fileName);
00355         midiReportWarning (reportPtr, routine, __FILE__, __LINE__, midiMessage);
00356         *error = 1;
00357         return;
00358     }
00359     else if ((qfits_is_fits (fileName)) != 1)  // Check if this is a FITS file
00360     {
00361                 sprintf (midiMessage, "Not a FITS file ... %s", fileName);
00362         midiReportWarning (reportPtr, routine, __FILE__, __LINE__, midiMessage);
00363         *error = 1;
00364         return;
00365     }
00366 
00367         //      Allocate memory
00368     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00369     emptyString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00370     strcpy (emptyString, "UNKNOWN");
00371 
00372         //      Get data Observation Category
00373         stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO DPR CATG");
00374         if (stringQfits == NULL) sprintf (format->obsCatg, "%s", emptyString);
00375         else
00376         {
00377                 cleanUpString (stringQfits, cleanString);
00378                 sprintf (format->obsCatg, "%s", cleanString);
00379         }
00380 
00381         //      Get data Observation Technique
00382         stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO DPR TECH");
00383         if (stringQfits == NULL) sprintf (format->obsTech, "%s", emptyString);
00384         else
00385         {
00386                 cleanUpString (stringQfits, cleanString);
00387                 sprintf (format->obsTech, "%s", cleanString);
00388         }
00389 
00390         //      Get data Observation Type
00391         stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO DPR TYPE");
00392         if (stringQfits == NULL) sprintf (format->obsType, "%s", emptyString);
00393         else
00394         {
00395                 cleanUpString (stringQfits, cleanString);
00396                 sprintf (format->obsType, "%s", cleanString);
00397         }
00398 
00399         //      Get target name
00400         stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TARG NAME");
00401         if (stringQfits == NULL) sprintf (format->targetName, "%s", emptyString);
00402         else
00403         {
00404                 cleanUpString (stringQfits, cleanString);
00405                 sprintf (format->targetName, "%s", cleanString);
00406         }
00407 
00408     //  Get TPL Start
00409     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL START");
00410     if (stringQfits == NULL) sprintf (format->tplStart, "%s", emptyString);
00411     else
00412     {
00413         cleanUpString (stringQfits, cleanString);
00414                 sprintf (format->tplStart, "%s", cleanString);
00415     }
00416 
00417     //  Get number of templates
00418     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TPLNO");
00419     if (stringQfits == NULL) format->numOfTemplates = unknownNumber;
00420     else sscanf (stringQfits, "%d", &(format->numOfTemplates));
00421 
00422     //  Get template's number of files
00423     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OCS TPL NFILE");
00424     if (stringQfits == NULL) format->numOfFilesInTemplate = unknownNumber;
00425     else sscanf (stringQfits, "%d", &(format->numOfFilesInTemplate));
00426 
00427     //  Get template's sequential file number
00428     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OCS TPL FILENO");
00429     if (stringQfits == NULL) format->fileNumInTemplate = unknownNumber;
00430     else sscanf (stringQfits, "%d", &(format->fileNumInTemplate));
00431 
00432     //  Get template's Exposure number
00433     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL EXPNO");
00434     if (stringQfits == NULL) format->exposureNumInTemplate = unknownNumber;
00435     else sscanf (stringQfits, "%d", &(format->exposureNumInTemplate));
00436 
00437     //  Get template's number of exposures
00438     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL NEXP");
00439     if (stringQfits == NULL) format->numOfExposures = unknownNumber;
00440     else sscanf (stringQfits, "%d", &(format->numOfExposures));
00441 
00442     //  Get template's file number within exposures
00443     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OCS EXPO FILENO");
00444     if (stringQfits == NULL) format->fileNumInExposure = unknownNumber;
00445     else sscanf (stringQfits, "%d", &(format->fileNumInExposure));
00446 
00447         //      Get template name
00448     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO TPL NAME");
00449         if (stringQfits == NULL) sprintf (format->tplName, "%s", emptyString);
00450         else
00451         {
00452                 cleanUpString (stringQfits, cleanString);
00453                 sprintf (format->tplName, "%s", cleanString);
00454         }
00455  
00456         //      Get beam combiner
00457         stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO INS OPT1 ID");
00458         if (stringQfits == NULL) sprintf (format->beamCombiner, "%s", emptyString);
00459         else
00460         {
00461                 cleanUpString (stringQfits, cleanString);
00462                 sprintf (format->beamCombiner, "%s", cleanString);
00463         }
00464 
00465         //      Get Shutter ID
00466         stringQfits = qfits_query_ext (fileName, "HIERARCH ESO INS SHUT ID", 0);
00467         if (stringQfits == NULL) sprintf (format->shutterId, "%s", emptyString);
00468         else
00469         {
00470                 cleanUpString (stringQfits, cleanString);
00471                 sprintf (format->shutterId, "%s", cleanString);
00472         }
00473 
00474     if (diagnostic)
00475     {
00476                 cpl_msg_info(cpl_func,"\nfile: %4d\n", fileNum);
00477                 cpl_msg_info(cpl_func,"fileName: %s\n", fileName);
00478                 cpl_msg_info(cpl_func,"tplName                (TPL NAME): %s\n", format->tplName);
00479                 cpl_msg_info(cpl_func,"tplStart              (TPL START): %s\n", format->tplStart);
00480                 if (format->fileNumInTemplate == -1) 
00481                         cpl_msg_info(cpl_func,"fileNumInTemplate    (TPL FILENO): UNKNOWN\n");
00482                 else
00483                         cpl_msg_info(cpl_func,"fileNumInTemplate    (TPL FILENO): %d\n", format->fileNumInTemplate);
00484                 
00485                 if (format->numOfFilesInTemplate == -1)
00486                         cpl_msg_info(cpl_func,"numOfFilesInTemplate  (TPL NFILE): UNKNOWN\n");
00487                 else
00488                         cpl_msg_info(cpl_func,"numOfFilesInTemplate  (TPL NFILE): %d\n", format->numOfFilesInTemplate);
00489                 
00490                 if (format->numOfTemplates)
00491                         cpl_msg_info(cpl_func,"numOfTemplates        (OBS TPLNO): UNKNOWN\n");
00492                 else
00493                         cpl_msg_info(cpl_func,"numOfTemplates        (OBS TPLNO): %d\n", format->numOfTemplates);
00494                 
00495                 if (format->numOfExposures)
00496                         cpl_msg_info(cpl_func,"numOfExposures         (TPL NEXP): UNKNOWN\n");
00497                 else
00498                         cpl_msg_info(cpl_func,"numOfExposures         (TPL NEXP): %d\n", format->numOfExposures);
00499                 
00500                 if (format->fileNumInExposure)
00501                         cpl_msg_info(cpl_func,"fileNumInExposure   (EXPO FILENO): UNKNOWN\n");
00502                 else
00503                         cpl_msg_info(cpl_func,"fileNumInExposure   (EXPO FILENO): %d\n", format->fileNumInExposure);
00504 
00505                 if (format->exposureNumInTemplate)
00506                         cpl_msg_info(cpl_func,"exposureNumInTemplate (TPL EXPNO): UNKNOWN\n");
00507                 else
00508                         cpl_msg_info(cpl_func,"exposureNumInTemplate (TPL EXPNO): %d\n", format->exposureNumInTemplate);
00509                 cpl_msg_info(cpl_func,"targetName            (TARG NAME): %s\n", format->targetName);
00510                 cpl_msg_info(cpl_func,"beamCombiner        (INS OPT1 ID): %s\n", format->beamCombiner);
00511                 cpl_msg_info(cpl_func,"shutterId           (INS SHUT ID): %s\n", format->shutterId);
00512                 cpl_msg_info(cpl_func,"observationCategory    (DPR CATG): %s\n", format->obsCatg);
00513                 cpl_msg_info(cpl_func,"observationType        (DPR TYPE): %s\n", format->obsType);
00514                 cpl_msg_info(cpl_func,"observationTechnique   (DPR TECH): %s\n", format->obsTech);
00515     }
00516 
00517         fprintf (reportPtr, "\nfile: %4d\n", fileNum);
00518         fprintf (reportPtr, "fileName: %s\n", fileName);
00519         fprintf (reportPtr, "tplName                (TPL NAME): %s\n", format->tplName);
00520         fprintf (reportPtr, "tplStart              (TPL START): %s\n", format->tplStart);
00521         if (format->fileNumInTemplate == -1) 
00522                 fprintf (reportPtr, "fileNumInTemplate    (TPL FILENO): UNKNOWN\n");
00523         else
00524                 fprintf (reportPtr, "fileNumInTemplate    (TPL FILENO): %d\n", format->fileNumInTemplate);
00525         
00526         if (format->numOfFilesInTemplate == -1)
00527                 fprintf (reportPtr, "numOfFilesInTemplate  (TPL NFILE): UNKNOWN\n");
00528         else
00529                 fprintf (reportPtr, "numOfFilesInTemplate  (TPL NFILE): %d\n", format->numOfFilesInTemplate);
00530                 
00531         if (format->numOfTemplates)
00532                 fprintf (reportPtr, "numOfTemplates        (OBS TPLNO): UNKNOWN\n");
00533         else
00534                 fprintf (reportPtr, "numOfTemplates        (OBS TPLNO): %d\n", format->numOfTemplates);
00535         
00536         if (format->numOfExposures)
00537                 fprintf (reportPtr, "numOfExposures         (TPL NEXP): UNKNOWN\n");
00538         else
00539                 fprintf (reportPtr, "numOfExposures         (TPL NEXP): %d\n", format->numOfExposures);
00540                 
00541         if (format->fileNumInExposure)
00542                 fprintf (reportPtr, "fileNumInExposure   (EXPO FILENO): UNKNOWN\n");
00543         else
00544                 fprintf (reportPtr, "fileNumInExposure   (EXPO FILENO): %d\n", format->fileNumInExposure);
00545 
00546         if (format->exposureNumInTemplate)
00547                 fprintf (reportPtr, "exposureNumInTemplate (TPL EXPNO): UNKNOWN\n");
00548         else
00549                 fprintf (reportPtr, "exposureNumInTemplate (TPL EXPNO): %d\n", format->exposureNumInTemplate);
00550         fprintf (reportPtr, "targetName            (TARG NAME): %s\n", format->targetName);
00551         fprintf (reportPtr, "beamCombiner        (INS OPT1 ID): %s\n", format->beamCombiner);
00552         fprintf (reportPtr, "shutterId           (INS SHUT ID): %s\n", format->shutterId);
00553         fprintf (reportPtr, "observationCategory    (DPR CATG): %s\n", format->obsCatg);
00554         fprintf (reportPtr, "observationType        (DPR TYPE): %s\n", format->obsType);
00555         fprintf (reportPtr, "observationTechnique   (DPR TECH): %s\n", format->obsTech);
00556 
00557     /*  Release memory */
00558     free (emptyString);
00559     free (cleanString);
00560 
00561     return;
00562 }
00563 /*****************************************************************************/
00564 
00565 
00566 /******************************************************************************
00567 *               European Southern Observatory
00568 *            VLTI MIDI Data Reduction Software
00569 *
00570 * Module name:  getFringeTrackBatch
00571 * Input/Output: See function arguments to avoid duplication
00572 * Description:  We know the first file in the given list is a TRACK. We
00573 *                               check to see if we can find a complete batch. If not the file
00574 *                               pointer is returned unchanged.
00575 *
00576 * History:
00577 * 28-Apr-05             (csabet) Created
00578 ******************************************************************************/
00579 void getFringeTrackBatch (
00580     ImageFormat *format,                // In: Format structure
00581     FILE              *inFitsTempPtr, // In: Pointer to the main buffer
00582     FILE              *inFitsListPtr, // In: Pointer to the list of FITS files
00583     char              *fileNameStr,   // In: Name of the first FITS file
00584     char              *dummyClassif,  // In: Dummy classification
00585     FILE              *filePtr,               // In: Pointer to the log file
00586     int               *localBatchNum, // IO: Local batch number
00587     int               *fileNumInList, // IO: pointer to file number
00588     int               *error,                 // Ou: Error status
00589     cpl_parameterlist *parlist,
00590     cpl_frameset      *frameset)
00591 
00592 {
00593 
00594     //  Local Declarations
00595     //  ------------------
00596     const char  routine[] = "getFringeTrackBatch";
00597         char            *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL, *localTplName=NULL,
00598                                 *localBeamCombiner, *localObsCatg, *localObsTech, *localObsType;
00599         FILE            *inFitsBatchTempPtr=NULL;
00600         int                     i, numOfInterfFiles, numOfPhotAFiles, numOfPhotBFiles, fileNumInTemplate, numOfEmptyFiles,
00601                                 localFileNumInList, numOfFiles;
00602         long            inFitsListPosition;
00603         int    checkSof=0;
00604         cpl_parameter * cur_param;
00605     //  Algorithm
00606     //  ---------
00607     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00608     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
00609 
00610         //      Reset status
00611         *error = 0;
00612         numOfFiles = 0;
00613         numOfInterfFiles = 0;
00614         numOfPhotAFiles = 0;
00615         numOfPhotBFiles = 0;
00616         numOfEmptyFiles = 0;
00617 
00618         /*Get the recipe parameter*/
00619         cur_param=cpl_parameterlist_find(parlist,
00620                 "midi.midi_fringe_all.checkSof");
00621         checkSof=cpl_parameter_get_bool(cur_param);
00622 
00623         //      Allocate memory
00624     inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00625     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00626     localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00627     localTplName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00628     localBeamCombiner = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00629     localObsCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00630     localObsTech = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00631     localObsType = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00632 
00633         //      Open a batch temporary file for writing
00634         sprintf (inFitsBatchTemp, "localBuffer.log");
00635         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
00636         {
00637                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
00638                 *error = 1;
00639                 free (inFitsBatchTemp);
00640                 free (localTplStart);
00641                 free (localTplName);
00642                 free (stringTemp);
00643                 free (localBeamCombiner);
00644                 free (localObsCatg);
00645                 free (localObsTech);
00646                 free (localObsType);
00647                 return;
00648         }
00649                                 
00650         //      Save the first file and its parameters
00651         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00652         inFitsListPosition = ftell (inFitsListPtr);
00653         localFileNumInList = *fileNumInList;
00654 
00655         if (strcmp (format->obsTech, "INTERFEROMETRY") == 0) 
00656                 numOfInterfFiles++;
00657         else if (strcmp (format->shutterId, "AOPEN") == 0) 
00658                 numOfPhotAFiles++;
00659         else if (strcmp (format->shutterId, "BOPEN") == 0) 
00660                 numOfPhotBFiles++;
00661 
00662         fileNumInTemplate = format->fileNumInTemplate;
00663 
00664         sprintf (localTplStart, "%s", format->tplStart);
00665         sprintf (localTplName, "%s", format->tplName);
00666         sprintf (localBeamCombiner, "%s", format->beamCombiner);
00667         sprintf (localObsCatg, "%s", format->obsCatg);
00668         sprintf (localObsTech, "%s", format->obsTech);
00669         sprintf (localObsType, "%s", format->obsType);
00670 
00671         sprintf (batchTemplate, "%s", format->tplName);
00672 
00673         //      Get the rest of the batch files from the list
00674         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
00675         {
00676                 sprintf (dummyClassif, "%s", "");
00677                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
00678 
00679                 localFileNumInList++;
00680                 fileNumInTemplate++;
00681 
00682                 //      Get FITS file parameters. If ok copy into the batch temporary file
00683                 getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
00684 
00685                       /*Check template name consistency. For the
00686                        * calibrator database the tpl Name is "UNKNOWN"*/
00687                 //if (strcmp (format->tplName, localTplName) != 0) *error = 1;
00688                 if (strcmp (format->tplName, localTplName) != 0 && strcmp (format->tplName, "UNKNOWN") != 0 )
00689                 {
00690                     *error = 1;
00691                 }
00692 
00693                 /*Checking for TPL START and OCS TPL FILENO (de)activating*/
00694                 if(checkSof==FALSE)
00695                 {
00696                     cpl_msg_warning(cpl_func,"Overruling TPL START homogeneity check");
00697                     cpl_msg_warning(cpl_func,"Overruling OCS TPL FILENO homogeneity check");
00698                     format->fileNumInTemplate=fileNumInTemplate;
00699                     sprintf (localTplStart, "%s", format->tplStart);
00700                 }
00701 
00702                 //      We expect possibly more split interferometry and photometry files and an empty file
00703                 if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) || 
00704                         (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00705                         (strcmp (format->obsTech, "INTERFEROMETRY") == 0) && 
00706                         ((strcmp (format->obsType, "TRACK,OBJECT,DISPERSED") == 0) || 
00707                         (strcmp (format->obsType, "TRACK,OBJECT,DISPERSED,SCIPHOT") == 0) ||
00708 
00709                         (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER") == 0) ||
00710                         (strcmp (format->obsType, "FRINGE_TRACK,OBJECT,FOURIER,SCI_PHOT") == 0) || 
00711  
00712                         (strcmp (format->obsType, "TRACK,OBJECT,DISPERSED") == 0)) &&
00713                         (strcmp (format->tplStart, localTplStart) == 0) && 
00714                         (format->fileNumInTemplate == fileNumInTemplate))
00715                 {
00716                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00717                         numOfInterfFiles++;
00718                 }
00719                 else if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) || 
00720                         (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00721                         (strcmp (format->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00722                         ((strcmp (format->obsType, "PHOTOMETRY,OBJECT") == 0)  ||
00723                         (strcmp (format->obsType, "PHOTOMETRY,OBJECT,SCIPHOT") == 0)) &&
00724                         (strcmp (format->tplStart, localTplStart) == 0) && 
00725                         (strcmp (format->shutterId, "AOPEN") == 0) &&
00726                         (format->fileNumInTemplate == fileNumInTemplate))
00727                 {
00728                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00729                         numOfPhotAFiles++;
00730                 }
00731                 else if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) || 
00732                         (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00733                         (strcmp (format->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00734                         ((strcmp (format->obsType, "PHOTOMETRY,OBJECT") == 0)  ||
00735                         (strcmp (format->obsType, "PHOTOMETRY,OBJECT,SCIPHOT") == 0)) &&
00736                         (strcmp (format->tplStart, localTplStart) == 0) && 
00737                         (strcmp (format->shutterId, "BOPEN") == 0) &&
00738                         (format->fileNumInTemplate == fileNumInTemplate))
00739                 {
00740                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00741                         numOfPhotBFiles++;
00742                 }
00743                 else if (!(*error) && ((strcmp (format->obsCatg, "CALIB") == 0) || 
00744                         (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00745                         (strcmp (format->obsTech, "OTHER") == 0) && 
00746                         (strcmp (format->obsType, "OTHER") == 0) &&
00747                         (strcmp (format->tplStart, localTplStart) == 0) &&
00748                         (format->fileNumInTemplate == fileNumInTemplate) && 
00749                         (numOfEmptyFiles < 1))
00750                 {
00751                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00752                         numOfEmptyFiles++;
00753                         break;
00754                 }
00755                 else
00756                 {
00757                         //      We have not found a complete set. Hence reset the file position
00758                         fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
00759                         localFileNumInList--;
00760                         break;
00761                 }
00762                 inFitsListPosition = ftell (inFitsListPtr);
00763         }
00764         *fileNumInList = localFileNumInList;
00765         
00766         //      If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
00767         if (!endOfTemplate) numOfEmptyFiles = 1;
00768         
00769         /*      Now finalise batchTemplate name. If batchTemplate 
00770         is valid then copy all files into the main temporary file */
00771         if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
00772                 (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
00773                 (strcmp (localBeamCombiner, "HIGH_SENS") == 0) && 
00774                 (strcmp (localObsCatg, "CALIB") == 0) &&
00775                 (numOfInterfFiles > 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) && 
00776                 (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
00777                 sprintf (batchTemplate, "%s", "HIGH_SENS_CALIB");
00778         else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
00779                 (strcmp (localTplName, "MIDI STARINTF OBS FRINGE SCI") == 0)) &&
00780                 (strcmp (localBeamCombiner, "HIGH_SENS") == 0) && 
00781                 (strcmp (localObsCatg, "SCIENCE") == 0) && 
00782                 (numOfInterfFiles > 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) && 
00783                 (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
00784                 sprintf (batchTemplate, "%s", "HIGH_SENS_SCIENCE");
00785         else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
00786                 (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
00787                 (strcmp (localBeamCombiner, "SCI_PHOT") == 0) && 
00788                 (strcmp (localObsCatg, "CALIB") == 0) && 
00789                 (numOfInterfFiles > 0) && (numOfPhotAFiles+numOfPhotBFiles == 0) && (numOfEmptyFiles == 1))
00790                 sprintf (batchTemplate, "%s", "SCI_PHOT_CALIB");
00791         else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
00792                 (strcmp (localTplName, "MIDI STARINTF OBS FRINGE SCI") == 0)) &&
00793                 (strcmp (localBeamCombiner, "SCI_PHOT") == 0) && 
00794                 (strcmp (localObsCatg, "SCIENCE") == 0) && 
00795                 (numOfInterfFiles > 0) && (numOfPhotAFiles+numOfPhotBFiles == 0) && (numOfEmptyFiles == 1))
00796                 sprintf (batchTemplate, "%s", "SCI_PHOT_SCIENCE");
00797         else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
00798                 (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
00799                 (strcmp (localBeamCombiner, "SCI_PHOT") == 0) && 
00800                 (numOfInterfFiles == 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) && 
00801                 (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
00802                 sprintf (batchTemplate, "%s", "SCI_PHOT_KAPPA");
00803         else if (((strcmp (localTplName, "MIDI STARINTF OBS FRINGE") == 0) ||
00804                 (strcmp (localTplName, "MIDI STARINTF OBS FRINGE CAL") == 0)) &&
00805                 (strcmp (localBeamCombiner, "SCI_PHOT") == 0) && 
00806                 (numOfInterfFiles > 0) && (numOfPhotAFiles > 0) && (numOfPhotBFiles > 0) && 
00807                 (numOfEmptyFiles == 1) && (numOfPhotAFiles == numOfPhotBFiles))
00808         {
00809                 numOfFiles = numOfInterfFiles + numOfPhotAFiles + numOfPhotBFiles + numOfEmptyFiles;
00810                 sprintf (batchTemplate, "%s", "SCI_PHOT_KAPPA");
00811         }
00812         else
00813         {
00814                 if (diagnostic > 1)
00815                 {
00816                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Unknown Template Configuration:");
00817                         cpl_msg_info(cpl_func,"     Template Name            = %s \n", localTplName);
00818                         cpl_msg_info(cpl_func,"     Beam Combiner            = %s \n", localBeamCombiner);
00819                         cpl_msg_info(cpl_func,"     Obs. Category            = %s \n", localObsCatg);
00820                         cpl_msg_info(cpl_func,"     Number of Interf files   = %d \n", numOfInterfFiles);
00821                         cpl_msg_info(cpl_func,"     Number of Photom A files = %d \n", numOfPhotAFiles);
00822                         cpl_msg_info(cpl_func,"     Number of Photom B files = %d \n", numOfPhotBFiles);
00823                         fprintf (filePtr, "     Template Name            = %s \n", localTplName);
00824                         fprintf (filePtr, "     Beam Combiner            = %s \n", localBeamCombiner);
00825                         fprintf (filePtr, "     Obs. Category            = %s \n", localObsCatg);
00826                         fprintf (filePtr, "     Number of Interf files   = %d \n", numOfInterfFiles);
00827                         fprintf (filePtr, "     Number of Photom A files = %d \n", numOfPhotAFiles);
00828                         fprintf (filePtr, "     Number of Photom B files = %d \n", numOfPhotBFiles);
00829                 }
00830                 free (inFitsBatchTemp);
00831                 free (localTplStart);
00832                 free (localTplName);
00833                 free (stringTemp);
00834                 free (localBeamCombiner);
00835                 free (localObsCatg);
00836                 free (localObsTech);
00837                 free (localObsType);
00838                 if (inFitsBatchTempPtr)
00839                 {
00840                         fclose (inFitsBatchTempPtr);
00841                         inFitsBatchTempPtr = NULL;
00842                 }
00843                 return;
00844         }
00845         
00846         //      Reopen the batch temporary file for reading
00847         fclose (inFitsBatchTempPtr);
00848         inFitsBatchTempPtr = NULL;
00849         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
00850         {
00851                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
00852                 *error = 1;
00853                 free (inFitsBatchTemp);
00854                 free (localTplStart);
00855                 free (localTplName);
00856                 free (stringTemp);
00857                 free (localBeamCombiner);
00858                 free (localObsCatg);
00859                 free (localObsTech);
00860                 free (localObsType);
00861                 return;
00862         }
00863 
00864         //      If it is a SCI_PHOT_KAPPA batch with interferometry files, it must be split into two batches
00865         if ((strcmp (batchTemplate, "SCI_PHOT_KAPPA") == 0) && (numOfInterfFiles > 0))
00866         {
00867                 //      First extract the Photometry files for kappa calculation, only if it is a CALIB template
00868                 if (strcmp (localObsCatg, "CALIB") == 0)
00869                 {
00870                         (*localBatchNum)++;
00871                         cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
00872                         fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
00873                         fprintf (inFitsTempPtr, "%s \n", batchTemplate);
00874                         for (i = 1; i <= numOfFiles; i++)
00875                         {
00876                                 fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr);
00877                                 if (i > numOfInterfFiles)
00878                                 {
00879                                         sprintf (dummyClassif, "%s", "");
00880                                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
00881                                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
00882                                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
00883                                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00884                                 }
00885                         }
00886                         cpl_msg_info(cpl_func,"\n");
00887                         fprintf (filePtr, "\n");
00888 
00889                         /*      We should now have a complete batch of files 
00890                         in the temporary file list. Add an appending keyword */
00891                         fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
00892                 }
00893 
00894                 //      Now rewind the batch list and extract the Interferometry files          
00895                 if (inFitsBatchTempPtr) rewind (inFitsBatchTempPtr);
00896                 (*localBatchNum)++;
00897                 if (strcmp (localObsCatg, "CALIB") == 0)
00898                 {
00899                         cpl_msg_info(cpl_func,"\nSelected batch  %d  is  SCI_PHOT_CALIB:\n", (*localBatchNum));
00900                         fprintf (filePtr, "\nSelected batch  %d:  is  SCI_PHOT_CALIB\n", (*localBatchNum));
00901                         fprintf (inFitsTempPtr, "%s \n", "SCI_PHOT_CALIB");
00902                 }
00903                 else
00904                 {
00905                         cpl_msg_info(cpl_func,"\nSelected batch  %d  is  SCI_PHOT_SCIENCE:\n", (*localBatchNum));
00906                         fprintf (filePtr, "\nSelected batch  %d:  is  SCI_PHOT_SCIENCE\n", (*localBatchNum));
00907                         fprintf (inFitsTempPtr, "%s \n", "SCI_PHOT_SCIENCE");
00908                 }
00909                 for (i = 1; i <= numOfFiles; i++)
00910                 {
00911                         fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr);
00912                         if (i <= numOfInterfFiles)
00913                         {
00914                                 sprintf (dummyClassif, "%s", "");
00915                                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
00916                                 cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
00917                                 fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
00918                                 fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00919                         }
00920                 }
00921                 cpl_msg_info(cpl_func,"\n");
00922                 fprintf (filePtr, "\n");
00923 
00924                 /*      We should now have a complete batch of files 
00925                 in the temporary file list. Add an appending keyword */
00926                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
00927                 if (inFitsBatchTempPtr)
00928                 {
00929                         fclose (inFitsBatchTempPtr);
00930                         inFitsBatchTempPtr = NULL;
00931                 }
00932                 remove (inFitsBatchTemp);
00933         }
00934         else
00935         {
00936                 (*localBatchNum)++;
00937                 cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
00938                 fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
00939                 fprintf (inFitsTempPtr, "%s \n", batchTemplate);
00940                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
00941                 {
00942                         sprintf (dummyClassif, "%s", "");
00943                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
00944                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
00945                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
00946                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
00947                 }
00948                 cpl_msg_info(cpl_func,"\n");
00949                 fprintf (filePtr, "\n");
00950 
00951                 /*      We should now have a complete batch of files 
00952                 in the temporary file list. Add an appending keyword */
00953                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
00954                 if (inFitsBatchTempPtr) 
00955                 {       
00956                         fclose (inFitsBatchTempPtr);
00957                         inFitsBatchTempPtr = NULL;
00958                 }
00959                 remove (inFitsBatchTemp);
00960         }
00961 
00962     //  Release memory
00963         if (inFitsBatchTempPtr)
00964         {
00965                 fclose (inFitsBatchTempPtr);
00966                 inFitsBatchTempPtr = NULL;
00967         }
00968     if (inFitsBatchTemp) free (inFitsBatchTemp);
00969         if (localTplStart) free (localTplStart);
00970         if (localTplName) free (localTplName);
00971         if (stringTemp) free (stringTemp);
00972         if (localBeamCombiner) free (localBeamCombiner);
00973         if (localObsCatg) free (localObsCatg);
00974         if (localObsTech) free (localObsTech);
00975         if (localObsType) free (localObsType);
00976 
00977         return;
00978 }
00979 /*****************************************************************************/
00980 
00981 
00982 /******************************************************************************
00983 *               European Southern Observatory
00984 *            VLTI MIDI Data Reduction Software
00985 *
00986 * Module name:  getAcquisitionBatch
00987 * Input/Output: See function arguments to avoid duplication
00988 * Description:  Acquisitio files are individual
00989 *
00990 * History:
00991 * 28-Apr-05             (csabet) Created
00992 ******************************************************************************/
00993 void getAcquisitionBatch (
00994     FILE                *inFitsTempPtr, // In: Pointer to the main buffer
00995         char            *fileNameStr,   // In: Name of the first FITS file
00996         char            *dummyClassif,  // In: Dummy classification
00997         FILE            *filePtr,               // In: Pointer to the log file
00998         int                     *localBatchNum, // IO: Local batch number
00999     int                 *error)                 // Ou: Error status
01000 {
01001 
01002     //  Local Declarations
01003     //  ------------------
01004     const char  routine[] = "getAcquisitionBatch";
01005         
01006     //  Algorithm
01007     //  ---------
01008     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01009     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
01010 
01011         //      Reset status
01012         *error = 0;
01013 
01014         (*localBatchNum)++;
01015         sprintf (batchTemplate, "%s", "ACQ");
01016         cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01017         fprintf (filePtr, "\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01018         sprintf (dummyClassif, "%s", "");
01019         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
01020         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
01021         fprintf (inFitsTempPtr, "%s \n", batchTemplate);
01022         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01023         fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
01024         cpl_msg_info(cpl_func,"\n");
01025         fprintf (filePtr, "\n");
01026 
01027         return;
01028 }
01029 /*****************************************************************************/
01030 
01031 
01032 
01033  
01034 /******************************************************************************
01035 *               European Southern Observatory
01036 *          VLTI MIDI Maintenance Templates Software
01037 *
01038 * Module name:  getDetLinBatch
01039 * Input/Output: See function arguments to avoid duplication
01040 * Description:  It loops through the given list of FITS files and searches for files
01041 *                               that relate to Detector Linearity template. It then creates
01042 *                               a batch file with the following format:
01043 *                               
01044 *                               <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC DETLIN
01045 *                               <FITS File 1>
01046 *                               <FITS File 2>
01047 *                               <.>
01048 *                               <.> 
01049 *                               <FITS File n>
01050 *                               END_OF_BATCH
01051 *
01052 * History:      
01053 * 14-Jun-04     (csabet) created
01054 ******************************************************************************/
01055 void getDetLinBatch (
01056     ImageFormat *format,                // In: Format structure
01057     FILE                *inFitsTempPtr, // In: Pointer to the main buffer
01058     FILE                *inFitsListPtr, // In: Pointer to the list of FITS files
01059         char            *fileNameStr,   // In: Name of the first FITS file
01060         char            *dummyClassif,  // In: Dummy classification
01061         FILE            *filePtr,               // In: Pointer to the log file
01062         int                     *localBatchNum, // IO: Local batch number
01063         int                     *fileNumInList, // IO: pointer to file number
01064     int                 *error)                 // Ou: Error status
01065 {
01066 
01067     //  Local Declarations
01068     //  ------------------
01069     const char  routine[] = "getDetLinBatch";
01070         char            *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
01071         FILE            *inFitsBatchTempPtr=NULL;
01072         int                     numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
01073                                 localFileNumInList;
01074         long            inFitsListPosition;
01075         
01076     //  Algorithm
01077     //  ---------
01078     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01079     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
01080 
01081         //      Reset status
01082         *error = 0;
01083         numOfDataFiles = 0;
01084         numOfEmptyFiles = 0;
01085 
01086         //      Allocate memory
01087     inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01088     localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01089     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01090 
01091         //      Open a batch temporary file for writing
01092         sprintf (inFitsBatchTemp, "localBuffer.log");
01093         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
01094         {
01095                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01096                 *error = 1;
01097                 free (inFitsBatchTemp);
01098                 free (localTplStart);
01099                 free (stringTemp);
01100                 return;
01101         }
01102                                 
01103         //      Save the first file and its parameters
01104         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01105         fileNumInTemplate = format->fileNumInTemplate;
01106         sscanf (format->tplStart, "%s", localTplStart);
01107         numOfDataFiles++;
01108         localFileNumInList = *fileNumInList;
01109         inFitsListPosition = ftell (inFitsListPtr);
01110         sprintf (batchTemplate, "%s", "DETLIN");
01111 
01112         //      Get the rest of the batch files from the list
01113         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
01114         {
01115                 sprintf (dummyClassif, "%s", "");
01116                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01117 
01118                 localFileNumInList++;
01119                 fileNumInTemplate++;
01120 
01121                 //      Get FITS file parameters. If ok copy into the batch temporary file
01122                 getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
01123 
01124                 //      We expect possibly more split files
01125                 if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01126                         ((strcmp (format->obsTech, "IMAGE") == 0) || 
01127                         (strcmp (format->obsTech, "SPECTRUM") == 0)) &&
01128                         (strcmp (format->obsType, "FLAT") == 0) &&
01129                         (strcmp (format->tplStart, localTplStart) == 0) &&
01130                         (format->fileNumInTemplate == fileNumInTemplate))
01131                 {
01132                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01133                         numOfDataFiles++;
01134                 }
01135                 else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01136                         (strcmp (format->obsTech, "OTHER") == 0) &&
01137                         (strcmp (format->obsType, "OTHER") == 0) &&
01138                         (strcmp (format->tplStart, localTplStart) == 0) &&
01139                         (format->fileNumInTemplate == fileNumInTemplate) && 
01140                         (numOfEmptyFiles < 1))
01141                 {
01142                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01143                         numOfEmptyFiles++;
01144                         break;
01145                 }
01146                 else
01147                 {
01148                         //      We have not found a complete set. Hence reset the file position
01149                         fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
01150                         localFileNumInList--;
01151                         break;
01152                 }
01153                 inFitsListPosition = ftell (inFitsListPtr);
01154         }
01155 
01156         //      If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
01157         if (!endOfTemplate) numOfEmptyFiles = 1;
01158 
01159         //      We expect at least one data file and one empty file. If there has been 
01160         //      a complete set of files then copy all files into the main temporary file
01161         if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
01162         {
01163                 //      Reopen the batch temporary file for reading
01164                 if (inFitsBatchTempPtr) 
01165                 {
01166                         fclose (inFitsBatchTempPtr);
01167                         inFitsBatchTempPtr = NULL;
01168                 }
01169                 if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
01170                 {
01171                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01172                         *error = 1;
01173                         free (inFitsBatchTemp);
01174                         free (localTplStart);
01175                         free (stringTemp);
01176                         return;
01177                 }
01178 
01179                 (*localBatchNum)++;
01180                 cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01181                 fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
01182                 fprintf (inFitsTempPtr, "%s \n", batchTemplate);
01183                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
01184                 {
01185                         sprintf (dummyClassif, "%s", "");
01186                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01187                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
01188                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
01189                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01190                 }
01191                 cpl_msg_info(cpl_func,"\n");
01192                 fprintf (filePtr, "\n");
01193         
01194                 //      We should now have a complete set of files
01195                 //      in the temporary file list. Add an appending keyword
01196                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
01197         }
01198         *fileNumInList = localFileNumInList;
01199         if (inFitsBatchTempPtr) 
01200         {
01201                 fclose (inFitsBatchTempPtr);
01202                 inFitsBatchTempPtr = NULL;
01203         }
01204         remove (inFitsBatchTemp);
01205 
01206     //  Release memory
01207     if (inFitsBatchTemp) free (inFitsBatchTemp);
01208         if (localTplStart) free (localTplStart);
01209         if (stringTemp) free (stringTemp);
01210 
01211         return;
01212 }
01213 /*****************************************************************************/
01214  
01215 
01216 /******************************************************************************
01217 *               European Southern Observatory
01218 *          VLTI MIDI Maintenance Templates Software
01219 *
01220 * Module name:  getDetRonBatch
01221 * Input/Output: See function arguments to avoid duplication
01222 * Description:  It loops through the given list of FITS files and searches for files
01223 *                               that relate to Detector Readout Noise template. It then creates
01224 *                               a batch file with the following format:
01225 *                               
01226 *                               <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC DETRON
01227 *                               <FITS File 1>
01228 *                               <FITS File 2>
01229 *                               <.>
01230 *                               <.> 
01231 *                               <FITS File n>
01232 *                               END_OF_BATCH
01233 *
01234 * History:      
01235 * 14-Jun-04     (csabet) created
01236 ******************************************************************************/
01237 void getDetRonBatch (
01238     ImageFormat *format,                // In: Format structure
01239     FILE                *inFitsTempPtr, // In: Pointer to the main buffer
01240     FILE                *inFitsListPtr, // In: Pointer to the list of FITS files
01241         char            *fileNameStr,   // In: Name of the first FITS file
01242         char            *dummyClassif,  // In: Dummy classification
01243         FILE            *filePtr,               // In: Pointer to the log file
01244         int                     *localBatchNum, // IO: Local batch number
01245         int                     *fileNumInList, // IO: pointer to file number
01246     int                 *error)                 // Ou: Error status
01247 {
01248 
01249     //  Local Declarations
01250     //  ------------------
01251     const char  routine[] = "getDetRonBatch";
01252         char            *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
01253         FILE            *inFitsBatchTempPtr=NULL;
01254         int                     numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
01255                                 localFileNumInList;
01256         long            inFitsListPosition;
01257         
01258     //  Algorithm
01259     //  ---------
01260     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01261     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
01262 
01263         //      Reset status
01264         *error = 0;
01265         numOfDataFiles = 0;
01266         numOfEmptyFiles = 0;
01267 
01268         //      Allocate memory
01269     inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01270     localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01271     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01272 
01273         //      Open a batch temporary file for writing
01274         sprintf (inFitsBatchTemp, "localBuffer.log");
01275         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
01276         {
01277                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01278                 *error = 1;
01279                 free (inFitsBatchTemp);
01280                 free (localTplStart);
01281                 free (stringTemp);
01282                 return;
01283         }
01284                                 
01285         //      Save the first file and its parameters
01286         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01287         fileNumInTemplate = format->fileNumInTemplate;
01288         sscanf (format->tplStart, "%s", localTplStart);
01289         numOfDataFiles++;
01290         localFileNumInList = *fileNumInList;
01291         inFitsListPosition = ftell (inFitsListPtr);
01292         sprintf (batchTemplate, "%s", "DETRON");
01293 
01294         //      Get the rest of the batch files from the list
01295         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
01296         {
01297                 sprintf (dummyClassif, "%s", "");
01298                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01299 
01300                 localFileNumInList++;
01301                 fileNumInTemplate++;
01302 
01303                 //      Get FITS file parameters. If ok copy into the batch temporary file
01304                 getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
01305 
01306                 //      We expect possibly more split files
01307                 if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01308                         (strcmp (format->obsTech, "IMAGE") == 0) &&
01309                         (strcmp (format->obsType, "BIAS") == 0) &&
01310                         (strcmp (format->tplStart, localTplStart) == 0) &&
01311                         (format->fileNumInTemplate == fileNumInTemplate))
01312                 {
01313                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01314                         numOfDataFiles++;
01315                 }
01316                 else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01317                         (strcmp (format->obsTech, "OTHER") == 0) &&
01318                         (strcmp (format->obsType, "OTHER") == 0) &&
01319                         (strcmp (format->tplStart, localTplStart) == 0) &&
01320                         (format->fileNumInTemplate == fileNumInTemplate) && 
01321                         (numOfEmptyFiles < 1))
01322                 {
01323                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01324                         numOfEmptyFiles++;
01325                         break;
01326                 }
01327                 else
01328                 {
01329                         //      We have not found a complete set. Hence reset the file position
01330                         fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
01331                         localFileNumInList--;
01332                         break;
01333                 }
01334                 inFitsListPosition = ftell (inFitsListPtr);
01335         }
01336 
01337         //      If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
01338         if (!endOfTemplate) numOfEmptyFiles = 1;
01339 
01340         //      We expect at least one data file and one empty file. If there has been 
01341         //      a complete set of files then copy all files into the main temporary file
01342         if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
01343         {
01344                 //      Reopen the batch temporary file for reading
01345                 if (inFitsBatchTempPtr) 
01346                 {
01347                         fclose (inFitsBatchTempPtr);
01348                         inFitsBatchTempPtr = NULL;
01349                 }
01350                 if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
01351                 {
01352                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01353                         *error = 1;
01354                         free (inFitsBatchTemp);
01355                         free (localTplStart);
01356                         free (stringTemp);
01357                         return;
01358                 }
01359 
01360                 (*localBatchNum)++;
01361                 cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01362                 fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
01363                 fprintf (inFitsTempPtr, "%s \n", batchTemplate);
01364                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
01365                 {
01366                         sprintf (dummyClassif, "%s", "");
01367                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01368                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
01369                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
01370                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01371                 }
01372                 cpl_msg_info(cpl_func,"\n");
01373                 fprintf (filePtr, "\n");
01374         
01375                 //      We should now have a complete set of files in the 
01376                 //      temporary file list. Add an appending keyword
01377                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
01378         }
01379         *fileNumInList = localFileNumInList;
01380         if (inFitsBatchTempPtr) 
01381         {
01382                 fclose (inFitsBatchTempPtr);
01383                 inFitsBatchTempPtr = NULL;
01384         }
01385         remove (inFitsBatchTemp);
01386 
01387     //  Release memory
01388     if (inFitsBatchTemp) free (inFitsBatchTemp);
01389         if (localTplStart) free (localTplStart);
01390         if (stringTemp) free (stringTemp);
01391 
01392         return;
01393 }
01394 /*****************************************************************************/
01395 
01396 
01397 
01398 /******************************************************************************
01399 *               European Southern Observatory
01400 *          VLTI MIDI Maintenance Templates Software
01401 *
01402 * Module name:  getDspTrnBatch
01403 * Input/Output: See function arguments to avoid duplication
01404 * Description:  It loops through the given list of FITS files and searches for files
01405 *                               that relate to Detector Readout Noise template. It then creates
01406 *                               a batch file with the following format:
01407 *                               
01408 *                               <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC DSPTRN
01409 *                               <FITS File 1>
01410 *                               <FITS File 2>
01411 *                               <.>
01412 *                               <.> 
01413 *                               <FITS File n>
01414 *                               END_OF_BATCH
01415 *
01416 * History:      
01417 * 14-Jun-04     (csabet) created
01418 ******************************************************************************/
01419 void getDspTrnBatch (
01420     ImageFormat *format,                // In: Format structure
01421     FILE                *inFitsTempPtr, // In: Pointer to the main buffer
01422     FILE                *inFitsListPtr, // In: Pointer to the list of FITS files
01423         char            *fileNameStr,   // In: Name of the first FITS file
01424         char            *dummyClassif,  // In: Dummy classification
01425         FILE            *filePtr,               // In: Pointer to the log file
01426         int                     *localBatchNum, // IO: Local batch number
01427         int                     *fileNumInList, // IO: pointer to file number
01428     int                 *error)                 // Ou: Error status
01429 {
01430 
01431     //  Local Declarations
01432     //  ------------------
01433     const char  routine[] = "getDspTrnBatch";
01434         char            *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
01435         FILE            *inFitsBatchTempPtr=NULL;
01436         int                     numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
01437                                 localFileNumInList;
01438         long            inFitsListPosition;
01439         
01440     //  Algorithm
01441     //  ---------
01442     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01443     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
01444 
01445         //      Reset status
01446         *error = 0;
01447         numOfDataFiles = 0;
01448         numOfEmptyFiles = 0;
01449 
01450         //      Allocate memory
01451     inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01452     localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01453     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01454 
01455         //      Open a batch temporary file for writing
01456         sprintf (inFitsBatchTemp, "localBuffer.log");
01457         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
01458         {
01459                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01460                 *error = 1;
01461                 free (inFitsBatchTemp);
01462                 free (localTplStart);
01463                 free (stringTemp);
01464                 return;
01465         }
01466                                 
01467         //      Save the first file and its parameters
01468         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01469         fileNumInTemplate = format->fileNumInTemplate;
01470         sscanf (format->tplStart, "%s", localTplStart);
01471         numOfDataFiles++;
01472         localFileNumInList = *fileNumInList;
01473         inFitsListPosition = ftell (inFitsListPtr);
01474         sprintf (batchTemplate, "%s", "DSPTRN");
01475 
01476         //      Get the rest of the batch files from the list
01477         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
01478         {
01479                 sprintf (dummyClassif, "%s", "");
01480                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01481 
01482                 localFileNumInList++;
01483                 fileNumInTemplate++;
01484 
01485                 //      Get FITS file parameters. If ok copy into the batch temporary file
01486                 getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
01487 
01488                 //      We expect possibly more split files
01489                 if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01490                         (strcmp (format->obsTech, "SPECTRUM") == 0) &&
01491                         (strcmp (format->obsType, "WAVE") == 0) &&
01492                         (strcmp (format->tplStart, localTplStart) == 0) &&
01493                         (format->fileNumInTemplate == fileNumInTemplate))
01494                 {
01495                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01496                         numOfDataFiles++;
01497                 }
01498                 else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01499                         (strcmp (format->obsTech, "OTHER") == 0) &&
01500                         (strcmp (format->obsType, "OTHER") == 0) &&
01501                         (strcmp (format->tplStart, localTplStart) == 0) &&
01502                         (format->fileNumInTemplate == fileNumInTemplate) && 
01503                         (numOfEmptyFiles < 1))
01504                 {
01505                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01506                         numOfEmptyFiles++;
01507                         break;
01508                 }
01509                 else
01510                 {
01511                         //      We have not found a complete set. Hence reset the file position
01512                         fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
01513                         localFileNumInList--;
01514                         break;
01515                 }
01516                 inFitsListPosition = ftell (inFitsListPtr);
01517         }
01518 
01519         //      If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
01520         if (!endOfTemplate) numOfEmptyFiles = 1;
01521 
01522         //      We expect at least one data file and one empty file. If there has been 
01523         //      a complete set of files then copy all files into the main temporary file
01524         if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
01525         {
01526                 //      Reopen the batch temporary file for reading
01527                 if (inFitsBatchTempPtr) 
01528                 {
01529                         fclose (inFitsBatchTempPtr);
01530                         inFitsBatchTempPtr = NULL;
01531                 }
01532                 if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
01533                 {
01534                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01535                         *error = 1;
01536                         free (inFitsBatchTemp);
01537                         free (localTplStart);
01538                         free (stringTemp);
01539                         return;
01540                 }
01541 
01542                 (*localBatchNum)++;
01543                 cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01544                 fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
01545                 fprintf (inFitsTempPtr, "%s \n", batchTemplate);
01546                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
01547                 {
01548                         sprintf (dummyClassif, "%s", "");
01549                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01550                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
01551                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
01552                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01553                 }
01554                 cpl_msg_info(cpl_func,"\n");
01555                 fprintf (filePtr, "\n");
01556         
01557                 //      We should now have a complete set of files
01558                 //      in the temporary file list. Add an appending keyword
01559                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
01560         }
01561         *fileNumInList = localFileNumInList;
01562         if (inFitsBatchTempPtr) 
01563         {
01564                 fclose (inFitsBatchTempPtr);
01565                 inFitsBatchTempPtr = NULL;
01566         }
01567         remove (inFitsBatchTemp);
01568 
01569     //  Release memory
01570     if (inFitsBatchTemp) free (inFitsBatchTemp);
01571         if (localTplStart) free (localTplStart);
01572         if (stringTemp) free (stringTemp);
01573 
01574         return;
01575 }
01576 /*****************************************************************************/
01577  
01578  
01579 /******************************************************************************
01580 *               European Southern Observatory
01581 *          VLTI MIDI Maintenance Templates Software
01582 *
01583 * Module name:  getRefPixBatch
01584 * Input/Output: See function arguments to avoid duplication
01585 * Description:  It loops through the given list of FITS files and searches for files
01586 *                               that relate to the Reference Pixels Alignment template. It then creates
01587 *                               a batch file with the following format:
01588 *                               
01589 *                               <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC REFPIX
01590 *                               <FITS File 1>
01591 *                               <FITS File 2>
01592 *                               <.>
01593 *                               <.> 
01594 *                               <FITS File n>
01595 *                               END_OF_BATCH
01596 *
01597 * History:      
01598 * 14-Jun-04     (csabet) created
01599 ******************************************************************************/
01600 void getRefPixBatch (
01601     ImageFormat *format,                // In: Format structure
01602     FILE                *inFitsTempPtr, // In: Pointer to the main buffer
01603     FILE                *inFitsListPtr, // In: Pointer to the list of FITS files
01604         char            *fileNameStr,   // In: Name of the first FITS file
01605         char            *dummyClassif,  // In: Dummy classification
01606         FILE            *filePtr,               // In: Pointer to the log file
01607         int                     *localBatchNum, // IO: Local batch number
01608         int                     *fileNumInList, // IO: pointer to file number
01609     int                 *error)                 // Ou: Error status
01610 {
01611 
01612     //  Local Declarations
01613     //  ------------------
01614     const char  routine[] = "getRefPixBatch";
01615         char            *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
01616         FILE            *inFitsBatchTempPtr=NULL;
01617         int                     numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
01618                                 localFileNumInList;
01619         long            inFitsListPosition;
01620         
01621     //  Algorithm
01622     //  ---------
01623     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01624     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
01625 
01626         //      Reset status
01627         *error = 0;
01628         numOfDataFiles = 0;
01629         numOfEmptyFiles = 0;
01630 
01631         //      Allocate memory
01632     inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01633     localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01634     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01635 
01636         //      Open a batch temporary file for writing
01637         sprintf (inFitsBatchTemp, "localBuffer.log");
01638         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
01639         {
01640                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01641                 *error = 1;
01642                 free (inFitsBatchTemp);
01643                 free (localTplStart);
01644                 free (stringTemp);
01645                 return;
01646         }
01647                                 
01648         //      Save the first file and its parameters
01649         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01650         fileNumInTemplate = format->fileNumInTemplate;
01651         sscanf (format->tplStart, "%s", localTplStart);
01652         numOfDataFiles++;
01653         localFileNumInList = *fileNumInList;
01654         inFitsListPosition = ftell (inFitsListPtr);
01655         sprintf (batchTemplate, "%s", "REFPIX");
01656 
01657         //      Get the rest of the batch files from the list
01658         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
01659         {
01660                 sprintf (dummyClassif, "%s", "");
01661                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01662 
01663                 localFileNumInList++;
01664                 fileNumInTemplate++;
01665 
01666                 //      Get FITS file parameters. If ok copy into the batch temporary file
01667                 getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
01668 
01669                 //      We expect possibly more split files
01670                 if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01671                         (strcmp (format->obsTech, "IMAGE") == 0) &&
01672                         (strcmp (format->obsType, "FMTCHCK") == 0) &&
01673                         (strcmp (format->tplStart, localTplStart) == 0) &&
01674                         (format->fileNumInTemplate == fileNumInTemplate))
01675                 {
01676                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01677                         numOfDataFiles++;
01678                 }
01679                 else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01680                         (strcmp (format->obsTech, "OTHER") == 0) &&
01681                         (strcmp (format->obsType, "OTHER") == 0) &&
01682                         (strcmp (format->tplStart, localTplStart) == 0) &&
01683                         (format->fileNumInTemplate == fileNumInTemplate) && 
01684                         (numOfEmptyFiles < 1))
01685                 {
01686                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01687                         numOfEmptyFiles++;
01688                         break;
01689                 }
01690                 else
01691                 {
01692                         //      We have not found a complete set. Hence reset the file position
01693                         fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
01694                         localFileNumInList--;
01695                         break;
01696                 }
01697                 inFitsListPosition = ftell (inFitsListPtr);
01698         }
01699 
01700         //      If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
01701         if (!endOfTemplate) numOfEmptyFiles = 1;
01702 
01703         //      We expect at least one data file and one empty file. If there has been 
01704         //      a complete set of files then copy all files into the main temporary file
01705         if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
01706         {
01707                 //      Reopen the batch temporary file for reading
01708                 if (inFitsBatchTempPtr) 
01709                 {
01710                         fclose (inFitsBatchTempPtr);
01711                         inFitsBatchTempPtr = NULL;
01712                 }
01713                 if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
01714                 {
01715                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01716                         *error = 1;
01717                         free (inFitsBatchTemp);
01718                         free (localTplStart);
01719                         free (stringTemp);
01720                         return;
01721                 }
01722 
01723                 (*localBatchNum)++;
01724                 cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01725                 fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
01726                 fprintf (inFitsTempPtr, "%s \n", batchTemplate);
01727                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
01728                 {
01729                         sprintf (dummyClassif, "%s", "");
01730                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01731                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
01732                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
01733                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01734                 }
01735                 cpl_msg_info(cpl_func,"\n");
01736                 fprintf (filePtr, "\n");
01737         
01738                 //      We should now have a complete set of files
01739                 //      in the temporary file list. Add an appending keyword
01740                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
01741         }
01742         *fileNumInList = localFileNumInList;
01743         if (inFitsBatchTempPtr) 
01744         {
01745                 fclose (inFitsBatchTempPtr);
01746                 inFitsBatchTempPtr = NULL;
01747         }
01748         remove (inFitsBatchTemp);
01749 
01750     //  Release memory
01751     if (inFitsBatchTemp) free (inFitsBatchTemp);
01752         if (localTplStart) free (localTplStart);
01753         if (stringTemp) free (stringTemp);
01754 
01755         return;
01756 }
01757 /*****************************************************************************/
01758 
01759 
01760 /******************************************************************************
01761 *               European Southern Observatory
01762 *          VLTI MIDI Maintenance Templates Software
01763 *
01764 * Module name:  getWavCalBatch
01765 * Input/Output: See function arguments to avoid duplication
01766 * Description:  It loops through the given list of FITS files and searches for files
01767 *                               that relate to the Wavelength Calibration template. It then creates
01768 *                               a batch file with the following format:
01769 *                               
01770 *                               <TEMPLATE_NAME> e.g. MIDI AUTOTEST TEC WAVECAL
01771 *                               <FITS File 1>
01772 *                               <FITS File 2>
01773 *                               <.>
01774 *                               <.> 
01775 *                               <FITS File n>
01776 *                               END_OF_BATCH
01777 *
01778 * History:      
01779 * 02-Jun-05     (csabet) created
01780 ******************************************************************************/
01781 void getWavCalBatch (
01782     ImageFormat *format,                // In: Format structure
01783     FILE                *inFitsTempPtr, // In: Pointer to the main buffer
01784     FILE                *inFitsListPtr, // In: Pointer to the list of FITS files
01785         char            *fileNameStr,   // In: Name of the first FITS file
01786         char            *dummyClassif,  // In: Dummy classification
01787         FILE            *filePtr,               // In: Pointer to the log file
01788         int                     *localBatchNum, // IO: Local batch number
01789         int                     *fileNumInList, // IO: pointer to file number
01790     int                 *error)                 // Ou: Error status
01791 {
01792 
01793     //  Local Declarations
01794     //  ------------------
01795     const char  routine[] = "getWavCalBatch";
01796         char            *inFitsBatchTemp=NULL, *localTplStart=NULL, *stringTemp=NULL;
01797         FILE            *inFitsBatchTempPtr=NULL;
01798         int                     numOfDataFiles, fileNumInTemplate, numOfEmptyFiles,
01799                                 localFileNumInList;
01800         long            inFitsListPosition;
01801         
01802     //  Algorithm
01803     //  ---------
01804     if (diagnostic > 4) cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01805     if (diagnostic > 4) fprintf(filePtr, "Invoking      routine   '%s' \n", routine);
01806 
01807         //      Reset status
01808         *error = 0;
01809         numOfDataFiles = 0;
01810         numOfEmptyFiles = 0;
01811 
01812         //      Allocate memory
01813     inFitsBatchTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01814     localTplStart = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01815     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01816 
01817         //      Open a batch temporary file for writing
01818         sprintf (inFitsBatchTemp, "localBuffer.log");
01819         if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "w")) == NULL)
01820         {
01821                 midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01822                 *error = 1;
01823                 free (inFitsBatchTemp);
01824                 free (localTplStart);
01825                 free (stringTemp);
01826                 return;
01827         }
01828                                 
01829         //      Save the first file and its parameters
01830         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01831         fileNumInTemplate = format->fileNumInTemplate;
01832         sscanf (format->tplStart, "%s", localTplStart);
01833         numOfDataFiles++;
01834         localFileNumInList = *fileNumInList;
01835         inFitsListPosition = ftell (inFitsListPtr);
01836         sprintf (batchTemplate, "%s", "WAVECAL");
01837 
01838         //      Get the rest of the batch files from the list
01839         while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsListPtr) != NULL)
01840         {
01841                 sprintf (dummyClassif, "%s", "");
01842                 sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01843 
01844                 localFileNumInList++;
01845                 fileNumInTemplate++;
01846 
01847                 //      Get FITS file parameters. If ok copy into the batch temporary file
01848                 getFitsClassification (localFileNumInList, filePtr, fileNameStr, format, error);
01849 
01850                 //      We expect possibly more split files
01851                 if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01852                         (strcmp (format->obsTech, "SPECTRUM") == 0) &&
01853                         (strcmp (format->obsType, "WAVE,SPECTEMPL") == 0) &&
01854                         (strcmp (format->tplStart, localTplStart) == 0) &&
01855                         (format->fileNumInTemplate == fileNumInTemplate))
01856                 {
01857                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01858                         numOfDataFiles++;
01859                 }
01860                 else if (!(*error) && (strcmp (format->obsCatg, "CALIB") == 0) &&
01861                         (strcmp (format->obsTech, "OTHER") == 0) &&
01862                         (strcmp (format->obsType, "OTHER") == 0) &&
01863                         (strcmp (format->tplStart, localTplStart) == 0) &&
01864                         (format->fileNumInTemplate == fileNumInTemplate) && 
01865                         (numOfEmptyFiles < 1))
01866                 {
01867                         fprintf (inFitsBatchTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01868                         numOfEmptyFiles++;
01869                         break;
01870                 }
01871                 else
01872                 {
01873                         //      We have not found a complete set. Hence reset the file position
01874                         fseek (inFitsListPtr, inFitsListPosition, SEEK_SET);
01875                         localFileNumInList--;
01876                         break;
01877                 }
01878                 inFitsListPosition = ftell (inFitsListPtr);
01879         }
01880 
01881         //      If END_OF_TEMPLATE is deactivated then we do not check the numOfEmptyFiles
01882         if (!endOfTemplate) numOfEmptyFiles = 1;
01883 
01884         //      We expect at least one data file and one empty file. If there has been 
01885         //      a complete set of files then copy all files into the main temporary file
01886         if ((numOfDataFiles > 0) && (numOfEmptyFiles == 1))
01887         {
01888                 //      Reopen the batch temporary file for reading
01889                 if (inFitsBatchTempPtr) 
01890                 {
01891                         fclose (inFitsBatchTempPtr);
01892                         inFitsBatchTempPtr = NULL;
01893                 }
01894                 if ((inFitsBatchTempPtr = fopen (inFitsBatchTemp, "r")) == NULL)
01895                 {
01896                         midiReportWarning (filePtr, routine, __FILE__, __LINE__, "Cannot open a batch temporary file");
01897                         *error = 1;
01898                         free (inFitsBatchTemp);
01899                         free (localTplStart);
01900                         free (stringTemp);
01901                         return;
01902                 }
01903 
01904                 (*localBatchNum)++;
01905                 cpl_msg_info(cpl_func,"\nSelected batch  %d  is  %s:\n", (*localBatchNum), batchTemplate);
01906                 fprintf (filePtr, "\nSelected batch  %d:  is  %s\n", (*localBatchNum), batchTemplate);
01907                 fprintf (inFitsTempPtr, "%s \n", batchTemplate);
01908                 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchTempPtr) != NULL)
01909                 {
01910                         sprintf (dummyClassif, "%s", "");
01911                         sscanf (stringTemp, "%s%s", fileNameStr, dummyClassif);
01912                         cpl_msg_info(cpl_func,"%s %s\n", fileNameStr, dummyClassif);
01913                         fprintf (filePtr, "%s %s\n", fileNameStr, dummyClassif);
01914                         fprintf (inFitsTempPtr, "%s %s\n", fileNameStr, dummyClassif);
01915                 }
01916                 cpl_msg_info(cpl_func,"\n");
01917                 fprintf (filePtr, "\n");
01918         
01919                 //      We should now have a complete set of files
01920                 //      in the temporary file list. Add an appending keyword
01921                 fprintf (inFitsTempPtr, "%s\n", "END_OF_BATCH");
01922         }
01923         *fileNumInList = localFileNumInList;
01924         if (inFitsBatchTempPtr) 
01925         {
01926                 fclose (inFitsBatchTempPtr);
01927                 inFitsBatchTempPtr = NULL;
01928         }
01929         remove (inFitsBatchTemp);
01930 
01931     //  Release memory
01932     if (inFitsBatchTemp) free (inFitsBatchTemp);
01933         if (localTplStart) free (localTplStart);
01934         if (stringTemp) free (stringTemp);
01935 
01936         return;
01937 }
01938 /*****************************************************************************/

Generated on 11 Feb 2011 for MIDI Pipeline Reference Manual by  doxygen 1.6.1