procKappa.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  procKappa.c
00007 * Description:  Contains routines for Transfer Ratios computations
00008 *
00009 * History:
00010 * 03-Dec-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 <stdlib.h>
00027 #include <stdlib.h>
00028 #include <math.h>
00029 #include "midiGlobal.h"
00030 #include "fft.h"
00031 #include "midiLib.h"
00032 #include "memoryHandling.h"
00033 #include "errorHandling.h"
00034 #include "diagnostics.h"
00035 #include "imageProcessing.h"
00036 #include "calibration.h"
00037 #include "statistics.h"
00038 #include "fitsAnalysisKappa.h"
00039 #include "preProcKappa.h"
00040 #include "procKappa.h"
00041 #include "procFrgHS.h"
00042 #include "createProdKappa.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:  procKappa
00062 * Input/Output: See function arguments to avoid duplication
00063 * Description:    This is the main routine for calculating Transfer Ratios
00064 *                If options->drsKap = 1, this process creates the Transfer Ratios file
00065 *                in current directory.
00066 *                If options->drsKap = 2, this process updates the Transfer Ratios file
00067 *                in the database as well as creating a file in the current directory
00068 *
00069 *                The relevant file structures are:
00070 *
00071 *                Photometry A files have the following configurations:
00072 *                DATA1 = Photometry A     data
00073 *                DATA2 = Interferometry     data
00074 *                DATA3 = Interferometry    data
00075 *                DATA4 = NULL
00076 *
00077 *                Photometry B files have the following configurations:
00078 *                DATA1 = NULL
00079 *                DATA2 = Interferometry     data
00080 *                DATA3 = Interferometry    data
00081 *                DATA4 = Photometry B     data
00082 *
00083 * History:
00084 * 05-Dec-05     (csabet) Created
00085 ******************************************************************************/
00086 void procKappa (
00087     MidiFiles    *fileNames,    // In: Pointer to midi files structure
00088     UserOptions    *options,    // In: Pointer to user options
00089     int            *error)        // Ou: Error status
00090 {
00091 
00092     //    Local Declarations
00093     //    ------------------
00094     const char      routine[] = "procKappa";
00095     ImageFormat        *formatInterfA=NULL, *formatInterfB=NULL, 
00096                     *formatPhotomA=NULL, *formatPhotomB=NULL;
00097     CompressedData    *compressedInterfA=NULL, *compressedInterfB=NULL, 
00098                     *compressedPhotomA=NULL, *compressedPhotomB=NULL;
00099     TransferRatios    *trr;
00100     FilterData        *filterInfo=NULL;
00101 
00102     //    Algorithm
00103     //    ---------
00104     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00105     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00106 
00107     //    Reset status
00108     *error = 0;
00109     if (options->processing == UNDISPERSED)
00110     {
00111         *error = 1;
00112         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00113             "'UNDISPERSED' is an invalid processing request for SCI_PHOT data");
00114         return;
00115     }
00116 
00117     //    Allocate memory
00118     formatInterfA = callocImageFormat ();
00119     formatInterfB = callocImageFormat ();
00120     formatPhotomA = callocImageFormat ();
00121     formatPhotomB = callocImageFormat ();
00122     
00123     //    Analyse Input files
00124     //    -------------------
00125     analyseFitsKappa (fileNames, formatInterfA, formatInterfB, 
00126         formatPhotomA, formatPhotomB, error);
00127     if (*error)
00128     {
00129         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse FITS files");
00130         freeImageFormat (formatInterfA);
00131         freeImageFormat (formatInterfB);
00132         freeImageFormat (formatPhotomA);
00133         freeImageFormat (formatPhotomB);
00134         return;
00135     }
00136 
00137     //    Reduce Data
00138     //    -----------
00139     //  Allocate memory for data compression
00140     compressedInterfA = callocMidiCompressed (formatInterfA);
00141     compressedInterfB = callocMidiCompressed (formatInterfB);
00142     compressedPhotomA = callocMidiCompressed (formatPhotomA);
00143     compressedPhotomB = callocMidiCompressed (formatPhotomB);
00144     badChannelList = (int *) calloc (formatInterfA->iXWidth, sizeof (int)); 
00145     filterInfo = callocFilterInfo();
00146 
00147     //    Compress data
00148     preProcKappa (options, filterInfo, fileNames, compressedInterfA, compressedInterfB, 
00149         compressedPhotomA, compressedPhotomB, formatInterfA, formatInterfB, formatPhotomA, 
00150         formatPhotomB, error);
00151     if (*error)
00152     {
00153         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress FITS files");
00154         freeCompressedData (formatInterfA, compressedInterfA);
00155         freeCompressedData (formatInterfB, compressedInterfB);
00156         freeCompressedData (formatPhotomA, compressedPhotomA);
00157         freeCompressedData (formatPhotomB, compressedPhotomB);
00158         free (badChannelList);
00159         freeImageFormat (formatInterfA);
00160         freeImageFormat (formatInterfB);
00161         freeImageFormat (formatPhotomA);
00162         freeImageFormat (formatPhotomB);
00163         freeFilterInfo (filterInfo);
00164         return;
00165     }
00166 
00167     //    Check Delay Lines for jumps within scans
00168     checkDelayLineJumps ("INTERFA", formatInterfA, compressedInterfA);
00169     checkDelayLineJumps ("INTERFB", formatInterfB, compressedInterfB);
00170     checkDelayLineJumps ("PHOTOMA", formatPhotomA, compressedPhotomA);
00171     checkDelayLineJumps ("PHOTOMB", formatPhotomB, compressedPhotomB);
00172 
00173     //    Check Delay Line Consistencies
00174     checkDelayLineConsistencies ("INTERFA", "PHOTOMA", compressedInterfA, compressedPhotomA, 
00175         formatInterfA, formatPhotomA, error);
00176     checkDelayLineConsistencies ("INTERFB", "PHOTOMB", compressedInterfB, compressedPhotomB, 
00177         formatInterfB, formatPhotomB, error);
00178     if (*error)
00179     {
00180         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Kappa calculation abandonned");
00181         freeCompressedData (formatInterfA, compressedInterfA);
00182         freeCompressedData (formatInterfB, compressedInterfB);
00183         freeCompressedData (formatPhotomA, compressedPhotomA);
00184         freeCompressedData (formatPhotomB, compressedPhotomB);
00185         free (badChannelList);
00186         freeImageFormat (formatInterfA);
00187         freeImageFormat (formatInterfB);
00188         freeImageFormat (formatPhotomA);
00189         freeImageFormat (formatPhotomB);
00190         freeFilterInfo (filterInfo);
00191         return;
00192     }
00193 
00194     //    Check Chopping Statistics. For QCLOG diagnostics
00195     //    ------------------------------------------------
00196     reportInterfChopping ("INTERFA", formatInterfA, compressedInterfA);
00197     reportInterfChopping ("INTERFB", formatInterfB, compressedInterfB);
00198     reportPhotomChopping ("PHOTOMA", formatPhotomA, compressedPhotomA);
00199     reportPhotomChopping ("PHOTOMB", formatPhotomB, compressedPhotomB);
00200     displayInterfChoppingDisp ("INTERFA", formatInterfA, compressedInterfA);
00201     displayInterfChoppingDisp ("INTERFB", formatInterfB, compressedInterfB);
00202     displayPhotomChoppingDisp ("PHOTOMA", formatPhotomA, compressedPhotomA);
00203     displayPhotomChoppingDisp ("PHOTOMB", formatPhotomB, compressedPhotomB);
00204 
00205     //    Remove Sky Background and display Target with Sky removed
00206     cleanupKappa (options->processing, formatInterfA, formatInterfB, formatPhotomA, formatPhotomB, 
00207         compressedInterfA, compressedInterfB, compressedPhotomA, compressedPhotomB, error);
00208     if (*error)
00209     {
00210         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Kappa calculation abandonned");
00211         freeCompressedData (formatInterfA, compressedInterfA);
00212         freeCompressedData (formatInterfB, compressedInterfB);
00213         freeCompressedData (formatPhotomA, compressedPhotomA);
00214         freeCompressedData (formatPhotomB, compressedPhotomB);
00215         free (badChannelList);
00216         freeImageFormat (formatInterfA);
00217         freeImageFormat (formatInterfB);
00218         freeImageFormat (formatPhotomA);
00219         freeImageFormat (formatPhotomB);
00220         freeFilterInfo (filterInfo);
00221         return;
00222     }
00223 
00224     //    Compute the Transfer Ratios using dispersed data
00225     trr = callocTransferRatios (formatInterfA->iXWidth);
00226     midiComputeKappaCoeff (compressedInterfA, compressedInterfB, compressedPhotomA, 
00227         compressedPhotomB, formatInterfA, formatInterfB, formatPhotomA, formatPhotomB, trr, error);
00228     if (*error > 1)
00229     {
00230         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Kappa calculation abandonned");
00231         freeCompressedData (formatInterfA, compressedInterfA);
00232         freeCompressedData (formatInterfB, compressedInterfB);
00233         freeCompressedData (formatPhotomA, compressedPhotomA);
00234         freeCompressedData (formatPhotomB, compressedPhotomB);
00235         free (badChannelList);
00236         freeImageFormat (formatInterfA);
00237         freeImageFormat (formatInterfB);
00238         freeImageFormat (formatPhotomA);
00239         freeImageFormat (formatPhotomB);
00240         freeTransferRatios (trr);
00241         freeFilterInfo (filterInfo);
00242         return;
00243     }
00244     
00245     //    Create product Transfer Ratios files
00246     //    ------------------------------------
00247     reportRejectList (formatInterfA, compressedInterfA);
00248 
00249 /* At the moment the Kappa matrix calculation is done by midi_kappamatrix */
00250 /* In order to avoid an empty dummy file MIDI_b1_spk.qc.dummy the next function  */
00251 /* is not executed any more */
00252 
00253 /*     createKappaProd (options->drsKap, fileNames, formatInterfA, trr, error); */
00254 
00255     //    Release memory used in this batch
00256     freeCompressedData (formatInterfA, compressedInterfA);
00257     freeCompressedData (formatInterfB, compressedInterfB);
00258     freeCompressedData (formatPhotomA, compressedPhotomA);
00259     freeCompressedData (formatPhotomB, compressedPhotomB);
00260     free (badChannelList);
00261     freeImageFormat (formatInterfA);
00262     freeImageFormat (formatInterfB);
00263     freeImageFormat (formatPhotomA);
00264     freeImageFormat (formatPhotomB);
00265     freeTransferRatios (trr);
00266     freeFilterInfo (filterInfo);
00267         
00268     return;
00269 }
00270 /*****************************************************************************/
00271 
00272 
00273 /******************************************************************************
00274 *               European Southern Observatory
00275 *            VLTI MIDI Data Reduction Software
00276 *
00277 * Module name:  cleanupKappa
00278 * Input/Output: See function arguments to avoid duplication
00279 * Description:  Removes sky etc from the data
00280 *
00281 * History:
00282 * 06-Dec-05     (csabet) Created
00283 ******************************************************************************/
00284 void cleanupKappa (
00285     enum ProcessingMode    processing,
00286     ImageFormat            *formatInterfA,        // In: Interf size parameters
00287     ImageFormat            *formatInterfB,        // In: Interf size parameters
00288     ImageFormat            *formatPhotomA,        // In: PhotA size parameters
00289     ImageFormat            *formatPhotomB,        // In: PhotB size parameters
00290     CompressedData        *compressedInterfA,    // Ou: Pointer to the compressed interferometry data structure
00291     CompressedData        *compressedInterfB,    // Ou: Pointer to the compressed interferometry data structure
00292     CompressedData        *compressedPhotomA,    // Ou: Pointer to the compressed photom A data structure
00293     CompressedData        *compressedPhotomB,    // Ou: Pointer to the compressed photom B data structure
00294     int                    *error)                // Ou: Error status
00295 {
00296 
00297     //    Local Declarations
00298     //    ------------------
00299     const char    routine[] = "cleanupKappa";
00300     int            transitions, f, X, R, n, localError;
00301     double        *array, fluxAve;
00302     float        *arrayF;
00303     char        *fileName, *title;
00304 
00305     //    Algorithm
00306     //    ---------
00307     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00308     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00309 
00310    cpl_msg_info(cpl_func,"\nCleaning up KAPPA files for batch  %d \n", batchNumber);
00311    cpl_msg_info(cpl_func,"--------------------------------- \n");
00312     fprintf (midiReportPtr, "\nCleaning up KAPPA files for batch  %d \n", batchNumber);
00313     fprintf (midiReportPtr, "--------------------------------- \n");
00314 
00315     //    Initialise
00316     localError = 0;
00317     *error = 0;
00318     transitions = 0;
00319     
00320     //    Check Interferometry A chopping cycle
00321     //    -------------------------------------    
00322     transitions = countTransitions ('T', formatInterfA->numOfFrames, compressedInterfA->tarType);
00323     sprintf (midiMessage, "Target Transitions for Interf A = %d", transitions); 
00324     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00325     if (transitions < 10)
00326     {
00327         sprintf (midiMessage, "Interferometry A data is not chopped"); 
00328         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00329         *error = 1;
00330     }
00331     
00332     //    Check Interferometry B chopping cycle
00333     //    -------------------------------------    
00334     transitions = countTransitions ('T', formatInterfB->numOfFrames, compressedInterfB->tarType);
00335     sprintf (midiMessage, "Target Transitions for Interf B = %d", transitions); 
00336     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00337     if (transitions < 10)
00338     {
00339         sprintf (midiMessage, "Interferometry B data is not chopped"); 
00340         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00341         *error = 1;
00342     }
00343 
00344     //    Check PhotomA chopping cycle
00345     //    ----------------------------    
00346     transitions = countTransitions ('T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
00347     sprintf (midiMessage, "Target Transitions for Photom A = %d", transitions); 
00348     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00349     if (transitions < 10)
00350     {
00351         sprintf (midiMessage, "Photometry A data is not chopped"); 
00352         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00353         *error = 1;
00354     }
00355 
00356     //    Check PhotomB chopping cycle
00357     //    ----------------------------
00358     transitions = countTransitions ('T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
00359     sprintf (midiMessage, "Target Transitions for Photom B = %d", transitions); 
00360     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00361     if (transitions < 10)
00362     {
00363         sprintf (midiMessage, "Photometry B data is not chopped"); 
00364         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00365         *error = 1;
00366     }
00367 
00368     //    if No error so far
00369     if (!(*error))
00370     {
00371         formatInterfA->chopped = 1;
00372         formatInterfB->chopped = 1;
00373         formatPhotomA->chopped = 1;
00374         formatPhotomB->chopped = 1;
00375     }
00376     else 
00377     {
00378         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot cleanup KAPPA files");
00379         return;
00380     }
00381  
00382     //    Correct benign tarType inconsistencies
00383     //    --------------------------------------
00384     correctTarType ("INTERFA", compressedInterfA->tarType, compressedInterfA->time, formatInterfA->numOfFrames, &localError);
00385     if (localError) *error = 1;
00386     correctTarType ("INTERFB", compressedInterfB->tarType, compressedInterfB->time, formatInterfB->numOfFrames, &localError);
00387     if (localError) *error = 1;
00388     correctTarType ("PHOTOMA", compressedPhotomA->tarType, compressedPhotomA->time, formatPhotomA->numOfFrames, &localError);
00389     if (localError) *error = 1;
00390     correctTarType ("PHOTOMB", compressedPhotomB->tarType, compressedPhotomB->time, formatPhotomB->numOfFrames, &localError);
00391     if (*error || localError)
00392     {
00393         *error = 1;
00394         return;
00395     }
00396     
00397     //    Remove sky background
00398     //    ---------------------
00399     removeSkyBackground ("INTERFA", processing, formatInterfA, compressedInterfA, &localError);
00400     if (localError) *error = 1;
00401     removeSkyBackground ("INTERFB", processing, formatInterfB, compressedInterfB, &localError);
00402     if (localError) *error = 1;
00403     removeSkyBackground ("PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
00404     if (localError) *error = 1;
00405     removeSkyBackground ("PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
00406     if (formatInterfA->numOfFrames != formatPhotomA->numOfFrames || 
00407         formatInterfA->numOfFrames != formatPhotomB->numOfFrames ||
00408         formatInterfA->numOfFrames != formatInterfB->numOfFrames || *error || localError)
00409     {
00410         *error = 1;
00411         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unequal numOfFrames");
00412         return;
00413     }
00414 
00415     //    Reject frames whose flux are <= 0 or are not 'T'
00416     for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
00417     {
00418         for (X = 0; X < formatInterfA->iXWidth; X++)
00419         {
00420             if (badChannelList[X])
00421                 continue;
00422                     
00423             for (f = 0; f < formatInterfA->numOfFrames; f++)
00424             {
00425                 if ((compressedInterfA->tarType[f] == 'T') && 
00426                     (((compressedInterfA->iDispFringe)[R])[X])[f] <= 0.0)
00427                 {
00428                     compressedInterfA->rejectList[X][f] |= BSL_DATA_ERROR;
00429                     if (diagnostic > 3) 
00430                     {
00431                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         InterfA %10.2f", 
00432                             R, X, f, (((compressedInterfA->iDispFringe)[R])[X])[f]);
00433                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00434                     }
00435                 }
00436                 //    Reject frames which are not 'T'
00437                 if (compressedInterfA->tarType[f] != 'T')
00438                     compressedInterfA->rejectList[X][f] |= BSL_SKY_SCAN;
00439             }
00440         }
00441     }
00442 
00443     for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
00444     {
00445         for (X = 0; X < formatInterfB->iXWidth; X++)
00446         {
00447             if (badChannelList[X])
00448                 continue;
00449                     
00450             for (f = 0; f < formatInterfB->numOfFrames; f++)
00451             {
00452                 if ((compressedInterfB->tarType[f] == 'T') && 
00453                     (((compressedInterfB->iDispFringe)[R])[X])[f] <= 0.0)
00454                 {
00455                     compressedInterfB->rejectList[X][f] |= BSL_DATA_ERROR;
00456                     if (diagnostic > 3) 
00457                     {
00458                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         InterfB %10.2f", 
00459                             R, X, f, (((compressedInterfB->iDispFringe)[R])[X])[f]);
00460                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00461                     }
00462                 }
00463                 if (compressedInterfB->tarType[f] != 'T')
00464                     compressedInterfB->rejectList[X][f] |= BSL_SKY_SCAN;
00465             }
00466         }
00467     }
00468 
00469     for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00470     {
00471         for (X = 0; X < formatPhotomA->iXWidth; X++)
00472         {
00473             if (badChannelList[X])
00474                 continue;
00475                     
00476             for (f = 0; f < formatPhotomA->numOfFrames; f++)
00477             {
00478                 if ((compressedPhotomA->tarType[f] == 'T') && 
00479                     (((compressedPhotomA->iDispFringe)[R])[X])[f] <= 0.0)
00480                 {
00481                     compressedPhotomA->rejectList[X][f] |= BSL_DATA_ERROR;
00482                     if (diagnostic > 3) 
00483                     {
00484                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         PhotomA %10.2f", 
00485                             R, X, f, (((compressedPhotomA->iDispFringe)[R])[X])[f]);
00486                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00487                     }
00488                 }
00489                 if (compressedPhotomA->tarType[f] != 'T')
00490                     compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
00491             }
00492         }
00493     }
00494 
00495     for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00496     {
00497         for (X = 0; X < formatPhotomB->iXWidth; X++)
00498         {
00499             if (badChannelList[X])
00500                 continue;
00501                     
00502             for (f = 0; f < formatPhotomB->numOfFrames; f++)
00503             {
00504                 if ((compressedPhotomB->tarType[f] == 'T') && 
00505                     (((compressedPhotomB->iDispFringe)[R])[X])[f] <= 0.0)
00506                 {
00507                     compressedPhotomB->rejectList[X][f] |= BSL_DATA_ERROR;
00508                     if (diagnostic > 3) 
00509                     {
00510                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         PhotomB %10.2f", 
00511                             R, X, f, (((compressedPhotomB->iDispFringe)[R])[X])[f]);
00512                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00513                     }
00514                 }
00515                 if (compressedPhotomB->tarType[f] != 'T')
00516                     compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
00517             }
00518         }
00519     }
00520     
00521     //    Display data with background removed
00522     if (plotFile && diagnostic > 1)
00523     {
00524         array = (double *) calloc (formatInterfA->numOfFrames, sizeof (double));
00525 
00526         fluxAve = 0.0;
00527         n = 0;
00528         for (f = 0; f < formatInterfA->numOfFrames; f++)
00529         {
00530             if (compressedInterfA->tarType[f] == 'T')
00531             {
00532                 array[n] = compressedInterfA->iFringe1[f];
00533                 fluxAve += array[n];
00534                 n++;
00535             }
00536         }
00537         fluxAve /= n;
00538        cpl_msg_info(cpl_func,"InterfA DATA2 Average Flux = %f \n", fluxAve);
00539         midiCreatePlotFileDouble2D ("InterfACleanDATA2", "Interferometry A DATA2 (Sky Removed)", 
00540             "Frame", "Flux", 0, array, 0, n, 1);
00541 
00542         fluxAve = 0.0;
00543         n = 0;
00544         for (f = 0; f < formatInterfA->numOfFrames; f++)
00545         {
00546             if (compressedInterfA->tarType[f] == 'T')
00547             {
00548                 array[n] = compressedInterfA->iFringe2[f];
00549                 fluxAve += array[n];
00550                 n++;
00551             }
00552         }
00553         fluxAve /= n;
00554        cpl_msg_info(cpl_func,"InterfA DATA3 Average Flux = %f \n", fluxAve);
00555         midiCreatePlotFileDouble2D ("InterfACleanDATA3", "Interferometry A DATA3 (Sky Removed)", 
00556             "Frame", "Flux", 0, array, 0, n, 1);
00557 
00558         n = 0;
00559         for (f = 0; f < formatInterfA->numOfFrames; f++)
00560         {
00561             if (compressedInterfA->tarType[f] == 'T')
00562             {
00563                 array[n] = compressedInterfA->iFringe[f];
00564                 n++;
00565             }
00566         }
00567         midiCreatePlotFileDouble2D ("InterfACleanDATA2_DATA3", "Interferometry A DATA2-DATA3 (Sky Removed)", 
00568             "Frame", "Flux", 0, array, 0, n, 1);
00569 
00570         fluxAve = 0.0;
00571         n = 0;
00572         for (f = 0; f < formatInterfB->numOfFrames; f++)
00573         {
00574             if (compressedInterfB->tarType[f] == 'T')
00575             {
00576                 array[n] = compressedInterfB->iFringe1[f];
00577                 fluxAve += array[n];
00578                 n++;
00579             }
00580         }
00581         fluxAve /= n;
00582        cpl_msg_info(cpl_func,"InterfB DATA2 Average Flux = %f \n", fluxAve);
00583         midiCreatePlotFileDouble2D ("InterfBCleanDATA2", "Interferometry B DATA2 (Sky Removed)", 
00584             "Frame", "Flux", 0, array, 0, n, 1);
00585 
00586         fluxAve = 0.0;
00587         n = 0;
00588         for (f = 0; f < formatInterfB->numOfFrames; f++)
00589         {
00590             if (compressedInterfB->tarType[f] == 'T')
00591             {
00592                 array[n] = compressedInterfB->iFringe2[f];
00593                 fluxAve += array[n];
00594                 n++;
00595             }
00596         }
00597         fluxAve /= n;
00598        cpl_msg_info(cpl_func,"InterfB DATA3 Average Flux = %f \n", fluxAve);
00599         midiCreatePlotFileDouble2D ("InterfBCleanDATA3", "Interferometry B DATA3 (Sky Removed)", 
00600             "Frame", "Flux", 0, array, 0, n, 1);
00601 
00602         n = 0;
00603         for (f = 0; f < formatInterfB->numOfFrames; f++)
00604         {
00605             if (compressedInterfB->tarType[f] == 'T')
00606             {
00607                 array[n] = compressedInterfB->iFringe[f];
00608                 n++;
00609             }
00610         }
00611         midiCreatePlotFileDouble2D ("InterfBCleanDATA2_DATA3", "Interferometry B DATA2-DATA3 (Sky Removed)", 
00612             "Frame", "Flux", 0, array, 0, n, 1);
00613 
00614         fluxAve = 0.0;
00615         n = 0;
00616         for (f = 0; f < formatPhotomA->numOfFrames; f++)
00617         {
00618             if (compressedPhotomA->tarType[f] == 'T')
00619             {
00620                 array[n] = compressedPhotomA->iFringe1[f];
00621                 fluxAve += array[n];
00622                 n++;
00623             }
00624         }
00625         fluxAve /= n;
00626        cpl_msg_info(cpl_func,"PhotomA DATA1 Average Flux = %f \n", fluxAve);
00627         midiCreatePlotFileDouble2D ("PhotomACleanDATA1", "Photometry A DATA1 (Sky Removed)", 
00628             "Frame", "Flux", 0, array, 0, n, 1);
00629 
00630         fluxAve = 0.0;
00631         n = 0;
00632         for (f = 0; f < formatPhotomB->numOfFrames; f++)
00633         {
00634             if (compressedPhotomB->tarType[f] == 'T')
00635             {
00636                 array[n] = compressedPhotomB->iFringe1[f];
00637                 fluxAve += array[n];
00638                 n++;
00639             }
00640         }
00641         fluxAve /= n;
00642        cpl_msg_info(cpl_func,"PhotomB DATA4 Average Flux = %f \n", fluxAve);
00643         midiCreatePlotFileDouble2D ("PhotomBCleanDATA4", "Photometry B DATA4 (Sky Removed)", 
00644             "Frame", "Flux", 0, array, 0, n, 1);
00645 
00646         free (array);
00647     }
00648 
00649     //    Display for each channel
00650     if (plotFile && diagnostic > 4)
00651     {
00652         arrayF = (float *) calloc (formatInterfA->numOfFrames, sizeof (float));
00653         fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00654         title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00655         
00656         for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
00657         {
00658             for (X = 0; X < formatInterfA->iXWidth; X++)
00659             {
00660                 if (badChannelList[X])
00661                     continue;
00662                     
00663                 n = 0;
00664                 for (f = 0; f < formatInterfA->numOfFrames; f++)
00665                 {
00666                     if (compressedInterfA->tarType[f] == 'T' && !(compressedInterfA->rejectList[X][f]))
00667                     {
00668                         arrayF[n] = compressedInterfA->iDispFringe[R][X][f];
00669                         n++;
00670                     }
00671                 }
00672                 sprintf (fileName, "InterfACleanDATA%d_X%d", R+2, X+1);
00673                 sprintf (title, "Interferometry A DATA%d for Channel %d (Sky Removed)", R+2, X+1);
00674                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00675             }
00676         }
00677         for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
00678         {
00679             for (X = 0; X < formatInterfB->iXWidth; X++)
00680             {
00681                 if (badChannelList[X])
00682                     continue;
00683                     
00684                 n = 0;
00685                 for (f = 0; f < formatInterfB->numOfFrames; f++)
00686                 {
00687                     if (compressedInterfB->tarType[f] == 'T' && !(compressedInterfB->rejectList[X][f]))
00688                     {
00689                         arrayF[n] = compressedInterfB->iDispFringe[R][X][f];
00690                         n++;
00691                     }
00692                 }
00693                 sprintf (fileName, "InterfBCleanDATA%d_X%d", R+2, X+1);
00694                 sprintf (title, "Interferometry B DATA%d for Channel %d (Sky Removed)", R+2, X+1);
00695                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00696             }
00697         }
00698         for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00699         {
00700             for (X = 0; X < formatPhotomA->iXWidth; X++)
00701             {
00702                 if (badChannelList[X])
00703                     continue;
00704                     
00705                 n = 0;
00706                 for (f = 0; f < formatPhotomA->numOfFrames; f++)
00707                 {
00708                     if (compressedPhotomA->tarType[f] == 'T' && !(compressedPhotomA->rejectList[X][f]))
00709                     {
00710                         arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
00711                         n++;
00712                     }
00713                 }
00714                 sprintf (fileName, "PhotomACleanDATA1_X%d", X+1);
00715                 sprintf (title, "Photometry A DATA1 for Channel %d (Sky Removed)", X+1);
00716                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00717             }
00718         }
00719         for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00720         {
00721             for (X = 0; X < formatPhotomB->iXWidth; X++)
00722             {
00723                 if (badChannelList[X])
00724                     continue;
00725                     
00726                 n = 0;
00727                 for (f = 0; f < formatPhotomB->numOfFrames; f++)
00728                 {
00729                     if (compressedPhotomB->tarType[f] == 'T' && !(compressedPhotomB->rejectList[X][f]))
00730                     {
00731                         arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
00732                         n++;
00733                     }
00734                 }
00735                 sprintf (fileName, "PhotomBCleanDATA4_X%d", X+1);
00736                 sprintf (title, "Photometry B DATA4 for Channel %d (Sky Removed)", X+1);
00737                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00738             }
00739         }
00740         free (fileName);
00741         free (title);
00742         free (arrayF);
00743     }
00744 
00745     //    Finalise rejection list
00746     finaliseRejectListPhotom ("Interferometry A", formatInterfA, compressedInterfA, error);
00747     if (*error) return;
00748     finaliseRejectListPhotom ("Interferometry B", formatInterfB, compressedInterfB, error);
00749     if (*error) return;
00750     finaliseRejectListPhotom ("Photometry A", formatPhotomA, compressedPhotomA, error);
00751     if (*error) return;
00752     finaliseRejectListPhotom ("Photometry B", formatPhotomB, compressedPhotomB, error);
00753     if (*error) return;
00754 
00755     return;
00756 }
00757 /*****************************************************************************/
00758 
00759 
00760 /******************************************************************************
00761 *               European Southern Observatory
00762 *            VLTI MIDI Data Reduction Software
00763 *
00764 * Module name:  midiComputeKappaCoeff
00765 * Input/Output: See function arguments to avoid duplication
00766 * Description:  Computes the transfer matrix. The FITS files have the following
00767 *                configuration:
00768 *
00769 *                Photometry A files (AOPEN) have the following configurations:
00770 *                DATA1 = Photometry A     PA    data
00771 *                DATA2 = Interferometry     IA1    data
00772 *                DATA3 = Interferometry    IA2    data
00773 *                DATA4 = NULL            
00774 *
00775 *                Photometry B files (BOPEN) have the following configurations:
00776 *                DATA1 = NULL
00777 *                DATA2 = Interferometry     IB1    data
00778 *                DATA3 = Interferometry    IB2    data
00779 *                DATA4 = Photometry B     PB    data
00780 *                
00781 *                In general we have:
00782 *                PA = k11 * TFA + k12 * TFB
00783 *                I1 = k21 * TFA + k22 * TFB
00784 *                I2 = k31 * TFA + k32 * TFB
00785 *                PB = k41 * TFA + k42 * TFB
00786 *
00787 *                Where
00788 *                TFA and TFB, the total flux through the telescopes are given by:
00789 *                TFA = PA + IA1 + IA2
00790 *                TFB = PB + IB1 + IB2
00791 *
00792 *                When AOPEN then B = 0. Hence
00793 *                k11 = PA / TFA
00794 *                k12 = 0
00795 *                k21 = IA1 / TFA
00796 *                k31 = IA2 / TFA
00797 *
00798 *                When BOPEN then TFA = 0. Hence
00799 *                k22 = IB1 / TFB
00800 *                k32 = IB2 / TFB
00801 *                k41 = 0
00802 *                k42 = PB / TFB
00803 *
00804 *                And the ratios are:
00805 *                krA1 = k21/k11
00806 *                krA2 = k31/k11
00807 *                krB1 = k22/k42
00808 *                krB2 = k32/k42
00809 *
00810 * History:
00811 * 06-Oct-05     (csabet) Created
00812 ******************************************************************************/
00813 void midiComputeKappaCoeff (
00814     CompressedData    *compressedIA,    // In: Compressed interferometry data
00815     CompressedData    *compressedIB,    // In: Compressed interferometry data
00816     CompressedData    *compressedPA,    // In: Photometry data
00817     CompressedData    *compressedPB,    // In: Photometry data
00818     ImageFormat        *formatIA,        // In: Image size of interferometry data
00819     ImageFormat        *formatIB,        // In: Image size of interferometry data
00820     ImageFormat        *formatPA,        // In: Applies to A photom files
00821     ImageFormat        *formatPB,        // In: Applies to B photom files
00822     TransferRatios    *trr,            // Ou: Transfer Ratios
00823     int                *error)            // Ou: Status
00824 {
00825  
00826     //  Local Declarations
00827     //    ------------------
00828     const char            routine[] = "midiComputeKappaCoeff";
00829     int                    f, x, numOfFrames, nzc, minFrames;
00830     float                *kappArray,    *totalFluxA, *totalFluxB, variance;
00831     KappaCoefficients    *kc;
00832     char                *string;
00833  
00834     //    Algorithm
00835     //    ---------
00836     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00837     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00838  
00839    cpl_msg_info(cpl_func,"\nComputing the Kappa Coefficients for batch  %d \n", batchNumber);
00840    cpl_msg_info(cpl_func,"------------------------------------------ \n");
00841     fprintf (midiReportPtr, "\nComputing the Kappa Coefficients for batch  %d \n", batchNumber);
00842     fprintf (midiReportPtr, "------------------------------------------ \n");
00843  
00844     //  Reset status
00845     *error = 0;
00846     
00847     //    Exception
00848     if ((formatIA->numOfFrames != formatIB->numOfFrames) || 
00849         (formatIA->numOfFrames != formatPA->numOfFrames) ||
00850         (formatIA->numOfFrames != formatPB->numOfFrames) ||
00851         (formatPA->numOfFrames != formatPB->numOfFrames))
00852     {
00853         *error = 2;
00854         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent frame counts");
00855         return;
00856     }
00857     numOfFrames = formatIA->numOfFrames;
00858     minFrames = MIN_FRAMES_PER_SCAN * compressedIA->numOfTargetChops;
00859 
00860 
00861     //    Allocate memory
00862     kappArray = (float *) calloc (numOfFrames, sizeof (float));
00863     totalFluxA = (float *) calloc (numOfFrames, sizeof (float));
00864     totalFluxB = (float *) calloc (numOfFrames, sizeof (float));
00865     string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00866     kc = callocKappaCoefficients (formatIA->iXWidth);
00867     
00868     //    Compute kappa coefficients for each wavelength
00869     //    ----------------------------------------------
00870    cpl_msg_info(cpl_func,"Channel    k11 sig11     k21 sig21     k31 sig31     k22 sig22     k32 sig32     k42 sig42 \n");
00871     fprintf (midiReportPtr, "Channel    k11 sig11     k21 sig21     k31 sig31     k22 sig22     k32 sig32     k42 sig42 \n");
00872     for (x = 0; x < formatIA->iXWidth; x++)
00873     {
00874         if (badChannelList[x])
00875         {
00876            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
00877             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
00878             continue;
00879         }
00880             
00881         //    Compute total flux through each telescope
00882         for (f = 0; f < numOfFrames; f++)
00883         {
00884             if ((compressedIA->tarType[f] == 'T') && 
00885                 (compressedPA->tarType[f] == 'T') &&
00886                 !(compressedIA->rejectList[x][f]) &&
00887                 !(compressedPA->rejectList[x][f]))
00888             {
00889                 totalFluxA[f] = compressedIA->iDispFringe[0][x][f] + compressedIA->iDispFringe[1][x][f] + 
00890                     compressedPA->iDispFringe[0][x][f];
00891                 
00892             }
00893             if ((compressedIB->tarType[f] == 'T') && 
00894                 (compressedPB->tarType[f] == 'T') && 
00895                 !(compressedIB->rejectList[x][f]) &&
00896                 !(compressedPB->rejectList[x][f]))
00897             {
00898                 totalFluxB[f] = compressedIB->iDispFringe[0][x][f] + compressedIB->iDispFringe[1][x][f] + 
00899                     compressedPB->iDispFringe[0][x][f];
00900             }
00901         }
00902         
00903         //    kappa11 computation
00904         nzc = -1;
00905         for (f = 0; f < numOfFrames; f++)
00906         {
00907             if ((compressedPA->tarType[f] == 'T') && 
00908                 (((compressedPA->iDispFringe[0])[x])[f] > 0.0) &&
00909                 (totalFluxA[f] > 0.0) && 
00910                 !(compressedPA->rejectList[x][f]))
00911             {
00912                 nzc++;
00913                 kappArray[nzc] = ((compressedPA->iDispFringe[0])[x])[f] / totalFluxA[f];
00914             }
00915         }
00916         if (nzc > minFrames)
00917         {
00918             kc->k11[x] = signalMean (kappArray, 0, nzc);
00919             variance = signalVariance (kappArray, 0, nzc, &(kc->sig11[x]));
00920             if (plotFile && diagnostic > 2)
00921             {
00922                 sprintf (string, "Kappa11 Array, Channel %d", x);
00923                 midiCreatePlotFile2D ("Kappa11Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
00924             }
00925         }
00926         else
00927         {
00928             badChannelList[x] |= BSL_DATA_ERROR;
00929            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
00930             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
00931             continue;
00932         }
00933 
00934         //    kappa21 computation
00935         nzc = -1;
00936         for (f = 0; f < numOfFrames; f++)
00937         {
00938             if ((compressedIA->tarType[f] == 'T') && 
00939                 (((compressedIA->iDispFringe[0])[x])[f] > 0.0) &&
00940                 (totalFluxA[f] > 0.0) &&
00941                 !(compressedIA->rejectList[x][f]))
00942             {
00943                 nzc++;
00944                 kappArray[nzc] = ((compressedIA->iDispFringe[0])[x])[f] / totalFluxA[f];
00945             }
00946         }
00947         if (nzc > minFrames)
00948         {
00949             kc->k21[x] = signalMean (kappArray, 0, nzc);
00950             variance = signalVariance (kappArray, 0, nzc, &(kc->sig21[x]));
00951             if (plotFile && diagnostic > 2)
00952             {
00953                 sprintf (string, "Kappa21 Array, Channel %d", x);
00954                 midiCreatePlotFile2D ("Kappa21Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
00955             }
00956         }
00957         else
00958         {
00959             badChannelList[x] |= BSL_DATA_ERROR;
00960            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
00961             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
00962             continue;
00963         }
00964 
00965         //    kappa31 computation
00966         nzc = -1;
00967         for (f = 0; f < numOfFrames; f++)
00968         {
00969             if ((compressedIA->tarType[f] == 'T') && 
00970                 (((compressedIA->iDispFringe[1])[x])[f] > 0.0) &&
00971                 (totalFluxA[f] > 0.0) &&
00972                 !(compressedIA->rejectList[x][f]))
00973             {
00974                 nzc++;
00975                 kappArray[nzc] = ((compressedIA->iDispFringe[1])[x])[f] / totalFluxA[f];
00976             }
00977         }
00978         if (nzc > minFrames)
00979         {
00980             kc->k31[x] = signalMean (kappArray, 0, nzc);
00981             variance = signalVariance (kappArray, 0, nzc, &(kc->sig31[x]));
00982             if (plotFile && diagnostic > 2)
00983             {
00984                 sprintf (string, "Kappa31 Array, Channel %d", x);
00985                 midiCreatePlotFile2D ("Kappa31Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
00986             }
00987         }
00988         else
00989         {
00990             badChannelList[x] |= BSL_DATA_ERROR;
00991            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
00992             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
00993             continue;
00994         }
00995 
00996         //    kappa22 computation
00997         nzc = -1;
00998         for (f = 0; f < numOfFrames; f++)
00999         {
01000             if ((compressedIB->tarType[f] == 'T') && 
01001                 (((compressedIB->iDispFringe[0])[x])[f] > 0.0) &&
01002                 (totalFluxB[f] > 0.0) &&
01003                 !(compressedIB->rejectList[x][f]))
01004             {
01005                 nzc++;
01006                 kappArray[nzc] = ((compressedIB->iDispFringe[0])[x])[f] / totalFluxB[f];
01007             }
01008         }
01009         if (nzc > minFrames)
01010         {
01011             kc->k22[x] = signalMean (kappArray, 0, nzc);
01012             variance = signalVariance (kappArray, 0, nzc, &(kc->sig22[x]));
01013             if (plotFile && diagnostic > 2)
01014             {
01015                 sprintf (string, "Kappa22 Array, Channel %d", x);
01016                 midiCreatePlotFile2D ("Kappa22Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
01017             }
01018         }
01019         else
01020         {
01021             badChannelList[x] |= BSL_DATA_ERROR;
01022            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
01023             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
01024             continue;
01025         }
01026 
01027         //    kappa32 computation
01028         nzc = -1;
01029         for (f = 0; f < numOfFrames; f++)
01030         {
01031             if ((compressedIB->tarType[f] == 'T') && 
01032                 (((compressedIB->iDispFringe[1])[x])[f] > 0.0) &&
01033                 (totalFluxB[f] > 0.0) &&
01034                 !(compressedIB->rejectList[x][f]))
01035             {
01036                 nzc++;
01037                 kappArray[nzc] = ((compressedIB->iDispFringe[1])[x])[f] / totalFluxB[f];
01038             }
01039         }
01040         if (nzc > minFrames)
01041         {
01042             kc->k32[x] = signalMean (kappArray, 0, nzc);
01043             variance = signalVariance (kappArray, 0, nzc, &(kc->sig32[x]));
01044             if (plotFile && diagnostic > 2)
01045             {
01046                 sprintf (string, "Kappa32 Array, Channel %d", x);
01047                 midiCreatePlotFile2D ("Kappa32Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
01048             }
01049         }
01050         else
01051         {
01052             badChannelList[x] |= BSL_DATA_ERROR;
01053            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
01054             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
01055             continue;
01056         }
01057 
01058         //    kappa42 computation
01059         nzc = -1;
01060         for (f = 0; f < numOfFrames; f++)
01061         {
01062             if ((compressedPB->tarType[f] == 'T') && 
01063                 (((compressedPB->iDispFringe[0])[x])[f] > 0.0) &&
01064                 (totalFluxB[f] > 0.0) &&
01065                 !(compressedPB->rejectList[x][f]))
01066             {
01067                 nzc++;
01068                 kappArray[nzc] = ((compressedPB->iDispFringe[0])[x])[f] / totalFluxB[f];
01069             }
01070         }
01071         if (nzc > minFrames)
01072         {
01073             kc->k42[x] = signalMean (kappArray, 0, nzc);
01074             variance = signalVariance (kappArray, 0, nzc, &(kc->sig42[x]));
01075             if (plotFile && diagnostic > 2)
01076             {
01077                 sprintf (string, "Kappa42 Array, Channel %d", x);
01078                 midiCreatePlotFile2D ("Kappa42Array", string, "Frame", "Flux", 1, kappArray, 0, nzc, 1, 1);
01079             }
01080         }
01081         else
01082         {
01083             badChannelList[x] |= BSL_DATA_ERROR;
01084            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
01085             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
01086             continue;
01087         }
01088        cpl_msg_info(cpl_func,"%3d        %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f  \n", 
01089             x, kc->k11[x], kc->sig11[x], kc->k21[x], kc->sig21[x], kc->k31[x], kc->sig31[x], kc->k22[x], kc->sig22[x], 
01090             kc->k32[x], kc->sig32[x], kc->k42[x], kc->sig42[x]);
01091         fprintf (midiReportPtr, "%3d        %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f     %1.2f %1.2f  \n", 
01092             x, kc->k11[x], kc->sig11[x], kc->k21[x], kc->sig21[x], kc->k31[x], kc->sig31[x], kc->k22[x], kc->sig22[x], 
01093             kc->k32[x], kc->sig32[x], kc->k42[x], kc->sig42[x]);
01094     }
01095 
01096     //    Smooth the coefficients. Not sure if this is the right moment for filtering. A
01097     //    more suitable place would probably be filtering "kappArray[nzc]" at frame ensemble level.
01098     smoothKappaCoefficiets (formatIA, kc, error);
01099     if (*error)
01100     {
01101         freeKappaCoefficients (kc);
01102         free (kappArray);
01103         free (totalFluxA);
01104         free (totalFluxB);
01105         free (string);
01106         return;
01107     }
01108     
01109     //    Now compute the ratios
01110     computeTransferRatios (formatIA, kc, trr, error);
01111 
01112     //    Release memory
01113     freeKappaCoefficients (kc);
01114     free (kappArray);
01115     free (totalFluxA);
01116     free (totalFluxB);
01117     free (string);
01118 
01119     return;
01120 }
01121 /*****************************************************************************/
01122 
01123 
01124 
01125 /******************************************************************************
01126 *               European Southern Observatory
01127 *            VLTI MIDI Data Reduction Software
01128 *
01129 * Module name:  computeTransferRatios
01130 * Input/Output: See function arguments to avoid duplication
01131 * Description:  Computes the transfer ratios and the associated errors as follows:
01132 *
01133 *                krA1 = k21/k11
01134 *                krA2 = k31/k11
01135 *                krB1 = k22/k42
01136 *                krB2 = k32/k42
01137 *
01138 * History:
01139 * 02-Feb-06     (csabet) Created
01140 ******************************************************************************/
01141 void computeTransferRatios (
01142     ImageFormat            *format,    // In: Image size of interferometry data
01143     KappaCoefficients    *kc,        // In: Kappa Coefficients
01144     TransferRatios        *tr,        // Ou: Transfer Ratios
01145     int                    *error)        // Ou: Status
01146 {
01147  
01148     //  Local Declarations
01149     //    ------------------
01150     const char    routine[] = "computeTransferRatios";
01151     int            x, n;
01152      
01153     //    Algorithm
01154     //    ---------
01155     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01156     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01157  
01158     //    Reset
01159     *error = 0;
01160     tr->exists = 0;
01161     
01162     //    Check sanity. Kappa coefficients must lie between 0.0 and 1.0 exclusively
01163    cpl_msg_info(cpl_func,"Channel    ka1 siga1         ka2 siga2         kb1 sigb1         kb2 sigb2 \n");
01164     fprintf (midiReportPtr, "Channel    ka1 siga1         ka2 siga2         kb1 sigb1         kb2 sigb2 \n");
01165     n = 0;
01166     for (x = 0; x < format->iXWidth; x++)
01167     {
01168         if (badChannelList[x])
01169         {
01170            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
01171             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
01172             continue;
01173         }
01174             
01175         if (((kc->k11[x] <= 0.0) || (kc->k11[x] >= 1.0)) || 
01176             ((kc->k21[x] <= 0.0) || (kc->k21[x] >= 1.0)) ||
01177             ((kc->k31[x] <= 0.0) || (kc->k31[x] >= 1.0)) || 
01178             ((kc->k22[x] <= 0.0) || (kc->k22[x] >= 1.0)) ||
01179             ((kc->k32[x] <= 0.0) || (kc->k32[x] >= 1.0)) || 
01180             ((kc->k42[x] <= 0.0) || (kc->k42[x] >= 1.0)))
01181         {
01182             badChannelList[x] |= BSL_DATA_ERROR;
01183             if (diagnostic > 1)
01184             {
01185                 sprintf (midiMessage, 
01186                     "Cannot compute Transfer Ratios for Channel %3d. Values of Kappa Matrix are unreasonable", x);
01187                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01188             }
01189            cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", x, UNAV);
01190             fprintf (midiReportPtr, "%3d    <-- %s --> \n", x, UNAV);
01191         }
01192         else
01193         {
01194             n++;
01195             tr->ka1[x] = kc->k21[x] / kc->k11[x];
01196             tr->siga1[x] = kc->sig21[x] / fabs(kc->k11[x]) + kc->sig11[x] * (fabs(kc->k21[x]) / (kc->k11[x] * kc->k11[x]));
01197             tr->ka2[x] = kc->k31[x] / kc->k11[x];
01198             tr->siga2[x] = kc->sig31[x] / fabs(kc->k11[x]) + kc->sig11[x] * (fabs(kc->k31[x]) / (kc->k11[x] * kc->k11[x]));
01199             tr->kb1[x] = kc->k22[x] / kc->k42[x];
01200             tr->sigb1[x] = kc->sig22[x] / fabs(kc->k42[x]) + kc->sig42[x] * (fabs(kc->k22[x]) / (kc->k42[x] * kc->k42[x]));
01201             tr->kb2[x] = kc->k32[x] / kc->k42[x];
01202             tr->sigb2[x] = kc->sig32[x] / fabs(kc->k42[x]) + kc->sig42[x] * (fabs(kc->k32[x]) / (kc->k42[x] * kc->k42[x]));
01203 
01204            cpl_msg_info(cpl_func,"%3d        %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f \n", 
01205                 x, tr->ka1[x], tr->siga1[x], tr->ka2[x], tr->siga2[x], tr->kb1[x], tr->sigb1[x], tr->kb2[x], tr->sigb2[x]);
01206             fprintf (midiReportPtr, "%3d        %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f      %6.2f %6.2f \n",
01207                 x, tr->ka1[x], tr->siga1[x], tr->ka2[x], tr->siga2[x], tr->kb1[x], tr->sigb1[x], tr->kb2[x], tr->sigb2[x]);
01208         }
01209     }
01210 
01211     if (!n)
01212     {
01213         tr->exists = 0;
01214         *error = 1;
01215         return;
01216     }    
01217 
01218     if (plotFile && diagnostic > 1)
01219     {
01220         midiCreatePlotFile2D ("Ka1Ratio", "Ka1 Ratio", "Channel", "Flux", 1, tr->ka1, 0, format->iXWidth, 1, 0);
01221         midiCreatePlotFile2D ("Ka1Sig", "Ka1 Error", "Channel", "Flux", 1, tr->siga1, 0, format->iXWidth, 1, 0);
01222  
01223         midiCreatePlotFile2D ("Ka2Ratio", "Ka2 Ratio", "Channel", "Flux", 1, tr->ka2, 0, format->iXWidth, 1, 0);
01224         midiCreatePlotFile2D ("Ka2Sig", "Ka2 Error", "Channel", "Flux", 1, tr->siga2, 0, format->iXWidth, 1, 0);
01225  
01226         midiCreatePlotFile2D ("Kb1Ratio", "Kb1 Ratio", "Channel", "Flux", 1, tr->kb1, 0, format->iXWidth, 1, 0);
01227         midiCreatePlotFile2D ("Kb1Sig", "Kb1 Error", "Channel", "Flux", 1, tr->sigb1, 0, format->iXWidth, 1, 0);
01228  
01229         midiCreatePlotFile2D ("Kb2Ratio", "Kb2 Ratio", "Channel", "Flux", 1, tr->kb2, 0, format->iXWidth, 1, 0);
01230         midiCreatePlotFile2D ("Kb2Sig", "Kb2 Error", "Channel", "Flux", 1, tr->sigb2, 0, format->iXWidth, 1, 0);
01231     }
01232     
01233     tr->exists = 1;
01234     
01235     return;
01236 }
01237 /*****************************************************************************/
01238  
01239  
01240 /******************************************************************************
01241 *               European Southern Observatory
01242 *            VLTI MIDI Data Reduction Software
01243 *
01244 * Module name:  smoothKappaCoefficiets
01245 * Input/Output: See function arguments to avoid duplication
01246 * Description:  Not sure if this routine is required. A better method would
01247 *                probably be to filter the kappa elements at frame enseble stage.
01248 *                Then compute the associated errors and propagate the errors to 
01249 *                the ratios.
01250 *
01251 * History:
01252 * 07-Mar-06     (csabet) Created
01253 ******************************************************************************/
01254 void smoothKappaCoefficiets (
01255     ImageFormat            *format,    // In: Image size of interferometry data
01256     KappaCoefficients    *kc,        // In: Kappa Coefficients
01257     int                    *error)
01258 {
01259  
01260     //  Local Declarations
01261     //    ------------------
01262     const char    routine[] = "smoothKappaCoefficiets";
01263     int            x, n;
01264      
01265     //    Algorithm
01266     //    ---------
01267     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01268     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01269 
01270     //    Reset
01271     *error = 0;
01272      
01273     //    First reject noisy values
01274     n = 0;
01275     for (x = 0; x < format->iXWidth; x++)
01276     {
01277         if (!(badChannelList[x]))
01278         {
01279             if ((kc->sig11[x] > KAPPA_NOISE_LIMIT * kc->k11[x]) || (kc->sig21[x] > KAPPA_NOISE_LIMIT * kc->k21[x]) ||
01280                 (kc->sig31[x] > KAPPA_NOISE_LIMIT * kc->k31[x]) || (kc->sig22[x] > KAPPA_NOISE_LIMIT * kc->k22[x]) ||
01281                 (kc->sig32[x] > KAPPA_NOISE_LIMIT * kc->k32[x]) || (kc->sig42[x] > KAPPA_NOISE_LIMIT * kc->k42[x]))
01282                 badChannelList[x] |= BSL_DATA_ERROR;
01283             else
01284                 n++;
01285         }
01286     }
01287 
01288     //    Plot
01289     if (plotFile && diagnostic > 1 && n)
01290     {
01291         midiCreatePlotFile2D ("Kappa11VersX", "k11 Versus X", "X", "k11", 1, kc->k11, 0, format->iXWidth, 1, 0);
01292         midiCreatePlotFile2D ("Kappa21VersX", "k21 Versus X", "X", "k21", 1, kc->k21, 0, format->iXWidth, 1, 0);
01293         midiCreatePlotFile2D ("Kappa31VersX", "k31 Versus X", "X", "k31", 1, kc->k31, 0, format->iXWidth, 1, 0);
01294         midiCreatePlotFile2D ("Kappa22VersX", "k22 Versus X", "X", "k22", 1, kc->k22, 0, format->iXWidth, 1, 0);
01295         midiCreatePlotFile2D ("Kappa32VersX", "k32 Versus X", "X", "k32", 1, kc->k32, 0, format->iXWidth, 1, 0);
01296         midiCreatePlotFile2D ("Kappa42VersX", "k42 Versus X", "X", "k42", 1, kc->k42, 0, format->iXWidth, 1, 0);
01297     }
01298 
01299     if (!n)
01300     {
01301         *error = 1;
01302         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "High error bars on all channels");
01303         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Computation of the Kappa Coefficients is abandonned");
01304         return;
01305     }
01306 
01307     for (x = 0; x < format->iXWidth; x++)
01308     {
01309         //    If it is other than a masked error then no kappa matrix should be computed
01310         if (badChannelList[x] & 1023)
01311         {
01312             *error = 1;
01313             return;
01314         }
01315     }
01316     
01317     //    Now smooth the result. (Not sure yet if filtering is wise)    
01318     midiReportTbd (midiReportPtr, routine, __FILE__, __LINE__, "Routine TBD");
01319     
01320     return;
01321 }
01322 /*****************************************************************************/

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