createProdFrg.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  createProdFrg.c
00007 * Description:  Contains routines for product files handling
00008 *
00009 * History:
00010 * 25-Apr-03     (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 <time.h>
00027 #include <math.h>
00028 #include "midiGlobal.h"
00029 #include "midiLib.h"
00030 #include "iauWrite.h"
00031 #include "createProdFrg.h"
00032 #include "midiFitsUtility.h"
00033 #include "geometry.h"
00034 #include "memoryHandling.h"
00035 #include "errorHandling.h"
00036 #include "fileHandling.h"
00037 #include "qfits.h"
00038 
00039 /**********************************************************
00040 *   Global Variables
00041 **********************************************************/
00042 
00043 /**********************************************************
00044 *   Constant definitions
00045 **********************************************************/
00046 
00047 /**********************************************************
00048 *   Function definitions
00049 **********************************************************/
00050 static void set_fnan(float *f)
00051 {
00052 
00053 #ifndef WORDS_BIGENDIAN
00054     static unsigned char fnan_pat[] = {0, 0, 0xc0, 0x7f};
00055 #else
00056     static unsigned char fnan_pat[] = {0x7f, 0xc0, 0, 0};
00057 #endif
00058 
00059 memcpy(f, fnan_pat, 4);
00060 }
00061 
00062 
00063 /*============================ C O D E    A R E A ===========================*/
00064 
00065 
00066 
00067 
00068 /******************************************************************************
00069 *               European Southern Observatory
00070 *            VLTI MIDI Data Reduction Software
00071 *
00072 * Module name:  createFrgProd
00073 * Input/Output: See function arguments to avoid duplication
00074 * Description:  Creates all product files for the DISPERSED mode of processing
00075 *
00076 *
00077 * History:
00078 * 09-Feb-05     (csabet) Created
00079 ******************************************************************************/
00080 IauExchange * createFrgProd (
00081     MidiFiles        *fileNames,            // In: File names
00082     ImageFormat        *imageSizeInterf,    // In: Interferometry data parameters
00083     float           *waveCal,            // In: Wavelength array
00084     DispersedResult    *dispResult,        // In: Dispersed result
00085     CalibratorParam    *calibrator,        // In: Calibrator parameters
00086     int                *error,             // Ou: Error Status
00087     CorrectedFrames   *corrFrames)
00088 
00089 {
00090 
00091     //    Local Declarations
00092     //    ------------------
00093     const char      routine[] = "createFrgProd";
00094     int                numOfTelescopes, extNumber;
00095     char            *stringQfits, *classification, *stringTemp;
00096     FILE            *inFitsBatchPtr = NULL;
00097     IauExchange     *iauData;
00098     UVW             *uvw;
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,"\nCreating Product files for batch  %d \n", batchNumber);
00106    cpl_msg_info(cpl_func,"-------------------------------- \n");
00107     fprintf (midiReportPtr, "\nCreating Product files for batch  %d \n", batchNumber);
00108     fprintf (midiReportPtr, "-------------------------------- \n");
00109 
00110     //    Reset status
00111     *error = 0;
00112     
00113     //    Allocate memory
00114     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00115     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00116 
00117     //    Open the list of files
00118     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00119     {
00120         *error = 1;
00121         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00122         free (stringTemp);
00123         free (classification);
00124         return NULL;
00125     }
00126 
00127     //    Read the name of the first file in the list and get it's full path name
00128     fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr);
00129     sprintf (classification, "%s", "");
00130     sscanf (stringTemp, "%s%s", fileNames->inFitsName, classification);
00131     fclose (inFitsBatchPtr);
00132 
00133     //    Copy keywords from the raw FITS file into the QC log
00134     createQcLog (fileNames->inFitsName, error);
00135     if (*error)
00136     {
00137         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot copy keywords to QC log");
00138         free (stringTemp);
00139         free (classification);
00140         return NULL;
00141     }
00142 
00143     //    Get number of array elements. Number of telescopes
00144     extNumber = getFitsExtensionNumber (fileNames->inFitsName, "IMAGING_DATA", error);
00145     stringQfits = qfits_query_ext (fileNames->inFitsName, "MAXTEL", extNumber);
00146     if (stringQfits == NULL)
00147     {
00148         *error = 1;
00149         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read MAXTEL from input FITS file");
00150         free (stringTemp);
00151         free (classification);
00152         return NULL;
00153     }
00154     else sscanf (stringQfits, "%d", &numOfTelescopes);
00155 
00156     //    Allocate memory for data structures relating to IAU exchange product FITS file
00157     iauData = callocIauExchange (numOfTelescopes, imageSizeInterf->iXWidth);
00158     uvw = (UVW *) calloc (1, sizeof (UVW));
00159 
00160     //    Load output data into the IAU exchange structures
00161     loadFrgOutputData (imageSizeInterf->obsCatg, fileNames->inFitsName, waveCal, dispResult, iauData, uvw, error);
00162     if (*error)
00163     {
00164         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load data structure");
00165         freeIauExchange (iauData);
00166         free (uvw);
00167         free (stringTemp);
00168         free (classification);
00169         return NULL;
00170     }
00171 
00172     //    Add product info to QC log
00173     addProdInfoToFrgQcLog (fileNames, imageSizeInterf, dispResult, uvw, waveCal, calibrator, error, corrFrames);
00174     if (*error)
00175     {
00176         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot add product info to QC log");
00177         freeIauExchange (iauData);
00178         free (uvw);
00179         free (stringTemp);
00180         free (classification);
00181         return NULL;
00182     }
00183 
00184     //    Create the primary header extension
00185     createFrgPrimHead (fileNames, imageSizeInterf, dispResult, uvw, waveCal,
00186             calibrator, error, corrFrames);
00187     if (*error)
00188     {
00189         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create Primary Header extension");
00190         freeIauExchange (iauData);
00191         free (uvw);
00192         free (stringTemp);
00193         free (classification);
00194         return NULL;
00195     }
00196 
00197     //    At this stage output FITS file has probably been created with appropriate
00198     //    header. Now we create the tables and write the data in accordance with the IAU exchange
00199     writeFrgFitsFile (fileNames->outFitsName, iauData, error);
00200     if (*error)
00201     {
00202         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write product FITS file");
00203         freeIauExchange (iauData);
00204         free (uvw);
00205         free (stringTemp);
00206         free (classification);
00207         return NULL;
00208     }
00209 
00210     if (diagnostic)cpl_msg_info(cpl_func,"Created Output FITS file: %s \n", fileNames->outFitsName);
00211     fprintf (midiReportPtr, "Created Output FITS file: %s \n", fileNames->outFitsName);
00212 
00213     //    Release memory
00214 
00215     /* Has to be free'ed by the caller */
00216     /*freeIauExchange (iauData);*/
00217 
00218     free (uvw);
00219     free (stringTemp);
00220     free (classification);
00221 
00222     return iauData;
00223 }
00224 /*****************************************************************************/
00225 
00226 
00227 
00228 /******************************************************************************
00229 *               European Southern Observatory
00230 *            VLTI MIDI Data Reduction Software
00231 *
00232 * Module name:  createFrgPrimHead
00233 * Input/Output: See function arguments to avoid duplication
00234 * Description:  Creates the primary header
00235 *
00236 *
00237 * History:
00238 * 10-Feb-05     (csabet) Created
00239 ******************************************************************************/
00240 void createFrgPrimHead (
00241     MidiFiles        *fileNames,
00242     ImageFormat        *sizeInterf,
00243     DispersedResult    *dispResult,
00244     UVW                *uvw,
00245     float           *waveCal,
00246     CalibratorParam    *calibrator,
00247     int                *error,
00248     CorrectedFrames   *corrFrames)
00249 {
00250 
00251     //    Local Declarations
00252     //    ------------------
00253     const char      routine[] = "createFrgPrimHead";
00254     qfits_header    *outFitsHeader;
00255     FILE            *inFitsBatchPtr, *outFitsPtr;
00256     int             X;
00257     char            *textBuff, *stringQfits, *messageBuffer, *currentTime, *cleanString,
00258                     *stringTemp, *classification;
00259 /*    unsigned int    stringLength;*/
00260     time_t          now;
00261     struct tm       *newTime;
00262     struct stat     buf;
00263     float            photA=0.0, photAErr=0.0, photB=0.0, photBErr=0.0, photI=0.0, photIErr=0.0,
00264                     photNet2=0.0, photNet2Err=0.0;
00265 
00266     //    Algorithm
00267     //    ---------
00268     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00269     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00270 
00271     *error = 0;
00272 
00273     //    Allocate memory
00274     textBuff = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00275     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00276     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00277     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00278     messageBuffer = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00279     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00280 
00281     //    Get current time/date
00282     now = time(NULL);
00283     newTime = gmtime (&now);
00284     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00285 
00286     //    Copy primary header from the raw file to the output header
00287     outFitsHeader = qfits_header_read (fileNames->inFitsName);
00288     if (outFitsHeader == NULL)
00289     {
00290         *error = 1;
00291         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load header from the input FITS file");
00292         free (messageBuffer);
00293         free (currentTime);
00294         free (cleanString);
00295         free (stringTemp);
00296         free (classification);
00297         free (textBuff);
00298         return;
00299     }
00300 
00301     //    Now write all the required product keaywords to the output header
00302     //    -----------------------------------------------------------------
00303     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TYPE", sizeInterf->obsType, "MIDI pipeline product type", "");
00304 
00305     if ((strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0) ||    (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0))
00306         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO CATG", "REDUCED_DISPERSED_SCIPHOT", "Pipeline product category", NULL);
00307     else
00308         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO CATG", "REDUCED_DISPERSED", "Pipeline product category", NULL);
00309     
00310     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO ARCFILE", fileNames->archFileName, "Arcfile name of first raw file", "");
00311     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO PIPEDATE", currentTime, "Pipeline run date", "");
00312     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO VERSION", MIDI_PIPE_VERSION, "Pipeline version", "");
00313     qfits_header_add (outFitsHeader, "PIPEFILE", fileNames->pipeFileName, "Pipeline product file name", "");
00314     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO DID", MIDI_QC_DIC_VERSION, "QC dictionary version", "");
00315 
00316     //    Open the list of files
00317     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00318     {
00319         *error = 1;
00320         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00321         free (messageBuffer);
00322         free (currentTime);
00323         free (cleanString);
00324         free (stringTemp);
00325         free (classification);
00326         free (textBuff);
00327         return;
00328     }
00329 
00330     //    Loop through the list of files and write into the primary header
00331     X = 1;
00332     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00333     {
00334         sprintf (classification, "%s", "");
00335         sscanf (stringTemp, "%s%s", messageBuffer, classification);
00336 
00337         //    If classification is not given get it from the raw file
00338         if (strcmp (classification, "") == 0)
00339         {
00340             stringQfits = qfits_query_hdr (messageBuffer, "HIERARCH ESO DPR CATG");
00341             if (stringQfits == NULL)
00342             {
00343                 sprintf (classification, "%s", "UNKNOWN");
00344                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get Observation Category");
00345             }
00346             else
00347             {
00348                 cleanUpString (stringQfits, cleanString);
00349                 sprintf (classification, "%s", cleanString);
00350             }
00351         }
00352         removePathName (messageBuffer, midiReportPtr);
00353         sprintf (textBuff, "HIERARCH ESO PRO REC1 RAW%d NAME", X);
00354         qfits_header_add (outFitsHeader, textBuff, messageBuffer, "FITS file name", "");
00355         sprintf (textBuff, "HIERARCH ESO PRO REC1 RAW%d CATG", X++);
00356         qfits_header_add (outFitsHeader, textBuff, classification, "Observation Categoty", "");
00357     }
00358     fclose (inFitsBatchPtr);
00359 
00360 /*
00361  * Not anymore needed as properly inserted  in the SOF and inherited by the cpl
00362  * saving functions
00363     //    Write the mask file
00364     sprintf (messageBuffer, "%s", fileNames->maskFileName);
00365     removePathName (messageBuffer, midiReportPtr);
00366     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO REC2 RAW1 NAME", messageBuffer, "Pipeline Mask File", "");
00367 
00368     //    Write the transfer function file
00369     if (strcmp (sizeInterf->obsCatg, "CALIB") == 0) sprintf (messageBuffer, "%s", fileNames->trfNameWrite);
00370     else sprintf (messageBuffer, "%s", fileNames->trfNameRead);
00371     removePathName (messageBuffer, midiReportPtr);
00372     stringLength = strlen (messageBuffer);
00373     sprintf (textBuff, "r.");
00374     strncat (textBuff, messageBuffer, stringLength-4);
00375     sprintf (messageBuffer, "%s_tpl_0001.dummy", textBuff);
00376     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO REC2 RAW2 NAME", messageBuffer, "Transfer Function File", "");
00377 */
00378 
00379     //    Scan parameters
00380     sprintf (messageBuffer, "%d", sizeInterf->numOfChannelsProcessed);
00381     qfits_header_add (outFitsHeader, "HIERARCH ESO QC CH PROCESSED", messageBuffer, "Number of chnnels processed", "");
00382     sprintf (messageBuffer, "%d", sizeInterf->numOfChannelsRejected);
00383     qfits_header_add (outFitsHeader, "HIERARCH ESO QC CH REJECTED", messageBuffer, "Number of channels rejected", "");
00384 
00385     //    Add Wavelengths
00386     for (X = 0; X < sizeInterf->iXWidth; X++)
00387     {
00388         if (badChannelList[X])
00389         {
00390             sprintf (textBuff, "HIERARCH ESO QC LAMBDA%d", X);
00391             sprintf (messageBuffer, "%s", UNAV);
00392             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Calibrated wavelength", "");
00393             continue;
00394         }
00395 
00396         sprintf (textBuff, "HIERARCH ESO QC LAMBDA%d", X);
00397         sprintf (messageBuffer, "%f", waveCal[X]);
00398         qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Calibrated wavelength", "");
00399     }
00400 
00401     //    Transfer functions
00402     if (dispResult->trfExists)
00403     {
00404         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TRF", "AVAILABLE", "Flag for Transfer Function availability", NULL);
00405         if (strcmp (sizeInterf->obsCatg, "CALIB") == 0)
00406         {
00407             sprintf (messageBuffer, "%f", calibrator->calibVisSqrd);
00408             qfits_header_add (outFitsHeader, "HIERARCH ESO QC THEOVIS", messageBuffer, "Theoretical visibility", "");
00409             sprintf (messageBuffer, "%f", calibrator->calibVisSqrdErr);
00410             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DTHEOVIS", messageBuffer, "(sigma) Error on theoretical visibility", "");
00411             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB POS", "UNKNOWN", "Position of the calibrator entry", "");
00412             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB NAME", calibrator->calibName, "Name of the calibrator entry", "");
00413             sprintf (messageBuffer, "%f", calibrator->calibRA);
00414             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB RA", messageBuffer, "Calibrator Right Ascension", "");
00415             sprintf (messageBuffer, "%f", calibrator->calibDEC);
00416             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DEC", messageBuffer, "Calibrator Declination", "");
00417             sprintf (messageBuffer, "%f", RAD_TO_ARCSEC * calibrator->calibDistance);
00418             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DIST", messageBuffer, "Calibrator Radial Distance in arcsec", "");
00419             sprintf (messageBuffer, "%f", calibrator->calibDiameter);
00420             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DIAM", messageBuffer, "Calibrator Diameter in milliarcsec", "");
00421             sprintf (messageBuffer, "%f", calibrator->calibDiameterErr);
00422             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DDIAM", messageBuffer, "(sigma) Error on calibrator Diameter in milliarcsec", "");
00423             sprintf (messageBuffer, "%d", calibrator->calibFlag);
00424             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB FLAG", messageBuffer, "Calibrator quality flag", "");
00425             sprintf (messageBuffer, "%g", calibrator->calibFlux);
00426             qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB FLUX", messageBuffer, "Calibrator N-band flux", "");
00427             sprintf (messageBuffer, "%f", calibrator->calibPblAverage);
00428             qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL LENGTH", messageBuffer, "Baseline vector length", "");
00429             sprintf (messageBuffer, "%f", calibrator->calibParangAverage);
00430             qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL ANGLE", messageBuffer, "Baseline vector angle", "");
00431 
00432             if (strcmp(sizeInterf->grismId, "PRISM") == 0)
00433             {
00434                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned1);
00435                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED1", messageBuffer, 
00436                                  "Binned transfer function at 8.64 micron (Ch 120-126)", "");
00437                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned2);
00438                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED2", messageBuffer, 
00439                                  "Binned transfer function at 9.00 micron (Ch 113-121)", "");
00440                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned3);
00441                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED3", messageBuffer, 
00442                                  "Binned transfer function at 10.46 micron (Ch 88-98)", "");
00443                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned4);
00444                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED4", messageBuffer, 
00445                                  "Binned transfer function at 11.79 micron (Ch 60-72)", "");
00446                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned5);
00447                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED5", messageBuffer, 
00448                                  "Binned transfer function at 12.80 micron (Ch 36-50)", "");
00449             }
00450             else
00451             {
00452                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned1);
00453                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED1", messageBuffer, 
00454                                  "Binned transfer function at 8.64 micron (Ch 38-50)", "");
00455                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned2);
00456                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED2", messageBuffer, 
00457                                  "Binned transfer function at 9.00 micron (Ch 52-68)", "");
00458                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned3);
00459                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED3", messageBuffer, 
00460                                  "Binned transfer function at 10.46 micron (Ch 116-136)", "");
00461                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned4);
00462                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED4", messageBuffer, 
00463                                  "Binned transfer function at 11.79 micron (Ch 176-200)", "");
00464                sprintf (messageBuffer, "%0.4f", dispResult->trfBinned5);
00465                qfits_header_add (outFitsHeader, "HIERARCH ESO QC TRF BINNED5", messageBuffer, 
00466                                  "Binned transfer function at 12.80 micron (Ch 217-245)", "");
00467             }
00468             
00469 
00470             for (X = 0; X < sizeInterf->iXWidth; X++)
00471             {
00472                 if (badChannelList[X])
00473                 {
00474                     sprintf (textBuff, "HIERARCH ESO QC T%d", X);
00475                     sprintf (messageBuffer, "%s", UNAV);
00476                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Measured transfer function", "");
00477 
00478                     sprintf (textBuff, "HIERARCH ESO QC DT%d", X);
00479                     sprintf (messageBuffer, "%s", UNAV);
00480                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error on measured transfer function", "");
00481 
00482                     sprintf (textBuff, "HIERARCH ESO QC COT%d", X);
00483                     sprintf (messageBuffer, "%s", UNAV);
00484                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Inverse transfer function", "");
00485 
00486                     sprintf (textBuff, "HIERARCH ESO QC DCOT%d", X);
00487                     sprintf (messageBuffer, "%s", UNAV);
00488                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error on inverse transfer function", "");
00489                 }
00490                 else
00491                 {
00492                     sprintf (textBuff, "HIERARCH ESO QC T%d", X);
00493                     sprintf (messageBuffer, "%f", (dispResult->trf)[X]);
00494                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Measured transfer function", "");
00495 
00496                     sprintf (textBuff, "HIERARCH ESO QC DT%d", X);
00497                     sprintf (messageBuffer, "%f", (dispResult->trfErr)[X]);
00498                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error on measured transfer function", "");
00499 
00500                     sprintf (textBuff, "HIERARCH ESO QC COT%d", X);
00501                     sprintf (messageBuffer, "%f", 1.0/(dispResult->trf)[X]);
00502                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Inverse transfer function", "");
00503 
00504                     sprintf (textBuff, "HIERARCH ESO QC DCOT%d", X);
00505                     sprintf (messageBuffer, "%f", 1.0/(dispResult->trfErr)[X]);
00506                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error on inverse transfer function", "");
00507                 }
00508             }
00509         }
00510         else
00511         {
00512             //    Add Applied Transfer Functions
00513             for (X = 0; X < sizeInterf->iXWidth; X++)
00514             {
00515                 if (badChannelList[X])
00516                 {
00517                     sprintf (textBuff, "HIERARCH ESO QC AT%d", X);
00518                     sprintf (messageBuffer, "%s", UNAV);
00519                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Applied transfer function", "");
00520 
00521                     sprintf (textBuff, "HIERARCH ESO QC DAT%d", X);
00522                     sprintf (messageBuffer, "%s", UNAV);
00523                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error on applied transfer function", "");
00524                 }
00525                 else
00526                 {
00527                     sprintf (textBuff, "HIERARCH ESO QC AT%d", X);
00528                     sprintf (messageBuffer, "%f", (dispResult->trf)[X]);
00529                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Applied transfer function", "");
00530 
00531                     sprintf (textBuff, "HIERARCH ESO QC DAT%d", X);
00532                     sprintf (messageBuffer, "%f", (dispResult->trfErr)[X]);
00533                     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error on applied transfer function", "");
00534                 }
00535             }
00536         }
00537     }
00538     else
00539         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TRF", UNAV, "Flag for Transfer Function availability", NULL);
00540 
00541     //    Add uncalibrated Normal Visibilities
00542     for (X = 0; X < sizeInterf->iXWidth; X++)
00543     {
00544         if (badChannelList[X])
00545         {
00546             sprintf (textBuff, "HIERARCH ESO QC UNCALV%d", X);
00547             sprintf (messageBuffer, "%s", UNAV);
00548             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Uncalibrated Normal Visibility", "");
00549 
00550             sprintf (textBuff, "HIERARCH ESO QC DUNCALV%d", X);
00551             sprintf (messageBuffer, "%s", UNAV);
00552             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of uncalibrated Normal Visibility", "");
00553         }
00554         else
00555         {
00556             sprintf (textBuff, "HIERARCH ESO QC UNCALV%d", X);
00557             sprintf (messageBuffer, "%f", (dispResult->normVis2)[X]);
00558             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Uncalibrated Normal Visibility", "");
00559 
00560             sprintf (textBuff, "HIERARCH ESO QC DUNCALV%d", X);
00561             sprintf (messageBuffer, "%f", (dispResult->normVis2Err)[X]);
00562             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of uncalibrated Normal Visibility", "");
00563         }
00564     }
00565     
00566 
00567     //    Add Photometry parameters
00568     for (X = 0; X < sizeInterf->iXWidth; X++)
00569     {
00570         if (!(badChannelList[X]))
00571         {
00572             photA += (dispResult->photomA)[0][X];
00573             photAErr += (dispResult->photomAErr)[0][X];
00574             photB += (dispResult->photomB)[0][X];
00575             photBErr += (dispResult->photomBErr)[0][X];
00576             photI += (dispResult->photomI)[0][X];
00577             photIErr += (dispResult->photomIErr)[0][X];
00578             photNet2 += (dispResult->photomNet2)[X];
00579             photNet2Err += (dispResult->photomNet2Err)[X];
00580         }
00581     }
00582     if (sizeInterf->numOfChannelsProcessed)
00583     {
00584         photA /= sizeInterf->numOfChannelsProcessed;
00585         photAErr /= sizeInterf->numOfChannelsProcessed;
00586         photB /= sizeInterf->numOfChannelsProcessed;
00587         photBErr /= sizeInterf->numOfChannelsProcessed;
00588         photI /= sizeInterf->numOfChannelsProcessed;
00589         photIErr /= sizeInterf->numOfChannelsProcessed;
00590         photNet2 /= sizeInterf->numOfChannelsProcessed;
00591         photNet2Err /= sizeInterf->numOfChannelsProcessed;
00592     }
00593     sprintf (textBuff, "HIERARCH ESO QC AV PHOTA");
00594     sprintf (messageBuffer, "%f", photA);
00595     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00596     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTA");
00597     sprintf (messageBuffer, "%f", photAErr);
00598     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00599     sprintf (textBuff, "HIERARCH ESO QC AV PHOTB");
00600     sprintf (messageBuffer, "%f", photB);
00601     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00602     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTB");
00603     sprintf (messageBuffer, "%f", photBErr);
00604     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00605     sprintf (textBuff, "HIERARCH ESO QC AV PHOTI");
00606     sprintf (messageBuffer, "%f", photI);
00607     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00608     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTI");
00609     sprintf (messageBuffer, "%f", photIErr);
00610     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00611     sprintf (textBuff, "HIERARCH ESO QC AV PHOTN");
00612     sprintf (messageBuffer, "%f", photNet2);
00613     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00614     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTN");
00615     sprintf (messageBuffer, "%f", photNet2Err);
00616     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00617 
00618 
00619     //    Add Calibrated Visibilities
00620     if (dispResult->calibVisExists)
00621     {
00622         for (X = 0; X < sizeInterf->iXWidth; X++)
00623         {
00624             if (badChannelList[X])
00625             {
00626                 sprintf (textBuff, "HIERARCH ESO QC CALV%d", X);
00627                 sprintf (messageBuffer, "%s", UNAV);
00628                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Calibrated Visibility", "");
00629 
00630                 sprintf (textBuff, "HIERARCH ESO QC DCALV%d", X);
00631                 sprintf (messageBuffer, "%s", UNAV);
00632                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of calibrated Visibility", "");
00633             }
00634             else
00635             {
00636                 sprintf (textBuff, "HIERARCH ESO QC CALV%d", X);
00637                 sprintf (messageBuffer, "%f", (dispResult->calibVis2)[X]);
00638                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Calibrated Visibility", "");
00639 
00640                 sprintf (textBuff, "HIERARCH ESO QC DCALV%d", X);
00641                 sprintf (messageBuffer, "%f", (dispResult->calibVis2Err)[X]);
00642                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of calibrated Visibility", "");
00643             }
00644         }
00645     }
00646 
00647 
00648     //    Add Target keywords
00649     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARG DIAM", "UNKNOWN", "Estimated diameter of the target (mas, Un.Disk model)", "");
00650     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARG DDIAM", "UNKNOWN", "(sigma) Error on estimated target diameter in milliarcsec", "");
00651 
00652     //    Add UVW. The values of uvw will be added during pipeline execution
00653     sprintf (messageBuffer, "%f", uvw->uCoord);
00654     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW1", messageBuffer, "u component of UVW vector", "");
00655     sprintf (messageBuffer, "%f", uvw->vCoord);
00656     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW2", messageBuffer, "v component of UVW vector", "");
00657     sprintf (messageBuffer, "%f", uvw->wCoord);
00658     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW3", messageBuffer, "w component of UVW vector", "");
00659 
00660     //    Add binned photometry measurements
00661     if ((strcmp(sizeInterf->grismId, "PRISM") == 0) && (strcmp(sizeInterf->beamCombiner, "HIGH_SENS") == 0))
00662     {
00663         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg1);
00664         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG1", messageBuffer, "PhotA Targ1 Ch 113 - 121", "");
00665         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg1);
00666         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG1", messageBuffer, "PhotB Targ1 Ch 113 - 121", "");
00667 
00668         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg2);
00669         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG2", messageBuffer, "PhotA Targ2 Ch 88 - 98", "");
00670         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg2);
00671         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG2", messageBuffer, "PhotB Targ2 Ch 88 - 98", "");
00672 
00673         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg3);
00674         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG3", messageBuffer, "PhotA Targ3 Ch 36 - 50", "");
00675         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg3);
00676         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG3", messageBuffer, "PhotB Targ3 Ch 36 - 50", "");
00677 
00678         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal1);
00679         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL1", messageBuffer, "PhotA Total1 Ch 113 - 121", "");
00680         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal1);
00681         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL1", messageBuffer, "PhotB Total1 Ch 113 - 121", "");
00682 
00683         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal2);
00684         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL2", messageBuffer, "PhotA Total2 Ch 88 - 98", "");
00685         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal2);
00686         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL2", messageBuffer, "PhotB Total2 Ch 88 - 98", "");
00687 
00688         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal3);
00689         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL3", messageBuffer, "PhotA Total3 Ch 36 - 50", "");
00690         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal3);
00691         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL3", messageBuffer, "PhotB Total3 Ch 36 - 50", "");
00692     }
00693     else
00694     {
00695         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg1);
00696         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG1", messageBuffer, "PhotA Targ1 Ch 52 - 68", "");
00697         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg1);
00698         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG1", messageBuffer, "PhotB Targ1 Ch 52 - 68", "");
00699 
00700         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg2);
00701         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG2", messageBuffer, "PhotA Targ2 Ch 116 - 136", "");
00702         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg2);
00703         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG2", messageBuffer, "PhotB Targ2 Ch 116 - 136", "");
00704 
00705         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg3);
00706         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG3", messageBuffer, "PhotA Targ3 Ch 217 - 244", "");
00707         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg3);
00708         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG3", messageBuffer, "PhotB Targ3 Ch 217 - 244", "");
00709 
00710         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal1);
00711         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL1", messageBuffer, "PhotA Total1 Ch 52 - 68", "");
00712         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal1);
00713         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL1", messageBuffer, "PhotB Total1 Ch 52 - 68", "");
00714 
00715         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal2);
00716         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL2", messageBuffer, "PhotA Total2 Ch 116 - 136", "");
00717         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal2);
00718         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL2", messageBuffer, "PhotB Total2 Ch 116 - 136", "");
00719 
00720         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal3);
00721         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL3", messageBuffer, "PhotA Total3 Ch 217 - 244", "");
00722         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal3);
00723         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL3", messageBuffer, "PhotB Total3 Ch 217 - 244", "");
00724     }
00725 
00726     //    Add binned uncalibrated visibilities
00727     if (strcmp(sizeInterf->grismId, "PRISM") == 0)
00728     {
00729         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned1);
00730         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED1", messageBuffer, 
00731             "Binned uncal vis at 8.64 micron (Ch 120-126)", "");
00732         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned2);
00733         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED2", messageBuffer, 
00734             "Binned uncal vis at 9.00 micron (Ch 113-121)", "");
00735         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned3);
00736         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED3", messageBuffer, 
00737             "Binned uncal vis at 10.46 micron (Ch 88-98)", "");
00738         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned4);
00739         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED4", messageBuffer, 
00740             "Binned uncal vis at 11.79 micron (Ch 60-72)", "");
00741         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned5);
00742         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED5", messageBuffer, 
00743             "Binned uncal vis at 12.80 micron (Ch 36-50)", "");
00744     }
00745     else
00746     {
00747         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned1);
00748         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED1", messageBuffer, 
00749             "Binned uncal vis at 8.64 micron (Ch 38-50)", "");
00750         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned2);
00751         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED2", messageBuffer, 
00752             "Binned uncal vis at 9.00 micron (Ch 52-68)", "");
00753         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned3);
00754         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED3", messageBuffer, 
00755             "Binned uncal vis at 10.46 micron (Ch 116-136)", "");
00756         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned4);
00757         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED4", messageBuffer, 
00758             "Binned uncal vis at 11.79 micron (Ch 176-200)", "");
00759         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned5);
00760         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED5", messageBuffer, 
00761             "Binned uncal vis at 12.80 micron (Ch 217-245)", "");
00762     }
00763 
00764     sprintf (messageBuffer, "%ld", corrFrames->CorrInterf);
00765     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARTYPE INTERF CHANGED", messageBuffer,
00766         "Number of type-modified frames", "");
00767 
00768     sprintf (messageBuffer, "%ld", corrFrames->CorrPhotomA);
00769     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARTYPE PHOTOMA CHANGED", messageBuffer,
00770         "Number of type-modified frames", "");
00771 
00772     sprintf (messageBuffer, "%ld", corrFrames->CorrPhotomB);
00773     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARTYPE PHOTOMB CHANGED", messageBuffer,
00774         "Number of type-modified frames", "");
00775 
00776     //    Place Holders
00777     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH1", "TBD", "TBD", "");
00778     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH2", "TBD", "TBD", "");
00779     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH3", "TBD", "TBD", "");
00780     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH4", "TBD", "TBD", "");
00781     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH5", "TBD", "TBD", "");
00782     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH6", "TBD", "TBD", "");
00783     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH7", "TBD", "TBD", "");
00784     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH8", "TBD", "TBD", "");
00785     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH9", "TBD", "TBD", "");
00786 
00787     //    Create the output fits file
00788     if (stat (fileNames->outFitsName, &buf) == 0)   // If the file exist delete it
00789         remove (fileNames->outFitsName);
00790     outFitsPtr = fopen (fileNames->outFitsName, "w");
00791     if (!outFitsPtr)
00792     {
00793         *error = 1;
00794         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create output FITS file");
00795         free (messageBuffer);
00796         free (currentTime);
00797         free (cleanString);
00798         free (stringTemp);
00799         free (classification);
00800         free (textBuff);
00801         return;
00802     }
00803 
00804     //    Write header into the output file
00805     qfits_header_sort (&outFitsHeader);
00806     qfits_header_dump (outFitsHeader, outFitsPtr);
00807     qfits_header_destroy (outFitsHeader);
00808     fclose (outFitsPtr);
00809 
00810     //    release memory
00811     free (messageBuffer);
00812     free (currentTime);
00813     free (cleanString);
00814     free (stringTemp);
00815     free (classification);
00816     free (textBuff);
00817 
00818     return;
00819 }
00820 /*****************************************************************************/
00821 
00822 
00823 
00824 
00825 /******************************************************************************
00826 *               European Southern Observatory
00827 *            VLTI MIDI Data Reduction Software
00828 *
00829 * Module name:  addProdInfoToFrgQcLog
00830 * Input/Output: See function arguments to avoid duplication
00831 * Description:  Adds product information to QC log
00832 *
00833 *
00834 * History:
00835 * 09-Feb-05     (csabet) Created
00836 ******************************************************************************/
00837 void addProdInfoToFrgQcLog (
00838     MidiFiles        *fileNames,        // In: File names
00839     ImageFormat        *sizeInterf,    // In: Interferometry data parameters
00840     DispersedResult    *dispResult,    // In: Dispersed result
00841     UVW                *uvw,            // In: UVW coordinates
00842     float           *freqCal,        // In: Frequency array
00843     CalibratorParam    *calibrator,    // In: Calibrator parameters
00844     int                *error,            // Ou: Error status
00845     CorrectedFrames   *corrFrames)
00846 {
00847 
00848     //    Local Declarations
00849     //    ------------------
00850     const char      routine[] = "addProdInfoToFrgQcLog";
00851     int             X;
00852     FILE            *inFitsBatchPtr;
00853     char            *stringQfits, *messageBuffer, *currentTime, *textBuff, *cleanString,
00854                     *stringTemp, *classification;
00855     unsigned int    stringLength;
00856     time_t          now;
00857     struct tm       *newTime;
00858     float            photA=0.0, photAErr=0.0, photB=0.0, photBErr=0.0, photI=0.0, photIErr=0.0,
00859                     photNet2=0.0, photNet2Err=0.0;
00860 
00861     //    Algorithm
00862     //    ---------
00863     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00864     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00865 
00866     //    Reset parameters
00867     *error = 0;
00868 
00869     //    Allocate memory
00870     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00871     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00872     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00873     textBuff = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00874     messageBuffer = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00875     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00876 
00877     //    Get current time/date
00878     now = time(NULL);
00879     newTime = gmtime (&now);
00880     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00881 
00882     //    Add keyword to QC log file
00883     fprintf (midiQcLogPtr, "PRO.TYPE            \"%s\" \n", sizeInterf->obsType);
00884 
00885     if ((strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0) ||    (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0))
00886         fprintf (midiQcLogPtr, "PRO.CATG            \"REDUCED_DISPERSED_SCIPHOT\" \n");
00887     else
00888         fprintf (midiQcLogPtr, "PRO.CATG            \"REDUCED_DISPERSED\" \n");
00889 
00890     fprintf (midiQcLogPtr, "PRO.ARCFILE         \"%s\" \n", fileNames->archFileName);
00891     fprintf (midiQcLogPtr, "PRO.PIPEDATE        \"%s\" \n", currentTime);
00892     fprintf (midiQcLogPtr, "PRO.VERSION         \"%s\" \n", MIDI_PIPE_VERSION);
00893     fprintf (midiQcLogPtr, "PRO.PIPEFILE        \"%s\" \n", fileNames->pipeFileName);
00894     fprintf (midiQcLogPtr, "PRO.DID             \"%s\" \n", MIDI_QC_DIC_VERSION);
00895 
00896     //    Open the list of files
00897     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00898     {
00899         *error = 1;
00900         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00901         free (messageBuffer);
00902         free (currentTime);
00903         free (cleanString);
00904         free (stringTemp);
00905         free (classification);
00906         free (textBuff);
00907         return;
00908     }
00909 
00910     //    Loop through the list of files and write into the QC log
00911     X = 1;
00912     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00913     {
00914         sprintf (classification, "%s", "");
00915         sscanf (stringTemp, "%s%s", messageBuffer, classification);
00916 
00917         //    If classification is not given get it from the raw file
00918         if (strcmp (classification, "") == 0)
00919         {
00920             stringQfits = qfits_query_hdr (messageBuffer, "HIERARCH ESO DPR CATG");
00921             if (stringQfits == NULL)
00922             {
00923                 sprintf (classification, "%s", "UNKNOWN");
00924                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get Observation Category");
00925             }
00926             else
00927             {
00928                 cleanUpString (stringQfits, cleanString);
00929                 sprintf (classification, "%s", cleanString);
00930             }
00931         }
00932         removePathName (messageBuffer, midiReportPtr);
00933         fprintf (midiQcLogPtr, "PRO.REC1.RAW%d.NAME  \"%s\" \n", X, messageBuffer);
00934         fprintf (midiQcLogPtr, "PRO.REC1.RAW%d.CATG  \"%s\" \n", X++, classification);
00935     }
00936     fclose (inFitsBatchPtr);
00937 
00938     //    Add name of Mask file to QC log file
00939     sprintf (messageBuffer, "%s", fileNames->maskFileName);
00940     removePathName (messageBuffer, midiReportPtr);
00941     fprintf (midiQcLogPtr, "PRO.REC2.RAW1.NAME  \"%s\" \n", messageBuffer);
00942 
00943     //    Add name of Transfer Function file to QC log file
00944     if (strcmp (sizeInterf->obsCatg, "CALIB") == 0) sprintf (messageBuffer, "%s", fileNames->trfNameWrite);
00945     else sprintf (messageBuffer, "%s", fileNames->trfNameRead);
00946     removePathName (messageBuffer, midiReportPtr);
00947     stringLength = strlen (messageBuffer);
00948     sprintf (textBuff, "r.");
00949     strncat (textBuff, messageBuffer, stringLength-4);
00950     sprintf (messageBuffer, "%s_tpl_0001.dummy", textBuff);
00951     fprintf (midiQcLogPtr, "PRO.REC2.RAW2.NAME  \"%s\" \n", messageBuffer);
00952 
00953     //    Scan parameters
00954     fprintf (midiQcLogPtr, "QC.CH.PROCESSED  %d \n", sizeInterf->numOfChannelsProcessed);
00955     fprintf (midiQcLogPtr, "QC.CH.REJECTED   %d \n", sizeInterf->numOfChannelsRejected);
00956 
00957     //    Add Wavelengths
00958     for (X = 0; X < sizeInterf->iXWidth; X++)
00959     {
00960         if (badChannelList[X])
00961         {
00962             fprintf (midiQcLogPtr, "QC.LAMBDA%d            %s \n", X, UNAV);
00963             continue;
00964         }
00965             
00966         fprintf (midiQcLogPtr, "QC.LAMBDA%d            %f \n", X, freqCal[X]);
00967     }
00968     
00969     //    Transfer functions
00970     if (dispResult->trfExists)
00971     {
00972         if (strcmp (sizeInterf->obsCatg, "CALIB") == 0)
00973         {
00974             //    Add Calibrator keywords
00975             fprintf (midiQcLogPtr, "QC.THEOVIS          %f \n", calibrator->calibVisSqrd);
00976             fprintf (midiQcLogPtr, "QC.DTHEOVIS         %f \n", calibrator->calibVisSqrdErr);
00977             fprintf (midiQcLogPtr, "QC.CALIB.POS        \"UNKNOWN\" \n");
00978             fprintf (midiQcLogPtr, "QC.CALIB.NAME       \"%s\" \n", calibrator->calibName);
00979             fprintf (midiQcLogPtr, "QC.CALIB.RA         %f \n", calibrator->calibRA);
00980             fprintf (midiQcLogPtr, "QC.CALIB.DEC        %f \n", calibrator->calibDEC);
00981             fprintf (midiQcLogPtr, "QC.CALIB.DIST       %f \n", RAD_TO_ARCSEC * calibrator->calibDistance);
00982             fprintf (midiQcLogPtr, "QC.CALIB.DIAM       %f \n", calibrator->calibDiameter);
00983             fprintf (midiQcLogPtr, "QC.CALIB.DDIAM      %f \n", calibrator->calibDiameterErr);
00984             fprintf (midiQcLogPtr, "QC.CALIB.FLAG       %d \n", calibrator->calibFlag);
00985             fprintf (midiQcLogPtr, "QC.CALIB.FLUX       %g \n", calibrator->calibFlux);
00986             fprintf (midiQcLogPtr, "QC.BL.LENGTH  %f \n", calibrator->calibPblAverage);
00987             fprintf (midiQcLogPtr, "QC.BL.ANGLE   %f \n", calibrator->calibParangAverage);
00988 
00989             //    Add binned Transfer Function
00990             fprintf (midiQcLogPtr, "QC.TRF.BINNED1  %0.4f \n", dispResult->trfBinned1);
00991             fprintf (midiQcLogPtr, "QC.TRF.BINNED2  %0.4f \n", dispResult->trfBinned2);
00992             fprintf (midiQcLogPtr, "QC.TRF.BINNED3  %0.4f \n", dispResult->trfBinned3);
00993             fprintf (midiQcLogPtr, "QC.TRF.BINNED4  %0.4f \n", dispResult->trfBinned4);
00994             fprintf (midiQcLogPtr, "QC.TRF.BINNED5  %0.4f \n", dispResult->trfBinned5);
00995 
00996         
00997             for (X = 0; X < sizeInterf->iXWidth; X++)
00998             {
00999                 if (badChannelList[X])
01000                 {
01001                     fprintf (midiQcLogPtr, "QC.T%d              %s \n", X, UNAV);
01002                     fprintf (midiQcLogPtr, "QC.DT%d             %s \n", X, UNAV);
01003                     fprintf (midiQcLogPtr, "QC.COT%d            %s \n", X, UNAV);
01004                     fprintf (midiQcLogPtr, "QC.DCOT%d           %s \n", X, UNAV);
01005                     continue;
01006                 }
01007             
01008                 fprintf (midiQcLogPtr, "QC.T%d              %f \n", X, (dispResult->trf)[X]);
01009                 fprintf (midiQcLogPtr, "QC.DT%d             %f \n", X, (dispResult->trfErr)[X]);
01010                 fprintf (midiQcLogPtr, "QC.COT%d            %f \n", X, 1.0/(dispResult->trf)[X]);
01011                 fprintf (midiQcLogPtr, "QC.DCOT%d           %f \n", X, 1.0/(dispResult->trfErr)[X]);
01012             }
01013         }
01014         else
01015         {
01016             //    Add Applied Transfer Functions
01017             for (X = 0; X < sizeInterf->iXWidth; X++)
01018             {
01019                 if (badChannelList[X])
01020                 {
01021                     fprintf (midiQcLogPtr, "QC.AT%d             %s \n", X, UNAV);
01022                     fprintf (midiQcLogPtr, "QC.DAT%d            %s \n", X, UNAV);
01023                 }
01024                 else
01025                 {
01026                     fprintf (midiQcLogPtr, "QC.AT%d             %f \n", X, (dispResult->trf)[X]);
01027                     fprintf (midiQcLogPtr, "QC.DAT%d            %f \n", X, (dispResult->trfErr)[X]);
01028                 }
01029             }
01030         }
01031     }
01032 
01033     //    Add uncalibrated Normal Visibilities
01034     for (X = 0; X < sizeInterf->iXWidth; X++)
01035     {
01036         if (badChannelList[X])
01037         {
01038             fprintf (midiQcLogPtr, "QC.UNCALV%d         %s \n", X, UNAV);
01039             fprintf (midiQcLogPtr, "QC.DUNCALV%d        %s \n", X, UNAV);
01040         }
01041         else
01042         {
01043             fprintf (midiQcLogPtr, "QC.UNCALV%d         %f \n", X, (dispResult->normVis2)[X]);
01044             fprintf (midiQcLogPtr, "QC.DUNCALV%d        %f \n", X, (dispResult->normVis2Err)[X]);
01045         }
01046     }
01047 
01048     //    Add Photometry parameters
01049     for (X = 0; X < sizeInterf->iXWidth; X++)
01050     {
01051         if (!(badChannelList[X]))
01052         {
01053             photA += (dispResult->photomA)[0][X];
01054             photAErr += (dispResult->photomAErr)[0][X];
01055             photB += (dispResult->photomB)[0][X];
01056             photBErr += (dispResult->photomBErr)[0][X];
01057             photI += (dispResult->photomI)[0][X];
01058             photIErr += (dispResult->photomIErr)[0][X];
01059             photNet2 += (dispResult->photomNet2)[X];
01060             photNet2Err += (dispResult->photomNet2Err)[X];
01061         }
01062     }
01063     if (sizeInterf->numOfChannelsProcessed)
01064     {
01065         photA /= sizeInterf->numOfChannelsProcessed;
01066         photAErr /= sizeInterf->numOfChannelsProcessed;
01067         photB /= sizeInterf->numOfChannelsProcessed;
01068         photBErr /= sizeInterf->numOfChannelsProcessed;
01069         photI /= sizeInterf->numOfChannelsProcessed;
01070         photIErr /= sizeInterf->numOfChannelsProcessed;
01071         photNet2 /= sizeInterf->numOfChannelsProcessed;
01072         photNet2Err /= sizeInterf->numOfChannelsProcessed;
01073     }
01074     fprintf (midiQcLogPtr, "QC.AV.PHOTA         %f \n", photA);
01075     fprintf (midiQcLogPtr, "QC.AV.DPHOTA        %f \n", photAErr);
01076     fprintf (midiQcLogPtr, "QC.AV.PHOTB         %f \n", photB);
01077     fprintf (midiQcLogPtr, "QC.AV.DPHOTB        %f \n", photBErr);
01078     fprintf (midiQcLogPtr, "QC.AV.PHOTI         %f \n", photI);
01079     fprintf (midiQcLogPtr, "QC.AV.DPHOTI        %f \n", photIErr);
01080     fprintf (midiQcLogPtr, "QC.AV.PHOTN         %f \n", photNet2);
01081     fprintf (midiQcLogPtr, "QC.AV.DPHOTN        %f \n", photNet2Err);
01082 
01083     //    Add Calibrated Visibilities
01084     if (dispResult->calibVisExists)
01085     {
01086         for (X = 0; X < sizeInterf->iXWidth; X++)
01087         {
01088             if (badChannelList[X])
01089             {
01090                 fprintf (midiQcLogPtr, "QC.CALV%d           %s \n", X, UNAV);
01091                 fprintf (midiQcLogPtr, "QC.DCALV%d          %s \n", X, UNAV);
01092             }
01093             else
01094             {
01095                 fprintf (midiQcLogPtr, "QC.CALV%d           %f \n", X, (dispResult->calibVis2)[X]);
01096                 fprintf (midiQcLogPtr, "QC.DCALV%d          %f \n", X, (dispResult->calibVis2Err)[X]);
01097             }
01098         }
01099     }
01100 
01101     //    Add Target keywords
01102     fprintf (midiQcLogPtr, "QC.TARG.DIAM        \"UNKNOWN\" \n");
01103     fprintf (midiQcLogPtr, "QC.TARG.DDIAM       \"UNKNOWN\" \n");
01104 
01105     //    Add UVW. The values of uvw will be added during pipeline execution
01106     fprintf (midiQcLogPtr, "QC.BL.UVW1    %f \n", uvw->uCoord);
01107     fprintf (midiQcLogPtr, "QC.BL.UVW2    %f \n", uvw->vCoord);
01108     fprintf (midiQcLogPtr, "QC.BL.UVW3    %f \n", uvw->wCoord);
01109 
01110     //    Add binned photometry measurements
01111     if (strcmp(sizeInterf->beamCombiner, "HIGH_SENS") == 0)
01112     {
01113         fprintf (midiQcLogPtr, "QC.PHOTA.TARG1  %0.2f \n", dispResult->photomATarg1);
01114         fprintf (midiQcLogPtr, "QC.PHOTB.TARG1  %0.2f \n", dispResult->photomBTarg1);
01115 
01116         fprintf (midiQcLogPtr, "QC.PHOTA.TARG2  %0.2f \n", dispResult->photomATarg2);
01117         fprintf (midiQcLogPtr, "QC.PHOTB.TARG2  %0.2f \n", dispResult->photomBTarg2);
01118 
01119         fprintf (midiQcLogPtr, "QC.PHOTA.TARG3  %0.2f \n", dispResult->photomATarg3);
01120         fprintf (midiQcLogPtr, "QC.PHOTB.TARG3  %0.2f \n", dispResult->photomBTarg3);
01121 
01122         fprintf (midiQcLogPtr, "QC.PHOTA.TOTAL1  %0.2f \n", dispResult->photomATotal1);
01123         fprintf (midiQcLogPtr, "QC.PHOTB.TOTAL1  %0.2f \n", dispResult->photomBTotal1);
01124 
01125         fprintf (midiQcLogPtr, "QC.PHOTA.TOTAL2  %0.2f \n", dispResult->photomATotal2);
01126         fprintf (midiQcLogPtr, "QC.PHOTB.TOTAL2  %0.2f \n", dispResult->photomBTotal2);
01127 
01128         fprintf (midiQcLogPtr, "QC.PHOTA.TOTAL3  %0.2f \n", dispResult->photomATotal3);
01129         fprintf (midiQcLogPtr, "QC.PHOTB.TOTAL3  %0.2f \n", dispResult->photomBTotal3);
01130     }
01131 
01132     //    Add binned uncalibrated visibilities
01133     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED1  %0.4f \n", dispResult->uncalVisBinned1);
01134     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED2  %0.4f \n", dispResult->uncalVisBinned2);
01135     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED3  %0.4f \n", dispResult->uncalVisBinned3);
01136     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED4  %0.4f \n", dispResult->uncalVisBinned4);
01137     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED5  %0.4f \n", dispResult->uncalVisBinned5);
01138 
01139     //    Add number of changed target types
01140     fprintf (midiQcLogPtr, "QC.TARTYPE.INTERF.CHANGED   %ld \n", corrFrames->CorrInterf );
01141     fprintf (midiQcLogPtr, "QC.TARTYPE.PHOTOMA.CHANGED  %ld \n", corrFrames->CorrPhotomA );
01142     fprintf (midiQcLogPtr, "QC.TARTYPE.PHOTOMB.CHANGED  %ld \n", corrFrames->CorrPhotomB );
01143 
01144     
01145     //    Release memory
01146     free (messageBuffer);
01147     free (currentTime);
01148     free (textBuff);
01149     free (cleanString);
01150     free (stringTemp);
01151     free (classification);
01152 
01153     return;
01154 }
01155 /*****************************************************************************/
01156 
01157 
01158 
01159 /******************************************************************************
01160 *               European Southern Observatory
01161 *            VLTI MIDI Data Reduction Software
01162 *
01163 * Module name:  loadFrgOutputData
01164 * Input/Output: See function arguments to avoid duplication
01165 * Description:  Loads MIDI product data into appropriate data structures
01166 *
01167 *
01168 * History:
01169 * 11-Feb-05     (csabet) Created
01170 ******************************************************************************/
01171 void loadFrgOutputData (
01172     char            *obsCategory,    // In: Observation category
01173     char            *fileName,        // In: Input file name
01174     float           *waveCal,        // In: array of frequencies
01175     DispersedResult    *dispResult,    // In: Dispersed result
01176     IauExchange        *iauData,        // Ou: IAU Exchange data structure
01177     UVW                *uvw,            // Ou: Coordinates of the uvw
01178     int                *error)
01179 
01180 {
01181     //    Local Declarations
01182     //    ------------------
01183     const char        routine[] = "loadFrgOutputData";
01184     int                i, irec, iwave, itarg, mjdObs, *selection, extNumber;
01185     float            equinox;
01186     char            *stringQfits, *emptyString, *buffer, *observationData, *arrayName,
01187                     *instName, *cleanString;
01188     double            raEp0, decEp0, integrationTime;
01189     qfits_table        *arrayGeometry;
01190 /*     char               *qfitsRow=NULL; */
01191     short            *qfitsRow_int, targetId = 1;
01192     float            *qfitsRow_flt, nullFloat;
01193     double            *qfitsRow_dbl, utcTime;
01194 
01195     cpl_table      * table    =NULL;       
01196     char          ** tel_name =NULL;
01197     char          ** sta_name =NULL;
01198 
01199 
01200 
01201     //    Algorithm
01202     //    ---------
01203     *error = 0;
01204     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01205     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01206 
01207     //    Allocate memory
01208     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01209     emptyString = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01210     buffer = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01211     observationData = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01212     arrayName = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01213     instName = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01214 
01215     //    Initialize values
01216     strcpy (emptyString, "NULL");
01217     set_fnan (&nullFloat);
01218 
01219     //    Get general parameters from the Header of the input FITS file
01220     //    -------------------------------------------------------------
01221     stringQfits = qfits_query_hdr (fileName, "DATE-OBS");
01222     if (stringQfits == NULL)
01223         sscanf (emptyString, "%s", observationData);
01224     else
01225     {
01226         cleanUpString (stringQfits, cleanString);
01227         sscanf (cleanString, "%s", observationData);
01228     }
01229 
01230     stringQfits = qfits_query_hdr (fileName, "EXPTIME");
01231     if (stringQfits == NULL) integrationTime = nullFloat;
01232     else sscanf (stringQfits, "%lf", &integrationTime);
01233 
01234     extNumber = getFitsExtensionNumber (fileName, "ARRAY_GEOMETRY", error);
01235     stringQfits = qfits_query_ext (fileName, "ARRNAME", extNumber);
01236     if (stringQfits == NULL)
01237         sscanf (emptyString, "%s", arrayName);
01238     else
01239     {
01240         cleanUpString (stringQfits, cleanString);
01241         sscanf (cleanString, "%s", arrayName);
01242     }
01243 
01244     stringQfits = qfits_query_hdr (fileName, "INSTRUME");
01245     if (stringQfits == NULL)
01246         sscanf (emptyString, "%s", instName);
01247     else
01248     {
01249         cleanUpString (stringQfits, cleanString);
01250         sscanf (cleanString, "%s", instName);
01251     }
01252 
01253     stringQfits = qfits_query_hdr (fileName, "RA");
01254     if (stringQfits == NULL) raEp0 = nullFloat;
01255     else sscanf (stringQfits, "%lf", &raEp0);
01256 
01257     stringQfits = qfits_query_hdr (fileName, "DEC");
01258     if (stringQfits == NULL) decEp0 = nullFloat;
01259     else sscanf (stringQfits, "%lf", &decEp0);
01260 
01261     stringQfits = qfits_query_hdr (fileName, "EQUINOX");
01262     if (stringQfits == NULL) equinox = nullFloat;
01263     else sscanf (stringQfits, "%f", &equinox);
01264 
01265     stringQfits = qfits_query_hdr (fileName, "MJD-OBS");
01266     if (stringQfits == NULL) mjdObs = nullFloat;
01267     else sscanf (stringQfits, "%d", &mjdObs);
01268 
01269     stringQfits = qfits_query_hdr (fileName, "UTC");
01270     if (stringQfits == NULL) utcTime = nullFloat;
01271     else sscanf (stringQfits, "%lf", &utcTime);
01272 
01273     //    Load info for OI_ARRAY table
01274     //    ----------------------------
01275     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_ARRAY data ... \n");
01276     if (diagnostic) fprintf (midiReportPtr, "Loading OI_ARRAY data ... \n");
01277 
01278     //    Get the required parameters from the ARRAY_GEOMETRY extension of the input FITS file
01279     sscanf (arrayName, "%s", iauData->array->arrname);
01280 
01281     //    Get extension number
01282     extNumber = getFitsExtensionNumber (fileName, "ARRAY_GEOMETRY", error);
01283     
01284     stringQfits = qfits_query_ext (fileName, "FRAME", extNumber);
01285     if (stringQfits == NULL)
01286         sscanf (emptyString, "%s", iauData->array->frame);
01287     else
01288     {
01289         cleanUpString (stringQfits, cleanString);
01290         sscanf (cleanString, "%s", iauData->array->frame);
01291     }
01292     
01293     stringQfits = qfits_query_ext (fileName, "ARRAYX", extNumber);
01294     if (stringQfits == NULL) iauData->array->arrayx = nullFloat;
01295     else sscanf (stringQfits, "%lf", &(iauData->array->arrayx));
01296 
01297     stringQfits = qfits_query_ext (fileName, "ARRAYY", extNumber);
01298     if (stringQfits == NULL) iauData->array->arrayy = nullFloat;
01299     else sscanf (stringQfits, "%lf", &(iauData->array->arrayy));
01300 
01301     stringQfits = qfits_query_ext (fileName, "ARRAYZ", extNumber);
01302     if (stringQfits == NULL) iauData->array->arrayz = nullFloat;
01303     else sscanf (stringQfits, "%lf", &(iauData->array->arrayz));
01304 
01305     //    Open the table
01306     arrayGeometry = qfits_table_open (fileName, extNumber);
01307     
01308     
01309     table = cpl_table_load(fileName, extNumber, 1);
01310     
01311     if (cpl_table_has_column(table, "TEL_NAME")) tel_name=cpl_table_get_data_string(table, "TEL_NAME");
01312     if (cpl_table_has_column(table, "STA_NAME")) sta_name=cpl_table_get_data_string(table, "STA_NAME");
01313     
01314     
01315 /*     for (i=0; i<cpl_table_get_nrow(table);i++){ */
01316 /*        cpl_msg_info(cpl_func, "tel_name: %s  sta_name: %s",tel_name[i], sta_name[i]); */
01317 /*     } */
01318     
01319 
01320     
01321 
01322 
01323     //    Load info for OI_ARRAY table
01324     //    ----------------------------
01325     selection = (int *) calloc (iauData->array->nelement, sizeof (int));
01326     for (i = 0; i < iauData->array->nelement; i++)
01327     {
01328         selection[i] = 1;
01329         sprintf ((iauData->array->elem)[i].tel_name, "%s", tel_name[i]);        
01330         sprintf ((iauData->array->elem)[i].sta_name, "%s", sta_name[i]);        
01331         if (diagnostic > 2){
01332            cpl_msg_info(cpl_func,"iauData->array->elem)[i].tel_name: %s tel_name[i] %s", (iauData->array->elem)[i].tel_name,tel_name[i]); 
01333            cpl_msg_info(cpl_func,"iauData->array->elem)[i].sta_name: %s sta_name[i] %s", (iauData->array->elem)[i].sta_name,sta_name[i]);
01334         }
01335  
01336 
01337 /*        qfitsRow = (char *) (qfits_query_column (arrayGeometry, 0, selection)); */
01338 /*         if (diagnostic > 2)cpl_msg_info(cpl_func,"qfitsRow    = %s \n", qfitsRow); */
01339 /*         removeNewLine (qfitsRow, cleanString); */
01340 /*         if (diagnostic > 2)cpl_msg_info(cpl_func,"cleanString = %s \n", cleanString); */
01341 /*         sprintf ((iauData->array->elem)[i].tel_name, "%s", cleanString); */
01342 /*         free (qfitsRow); */
01343 
01344         
01345 /*         qfitsRow = (char *) (qfits_query_column (arrayGeometry, 1, selection)); */
01346 /*         removeNewLine (qfitsRow, cleanString); */
01347 /*         sprintf ((iauData->array->elem)[i].sta_name, "%s", cleanString); */
01348 /*         free (qfitsRow); */
01349 
01350 
01351         qfitsRow_int = (short *) (qfits_query_column (arrayGeometry, 2, selection));
01352         (iauData->array->elem)[i].sta_index = qfitsRow_int[0];
01353         free (qfitsRow_int);
01354 
01355         qfitsRow_flt = (float *) (qfits_query_column (arrayGeometry, 3, selection));
01356         (iauData->array->elem)[i].diameter = qfitsRow_flt[0];
01357         free (qfitsRow_flt);
01358 
01359         qfitsRow_dbl = (double *) (qfits_query_column (arrayGeometry, 4, selection));
01360         (iauData->array->elem)[i].staxyz[0] = qfitsRow_dbl[0];
01361         (iauData->array->elem)[i].staxyz[1] = qfitsRow_dbl[1];
01362         (iauData->array->elem)[i].staxyz[2] = qfitsRow_dbl[2];
01363         free (qfitsRow_dbl);
01364 
01365         selection[i] = 0;
01366     }
01367 /*     Free table memory */
01368     cpl_table_delete(table);    
01369 
01370     sprintf (iauData->array->revision, "%s", IAUEXCHANGE_VERSION);
01371     free (selection);
01372     qfits_table_close (arrayGeometry);
01373 
01374     //    Load info for OI_TARGET table
01375     //    -----------------------------
01376     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_TARGET data ... \n");
01377     if (diagnostic) fprintf (midiReportPtr, "Loading OI_TARGET data ... \n");
01378 
01379     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TARG NAME");
01380     if (stringQfits == NULL)
01381         sscanf (emptyString, "%s", buffer);
01382     else
01383     {
01384         cleanUpString (stringQfits, cleanString);
01385         sscanf (cleanString, "%s", buffer);
01386     }
01387 
01388     for (itarg = 0; itarg < iauData->targets->ntarget; itarg++)
01389     {
01390         (iauData->targets->targ)[itarg].target_id = targetId;
01391         sscanf (buffer, "%s", (iauData->targets->targ)[itarg].target);
01392         (iauData->targets->targ)[itarg].raep0 = raEp0;
01393         (iauData->targets->targ)[itarg].decep0 = decEp0;
01394         (iauData->targets->targ)[itarg].equinox = equinox;
01395         (iauData->targets->targ)[itarg].ra_err = nullFloat;
01396         (iauData->targets->targ)[itarg].dec_err = nullFloat;
01397         (iauData->targets->targ)[itarg].sysvel = nullFloat;
01398         sscanf (emptyString, "%s", (iauData->targets->targ)[itarg].veltyp);
01399         sscanf (emptyString, "%s", (iauData->targets->targ)[itarg].veldef);
01400         (iauData->targets->targ)[itarg].pmra = nullFloat;
01401         (iauData->targets->targ)[itarg].pmdec = nullFloat;
01402         (iauData->targets->targ)[itarg].pmra_err = nullFloat;
01403         (iauData->targets->targ)[itarg].pmdec_err = nullFloat;
01404         (iauData->targets->targ)[itarg].parallax = nullFloat;
01405         (iauData->targets->targ)[itarg].para_err = nullFloat;
01406         sscanf (emptyString, "%s", (iauData->targets->targ)[itarg].spectyp);
01407     }
01408     sprintf (iauData->targets->revision, "%s", IAUEXCHANGE_VERSION);
01409 
01410 
01411     //    Load info for OI_WAVELENGTH table
01412     //    ---------------------------------
01413     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_WAVELENGTH data ... \n");
01414     if (diagnostic) fprintf (midiReportPtr, "Loading OI_WAVELENGTH data ... \n");
01415 
01416     //    Compute the parameters
01417     sscanf (instName, "%s", iauData->wavelength->insname);
01418     for (i = 0; i < (iauData->wavelength->nwave-1); i++)
01419     {
01420         (iauData->wavelength->eff_wave)[i] = 1.e-6 * waveCal[i];
01421         (iauData->wavelength->eff_band)[i] =  1.e-6 * fabs ((waveCal[1+i] - waveCal[i]));
01422     }
01423     //    Final row
01424     (iauData->wavelength->eff_wave)[iauData->wavelength->nwave-1] = 1.e-6 * 
01425         waveCal[iauData->wavelength->nwave-1];
01426     (iauData->wavelength->eff_band)[iauData->wavelength->nwave-1] =
01427         (iauData->wavelength->eff_band)[iauData->wavelength->nwave-2];
01428     sprintf (iauData->wavelength->revision, "%s", IAUEXCHANGE_VERSION);
01429 
01430     //    Get the uvw parameters
01431     computeUVW ((iauData->array->elem)[0].staxyz[0], (iauData->array->elem)[0].staxyz[1], (iauData->array->elem)[0].staxyz[2], 
01432         (iauData->array->elem)[1].staxyz[0], (iauData->array->elem)[1].staxyz[1], (iauData->array->elem)[1].staxyz[2],
01433         mjdObs, raEp0, decEp0, uvw);
01434 
01435     //    Load info for OI_VIS table
01436     //    --------------------------
01437     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_VIS data ... \n");
01438     if (diagnostic) fprintf (midiReportPtr, "Loading OI_VIS data ... \n");
01439 
01440     //    Get the required parameters from the FITS file
01441     sscanf (observationData, "%s", iauData->vis->date_obs);
01442     sscanf (arrayName, "%s", iauData->vis->arrname);
01443     sscanf (instName, "%s", iauData->vis->insname);
01444     for (irec = 0; irec < iauData->vis->numrec; irec++)
01445     {
01446         (iauData->vis->record)[irec].target_id = targetId;
01447         (iauData->vis->record)[irec].time = utcTime;
01448         (iauData->vis->record)[irec].mjd = mjdObs;
01449         (iauData->vis->record)[irec].int_time = integrationTime;
01450         (iauData->vis->record)[irec].ucoord = uvw->uCoord;
01451         (iauData->vis->record)[irec].vcoord = uvw->vCoord;
01452         (iauData->vis->record)[irec].sta_index[0] = (iauData->array->elem)[0].sta_index;
01453         (iauData->vis->record)[irec].sta_index[1] = (iauData->array->elem)[1].sta_index;
01454 
01455         if ((strcmp (obsCategory, "SCIENCE") == 0) && dispResult->calibVisExists)
01456         {
01457             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01458             {
01459                 if (badChannelList[iwave])
01460                 {
01461                     (iauData->vis->record)[irec].visamp[iwave] = nullFloat;
01462                     (iauData->vis->record)[irec].visamperr[iwave] = nullFloat;
01463                 }
01464                 else
01465                 {
01466                     (iauData->vis->record)[irec].visamp[iwave] = sqrt ((dispResult->calibVis2)[iwave]);
01467                     (iauData->vis->record)[irec].visamperr[iwave] = sqrt ((dispResult->calibVis2Err)[iwave]);
01468                 }                
01469                 (iauData->vis->record)[irec].visphi[iwave] = nullFloat;
01470                 (iauData->vis->record)[irec].visphierr[iwave] = nullFloat;
01471             }
01472         }
01473         else
01474         {
01475             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01476             {
01477                 if (badChannelList[iwave])
01478                 {
01479                     (iauData->vis->record)[irec].visamp[iwave] = nullFloat;
01480                     (iauData->vis->record)[irec].visamperr[iwave] = nullFloat;
01481                 }
01482                 else
01483                 {
01484                     (iauData->vis->record)[irec].visamp[iwave] = sqrt ((dispResult->normVis2)[iwave]);
01485                     (iauData->vis->record)[irec].visamperr[iwave] = sqrt ((dispResult->normVis2Err)[iwave]);
01486                 }                
01487                 (iauData->vis->record)[irec].visphi[iwave] = nullFloat;
01488                 (iauData->vis->record)[irec].visphierr[iwave] = nullFloat;
01489             }
01490         }
01491         sprintf ((iauData->vis->record)[irec].flag, "%s", "FALSE");
01492     }
01493     sprintf (iauData->vis->revision, "%s", IAUEXCHANGE_VERSION);
01494     iauData->vis->nwave = iauData->wavelength->nwave;
01495 
01496 
01497     //    Load info for OI_VIS2 table
01498     //    ---------------------------
01499     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_VIS2 data ... \n");
01500     if (diagnostic) fprintf (midiReportPtr, "Loading OI_VIS2 data ... \n");
01501 
01502     //    Get the required parameters from the FITS file
01503     sscanf (observationData, "%s", iauData->vis2->date_obs);
01504     sscanf (arrayName, "%s", iauData->vis2->arrname);
01505     sscanf (instName, "%s", iauData->vis2->insname);
01506     for(irec = 0; irec < iauData->vis2->numrec; irec++)
01507     {
01508         (iauData->vis2->record)[irec].target_id = targetId;
01509         (iauData->vis2->record)[irec].time = utcTime;
01510         (iauData->vis2->record)[irec].mjd = mjdObs;
01511         (iauData->vis2->record)[irec].int_time = integrationTime;
01512         (iauData->vis2->record)[irec].ucoord = uvw->uCoord;
01513         (iauData->vis2->record)[irec].vcoord = uvw->vCoord;
01514         (iauData->vis2->record)[irec].sta_index[0] = (iauData->array->elem)[0].sta_index;;
01515         (iauData->vis2->record)[irec].sta_index[1] = (iauData->array->elem)[1].sta_index;;
01516 
01517         if ((strcmp (obsCategory, "SCIENCE") == 0) && dispResult->calibVisExists)
01518         {
01519             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01520             {
01521                 if (badChannelList[iwave])
01522                 {
01523                     (iauData->vis2->record)[irec].vis2data[iwave] = nullFloat;
01524                     (iauData->vis2->record)[irec].vis2err[iwave] = nullFloat;
01525                 }
01526                 else
01527                 {
01528                     (iauData->vis2->record)[irec].vis2data[iwave] = (dispResult->calibVis2)[iwave];
01529                     (iauData->vis2->record)[irec].vis2err[iwave] = (dispResult->calibVis2Err)[iwave];
01530                 }                
01531             }
01532         }
01533         else
01534         {
01535             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01536             {
01537                 if (badChannelList[iwave])
01538                 {
01539                     (iauData->vis2->record)[irec].vis2data[iwave] = nullFloat;
01540                     (iauData->vis2->record)[irec].vis2err[iwave] = nullFloat;
01541                 }
01542                 else
01543                 {
01544                     (iauData->vis2->record)[irec].vis2data[iwave] = (dispResult->normVis2)[iwave];
01545                     (iauData->vis2->record)[irec].vis2err[iwave] = (dispResult->normVis2Err)[iwave];
01546                 }
01547             }
01548         }
01549         sprintf ((iauData->vis2->record)[irec].flag, "%s", "FALSE");
01550     }
01551     sprintf (iauData->vis2->revision, "%s", IAUEXCHANGE_VERSION);
01552     iauData->vis2->nwave = iauData->wavelength->nwave;
01553 
01554     //    Release memory
01555     free (instName);
01556     free (arrayName);
01557     free (observationData);
01558     free (emptyString);
01559     free (buffer);
01560     free (cleanString);
01561 
01562     return;
01563 
01564 }
01565 /*****************************************************************************/
01566 
01567 
01568 
01569 
01570 /******************************************************************************
01571 *               European Southern Observatory
01572 *            VLTI MIDI Data Reduction Software
01573 *
01574 * Module name:  writeFrgFitsFile
01575 * Input/Output: See function arguments to avoid duplication
01576 * Description:  Writes and creates appropriate tables into a new fits file in
01577 *               accordance with the IAU format
01578 *
01579 * History:
01580 * 11-Feb-05     (csabet) Created
01581 ******************************************************************************/
01582 void writeFrgFitsFile (
01583     char        *outFitsName,
01584     IauExchange    *iauData,
01585     int            *error)
01586 {
01587     //    Local Declarations
01588     //    ------------------
01589     const char  routine[] = "writeFrgFitsFile";
01590 
01591     //    Algorithm
01592     //    ---------
01593     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01594     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01595 
01596     if (diagnostic)cpl_msg_info(cpl_func,"Writing data into the output FITS file \n\n");
01597     if (diagnostic) fprintf (midiReportPtr, "Writing data into the output FITS file \n\n");
01598 
01599     //    Reset parameters
01600     *error = 0;
01601 
01602     //    Write OI_ARRAY table
01603     writeOiArray (outFitsName, iauData->array, error);
01604     if (*error)
01605     {
01606         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_ARRAY in the output FITS file");
01607         return;
01608     }
01609 
01610     //    Write OI_TARGET table
01611     writeOiTarget (outFitsName, iauData->targets, error);
01612     if (*error)
01613     {
01614         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_TARGET in the output FITS file");
01615         return;
01616     }
01617 
01618     //    Write OI_WAVELENGTH table
01619     writeOiWavelength (outFitsName, iauData->wavelength, error);
01620     if (*error)
01621     {
01622         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_WAVELENGTH in the output FITS file");
01623         return;
01624     }
01625 
01626     //    Write OI_VIS table
01627     writeOiVis (outFitsName, iauData->vis, error);
01628     if (*error)
01629     {
01630         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_VIS in the output FITS file");
01631         return;
01632     }
01633 
01634     //    Write OI_VIS2 table
01635     writeOiVis2 (outFitsName, iauData->vis2, error);
01636     if (*error)
01637     {
01638         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_VIS2 in the output FITS file");
01639         return;
01640     }
01641 
01642     return;
01643 
01644 }
01645 /*****************************************************************************/
01646 

Generated on 11 Feb 2011 for MIDI Pipeline Reference Manual by  doxygen 1.6.1