procFrgSP.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  procFrgSP.c
00007 * Description:  Contains routines for Fringe Track data post processing
00008 *
00009 * History:
00010 * 03-Feb-05     (csabet) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Compiler directives
00016 ******************************************************************************/
00017 
00018 /******************************************************************************
00019 *   Include files
00020 ******************************************************************************/
00021 #include <stdio.h>
00022 #include <cpl.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include "midiGlobal.h"
00027 #include "midiLib.h"
00028 #include "memoryHandling.h"
00029 #include "errorHandling.h"
00030 #include "calibration.h"
00031 #include "fitsAnalysisFrgSP.h"
00032 #include "imageProcessing.h"
00033 #include "createProdFrg.h"
00034 #include "transferFunction.h"
00035 #include "visibility.h"
00036 #include "diagnostics.h"
00037 #include "estimationFrg.h"
00038 #include "photometry.h"
00039 #include "preProcFrgSP.h"
00040 #include "procFrgSP.h"
00041 #include "procFrgHS.h"
00042 #include "statistics.h"
00043 
00044 /**********************************************************
00045 *   Constant definitions
00046 **********************************************************/
00047 
00048 /**********************************************************
00049 *   Global Variables
00050 **********************************************************/
00051 
00052 /*============================ C O D E    A R E A ===========================*/
00053 
00054 
00055 
00056 
00057 /******************************************************************************
00058 *               European Southern Observatory
00059 *            VLTI MIDI Data Reduction Software
00060 *
00061 * Module name:  procFrgSP
00062 * Input/Output: See function arguments to avoid duplication
00063 * Description:    This is the main routine for processing the SCI_PHOT
00064 *                computation. If this is a SCI_PHOT_CALIB or SCI_PHOT_SCIENCE template 
00065 *                then we exepect ony Interferometry files with the following 
00066 *                configurations:
00067 *                
00068 *                DATA1 = Photometry A     data
00069 *                DATA2 = Interferometry     data
00070 *                DATA3 = Interferometry    data
00071 *                DATA4 = Photometry B    data
00072 *
00073 * History:
00074 * 21-Jul-03     (csabet) Created
00075 ******************************************************************************/
00076 void procFrgSP (
00077    MidiFiles         *fileNames,    // In: Pointer to midi files structure
00078    UserOptions       *options,    // In: Pointer to user options
00079    int               *error,      // Ou: Error status
00080    cpl_parameterlist *parlist,
00081    cpl_frameset      *frameset)
00082 {
00083 
00084     //    Local Declarations
00085     //    ------------------
00086     const char      routine[] = "procFrgSP";
00087     ImageFormat        *formatInterf=NULL;
00088     ImageFormat        *formatPhotomA=NULL;
00089     ImageFormat        *formatPhotomB=NULL;
00090     CompressedData    *compressedInterf=NULL, *compressedPhotomA=NULL, *compressedPhotomB=NULL;
00091     FilterData        *filterInfo=NULL;
00092     DispersedResult    *dispResult=NULL;
00093     CorrectedFrames     *corrFrames=NULL;
00094     //    Algorithm
00095     //    ---------
00096     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00097     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00098 
00099     //    Reset status
00100     *error = 0;
00101     if (options->processing == UNDISPERSED)
00102     {
00103         *error = 1;
00104         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00105             "'UNDISPERSED' is an invalid processing request for SCI_PHOT data");
00106         return;
00107     }
00108 
00109     //    Allocate memory
00110     formatInterf = callocImageFormat ();
00111     formatPhotomA = callocImageFormat ();
00112     formatPhotomB = callocImageFormat ();
00113     corrFrames = cpl_calloc (1, sizeof (CorrectedFrames));
00114 
00115     corrFrames->CorrInterf=0;
00116     corrFrames->CorrPhotomA=0;
00117     corrFrames->CorrPhotomB=0;
00118     
00119     //    Analyse Input files
00120     //    -------------------
00121     analyseFitsFrgSP (fileNames, formatInterf, formatPhotomA, formatPhotomB, error);
00122     if (*error)
00123     {
00124         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse FITS files");
00125         freeImageFormat (formatInterf);
00126         freeImageFormat (formatPhotomA);
00127         freeImageFormat (formatPhotomB);
00128         return;
00129     }
00130 
00131     //    Reduce Data
00132     //    -----------
00133     //  Allocate memory for data compression
00134     compressedInterf = callocMidiCompressed (formatInterf);
00135     compressedPhotomA = callocMidiCompressed (formatPhotomA);
00136     compressedPhotomB = callocMidiCompressed (formatPhotomB);
00137     badChannelList = (int *) calloc (formatInterf->iXWidth, sizeof (int)); 
00138     filterInfo = callocFilterInfo();
00139 
00140 
00141     //    Preprocessing
00142     preProcFrgSP (options, filterInfo, fileNames, compressedInterf, 
00143         compressedPhotomA, compressedPhotomB, formatInterf, formatPhotomA, formatPhotomB, error);
00144     if (*error)
00145     {
00146         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress FITS files");
00147         freeCompressedData (formatInterf, compressedInterf);
00148         freeCompressedData (formatPhotomA, compressedPhotomA);
00149         freeCompressedData (formatPhotomB, compressedPhotomB);
00150         free (badChannelList);
00151         freeImageFormat (formatInterf);
00152         freeImageFormat (formatPhotomA);
00153         freeImageFormat (formatPhotomB);
00154         freeFilterInfo (filterInfo);
00155         return;
00156     }
00157 
00158     //    Check Delay Lines for jumps within scans
00159     checkDelayLineJumps ("INTERF", formatInterf, compressedInterf);
00160     checkDelayLineJumps ("PHOTOMA", formatPhotomA, compressedPhotomA);
00161     checkDelayLineJumps ("PHOTOMB", formatPhotomB, compressedPhotomB);
00162 
00163     //    Check Delay Line Consistencies
00164     checkDelayLineConsistencies ("INTERF", "PHOTOMA", compressedInterf, compressedPhotomA, formatInterf, formatPhotomA, error);
00165     checkDelayLineConsistencies ("INTERF", "PHOTOMB", compressedInterf, compressedPhotomB, formatInterf, formatPhotomB, error);
00166     if (*error)
00167     {
00168         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "SCI_PHOT processing abandonned");
00169         freeCompressedData (formatInterf, compressedInterf);
00170         freeCompressedData (formatPhotomA, compressedPhotomA);
00171         freeCompressedData (formatPhotomB, compressedPhotomB);
00172         free (badChannelList);
00173         freeImageFormat (formatInterf);
00174         freeImageFormat (formatPhotomA);
00175         freeImageFormat (formatPhotomB);
00176         freeFilterInfo (filterInfo);
00177         return;
00178     }
00179 
00180     //    Check Chopping Statistics. for QCLOG diagnostics
00181     //    ------------------------------------------------
00182     reportInterfChopping ("INTERF", formatInterf, compressedInterf);
00183     reportPhotomChopping ("PHOTOMA", formatPhotomA, compressedPhotomA);
00184     reportPhotomChopping ("PHOTOMB", formatPhotomB, compressedPhotomB);
00185     displayInterfChoppingDisp ("INTERF", formatInterf, compressedInterf);
00186     displayPhotomChoppingDisp ("PHOTOMA", formatPhotomA, compressedPhotomA);
00187     displayPhotomChoppingDisp ("PHOTOMB", formatPhotomB, compressedPhotomB);
00188  
00189     //    Remove Sky Background and display Target with Sky removed
00190     dispResult = callocDispResult (formatInterf->iXWidth);
00191     cleanupFrgSP (options->processing, formatInterf, formatPhotomA, formatPhotomB, compressedInterf,
00192         compressedPhotomA, compressedPhotomB, error, corrFrames);
00193     if (*error)
00194     {
00195         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "SCI_PHOT processing abandonned");
00196         freeCompressedData (formatInterf, compressedInterf);
00197         freeCompressedData (formatPhotomA, compressedPhotomA);
00198         freeCompressedData (formatPhotomB, compressedPhotomB);
00199         free (badChannelList);
00200         freeImageFormat (formatInterf);
00201         freeImageFormat (formatPhotomA);
00202         freeImageFormat (formatPhotomB);
00203         freeFilterInfo (filterInfo);
00204         freeDispResult (dispResult);
00205         return;
00206     }
00207 
00208     //    Display Waterfall data
00209     prepareWaterfallDisplay (fileNames, formatInterf, compressedInterf);
00210 
00211     //    Apply optical compensation using the available Transfer Ratios
00212     applyOpticalCompensation (fileNames, formatInterf, compressedInterf, 
00213                               compressedPhotomA, compressedPhotomB, error, parlist, frameset);
00214 
00215     //    Frequency Calibration, if it is a CALIB batch
00216     //    ---------------------------------------------
00217     if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
00218         calibrateWaveUsingPolynom (fileNames, formatInterf);
00219 
00220     //    Create UNDISPERSED allSpectrum and dcLevels. For SCI_PHOT this is not necessary
00221     //    -------------------------------------------
00222 //    getUndispersedPowerSpectrum (formatInterf, compressedInterf);
00223         
00224     postProcFringe (fileNames, filterInfo, compressedInterf, compressedPhotomA,
00225                     compressedPhotomB, formatInterf, formatPhotomA,
00226                     formatPhotomB, dispResult, error,
00227                     parlist, frameset, corrFrames);
00228     if (*error)
00229     {
00230         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "SCI_PHOT processing abandonned");
00231         freeCompressedData (formatInterf, compressedInterf);
00232         freeCompressedData (formatPhotomA, compressedPhotomA);
00233         freeCompressedData (formatPhotomB, compressedPhotomB);
00234         free (badChannelList);
00235         freeImageFormat (formatInterf);
00236         freeImageFormat (formatPhotomA);
00237         freeImageFormat (formatPhotomB);
00238         freeFilterInfo (filterInfo);
00239         freeDispResult (dispResult);
00240         return;
00241     }
00242 
00243     //    Release memory used in this batch
00244     freeCompressedData (formatInterf, compressedInterf);
00245     freeCompressedData (formatPhotomA, compressedPhotomA);
00246     freeCompressedData (formatPhotomB, compressedPhotomB);
00247     free (badChannelList);
00248     freeImageFormat (formatInterf);
00249     freeImageFormat (formatPhotomA);
00250     freeImageFormat (formatPhotomB);
00251     freeFilterInfo (filterInfo);
00252     freeDispResult (dispResult);
00253     cpl_free(corrFrames);
00254     return;
00255 }
00256 /*****************************************************************************/
00257 
00258 
00259 /******************************************************************************
00260 *               European Southern Observatory
00261 *            VLTI MIDI Data Reduction Software
00262 *
00263 * Module name:  cleanupFrgSP
00264 * Input/Output: See function arguments to avoid duplication
00265 * Description:  Removes sky etc from the data
00266 *
00267 * History:
00268 * 06-Dec-05     (csabet) Created
00269 ******************************************************************************/
00270 void cleanupFrgSP (
00271     enum ProcessingMode    processing,
00272     ImageFormat            *formatInterf,        // In: Interf size parameters
00273     ImageFormat            *formatPhotomA,        // In: PhotA size parameters
00274     ImageFormat            *formatPhotomB,        // In: PhotB size parameters
00275     CompressedData        *compressedInterf,    // Ou: Pointer to the compressed interferometry data structure
00276     CompressedData        *compressedPhotomA,    // Ou: Pointer to the compressed photom A data structure
00277     CompressedData        *compressedPhotomB,    // Ou: Pointer to the compressed photom B data structure
00278     int                    *error,               // Ou: Error status
00279     CorrectedFrames         *corrFrames)         // Corrected/removed frames
00280 {
00281  
00282     //    Local Declarations
00283     //    ------------------
00284     const char    routine[] = "cleanupFrgSP";
00285     int            transitions, f, R, X, n, localError;
00286     double        *array, fluxAve;
00287     float        *arrayF;
00288     char        *fileName, *title;
00289 
00290     //    Algorithm
00291     //    ---------
00292     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00293     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00294  
00295    cpl_msg_info(cpl_func,"\nCleaning up SCI_PHOT files for batch  %d \n", batchNumber);
00296    cpl_msg_info(cpl_func,"------------------------------------ \n");
00297     fprintf (midiReportPtr, "\nCleaning up SCI_PHOT files for batch  %d \n", batchNumber);
00298     fprintf (midiReportPtr, "------------------------------------ \n");
00299  
00300     //    Initialise
00301     localError = 0;
00302     *error = 0;
00303     transitions = 0;
00304     
00305     //    Check Interferometry chopping cycle
00306     //    -----------------------------------    
00307     transitions = countTransitions ('T', formatInterf->numOfFrames, compressedInterf->tarType);
00308     sprintf (midiMessage, "Target Transitions for Interf   = %d", transitions); 
00309     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00310     if (transitions < 10)
00311     {
00312         sprintf (midiMessage, "Interferometry data is not chopped"); 
00313         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00314         *error = 1;
00315     }
00316  
00317     //    Check PhotomA chopping cycle
00318     //    ----------------------------    
00319     transitions = countTransitions ('T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
00320     sprintf (midiMessage, "Target Transitions for Photom A = %d", transitions); 
00321     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00322     if (transitions < 10)
00323     {
00324         sprintf (midiMessage, "Photometry A data is not chopped"); 
00325         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00326         *error = 1;
00327     }
00328  
00329     //    Check PhotomB chopping cycle
00330     //    ----------------------------
00331     transitions = countTransitions ('T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
00332     sprintf (midiMessage, "Target Transitions for Photom B = %d", transitions); 
00333     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00334     if (transitions < 10)
00335     {
00336         sprintf (midiMessage, "Photometry B data is not chopped"); 
00337         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00338         *error = 1;
00339     }
00340  
00341     //    if No error so far
00342     if (!(*error))
00343     {
00344         formatInterf->chopped = 1;
00345         formatPhotomA->chopped = 1;
00346         formatPhotomB->chopped = 1;
00347     }
00348     else 
00349     {
00350         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot cleanup SCI_PHOT files");
00351         *error = 1;
00352         return;
00353     }
00354     
00355     //    Correct benign tarType inconsistencies
00356     //    --------------------------------------
00357 
00358 
00359 
00360     corrFrames->CorrInterf=correctTarType("INTERF", compressedInterf->tarType,
00361             compressedInterf->time, formatInterf->numOfFrames, &localError);
00362     if (localError) *error = 1;
00363 
00364     corrFrames->CorrPhotomA=correctTarType("PHOTOMA", compressedPhotomA->tarType,
00365             compressedPhotomA->time, formatPhotomA->numOfFrames, &localError);
00366     if (localError) *error = 1;
00367 
00368     corrFrames->CorrPhotomB=correctTarType("PHOTOMB", compressedPhotomB->tarType,
00369             compressedPhotomB->time, formatPhotomB->numOfFrames, &localError);
00370     if (*error || localError) 
00371     {
00372         *error = 1;
00373         return;
00374     }
00375 
00376     //    Remove sky background
00377     //    ---------------------
00378     removeSkyBackground ("INTERF", processing, formatInterf, compressedInterf, &localError);
00379     if (localError) *error = 1;
00380     removeSkyBackground ("PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
00381     if (localError) *error = 1;
00382     removeSkyBackground ("PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
00383     if (formatInterf->numOfFrames != formatPhotomA->numOfFrames || 
00384         formatInterf->numOfFrames != formatPhotomB->numOfFrames || *error || localError)
00385     {
00386         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unequal numOfFrames");
00387         *error = 1;
00388         return;
00389     }
00390  
00391     //    Reject frames whose flux are <= 0 or are not 'T'
00392     for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00393     {
00394         for (X = 0; X < formatInterf->iXWidth; X++)
00395         {
00396             if (badChannelList[X])
00397                 continue;
00398                     
00399             for (f = 0; f < formatInterf->numOfFrames; f++)
00400             {
00401 /*                 if ((compressedInterf->tarType[f] == 'T') &&  */
00402 /*                     (((compressedInterf->iDispFringe)[R])[X])[f] <= 0.0) */
00403 /*                 { */
00404 /*                     compressedInterf->rejectList[X][f] |= BSL_DATA_ERROR; */
00405 /*                     if (diagnostic > 3)  */
00406 /*                     { */
00407 /*                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d          Interf %10.2f",  */
00408 /*                             R, X, f, (((compressedInterf->iDispFringe)[R])[X])[f]); */
00409 /*                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00410 /*                     } */
00411 /*                 } */
00412                 //    Reject frames which are not 'T'
00413                 if (compressedInterf->tarType[f] != 'T')
00414                     compressedInterf->rejectList[X][f] |= BSL_SKY_SCAN;
00415             }
00416         }
00417     }
00418  
00419     for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00420     {
00421         for (X = 0; X < formatPhotomA->iXWidth; X++)
00422         {
00423             if (badChannelList[X])
00424                 continue;
00425                     
00426             for (f = 0; f < formatPhotomA->numOfFrames; f++)
00427             {
00428 /*                 if ((compressedPhotomA->tarType[f] == 'T') &&  */
00429 /*                     (((compressedPhotomA->iDispFringe)[R])[X])[f] <= 0.0) */
00430 /*                 { */
00431 /*                     compressedPhotomA->rejectList[X][f] |= BSL_DATA_ERROR; */
00432 /* /\*         If the photometry is bad in one of the channels, set also the interferometry to "BSL_DATA_ERROR" *\/ */
00433 /*                     if(formatPhotomA->iXWidth == formatInterf->iXWidth && formatPhotomA->numOfFrames == formatInterf->numOfFrames)  */
00434 /*                     { */
00435 /*                        compressedInterf->rejectList[X][f] |= BSL_DATA_ERROR; */
00436 /*                     } */
00437 /*                     if (diagnostic > 3)  */
00438 /*                     { */
00439 /*                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         PhotomA %10.2f",  */
00440 /*                             R, X, f, (((compressedPhotomA->iDispFringe)[R])[X])[f]); */
00441 /*                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00442 /*                     } */
00443 /*                 } */
00444                 //    Reject frames which are not 'T'
00445                 if (compressedPhotomA->tarType[f] != 'T')
00446                     compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
00447             }
00448         }
00449     }
00450  
00451     for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00452     {
00453         for (X = 0; X < formatPhotomB->iXWidth; X++)
00454         {
00455             if (badChannelList[X])
00456                 continue;
00457                     
00458             for (f = 0; f < formatPhotomB->numOfFrames; f++)
00459             {
00460 /*                 if ((compressedPhotomB->tarType[f] == 'T') &&  */
00461 /*                     (((compressedPhotomB->iDispFringe)[R])[X])[f] <= 0.0) */
00462 /*                 { */
00463 /*                     compressedPhotomB->rejectList[X][f] |= BSL_DATA_ERROR; */
00464 /* /\*         If the photometry is bad in one of the channels, set also the interferometry to "BSL_DATA_ERROR" *\/ */
00465 /*                     if(formatPhotomB->iXWidth == formatInterf->iXWidth && formatPhotomB->numOfFrames == formatInterf->numOfFrames)  */
00466 /*                     { */
00467 /*                     compressedInterf->rejectList[X][f] |= BSL_DATA_ERROR; */
00468 /*                     } */
00469 /*                     if (diagnostic > 3)  */
00470 /*                     { */
00471 /*                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         PhotomB %10.2f",  */
00472 /*                             R, X, f, (((compressedPhotomB->iDispFringe)[R])[X])[f]); */
00473 /*                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00474 /*                     } */
00475 /*                 } */
00476                 //    Reject frames which are not 'T'
00477                 if (compressedPhotomB->tarType[f] != 'T')
00478                     compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
00479             }
00480         }
00481     }
00482 
00483 
00484 
00485 
00486 
00487 
00488 
00489 
00490 
00491 
00492     //    Display data with background removed
00493     if (plotFile)
00494     {
00495         array = (double *) calloc (formatInterf->numOfFrames, sizeof (double));
00496  
00497         n = 0;
00498         fluxAve = 0.0;
00499         for (f = 0; f < formatInterf->numOfFrames; f++)
00500         {
00501             if (compressedInterf->tarType[f] == 'T')
00502             {
00503                 array[n] = compressedInterf->iFringe1[f];
00504                 fluxAve += array[n];
00505                 n++;
00506             }
00507         }
00508         fluxAve /= n;
00509        cpl_msg_info(cpl_func,"Interf DATA2 Average Flux = %f \n", fluxAve);
00510         midiCreatePlotFileDouble2D ("InterfCleanDATA2", "Interferometry DATA2 (Sky Removed)", 
00511             "Frame", "Flux", 0, array, 0, n, 1);
00512  
00513         n = 0;
00514         fluxAve = 0.0;
00515         for (f = 0; f < formatInterf->numOfFrames; f++)
00516         {
00517             if (compressedInterf->tarType[f] == 'T')
00518             {
00519                 array[n] = compressedInterf->iFringe2[f];
00520                 fluxAve += array[n];
00521                 n++;
00522             }
00523         }
00524         fluxAve /= n;
00525        cpl_msg_info(cpl_func,"Interf DATA3 Average Flux = %f \n", fluxAve);
00526         midiCreatePlotFileDouble2D ("InterfCleanDATA3", "Interferometry DATA3 (Sky Removed)", 
00527             "Frame", "Flux", 0, array, 0, n, 1);
00528  
00529         n = 0;
00530         for (f = 0; f < formatInterf->numOfFrames; f++)
00531         {
00532             if (compressedInterf->tarType[f] == 'T')
00533             {
00534                 array[n] = compressedInterf->iFringe[f];
00535                 n++;
00536             }
00537         }
00538         midiCreatePlotFileDouble2D ("InterfCleanDATA2_DATA3", "Interferometry DATA2-DATA3 (Sky Removed)", 
00539             "Frame", "Flux", 0, array, 0, n, 1);
00540  
00541         n = 0;
00542         fluxAve = 0.0;
00543         for (f = 0; f < formatPhotomA->numOfFrames; f++)
00544         {
00545             if (compressedPhotomA->tarType[f] == 'T')
00546             {
00547                 array[n] = compressedPhotomA->iFringe1[f];
00548                 fluxAve += array[n];
00549                 n++;
00550             }
00551         }
00552         fluxAve /= n;
00553        cpl_msg_info(cpl_func,"PhotomA DATA1 Average Flux = %f \n", fluxAve);
00554         midiCreatePlotFileDouble2D ("PhotomACleanDATA1", "Photometry A DATA1 (Sky Removed)", 
00555             "Frame", "Flux", 0, array, 0, n, 1);
00556  
00557         n = 0;
00558         fluxAve = 0.0;
00559         for (f = 0; f < formatPhotomB->numOfFrames; f++)
00560         {
00561             if (compressedPhotomB->tarType[f] == 'T')
00562             {
00563                 array[n] = compressedPhotomB->iFringe1[f];
00564                 fluxAve += array[n];
00565                 n++;
00566             }
00567         }
00568         fluxAve /= n;
00569        cpl_msg_info(cpl_func,"PhotomB DATA4 Average Flux = %f \n", fluxAve);
00570         midiCreatePlotFileDouble2D ("PhotomBCleanDATA4", "Photometry B DATA4 (Sky Removed)", 
00571             "Frame", "Flux", 0, array, 0, n, 1);
00572  
00573         free (array);
00574     }
00575 
00576     //    Display for each channel
00577     if (plotFile && diagnostic > 4)
00578     {
00579         arrayF = (float *) calloc (formatInterf->numOfFrames, sizeof (float));
00580         fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00581         title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00582         
00583         for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00584         {
00585             for (X = 0; X < formatInterf->iXWidth; X++)
00586             {
00587                 if (badChannelList[X])
00588                     continue;
00589                     
00590                 n = 0;
00591                 for (f = 0; f < formatInterf->numOfFrames; f++)
00592                 {
00593                     if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
00594                     {
00595                         arrayF[n] = compressedInterf->iDispFringe[R][X][f];
00596                         n++;
00597                     }
00598                 }
00599                 sprintf (fileName, "InterfCleanDATA%d_X%d", R+2, X+1);
00600                 sprintf (title, "Interferometry DATA%d for Channel %d (Sky Removed)", R+2, X+1);
00601                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00602             }
00603         }
00604         for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00605         {
00606             for (X = 0; X < formatPhotomA->iXWidth; X++)
00607             {
00608                 if (badChannelList[X])
00609                     continue;
00610                     
00611                 n = 0;
00612                 for (f = 0; f < formatPhotomA->numOfFrames; f++)
00613                 {
00614                     if (compressedPhotomA->tarType[f] == 'T' && !(compressedPhotomA->rejectList[X][f]))
00615                     {
00616                         arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
00617                         n++;
00618                     }
00619                 }
00620                 sprintf (fileName, "PhotomACleanDATA1_X%d", X+1);
00621                 sprintf (title, "Photometry A DATA1 for Channel %d (Sky Removed)", X+1);
00622                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00623             }
00624         }
00625         for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00626         {
00627             for (X = 0; X < formatPhotomB->iXWidth; X++)
00628             {
00629                 if (badChannelList[X])
00630                     continue;
00631                     
00632                 n = 0;
00633                 for (f = 0; f < formatPhotomB->numOfFrames; f++)
00634                 {
00635                     if (compressedPhotomB->tarType[f] == 'T' && !(compressedPhotomB->rejectList[X][f]))
00636                     {
00637                         arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
00638                         n++;
00639                     }
00640                 }
00641                 sprintf (fileName, "PhotomBCleanDATA4_X%d", X+1);
00642                 sprintf (title, "Photometry B DATA4 for Channel %d (Sky Removed)", X+1);
00643                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00644             }
00645         }
00646         free (fileName);
00647         free (title);
00648         free (arrayF);
00649     }
00650 
00651 
00652 
00653     //    Display the mean flux as a function of the channel
00654 
00655     if (plotFile)
00656     {   
00657        arrayF = (float *) calloc (formatInterf->iXWidth, sizeof (float));
00658        fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00659        title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00660        
00661        for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00662        {
00663           for (X = 0; X < formatInterf->iXWidth; X++)
00664           {
00665              arrayF[X]=0;
00666              if (badChannelList[X])
00667                 continue;
00668              
00669               n = 0;
00670               for (f = 0; f < formatInterf->numOfFrames; f++)
00671               {
00672                  if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
00673                  {
00674                     arrayF[X] += compressedInterf->iDispFringe[R][X][f];
00675                     n++;
00676                  }
00677               }
00678               arrayF[X] /=(float)n;
00679           }
00680           
00681           sprintf (fileName, "MeanInterfCleanDATA%d", R+1);
00682           sprintf (title, "Mean Interferometry DATA%d (Sky Removed)", R+1);
00683           midiCreatePlotFile2D (fileName, title, "Channel", "Flux", 0, arrayF, 0, formatInterf->iXWidth, 1, 0);
00684        }
00685        free (fileName);
00686        free (title);
00687        free (arrayF);
00688     }
00689 
00690 
00691     //    Finalise rejection list
00692     finaliseRejectListInterf ("Combined Interferometry", formatInterf, compressedInterf, error);
00693     if (*error) return;
00694     finaliseRejectListPhotom ("Photometry A", formatPhotomA, compressedPhotomA, error);
00695     if (*error) return;
00696     finaliseRejectListPhotom ("Photometry B", formatPhotomB, compressedPhotomB, error);
00697     if (*error) return;
00698 
00699     return;
00700     
00701 }
00702 /*****************************************************************************/
00703 
00704 
00705 /******************************************************************************
00706 *               European Southern Observatory
00707 *            VLTI MIDI Data Reduction Software
00708 *
00709 * Module name:  applyOpticalCompensation
00710 * Input/Output: See function arguments to avoid duplication
00711 * Description:  Uses the existing Transfer Ratios to carry out optical compensation
00712 *                If Transfer Ratios is not available in the current directory
00713 *                it will extract the data from the static database
00714 *
00715 * History:
00716 * 06-Dec-05     (csabet) Created
00717 ******************************************************************************/
00718 void applyOpticalCompensation (
00719    MidiFiles         *fileNames,            // In: Pointer to midi files structure
00720    ImageFormat       *format,            // In: Interf size parameters
00721    CompressedData    *compressedInterf,    // Ou: Pointer to the compressed interferometry data structure
00722    CompressedData    *compressedPhotomA,    // Ou: Pointer to the compressed photom A data structure
00723    CompressedData    *compressedPhotomB,    // Ou: Pointer to the compressed photom B data structure
00724    int               *error,  // Ou: Error status
00725    cpl_parameterlist *parlist,
00726    cpl_frameset      *frameset)                
00727 {
00728    
00729    //    Local Declarations
00730    //    ------------------
00731    const char      routine[] = "applyOpticalCompensation";
00732 /*    FILE            *trrPtr=NULL; */
00733    TransferRatios  *trr;
00734    int             i, X, n;
00735 /*    int             numOfRecords; */
00736 /*    char            *fileString; */
00737    char            * tag=NULL;
00738    char            * softag=NULL;
00739    cpl_frame       * cur_frame;
00740    cpl_table       * table;
00741    float * kappamatrix11=NULL;  
00742    float * kappamatrix12=NULL;  
00743    float * kappamatrix21=NULL;  
00744    float * kappamatrix22=NULL;  
00745    float * kappamatrix11_error=NULL;  
00746    float * kappamatrix12_error=NULL;  
00747    float * kappamatrix21_error=NULL;  
00748    float * kappamatrix22_error=NULL;  
00749    int     dimenkappamatrix=0;  
00750    cpl_errorstate          prestate = cpl_errorstate_get();   
00751    
00752    
00753    //    Algorithm
00754    //    ---------
00755    if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00756    if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00757    
00758    //    Initialise
00759    *error = 0;
00760    
00761    //    Check if Transfer Ratios file is available. Try to open the file for reading
00762    cpl_msg_info(cpl_func,"\nApplying Optical Compensation for batch %d \n", batchNumber);
00763    cpl_msg_info(cpl_func,"--------------------------------------- \n");
00764    fprintf (midiReportPtr, "\nApplying Optical Compensation for batch %d \n", batchNumber);
00765    fprintf (midiReportPtr, "--------------------------------------- \n");
00766 
00767 
00768 /* ---------------------------------------- */
00769    {
00770 /*       Grism or Prism? */
00771       if (strcmp(format->grismId,"GRISM")==0){
00772          softag = cpl_sprintf("%s", "MIDI_KAPPAMATRIX_GRISM");
00773       }
00774       if (strcmp(format->grismId,"PRISM")==0){
00775          softag = cpl_sprintf("%s", "MIDI_KAPPAMATRIX_PRISM");
00776       }
00777       
00778       
00779       cur_frame = cpl_frameset_get_first(frameset);
00780       if (cur_frame == NULL) {
00781          cpl_msg_error(cpl_func, "No frame found in the SOF");  
00782          return ;
00783          
00784       }
00785       
00786       while(cur_frame)
00787       {
00788          /*      Check the right tags */
00789          tag = (char*)cpl_frame_get_tag(cur_frame);
00790          if (strcmp(tag, softag)) {
00791             cur_frame = cpl_frameset_get_next( frameset );   
00792             continue;
00793          }
00794          else{
00795             cpl_msg_info(cpl_func, "Kappamatrix found in the SOF: %s\n",cpl_frame_get_filename(cur_frame));   
00796             break;
00797          }
00798 /*          cur_frame = cpl_frameset_get_next( frameset );     */
00799       }
00800       
00801       if (strcmp(tag, softag)) {
00802          cpl_msg_info(cpl_func, "No Kappamatrix found in the SOF!");   
00803          cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00804          *error = 1;
00805          return;
00806       }
00807 
00808       cpl_free(softag);
00809 
00810       /* Load extension  MIDI_KAPPAMATRIX == extension 1  Do it more clever in the future*/ 
00811 
00812       prestate = cpl_errorstate_get();   
00813       if((table = cpl_table_load(cpl_frame_get_filename(cur_frame), 1, 1))==NULL)
00814       {
00815          cpl_msg_info(cpl_func, "No siutable table found in the file %s\n",cpl_frame_get_filename(cur_frame));   
00816          cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00817          cpl_errorstate_set(prestate);  
00818          *error = 1;
00819          return;
00820       }
00821 
00822       /* If all is Ok read the values */
00823       
00824       if (cpl_table_has_column(table, "kappam11"))
00825       {
00826          dimenkappamatrix=cpl_table_get_column_dimensions(table, "kappam11");
00827          kappamatrix11=cpl_table_get_data_float(table, "kappam11");
00828          kappamatrix12=cpl_table_get_data_float(table, "kappam12");
00829          kappamatrix21=cpl_table_get_data_float(table, "kappam21");
00830          kappamatrix22=cpl_table_get_data_float(table, "kappam22");
00831          kappamatrix11_error=cpl_table_get_data_float(table, "kappam11_error");
00832          kappamatrix12_error=cpl_table_get_data_float(table, "kappam12_error");
00833          kappamatrix21_error=cpl_table_get_data_float(table, "kappam21_error");
00834          kappamatrix22_error=cpl_table_get_data_float(table, "kappam22_error");
00835 /*        cpl_msg_info(cpl_func, "kappam11: %f", kappamatrix11[0]); */
00836       }
00837       else{
00838          cpl_msg_info(cpl_func, "No siutable table column found in the file %s\n",cpl_frame_get_filename(cur_frame));   
00839          cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00840          *error = 1;
00841          return;
00842       }
00843       
00844       trr = callocTransferRatios (format->iXWidth);
00845       
00846       cpl_msg_info(cpl_func,"Dimension: %d",dimenkappamatrix);
00847 /*       for (i=0; i<dimenkappamatrix;i++){ */
00848       for (i=0; i<format->iXWidth;i++){
00849          
00850          trr->ka1[i]=kappamatrix11[i] ;
00851          trr->siga1[i]=kappamatrix11_error[i] ; 
00852          trr->ka2[i]=kappamatrix12[i] ; 
00853          trr->siga2[i]=kappamatrix12_error[i] ; 
00854          trr->kb1[i]=kappamatrix21[i] ; 
00855          trr->sigb1[i]=kappamatrix21_error[i] ; 
00856          trr->kb2[i]=kappamatrix22[i] ; 
00857          trr->sigb2[i]=kappamatrix22_error[i] ;
00858          cpl_msg_info(cpl_func,"%3d        %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f \n", i, 
00859                       trr->ka1[i], trr->siga1[i], trr->ka2[i], trr->siga2[i], 
00860                       trr->kb1[i], trr->sigb1[i], trr->kb2[i], trr->sigb2[i]);
00861       }         
00862 
00863       for (i=0; i<format->iXWidth;i++){
00864          if (!(trr->ka1[i]) || !(trr->ka2[i]) || !(trr->kb1[i]) || !(trr->kb2[i]))
00865          {
00866             badChannelList[i] |= BSL_DATA_ERROR;
00867             cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", i, UNAV);
00868             fprintf (midiReportPtr, "%3d    <-- %s --> QCLOG \n", i, UNAV);
00869          }
00870       }
00871       
00872       //    Compute compensated beams:
00873       
00874       //    Check for valid channels
00875       n = 0;
00876       for (X = 0; X < format->iXWidth; X++)
00877       {
00878          if (!(badChannelList[X]))
00879             n++;
00880       }        
00881       if (!n)
00882       {
00883          cpl_msg_warning(cpl_func, "Cannot find any valid Transfer Ratios");
00884          cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00885          *error = 1;
00886          freeTransferRatios (trr);
00887          return;
00888       }
00889       
00890       //    Get compensated beams
00891       getCompensatedBeams (trr, format, compressedInterf, compressedPhotomA, compressedPhotomB);
00892       freeTransferRatios (trr);
00893       cpl_table_delete(table);      
00894 /*       *error = 1; */
00895       return;   
00896    }
00897 
00898 /* ---------------------------------------- */
00899 
00900 
00901 
00902    
00903 /*    if (diagnostic)cpl_msg_info(cpl_func,"Trying to extract Transfer Ratios from %s \n", fileNames->trrNameRead); */
00904 /*    fprintf (midiReportPtr, "Trying to extract Transfer Ratios from %s \n", fileNames->trrNameRead); */
00905 /*    if ((trrPtr = fopen (fileNames->trrNameRead, "r")) == NULL) */
00906 /*    { */
00907 /*       *error = 1; */
00908 /*       sprintf (midiMessage, "Cannot open Transfer Ratios file %s in the local directory", fileNames->trrNameRead);  */
00909 /*       midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00910 /*    } */
00911    
00912 /*    //    If Transfer Ratios file is not available in the local directory try the static database */
00913 /*    if (*error) */
00914 /*    { */
00915 /*       *error = 0; */
00916 /*       fileString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char)); */
00917 /*       sprintf (fileString , "%s_%s.dat", fileNames->transferRatiosName, format->grismId); */
00918 /*       if (diagnostic)cpl_msg_info(cpl_func,"Trying to extract Transfer Ratios from the static database %s \n", fileString); */
00919 /*       fprintf (midiReportPtr, "Trying to extract Transfer Ratios from the static database %s \n", fileString); */
00920 /*       if ((trrPtr = fopen (fileString, "r")) == NULL) */
00921 /*       { */
00922 /*          sprintf (midiMessage, "Cannot open Transfer Ratios file %s", fileString);  */
00923 /*          midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00924 /*          *error = 1; */
00925 /*       } */
00926 /*       free (fileString); */
00927 /*    } */
00928    
00929 /*    //    If all is well, read the supporting parameters */
00930 /*    if (!(*error)) */
00931 /*    { */
00932 /*       //    Read number of record in the file */
00933 /*       if (fscanf (trrPtr, "%d \n", &numOfRecords) == EOF) */
00934 /*       { */
00935 /*          *error = 1; */
00936 /*          midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Number of Records"); */
00937 /*          if (trrPtr) fclose (trrPtr); */
00938 /*       } */
00939 /*       else  */
00940 /*       { */
00941 /*          //    Check if number of records is consistent */
00942 /*          if (numOfRecords != format->iXWidth) */
00943 /*          { */
00944 /*             *error = 1; */
00945 /*             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent Number of Records"); */
00946 /*             cpl_msg_info(cpl_func,"Given Number of Records = %d. Expected %d \n", numOfRecords, format->iXWidth); */
00947 /*             fprintf (midiReportPtr, "Given Number of Records = %d. Expected %d \n", numOfRecords, format->iXWidth); */
00948 /*             if (trrPtr) fclose (trrPtr); */
00949 /*          } */
00950 /*       } */
00951       
00952 /*       //    If no error, extract the data */
00953 /*       if (!(*error)) */
00954 /*       { */
00955 /*          trr = callocTransferRatios (format->iXWidth); */
00956          
00957 /*          //    Extract Transfer Ratios. File pointer should now be at the second line */
00958 /*          cpl_msg_info(cpl_func,"Channel     ka1   siga1         ka2   siga2         kb1   sigb1         kb2   sigb2 \n"); */
00959 /*          fprintf (midiReportPtr, "Channel    ka1   siga1         ka2   siga2         kb1   sigb1         kb2   sigb2 \n"); */
00960 
00961 
00962 /*          for (i = 0; i < format->iXWidth; i++) */
00963 /*          { */
00964 /*             if (fscanf (trrPtr, "%f %f %f %f %f %f %f %f \n", &(trr->ka1[i]), &(trr->siga1[i]),  */
00965 /*                         &(trr->ka2[i]), &(trr->siga2[i]), &(trr->kb1[i]), &(trr->sigb1[i]),  */
00966 /*                         &(trr->kb2[i]), &(trr->sigb2[i])) == EOF) */
00967 /*             { */
00968 /*                *error = 1; */
00969 /*                sprintf (midiMessage, "Extracted %d records. Expected %d", i, format->iXWidth); */
00970 /*                midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00971 /*                if (trrPtr) fclose (trrPtr); */
00972 /*                freeTransferRatios (trr); */
00973 /*                break; */
00974 /*             } */
00975 /*             else */
00976 /*             { */
00977 /*                if (!(trr->ka1[i]) || !(trr->ka2[i]) || !(trr->kb1[i]) || !(trr->kb2[i])) */
00978 /*                { */
00979 /*                   badChannelList[i] |= BSL_DATA_ERROR; */
00980 /*                   cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", i, UNAV); */
00981 /*                   fprintf (midiReportPtr, "%3d    <-- %s --> QCLOG \n", i, UNAV); */
00982 /*                   if (diagnostic > 2) */
00983 /*                   { */
00984 /*                      sprintf (midiMessage, "Found invalid Transfer Ratios for channel %3d", i); */
00985 /*                      midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
00986 /*                   } */
00987 /*                } */
00988 /*                else */
00989 /*                { */
00990 /*                   cpl_msg_info(cpl_func,"%3d        %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f \n", i,  */
00991 /*                                trr->ka1[i], trr->siga1[i], trr->ka2[i], trr->siga2[i],  */
00992 /*                                trr->kb1[i], trr->sigb1[i], trr->kb2[i], trr->sigb2[i]); */
00993 /*                   fprintf (midiReportPtr, "%3d        %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f QCLOG \n", i,  */
00994 /*                            trr->ka1[i], trr->siga1[i], trr->ka2[i], trr->siga2[i],  */
00995 /*                            trr->kb1[i], trr->sigb1[i], trr->kb2[i], trr->sigb2[i]); */
00996 /*                } */
00997 /*             } */
00998 /*          } */
00999          
01000 /*          //    Compute compensated beams */
01001 /*          if (!(*error)) */
01002 /*          { */
01003 /*             sprintf (midiMessage, "Extracted the above %d Transfer Ratios records \n", format->iXWidth); */
01004 /*             midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage); */
01005 /*             if (trrPtr) fclose (trrPtr); */
01006             
01007 /*             //    Check for valid channels */
01008 /*             n = 0; */
01009 /*             for (X = 0; X < format->iXWidth; X++) */
01010 /*             { */
01011 /*                if (!(badChannelList[X])) */
01012 /*                   n++; */
01013 /*             }         */
01014 /*             if (!n) */
01015 /*             { */
01016 /*                midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find any valid Transfer Ratios"); */
01017 /*                midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,  */
01018 /*                                   "Subsequent computational values are without Optical Compensation"); */
01019 /*                *error = 1; */
01020 /*                freeTransferRatios (trr); */
01021 /*                return; */
01022 /*             } */
01023             
01024 /*             //    Get compensated beams */
01025 /*             getCompensatedBeams (trr, format, compressedInterf, compressedPhotomA, compressedPhotomB); */
01026 /*             freeTransferRatios (trr); */
01027 /*          } */
01028 /*       } */
01029 /*       else */
01030 /*       { */
01031 /*          midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,  */
01032 /*                             "Subsequent computational values are without Optical Compensation"); */
01033 /*       } */
01034 /*    } */
01035 /*    else */
01036 /*    { */
01037 /*       midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,  */
01038 /*                          "Subsequent computational values are without Optical Compensation"); */
01039 /*    } */
01040    
01041 /*    return; */
01042 }
01043 /*****************************************************************************/
01044 
01045 
01046  
01047 /******************************************************************************
01048 *               European Southern Observatory
01049 *            VLTI MIDI Data Reduction Software
01050 *
01051 * Module name:  getCompensatedBeams
01052 * Input/Output: See function arguments to avoid duplication
01053 * Description:  Computes compensated Interferometry channels
01054 *
01055 * History:
01056 * 06-Mar-06     (csabet) Created
01057 ******************************************************************************/
01058 void getCompensatedBeams (
01059     TransferRatios    *trr,                // In: Transfer Ratios
01060     ImageFormat        *format,            // In: Interf size parameters
01061     CompressedData    *compressedInterf,    // Ou: Pointer to the compressed interferometry data structure
01062     CompressedData    *compressedPhotomA,    // Ou: Pointer to the compressed photom A data structure
01063     CompressedData    *compressedPhotomB)    // Ou: Pointer to the compressed photom B data structure
01064 {
01065 
01066     //    Local Declarations
01067     //    ------------------
01068     const char    routine[] = "getCompensatedBeams";
01069     int            f, X, R;
01070     double compensate; 
01071     
01072     //    Algorithm
01073     //    ---------
01074     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01075     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01076 
01077     //    For all regions
01078     for (R = 0; R < format->numOfRegionsToProcess; R++)
01079     {
01080        //    For all valid channels (Wavelengths)
01081        for (X = 0; X < format->iXWidth; X++)
01082        {
01083           if (badChannelList[X])
01084              continue;
01085           
01086           for (f = 0; f < format->numOfFrames; f++)
01087           {
01088              //    Process good frames only
01089              if (!((compressedInterf->rejectList[X])[f]) )
01090              {
01091 /*                 At the moment no compensation in the fringe */
01092 /*                 channels should be done, but one has to compensate */
01093 /*                 the photA and photB photometry. photA and photB are */
01094 /*                 scaled to the total flux of telescope A and */
01095 /*                 telescope B used to produce the fringes(!!), respectively. */
01096 
01097                 if (R == 0)
01098                 {
01099                    if(trr->ka1[X] > 0 && trr->ka2[X] > 0 && trr->kb1[X] > 0 && trr->kb2[X] > 0)
01100                    {
01101 /*                       Total flux of telescope A and B */
01102 /*                       ((compressedPhotomA->iDispFringe[0])[X])[f]=(((compressedPhotomA->iDispFringe[0])[X])[f])*(1.+ trr->ka1[X] + trr->kb1[X]); */
01103 /*                       ((compressedPhotomB->iDispFringe[0])[X])[f]=(((compressedPhotomB->iDispFringe[0])[X])[f])*(1.+trr->ka2[X] + trr->kb2[X]); */
01104 
01105 /*                       Total flux of telescope A and B used to produce the fringes (!) */
01106                       ((compressedPhotomA->iDispFringe[0])[X])[f]=(((compressedPhotomA->iDispFringe[0])[X])[f])*(trr->ka1[X] + trr->kb1[X]);
01107                       ((compressedPhotomB->iDispFringe[0])[X])[f]=(((compressedPhotomB->iDispFringe[0])[X])[f])*(trr->ka2[X] + trr->kb2[X]);
01108                    }
01109                    else
01110                    {
01111                       ((compressedPhotomA->iDispFringe[0])[X])[f]=(((compressedPhotomA->iDispFringe[0])[X])[f]);
01112                       ((compressedPhotomB->iDispFringe[0])[X])[f]=(((compressedPhotomB->iDispFringe[0])[X])[f]);
01113                    }
01114 /*                      cpl_msg_info(cpl_func, "PhotA: %f PhotB: %f ka1: %f kb1 %f Fringe: %f", ((compressedPhotomA->iDispFringe[R])[X])[f], */
01115 /*                                    ((compressedPhotomB->iDispFringe[R])[X])[f], trr->ka1[X], trr->kb1[X],((compressedInterf->iDispFringe[R])[X])[f]); */
01116                    
01117                 }
01118                 continue;
01119                 
01120                 if (R == 0)
01121                 {
01122                    compensate=1. / ( (sqrt(((compressedPhotomA->iDispFringe[R])[X])[f] * 
01123                                            ((compressedPhotomB->iDispFringe[R])[X])[f])) *
01124                                      (sqrt(trr->ka1[X] * trr->kb1[X])) );
01125                    if(isnan(compensate) || isinf(compensate))
01126                    {
01127                       compensate=0.;
01128 /*                       cpl_msg_info(cpl_func, "PhotA: %f PhotB: %f ka1: %f kb1 %f ", ((compressedPhotomA->iDispFringe[R])[X])[f], */
01129 /*                                    ((compressedPhotomB->iDispFringe[R])[X])[f], trr->ka1[X], trr->kb1[X]); */
01130                    }
01131                    ((compressedInterf->iDispFringe[R])[X])[f] = ((compressedInterf->iDispFringe[R])[X])[f] * compensate;
01132                 }
01133                 else
01134                 {
01135                    compensate=1. / ( (sqrt(((compressedPhotomA->iDispFringe[R-1])[X])[f] * 
01136                                            ((compressedPhotomB->iDispFringe[R-1])[X])[f])) *
01137                                      (sqrt(trr->ka2[X] * trr->kb2[X])) );
01138                    
01139                    if(isnan(compensate) || isinf(compensate))
01140 /*                       if(1) */
01141                    {
01142                       compensate=0.;
01143 /*                       cpl_msg_info(cpl_func, "PhotA: %f PhotB: %f ka2: %f kb2 %f ", ((compressedPhotomA->iDispFringe[R-1])[X])[f], */
01144 /*                                    ((compressedPhotomB->iDispFringe[R-1])[X])[f], trr->ka2[X], trr->kb2[X]); */
01145                    }
01146                    
01147                    ((compressedInterf->iDispFringe[R])[X])[f] = 
01148                       ((compressedInterf->iDispFringe[R])[X])[f] * compensate;
01149                    
01150                 }
01151              }
01152              else
01153              {
01154                 if (diagnostic > 2)
01155                 {
01156                    sprintf (midiMessage, "Interferometry data in region %d, channel %3d, frame %4d cannot be compensated", 
01157                             R, X, f);
01158                    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01159                 }
01160              }
01161           }
01162        }
01163     }
01164     
01165     return;
01166 }
01167 /*****************************************************************************/

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