fitsAnalysisKappa.c

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

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