preProcFrgHS.c

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

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