fitsAnalysisFrgSP.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *          VLTI MIDI Maintenance Templates Software
00005 *
00006 * Module name:  fitsAnalysisFrgSP.c
00007 * Description:  Contains all the routines for FITS format analysis related to
00008 *                Kappa computation
00009 *
00010 * History:      
00011 * 05-Dec-05     (csabet) created
00012 *******************************************************************************
00013 ******************************************************************************/
00014 
00015 /******************************************************************************
00016 *   Compiler directives
00017 ******************************************************************************/
00018 
00019 /******************************************************************************
00020 *   Include files
00021 ******************************************************************************/
00022 #include <stdio.h>
00023 #include <cpl.h>
00024 #include <math.h>
00025 #include "midiGlobal.h"
00026 #include "midiLib.h"
00027 #include "fft.h"
00028 #include "errorHandling.h"
00029 #include "memoryHandling.h"
00030 #include "midiFitsUtility.h"
00031 #include "fitsAnalysisFrgSP.h"
00032 
00033 /**********************************************************
00034 *   Global Variables 
00035 **********************************************************/
00036 
00037 
00038 
00039 /*============================ C O D E    A R E A ===========================*/
00040 
00041 
00042 
00043 
00044 /******************************************************************************
00045 *               European Southern Observatory
00046 *             VLTI MIDI Data Reduction Software
00047 *
00048 * Module name:  analyseFitsFrgSP
00049 * Input/Output: See function arguments to avoid duplication
00050 * Description:  Analyses all the FITS files in the given list. This routine
00051 *                determines if the FITS list contains useful data files. If
00052 *                not it will discard the list.
00053 *
00054 * History:      
00055 * 05-Dec-05     (csabet) Created
00056 ******************************************************************************/
00057 void analyseFitsFrgSP (
00058     MidiFiles    *fileNames,            // IO: Pointer to midi files structure
00059     ImageFormat    *formatInterf,        // Ou: Size for Interferometry file
00060     ImageFormat    *formatPhotomA,        // Ou: Size for Photometry A file
00061     ImageFormat    *formatPhotomB,        // Ou: Size for Photometry B file
00062     int            *error)                // Ou: Error status
00063 {
00064 
00065     //    Local Declarations
00066     //    ------------------
00067     const char            routine[] = "analyseFitsFrgSP";
00068     char                *fileTemp, *classification;
00069     FILE                *inFitsBatchPtr=NULL;
00070     int                    extNumOfImagingDataFile, aprioriDataIsDisp=0;
00071     ImageFormat            *localFormat=NULL;
00072     char                *cleanString=NULL, *aprioriCatg=NULL;
00073     unsigned int        numOfInterf;
00074         
00075     //    Algorithm
00076     //    ---------
00077     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00078     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00079 
00080    cpl_msg_info(cpl_func,"\nAnalysing batch  %d \n", batchNumber);
00081    cpl_msg_info(cpl_func,"--------------- \n");
00082     fprintf (midiReportPtr, "\nAnalysing batch  %d \n", batchNumber);
00083     fprintf (midiReportPtr, "--------------- \n");
00084 
00085     //    Reset status
00086     *error = 0;
00087     
00088     //    Allocate memory for a temporary file name buffer
00089     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00090     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00091     fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00092     aprioriCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00093     localFormat = callocImageFormat ();
00094     
00095     //    Open the list of files
00096     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00097     {
00098         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00099         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "No analysis has been carried out for this batch");
00100         free (fileTemp);
00101         free (cleanString);
00102         freeImageFormat (localFormat);
00103         free (classification);
00104         *error = 1;
00105         return;
00106     }
00107 
00108     //    Reset image format
00109     numOfInterf = 0;
00110     midiInitFormat (formatInterf);
00111 
00112     //    Loop through the files and analyse
00113     while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00114     {
00115         sprintf (classification, "%s", "");
00116         sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
00117         if (diagnostic)cpl_msg_info(cpl_func,"\nAnalysing file   %s \n", fileNames->inFitsName);
00118         fprintf(midiReportPtr, "\nAnalysing file   %s \n", fileNames->inFitsName);
00119 
00120         //    Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
00121         extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
00122         if (*error) break;
00123 
00124         //    Get Image Format parameters
00125         if (extNumOfImagingDataFile > 0)
00126         {
00127             getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
00128             if (*error) break;
00129         }
00130         else localFormat->hasData = 0;
00131 
00132         //    Check if the file has data
00133         if (localFormat->hasData)
00134         {
00135             //    Save initial Beam Combiner and Category
00136             if (numOfInterf == 0)
00137             {
00138                 aprioriDataIsDisp = localFormat->isDisp;
00139                 sprintf (aprioriCatg, "%s", localFormat->obsCatg);
00140             }
00141             
00142             //    Check Beam Combiner consistency
00143             if (localFormat->isDisp != aprioriDataIsDisp)
00144             {
00145                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent Beam Combiner");
00146                 *error = 1;
00147                 break;
00148             }
00149             else
00150                 aprioriDataIsDisp = localFormat->isDisp;
00151 
00152             //    Check Category consistency
00153             if (strcmp (localFormat->obsCatg, aprioriCatg) != 0)
00154             {
00155                 sprintf (midiMessage, "Inconsistent Observation Category. Expected: %s", aprioriCatg);
00156                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00157                 *error = 1;
00158                 break;
00159             }
00160             else
00161                 sprintf (aprioriCatg, "%s", localFormat->obsCatg);
00162 
00163             //    Check Tech and Shutter ID
00164             if (strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0)
00165             {
00166                 //    Save local parameters
00167                 if (numOfInterf == 0)
00168                 {
00169                     formatInterf->hasData = 1;
00170                     formatInterf->chopped = 1;
00171                     midiCopyFormat (localFormat, formatInterf);
00172 
00173                     formatPhotomA->hasData = 1;
00174                     formatPhotomA->chopped = 1;
00175                     midiCopyFormat (localFormat, formatPhotomA);
00176                     formatPhotomA->numOfRegionsToProcess = 1;
00177 
00178                     formatPhotomB->hasData = 1;
00179                     formatPhotomB->chopped = 1;
00180                     midiCopyFormat (localFormat, formatPhotomB);
00181                     formatPhotomB->numOfRegionsToProcess = 1;
00182                 }
00183 
00184                 //    Get cummulative results
00185                 formatInterf->numOfFrames += localFormat->numOfFrames;
00186                 formatPhotomA->numOfFrames += localFormat->numOfFrames;
00187                 formatPhotomB->numOfFrames += localFormat->numOfFrames;
00188 
00189                 //    Increment file counter
00190                 ++numOfInterf;
00191             }
00192             else if (strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0)
00193             {
00194                 if (diagnostic > 1)    midiReportInfo (midiReportPtr, 
00195                     routine, __FILE__, __LINE__, "The above Photometry file will not be processed");
00196             }
00197             else
00198             {
00199                 sprintf (midiMessage, "Unknown Category, Tech or Type in ... %s. Not processed", 
00200                     fileNames->inFitsName);
00201                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00202             }
00203         }
00204         else
00205         {
00206             if (diagnostic)
00207             {
00208                 sprintf (midiMessage, "No data tables in ... %s. Not processed", fileNames->inFitsName);
00209                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00210             }
00211         }
00212     }
00213 
00214     //    Close the file list
00215     if (fclose (inFitsBatchPtr))
00216         midiReportError (midiReportPtr, routine, __FILE__, __LINE__, "Cannot close batch file");
00217     
00218     //    Check if the FITS files contained useful data
00219     if (!(*error) && (numOfInterf > 0))
00220     {
00221         //    Check potential singularities
00222         if ((formatInterf->framesPerScan == 0) || (formatPhotomA->framesPerScan == 0) || 
00223             (formatPhotomB->framesPerScan == 0))
00224         {
00225             if (fileTemp) free (fileTemp);
00226             if (localFormat) freeImageFormat (localFormat);
00227             if (cleanString) free (cleanString);
00228             if (classification) free (classification);
00229             if (aprioriCatg) free (aprioriCatg);
00230             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Potential singularity detected");
00231             *error = 1;
00232             return;
00233         }
00234 
00235         //    Compute number of scans
00236         formatInterf->numOfScans = formatInterf->numOfFrames / formatInterf->framesPerScan;
00237         formatPhotomA->numOfScans = formatPhotomA->numOfFrames / formatPhotomA->framesPerScan;
00238         formatPhotomB->numOfScans = formatPhotomB->numOfFrames / formatPhotomB->framesPerScan;
00239 
00240         //    Make sure numberOfFrames is a muliple number of the framesPerScan
00241         formatInterf->numOfFrames = formatInterf->numOfScans * formatInterf->framesPerScan;
00242         formatPhotomA->numOfFrames = formatPhotomA->numOfScans * formatPhotomA->framesPerScan;
00243         formatPhotomB->numOfFrames = formatPhotomB->numOfScans * formatPhotomB->framesPerScan;
00244 
00245         //    Compute default FFT size
00246         formatInterf->fftsize = determineFFTsize (formatInterf->framesPerScan);
00247 
00248         //    Compute length of allSpectrum
00249         formatInterf->allSpectrumLength = REGIONS_UNDISPERSED * formatInterf->numOfScans * (formatInterf->fftsize/2);
00250         
00251         //    Log results
00252         midiReportFormat ("INTERFEROMETRY", aprioriDataIsDisp, formatInterf);
00253         midiReportFormat ("PHOTOMETRY A", aprioriDataIsDisp, formatPhotomA);
00254         midiReportFormat ("PHOTOMETRY B", aprioriDataIsDisp, formatPhotomB);
00255 
00256         sprintf (midiMessage, "Detected %d Interferometry files", numOfInterf);
00257         midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00258     }
00259     else
00260     {
00261         sprintf (fileTemp, "%d", batchNumber);
00262         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent batch files");
00263         *error = 1;
00264     }
00265  
00266     //    Check if requested processing is valid. For now just report warning.
00267     if (!(*error))
00268     {
00269         if (!aprioriDataIsDisp)
00270         {
00271             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00272                 "Data is UNDISPERSED\n                 Algorithm for processing this type of data is not ready");
00273             *error = 1;
00274         }
00275     }
00276 
00277     //    Release memory
00278     if (fileTemp) free (fileTemp);
00279     if (localFormat) freeImageFormat (localFormat);
00280     if (cleanString) free (cleanString);
00281     if (classification) free (classification);
00282     if (aprioriCatg) free (aprioriCatg);
00283      
00284     return;
00285 }
00286 /******************************************************************************/
00287 

Generated on 15 Mar 2012 for MIDI Pipeline Reference Manual by  doxygen 1.6.1