transferFunction.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *                European Southern Observatory
00004 *              VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:    transferFunction.c
00007 * Description:    Contains routines for all transfer function routines
00008 *
00009 * History:        
00010 * 21-Jul-03        (csabet) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *    Compiler directives
00016 ******************************************************************************/
00017 #define NEW_PBL_x
00018 
00019 /******************************************************************************
00020 *    Include files
00021 ******************************************************************************/
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <unistd.h>
00025 #include <stdio.h>
00026 #include <cpl.h>
00027 #include <string.h>
00028 #include <math.h>
00029 #include "midiGlobal.h"
00030 #include "midiLib.h"
00031 #include "qfits.h"
00032 #include "errorHandling.h"
00033 #include "diagnostics.h"
00034 #include "fileHandling.h"
00035 #include "transferFunction.h"
00036 #include "midi_dfs.h"
00037 
00038 /**********************************************************
00039 *   Constant definitions
00040 **********************************************************/
00041 #define START_MIDI_RECORD        (173)            /* Start line for MIDI record in the calibrator database */
00042 #define END_MIDI_RECORD          (620)            /* End line for MIDI record in the calibrator database */
00043 #define CALIB_DB_LEN             (30)             /* Maximum length of each elements in the calibrator database */
00044 #define UNWANTED_ELEMENTS        (27)             /* Number of unwanted elements in the calibrator database */
00045 #define CALIBRATOR_MIN_THRESHOLD (0.000290888)    /* Minimum threshold for finding diameter in radians (60 arcsec) */
00046 #define DIAMETER_FACTOR          (4.848136958e-9) /* Diameter factor = (1.0 / (1000 * 206264.8)) */
00047 #define INVERSE_WAVELENGTH       (100000.0)       /* Inverse Effective wavelength = (1.0 / 10 micron) */
00048 
00049 /*============================ C O D E    A R E A ===========================*/
00050 
00051 
00052 
00053 /******************************************************************************
00054 *                European Southern Observatory
00055 *              VLTI MIDI Data Reduction Software 
00056 *
00057 * Module name:    estimateTransferFunction
00058 * Input/Output:    See function arguments to avoid duplication
00059 * Description:    This routine computes the transfer fuction as follows: If the current
00060 *               observation category is CALIB then the estimate of the uncalibrated
00061 *               visibility has already been calculated and is given as an input to 
00062 *               this routine. Here we calculate the expected visibility and thereby
00063 *               the transfer function. The ASCCI file containing the transfer function
00064 *               is shown below where the first line gives the number of regions and the
00065 *                subsequent lines give the transfer functions and the errors for each channel
00066 *
00067 *                Record 1    Grism ID
00068 *                Record 2    Number of Record
00069 *                Record 3    number_of_combined_regions
00070 *                Record 4    value_1 value_1_Err ... Value_n value_n_Error
00071 *                .
00072 *                .
00073 *                Record n    value_1 value_1_Err ... Value_n value_n_Error
00074 *
00075 * History:        
00076 * 07-Feb-05        (csabet) Created
00077 ******************************************************************************/
00078 void estimateTransferFunction (
00079    ImageFormat        *formatInterf,        // In: Image size of interferometry data
00080    MidiFiles          *fileNames,            // In: Pointer to MIDI file structure
00081    DispersedResult    *dispResult,        // IO: Dispersed result structure
00082    CalibratorParam    *calibrator,        // Ou: Calibrator parameters
00083    int                *error,                // Ou: Error Status
00084    cpl_parameterlist  *parlist,
00085    cpl_frameset       *frameset)
00086 {
00087    
00088     //    Local Declarations
00089     //    ------------------
00090    const char         routine[] = "estimateTransferFunction";
00091    FILE             * trfPtr, *trfHistoryPtr;
00092    int                X;
00093    struct stat        buf;
00094    cpl_table        * trftable=NULL;
00095    cpl_propertylist * qclist=NULL;   
00096    char             * dataname=NULL;
00097    char             * system_call=NULL;
00098 
00099     
00100     //    Algorithm
00101     //    ---------
00102     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00103     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00104 
00105    cpl_msg_info(cpl_func,"\nComputing Transfer Functions for batch  %d \n", batchNumber);
00106    cpl_msg_info(cpl_func,"-------------------------------------- \n");
00107     fprintf (midiReportPtr, "\nComputing Transfer Functions for batch  %d \n", batchNumber);
00108     fprintf (midiReportPtr, "-------------------------------------- \n");
00109 
00110     //    Reset status
00111     *error = 0;
00112     dispResult->trfExists = 0;
00113 
00114     //    Check if transfer function file exist. If so delete it
00115     if (stat (fileNames->trfNameWrite, &buf) == 0)    //    If the file exist delete it
00116         remove (fileNames->trfNameWrite);
00117 
00118     identifyCalibrator (fileNames, calibrator, error, parlist, frameset);
00119     if (*error)
00120     {
00121         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot identify Calibrator");
00122         return;
00123     }
00124 
00125     computeExpectedVis (calibrator, error);
00126     if (*error)
00127     {
00128         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Expected Visibilities");
00129         return;
00130     }
00131     
00132     //    Compute Transfer Function
00133     if (calibrator->calibVis == 0 || calibrator->calibVisSqrd == 0)
00134     {
00135         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00136             "A singularity encountered\n                 Cannot compute transfer functions");
00137         *error = 1;
00138         return;
00139     }
00140     else
00141     {
00142         //    Open the transfer function file for writing
00143         if ((trfPtr = fopen (fileNames->trfNameWrite, "w")) == NULL)
00144         {
00145             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open file to store transfer functions");
00146             *error = 1;
00147             return;
00148         }
00149         
00150         //    Open a file for transfer function history
00151         trfHistoryPtr = fopen (fileNames->trfHistoryName, "a");
00152         fprintf (trfHistoryPtr, "Transfer Functions (DISPERSED) for batch %d \n", batchNumber);
00153         fprintf (trfHistoryPtr, "---------------------------------------- \n");
00154         fprintf (trfHistoryPtr, "Grism ID                   = %s \n", formatInterf->grismId);
00155         fprintf (trfHistoryPtr, "Number of Valid Channels   = %d \n", formatInterf->iXWidth);
00156 
00157         if (diagnostic)cpl_msg_info(cpl_func,"Grism ID                   = %s \n", formatInterf->grismId);
00158         if (diagnostic)cpl_msg_info(cpl_func,"Number of Channels         = %d \n", formatInterf->iXWidth);
00159 
00160         fprintf (midiReportPtr, "\nTransfer Functions (DISPERSED) for batch %d QCLOG \n", batchNumber);
00161         fprintf (midiReportPtr, "---------------------------------------- QCLOG \n");
00162         fprintf (midiReportPtr, "Grism ID                   = %s QCLOG \n", formatInterf->grismId);
00163         fprintf (midiReportPtr, "Number of Valid Channels   = %d QCLOG \n", formatInterf->iXWidth);
00164 
00165         fprintf (trfPtr, "%s \n", formatInterf->grismId);
00166         fprintf (trfPtr, "%d \n", formatInterf->iXWidth);
00167 
00168         for (X = 0; X < formatInterf->iXWidth; X++)
00169         {
00170             if (badChannelList[X])
00171             {
00172                 if (diagnostic)cpl_msg_info(cpl_func,"%3d    <-- %s --> \n", X, UNAV);
00173                 fprintf (midiReportPtr, "%3d    <-- %s -->  QCLOG \n", X, UNAV);
00174                 fprintf (trfHistoryPtr, "%3d    <-- %s --> \n", X, UNAV);
00175                 fprintf (trfPtr, "%f %f \n", (dispResult->trf)[X], (dispResult->trfErr)[X]);
00176                 continue;
00177             }
00178         
00179             dispResult->trf[X] = dispResult->normVis2[X] / calibrator->calibVis;
00180             dispResult->trfErr[X] = dispResult->normVis2Err[X] / (fabs (calibrator->calibVis)) + 
00181                 calibrator->calibVisErr * (fabs (dispResult->normVis2[X]) / 
00182                 (calibrator->calibVis * calibrator->calibVis));
00183 
00184             if (diagnostic)cpl_msg_info(cpl_func,"%3d  %f %f \n", X, (dispResult->trf)[X], (dispResult->trfErr)[X]);
00185             fprintf (midiReportPtr, "%3d  %f %f QCLOG \n", X, (dispResult->trf)[X], (dispResult->trfErr)[X]);
00186             fprintf (trfHistoryPtr, "%3d  %f %f \n", X, (dispResult->trf)[X], (dispResult->trfErr)[X]);
00187             fprintf (trfPtr, "%f %f \n", (dispResult->trf)[X], (dispResult->trfErr)[X]);
00188         }
00189 
00190         fclose (trfPtr);
00191         fclose (trfHistoryPtr);
00192         if (diagnostic)cpl_msg_info(cpl_func,"Created Transfer Function file   %s\n", fileNames->trfNameWrite);
00193         fprintf (midiReportPtr, "Created Transfer Function file   %s\n", fileNames->trfNameWrite);
00194         dispResult->trfExists = 1;
00195 
00196 /* -------------------------------------------------------------------------- */
00197 
00198 /* Write transfer function in a fits table */
00199         trftable=cpl_table_new(formatInterf->iXWidth);
00200         cpl_table_new_column(trftable,"CHANNEL",CPL_TYPE_INT);      
00201         cpl_table_new_column(trftable,"TRF",CPL_TYPE_FLOAT);        
00202         cpl_table_new_column(trftable,"TRF_ERROR",CPL_TYPE_FLOAT);      
00203         
00204         for (X = 0; X < formatInterf->iXWidth; X++)
00205         {
00206            cpl_table_set_int(trftable,"CHANNEL",X, X+1);    
00207            cpl_table_set_float(trftable,"TRF",X, (dispResult->trf)[X]);     
00208            cpl_table_set_float(trftable,"TRF_ERROR",X, (dispResult->trfErr)[X]);    
00209            if (badChannelList[X])
00210            {
00211               cpl_table_set_invalid(trftable, "TRF", X);        
00212               cpl_table_set_invalid(trftable, "TRF_ERROR", X);        
00213            }
00214         }  
00215         
00216         
00217 /* Save the fits table  into a fits file */
00218         
00219         qclist=cpl_propertylist_new();
00220         
00221         if (strcmp(formatInterf->grismId,"GRISM")==0) {
00222            cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "TRF_GRISM");
00223            dataname=cpl_sprintf("MIDI_trf_grism.fits");
00224         }
00225         if (strcmp(formatInterf->grismId,"PRISM")==0) {
00226            cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "TRF_PRISM");
00227            dataname=cpl_sprintf("MIDI_trf_prism.fits");
00228         }
00229         cpl_propertylist_append_string(qclist, "EXTNAME", "TRANSFER_FUNCTION");
00230  
00231 
00232         cpl_propertylist_update_float(qclist,  "ESO QC OBS CAL RA"      , calibrator->calibRA);
00233         cpl_propertylist_update_float(qclist,  "ESO QC OBS CAL DEC"     , calibrator->calibDEC);
00234         cpl_propertylist_update_float(qclist,  "ESO QC OBS AVR PBL "    , calibrator->calibPblAverage);
00235         cpl_propertylist_update_float(qclist,  "ESO QC OBS AVR PARANG"  , calibrator->calibParangAverage);
00236         cpl_propertylist_update_string(qclist, "ESO QC DB NAME"         , calibrator->calibName);                       
00237         cpl_propertylist_update_float(qclist,  "ESO QC DB DIAM"         , calibrator->calibDiameter);                   
00238         cpl_propertylist_update_float(qclist,  "ESO QC DB DIAM ERR "    , calibrator->calibDiameterErr);                
00239         cpl_propertylist_update_float(qclist,  "ESO QC DB DIST"         , RAD_TO_ARCSEC * calibrator->calibDistance);   
00240         cpl_propertylist_update_double(qclist, "ESO QC DB MAG NBAND"    , calibrator->calibFlux);                       
00241         cpl_propertylist_update_int   (qclist, "ESO QC DB FLAG"         , calibrator->calibFlag);                       
00242         cpl_propertylist_update_float(qclist,  "ESO QC EXP VIS"         , calibrator->calibVis);        
00243         cpl_propertylist_update_float(qclist,  "ESO QC EXP VIS ERR"     , calibrator->calibVisErr);     
00244         cpl_propertylist_update_float(qclist,  "ESO QC EXP VIS2"        , calibrator->calibVisSqrd);    
00245         cpl_propertylist_update_float(qclist,  "ESO QC EXP VIS2 ERR"    , calibrator->calibVisSqrdErr); 
00246 
00247         cpl_propertylist_set_comment (qclist, "ESO QC OBS CAL RA"      , "Observed Calibrator RA in radians");
00248         cpl_propertylist_set_comment (qclist, "ESO QC OBS CAL DEC"     , "Observed Calibrator DEC in radians");
00249         cpl_propertylist_set_comment (qclist, "ESO QC OBS AVR PBL "    , "Observed Calibrator Average PBL in meter");
00250         cpl_propertylist_set_comment (qclist, "ESO QC OBS AVR PARANG"  , "Observed Calibrator Average PARANG in degrees");
00251         cpl_propertylist_set_comment (qclist, "ESO QC DB NAME"         , "Closest Calibrator in the database");
00252         cpl_propertylist_set_comment (qclist, "ESO QC DB DIAM"         , "Calibrator Diameter in marcsec");
00253         cpl_propertylist_set_comment (qclist, "ESO QC DB DIAM ERR "    , "Calibrator Diameter error in marcsec");
00254         cpl_propertylist_set_comment (qclist, "ESO QC DB DIST"         , "Computed Distance to Observed Calibrator in arcsec");
00255         cpl_propertylist_set_comment (qclist, "ESO QC DB MAG NBAND"    , "Calibrator Magnitude in the N-Band");
00256         cpl_propertylist_set_comment (qclist, "ESO QC DB FLAG"         , "Calibrator Quality Flag");
00257         cpl_propertylist_set_comment (qclist, "ESO QC EXP VIS"         , "Expected Visibility");
00258         cpl_propertylist_set_comment (qclist, "ESO QC EXP VIS ERR"     , "Expected Visibility Error");
00259         cpl_propertylist_set_comment (qclist, "ESO QC EXP VIS2"        , "Expected Squared Visibility");
00260         cpl_propertylist_set_comment (qclist, "ESO QC EXP VIS2 ERR"    , "Expected Squared Visibility Error");
00261 
00262 
00263         cpl_dfs_save_table(frameset, NULL, parlist, frameset, NULL, trftable, 
00264                            qclist, "midi_fringe_all",
00265                            qclist, NULL, 
00266                            PACKAGE "/" PACKAGE_VERSION,
00267                            dataname);
00268         
00269         /* Now copy the file to tmp to be able to use the files after the
00270            originals are renamed by ESOREX 
00271         */
00272         system_call=cpl_sprintf("cp %s /tmp/MIDI_trf.fits", dataname);
00273         cpl_msg_info(cpl_func, "Copying the file into the tmp directory ...");         
00274         cpl_msg_info(cpl_func,system_call);         
00275         system(system_call);
00276 
00277         cpl_table_delete(trftable);   
00278         cpl_propertylist_delete(qclist);        
00279         cpl_free(dataname); 
00280         cpl_free(system_call); 
00281 
00282 
00283 
00284 /* -------------------------------------------------------------------------- */
00285 
00286         //    Compute Binned Tranfer Function
00287         computeBinnedTrf (formatInterf, dispResult, error);
00288         if (*error)
00289         {
00290            midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Binned Transfer Function");
00291            return;
00292         }
00293         
00294         
00295     }
00296     
00297 
00298 
00299 
00300     if (plotFile && diagnostic)
00301     {
00302         midiCreatePlotFile2D ("TransferFunctionConbined", "Combined Transfer Function", 
00303             "Channel", "Transfer Function", 0, dispResult->trf, 0, formatInterf->iXWidth, 1, 0);
00304 
00305         midiCreatePlotFile2D ("TransferFunctionErrConbined", "Combined Transfer Function", 
00306             "Channel", "Transfer Function Error", 0, dispResult->trfErr, 0, formatInterf->iXWidth, 1, 0);
00307     }
00308     
00309     return;
00310 }
00311 /*****************************************************************************/
00312 
00313 
00314 /******************************************************************************
00315 *                European Southern Observatory
00316 *              VLTI MIDI Data Reduction Software 
00317 *
00318 * Module name:    estimateTransferFunctionUndisp
00319 * Input/Output:    See function arguments to avoid duplication
00320 * Description:    This routine computes the transfer fuction as follows: If the current
00321 *               observation category is CALIB then the estimate of the uncalibrated
00322 *               visibility has already been calculated and is given as an input to 
00323 *               this routine. Here we calculate the expected visibility and thereby
00324 *               the transfer function. The ASCCI file containing the transfer function
00325 *               has the following format:
00326 *
00327 *               transfer function region 0
00328 *               transfer function region 0 Error
00329 *               transfer function region 1
00330 *               transfer function region 1 Error
00331 *               transfer function region 2
00332 *               transfer function region 2 Error
00333 *               transfer function squared region 0
00334 *               transfer function squared region 0 Error
00335 *               transfer function squared region 1
00336 *               transfer function squared region 1 Error
00337 *               transfer function squared region 2
00338 *               transfer function squared region 2 Error
00339 *
00340 * History:        
00341 * 21-Jul-03        (csabet) Created
00342 ******************************************************************************/
00343 void estimateTransferFunctionUndisp (
00344    MidiFiles           *fileNames,        // In: Pointer to MIDI file structure
00345    RawVisibility       *measVis,        // In: Measured visibilities
00346    TransferFunction    *trf,            // Ou: Transfer Functions
00347    CalibratorParam     *calibrator,    // Ou: Calibrator parameters
00348    int                 *error,            // Ou: Status
00349    cpl_parameterlist   *parlist,
00350    cpl_frameset        *frameset)
00351 {
00352 
00353     /*    Local Declarations
00354     --------------------*/
00355     const char     routine[] = "estimateTransferFunctionUndisp";
00356     FILE        *trfPtr;
00357     int         i;
00358     FILE        *trfHistoryPtr;
00359     struct stat    buf;
00360     
00361     /*    Algorithm
00362     -----------*/
00363     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00364     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00365 
00366    cpl_msg_info(cpl_func,"\nComputing Transfer Functions for batch  %d \n", batchNumber);
00367    cpl_msg_info(cpl_func,"-------------------------------------- \n");
00368     fprintf (midiReportPtr, "\nComputing Transfer Functions for batch  %d \n", batchNumber);
00369     fprintf (midiReportPtr, "-------------------------------------- \n");
00370 
00371     /* Reset status */
00372     *error = 0;
00373     trf->exists = 0;
00374 
00375     /* Check if transfer function file exist. If so delete it */
00376     if (stat (fileNames->trfNameWrite, &buf) == 0)    /*    If the file exist delete it */
00377         remove (fileNames->trfNameWrite);
00378 
00379     identifyCalibrator (fileNames, calibrator, error, parlist, frameset);
00380     if (*error)
00381     {
00382         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot identify Calibrator");
00383         return;
00384     }
00385 
00386     computeExpectedVis (calibrator, error);
00387     if (*error)
00388     {
00389         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compute Expected Visibilities");
00390         return;
00391     }
00392     
00393     /* Compute Transfer Function */
00394     if (calibrator->calibVis == 0 || calibrator->calibVisSqrd == 0)
00395     {
00396         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00397             "A singularity encountered. Cannot compute transfer functions");
00398         *error = 1;
00399         return;
00400     }
00401     else
00402     {
00403         trf->trf[0] = measVis->vis / calibrator->calibVis;
00404         trf->trfErr[0] = measVis->visErr / (fabs (calibrator->calibVis)) + 
00405             calibrator->calibVisErr * (fabs (measVis->vis) / (calibrator->calibVis * calibrator->calibVis));
00406 
00407         trf->trf[1] = measVis->vis1 / calibrator->calibVis;
00408         trf->trfErr[1] = measVis->vis1Err / (fabs (calibrator->calibVis)) + 
00409             calibrator->calibVisErr * (fabs (measVis->vis1) / (calibrator->calibVis * calibrator->calibVis));
00410 
00411         trf->trf[2] = measVis->vis2 / calibrator->calibVis;
00412         trf->trfErr[2] = measVis->vis2Err / (fabs (calibrator->calibVis)) + 
00413             calibrator->calibVisErr * (fabs (measVis->vis2) / (calibrator->calibVis * calibrator->calibVis));
00414 
00415         trf->trf[3] = measVis->visSqrd / calibrator->calibVisSqrd;
00416         trf->trfErr[3] = measVis->visSqrdErr / (fabs (calibrator->calibVisSqrd)) + 
00417             calibrator->calibVisSqrdErr * (fabs (measVis->visSqrd) / (calibrator->calibVisSqrd * calibrator->calibVisSqrd));
00418 
00419         trf->trf[4] = measVis->visSqrd1 / calibrator->calibVisSqrd;
00420         trf->trfErr[4] = measVis->visSqrd1Err / (fabs (calibrator->calibVisSqrd)) + 
00421             calibrator->calibVisSqrdErr * (fabs (measVis->visSqrd1) / (calibrator->calibVisSqrd * calibrator->calibVisSqrd));
00422 
00423         trf->trf[5] = measVis->visSqrd2 / calibrator->calibVisSqrd;
00424         trf->trfErr[5] = measVis->visSqrd2Err / (fabs (calibrator->calibVisSqrd)) + 
00425             calibrator->calibVisSqrdErr * (fabs (measVis->visSqrd2) / (calibrator->calibVisSqrd * calibrator->calibVisSqrd));
00426     }
00427     
00428     /* Open the transfer function file for writing */
00429     if ((trfPtr = fopen (fileNames->trfNameWrite, "w")) == NULL)
00430     {
00431         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open file to store transfer functions");
00432         *error = 1;
00433         return;
00434     }
00435     else
00436     {
00437        cpl_msg_info(cpl_func,"Transfer Function region 0               = %f \n", trf->trf[0]);
00438        cpl_msg_info(cpl_func,"Transfer Function region 0 Error         = %f \n", trf->trfErr[0]);
00439        cpl_msg_info(cpl_func,"Transfer Function region 1               = %f \n", trf->trf[1]);
00440        cpl_msg_info(cpl_func,"Transfer Function region 1 Error         = %f \n", trf->trfErr[1]);
00441        cpl_msg_info(cpl_func,"Transfer Function region 2               = %f \n", trf->trf[2]);
00442        cpl_msg_info(cpl_func,"Transfer Function region 2 Error         = %f \n", trf->trfErr[2]);
00443        cpl_msg_info(cpl_func,"Transfer Function Squared region 0       = %f \n", trf->trf[3]);
00444        cpl_msg_info(cpl_func,"Transfer Function Squared region 0 Error = %f \n", trf->trfErr[3]);
00445        cpl_msg_info(cpl_func,"Transfer Function Squared region 1       = %f \n", trf->trf[4]);
00446        cpl_msg_info(cpl_func,"Transfer Function Squared region 1 Error = %f \n", trf->trfErr[4]);
00447        cpl_msg_info(cpl_func,"Transfer Function Squared region 2       = %f \n", trf->trf[5]);
00448        cpl_msg_info(cpl_func,"Transfer Function Squared region 2 Error = %f \n", trf->trfErr[5]);
00449         fprintf (midiReportPtr, "Transfer Function region 0               = %f (QCLOG)\n", trf->trf[0]);
00450         fprintf (midiReportPtr, "Transfer Function region 0 Error         = %f (QCLOG)\n", trf->trfErr[0]);
00451         fprintf (midiReportPtr, "Transfer Function region 1               = %f (QCLOG)\n", trf->trf[1]);
00452         fprintf (midiReportPtr, "Transfer Function region 1 Error         = %f (QCLOG)\n", trf->trfErr[1]);
00453         fprintf (midiReportPtr, "Transfer Function region 2               = %f (QCLOG)\n", trf->trf[2]);
00454         fprintf (midiReportPtr, "Transfer Function region 2 Error         = %f (QCLOG)\n", trf->trfErr[2]);
00455         fprintf (midiReportPtr, "Transfer Function Squared region 0       = %f (QCLOG)\n", trf->trf[3]);
00456         fprintf (midiReportPtr, "Transfer Function Squared region 0 Error = %f (QCLOG)\n", trf->trfErr[3]);
00457         fprintf (midiReportPtr, "Transfer Function Squared region 1       = %f (QCLOG)\n", trf->trf[4]);
00458         fprintf (midiReportPtr, "Transfer Function Squared region 1 Error = %f (QCLOG)\n", trf->trfErr[4]);
00459         fprintf (midiReportPtr, "Transfer Function Squared region 2       = %f (QCLOG)\n", trf->trf[5]);
00460         fprintf (midiReportPtr, "Transfer Function Squared region 2 Error = %f (QCLOG)\n", trf->trfErr[5]);
00461 
00462         /* Write transfer function into the file */
00463         for (i = 0; i < 6; i++)
00464         {
00465             fprintf (trfPtr, "%f\n", trf->trf[i]);
00466             fprintf (trfPtr, "%f\n", trf->trfErr[i]);
00467         }
00468         fclose (trfPtr);
00469         if (diagnostic)cpl_msg_info(cpl_func,"Created Transfer Function file   %s\n", fileNames->trfNameWrite);
00470         fprintf (midiReportPtr, "Created Transfer Function file   %s\n", fileNames->trfNameWrite);
00471         trf->exists = 1;
00472     }
00473 
00474     /* Open a file for transfer function history */
00475     trfHistoryPtr = fopen (fileNames->trfHistoryName, "a");
00476     fprintf (trfHistoryPtr, "Transfer Functions (UNDISPERSED) Batch %d \n", batchNumber);
00477     fprintf (trfHistoryPtr, "-------------------------------------- \n");
00478     for (i = 0; i < 6; i++) fprintf (trfHistoryPtr, "%f %f \n", trf->trf[i], trf->trfErr[i]);
00479     fprintf (trfHistoryPtr, "\n");
00480     fclose (trfHistoryPtr);
00481 
00482     return;
00483 }
00484 /*****************************************************************************/
00485 
00486 /******************************************************************************
00487 *                European Southern Observatory
00488 *              VLTI MIDI Data Reduction Software 
00489 *
00490 * Module name:  identifyCalibrator
00491 * Input/Output: See function arguments to avoid duplication
00492 * Description:  This routine identifies the calibrator diameter and computes
00493 *               the average projected baseline.
00494 *
00495 * History:        
00496 * 21-Jul-03        (csabet) Created
00497 ******************************************************************************/
00498 void identifyCalibrator (
00499    MidiFiles          *fileNames,        // In: Pointer to MIDI file structure
00500    CalibratorParam    *calibrator,    // Ou: Calibrator parameters
00501    int                *error,
00502    cpl_parameterlist  *parlist,
00503    cpl_frameset       *frameset)
00504 {
00505 
00506     /* Local Declarations
00507     -------------------*/
00508     const char     routine[] = "identifyCalibrator";
00509     char        *fileNameStr, *qfitsString, *cleanString, *stringTemp, *classification;
00510     int         fileNum;
00511     float       floatDummy, pblStart, pblEnd, parangStart, parangEnd;
00512     FILE        *inFitsBatchPtr = NULL;
00513 
00514     /* Algorithm
00515     ----------*/
00516     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00517     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00518     
00519     /* Initialize parameters */
00520     *error = 0;
00521     pblStart = 0.0;
00522     pblEnd = 0.0;
00523     parangStart = 0.0;
00524     parangEnd = 0.0;
00525     fileNum = 0;
00526 
00527     /* Allocate memory */
00528     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00529     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00530     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00531     fileNameStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00532 
00533     /*  Open the list of files */
00534     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00535     {
00536         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00537         free (cleanString);
00538         free (fileNameStr);
00539         free (classification);
00540         free (stringTemp);
00541         *error = 1;
00542         return;
00543     }
00544 
00545     /* Extract Projected baseline, RA and DEC for the calibrator */
00546     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00547     {
00548         sprintf (classification, "%s", "");
00549         sscanf (stringTemp, "%s%s", fileNameStr, classification);
00550 
00551         /* Ignore the last empty file */
00552         qfitsString = qfits_query_ext (fileNameStr, "HIERARCH ESO DPR TYPE", 0);
00553         if (qfitsString == NULL)
00554         {
00555             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get DPR TYPE from");
00556             *error = 1;
00557             free (cleanString);
00558             free (fileNameStr);
00559             free (classification);
00560             free (stringTemp);
00561             fclose (inFitsBatchPtr);
00562             return;
00563         }
00564         else
00565         {
00566             cleanUpString (qfitsString, cleanString);
00567             if (strcmp (cleanString, "OTHER") != 0)
00568             {
00569                 fileNum++;
00570                 
00571                 /* Get RA and DEC from the first file */
00572                 if (fileNum == 1)
00573                 {
00574                     /* Get RA */
00575                     qfitsString = qfits_query_ext (fileNameStr, "RA", 0);
00576                     if (qfitsString == NULL)
00577                     {
00578                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get RA from");
00579                         *error = 1;
00580                         free (cleanString);
00581                         free (fileNameStr);
00582                         free (classification);
00583                         free (stringTemp);
00584                         fclose (inFitsBatchPtr);
00585                         return;
00586                     }
00587                     else
00588                     {
00589                         cleanUpString (qfitsString, cleanString);
00590                         if (qfits_is_float (cleanString))
00591                         {
00592                             sscanf(cleanString, "%f", &(calibrator->calibRA));
00593                             calibrator->calibRA *= DEG_TO_RAD;
00594                         }
00595                         else
00596                         {
00597                             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-float RA");
00598                             *error = 1;
00599                             free (cleanString);
00600                             free (fileNameStr);
00601                             free (classification);
00602                             free (stringTemp);
00603                             fclose (inFitsBatchPtr);
00604                             return;
00605                         }
00606                     }
00607                 
00608                     /* Get DEC */
00609                     qfitsString = qfits_query_ext (fileNameStr, "DEC", 0);
00610                     if (qfitsString == NULL)
00611                     {
00612                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get DEC");
00613                         *error = 1;
00614                         free (cleanString);
00615                         free (fileNameStr);
00616                         free (classification);
00617                         free (stringTemp);
00618                         fclose (inFitsBatchPtr);
00619                         return;
00620                     }
00621                     else
00622                     {
00623                         cleanUpString (qfitsString, cleanString);
00624                         if (qfits_is_float (cleanString))
00625                         {
00626                             sscanf(cleanString, "%f", &(calibrator->calibDEC));
00627                             calibrator->calibDEC *= DEG_TO_RAD;
00628                         }
00629                         else
00630                         {
00631                             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-float DEC");
00632                             *error = 1;
00633                             free (cleanString);
00634                             free (fileNameStr);
00635                             free (classification);
00636                             free (stringTemp);
00637                             fclose (inFitsBatchPtr);
00638                             return;
00639                         }
00640                     }
00641                     
00642                     /* Get target name */
00643                     qfitsString = qfits_query_ext (fileNameStr, "HIERARCH ESO OBS TARG NAME", 0);
00644                     if (qfitsString == NULL)
00645                     {
00646                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get target name");
00647                         *error = 1;
00648                         free (cleanString);
00649                         free (fileNameStr);
00650                         free (classification);
00651                         free (stringTemp);
00652                         fclose (inFitsBatchPtr);
00653                         return;
00654                     }
00655                     else
00656                     {
00657                         cleanUpString (qfitsString, cleanString);
00658                         sscanf (cleanString, "%s", calibrator->calibName);
00659                     }    
00660                 }
00661 
00662                 /* Get PBL12 START */
00663                 qfitsString = qfits_query_ext (fileNameStr, "HIERARCH ESO ISS PBL12 START", 0);
00664                 if (qfitsString == NULL)
00665                 {
00666                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get PBL12 START");
00667                     *error = 1;
00668                     free (cleanString);
00669                     free (fileNameStr);
00670                     free (classification);
00671                     free (stringTemp);
00672                     fclose (inFitsBatchPtr);
00673                     return;
00674                 }
00675                 else
00676                 {
00677                     cleanUpString (qfitsString, cleanString);
00678                     if (qfits_is_float (cleanString))
00679                     {
00680                         sscanf(cleanString, "%f", &floatDummy);
00681                         if (floatDummy > 0.0)
00682                             pblStart += floatDummy;
00683                         else
00684                         {
00685                             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-positive PBL12 START");
00686                             *error = 1;
00687                             free (cleanString);
00688                             free (fileNameStr);
00689                             free (classification);
00690                             free (stringTemp);
00691                             fclose (inFitsBatchPtr);
00692                             return;
00693                         }
00694                     }
00695                     else
00696                     {
00697                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-float PBL12 START");
00698                         *error = 1;
00699                         free (cleanString);
00700                         free (fileNameStr);
00701                         free (classification);
00702                         free (stringTemp);
00703                         fclose (inFitsBatchPtr);
00704                         return;
00705                     }
00706                 }
00707 
00708                 /* Get PBL12 END */
00709                 qfitsString = qfits_query_ext (fileNameStr, "HIERARCH ESO ISS PBL12 END", 0);
00710                 if (qfitsString == NULL)
00711                 {
00712                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get PBL12 END");
00713                     *error = 1;
00714                     free (cleanString);
00715                     free (fileNameStr);
00716                     free (classification);
00717                     free (stringTemp);
00718                     fclose (inFitsBatchPtr);
00719                     return;
00720                 }
00721                 else
00722                 {
00723                     cleanUpString (qfitsString, cleanString);
00724                     if (qfits_is_float (cleanString))
00725                     {
00726                         sscanf(cleanString, "%f", &floatDummy);
00727                         if (floatDummy > 0.0)
00728                             pblEnd += floatDummy;
00729                         else
00730                         {
00731                             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-positive PBL12 END");
00732                             *error = 1;
00733                              free (cleanString);
00734                             free (fileNameStr);
00735                             free (classification);
00736                             free (stringTemp);
00737                             fclose (inFitsBatchPtr);
00738                             return;
00739                         }
00740                     }
00741                     else
00742                     {
00743                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-float PBL12 END");
00744                         *error = 1;
00745                         free (cleanString);
00746                         free (fileNameStr);
00747                         free (classification);
00748                         free (stringTemp);
00749                         fclose (inFitsBatchPtr);
00750                         return;
00751                     }
00752                 }
00753 
00754                 /* Get PARANG START */
00755                 qfitsString = qfits_query_ext (fileNameStr, "HIERARCH ESO ISS PARANG START", 0);
00756                 if (qfitsString == NULL)
00757                 {
00758                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get PARANG START");
00759                     *error = 1;
00760                     free (cleanString);
00761                     free (fileNameStr);
00762                     free (classification);
00763                     free (stringTemp);
00764                     fclose (inFitsBatchPtr);
00765                     return;
00766                 }
00767                 else
00768                 {
00769                     cleanUpString (qfitsString, cleanString);
00770                     if (qfits_is_float (cleanString))
00771                     {
00772                         sscanf(cleanString, "%f", &floatDummy);
00773                         /*if (floatDummy > 0.0)*/
00774                             parangStart += floatDummy;
00775                         /*else
00776                         {
00777                             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-positive PARANG START");
00778                             *error = 1;
00779                             free (cleanString);
00780                             free (fileNameStr);
00781                             free (classification);
00782                             free (stringTemp);
00783                             fclose (inFitsBatchPtr);
00784                             return;
00785                         }*/
00786                     }
00787                     else
00788                     {
00789                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-float PARANG START in");
00790                         *error = 1;
00791                         free (cleanString);
00792                         free (fileNameStr);
00793                         free (classification);
00794                         free (stringTemp);
00795                         fclose (inFitsBatchPtr);
00796                         return;
00797                     }
00798                 }
00799 
00800                 /* Get PARANG END */
00801                 qfitsString = qfits_query_ext (fileNameStr, "HIERARCH ESO ISS PARANG END", 0);
00802                 if (qfitsString == NULL)
00803                 {
00804                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get PARANG END");
00805                     *error = 1;
00806                     free (cleanString);
00807                     free (fileNameStr);
00808                     free (classification);
00809                     free (stringTemp);
00810                     fclose (inFitsBatchPtr);
00811                     return;
00812                 }
00813                 else
00814                 {
00815                     cleanUpString (qfitsString, cleanString);
00816                     if (qfits_is_float (cleanString))
00817                     {
00818                         sscanf(cleanString, "%f", &floatDummy);
00819                         /*if (floatDummy > 0.0)*/
00820                             parangEnd += floatDummy;
00821                         /*else
00822                         {
00823                             midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-positive PARANG END");
00824                             *error = 1;
00825                              free (cleanString);
00826                             free (fileNameStr);
00827                             free (classification);
00828                             free (stringTemp);
00829                             fclose (inFitsBatchPtr);
00830                             return;
00831                         }*/
00832                     }
00833                     else
00834                     {
00835                         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found non-float PARANG END");
00836                         *error = 1;
00837                         free (cleanString);
00838                         free (fileNameStr);
00839                         free (classification);
00840                         free (stringTemp);
00841                         fclose (inFitsBatchPtr);
00842                         return;
00843                     }
00844                 }
00845             }
00846         }        
00847     }
00848 
00849     /* Compute average of projected baselines and angles */
00850     pblStart /= fileNum;
00851     pblEnd /= fileNum;
00852     calibrator->calibPblAverage = 0.5 * (pblStart + pblEnd);
00853     parangStart /= fileNum;
00854     parangEnd /= fileNum;
00855     calibrator->calibParangAverage = 0.5 * (parangStart + parangEnd);
00856 
00857    cpl_msg_info(cpl_func,"Observed Calibrator                      = %s         \n", calibrator->calibName);
00858    cpl_msg_info(cpl_func,"Observed Calibrator RA                   = %f radians \n", calibrator->calibRA);
00859    cpl_msg_info(cpl_func,"Observed Calibrator DEC                  = %f radians \n", calibrator->calibDEC);
00860    cpl_msg_info(cpl_func,"Observed Calibrator Average PBL          = %f metres  \n", calibrator->calibPblAverage);
00861    cpl_msg_info(cpl_func,"Observed Calibrator Average PARANG       = %f degrees \n", calibrator->calibParangAverage);
00862     fprintf (midiReportPtr, "Observed Calibrator                      = %s         (QCLOG)\n", calibrator->calibName);
00863     fprintf (midiReportPtr, "Observed Calibrator RA                   = %f radians (QCLOG)\n", calibrator->calibRA);
00864     fprintf (midiReportPtr, "Observed Calibrator DEC                  = %f radians (QCLOG)\n", calibrator->calibDEC);
00865     fprintf (midiReportPtr, "Observed Calibrator Average PBL          = %f metres  (QCLOG)\n", calibrator->calibPblAverage);
00866     fprintf (midiReportPtr, "Observed Calibrator Average PARANG       = %f degrees (QCLOG)\n", calibrator->calibParangAverage);
00867 
00868     /* Get diameter of the calibrator from the Calibrator database */
00869     getCalibratorDiameter (fileNames, calibrator, error, parlist, frameset);
00870     if (*error) 
00871     {
00872         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get Calibrator Diameter");
00873         free (cleanString);
00874         free (fileNameStr);
00875         free (classification);
00876         free (stringTemp);
00877         fclose (inFitsBatchPtr);
00878         return;
00879     }
00880 
00881     /* Release memory and close files */
00882     free (fileNameStr);
00883     fclose (inFitsBatchPtr);
00884     free (cleanString);
00885     free (classification);
00886     free (stringTemp);
00887 
00888     return;
00889 }
00890 /*****************************************************************************/
00891 
00892 
00893 /******************************************************************************
00894 *                European Southern Observatory
00895 *              VLTI MIDI Data Reduction Software 
00896 *
00897 * Module name:    getCalibratorDiameter
00898 * Input/Output:    See function arguments to avoid duplication
00899 * Description:    Searches through the CalibDB and extracts the calibrator
00900 *               diameter. there are 36 elements in one line. there are
00901 *               9 useful elements and UNWANTED_ELEMENTS dummy.
00902 *
00903 *
00904 * History:        
00905 * 26-Feb-04        (csabet) Created
00906 ******************************************************************************/
00907 void getCalibratorDiameter (
00908    MidiFiles        *fileNames,
00909    CalibratorParam    *calibrator,
00910    int                *error,
00911    cpl_parameterlist *parlist,
00912    cpl_frameset      *frameset)
00913 {
00914    
00915    /*    Local Declarations
00916          --------------------*/
00917    const char     routine[] = "getCalibratorDiameter";
00918    int            i;
00919    double          searchRA, searchDEC, diffAngle;
00920 
00921    cpl_errorstate    prestate = cpl_errorstate_get();   
00922 
00923    cpl_frame       * cur_frame;
00924 
00925    cpl_table       * table;
00926 
00927    char           ** Name=NULL;
00928    int             * hourRA=NULL;      
00929    int             * minuteRA=NULL;    
00930    double           * secondRA=NULL;    
00931    int             * signDEC=NULL;     
00932    int             * degreeDEC=NULL;   
00933    int             * minuteDEC=NULL;   
00934    double           * secondDEC=NULL;   
00935    double          * flux=NULL;        
00936    int             * flag=NULL;        
00937    double           * diameter=NULL;    
00938    double           * diameterErr=NULL; 
00939 
00940    int               ext_calibrator_data=0;
00941    int               dimen_calibrator_data=0;
00942    char            * tag=NULL;   
00943 
00944 
00945 
00946    /*    Algorithm
00947          -----------*/
00948    if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00949    if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00950    
00951    
00952    
00953    cur_frame = cpl_frameset_get_first(frameset);
00954    if (cur_frame == NULL) {
00955       cpl_msg_error(cpl_func, "No frame found in the SOF");  
00956       *error = 1;
00957       return ;
00958       
00959    }
00960 
00961    /*  Find the calibrator database file in the SOF */
00962    while(cur_frame)
00963    {
00964       /*      Check the right tags */
00965       tag = (char*)cpl_frame_get_tag(cur_frame);
00966       if (strcmp(tag, MIDI_CALIB_DATABASE)) {
00967          cur_frame = cpl_frameset_get_next( frameset );   
00968          continue;
00969       }
00970       else{
00971          cpl_msg_info(cpl_func, "Calibrator database found in the SOF: %s",cpl_frame_get_filename(cur_frame));   
00972          break;
00973       }
00974    }
00975    
00976    if (strcmp(tag, MIDI_CALIB_DATABASE)) {
00977       cpl_msg_warning(cpl_func, "No calibrator database file found in the SOF!");   
00978       cpl_msg_warning(cpl_func, "Transfer function can not be calculated!");
00979       *error = 1;
00980       return;
00981    }
00982    
00983    
00984    /* Load extension CALIBRATOR_DATA */ 
00985    
00986    prestate = cpl_errorstate_get();   
00987    
00988    cpl_msg_info(cpl_func, "Reading %s",cpl_frame_get_filename(cur_frame));
00989    
00990    ext_calibrator_data=cpl_fits_find_extension(cpl_frame_get_filename(cur_frame),"CALIBRATOR_DATA");
00991 
00992    /* Load extension  */ 
00993    if((table = cpl_table_load(cpl_frame_get_filename(cur_frame), ext_calibrator_data, 1))==NULL)
00994    {
00995       cpl_msg_info(cpl_func, "No siutable table found in the file: %s",cpl_frame_get_filename(cur_frame));   
00996       cpl_msg_warning(cpl_func, "Transfer function can not be calculated!");
00997       cpl_errorstate_set(prestate);  
00998       *error = 1;
00999       return;
01000    }
01001    
01002    
01003    if (cpl_table_has_column(table, "Name"       )) {Name       =cpl_table_get_data_string(table, "Name"       );} else {cpl_msg_warning(cpl_func, "Entry not found:  Name"       );*error = 1;};
01004    if (cpl_table_has_column(table, "hourRA"     )) {hourRA     =cpl_table_get_data_int   (table, "hourRA"     );} else {cpl_msg_warning(cpl_func, "Entry not found:  hourRA"     );*error = 1;};
01005    if (cpl_table_has_column(table, "minuteRA"   )) {minuteRA   =cpl_table_get_data_int   (table, "minuteRA"   );} else {cpl_msg_warning(cpl_func, "Entry not found:  minuteRA"   );*error = 1;};
01006    if (cpl_table_has_column(table, "secondRA"   )) {secondRA   =cpl_table_get_data_double (table, "secondRA"   );} else {cpl_msg_warning(cpl_func, "Entry not found:  secondRA"   );*error = 1;};
01007    if (cpl_table_has_column(table, "signDEC"    )) {signDEC    =cpl_table_get_data_int   (table, "signDEC"    );} else {cpl_msg_warning(cpl_func, "Entry not found:  signDEC"    );*error = 1;};
01008    if (cpl_table_has_column(table, "degreeDEC"  )) {degreeDEC  =cpl_table_get_data_int   (table, "degreeDEC"  );} else {cpl_msg_warning(cpl_func, "Entry not found:  degreeDEC"  );*error = 1;};
01009    if (cpl_table_has_column(table, "minuteDEC"  )) {minuteDEC  =cpl_table_get_data_int   (table, "minuteDEC"  );} else {cpl_msg_warning(cpl_func, "Entry not found:  minuteDEC"  );*error = 1;};
01010    if (cpl_table_has_column(table, "secondDEC"  )) {secondDEC  =cpl_table_get_data_double (table, "secondDEC"  );} else {cpl_msg_warning(cpl_func, "Entry not found:  secondDEC"  );*error = 1;};
01011    if (cpl_table_has_column(table, "N_band"     )) {flux       =cpl_table_get_data_double(table, "N_band"     );} else {cpl_msg_warning(cpl_func, "Entry not found:  N_band"     );*error = 1;};
01012    if (cpl_table_has_column(table, "flag"       )) {flag       =cpl_table_get_data_int   (table, "flag"       );} else {cpl_msg_warning(cpl_func, "Entry not found:  flag"       );*error = 1;};
01013    if (cpl_table_has_column(table, "diameter"   )) {diameter   =cpl_table_get_data_double (table, "diameter"   );} else {cpl_msg_warning(cpl_func, "Entry not found:  diameter"   );*error = 1;};
01014    if (cpl_table_has_column(table, "diameterErr")) {diameterErr=cpl_table_get_data_double (table, "diameterErr");} else {cpl_msg_warning(cpl_func, "Entry not found:  diameterErr");*error = 1;};
01015    
01016    dimen_calibrator_data=cpl_table_get_nrow(table);
01017    
01018    cpl_msg_info(cpl_func, "Total number of Calibrators in the table: %d", dimen_calibrator_data);      
01019 
01020    calibrator->calibDistance =FLT_MAX;
01021    
01022       /* Search for the minimum distance and get the diameter */
01023       for (i=0; i<dimen_calibrator_data;i++)
01024       {
01025          
01026          searchRA  = HOUR_TO_RAD * ((double) hourRA[i] + minuteRA[i]/60.0 + secondRA[i]/3600.0);
01027          searchDEC = DEG_TO_RAD * (((double) degreeDEC[i] + minuteDEC[i]/60.0 + secondDEC[i]/3600.0) * signDEC[i]);
01028          diffAngle = acos ( sin(searchDEC) * sin(calibrator->calibDEC) + 
01029                             cos(searchDEC) * cos(calibrator->calibDEC) * cos(searchRA - calibrator->calibRA) );
01030 
01031          /* cpl_msg_info(cpl_func, " i: %d  diffAngle : %f    Name: %s", i, diffAngle,Name[i]); */
01032          if (diffAngle < calibrator->calibDistance) 
01033          {
01034             calibrator->calibDistance = (float)diffAngle;
01035             calibrator->calibDiameter = (float)diameter[i];
01036             calibrator->calibDiameterErr = (float)diameterErr[i];
01037             strcpy (calibrator->calibName, Name[i]);
01038             calibrator->calibFlux = flux[i];  // Flux in the N-Band (Actually the magnitude)
01039             calibrator->calibFlag = flag[i]; // Quality Parameter/Flag
01040             
01041          }
01042          
01043       }
01044       
01045 
01046     /* Check if minimum angle is within the threshold */
01047     if (calibrator->calibDistance <= CALIBRATOR_MIN_THRESHOLD)
01048     {
01049        cpl_msg_info(cpl_func,"Closest Calibrator in the database       = %s         \n", calibrator->calibName);
01050        cpl_msg_info(cpl_func,"Calibrator Diameter                      = %f marcsec \n", calibrator->calibDiameter);
01051        cpl_msg_info(cpl_func,"Calibrator Diameter error                = %f marcsec \n", calibrator->calibDiameterErr);
01052        cpl_msg_info(cpl_func,"Computed Distance to Observed Calibrator = %f arcsec  \n", RAD_TO_ARCSEC * calibrator->calibDistance);
01053        cpl_msg_info(cpl_func,"Calibrator Flux in the N-Band            = %g mag     \n", calibrator->calibFlux);
01054        cpl_msg_info(cpl_func,"Calibrator quality flag                  = %d         \n", calibrator->calibFlag);
01055        
01056        fprintf (midiReportPtr, "Closest Calibrator in the database       = %s         (QCLOG)\n", calibrator->calibName);
01057        fprintf (midiReportPtr, "Calibrator Diameter                      = %f marcsec (QCLOG)\n", calibrator->calibDiameter);
01058        fprintf (midiReportPtr, "Calibrator Diameter error                = %f marcsec (QCLOG)\n", calibrator->calibDiameterErr);
01059        fprintf (midiReportPtr, "Computed Distance to Observed Calibrator = %f arcsec  (QCLOG)\n", RAD_TO_ARCSEC * calibrator->calibDistance);
01060        fprintf (midiReportPtr, "Calibrator Flux in the N-Band            = %g mag     (QCLOG)\n", calibrator->calibFlux);
01061        fprintf (midiReportPtr, "Calibrator quality flag                  = %d         (QCLOG)\n", calibrator->calibFlag);
01062     }
01063     else
01064     {
01065         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find a close match in the database");
01066         *error = 1;
01067     }
01068 
01069     /* Release memory */
01070     if(table !=NULL) cpl_table_delete(table);
01071     
01072 /*     fclose (calibDbPtr); */
01073 /*     for (i = 0; i < UNWANTED_ELEMENTS; i++) */
01074 /*         free (dummyStr[i]); */
01075 /*     free (dummyStr); */
01076 /*     free (lineStr); */
01077     
01078     return;
01079 }
01080 /*****************************************************************************/
01081 /* /\****************************************************************************** */
01082 /* *                European Southern Observatory */
01083 /* *              VLTI MIDI Data Reduction Software  */
01084 /* * */
01085 /* * Module name:    getCalibratorDiameter */
01086 /* * Input/Output:    See function arguments to avoid duplication */
01087 /* * Description:    Searches through the CalibDB and extracts the calibrator */
01088 /* *               diameter. there are 36 elements in one line. there are */
01089 /* *               9 useful elements and UNWANTED_ELEMENTS dummy. */
01090 /* * */
01091 /* * */
01092 /* * History:         */
01093 /* * 26-Feb-04        (csabet) Created */
01094 /* ******************************************************************************\/ */
01095 /* void getCalibratorDiameter ( */
01096 /*    MidiFiles        *fileNames, */
01097 /*    CalibratorParam    *calibrator, */
01098 /*    int                *error, */
01099 /*    cpl_parameterlist *parlist, */
01100 /*    cpl_frameset      *frameset) */
01101 /* { */
01102 
01103 /*     /\*    Local Declarations */
01104 /*     --------------------*\/ */
01105 /*     const char     routine[] = "getCalibratorDiameter"; */
01106 /*     int         i, lineNum,flag; */
01107 /*     int         hourRA, minuteRA, signDEC, degreeDEC, minuteDEC; */
01108 /*     float       secondRA, secondDEC, diameter, diameterErr; */
01109 /*     double flux;  */
01110 /*    char        **dummyStr, *lineStr; */
01111 /*     float       searchRA, searchDEC, diffAngle; */
01112 /*     FILE        *calibDbPtr; */
01113     
01114 /*     /\*    Algorithm */
01115 /*     -----------*\/ */
01116 /*     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine); */
01117 /*     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine); */
01118 
01119 /*     /\* Allocate memory *\/ */
01120 /*     lineStr = (char *) calloc (MAX_STRING_LENGTH, sizeof (char)); */
01121 /*     dummyStr = (char **) calloc (UNWANTED_ELEMENTS, sizeof (char *)); */
01122 /*     for (i = 0; i < UNWANTED_ELEMENTS; i++) */
01123 /*         dummyStr[i] = (char *) calloc (CALIB_DB_LEN, sizeof (char)); */
01124 
01125 /*     /\* Reset parameters *\/ */
01126 /*     *error = 0; */
01127 /*     lineNum = 0; */
01128 
01129 /*     /\*  Open the calibDB file for reading *\/ */
01130 /*     if ((calibDbPtr = fopen (fileNames->calibDbName, "r")) == NULL) */
01131 /*     { */
01132 /*         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open calibrator database"); */
01133 /*         *error = 1; */
01134 /*         for (i = 0; i < UNWANTED_ELEMENTS; i++) */
01135 /*             free (dummyStr[i]); */
01136 /*         free (dummyStr); */
01137 /*         free (lineStr); */
01138 /*         return; */
01139 /*     } */
01140 
01141 /*     /\* Loop through the calibrator database and identify the closest RA and DEC *\/ */
01142 /*     while (fgets (lineStr, MAX_STRING_LENGTH, calibDbPtr) != NULL) */
01143 /*     { */
01144 /*         sscanf (lineStr, "%s%d%d%f%d%d%d%f%s%s%s%s%s%s%s%s%lg%s%s%s%d%f%f%s%s%s%s%s%s%s%s%s%s%s%s%s",  */
01145 /*             dummyStr[0], &hourRA, &minuteRA, &secondRA, &signDEC, &degreeDEC, &minuteDEC, &secondDEC, dummyStr[1], dummyStr[2],  */
01146 /*             dummyStr[3], dummyStr[4], dummyStr[5], dummyStr[6], dummyStr[7], dummyStr[8], &flux, dummyStr[10],  */
01147 /*             dummyStr[11], dummyStr[12], &flag, &diameter, &diameterErr, dummyStr[14], dummyStr[15], dummyStr[16],  */
01148 /*             dummyStr[17], dummyStr[18], dummyStr[19], dummyStr[20], dummyStr[21], dummyStr[22], dummyStr[23], dummyStr[24], */
01149 /*             dummyStr[25], dummyStr[26]); */
01150         
01151 /*         lineNum++; */
01152         
01153 /*         /\* Find initial difference *\/ */
01154 /*         if (lineNum == START_MIDI_RECORD) */
01155 /*         { */
01156 /*             searchRA = HOUR_TO_RAD * ((float) hourRA + minuteRA/60.0 + secondRA/3600.0); */
01157 /*             searchDEC = DEG_TO_RAD * (((float) degreeDEC + minuteDEC/60.0 + secondDEC/3600.0) * signDEC); */
01158 /*             calibrator->calibDistance = acos ( sin(searchDEC) * sin(calibrator->calibDEC) +  */
01159 /*                 cos(searchDEC) * cos(calibrator->calibDEC) * cos(searchRA - calibrator->calibRA) ); */
01160 /*             calibrator->calibDiameter = diameter; */
01161 /*             calibrator->calibDiameterErr = diameterErr; */
01162 /*             sscanf (dummyStr[0], "%s", calibrator->calibName); */
01163 /*         } */
01164         
01165 /*         /\* Now search for the minimum distance and get the diameter *\/ */
01166 /*         if (lineNum > START_MIDI_RECORD && lineNum <= END_MIDI_RECORD) */
01167 /*         { */
01168 /*             searchRA = HOUR_TO_RAD * ((float) hourRA + minuteRA/60.0 + secondRA/3600.0); */
01169 /*             searchDEC = DEG_TO_RAD * (((float) degreeDEC + minuteDEC/60.0 + secondDEC/3600.0) * signDEC); */
01170 /*             diffAngle = acos ( sin(searchDEC) * sin(calibrator->calibDEC) +  */
01171 /*                 cos(searchDEC) * cos(calibrator->calibDEC) * cos(searchRA - calibrator->calibRA) ); */
01172 /*             if (diffAngle < calibrator->calibDistance)  */
01173 /*             { */
01174 /*                 calibrator->calibDistance = diffAngle; */
01175 /*                 calibrator->calibDiameter = diameter; */
01176 /*                 calibrator->calibDiameterErr = diameterErr; */
01177 /*                 sscanf (dummyStr[0], "%s", calibrator->calibName); */
01178 /*                 calibrator->calibFlux = flux;  // Flux in the N-Band */
01179 /*                 calibrator->calibFlag = flag; // Quality Parameter/Flag */
01180 
01181 
01182 /*             } */
01183 /*         } */
01184 /*         if (lineNum > END_MIDI_RECORD) break; */
01185 /*     } */
01186 
01187 /*     /\* Check if there was enough record in the file *\/ */
01188 /*     if (lineNum < START_MIDI_RECORD) */
01189 /*     { */
01190 /*         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Not enough records in database"); */
01191 /*         *error = 1; */
01192 /*         for (i = 0; i < UNWANTED_ELEMENTS; i++) */
01193 /*             free (dummyStr[i]); */
01194 /*         free (dummyStr); */
01195 /*         free (lineStr); */
01196 /*         return; */
01197 /*     } */
01198     
01199 /*     /\* Check if minimum angle is within the threshold *\/ */
01200 /*     if (calibrator->calibDistance <= CALIBRATOR_MIN_THRESHOLD) */
01201 /*     { */
01202 /*        cpl_msg_info(cpl_func,"Closest Calibrator in the database       = %s         \n", calibrator->calibName); */
01203 /*        cpl_msg_info(cpl_func,"Calibrator Diameter                      = %f marcsec \n", calibrator->calibDiameter); */
01204 /*        cpl_msg_info(cpl_func,"Calibrator Diameter error                = %f marcsec \n", calibrator->calibDiameterErr); */
01205 /*        cpl_msg_info(cpl_func,"Computed Distance to Observed Calibrator = %f arcsec  \n", RAD_TO_ARCSEC * calibrator->calibDistance); */
01206 /*        cpl_msg_info(cpl_func,"Calibrator Flux in the N-Band            = %g mag     \n", calibrator->calibFlux); */
01207 /*        cpl_msg_info(cpl_func,"Calibrator quality flag                  = %d         \n", calibrator->calibFlag); */
01208 
01209 /*         fprintf (midiReportPtr, "Closest Calibrator in the database       = %s         (QCLOG)\n", calibrator->calibName); */
01210 /*         fprintf (midiReportPtr, "Calibrator Diameter                      = %f marcsec (QCLOG)\n", calibrator->calibDiameter); */
01211 /*         fprintf (midiReportPtr, "Calibrator Diameter error                = %f marcsec (QCLOG)\n", calibrator->calibDiameterErr); */
01212 /*         fprintf (midiReportPtr, "Computed Distance to Observed Calibrator = %f arcsec  (QCLOG)\n", RAD_TO_ARCSEC * calibrator->calibDistance); */
01213 /*         fprintf (midiReportPtr, "Calibrator Flux in the N-Band            = %g mag     (QCLOG)\n", calibrator->calibFlux); */
01214 /*         fprintf (midiReportPtr, "Calibrator quality flag                  = %d         (QCLOG)\n", calibrator->calibFlag); */
01215 /*     } */
01216 /*     else */
01217 /*     { */
01218 /*         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find a close match in the database"); */
01219 /*         *error = 1; */
01220 /*     } */
01221 
01222 /*     /\* Release memory and close file *\/ */
01223 /*     fclose (calibDbPtr); */
01224 /*     for (i = 0; i < UNWANTED_ELEMENTS; i++) */
01225 /*         free (dummyStr[i]); */
01226 /*     free (dummyStr); */
01227 /*     free (lineStr); */
01228     
01229 /*     return; */
01230 /* } */
01231 /*****************************************************************************/
01232 
01233 
01234 
01235 /******************************************************************************
01236 *                European Southern Observatory
01237 *              VLTI MIDI Data Reduction Software 
01238 *
01239 * Module name:    computeExpectedVis
01240 * Input/Output:    See function arguments to avoid duplication
01241 * Description:    
01242 *
01243 *
01244 * History:        
01245 * 21-Jul-03        (csabet) Created
01246 ******************************************************************************/
01247 void computeExpectedVis (
01248     CalibratorParam    *calibrator,    // Ou: Calibrator parameters
01249     int                *error)            // Ou: Status
01250 {
01251     /*    Local Declarations
01252     --------------------*/
01253     const char     routine[] = "computeExpectedVis";
01254     double      besselK1o1, besselK1o1Err, xArg, xArgErr;
01255     
01256     /*    Algorithm
01257     -----------*/
01258     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01259     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01260 
01261     /* Reset parameters */
01262     *error = 0;
01263     
01264     /* Compute Bessel function of first kind of order 1
01265     For faster processing most divisions are stated by multiplying factors */
01266     xArg = (double) (MIDI_PI * DIAMETER_FACTOR * calibrator->calibDiameter * calibrator->calibPblAverage * INVERSE_WAVELENGTH);
01267     xArgErr = (double) (MIDI_PI * DIAMETER_FACTOR * calibrator->calibDiameterErr * calibrator->calibPblAverage * INVERSE_WAVELENGTH);
01268     besselK1o1 = computeBesselK1o1 (xArg, xArgErr, &besselK1o1Err);
01269 
01270     /* Compute expected visibilities */
01271     calibrator->calibVis = (float) (2.0 * (besselK1o1 / xArg));
01272     calibrator->calibVisSqrd = calibrator->calibVis * calibrator->calibVis;
01273 
01274     /* Compute errors */
01275     calibrator->calibVisErr = (float) (2.0 * (besselK1o1Err/fabs(xArg) + xArgErr * (fabs(besselK1o1)/(xArg * xArg))));
01276     calibrator->calibVisSqrdErr = 2.0 * (calibrator->calibVisErr * fabs (calibrator->calibVis));
01277 
01278    cpl_msg_info(cpl_func,"Expected Visibility                      = %f \n", calibrator->calibVis);
01279    cpl_msg_info(cpl_func,"Expected Visibility Error                = %f \n", calibrator->calibVisErr);
01280    cpl_msg_info(cpl_func,"Expected Squared Visibility              = %f \n", calibrator->calibVisSqrd);
01281    cpl_msg_info(cpl_func,"Expected Squared Visibility Error        = %f \n", calibrator->calibVisSqrdErr);
01282     fprintf (midiReportPtr, "Expected Visibility                      = %f (QCLOG)\n", calibrator->calibVis);
01283     fprintf (midiReportPtr, "Expected Visibility Error                = %f (QCLOG)\n", calibrator->calibVisErr);
01284     fprintf (midiReportPtr, "Expected Squared Visibility              = %f (QCLOG)\n", calibrator->calibVisSqrd);
01285     fprintf (midiReportPtr, "Expected Squared Visibility Error        = %f (QCLOG)\n", calibrator->calibVisSqrdErr);
01286 
01287     return;
01288 }
01289 /*****************************************************************************/
01290 
01291 
01292 /******************************************************************************
01293 *                European Southern Observatory
01294 *              VLTI MIDI Data Reduction Software 
01295 *
01296 * Module name:    computeBesselK1o1
01297 * Input/Output:    See function arguments to avoid duplication
01298 * Description:    Computes bessel function of first kind of order 1 and the
01299 *               associated errors.
01300 *
01301 *
01302 * History:        
01303 * 01-Mar-04        (csabet) Created
01304 ******************************************************************************/
01305 double computeBesselK1o1 (  /* Ou: Computed Bessel function */
01306     double xArg,            /* In: Bessel argument */
01307     double xArgErr,         /* In: Error of argument */
01308     double *besselK1o1Err)  /* Ou: Error of computed Bessel function */
01309 {
01310     /*    Local Declarations
01311     --------------------*/
01312     const char     routine[] = "computeBesselK1o1";
01313     
01314     /*    Algorithm
01315     -----------*/
01316     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01317     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01318 
01319     *besselK1o1Err = (
01320         0.5 * xArgErr +
01321         0.062500000000 * xArgErr * fabs (3.0 * xArg * xArg) +
01322         0.002604166666 * xArgErr * fabs (5.0 * xArg * xArg * xArg * xArg) +
01323         0.000054253472 * xArgErr * fabs (7.0 * xArg * xArg * xArg * xArg * xArg * xArg));
01324         
01325     return (
01326         0.5 * xArg - 
01327         0.062500000000 * xArg * xArg * xArg + 
01328         0.002604166666 * xArg * xArg * xArg * xArg * xArg -
01329         0.000054253472 * xArg * xArg * xArg * xArg * xArg * xArg * xArg);
01330 }
01331 /*****************************************************************************/
01332 
01333 
01334 /******************************************************************************
01335 *                European Southern Observatory
01336 *              VLTI MIDI Data Reduction Software 
01337 *
01338 * Module name:  computeBinnedTrf
01339 * Input/Output: See function arguments to avoid duplication
01340 * Description:  This routine computes the transfer function at 5 spectral
01341 *                bands as follows:
01342 *
01343 *                1)  8.64 microns (Channels 120 -> 126) PRISM
01344 *                                 (Channels  38 ->  50) GRISM
01345 *                2)  9.00 microns (Channels 113 -> 121) PRISM
01346 *                                 (Channels  52 ->  68) GRISM
01347 *                3) 10.46 microns (Channels  88 ->  99) PRISM
01348 *                                 (Channels 116 -> 136) GRISM
01349 *                4) 11.79 microns (Channels  60 ->  72) PRISM
01350 *                                 (Channels 176 -> 200) GRISM
01351 *                5) 12.80 microns (Channels  36 ->  50) PRISM
01352 *                                 (Channels 217 -> 245) GRISM
01353 *
01354 * History:        
01355 * 01-Jul-08        Created
01356 ******************************************************************************/
01357 void computeBinnedTrf (
01358     ImageFormat        *format,        // In: General data format
01359     DispersedResult    *dispResult,    // In: Dispersed result structure
01360     int                *error)            // Ou: Error status
01361 {
01362 
01363     //    Local Declarations
01364     //    ------------------
01365     const char    routine[] = "computeBinnedTrf";
01366     int            i, n;
01367 
01368     //    Algorithm
01369     //    ---------
01370    cpl_msg_info(cpl_func,"\nBinned Transfer Function for batch  %d \n", batchNumber);
01371    cpl_msg_info(cpl_func,"------------------------------------------ \n");
01372     fprintf (midiReportPtr, "\nBinned Transfer Function for batch  %d QCLOG \n", batchNumber);
01373     fprintf (midiReportPtr, "------------------------------------------ QCLOG \n");
01374 
01375     //    Reset parameters
01376     *error = 0;
01377     n = 0;
01378 
01379     dispResult->trfBinned1 = 0.0;
01380     dispResult->trfBinned2 = 0.0;
01381     dispResult->trfBinned3 = 0.0;
01382     dispResult->trfBinned4 = 0.0;
01383     dispResult->trfBinned5 = 0.0;
01384 
01385     if (strcmp(format->grismId, "PRISM") == 0)
01386     {
01387        n = 0;
01388        for (i = 36; i < 51; i++)
01389        {
01390           if (badChannelList[i])
01391              continue;
01392                 
01393           dispResult->trfBinned5 += (dispResult->trf)[i];
01394           n++;
01395        }
01396        if(n>0){
01397           dispResult->trfBinned5 /= n;
01398        }
01399        else{
01400           dispResult->trfBinned5= -1.;   
01401        }
01402         
01403        n = 0;
01404        for (i = 60; i < 73; i++)
01405        {
01406           if (badChannelList[i])
01407              continue;
01408                 
01409           dispResult->trfBinned4 += (dispResult->trf)[i];
01410           n++;
01411        }
01412        if(n>0){
01413           dispResult->trfBinned4 /= n;
01414        }
01415        else{
01416           dispResult->trfBinned4= -1.;   
01417        }
01418 
01419        n = 0;
01420        for (i = 88; i < 99; i++)
01421        {
01422           if (badChannelList[i])
01423              continue;
01424                 
01425           dispResult->trfBinned3 += (dispResult->trf)[i];
01426           n++;
01427        }
01428        if(n>0){
01429           dispResult->trfBinned3 /= n;
01430        }
01431        else{
01432           dispResult->trfBinned3= -1.;   
01433        }
01434 
01435        n = 0;
01436        for (i = 113; i < 122; i++)
01437        {
01438           if (badChannelList[i])
01439              continue;
01440                 
01441           dispResult->trfBinned2 += (dispResult->trf)[i];
01442           n++;
01443        }
01444        if(n>0){
01445           dispResult->trfBinned2 /= n;
01446        }
01447        else{
01448           dispResult->trfBinned2= -1.;   
01449        }
01450 
01451        n = 0;
01452        for (i = 120; i < 127; i++)
01453        {
01454           if (badChannelList[i])
01455              continue;
01456                 
01457           dispResult->trfBinned1 += (dispResult->trf)[i];
01458           n++;
01459        }
01460 
01461        if(n>0){
01462           dispResult->trfBinned1 /= n;
01463        }
01464        else{
01465           dispResult->trfBinned1= -1.;   
01466        }
01467     }
01468     else if (strcmp(format->grismId, "GRISM") == 0)
01469     {
01470        n = 0;
01471        for (i = 38; i < 51; i++)
01472        {
01473           if (badChannelList[i])
01474              continue;
01475                 
01476           dispResult->trfBinned1 += (dispResult->trf)[i];
01477           n++;
01478        }
01479        if(n>0){
01480           dispResult->trfBinned1 /= n;
01481        }
01482        else{
01483           dispResult->trfBinned1= -1.;   
01484        }
01485 
01486        n = 0;
01487        for (i = 52; i < 69; i++)
01488        {
01489           if (badChannelList[i])
01490              continue;
01491                 
01492           dispResult->trfBinned2 += (dispResult->trf)[i];
01493           n++;
01494        }
01495        if(n>0){
01496           dispResult->trfBinned2 /= n;
01497        }
01498        else{
01499           dispResult->trfBinned2= -1.;   
01500        }
01501 
01502        n = 0;
01503        for (i = 116; i < 137; i++)
01504        {
01505           if (badChannelList[i])
01506              continue;
01507                 
01508           dispResult->trfBinned3 += (dispResult->trf)[i];
01509           n++;
01510        }
01511        if(n>0){
01512           dispResult->trfBinned3 /= n;
01513        }
01514        else{
01515           dispResult->trfBinned3= -1.;   
01516        }
01517 
01518        n = 0;
01519        for (i = 176; i < 201; i++)
01520        {
01521           if (badChannelList[i])
01522              continue;
01523                 
01524           dispResult->trfBinned4 += (dispResult->trf)[i];
01525           n++;
01526        }
01527        if(n>0){
01528           dispResult->trfBinned4 /= n;
01529        }
01530        else{
01531           dispResult->trfBinned4= -1.;   
01532        }
01533 
01534        n = 0;
01535        for (i = 217; i < 246; i++)
01536        {
01537           if (badChannelList[i])
01538              continue;
01539                 
01540           dispResult->trfBinned5 += (dispResult->trf)[i];
01541           n++;
01542        }
01543        if(n>0){
01544           dispResult->trfBinned5 /= n;
01545        }
01546        else{
01547           dispResult->trfBinned5= -1.;   
01548        }
01549     }
01550     else
01551     {
01552         sprintf (midiMessage, "Unknown GRISM ID ... %s", format->grismId);
01553         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01554         *error = 1;
01555         return;
01556     }
01557 
01558     //    Report
01559    cpl_msg_info(cpl_func,"Grism ID: %s \n", format->grismId);
01560     fprintf (midiReportPtr, "Grism ID: %s QCLOG \n", format->grismId);
01561     if (strcmp(format->grismId, "PRISM") == 0)
01562     {
01563        cpl_msg_info(cpl_func," 8.64 micron (Channel 120 -> 126) = %0.4f \n", dispResult->trfBinned1);
01564        cpl_msg_info(cpl_func," 9.00 micron (Channel 113 -> 121) = %0.4f \n", dispResult->trfBinned2);
01565        cpl_msg_info(cpl_func,"10.46 micron (Channel  88 ->  98) = %0.4f \n", dispResult->trfBinned3);
01566        cpl_msg_info(cpl_func,"11.79 micron (Channel  60 ->  72) = %0.4f \n", dispResult->trfBinned4);
01567        cpl_msg_info(cpl_func,"12.80 micron (Channel  36 ->  50) = %0.4f \n", dispResult->trfBinned5);
01568 
01569         fprintf (midiReportPtr, " 8.64 micron (Channel 120 -> 126) = %0.4f QCLOG \n", dispResult->trfBinned1);
01570         fprintf (midiReportPtr, " 9.00 micron (Channel 113 -> 121) = %0.4f QCLOG \n", dispResult->trfBinned2);
01571         fprintf (midiReportPtr, "10.46 micron (Channel  88 ->  98) = %0.4f QCLOG \n", dispResult->trfBinned3);
01572         fprintf (midiReportPtr, "11.79 micron (Channel  60 ->  72) = %0.4f QCLOG \n", dispResult->trfBinned4);
01573         fprintf (midiReportPtr, "12.80 micron (Channel  36 ->  50) = %0.4f QCLOG \n", dispResult->trfBinned5);
01574     }
01575     else
01576     {
01577        cpl_msg_info(cpl_func," 8.64 micron (Channel  38 ->  50) = %0.4f \n", dispResult->trfBinned1);
01578        cpl_msg_info(cpl_func," 9.00 micron (Channel  52 ->  68) = %0.4f \n", dispResult->trfBinned2);
01579        cpl_msg_info(cpl_func,"10.46 micron (Channel 116 -> 136) = %0.4f \n", dispResult->trfBinned3);
01580        cpl_msg_info(cpl_func,"11.79 micron (Channel 176 -> 200) = %0.4f \n", dispResult->trfBinned4);
01581        cpl_msg_info(cpl_func,"12.80 micron (Channel 217 -> 245) = %0.4f \n", dispResult->trfBinned5);
01582 
01583         fprintf (midiReportPtr, " 8.64 micron (Channel  38 ->  50) = %0.4f QCLOG \n", dispResult->trfBinned1);
01584         fprintf (midiReportPtr, " 9.00 micron (Channel  52 ->  68) = %0.4f QCLOG \n", dispResult->trfBinned2);
01585         fprintf (midiReportPtr, "10.46 micron (Channel 116 -> 136) = %0.4f QCLOG \n", dispResult->trfBinned3);
01586         fprintf (midiReportPtr, "11.79 micron (Channel 176 -> 200) = %0.4f QCLOG \n", dispResult->trfBinned4);
01587         fprintf (midiReportPtr, "12.80 micron (Channel 217 -> 245) = %0.4f QCLOG \n", dispResult->trfBinned5);
01588     }
01589     
01590     return;
01591 }
01592 /*****************************************************************************/
01593 
01594 
01595 
01596 
01597 #ifdef NEW_PBL
01598 From: Anders Wallander [awalland@eso.org]
01599 Sent: Wednesday, 12 July, 2006 10:46
01600 To: Isabelle Percheron
01601 Cc: tlicha@eso.org; csabet@eso.org; awalland@eso.org
01602 Subject: Re: VLTI header keyword ISS PBLAxy START/END
01603 
01604 Attachments: VLTSW20060066.PDF
01605 Isabelle at al,
01606 
01607 The keyword still represents the value of projected baseline at the beginning and end of integration.
01608 The format is the same, but the definition of projected baseline is different, hence a new algorithm.
01609 Attached is the change request. The plan is to install this at Paranal end of September.
01610 The point is that pipelines should not break. I am waiting for your comments on this
01611 
01612 Below the 2 different algorithms (GIP is current, RMA is future) to compute PBLA.
01613 
01614 Cheers,
01615 Anders
01616 
01617 double pblaGIP (double alt, double az, double T1[3], double T2[3])
01618 {
01619 
01620     vltDOUBLE           star[]          = {0.0, 0.0, 0.0};
01621     vltDOUBLE           baseline[]      = {0.0, 0.0, 0.0};
01622     vltDOUBLE           projBaseline[]  = {0.0, 0.0, 0.0};
01623     vltDOUBLE           sidOpd          = 0.0;
01624     vltDOUBLE           cosAlt          = 0.0;
01625     vltDOUBLE           sinAlt          = 0.0;
01626     vltDOUBLE           cosAz           = 0.0;
01627     vltDOUBLE           sinAz           = 0.0;
01628 
01629     double              gip;
01630 
01631     cosAlt  = cos(alt * DDEG2RAD);
01632     sinAlt  = sin(alt * DDEG2RAD);
01633     cosAz   = cos(az * DDEG2RAD);
01634     sinAz   = sin(az * DDEG2RAD);
01635 
01636     star[0] = sinAz * cosAlt;
01637     star[1] = (- cosAz) * cosAlt;
01638     star[2] = sinAlt;
01639 
01640     baseline[0] = T2[0] - T1[0];
01641     baseline[1] = T2[1] - T1[1];
01642     baseline[2] = T2[2] - T1[2];
01643 
01644     /* scalar product <star>*<baseline> */
01645     sidOpd = star[0] * baseline[0] + star[1] * baseline[1] + star[2] * baseline[2];
01646     /* projected baseline vector in XYZ coordinate system */
01647     projBaseline[0] = baseline[0] - sidOpd * star[0];
01648     projBaseline[1] = baseline[1] - sidOpd * star[1];
01649     projBaseline[2] = baseline[2] - sidOpd * star[2];
01650             
01651     gip = atan(projBaseline[0]/projBaseline[1]) * DRAD2DEG;
01652 
01653     return gip;
01654 
01655 }
01656 
01657 
01658 double pblaRMA (double alt, double az, double prltic, double T1[3], double T2[3])
01659 {
01660 
01661     vltDOUBLE           a, A, A_b, p;
01662     vltDOUBLE           sinpsi, cospsi, psi;
01663 
01664     double              rma;
01665 
01666     /*
01667      * 1. Compute A, star azimuth, with FITS convention; 0=south, +90 west
01668      *    VLT convention is 0=south, +90 east
01669      */
01670     A = (360 - az) * DDEG2RAD;
01671 
01672     /*
01673      * 2. Compute baseline azimuth on the platform
01674      *    This is zero if T2 is south of T1 and +90 deg if T2 is west of T1.
01675      *    It seems that DLCS applies the convention X pos to west and Y pos to south
01676      *    for station coordinates (T vectors).
01677      *    In a south-over-west convention for 'A_b' and 'a', the y-component
01678      *    of the atan2(y,x) must represent +West and the x-component +South.
01679      *    The vector from T1[.] to T2[.] is obtained as T2[.]-T1[.].
01680      */
01681     A_b = atan2( (T2[0] - T1[0]), (T2[1] - T1[1]) );
01682     
01683     /*
01684      * 3. Get a, star elevation
01685      *    This is directly given by VLT altitude
01686      */
01687     a = alt * DDEG2RAD;
01688 
01689     /*
01690      * 4. Compute auxiliary angle psi
01691      */
01692     sinpsi = sin(A_b - A);
01693     cospsi = cos(A_b - A) * sin(a);
01694     psi = atan2(sinpsi,cospsi);
01695 
01696     /*
01697      * 5. Get parallactic angle.
01698      *    This value is computed by ISS using standard slaPa 
01699      */
01700     p = prltic * DDEG2RAD;
01701 
01702     /*
01703      * 6. Compute final angle
01704      *
01705      */
01706     rma = slaDranrm(p + psi + DPI) * DRAD2DEG;
01707 
01708     return rma;
01709 
01710 }
01711 
01712 Isabelle Percheron wrote: 
01713 Anders
01714 
01715 I was checking with Cyrus about the uses of the keywords, the pipelines (MIDI
01716 and AMBER for sure) read it and in the case of MIDI uses it, I am stilll not
01717 sure if AMBER pipeline just retrieves it or uses it for other calculations, I
01718 guess they have to retrieve the baseline from somewhere maybe from the stations,
01719 object ...
01720 
01721 So just to be sure, could you confirm if this keyword will still represent the
01722 value of the projected baseline at the beginning of the integration and at the
01723 end. How is the format affected (still the same?). And last could you please
01724 tell us when this is implemented on Paranal for Cyrus to check.
01725 Thanks
01726 Isabelle
01727 
01728 #endif
01729   

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