preProcKappa.c

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

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