procFrgHS.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  procFrgHS.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 <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #include <stdio.h>
00025 #include <cpl.h>
00026 #include <math.h>
00027 #include "midiGlobal.h"
00028 #include "midiLib.h"
00029 #include "memoryHandling.h"
00030 #include "calibration.h"
00031 #include "errorHandling.h"
00032 #include "midiFitsUtility.h"
00033 #include "fitsAnalysisFrgHS.h"
00034 #include "imageProcessing.h"
00035 #include "createProdFrg.h"
00036 #include "createProdFrgUndisp.h"
00037 #include "transferFunction.h"
00038 #include "visibility.h"
00039 #include "diagnostics.h"
00040 #include "estimationFrg.h"
00041 #include "estimationFrgHSUndisp.h"
00042 #include "photometry.h"
00043 #include "procFrgHS.h"
00044 #include "preProcFrgHS.h"
00045 #include "qfits.h"
00046 
00047 /**********************************************************
00048 *   Constant definitions
00049 **********************************************************/
00050 
00051 /**********************************************************
00052 *   Global Variables
00053 **********************************************************/
00054 
00055 /*============================ C O D E    A R E A ===========================*/
00056 
00057 
00058 
00059 
00060 /******************************************************************************
00061 *               European Southern Observatory
00062 *            VLTI MIDI Data Reduction Software
00063 *
00064 * Module name:  procFrgHS
00065 * Input/Output: See function arguments to avoid duplication
00066 * Description:    This is the main routine for HIGH_SEND fringe track
00067 *                The memory allocation needs the following considerations:
00068 *                
00069 *                Split Interferometry files require     "compressedInterf"    & "formatInterf"
00070 *                One Photometry A file requires        "compressedPhotomA"    & "formatPhotomA"
00071 *                One Photometry B file requires        "compressedPhotomB"    & "formatPhotomB"
00072 *    
00073 * History:
00074 * 21-Jul-03     (csabet) Created
00075 ******************************************************************************/
00076 void procFrgHS (
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[] = "procFrgHS";
00087     ImageFormat        *formatInterf=NULL;    // Applies to interf files
00088     ImageFormat        *formatPhotomA=NULL;// Applies to photom A files
00089     ImageFormat        *formatPhotomB=NULL;// Applies to photom B files
00090     CompressedData    *compressedInterf=NULL, *compressedPhotomA=NULL, *compressedPhotomB=NULL;
00091     FilterData        *filterInfo=NULL;
00092     FILE            *signaturePtr=NULL;
00093     DispersedResult    *dispResult=NULL;
00094     CorrectedFrames     *corrFrames=NULL;
00095     //    Algorithm
00096     //    ---------
00097     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00098     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00099 
00100     //    Write a signature. Midas checks to see which template is running
00101     if (strcmp (batchTemplate, "HIGH_SENS_CALIB") == 0)
00102         signaturePtr = fopen ("MIDI_sig_hsc.log", "w");
00103     else if (strcmp (batchTemplate, "HIGH_SENS_SCIENCE") == 0)
00104         signaturePtr = fopen ("MIDI_sig_hss.log", "w");
00105     else
00106     {
00107         sprintf (midiMessage, "Invalid template ... %s", batchTemplate);
00108         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00109     }
00110     fclose (signaturePtr);
00111 
00112     //    Reset status
00113     *error = 0;
00114 
00115     //    Analyse Input files
00116     //    -------------------
00117     //    Allocate memory
00118     formatInterf = callocImageFormat ();
00119     formatPhotomA = callocImageFormat ();
00120     formatPhotomB = callocImageFormat ();
00121     corrFrames = cpl_calloc (1, sizeof (CorrectedFrames));
00122 
00123     corrFrames->CorrInterf=0;
00124     corrFrames->CorrPhotomA=0;
00125     corrFrames->CorrPhotomB=0;
00126 
00127     analyseFitsFrgHS (fileNames, formatInterf, formatPhotomA, formatPhotomB, error);
00128     if (*error)
00129     {
00130         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse FITS files");
00131         freeImageFormat (formatInterf);
00132         freeImageFormat (formatPhotomA);
00133         freeImageFormat (formatPhotomB);
00134         return;
00135     }
00136 
00137     //    Reduce Data
00138     //    -----------
00139     //  Allocate memory for the compressed data, filter data and initialise to zero
00140     compressedInterf = callocMidiCompressed (formatInterf);
00141     compressedPhotomA = callocMidiCompressed (formatPhotomA);
00142     compressedPhotomB = callocMidiCompressed (formatPhotomB);
00143     badChannelList = (int *) calloc (formatInterf->iXWidth, sizeof (int)); 
00144     filterInfo = callocFilterInfo();
00145     
00146     //    Preprocessing
00147     preProcFrgHS (options, filterInfo, fileNames, compressedInterf, compressedPhotomA, 
00148         compressedPhotomB, formatInterf, formatPhotomA, formatPhotomB, error);
00149     
00150     if (*error)
00151     {
00152         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress FITS files");
00153         freeCompressedData (formatInterf, compressedInterf);
00154         freeCompressedData (formatPhotomA, compressedPhotomA);
00155         freeCompressedData (formatPhotomB, compressedPhotomB);
00156         free (badChannelList);
00157         freeImageFormat (formatInterf);
00158         freeImageFormat (formatPhotomA);
00159         freeImageFormat (formatPhotomB);
00160         freeFilterInfo (filterInfo);
00161         return;
00162     }
00163 
00164     //    Check Delay Lines for jumps within scans
00165     checkDelayLineJumps ("INTERF", formatInterf, compressedInterf);
00166 
00167     //    Check Delay Line Consistencies
00168     checkDelayLineConsistencies ("INTERF", "PHOTOMA", compressedInterf, compressedPhotomA, formatInterf, formatPhotomA, error);
00169     checkDelayLineConsistencies ("INTERF", "PHOTOMB", compressedInterf, compressedPhotomB, formatInterf, formatPhotomB, error);
00170     if (*error)
00171     {
00172         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
00173         freeCompressedData (formatInterf, compressedInterf);
00174         freeCompressedData (formatPhotomA, compressedPhotomA);
00175         freeCompressedData (formatPhotomB, compressedPhotomB);
00176         free (badChannelList);
00177         freeImageFormat (formatInterf);
00178         freeImageFormat (formatPhotomA);
00179         freeImageFormat (formatPhotomB);
00180         freeFilterInfo (filterInfo);
00181         return;
00182     }
00183 
00184     //    Check Chopping Statistics. for QCLOG diagnostics
00185     //    ------------------------------------------------
00186     reportInterfChopping ("INTERF", formatInterf, compressedInterf);
00187     reportPhotomChopping ("PHOTOMA", formatPhotomA, compressedPhotomA);
00188     reportPhotomChopping ("PHOTOMB", formatPhotomB, compressedPhotomB);
00189     if (options->processing == DISPERSED)
00190     {
00191         displayInterfChoppingDisp ("INTERF", formatInterf, compressedInterf);
00192         displayPhotomChoppingDisp ("PHOTOMA", formatPhotomA, compressedPhotomA);
00193         displayPhotomChoppingDisp ("PHOTOMB", formatPhotomB, compressedPhotomB);
00194     }
00195     
00196     //    Remove Sky Background and display Target with Sky removed
00197     dispResult = callocDispResult (formatInterf->iXWidth);
00198     cleanupFrgHS (options->processing, formatInterf, formatPhotomA, formatPhotomB, compressedInterf,
00199         compressedPhotomA, compressedPhotomB, dispResult, error, corrFrames);
00200     if (*error)
00201     {
00202         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
00203         freeCompressedData (formatInterf, compressedInterf);
00204         freeCompressedData (formatPhotomA, compressedPhotomA);
00205         freeCompressedData (formatPhotomB, compressedPhotomB);
00206         free (badChannelList);
00207         freeImageFormat (formatInterf);
00208         freeImageFormat (formatPhotomA);
00209         freeImageFormat (formatPhotomB);
00210         freeFilterInfo (filterInfo);
00211         freeDispResult (dispResult);
00212         return;
00213     }
00214     //    Display Waterfall data
00215     prepareWaterfallDisplay (fileNames, formatInterf, compressedInterf);
00216 
00217     //    Frequency Calibration, if it is a CALIB batch
00218     //    ---------------------------------------------
00219     if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
00220         calibrateWaveUsingPolynom (fileNames, formatInterf);
00221             
00222     //    Create UNDISPERSED allSpectrum and dcLevels
00223     //    -------------------------------------------
00224     getUndispersedPowerSpectrum (formatInterf, compressedInterf);
00225     
00226     //    Display Waterpower data
00227     prepareWaterpowerDisplay (fileNames, formatInterf, compressedInterf->allSpectrum);
00228 
00229     //    Select processing mode
00230     //    ----------------------
00231     if (options->processing == DISPERSED)
00232     {
00233         postProcFringe (fileNames, filterInfo, compressedInterf,
00234                 compressedPhotomA, compressedPhotomB, formatInterf,
00235                 formatPhotomA, formatPhotomB, dispResult, error,
00236                 parlist, frameset, corrFrames);
00237         if (*error)
00238         {
00239             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
00240             freeCompressedData (formatInterf, compressedInterf);
00241             freeCompressedData (formatPhotomA, compressedPhotomA);
00242             freeCompressedData (formatPhotomB, compressedPhotomB);
00243             free (badChannelList);
00244             freeImageFormat (formatInterf);
00245             freeImageFormat (formatPhotomA);
00246             freeImageFormat (formatPhotomB);
00247             freeFilterInfo (filterInfo);
00248             freeDispResult (dispResult);
00249             return;
00250         }
00251     }
00252     else if (options->processing == UNDISPERSED)
00253     {
00254        postProcFrgHSUndisp (fileNames, compressedInterf, compressedPhotomA, 
00255                             compressedPhotomB, filterInfo, formatInterf, formatPhotomA, formatPhotomB, error, parlist, frameset);
00256        if (*error)
00257        {
00258             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "HIGH_SENSE processing abandonned");
00259             freeCompressedData (formatInterf, compressedInterf);
00260             freeCompressedData (formatPhotomA, compressedPhotomA);
00261             freeCompressedData (formatPhotomB, compressedPhotomB);
00262             free (badChannelList);
00263             freeImageFormat (formatInterf);
00264             freeImageFormat (formatPhotomA);
00265             freeImageFormat (formatPhotomB);
00266             freeFilterInfo (filterInfo);
00267             freeDispResult (dispResult);
00268             return;
00269         }
00270     }
00271     else
00272     {
00273         *error = 1;
00274         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown processing mode requested");
00275         freeCompressedData (formatInterf, compressedInterf);
00276         freeCompressedData (formatPhotomA, compressedPhotomA);
00277         freeCompressedData (formatPhotomB, compressedPhotomB);
00278         free (badChannelList);
00279         freeImageFormat (formatInterf);
00280         freeImageFormat (formatPhotomA);
00281         freeImageFormat (formatPhotomB);
00282         freeFilterInfo (filterInfo);
00283         freeDispResult (dispResult);
00284         return;
00285     }
00286 
00287     //    Release memory used in this batch
00288     freeCompressedData (formatInterf, compressedInterf);
00289     freeCompressedData (formatPhotomA, compressedPhotomA);
00290     freeCompressedData (formatPhotomB, compressedPhotomB);
00291     free (badChannelList);
00292     freeImageFormat (formatInterf);
00293     freeImageFormat (formatPhotomA);
00294     freeImageFormat (formatPhotomB);
00295     freeFilterInfo (filterInfo);
00296     freeDispResult (dispResult);
00297     cpl_free(corrFrames);
00298     return;
00299 }
00300 
00301 
00302 
00303 /******************************************************************************
00304 *               European Southern Observatory
00305 *            VLTI MIDI Data Reduction Software
00306 *
00307 * Module name:  postProcFringe
00308 * Input/Output: See function arguments to avoid duplication
00309 * Description:  Performs complete post processing for the DISPERSED mode
00310 *
00311 * History:
00312 * 03-Feb-05     (csabet) Created
00313 ******************************************************************************/
00314 void postProcFringe (
00315    MidiFiles         *fileNames,            // In: Pointer to file names
00316    FilterData        *filterInfo,        // In: Filter data
00317    CompressedData    *compressedInterf,    // In: Compressed interferometry data
00318    CompressedData    *compressedPhotomA,    // In: Photometry data
00319    CompressedData    *compressedPhotomB,    // In: Photometry data
00320    ImageFormat       *formatInterf,        // In: Image size of interferometry data
00321    ImageFormat       *formatPhotomA,        // In: Applies to A photom files
00322    ImageFormat       *formatPhotomB,        // In: Applies to B photom files
00323    DispersedResult   *dispResult,        // IO: Output results
00324    int               *error,                // Ou: Status
00325    cpl_parameterlist *parlist,
00326    cpl_frameset      *frameset,
00327    CorrectedFrames   *corrFrames)
00328 {
00329 
00330     //  Local Declarations
00331     //    ------------------
00332     const char        routine[] = "postProcFringe";
00333     CalibratorParam    *calibrator=NULL;
00334     float            *freqCal=NULL;
00335     IauExchange      *iauData=NULL;
00336 
00337     //    Algorithm
00338     //    ---------
00339     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00340     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00341 
00342    cpl_msg_info(cpl_func,"\nProcessing data in DISPERSED mode for batch  %d \n", batchNumber);
00343    cpl_msg_info(cpl_func,"------------------------------------------- \n");
00344     fprintf (midiReportPtr, "\nProcessing data in DISPERSED mode for batch  %d \n", batchNumber);
00345     fprintf (midiReportPtr, "------------------------------------------- \n");
00346 
00347     //    Allocate memory
00348     formatInterf->fftsize = POST_FFT_SIZE;
00349     calibrator = callocCalibrator ();
00350     freqCal = (float *) calloc (formatInterf->iXWidth, sizeof (float));
00351     
00352     //  Reset status
00353     *error = 0;
00354     dispResult->photomAExists = 0;
00355     dispResult->photomBExists = 0;
00356     dispResult->photomIExists = 0;
00357     dispResult->calibVisExists = 0;
00358     dispResult->trfExists = 0;
00359 
00360     //    Estimate fringe
00361     //    ---------------
00362     estimateFringe (fileNames, filterInfo, compressedInterf, formatInterf, freqCal, 
00363         dispResult->rawVis2, dispResult->rawVis2Err, error);
00364     if (*error)
00365     {
00366         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Raw Visibilities");
00367         return;
00368     }
00369 
00370 
00371    cpl_msg_info(cpl_func,"\nRunning Photometry on photom A data: ");
00372     fprintf (midiReportPtr, "\nRunning Photometry on photom A data: ");
00373     estimatePhotom ('A', compressedPhotomA, formatPhotomA, dispResult->photomA, dispResult->photomAErr, error);
00374     if (*error)
00375     {
00376        cpl_msg_info(cpl_func,"%s \n", UNAV);
00377         fprintf (midiReportPtr, "%s \n", UNAV);
00378     }
00379     else
00380     {
00381         dispResult->photomAExists = 1;
00382        cpl_msg_info(cpl_func,"\n");
00383         fprintf (midiReportPtr, "\n");
00384     } 
00385 
00386    cpl_msg_info(cpl_func,"Running Photometry on Photom B data: ");
00387     fprintf (midiReportPtr, "Running Photometry on Photom B data: ");
00388     estimatePhotom ('B', compressedPhotomB, formatPhotomB, dispResult->photomB, dispResult->photomBErr, error);
00389     if (*error)
00390     {
00391        cpl_msg_info(cpl_func,"%s \n", UNAV);
00392         fprintf (midiReportPtr, "%s \n", UNAV);
00393     }
00394     else
00395     {
00396         dispResult->photomBExists = 1;
00397        cpl_msg_info(cpl_func,"\n");
00398         fprintf (midiReportPtr, "\n");
00399     } 
00400 
00401    cpl_msg_info(cpl_func,"Running Photometry on Interf data: ");
00402     fprintf (midiReportPtr, "Running Photometry on Interf data: ");
00403     estimatePhotom ('I', compressedInterf, formatInterf, dispResult->photomI, dispResult->photomIErr, error);
00404     if (*error)
00405     {
00406        cpl_msg_info(cpl_func,"%s \n", UNAV);
00407         fprintf (midiReportPtr, "%s \n", UNAV);
00408     }
00409     else
00410     {
00411         dispResult->photomIExists = 1;
00412        cpl_msg_info(cpl_func,"\n");
00413         fprintf (midiReportPtr, "\n");
00414     } 
00415         
00416     //    Compute DISPERSED Normalized but Uncalibrated Visibility
00417     //    --------------------------------------------------------
00418     computeUncalibVis (formatInterf, freqCal, dispResult, error);
00419     if (*error)
00420         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Uncalibrated Visibility for this batch");
00421 
00422     //    Compute Transfer Function if observation category is CALIB
00423     //    ----------------------------------------------------------
00424     if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
00425     {
00426        estimateTransferFunction (formatInterf, fileNames, dispResult, calibrator, error, parlist, frameset);
00427         if (*error)
00428             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Transfer Function for this batch");
00429     }
00430 
00431     //    Compute Calibrated Visibility if observation category is SCIENCE
00432     //    ----------------------------------------------------------------
00433     if (strcmp (formatInterf->obsCatg, "SCIENCE") == 0)
00434     {
00435        computeCalibVis (fileNames, formatInterf, dispResult, error, parlist, frameset);
00436         if (*error)
00437             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Calibrated Visibility for this batch");
00438     }
00439 
00440     //    Report results
00441     if (diagnostic) midiReportResultsFringe (freqCal, formatInterf, dispResult);
00442     reportRejectList (formatInterf, compressedInterf);
00443     
00444     //    Create the product files
00445 
00446     iauData=createFrgProd (fileNames, formatInterf, freqCal, dispResult, calibrator,
00447             error, corrFrames);
00448 
00449     /*Save the correlated flux*/
00450     if(iauData != NULL){
00451         midiSaveCorrelatedFlux(iauData, dispResult, parlist, frameset);
00452     }
00453     if (*error)
00454         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create product files");
00455 
00456     /*  Release memory */
00457     freeCalibrator (calibrator);
00458     free (freqCal);
00459     if(iauData != NULL){
00460         freeIauExchange (iauData);
00461     }
00462     return;
00463 }
00464 /*****************************************************************************/
00465 
00466 
00467 
00468 /******************************************************************************
00469 *               European Southern Observatory
00470 *            VLTI MIDI Data Reduction Software
00471 *
00472 * Module name:  postProcFrgHSUndisp
00473 * Input/Output: See function arguments to avoid duplication
00474 * Description:  Performs complete post processing for the UNDISPERSED mode
00475 *
00476 * History:
00477 * 03-Feb-05     (csabet) Created
00478 ******************************************************************************/
00479 void postProcFrgHSUndisp (
00480    MidiFiles        *fileNames,            // In: Pointer to file names
00481    CompressedData    *compressedInterf,    // In: Compressed interferometry data
00482    CompressedData    *compressedPhotomA,    // In: Compressed photometry data
00483    CompressedData    *compressedPhotomB,    // In: Compressed photometry data
00484    FilterData        *filterInfo,        // In: Filter information
00485    ImageFormat       *formatInterf,
00486    ImageFormat       *formatPhotomA,
00487    ImageFormat       *formatPhotomB,
00488    int               *error,                // Ou: Status
00489    cpl_parameterlist *parlist,
00490    cpl_frameset      *frameset)
00491 {
00492    
00493     //     Local Declarations
00494     //    ------------------
00495     const char                routine[] = "postProcFrgHSUndisp";
00496     RawVisibility            *rawVis=NULL;
00497     PhotometryResult        *photometry=NULL;
00498     CalibratedVisibility    *calibVis=NULL;
00499     CalibratorParam            *calibrator=NULL;
00500     TransferFunction        *trf=NULL;
00501 
00502 
00503     //    Algorithm
00504     //    ---------
00505     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00506     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00507 
00508    cpl_msg_info(cpl_func,"\nProcessing HIGH_SENS data in UNDISPERSED mode for batch  %d \n", batchNumber);
00509    cpl_msg_info(cpl_func,"------------------------------------------------------- \n");
00510     fprintf (midiReportPtr, "\nProcessing HIGH_SENS data in UNDISPERSED mode for batch  %d \n", batchNumber);
00511     fprintf (midiReportPtr, "------------------------------------------------------- \n");
00512 
00513     //    Reset status
00514     *error = 0;
00515     
00516     //    Finalise reject list using Signal / Noise
00517     getBadScansFromSpectrumUndisp (filterInfo, formatInterf, compressedInterf, error);
00518     if (*error) return;
00519     
00520     //    Compute UNDISPERSED fringe
00521     //    --------------------------
00522     rawVis = callocRawVis ();
00523     photometry = callocPhotometry ();
00524     estimateFrgHSUndisp (compressedInterf, compressedPhotomA, compressedPhotomB, filterInfo, 
00525         formatInterf, formatPhotomA, formatPhotomB, rawVis, photometry);
00526  
00527      //    Compute UNDISPERSED Uncalibrated Visibility
00528     //    -------------------------------------------
00529     computeUncalibVisHSUndisp (rawVis, photometry);
00530 
00531     //    Compute Transfer Function if observation category is CALIB
00532     //    ----------------------------------------------------------
00533     calibrator = callocCalibrator ();
00534     trf = callocTrFunction (6);
00535     if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
00536     {
00537        estimateTransferFunctionUndisp (fileNames, rawVis, trf, calibrator, error,parlist, frameset);
00538        if (*error)    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00539                                          "Cannot compute Transfer Function for this batch");
00540     }
00541 
00542     //    Compute Calibrated Visibility if observation category is SCIENCE
00543     //    ----------------------------------------------------------------
00544     calibVis = callocCalibVis ();
00545     if (strcmp (formatInterf->obsCatg, "SCIENCE") == 0)
00546     {
00547         computeCalibVisHSUndisp (fileNames, rawVis, trf, calibVis, error);
00548         if (*error)    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00549             "Cannot compute Calibrated Visibility for this batch");
00550     }
00551         
00552     //    Create the product files
00553     //    ------------------------
00554     createFrgProdUndisp (fileNames, formatInterf, filterInfo, rawVis, trf, 
00555         calibrator, calibVis, photometry, error);
00556     if (*error)    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create product files");
00557 
00558     //    Release memory
00559     freeRawVis (rawVis);
00560     freePhotometry (photometry);
00561     freeTrFunction (trf);
00562     freeCalibrator (calibrator);
00563     freeCalibVis (calibVis);
00564 
00565     return;
00566 }
00567 /*****************************************************************************/
00568 
00569 
00570 /******************************************************************************
00571 *               European Southern Observatory
00572 *            VLTI MIDI Data Reduction Software
00573 *
00574 * Module name:  cleanupFrgHS
00575 * Input/Output: See function arguments to avoid duplication
00576 * Description:  Removes sky etc from the data
00577 *
00578 * History:
00579 * 06-Dec-05     (csabet) Created
00580 ******************************************************************************/
00581 void cleanupFrgHS (
00582     enum ProcessingMode    processing,
00583     ImageFormat            *formatInterf,        // In: Interf size parameters
00584     ImageFormat            *formatPhotomA,        // In: PhotA size parameters
00585     ImageFormat            *formatPhotomB,        // In: PhotB size parameters
00586     CompressedData        *compressedInterf,    // Ou: Pointer to the compressed interferometry data structure
00587     CompressedData        *compressedPhotomA,    // Ou: Pointer to the compressed photom A data structure
00588     CompressedData        *compressedPhotomB,    // Ou: Pointer to the compressed photom B data structure
00589     DispersedResult        *dispResult,        // Ou: Output results
00590     int                    *error,
00591     CorrectedFrames        *corrFrames)                // Ou: Error status
00592 {
00593 
00594     //    Local Declarations
00595     //    ------------------
00596     const char    routine[] = "cleanupFrgHS";
00597     int            F, s, t, u, n, f, R, X, transitions, localError;
00598     double        *array, fluxAve;
00599     float        *arrayF;
00600     char        *fileName, *title;
00601     
00602     //    Algorithm
00603     //    ---------
00604     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00605     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00606 
00607    cpl_msg_info(cpl_func,"\nCleaning up HIGH_SENS files for batch  %d \n", batchNumber);
00608    cpl_msg_info(cpl_func,"------------------------------------- \n");
00609     fprintf (midiReportPtr, "\nCleaning up HIGH_SENS files for batch  %d \n", batchNumber);
00610     fprintf (midiReportPtr, "------------------------------------- \n");
00611 
00612     //    Initialise
00613     localError = 0;
00614     *error = 0;
00615     transitions = 0;
00616         
00617     //    Check Interf chopping cycle
00618     //    ---------------------------
00619     s = t = u = 0;
00620     for (F = 0; F < formatInterf->numOfFrames; F++)
00621     {
00622         if (compressedInterf->tarType[F] == 'S') s++;
00623         else if (compressedInterf->tarType[F] == 'T') t++;
00624         else u++;
00625     }
00626     //    Report
00627     if (u || s || (t != formatInterf->numOfFrames))
00628     {
00629         sprintf (midiMessage, "Interferometry data is chopped"); 
00630         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00631         *error = 1;
00632     }
00633     transitions = countTransitions ('T', formatInterf->numOfFrames, compressedInterf->tarType);
00634     sprintf (midiMessage, "Target Transitions for Interf   = %d", transitions); 
00635     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00636     formatInterf->numOfSkyScans = 0;
00637 
00638     //    Check PhotomA chopping cycle
00639     //    ----------------------------    
00640     transitions = countTransitions ('T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
00641     sprintf (midiMessage, "Target Transitions for Photom A = %d", transitions); 
00642     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00643     if (transitions < 10)
00644     {
00645         sprintf (midiMessage, "Photometry A data is not chopped"); 
00646         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00647         *error = 1;
00648     }
00649 
00650     //    Check PhotomB chopping cycle
00651     //    ----------------------------
00652     transitions = countTransitions ('T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
00653     sprintf (midiMessage, "Target Transitions for Photom B = %d", transitions); 
00654     midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00655     if (transitions < 10)
00656     {
00657         sprintf (midiMessage, "Photometry B data is not chopped"); 
00658         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00659         *error = 1;
00660     }
00661 
00662     //    if No error so far
00663     if (!(*error))
00664     {
00665         formatInterf->chopped = 0;
00666         formatPhotomA->chopped = 1;
00667         formatPhotomB->chopped = 1;
00668     }
00669     else 
00670     {
00671         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot cleanup HIGH_SENS files");
00672         *error = 1;
00673         return;
00674     }
00675 
00676     //    Correct benign tarType inconsistencies
00677     //    --------------------------------------
00678     corrFrames->CorrInterf=0;
00679     corrFrames->CorrPhotomA = correctTarType ("PHOTOMA",
00680             compressedPhotomA->tarType, compressedPhotomA->time,
00681             formatPhotomA->numOfFrames, &localError);
00682     if (localError) *error = 1;
00683 
00684     corrFrames->CorrPhotomB = correctTarType ("PHOTOMB",
00685             compressedPhotomB->tarType, compressedPhotomB->time,
00686             formatPhotomB->numOfFrames, &localError);
00687     if (*error || localError)
00688     {
00689         *error = 1;
00690         return;
00691     }
00692 
00693     //    If all is well, carry out cleaning
00694     //    ----------------------------------
00695     //    First compute binned Target 'T' photometries with background
00696     if (processing == DISPERSED && (strcmp(formatPhotomA->beamCombiner, "HIGH_SENS") == 0))
00697     {
00698         computeBinnedPhotom (compressedPhotomA,    compressedPhotomB, formatPhotomA,
00699             &(dispResult->photomATotal1), &(dispResult->photomATotal2), &(dispResult->photomATotal3),
00700             &(dispResult->photomBTotal1), &(dispResult->photomBTotal2), &(dispResult->photomBTotal3), error);
00701 
00702         //    Report
00703        cpl_msg_info(cpl_func,"\nBinned photometric measurements with background \n");
00704        cpl_msg_info(cpl_func,"----------------------------------------------- \n");
00705        cpl_msg_info(cpl_func,"Grism ID: %s \n", formatPhotomA->grismId);
00706         fprintf(midiReportPtr, "\nBinned photometric measurements with background QCLOG \n");
00707         fprintf(midiReportPtr, "----------------------------------------------- QCLOG \n");
00708         fprintf(midiReportPtr, "Grism ID: %s QCLOG \n", formatPhotomA->grismId);
00709         if (strcmp(formatPhotomA->grismId, "PRISM") == 0)
00710         {
00711            cpl_msg_info(cpl_func,"ArIII (Channel 113 -> 121) Beam A = %0.2f  Beam B = %0.2f \n", 
00712                 dispResult->photomATotal1, dispResult->photomBTotal1);
00713            cpl_msg_info(cpl_func,"  SIV (Channel  88 ->  98) Beam A = %0.2f  Beam B = %0.2f \n", 
00714                 dispResult->photomATotal2, dispResult->photomBTotal2);
00715            cpl_msg_info(cpl_func," NeII (Channel  36 ->  50) Beam A = %0.2f  Beam B = %0.2f \n\n", 
00716                 dispResult->photomATotal3, dispResult->photomBTotal3);
00717 
00718             fprintf(midiReportPtr, "ArIII (Channel 113 -> 121) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00719                 dispResult->photomATotal1, dispResult->photomBTotal1);
00720             fprintf(midiReportPtr, "  SIV (Channel  88 ->  98) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00721                 dispResult->photomATotal2, dispResult->photomBTotal2);
00722             fprintf(midiReportPtr, " NeII (Channel  36 ->  50) Beam A = %0.2f  Beam B = %0.2f QCLOG \n\n", 
00723                 dispResult->photomATotal3, dispResult->photomBTotal3);
00724         }
00725         else
00726         {
00727            cpl_msg_info(cpl_func,"ArIII (Channel  52 ->  68) Beam A = %0.2f  Beam B = %0.2f \n", 
00728                 dispResult->photomATotal1, dispResult->photomBTotal1);
00729            cpl_msg_info(cpl_func,"  SIV (Channel 116 -> 136) Beam A = %0.2f  Beam B = %0.2f \n", 
00730                 dispResult->photomATotal2, dispResult->photomBTotal2);
00731            cpl_msg_info(cpl_func," NeII (Channel 217 -> 244) Beam A = %0.2f  Beam B = %0.2f \n\n", 
00732                 dispResult->photomATotal3, dispResult->photomBTotal3);
00733 
00734             fprintf(midiReportPtr, "ArIII (Channel  52 ->  68) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00735                 dispResult->photomATotal1, dispResult->photomBTotal1);
00736             fprintf(midiReportPtr, "  SIV (Channel 116 -> 136) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00737                 dispResult->photomATotal2, dispResult->photomBTotal2);
00738             fprintf(midiReportPtr, " NeII (Channel 217 -> 244) Beam A = %0.2f  Beam B = %0.2f QCLOG \n\n", 
00739                 dispResult->photomATotal3, dispResult->photomBTotal3);
00740         }
00741     }
00742     
00743     removeSkyBackground ("PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
00744     if (localError) *error = 1;
00745     removeSkyBackground ("PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
00746     if (formatPhotomA->numOfFrames != formatPhotomA->numOfFrames || *error || localError)
00747     {
00748         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unequal numOfFrames");
00749         *error = 1;
00750         return;
00751     }
00752 
00753     //    Now compute binned Target 'T' photometries with background removed
00754     if (processing == DISPERSED && (strcmp(formatPhotomB->beamCombiner, "HIGH_SENS") == 0))
00755     {
00756         computeBinnedPhotom (compressedPhotomA,    compressedPhotomB, formatPhotomA,
00757             &(dispResult->photomATarg1), &(dispResult->photomATarg2), &(dispResult->photomATarg3),
00758             &(dispResult->photomBTarg1), &(dispResult->photomBTarg2), &(dispResult->photomBTarg3), error);
00759 
00760         //    Report
00761        cpl_msg_info(cpl_func,"\nBinned photometric measurements without background \n");
00762        cpl_msg_info(cpl_func,"-------------------------------------------------- \n");
00763        cpl_msg_info(cpl_func,"Grism ID: %s \n", formatPhotomA->grismId);
00764         fprintf(midiReportPtr, "\nBinned photometric measurements without background QCLOG \n");
00765         fprintf(midiReportPtr, "-------------------------------------------------- QCLOG \n");
00766         fprintf(midiReportPtr, "Grism ID: %s QCLOG \n", formatPhotomA->grismId);
00767         if (strcmp(formatPhotomA->grismId, "PRISM") == 0)
00768         {
00769            cpl_msg_info(cpl_func,"ArIII (Channel 113 -> 121) Beam A = %0.2f  Beam B = %0.2f \n", 
00770                 dispResult->photomATarg1, dispResult->photomBTarg1);
00771            cpl_msg_info(cpl_func,"  SIV (Channel  88 ->  98) Beam A = %0.2f  Beam B = %0.2f \n", 
00772                 dispResult->photomATarg2, dispResult->photomBTarg2);
00773            cpl_msg_info(cpl_func," NeII (Channel  36 ->  50) Beam A = %0.2f  Beam B = %0.2f \n\n", 
00774                 dispResult->photomATarg3, dispResult->photomBTarg3);
00775 
00776             fprintf(midiReportPtr, "ArIII (Channel 113 -> 121) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00777                 dispResult->photomATarg1, dispResult->photomBTarg1);
00778             fprintf(midiReportPtr, "  SIV (Channel  88 ->  98) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00779                 dispResult->photomATarg2, dispResult->photomBTarg2);
00780             fprintf(midiReportPtr, " NeII (Channel  36 ->  50) Beam A = %0.2f  Beam B = %0.2f QCLOG \n\n", 
00781                 dispResult->photomATarg3, dispResult->photomBTarg3);
00782         }
00783         else
00784         {
00785            cpl_msg_info(cpl_func,"ArIII (Channel  52 ->  68) Beam A = %0.2f  Beam B = %0.2f \n", 
00786                 dispResult->photomATarg1, dispResult->photomBTarg1);
00787            cpl_msg_info(cpl_func,"  SIV (Channel 116 -> 136) Beam A = %0.2f  Beam B = %0.2f \n", 
00788                 dispResult->photomATarg2, dispResult->photomBTarg2);
00789            cpl_msg_info(cpl_func," NeII (Channel 217 -> 244) Beam A = %0.2f  Beam B = %0.2f \n\n", 
00790                 dispResult->photomATarg3, dispResult->photomBTarg3);
00791 
00792             fprintf(midiReportPtr, "ArIII (Channel  52 ->  68) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00793                 dispResult->photomATarg1, dispResult->photomBTarg1);
00794             fprintf(midiReportPtr, "  SIV (Channel 116 -> 136) Beam A = %0.2f  Beam B = %0.2f QCLOG \n", 
00795                 dispResult->photomATarg2, dispResult->photomBTarg2);
00796             fprintf(midiReportPtr, " NeII (Channel 217 -> 244) Beam A = %0.2f  Beam B = %0.2f QCLOG \n\n", 
00797                 dispResult->photomATarg3, dispResult->photomBTarg3);
00798         }
00799     }
00800     
00801     //    Reject frames whose flux are <= 0 or are not 'T'
00802     for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00803     {
00804         for (X = 0; X < formatInterf->iXWidth; X++)
00805         {
00806             if (badChannelList[X])
00807                 continue;
00808                 
00809             for (f = 0; f < formatInterf->numOfFrames; f++)
00810             {
00811                 if ((compressedInterf->tarType[f] == 'T') && 
00812                     (((compressedInterf->iDispFringe)[R])[X])[f] <= 0.0)
00813                 {
00814                     compressedInterf->rejectList[X][f] |= BSL_DATA_ERROR;
00815                     if (diagnostic > 3) 
00816                     {
00817                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d          Interf %10.2f", 
00818                             R, X, f, (((compressedInterf->iDispFringe)[R])[X])[f]);
00819                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00820                     }
00821                 }
00822                 //    Reject frames which are not 'T'
00823                 if (compressedInterf->tarType[f] != 'T')
00824                     compressedInterf->rejectList[X][f] |= BSL_SKY_SCAN;
00825             }
00826         }
00827     }
00828  
00829     for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00830     {
00831         for (X = 0; X < formatPhotomA->iXWidth; X++)
00832         {
00833             if (badChannelList[X])
00834                 continue;
00835                     
00836             for (f = 0; f < formatPhotomA->numOfFrames; f++)
00837             {
00838                 if ((compressedPhotomA->tarType[f] == 'T') && 
00839                     (((compressedPhotomA->iDispFringe)[R])[X])[f] <= 0.0)
00840                 {
00841                     compressedPhotomA->rejectList[X][f] |= BSL_DATA_ERROR;
00842                     if (diagnostic > 3) 
00843                     {
00844                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         PhotomA %10.2f", 
00845                             R, X, f, (((compressedPhotomA->iDispFringe)[R])[X])[f]);
00846                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00847                     }
00848                 }
00849                 //    Reject frames which are not 'T'
00850                 if (compressedPhotomA->tarType[f] != 'T')
00851                     compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
00852             }
00853         }
00854     }
00855  
00856     for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00857     {
00858         for (X = 0; X < formatPhotomB->iXWidth; X++)
00859         {
00860             if (badChannelList[X])
00861                 continue;
00862                 
00863             for (f = 0; f < formatPhotomB->numOfFrames; f++)
00864             {
00865                 if ((compressedPhotomB->tarType[f] == 'T') && 
00866                     (((compressedPhotomB->iDispFringe)[R])[X])[f] <= 0.0)
00867                 {
00868                     compressedPhotomB->rejectList[X][f] |= BSL_DATA_ERROR;
00869                     if (diagnostic > 3) 
00870                     {
00871                         sprintf (midiMessage, "Negative Flux at: R %d    X %3d    F %4d         PhotomB %10.2f", 
00872                             R, X, f, (((compressedPhotomB->iDispFringe)[R])[X])[f]);
00873                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00874                     }
00875                 }
00876                 //    Reject frames which are not 'T'
00877                 if (compressedPhotomB->tarType[f] != 'T')
00878                     compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
00879             }
00880         }
00881     }
00882     
00883     //    Display data with background removed
00884     if (plotFile)
00885     {
00886         array = (double *) calloc (formatInterf->numOfFrames, sizeof (double));
00887  
00888         n = 0;
00889         fluxAve = 0.0;
00890         for (f = 0; f < formatInterf->numOfFrames; f++)
00891         {
00892             if (compressedInterf->tarType[f] == 'T')
00893             {
00894                 array[n] = compressedInterf->iFringe1[f];
00895                 fluxAve += array[n];
00896                 n++;
00897             }
00898         }
00899         fluxAve /= n;
00900        cpl_msg_info(cpl_func,"Interf DATA1 Average Flux = %f \n", fluxAve);
00901         midiCreatePlotFileDouble2D ("InterfCleanDATA1", "Interferometry DATA1 (Sky Removed)", 
00902             "Frame", "Flux", 0, array, 0, n, 1);
00903  
00904         n = 0;
00905         fluxAve = 0.0;
00906         for (f = 0; f < formatInterf->numOfFrames; f++)
00907         {
00908             if (compressedInterf->tarType[f] == 'T')
00909             {
00910                 array[n] = compressedInterf->iFringe2[f];
00911                 fluxAve += array[n];
00912                 n++;
00913             }
00914         }
00915         fluxAve /= n;
00916        cpl_msg_info(cpl_func,"Interf DATA2 Average Flux = %f \n", fluxAve);
00917         midiCreatePlotFileDouble2D ("InterfCleanDATA2", "Interferometry DATA2 (Sky Removed)", 
00918             "Frame", "Flux", 0, array, 0, n, 1);
00919  
00920         n = 0;
00921         for (f = 0; f < formatInterf->numOfFrames; f++)
00922         {
00923             if (compressedInterf->tarType[f] == 'T')
00924             {
00925                 array[n] = compressedInterf->iFringe[f];
00926                 n++;
00927             }
00928         }
00929         midiCreatePlotFileDouble2D ("InterfCleanDATA1_DATA2", "Interferometry DATA1-DATA2 (Sky Removed)", 
00930             "Frame", "Flux", 0, array, 0, n, 1);
00931 
00932         n = 0;
00933         fluxAve = 0.0;
00934         for (f = 0; f < formatPhotomA->numOfFrames; f++)
00935         {
00936             if (compressedPhotomA->tarType[f] == 'T')
00937             {
00938                 array[n] = compressedPhotomA->iFringe1[f];
00939                 fluxAve += array[n];
00940                 n++;
00941             }
00942         }
00943         fluxAve /= n;
00944        cpl_msg_info(cpl_func,"PhotomA DATA1 Average Flux = %f \n", fluxAve);
00945         midiCreatePlotFileDouble2D ("PhotomACleanDATA1", "Photometry A DATA1 (Sky Removed)", 
00946             "Frame", "Flux", 0, array, 0, n, 1);
00947  
00948         n = 0;
00949         fluxAve = 0.0;
00950         for (f = 0; f < formatPhotomA->numOfFrames; f++)
00951         {
00952             if (compressedPhotomA->tarType[f] == 'T')
00953             {
00954                 array[n] = compressedPhotomA->iFringe2[f];
00955                 fluxAve += array[n];
00956                 n++;
00957             }
00958         }
00959         fluxAve /= n;
00960        cpl_msg_info(cpl_func,"PhotomA DATA2 Average Flux = %f \n", fluxAve);
00961         midiCreatePlotFileDouble2D ("PhotomACleanDATA2", "Photometry A DATA2 (Sky Removed)", 
00962             "Frame", "Flux", 0, array, 0, n, 1);
00963 
00964         n = 0;
00965         fluxAve = 0.0;
00966         for (f = 0; f < formatPhotomB->numOfFrames; f++)
00967         {
00968             if (compressedPhotomB->tarType[f] == 'T')
00969             {
00970                 array[n] = compressedPhotomB->iFringe1[f];
00971                 fluxAve += array[n];
00972                 n++;
00973             }
00974         }
00975         fluxAve /= n;
00976        cpl_msg_info(cpl_func,"PhotomB DATA1 Average Flux = %f \n", fluxAve);
00977         midiCreatePlotFileDouble2D ("PhotomBCleanDATA1", "Photometry B DATA1 (Sky Removed)", 
00978             "Frame", "Flux", 0, array, 0, n, 1);
00979 
00980         n = 0;
00981         fluxAve = 0.0;
00982         for (f = 0; f < formatPhotomB->numOfFrames; f++)
00983         {
00984             if (compressedPhotomB->tarType[f] == 'T')
00985             {
00986                 array[n] = compressedPhotomB->iFringe2[f];
00987                 fluxAve += array[n];
00988                 n++;
00989             }
00990         }
00991         fluxAve /= n;
00992        cpl_msg_info(cpl_func,"PhotomB DATA2 Average Flux = %f \n", fluxAve);
00993         midiCreatePlotFileDouble2D ("PhotomBCleanDATA2", "Photometry B DATA2 (Sky Removed)", 
00994             "Frame", "Flux", 0, array, 0, n, 1);
00995 
00996         free (array);
00997     }
00998 
00999     //    Display for each channel
01000     if (plotFile && diagnostic > 4)
01001     {
01002         arrayF = (float *) calloc (formatInterf->numOfFrames, sizeof (float));
01003         fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01004         title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01005         
01006         for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
01007         {
01008             for (X = 0; X < formatInterf->iXWidth; X++)
01009             {
01010                 if (badChannelList[X])
01011                     continue;
01012     
01013                 n = 0;
01014                 for (f = 0; f < formatInterf->numOfFrames; f++)
01015                 {
01016                     if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
01017                     {
01018                         arrayF[n] = compressedInterf->iDispFringe[R][X][f];
01019                         n++;
01020                     }
01021                 }
01022                 sprintf (fileName, "InterfCleanDATA%d_X%d", R+1, X+1);
01023                 sprintf (title, "Interferometry DATA%d for Channel %d (Sky Removed)", R+1, X+1);
01024                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 0, arrayF, 0, n, 1, 0);
01025             }
01026         }
01027         for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
01028         {
01029             for (X = 0; X < formatPhotomA->iXWidth; X++)
01030             {
01031                 if (badChannelList[X])
01032                     continue;
01033                 
01034                 n = 0;
01035                 for (f = 0; f < formatPhotomA->numOfFrames; f++)
01036                 {
01037                     if (compressedPhotomA->tarType[f] == 'T' && !(compressedPhotomA->rejectList[X][f]))
01038                     {
01039                         arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
01040                         n++;
01041                     }
01042                 }
01043                 sprintf (fileName, "PhotomACleanDATA%d_X%d", R+1, X+1);
01044                 sprintf (title, "Photometry A DATA%d for Channel %d (Sky Removed)", R+1, X+1);
01045                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 0, arrayF, 0, n, 1, 0);
01046             }
01047         }
01048         for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
01049         {
01050             for (X = 0; X < formatPhotomB->iXWidth; X++)
01051             {
01052                 if (badChannelList[X])
01053                     continue;
01054                 
01055                 n = 0;
01056                 for (f = 0; f < formatPhotomB->numOfFrames; f++)
01057                 {
01058                     if (compressedPhotomB->tarType[f] == 'T' && !(compressedPhotomB->rejectList[X][f]))
01059                     {
01060                         arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
01061                         n++;
01062                     }
01063                 }
01064                 sprintf (fileName, "PhotomBCleanDATA%d_X%d", R+1, X+1);
01065                 sprintf (title, "Photometry B DATA%d for Channel %d (Sky Removed)", R+1, X+1);
01066                 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 0, arrayF, 0, n, 1, 0);
01067             }
01068         }
01069         free (fileName);
01070         free (title);
01071         free (arrayF);
01072     }
01073 
01074     //    Display the mean flux as a function of the channel
01075 
01076     if (plotFile)
01077     {   
01078        arrayF = (float *) calloc (formatInterf->iXWidth, sizeof (float));
01079        fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01080        title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01081        
01082        for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
01083        {
01084           for (X = 0; X < formatInterf->iXWidth; X++)
01085           {
01086              arrayF[X]=0;
01087              if (badChannelList[X])
01088                 continue;
01089              
01090               n = 0;
01091               for (f = 0; f < formatInterf->numOfFrames; f++)
01092               {
01093                  if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
01094                  {
01095                     arrayF[X] += compressedInterf->iDispFringe[R][X][f];
01096                     n++;
01097                  }
01098               }
01099               arrayF[X] /=(float)n;
01100           }
01101           
01102           sprintf (fileName, "MeanInterfCleanDATA%d", R+1);
01103           sprintf (title, "Mean Interferometry DATA%d (Sky Removed)", R+1);
01104           midiCreatePlotFile2D (fileName, title, "Channel", "Flux", 0, arrayF, 0, formatInterf->iXWidth, 1, 0);
01105        }
01106        free (fileName);
01107        free (title);
01108        free (arrayF);
01109     }
01110 
01111 
01112     //    Finalise rejection list
01113     finaliseRejectListInterf ("Combined Interferometry", formatInterf, compressedInterf, error);
01114     if (*error) return;
01115     finaliseRejectListPhotom ("Combined Photometry A", formatPhotomA, compressedPhotomA, error);
01116     if (*error) return;
01117     finaliseRejectListPhotom ("Combined Photometry B", formatPhotomB, compressedPhotomB, error);
01118     if (*error) return;
01119 
01120     return;
01121 }
01122 /*****************************************************************************/
01123 
01124 
01125 
01126 /******************************************************************************
01127 *               European Southern Observatory
01128 *            VLTI MIDI Data Reduction Software
01129 *
01130 * Module name:  finaliseRejectListInterf
01131 * Input/Output: See function arguments to avoid duplication
01132 * Description:    Finalising the badScanList, rejectList and badChannelList
01133 *
01134 * History:
01135 * 14-Mar-06     (csabet) Created
01136 ******************************************************************************/
01137 void finaliseRejectListInterf (
01138     const char            *dataKey,        // In: Data type flag
01139     ImageFormat     *format,        // In: Points to the image format
01140     CompressedData    *compressed,    // IO: Pointer to the compressed data structure
01141     int                *error)            // Ou: Error status
01142 {
01143     //    Local Declarations
01144     //--------------------
01145     const char  routine[] = "finaliseRejectListInterf";
01146     int            i, f, s, x, numOfGoodFrames, numOfGoodChannels,
01147                 numOfBadScans, minFrames;
01148 
01149     //    Algorithm
01150     //    ---------
01151     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01152     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine '%s'  \n", routine);
01153 
01154    cpl_msg_info(cpl_func,"\nFinalising Rejection List for %s data \n", dataKey);
01155    cpl_msg_info(cpl_func,"-------------------------------------------------- \n");
01156     fprintf (midiReportPtr, "\nFinalising Rejection List for %s data \n", dataKey);
01157     fprintf (midiReportPtr, "-------------------------------------------------- \n");
01158 
01159     //    Reset
01160     *error = 0;
01161     minFrames = MIN_FRAMES_PER_SCAN * format->framesPerScan;
01162     
01163     numOfGoodChannels = 0;
01164     for (x = 0; x < format->iXWidth; x++)
01165     {
01166         if (badChannelList[x])
01167         {
01168             for (f = 0; f < format->numOfFrames; f++)
01169                 compressed->rejectList[x][f] |= badChannelList[x];
01170             continue;
01171         }
01172             
01173         numOfBadScans = 0;
01174         for (s = 0; s < format->numOfScans; s++)
01175         {
01176             numOfGoodFrames = 0;
01177             for (i = 0; i < format->framesPerScan; i++)
01178             {
01179                 f = i + s * format->framesPerScan;
01180                 if (!(compressed->rejectList[x][f]))
01181                     numOfGoodFrames++;
01182             }
01183             //    If number of good frames in a scan is less than a minimum reject scan
01184             if (numOfGoodFrames < minFrames)
01185                 numOfBadScans++;
01186         }
01187         if (numOfBadScans == format->numOfScans)
01188             badChannelList[x] |= BSL_DATA_ERROR;
01189         else
01190             numOfGoodChannels++;
01191     }
01192     
01193     //    If no good channels return error
01194     if (!numOfGoodChannels)
01195     {
01196         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "All channels have been rejected");
01197         *error = 1;
01198     }
01199 
01200     return;
01201 }
01202 /*****************************************************************************/
01203 
01204 
01205 /******************************************************************************
01206 *               European Southern Observatory
01207 *            VLTI MIDI Data Reduction Software
01208 *
01209 * Module name:  finaliseRejectListPhotom
01210 * Input/Output: See function arguments to avoid duplication
01211 * Description:    Finalising the badScanList, rejectList and badChannelList
01212 *                This routine is used for the photometry of the HIGH_SENS
01213 *                as concept of scan is meaningless
01214 *
01215 * History:
01216 * 14-Mar-06     (csabet) Created
01217 ******************************************************************************/
01218 void finaliseRejectListPhotom (
01219     const char            *dataKey,        // In: Data type flag
01220     ImageFormat     *format,        // In: Points to the image format
01221     CompressedData    *compressed,    // IO: Pointer to the compressed data structure
01222     int                *error)            // Ou: Error status
01223 {
01224     //    Local Declarations
01225     //--------------------
01226     const char  routine[] = "finaliseRejectListPhotom";
01227     int            f, x, numOfBadFrames, numOfGoodChannels;
01228 
01229     //    Algorithm
01230     //    ---------
01231     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01232     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine '%s'  \n", routine);
01233 
01234    cpl_msg_info(cpl_func,"\nFinalising Rejection List for %s data \n", dataKey);
01235    cpl_msg_info(cpl_func,"-------------------------------------------------- \n");
01236     fprintf (midiReportPtr, "\nFinalising Rejection List for %s data \n", dataKey);
01237     fprintf (midiReportPtr, "-------------------------------------------------- \n");
01238 
01239     //    Reset
01240     *error = 0;
01241 
01242     numOfGoodChannels = 0;
01243     for (x = 0; x < format->iXWidth; x++)
01244     {
01245         if (badChannelList[x])
01246         {
01247             for (f = 0; f < format->numOfFrames; f++)
01248                 compressed->rejectList[x][f] |= badChannelList[x];
01249             continue;
01250         }
01251             
01252         numOfBadFrames = 0;
01253         for (f = 0; f < format->numOfFrames; f++)
01254         {
01255             if (compressed->rejectList[x][f])
01256                 numOfBadFrames++;
01257         }
01258         if (numOfBadFrames == format->numOfFrames)
01259             badChannelList[x] |= BSL_DATA_ERROR;
01260         else
01261             numOfGoodChannels++;
01262     }
01263     
01264     //    If no good channels return error
01265     if (!numOfGoodChannels)
01266     {
01267         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "All channels have been rejected");
01268         *error = 1;
01269     }
01270 
01271     return;
01272 }
01273 /*****************************************************************************/
01274 
01275 /*----------------------------------------------------------------------------*/
01282 /*----------------------------------------------------------------------------*/
01283 
01284 cpl_error_code midiSaveCorrelatedFlux(IauExchange *iauData,
01285         DispersedResult *dispResult, cpl_parameterlist *parlist,
01286         cpl_frameset *frameset)
01287 {
01288     cpl_table        * table=NULL;
01289     cpl_propertylist * qclist=NULL;
01290     char             * dataname=NULL;
01291 
01292     table=cpl_table_new(iauData->wavelength->nwave);
01293 
01294     cpl_table_new_column(table, "Wavelength",          CPL_TYPE_FLOAT);
01295     cpl_table_new_column(table, "CorrelatedFlux",      CPL_TYPE_FLOAT);
01296     cpl_table_new_column(table, "CorrelatedFluxError", CPL_TYPE_FLOAT);
01297     cpl_table_new_column(table, "Flag",                CPL_TYPE_INT);
01298 
01299     cpl_table_set_column_unit(table ,"Wavelength",          "m");
01300     cpl_table_set_column_unit(table, "CorrelatedFlux",      "ADU");
01301     cpl_table_set_column_unit(table, "CorrelatedFluxError", "ADU");
01302 
01303     cpl_table_copy_data_float(table,"Wavelength",iauData->wavelength->eff_wave);
01304     cpl_table_copy_data_float(table,"CorrelatedFlux",dispResult->rawVis2);
01305     cpl_table_copy_data_float(table,"CorrelatedFluxError",dispResult->rawVis2Err);
01306     cpl_table_copy_data_int(table,"Flag",badChannelList);
01307     /*
01308     cpl_table_dump_structure(table,NULL);
01309     cpl_table_dump(table,0,200,NULL);
01310      */
01311     qclist=cpl_propertylist_new();
01312 
01313     cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "CORRELATED_FLUX");
01314     dataname=cpl_sprintf("MIDI_CorrelatedFlux.fits");
01315 
01316     cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, table,
01317             qclist, "midi_fringe_all",
01318             qclist, NULL,
01319             PACKAGE "/" PACKAGE_VERSION,
01320             dataname);
01321 
01322     cpl_propertylist_delete(qclist);
01323     cpl_free(dataname);
01324     cpl_table_delete(table);
01325 
01326     if(cpl_error_get_code()!=CPL_ERROR_NONE)
01327     {
01328         cpl_msg_error(cpl_func,"Correlated flux can not be saved!");
01329         return cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_CREATED);
01330     }
01331     else {
01332         return CPL_ERROR_NONE;
01333     }
01334 }
01335 
01336 /*****************************************************************************/
01337 

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