preProcFrgSP.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  preProcFrgSP.c
00007 * Description:  Contains routines for pre processing
00008 *
00009 * History:
00010 * 08-Aug-05     (csabet) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Compiler directives
00016 ******************************************************************************/
00017 
00018 /******************************************************************************
00019 *   Include files
00020 ******************************************************************************/
00021 #include <stdio.h>
00022 #include <cpl.h>
00023 #include <math.h>
00024 #include "midiGlobal.h"
00025 #include "fft.h"
00026 #include "midiLib.h"
00027 #include "memoryHandling.h"
00028 #include "errorHandling.h"
00029 #include "midiFitsUtility.h"
00030 #include "diagnostics.h"
00031 #include "preProcFrgSP.h"
00032 
00033 /**********************************************************
00034 *   Constant definitions
00035 **********************************************************/
00036 
00037 /**********************************************************
00038 *   Global Variables
00039 **********************************************************/
00040 
00041 /*============================ C O D E    A R E A ===========================*/
00042 
00043 
00044 
00045 /******************************************************************************
00046 *               European Southern Observatory
00047 *            VLTI MIDI Data Reduction Software
00048 *
00049 * Module name:  preProcFrgSP
00050 * Input/Output: See function arguments to avoid duplication
00051 * Description:  Prepares the data for DISPERSED mode of processing. 
00052 *                This is configured for SCI_PHOT
00053 *
00054 * History:
00055 * 07-Jul-05     (csabet) Created
00056 ******************************************************************************/
00057 void preProcFrgSP (
00058     UserOptions        *options,                // In: User parameters
00059     FilterData        *filterInfo,            // IO: Pointer to the filter data structure
00060     MidiFiles        *fileNames,                // In: Pointer to the MIDI file structure
00061     CompressedData    *compressedInterf,        // Ou: Pointer to the compressed interferometry data structure
00062     CompressedData    *compressedPhotomA,        // Ou: Pointer to the compressed photom A data structure
00063     CompressedData    *compressedPhotomB,        // Ou: Pointer to the compressed photom B data structure
00064     ImageFormat        *formatInterf,            // In: Interf size parameters
00065     ImageFormat        *formatPhotomA,            // In: PhotA size parameters
00066     ImageFormat        *formatPhotomB,            // In: PhotB size parameters
00067     int                *error)                    // Ou: Error status
00068 {
00069 
00070     //    Local Declarations
00071     //    ------------------
00072     const char            routine[] = "preProcFrgSP";
00073     FILE                *inFitsBatchPtr=NULL;
00074     int                    newFile, extNumOfImagingDataFile, extNumOfImagingDataMask, R;
00075     char                *maskFile, *cleanString, *classification, *stringTemp, *fileName, *title;
00076     unsigned int        loopCount = 0;
00077     ImageFormat            *localFormat;
00078     enum ObsTechnique    obsTech;
00079 
00080     //    Algorithm
00081     //    ---------
00082     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00083     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00084 
00085    cpl_msg_info(cpl_func,"\nReducing data of batch  %d \n", batchNumber);
00086    cpl_msg_info(cpl_func,"---------------------- \n");
00087     fprintf (midiReportPtr, "\nReducing data of batch  %d \n", batchNumber);
00088     fprintf (midiReportPtr, "---------------------- \n");
00089 
00090     //    Reset status
00091     *error = 0;
00092     compressedInterf->exists = 0;
00093     compressedPhotomA->exists = 0;
00094     compressedPhotomB->exists = 0;
00095     newFile = 1;
00096     obsTech = UNKNOWN;
00097 
00098     //    Allocate memory
00099     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00100     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00101     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00102     localFormat = callocImageFormat ();
00103     maskFile = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00104 
00105     //    Open the list of files
00106     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00107     {
00108         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00109             "Cannot open input FITS file list. No compression has been carried out for this batch");
00110         free (cleanString);
00111         free (localFormat);
00112         free (stringTemp);
00113         free (classification);
00114         freeImageFormat (localFormat);
00115         *error = 1;
00116         return;
00117     }
00118 
00119     //  Loop through the list of files and compress data
00120     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00121     {
00122         sprintf (classification, "%s", "");
00123         sscanf (stringTemp, "%s%s", fileNames->inFitsName, classification);
00124        cpl_msg_info(cpl_func,"\nProcessing file   %s \n", fileNames->inFitsName);
00125         fprintf (midiReportPtr, "\nProcessing file   %s \n", fileNames->inFitsName);
00126 
00127         //    Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
00128         extNumOfImagingDataFile  = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
00129         if (*error) break;
00130 
00131         //    Get Image Format parameters
00132         if (extNumOfImagingDataFile > 0)
00133         {
00134             getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
00135             if (*error) break;
00136         }
00137         else localFormat->hasData = 0;
00138 
00139         //    Get filter data and mask file name from the first FITS file
00140         if (loopCount == 0)
00141         {
00142             getFilterData (fileNames->inFitsName, filterInfo, error);
00143             if (*error) break;
00144             else fprintf (midiReportPtr, "First file of batch = %s (QCLOG)\n", fileNames->inFitsName);
00145 
00146             //    Select mask file
00147             selectMask (options->maskMode, fileNames, maskFile, error);
00148             if (*error) break;
00149 
00150             //    Get 'extNumOfImagingDataMask' extension number of IMAGING_DATA in mask file
00151             extNumOfImagingDataMask = findImagingDataExtension (maskFile, MASK_IMAGING_DATA, error);
00152             if (*error) break;
00153 
00154            cpl_msg_info(cpl_func,"Mask File is   %s\n", maskFile);
00155             fprintf( midiReportPtr, "Mask File is   %s\n", maskFile);
00156         }
00157 
00158         //    Check if the file has data
00159         if (localFormat->hasData)
00160         {
00161             //    Check file Category and Type
00162             if (strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0)
00163             {
00164                 //    Check previous TYPE
00165                 if (obsTech != INTERF) newFile = 1;
00166                 
00167                 //    Set the TYPE flag
00168                 obsTech = INTERF;
00169                 
00170                 //    Check consistency
00171                 if ((formatInterf->numOfDetectorRegions != localFormat->numOfDetectorRegions) ||
00172                     (formatInterf->iXWidth != localFormat->iXWidth) || (formatInterf->iYWidth != localFormat->iYWidth))
00173                 {
00174                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Expected format is incorrect");
00175                     *error = 1;
00176                     break;
00177                 }
00178                 
00179                 //    The Interferometry files contribute to the computation of the visibilities
00180                 organiseFrgSP (newFile, extNumOfImagingDataFile, fileNames, extNumOfImagingDataMask, 
00181                     maskFile, localFormat, formatInterf, compressedInterf, compressedPhotomA, 
00182                     compressedPhotomB, error);
00183 
00184                 if (*error)    break;
00185                 newFile = 0;    // So the next time through, will know not to start at frame 0
00186             }
00187             else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00188                 (strcmp(localFormat->shutterId,"AOPEN") == 0))
00189             {
00190                 if (diagnostic > 1)    midiReportInfo (midiReportPtr, 
00191                     routine, __FILE__, __LINE__, "The above Photometry file will not be processed");
00192                 obsTech = PHOTOMA;
00193             }
00194             else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00195                 (strcmp(localFormat->shutterId,"BOPEN") == 0))
00196             {
00197                 if (diagnostic > 1)    midiReportInfo (midiReportPtr, 
00198                     routine, __FILE__, __LINE__, "The above Photometry file will not be processed");
00199                 obsTech = PHOTOMB;
00200             }
00201             else
00202             {
00203                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Catg, Type or Tech");
00204                 *error = 1;
00205                 break;
00206             }
00207         }
00208         else
00209         {
00210             sprintf (midiMessage, "No data tables in %s. Not processed", fileNames->inFitsName);
00211             midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00212         }
00213         loopCount++;
00214     }
00215 
00216     //    Close files and release memory
00217     fclose (inFitsBatchPtr);
00218     free (cleanString);
00219     free (stringTemp);
00220     free (classification);
00221     freeImageFormat (localFormat);
00222     free (maskFile);
00223 
00224     //    Check if reduction has been carried out
00225    cpl_msg_info(cpl_func,"\nCompression status \n");
00226    cpl_msg_info(cpl_func,"------------------ \n");
00227     if (compressedInterf->exists)cpl_msg_info(cpl_func,"Created Compressed Interferometry Data\n");
00228     if (compressedPhotomA->exists)cpl_msg_info(cpl_func,"Created Compressed Photometry A Data\n");
00229     if (compressedPhotomB->exists)cpl_msg_info(cpl_func,"Created Compressed Photometry B Data\n");
00230    cpl_msg_info(cpl_func,"\n");
00231 
00232     fprintf (midiReportPtr, "\nCompression status \n");
00233     fprintf (midiReportPtr, "------------------ \n");
00234     if (compressedInterf->exists) fprintf (midiReportPtr, "Created Compressed Interferometry Data\n");
00235     if (compressedPhotomA->exists) fprintf (midiReportPtr, "Created Compressed Photometry A Data\n");
00236     if (compressedPhotomB->exists) fprintf (midiReportPtr, "Created Compressed Photometry B Data\n");
00237     fprintf (midiReportPtr, "\n");
00238 
00239     if (!(compressedInterf->exists) || 
00240         !(compressedPhotomA->exists) || !(compressedPhotomB->exists) || *error)
00241     {
00242         *error = 1;
00243         sprintf (midiMessage, 
00244             "Cannot continue. Need the following compressed data: Interf, PhotomA, PhotomB");
00245         midiReportWarning (midiReportPtr, 
00246             routine, __FILE__, __LINE__, midiMessage);
00247 
00248         return;
00249     }
00250 
00251     //    Display images
00252     if (diagnostic > 1 && plotFile)
00253     {
00254         fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00255         title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00256 
00257         for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00258         {
00259             sprintf (fileName, "3dInterfDATA%d", R+2);
00260             sprintf (title, "Interferometry DATA %d (Masked and Sky removed)", R+2);
00261             midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0, 
00262                 compressedInterf->image[R], formatInterf->iXWidth, formatInterf->iYWidth, "lines", "3");
00263         }
00264 
00265         for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00266         {
00267             sprintf (fileName, "3dPhotomADATA%d", R+1);
00268             sprintf (title, "Photometry A DATA %d (Masked and Sky removed)", R+1);
00269             midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0, 
00270                 compressedPhotomA->image[R], formatPhotomA->iXWidth, formatPhotomA->iYWidth, "lines", "3");
00271         }
00272 
00273         for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00274         {
00275             sprintf (fileName, "3dPhotomBDATA%d", R+4);
00276             sprintf (title, "Photometry B DATA %d (Masked and Sky removed)", R+4);
00277             midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0, 
00278                 compressedPhotomB->image[R], formatPhotomB->iXWidth, formatPhotomB->iYWidth, "lines", "3");
00279         }
00280 
00281         free (fileName);
00282         free (title);
00283     }
00284 
00285     return;
00286 }
00287 /*****************************************************************************/
00288 
00289 
00290 
00291 
00292 /******************************************************************************
00293 *               European Southern Observatory
00294 *            VLTI MIDI Data Reduction Software
00295 *
00296 * Module name:  organiseFrgSP
00297 * Input/Output: See function arguments to avoid duplication
00298 * Description:  Prepares SCI_PHOT files for compression
00299 *
00300 * History:
00301 * 03-Nov-05     (csabet) Created
00302 ******************************************************************************/
00303 void organiseFrgSP (
00304     int                    newFile,                    // In: Flag indicating continuation
00305     int                    extNumOfImagingDataFile,    // In: Extention number
00306     MidiFiles            *fileNames,                    // In: Pointer to the MIDI file structure
00307     int                    extNumOfImagingDataMask,    // In: Extention number
00308     char                *maskFile,                    // In: Mask file
00309     ImageFormat            *localFormat,                // In: Local format size
00310     ImageFormat            *formatInterf,                // In: Interf size parameters
00311     CompressedData        *compressedInterf,            // Ou: Pointer to the compressed interferometry data structure
00312     CompressedData        *compressedPhotomA,            // Ou: Pointer to the compressed photom A data structure
00313     CompressedData        *compressedPhotomB,            // Ou: Pointer to the compressed photom B data structure
00314     int                    *error)                        // Ou: Error status
00315 {
00316 
00317     //    Local Declarations
00318     //    ------------------
00319     const char        routine[] = "organiseFrgSP";
00320     int                frame0;
00321 
00322     //    Algorithm
00323     //    ---------
00324     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00325     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00326 
00327     //    Initialise
00328     *error = 0;
00329 
00330     //    Compress data
00331     frame0 = compressFrgSP (fileNames->inFitsName, maskFile, extNumOfImagingDataFile, 
00332         extNumOfImagingDataMask, compressedInterf, compressedPhotomA, compressedPhotomB, 
00333         newFile, localFormat, formatInterf->numOfFrames, error);
00334     if (*error) return;
00335 
00336     //    Report compression status
00337     if (diagnostic)
00338     {
00339         sprintf (midiMessage, "\nWrote %d frames into Interf and Photom, starting at frame %d\nThus now %d frames total,"
00340             " expecting %d altogether.  Error=%d \n\n", localFormat->numOfFrames, frame0, 
00341             localFormat->numOfFrames+frame0, formatInterf->numOfFrames, *error);
00342         midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00343     }
00344     
00345     //    Set the flags
00346     if ((frame0 + localFormat->numOfFrames) == formatInterf->numOfFrames)  // ALL have been read in
00347     {
00348         compressedInterf->exists = 1;    // Mark it as completed
00349         compressedPhotomA->exists = 1;    // Mark it as completed
00350         compressedPhotomB->exists = 1;    // Mark it as completed
00351 
00352         //    Now with data read in, display some of the parameters:
00353         if (plotFile && diagnostic)
00354         {
00355             midiCreatePlotFile2D ("Time", "TIME", "Frame", "Time", 0, compressedInterf->time, 0, 
00356                 formatInterf->numOfFrames, 1, 0);
00357             midiCreatePlotFile2D ("BigDelayLine", "Net VLTI Delay Line Positions", "Frame", 
00358                 "Net Position", 0, compressedInterf->bigDL, 0, formatInterf->numOfFrames, 1, 0);
00359             midiCreatePlotFile2D ("PiezoDelayLine", "Net Piezo Delay Line Position", "Frame", "Net Delay", 0, 
00360                 compressedInterf->localOPD, 0, formatInterf->numOfFrames, 1, 0);
00361         }
00362     }
00363                     
00364     return;
00365 }
00366 /*****************************************************************************/
00367 
00368 
00369 
00370 /******************************************************************************
00371 *               European Southern Observatory
00372 *            VLTI MIDI Data Reduction Software
00373 *
00374 * Module name:    compressFrgSP
00375 * Input/Output:    See function arguments to avoid duplication
00376 * Description:    This routine prepares the interferomerty data for dispersed
00377 *               processing. In DISPERSED mode iDispFringe points to a 3D structure. 
00378 *                One dimension is time (frame), the second dimension is wave (produced 
00379 *                by collapsing each X, Y image into one line) and the third dimension 
00380 *                is the number of sub-regions. This routine is used for
00381 *                SCI_PHOT templates
00382 *
00383 *                Note:
00384 *                DATA1 = PHOTOM A
00385 *                DATA2 = INTERF
00386 *                DATA3 = INTERF
00387 *                DATA4 = PHOTOM B
00388 *
00389 *                All 4 data are chopped
00390 *
00391 * History:
00392 * 07-Jul-05        (csabet) Adapted from HS. This module is far too big. Will modularise later
00393 ******************************************************************************/
00394 int compressFrgSP (                            // Ou: Status. Returns number of first frame compressed; -1 if error
00395     char            *inFitsFile,            // In: Name of the input FITS file
00396     char            *maskFile,                // In: Name of the mask file
00397     int                extNumOfImagingDataFile,// In: Extension number of the IMAGING_DATA in input file
00398     int                extNumOfImagingDataMask,// In: Extension number of the IMAGING_DATA in mask file
00399     CompressedData    *compressedInterf,        // In: Compressed Interf data
00400     CompressedData    *compressedPhotomA,        // In: Compressed Photom A data
00401     CompressedData    *compressedPhotomB,        // In: Compressed Photom B data
00402     int                newSet,                    // In: Flag indicating the arrival of a new set of FITS files
00403     ImageFormat        *localFormat,            // In: Local split file format 
00404     int                numOfFramesMax,            // In: Maximum number of frames
00405     int                *error)                    // Ou: Error status
00406 {
00407 
00408     //    Local Declarations
00409     //    ------------------
00410     const char        routine[] = "compressFrgSP";
00411     qfits_table        *pTable  = NULL, *pMask = NULL;
00412     short int        **inData, *inSteppingPhase;
00413     float            **inMask, accum, current,
00414                     *normalization; // Normalization factor. Becomes an array in X for the normalization of the 
00415                                     // compressedInterf data, valid for each subRegion then overwritten
00416     char            **inTARTYP = NULL, *tempStr, fitsColumnString[10], *dataName, *title=NULL, *fileString=NULL;
00417     double            *inTIME = NULL; 
00418     int                frame0 = - 1;     // Gets set to a valid number UNLESS routine terminates in error
00419     double            (*inLOCALOPD)[2] = NULL, (*inOPD)[2] = NULL; 
00420     static int        aprioriSteppingPhase, iF, channelSelected = 0;
00421     static double    zeroTime;
00422     int                i, k, *foundData, foundSteppingPhase = 0, indexSteppingPhase, scalingOffset, *indexData, 
00423                     *indexMask, maskWidthX, maskWidthY,    maskSubWindow, F, X, Y, R, 
00424                     *indexTARTYP, *foundTARTYP, startframe=0, frameOffset, indexOPD= -1, indexLOCALOPD= -1, 
00425                     indexTIME= -1, maxstep,
00426                     tartypMult=2;    // Signifies that there are 2 (instead of 1!) characters per tartyp value (only 
00427                                     // first is useful). In future, if the problem producing the MIDI files is solved, 
00428                                     // can be changed to 1 (only for newer files) or made variable (with some way to 
00429                                     // determine whether it is an "old" type file). Note: this problem really has 
00430                                     // nothing to do with Qfits, but with the (unintended) way the files are written.
00431     int                i2, fst, snd, found;
00432     
00433     
00434     //    Algorithm
00435     //    ---------
00436     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00437     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00438 
00439     //    Reset status
00440     *error = 0;
00441     if (localFormat->numOfDetectorRegions != 4)
00442     {
00443         *error = 1;
00444         sprintf (midiMessage, "Incorrect number of regions. Expected 4, found %d", localFormat->numOfDetectorRegions);
00445         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00446         return (-1);
00447     }
00448 
00449     //    Allocate memory
00450     inData = (short int **) calloc (localFormat->numOfDetectorRegions, sizeof (short int *));
00451     inTARTYP = (char **) calloc (localFormat->numOfDetectorRegions, sizeof (char *));
00452     inMask = (float **) calloc (localFormat->numOfDetectorRegions, sizeof (float *));  // Now has each OUTPUT region allocated
00453     foundData = (int *) calloc (localFormat->numOfDetectorRegions, sizeof (int));
00454     indexData = (int *) calloc (localFormat->numOfDetectorRegions, sizeof (int));
00455     foundTARTYP = (int *) calloc (localFormat->numOfDetectorRegions, sizeof (int));
00456     indexTARTYP = (int *) calloc (localFormat->numOfDetectorRegions, sizeof (int));
00457     indexMask = (int *) calloc (localFormat->numOfDetectorRegions, sizeof (int));
00458     normalization = (float *) calloc (localFormat->iXWidth, sizeof (float));
00459     dataName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00460         
00461     //    Initialise the indices for a new set of FITS files. The
00462     //    files may be split the indices ensure continuity of split files
00463     if (newSet)
00464     {
00465         aprioriSteppingPhase = 0;
00466         iF = 0;
00467     }
00468 
00469     //    Make sure the sum of the split-files numOfFrames is not more than the allowed maximum
00470     if ((iF + localFormat->numOfFrames) > numOfFramesMax)
00471         localFormat->numOfFrames = numOfFramesMax - iF;
00472 
00473     //    Open IMAGING_DATA = INDEX 2
00474     pMask = qfits_table_open (maskFile, extNumOfImagingDataMask);
00475     if (!pMask)
00476     {
00477         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load Mask's IMAGING_DATA");
00478         free (inData);
00479         free (inTARTYP);
00480         free (inMask);
00481         free (foundData);
00482         free (indexData);
00483         free (foundTARTYP);
00484         free (indexTARTYP);
00485         free (indexMask);
00486         free (normalization);
00487         free (dataName);
00488         *error = 1;
00489         return (-1);
00490     }
00491     pTable = qfits_table_open (inFitsFile, extNumOfImagingDataFile);
00492     if (!pTable)
00493     {
00494         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load Data's IMAGING_DATA");
00495         qfits_table_close (pMask);
00496         free (inData);
00497         free (inTARTYP);
00498         free (inMask);
00499         free (foundData);
00500         free (indexData);
00501         free (foundTARTYP);
00502         free (indexTARTYP);
00503         free (indexMask);
00504         free (normalization);
00505         free (dataName);
00506         *error = 1;
00507         return (-1);
00508     }
00509 
00510     //    Get data table information
00511     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00512     {
00513         foundData[R] = 0;
00514         indexData[R] = 0;
00515     }
00516     for (i = 0; i < pTable->nc; i++)
00517     {
00518         for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00519         {
00520             sprintf (dataName, "DATA%d", R+1);
00521             if (strcmp (pTable->col[i].tlabel, dataName) == 0)
00522             {
00523                 foundData[R] = 1;
00524                 indexData[R] = i;
00525                 if (diagnostic) 
00526                 {
00527                    cpl_msg_info(cpl_func,"Found 'DATA%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00528                     fprintf(midiReportPtr, "Found 'DATA%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00529                 }
00530             }
00531             
00532             sprintf (dataName, "TARTYP%d", R+1);
00533             if (strcmp (pTable->col[i].tlabel, dataName) == 0)
00534             {
00535                 foundTARTYP[R] = 1;
00536                 indexTARTYP[R] = i;
00537                 if (diagnostic) 
00538                 {
00539                    cpl_msg_info(cpl_func,"Found 'TARTYP%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00540                     fprintf(midiReportPtr, "Found 'TARTYP%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00541                 }
00542             }
00543         }
00544         if (strcmp (pTable->col[i].tlabel, "STEPPING_PHASE") == 0)
00545         {
00546             foundSteppingPhase = 1;
00547             indexSteppingPhase = i;
00548         }
00549         if (strcmp (pTable->col[i].tlabel, "OPD") == 0)
00550         {
00551             indexOPD = i;
00552         }
00553         if (strcmp (pTable->col[i].tlabel, "LOCALOPD") == 0)
00554         {
00555             indexLOCALOPD = i;
00556         }
00557         if (strcmp (pTable->col[i].tlabel, "TIME") == 0)
00558         {
00559             indexTIME = i;
00560         }
00561     }
00562 
00563     //    Now issue warnings
00564     if (foundSteppingPhase == 0)
00565     {
00566         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find STEPPING_PHASE in data FITS file");
00567         *error = 1;
00568     }
00569     if (indexOPD < 0)
00570     {
00571         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find column for OPD in data FITS file");
00572         *error = 1;
00573     }
00574     if (indexLOCALOPD < 0)
00575     {
00576         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find column for LOCALOPD in data FITS file");
00577         *error = 1;
00578     }
00579     if (indexTIME < 0)
00580     {
00581         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find column for TIME in data FITS file");
00582         *error = 1;
00583     }
00584     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00585     {
00586         if (foundData[R] == 0)
00587         {
00588             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00589                 "Cannot find requested DATA column in data FITS file");
00590             *error = 1;
00591         }
00592         if (foundTARTYP[R] == 0)
00593         {
00594             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00595                 "Cannot find requested TARTYP column in data FITS file");
00596             *error = 1;
00597         }
00598     }
00599     
00600     //    Get mask data table information
00601     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00602     {
00603         foundData[R] = 0;
00604         indexMask[R] = 0;
00605     }
00606     for (i = 0; i < pMask->nc; i++)
00607     {
00608         for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00609         {
00610             sprintf (dataName, "DATA%d", R+1);
00611             if (strcmp (pMask->col[i].tlabel, dataName) == 0)
00612             {
00613                 foundData[R] = 1;
00614                 indexMask[R] = i;
00615                 if (diagnostic) 
00616                 {
00617                    cpl_msg_info(cpl_func,"Found 'DATA%d' at column %d in mask file %s \n", R+1, i+1, maskFile);
00618                     fprintf(midiReportPtr, "Found 'DATA%d' at column %d in mask file %s \n", R+1, i+1, maskFile);
00619                 }
00620             }
00621         }
00622     }
00623 
00624     //    Now issue warnings
00625     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00626     {
00627         if (foundData[R] == 0)
00628         {
00629             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00630                 "Cannot find requested DATA column in mask FITS file");
00631            *error = 1;
00632         }
00633     }
00634 
00635     //    Get subwindow format for the mask file
00636     sprintf (fitsColumnString, "TDIM%d", indexMask[1]+1);    // Point it to the column of a valid region
00637     tempStr = qfits_query_ext (maskFile, fitsColumnString, extNumOfImagingDataMask);
00638     sscanf (tempStr, "'(%d,%d) '", &maskWidthX, &maskWidthY);
00639     maskSubWindow = maskWidthX * maskWidthY;
00640     if (diagnostic)cpl_msg_info(cpl_func,"Mask sub-window size = %d\n", maskSubWindow);
00641     if (diagnostic)cpl_msg_info(cpl_func,"Data sub-window size = %d\n", localFormat->subWindowSize);
00642     fprintf (midiReportPtr, "Mask sub-window size = %d\n", maskSubWindow);
00643     fprintf (midiReportPtr, "Data sub-window size = %d\n", localFormat->subWindowSize);
00644     if (maskSubWindow != localFormat->subWindowSize)
00645     {
00646         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Mask has incompatible sub window size");
00647         *error = 1;
00648     }
00649 
00650     //    Get MAXSTEP value
00651     tempStr   = qfits_query_ext (inFitsFile, "MAXSTEP", extNumOfImagingDataFile);
00652     if (tempStr != NULL)
00653     { 
00654         if (diagnostic)cpl_msg_info(cpl_func,"MAXSTEP = %s\n", tempStr);
00655         fprintf( midiReportPtr, "MAXSTEP = %s\n", tempStr);
00656         sscanf (tempStr, "%d", &maxstep);
00657     }
00658     else
00659     {
00660         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read MAXSTEP");
00661         *error = 1;
00662     }
00663 
00664     //    Read column COL_STEPPING_PHASE and check compatibility
00665     inSteppingPhase = (short int*)qfits_query_column (pTable, indexSteppingPhase, NULL);
00666     if (inSteppingPhase[0] != ((aprioriSteppingPhase % localFormat->framesPerScan) + 1))
00667     {
00668         sprintf (midiMessage, "Incorrect Stepping Phase. Expected %d to %d. Instead found %d to %d", 
00669             aprioriSteppingPhase+1, maxstep, inSteppingPhase[0], maxstep-1);
00670         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00671         *error = 1;
00672     }
00673 
00674     //    Release memory and exit this module if there has been a warning. No point going any further
00675     if (*error)
00676     {
00677         qfits_table_close (pTable);
00678         qfits_table_close (pMask);
00679         free (inData);
00680         free (inTARTYP);
00681         free (inMask);
00682         free (foundData);
00683         free (indexData);
00684         free (foundTARTYP);
00685         free (indexTARTYP);
00686         free (indexMask);
00687         free (normalization);
00688         free (inSteppingPhase);
00689         free (dataName);
00690         return (-1);
00691     }
00692 
00693     //    Read DATA and TARTYP for all regions from input files
00694     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00695     {
00696         inData[R] = (short int*) qfits_query_column (pTable, indexData[R], NULL);
00697         inTARTYP[R] = (char *) qfits_query_column (pTable, indexTARTYP[R], NULL);
00698         
00699         found = 0;
00700         for (F = 0; F < localFormat->numOfFrames; F++)
00701         {
00702             //    Correct the initial anomalies
00703             if (newSet)
00704             {
00705                 if (inTARTYP[R][F*tartypMult] == 'U')
00706                     found = 1;
00707                 if (!found)
00708                     inTARTYP[R][F*tartypMult] = 'U';
00709             }
00710             
00711             //    Load data and correct tartype problem
00712             if (R == 0) (compressedPhotomA->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00713             else if (R == 1) (compressedInterf->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00714             else if (R == 2) 
00715             {
00716                 if (inTARTYP[R][F*tartypMult] != inTARTYP[R-1][F*tartypMult])
00717                 {
00718                     //    All channels and regions are affected
00719                     for (i = 0; i < localFormat->iXWidth; i++)
00720                     {
00721                         compressedInterf->rejectList[i][iF+F] |= BSL_TARTYPE_CROSS;
00722                     }
00723                     sprintf (midiMessage, "Incompatible Interferometry TARTYP2 & TARTYP3 at frame %d", iF+F);
00724                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00725                 }
00726             }
00727             else if (R == 3) 
00728             {    
00729                 (compressedPhotomB->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00730                 if (inTARTYP[R][F*tartypMult] != inTARTYP[R-3][F*tartypMult])
00731                 {
00732                     //    All channels and regions are affected
00733                     for (i = 0; i < localFormat->iXWidth; i++)
00734                     {
00735                         compressedPhotomA->rejectList[i][iF+F] |= BSL_TARTYPE_CROSS;
00736                         compressedPhotomB->rejectList[i][iF+F] |= BSL_TARTYPE_CROSS;
00737                     }
00738                     sprintf (midiMessage, "Incompatible Photometry TARTYP1 & TARTYP4 at frame %d", iF+F);
00739                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00740                 }
00741             }
00742         }
00743     }
00744 
00745     //    Get TIME
00746     if(indexTIME >= 0)    // Valid ...
00747     {
00748         startframe = 0;
00749         inTIME = (double *) qfits_query_column (pTable, indexTIME, NULL);
00750         //    Stuff it into time, subtracting off the zero time
00751         if (iF == 0) zeroTime = inTIME[0];
00752 
00753         //    Fix a bug found in the data:
00754         if (iF == 0)
00755         {
00756             for (startframe = 0; startframe < ARB_NUM_OF_FRAMES; startframe++)
00757             {
00758                 if ((zeroTime = inTIME[startframe]) >  1.0)    // Should be more like 53000 or something!
00759                 break;
00760             }
00761         }
00762         if (startframe)
00763         {
00764             if (diagnostic) 
00765             {
00766                cpl_msg_info(cpl_func,"\nLOOK: frames 0 - %d had ZERO for their time field!!\n", startframe-1);
00767                 fprintf(midiReportPtr, "\nLOOK: frames 0 - %d had ZERO for their time field!!\n", startframe-1);
00768             }
00769         }
00770 
00771         for (F = startframe; F < localFormat->numOfFrames; F++)
00772         {
00773             compressedInterf->time[iF+F] = (float)(inTIME[F] - zeroTime);
00774             compressedPhotomA->time[iF+F] = (float)(inTIME[F] - zeroTime);
00775             compressedPhotomB->time[iF+F] = (float)(inTIME[F] - zeroTime);
00776             if (isnan (compressedInterf->time[iF+F]) || 
00777                 isnan (compressedPhotomA->time[iF+F]) ||
00778                 isnan (compressedPhotomB->time[iF+F]))
00779             {
00780                 //    All channels and regions are affected
00781                 for (i = 0; i < localFormat->iXWidth; i++)
00782                 {
00783                     compressedInterf->rejectList[i][iF+F] |= BSL_TIME_ERROR;
00784                     compressedPhotomA->rejectList[i][iF+F] |= BSL_TIME_ERROR;
00785                     compressedPhotomB->rejectList[i][iF+F] |= BSL_TIME_ERROR;
00786                 }
00787                 sprintf (midiMessage, "inTIME has an INVALID value at frame %d", iF+F);
00788                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00789             }
00790         }
00791     }
00792 
00793     //    Get LOCALOPD
00794     if (indexLOCALOPD >= 0)    // Valid ...
00795     {
00796         inLOCALOPD = (double (*)[2]) qfits_query_column (pTable, indexLOCALOPD, NULL);
00797         //    Add up the 2 delay lines, and stuff it into localOPD:
00798         for (F = 0; F < localFormat->numOfFrames; F++)
00799         {
00800             compressedInterf->localOPD[iF+F] = (float)(inLOCALOPD[F][0] + inLOCALOPD[F][1]);
00801             compressedPhotomA->localOPD[iF+F] = (float)(inLOCALOPD[F][0] + inLOCALOPD[F][1]);
00802             compressedPhotomB->localOPD[iF+F] = (float)(inLOCALOPD[F][0] + inLOCALOPD[F][1]);
00803             if (isnan (compressedInterf->localOPD[iF+F]) ||
00804                 isnan (compressedPhotomA->localOPD[iF+F]) ||
00805                 isnan (compressedPhotomB->localOPD[iF+F]))
00806             {
00807                 //    All channels and regions are affected
00808                 for (i = 0; i < localFormat->iXWidth; i++)
00809                 {
00810                     compressedInterf->rejectList[i][iF+F] |= BSL_LOCALOPD_ERROR;
00811                     compressedPhotomA->rejectList[i][iF+F] |= BSL_LOCALOPD_ERROR;
00812                     compressedPhotomB->rejectList[i][iF+F] |= BSL_LOCALOPD_ERROR;
00813                 }
00814                 sprintf (midiMessage, "localOPD has an INVALID value at frame %d", iF+F);
00815                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00816             }
00817         }
00818     }
00819 
00820     //    Get OPD
00821     if (indexOPD >= 0)    // Valid ...
00822     {
00823         inOPD = (double (*)[2]) qfits_query_column (pTable, indexOPD, NULL);
00824         //    Add up the 2 delay lines, and stuff it into bigDL:
00825         for (F = 0; F < localFormat->numOfFrames; F++)
00826         {
00827             (compressedInterf->bigDL)[iF+F] = (float)(inOPD[F][0] + inOPD[F][1]);
00828             (compressedPhotomA->bigDL)[iF+F] = (float)(inOPD[F][0] + inOPD[F][1]);
00829             (compressedPhotomB->bigDL)[iF+F] = (float)(inOPD[F][0] + inOPD[F][1]);
00830             if (isnan (compressedInterf->bigDL[iF+F]) ||
00831                 isnan (compressedPhotomA->bigDL[iF+F]) ||
00832                 isnan (compressedPhotomB->bigDL[iF+F]))
00833             {
00834                 //    All channels and regions are affected
00835                 for (i = 0; i < localFormat->iXWidth; i++)
00836                 {
00837                     compressedInterf->rejectList[i][iF+F] |= BSL_OPD_ERROR;
00838                     compressedPhotomA->rejectList[i][iF+F] |= BSL_OPD_ERROR;
00839                     compressedPhotomB->rejectList[i][iF+F] |= BSL_OPD_ERROR;
00840                 }
00841                 sprintf (midiMessage, "bigDL has an INVALID value at frame %d", iF+F); 
00842                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00843             }
00844         }
00845     }
00846 
00847     //    Read all subregions from mask file
00848     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00849     {
00850         inMask[R] = (float*) qfits_query_column (pMask, indexMask[R], NULL);
00851 
00852         //    Display mask data
00853         if (diagnostic > 1 && plotFile && newSet)
00854         {
00855             fileString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00856             title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00857             sprintf (fileString, "3dMaskDATA%d", R+1);
00858             sprintf (title, "3D Mask DATA %d", R+1);
00859             midiCreatePlotFile3D (fileString, title, "X", "Y", "Flux", 0, 
00860                 inMask[R], localFormat->iXWidth, localFormat->iYWidth, "lines", "3");
00861             free (fileString);
00862             free (title);
00863         }
00864     }
00865 
00866     //    Select valid channels
00867     if (newSet || !channelSelected)
00868     {
00869         selectChannels (0, localFormat->numOfDetectorRegions, localFormat, inMask);
00870         channelSelected = 1;
00871     }
00872 
00873     //    Get the scaling offset
00874     for (i = 14; i < 25; i++)
00875     {
00876         sprintf (dataName, "TZERO%d", i);
00877         tempStr = qfits_query_ext (inFitsFile, dataName, extNumOfImagingDataFile);
00878         if (tempStr != NULL)
00879         {
00880             if (diagnostic)cpl_msg_info(cpl_func,"Scaling Offset = %s\n", tempStr);
00881             if (diagnostic) fprintf (midiReportPtr, "Scaling Offset = %s\n", tempStr);
00882             sscanf (tempStr, "%d", &scalingOffset);
00883             break;
00884         }
00885     }
00886     if (tempStr == NULL)
00887     {
00888         scalingOffset = 0;
00889         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Scaling Offset. It is set to 0");
00890     }
00891         
00892     //    ACTUAL COMPRESSION OF DATA HERE
00893     for (R = 0; R < localFormat->numOfDetectorRegions; R++)
00894     {
00895         //    Determine the normalization for each X (DISPERSED mode)
00896         for (X = 0; X < localFormat->iXWidth; X++)
00897         {
00898             accum = 0.0F;
00899             for (Y = 0; Y < localFormat->iYWidth; Y++)
00900                 accum +=  inMask[R][Y * localFormat->iXWidth + X];
00901 
00902             if (accum > 0.0)
00903                 normalization[X] = 1.F/accum;
00904             else
00905                 normalization[X] = 1.F;
00906         }
00907 
00908         for (F = 0; F < localFormat->numOfFrames; F++)
00909         {
00910             frameOffset = F * localFormat->subWindowSize;
00911             for (X = 0; X < localFormat->iXWidth; X++)
00912             {
00913                 accum = 0.0F;
00914                 for (Y = 0; Y < localFormat->iYWidth; Y++)
00915                 {
00916                     k = Y * localFormat->iXWidth + X;
00917                     i = frameOffset + k;
00918                     
00919                     //    If data is bad reject it
00920                     if (isnan (inData[R][i])) 
00921                     {
00922                         compressedInterf->rejectList[X][iF+F] |= BSL_DATA_ERROR;
00923                         compressedPhotomA->rejectList[X][iF+F] |= BSL_DATA_ERROR;
00924                         compressedPhotomB->rejectList[X][iF+F] |= BSL_DATA_ERROR;
00925                         sprintf (midiMessage, "inData has an INVALID value at frame %d", (iF+F));
00926                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00927                     }
00928                     else 
00929                     {
00930                         current = (inData[R][i] + scalingOffset) * inMask[R][k];
00931                         accum += current;
00932                     }
00933                 }
00934             
00935                 //    Accumulate further 
00936                 if (R == 0)
00937                 {
00938                     (((compressedPhotomA->iDispFringe)[R])[X])[iF+F] = 
00939                         accum * normalization[X];
00940                     (compressedPhotomA->iFringe1)[iF+F] += accum;
00941                 }
00942                 else if (R == 1)
00943                 {
00944                     (((compressedInterf->iDispFringe)[R-1])[X])[iF+F] = 
00945                         accum * normalization[X];
00946                     (compressedInterf->iFringe1)[iF+F] += accum;
00947                     (compressedInterf->iFringe)[iF+F] += accum;
00948                 }
00949                 else if (R == 2)
00950                 {
00951                     (((compressedInterf->iDispFringe)[R-1])[X])[iF+F] = 
00952                         accum * normalization[X];
00953                     (compressedInterf->iFringe2)[iF+F] += accum;
00954                     (compressedInterf->iFringe)[iF+F] -= accum;
00955                 }
00956                 else if (R == 3)
00957                 {
00958                     (((compressedPhotomB->iDispFringe)[R-3])[X])[iF+F] = 
00959                         accum * normalization[X];
00960                     (compressedPhotomB->iFringe1)[iF+F] += accum;
00961                 }
00962             }
00963         }
00964 
00965         //    Diagnostic
00966         if (diagnostic > 1)
00967         {
00968             for (F = 0; F < localFormat->numOfFrames-maxstep; F++)
00969             {
00970                 fst = F * localFormat->subWindowSize;
00971                 snd = (F+maxstep) * localFormat->subWindowSize;
00972                 if (compressedInterf->tarType[F] != 'U')
00973                 {
00974                     for (Y = 0; Y < localFormat->iYWidth; Y++)
00975                     {
00976                         for (X = 0; X < localFormat->iXWidth; X++)
00977                         {
00978                             k = X * localFormat->iYWidth + Y;
00979                             i = fst + k;
00980                             i2 = snd + k;
00981                             if ((compressedInterf->tarType[F] == 'T') && (compressedInterf->tarType[F+maxstep] == 'S'))
00982                                 current = (inData[R][i] + scalingOffset) - (inData[R][i2] + scalingOffset);
00983                             else if ((compressedInterf->tarType[F] == 'S') && (compressedInterf->tarType[F+maxstep] == 'T'))
00984                                 current = (inData[R][i2] + scalingOffset) - (inData[R][i] + scalingOffset);
00985                         
00986                             //    Load
00987                             if (R == 0)
00988                                 compressedPhotomA->image[R][k] += (current * inMask[R][k]);
00989                             if (R == 1 || R == 2)
00990                                 compressedInterf->image[R-1][k] += (current * inMask[R][k]);
00991                             if (R == 3)
00992                                 compressedPhotomB->image[R-3][k] += (current * inMask[R][k]);
00993                         }
00994                     }
00995                 }
00996             }
00997             F += maxstep;
00998         }
00999     }
01000 
01001     //    Save local parameters and indices
01002     aprioriSteppingPhase = inSteppingPhase[F-1];    // F-1 because of the above loop
01003     if (aprioriSteppingPhase == localFormat->framesPerScan) aprioriSteppingPhase = 0;
01004 
01005     frame0 = iF;    // To output on return, below
01006     iF += localFormat->numOfFrames;    // Set it correctly for the next call to this function
01007 
01008     //    Clean up now:
01009     for (R = 0; R < localFormat->numOfDetectorRegions; R++) 
01010     {
01011         free (inData[R]);
01012         free (inTARTYP[R]);
01013         free (inMask[R]);
01014     }
01015     if (pMask) qfits_table_close (pMask);
01016     if (pTable) qfits_table_close (pTable);
01017     if (inLOCALOPD) free(inLOCALOPD);
01018     if (inOPD) free(inOPD);
01019     if (inTIME) free(inTIME);
01020     free (inData);
01021     free (inTARTYP);
01022     free (inMask);
01023     free (inSteppingPhase);
01024     free (foundData);
01025     free (indexData);
01026     free (foundTARTYP);
01027     free (indexTARTYP);
01028     free (indexMask);
01029     free (normalization);
01030     free (dataName);
01031 
01032     return (frame0);
01033 }
01034 /*****************************************************************************/
01035 

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