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     {
00540         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TRF", UNAV, "Flag for Transfer Function availability", NULL);
00541         if (strcmp (sizeInterf->obsCatg, "CALIB") == 0)
00542         {
00543             sprintf (messageBuffer, "%f", calibrator->calibPblAverage);
00544             qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL LENGTH", messageBuffer, "Baseline vector length", "");
00545             sprintf (messageBuffer, "%f", calibrator->calibParangAverage);
00546             qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL ANGLE", messageBuffer, "Baseline vector angle", "");
00547 
00548         }
00549     }
00550 
00551     //    Add uncalibrated Normal Visibilities
00552     for (X = 0; X < sizeInterf->iXWidth; X++)
00553     {
00554         if (badChannelList[X])
00555         {
00556             sprintf (textBuff, "HIERARCH ESO QC UNCALV%d", X);
00557             sprintf (messageBuffer, "%s", UNAV);
00558             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Uncalibrated Normal Visibility", "");
00559 
00560             sprintf (textBuff, "HIERARCH ESO QC DUNCALV%d", X);
00561             sprintf (messageBuffer, "%s", UNAV);
00562             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of uncalibrated Normal Visibility", "");
00563         }
00564         else
00565         {
00566             sprintf (textBuff, "HIERARCH ESO QC UNCALV%d", X);
00567             sprintf (messageBuffer, "%f", (dispResult->normVis2)[X]);
00568             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Uncalibrated Normal Visibility", "");
00569 
00570             sprintf (textBuff, "HIERARCH ESO QC DUNCALV%d", X);
00571             sprintf (messageBuffer, "%f", (dispResult->normVis2Err)[X]);
00572             qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of uncalibrated Normal Visibility", "");
00573         }
00574     }
00575     
00576 
00577     //    Add Photometry parameters
00578     for (X = 0; X < sizeInterf->iXWidth; X++)
00579     {
00580         if (!(badChannelList[X]))
00581         {
00582             photA += (dispResult->photomA)[0][X];
00583             photAErr += (dispResult->photomAErr)[0][X];
00584             photB += (dispResult->photomB)[0][X];
00585             photBErr += (dispResult->photomBErr)[0][X];
00586             photI += (dispResult->photomI)[0][X];
00587             photIErr += (dispResult->photomIErr)[0][X];
00588             photNet2 += (dispResult->photomNet2)[X];
00589             photNet2Err += (dispResult->photomNet2Err)[X];
00590         }
00591     }
00592     if (sizeInterf->numOfChannelsProcessed)
00593     {
00594         photA /= sizeInterf->numOfChannelsProcessed;
00595         photAErr /= sizeInterf->numOfChannelsProcessed;
00596         photB /= sizeInterf->numOfChannelsProcessed;
00597         photBErr /= sizeInterf->numOfChannelsProcessed;
00598         photI /= sizeInterf->numOfChannelsProcessed;
00599         photIErr /= sizeInterf->numOfChannelsProcessed;
00600         photNet2 /= sizeInterf->numOfChannelsProcessed;
00601         photNet2Err /= sizeInterf->numOfChannelsProcessed;
00602     }
00603     sprintf (textBuff, "HIERARCH ESO QC AV PHOTA");
00604     sprintf (messageBuffer, "%f", photA);
00605     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00606     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTA");
00607     sprintf (messageBuffer, "%f", photAErr);
00608     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00609     sprintf (textBuff, "HIERARCH ESO QC AV PHOTB");
00610     sprintf (messageBuffer, "%f", photB);
00611     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00612     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTB");
00613     sprintf (messageBuffer, "%f", photBErr);
00614     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00615     sprintf (textBuff, "HIERARCH ESO QC AV PHOTI");
00616     sprintf (messageBuffer, "%f", photI);
00617     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00618     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTI");
00619     sprintf (messageBuffer, "%f", photIErr);
00620     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00621     sprintf (textBuff, "HIERARCH ESO QC AV PHOTN");
00622     sprintf (messageBuffer, "%f", photNet2);
00623     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Average photometry", "");
00624     sprintf (textBuff, "HIERARCH ESO QC AV DPHOTN");
00625     sprintf (messageBuffer, "%f", photNet2Err);
00626     qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of Average photometry", "");
00627 
00628 
00629     //    Add Calibrated Visibilities
00630     if (dispResult->calibVisExists)
00631     {
00632         for (X = 0; X < sizeInterf->iXWidth; X++)
00633         {
00634             if (badChannelList[X])
00635             {
00636                 sprintf (textBuff, "HIERARCH ESO QC CALV%d", X);
00637                 sprintf (messageBuffer, "%s", UNAV);
00638                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Calibrated Visibility", "");
00639 
00640                 sprintf (textBuff, "HIERARCH ESO QC DCALV%d", X);
00641                 sprintf (messageBuffer, "%s", UNAV);
00642                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of calibrated Visibility", "");
00643             }
00644             else
00645             {
00646                 sprintf (textBuff, "HIERARCH ESO QC CALV%d", X);
00647                 sprintf (messageBuffer, "%f", (dispResult->calibVis2)[X]);
00648                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "Calibrated Visibility", "");
00649 
00650                 sprintf (textBuff, "HIERARCH ESO QC DCALV%d", X);
00651                 sprintf (messageBuffer, "%f", (dispResult->calibVis2Err)[X]);
00652                 qfits_header_add (outFitsHeader, textBuff, messageBuffer, "(sigma) Error of calibrated Visibility", "");
00653             }
00654         }
00655     }
00656 
00657 
00658     //    Add Target keywords
00659     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARG DIAM", "UNKNOWN", "Estimated diameter of the target (mas, Un.Disk model)", "");
00660     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARG DDIAM", "UNKNOWN", "(sigma) Error on estimated target diameter in milliarcsec", "");
00661 
00662     //    Add UVW. The values of uvw will be added during pipeline execution
00663     sprintf (messageBuffer, "%f", uvw->uCoord);
00664     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW1", messageBuffer, "u component of UVW vector", "");
00665     sprintf (messageBuffer, "%f", uvw->vCoord);
00666     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW2", messageBuffer, "v component of UVW vector", "");
00667     sprintf (messageBuffer, "%f", uvw->wCoord);
00668     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW3", messageBuffer, "w component of UVW vector", "");
00669 
00670     //    Add binned photometry measurements
00671     if ((strcmp(sizeInterf->grismId, "PRISM") == 0) && (strcmp(sizeInterf->beamCombiner, "HIGH_SENS") == 0))
00672     {
00673         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg1);
00674         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG1", messageBuffer, "PhotA Targ1 Ch 113 - 121", "");
00675         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg1);
00676         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG1", messageBuffer, "PhotB Targ1 Ch 113 - 121", "");
00677 
00678         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg2);
00679         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG2", messageBuffer, "PhotA Targ2 Ch 88 - 98", "");
00680         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg2);
00681         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG2", messageBuffer, "PhotB Targ2 Ch 88 - 98", "");
00682 
00683         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg3);
00684         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG3", messageBuffer, "PhotA Targ3 Ch 36 - 50", "");
00685         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg3);
00686         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG3", messageBuffer, "PhotB Targ3 Ch 36 - 50", "");
00687 
00688         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal1);
00689         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL1", messageBuffer, "PhotA Total1 Ch 113 - 121", "");
00690         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal1);
00691         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL1", messageBuffer, "PhotB Total1 Ch 113 - 121", "");
00692 
00693         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal2);
00694         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL2", messageBuffer, "PhotA Total2 Ch 88 - 98", "");
00695         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal2);
00696         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL2", messageBuffer, "PhotB Total2 Ch 88 - 98", "");
00697 
00698         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal3);
00699         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL3", messageBuffer, "PhotA Total3 Ch 36 - 50", "");
00700         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal3);
00701         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL3", messageBuffer, "PhotB Total3 Ch 36 - 50", "");
00702     }
00703     else
00704     {
00705         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg1);
00706         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG1", messageBuffer, "PhotA Targ1 Ch 52 - 68", "");
00707         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg1);
00708         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG1", messageBuffer, "PhotB Targ1 Ch 52 - 68", "");
00709 
00710         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg2);
00711         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG2", messageBuffer, "PhotA Targ2 Ch 116 - 136", "");
00712         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg2);
00713         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG2", messageBuffer, "PhotB Targ2 Ch 116 - 136", "");
00714 
00715         sprintf (messageBuffer, "%0.2f", dispResult->photomATarg3);
00716         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TARG3", messageBuffer, "PhotA Targ3 Ch 217 - 244", "");
00717         sprintf (messageBuffer, "%0.2f", dispResult->photomBTarg3);
00718         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TARG3", messageBuffer, "PhotB Targ3 Ch 217 - 244", "");
00719 
00720         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal1);
00721         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL1", messageBuffer, "PhotA Total1 Ch 52 - 68", "");
00722         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal1);
00723         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL1", messageBuffer, "PhotB Total1 Ch 52 - 68", "");
00724 
00725         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal2);
00726         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL2", messageBuffer, "PhotA Total2 Ch 116 - 136", "");
00727         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal2);
00728         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL2", messageBuffer, "PhotB Total2 Ch 116 - 136", "");
00729 
00730         sprintf (messageBuffer, "%0.2f", dispResult->photomATotal3);
00731         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA TOTAL3", messageBuffer, "PhotA Total3 Ch 217 - 244", "");
00732         sprintf (messageBuffer, "%0.2f", dispResult->photomBTotal3);
00733         qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB TOTAL3", messageBuffer, "PhotB Total3 Ch 217 - 244", "");
00734     }
00735 
00736     //    Add binned uncalibrated visibilities
00737     if (strcmp(sizeInterf->grismId, "PRISM") == 0)
00738     {
00739         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned1);
00740         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED1", messageBuffer, 
00741             "Binned uncal vis at 8.64 micron (Ch 120-126)", "");
00742         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned2);
00743         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED2", messageBuffer, 
00744             "Binned uncal vis at 9.00 micron (Ch 113-121)", "");
00745         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned3);
00746         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED3", messageBuffer, 
00747             "Binned uncal vis at 10.46 micron (Ch 88-98)", "");
00748         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned4);
00749         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED4", messageBuffer, 
00750             "Binned uncal vis at 11.79 micron (Ch 60-72)", "");
00751         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned5);
00752         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED5", messageBuffer, 
00753             "Binned uncal vis at 12.80 micron (Ch 36-50)", "");
00754     }
00755     else
00756     {
00757         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned1);
00758         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED1", messageBuffer, 
00759             "Binned uncal vis at 8.64 micron (Ch 38-50)", "");
00760         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned2);
00761         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED2", messageBuffer, 
00762             "Binned uncal vis at 9.00 micron (Ch 52-68)", "");
00763         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned3);
00764         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED3", messageBuffer, 
00765             "Binned uncal vis at 10.46 micron (Ch 116-136)", "");
00766         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned4);
00767         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED4", messageBuffer, 
00768             "Binned uncal vis at 11.79 micron (Ch 176-200)", "");
00769         sprintf (messageBuffer, "%0.4f", dispResult->uncalVisBinned5);
00770         qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCAL BINNED5", messageBuffer, 
00771             "Binned uncal vis at 12.80 micron (Ch 217-245)", "");
00772     }
00773 
00774     sprintf (messageBuffer, "%ld", corrFrames->CorrInterf);
00775     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARTYPE INTERF CHANGED", messageBuffer,
00776         "Number of type-modified frames", "");
00777 
00778     sprintf (messageBuffer, "%ld", corrFrames->CorrPhotomA);
00779     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARTYPE PHOTOMA CHANGED", messageBuffer,
00780         "Number of type-modified frames", "");
00781 
00782     sprintf (messageBuffer, "%ld", corrFrames->CorrPhotomB);
00783     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARTYPE PHOTOMB CHANGED", messageBuffer,
00784         "Number of type-modified frames", "");
00785 
00786     //    Place Holders
00787     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH1", "TBD", "TBD", "");
00788     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH2", "TBD", "TBD", "");
00789     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH3", "TBD", "TBD", "");
00790     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH4", "TBD", "TBD", "");
00791     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH5", "TBD", "TBD", "");
00792     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH6", "TBD", "TBD", "");
00793     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH7", "TBD", "TBD", "");
00794     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH8", "TBD", "TBD", "");
00795     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH9", "TBD", "TBD", "");
00796 
00797     //    Create the output fits file
00798     if (stat (fileNames->outFitsName, &buf) == 0)   // If the file exist delete it
00799         remove (fileNames->outFitsName);
00800     outFitsPtr = fopen (fileNames->outFitsName, "w");
00801     if (!outFitsPtr)
00802     {
00803         *error = 1;
00804         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create output FITS file");
00805         free (messageBuffer);
00806         free (currentTime);
00807         free (cleanString);
00808         free (stringTemp);
00809         free (classification);
00810         free (textBuff);
00811         return;
00812     }
00813 
00814     //    Write header into the output file
00815     qfits_header_sort (&outFitsHeader);
00816     qfits_header_dump (outFitsHeader, outFitsPtr);
00817     qfits_header_destroy (outFitsHeader);
00818     fclose (outFitsPtr);
00819 
00820     //    release memory
00821     free (messageBuffer);
00822     free (currentTime);
00823     free (cleanString);
00824     free (stringTemp);
00825     free (classification);
00826     free (textBuff);
00827 
00828     return;
00829 }
00830 /*****************************************************************************/
00831 
00832 
00833 
00834 
00835 /******************************************************************************
00836 *               European Southern Observatory
00837 *            VLTI MIDI Data Reduction Software
00838 *
00839 * Module name:  addProdInfoToFrgQcLog
00840 * Input/Output: See function arguments to avoid duplication
00841 * Description:  Adds product information to QC log
00842 *
00843 *
00844 * History:
00845 * 09-Feb-05     (csabet) Created
00846 ******************************************************************************/
00847 void addProdInfoToFrgQcLog (
00848     MidiFiles        *fileNames,        // In: File names
00849     ImageFormat        *sizeInterf,    // In: Interferometry data parameters
00850     DispersedResult    *dispResult,    // In: Dispersed result
00851     UVW                *uvw,            // In: UVW coordinates
00852     float           *freqCal,        // In: Frequency array
00853     CalibratorParam    *calibrator,    // In: Calibrator parameters
00854     int                *error,            // Ou: Error status
00855     CorrectedFrames   *corrFrames)
00856 {
00857 
00858     //    Local Declarations
00859     //    ------------------
00860     const char      routine[] = "addProdInfoToFrgQcLog";
00861     int             X;
00862     FILE            *inFitsBatchPtr;
00863     char            *stringQfits, *messageBuffer, *currentTime, *textBuff, *cleanString,
00864                     *stringTemp, *classification;
00865     unsigned int    stringLength;
00866     time_t          now;
00867     struct tm       *newTime;
00868     float            photA=0.0, photAErr=0.0, photB=0.0, photBErr=0.0, photI=0.0, photIErr=0.0,
00869                     photNet2=0.0, photNet2Err=0.0;
00870 
00871     //    Algorithm
00872     //    ---------
00873     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00874     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00875 
00876     //    Reset parameters
00877     *error = 0;
00878 
00879     //    Allocate memory
00880     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00881     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00882     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00883     textBuff = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00884     messageBuffer = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00885     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00886 
00887     //    Get current time/date
00888     now = time(NULL);
00889     newTime = gmtime (&now);
00890     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00891 
00892     //    Add keyword to QC log file
00893     fprintf (midiQcLogPtr, "PRO.TYPE            \"%s\" \n", sizeInterf->obsType);
00894 
00895     if ((strcmp (batchTemplate, "SCI_PHOT_CALIB") == 0) ||    (strcmp (batchTemplate, "SCI_PHOT_SCIENCE") == 0))
00896         fprintf (midiQcLogPtr, "PRO.CATG            \"REDUCED_DISPERSED_SCIPHOT\" \n");
00897     else
00898         fprintf (midiQcLogPtr, "PRO.CATG            \"REDUCED_DISPERSED\" \n");
00899 
00900     fprintf (midiQcLogPtr, "PRO.ARCFILE         \"%s\" \n", fileNames->archFileName);
00901     fprintf (midiQcLogPtr, "PRO.PIPEDATE        \"%s\" \n", currentTime);
00902     fprintf (midiQcLogPtr, "PRO.VERSION         \"%s\" \n", MIDI_PIPE_VERSION);
00903     fprintf (midiQcLogPtr, "PRO.PIPEFILE        \"%s\" \n", fileNames->pipeFileName);
00904     fprintf (midiQcLogPtr, "PRO.DID             \"%s\" \n", MIDI_QC_DIC_VERSION);
00905 
00906     //    Open the list of files
00907     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00908     {
00909         *error = 1;
00910         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00911         free (messageBuffer);
00912         free (currentTime);
00913         free (cleanString);
00914         free (stringTemp);
00915         free (classification);
00916         free (textBuff);
00917         return;
00918     }
00919 
00920     //    Loop through the list of files and write into the QC log
00921     X = 1;
00922     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00923     {
00924         sprintf (classification, "%s", "");
00925         sscanf (stringTemp, "%s%s", messageBuffer, classification);
00926 
00927         //    If classification is not given get it from the raw file
00928         if (strcmp (classification, "") == 0)
00929         {
00930             stringQfits = qfits_query_hdr (messageBuffer, "HIERARCH ESO DPR CATG");
00931             if (stringQfits == NULL)
00932             {
00933                 sprintf (classification, "%s", "UNKNOWN");
00934                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get Observation Category");
00935             }
00936             else
00937             {
00938                 cleanUpString (stringQfits, cleanString);
00939                 sprintf (classification, "%s", cleanString);
00940             }
00941         }
00942         removePathName (messageBuffer, midiReportPtr);
00943         fprintf (midiQcLogPtr, "PRO.REC1.RAW%d.NAME  \"%s\" \n", X, messageBuffer);
00944         fprintf (midiQcLogPtr, "PRO.REC1.RAW%d.CATG  \"%s\" \n", X++, classification);
00945     }
00946     fclose (inFitsBatchPtr);
00947 
00948     //    Add name of Mask file to QC log file
00949     sprintf (messageBuffer, "%s", fileNames->maskFileName);
00950     removePathName (messageBuffer, midiReportPtr);
00951     fprintf (midiQcLogPtr, "PRO.REC2.RAW1.NAME  \"%s\" \n", messageBuffer);
00952 
00953     //    Add name of Transfer Function file to QC log file
00954     if (strcmp (sizeInterf->obsCatg, "CALIB") == 0) sprintf (messageBuffer, "%s", fileNames->trfNameWrite);
00955     else sprintf (messageBuffer, "%s", fileNames->trfNameRead);
00956     removePathName (messageBuffer, midiReportPtr);
00957     stringLength = strlen (messageBuffer);
00958     sprintf (textBuff, "r.");
00959     strncat (textBuff, messageBuffer, stringLength-4);
00960     sprintf (messageBuffer, "%s_tpl_0001.dummy", textBuff);
00961     fprintf (midiQcLogPtr, "PRO.REC2.RAW2.NAME  \"%s\" \n", messageBuffer);
00962 
00963     //    Scan parameters
00964     fprintf (midiQcLogPtr, "QC.CH.PROCESSED  %d \n", sizeInterf->numOfChannelsProcessed);
00965     fprintf (midiQcLogPtr, "QC.CH.REJECTED   %d \n", sizeInterf->numOfChannelsRejected);
00966 
00967     //    Add Wavelengths
00968     for (X = 0; X < sizeInterf->iXWidth; X++)
00969     {
00970         if (badChannelList[X])
00971         {
00972             fprintf (midiQcLogPtr, "QC.LAMBDA%d            %s \n", X, UNAV);
00973             continue;
00974         }
00975             
00976         fprintf (midiQcLogPtr, "QC.LAMBDA%d            %f \n", X, freqCal[X]);
00977     }
00978     
00979     //    Transfer functions
00980     if (dispResult->trfExists)
00981     {
00982         if (strcmp (sizeInterf->obsCatg, "CALIB") == 0)
00983         {
00984             //    Add Calibrator keywords
00985             fprintf (midiQcLogPtr, "QC.THEOVIS          %f \n", calibrator->calibVisSqrd);
00986             fprintf (midiQcLogPtr, "QC.DTHEOVIS         %f \n", calibrator->calibVisSqrdErr);
00987             fprintf (midiQcLogPtr, "QC.CALIB.POS        \"UNKNOWN\" \n");
00988             fprintf (midiQcLogPtr, "QC.CALIB.NAME       \"%s\" \n", calibrator->calibName);
00989             fprintf (midiQcLogPtr, "QC.CALIB.RA         %f \n", calibrator->calibRA);
00990             fprintf (midiQcLogPtr, "QC.CALIB.DEC        %f \n", calibrator->calibDEC);
00991             fprintf (midiQcLogPtr, "QC.CALIB.DIST       %f \n", RAD_TO_ARCSEC * calibrator->calibDistance);
00992             fprintf (midiQcLogPtr, "QC.CALIB.DIAM       %f \n", calibrator->calibDiameter);
00993             fprintf (midiQcLogPtr, "QC.CALIB.DDIAM      %f \n", calibrator->calibDiameterErr);
00994             fprintf (midiQcLogPtr, "QC.CALIB.FLAG       %d \n", calibrator->calibFlag);
00995             fprintf (midiQcLogPtr, "QC.CALIB.FLUX       %g \n", calibrator->calibFlux);
00996             fprintf (midiQcLogPtr, "QC.BL.LENGTH  %f \n", calibrator->calibPblAverage);
00997             fprintf (midiQcLogPtr, "QC.BL.ANGLE   %f \n", calibrator->calibParangAverage);
00998 
00999             //    Add binned Transfer Function
01000             fprintf (midiQcLogPtr, "QC.TRF.BINNED1  %0.4f \n", dispResult->trfBinned1);
01001             fprintf (midiQcLogPtr, "QC.TRF.BINNED2  %0.4f \n", dispResult->trfBinned2);
01002             fprintf (midiQcLogPtr, "QC.TRF.BINNED3  %0.4f \n", dispResult->trfBinned3);
01003             fprintf (midiQcLogPtr, "QC.TRF.BINNED4  %0.4f \n", dispResult->trfBinned4);
01004             fprintf (midiQcLogPtr, "QC.TRF.BINNED5  %0.4f \n", dispResult->trfBinned5);
01005 
01006         
01007             for (X = 0; X < sizeInterf->iXWidth; X++)
01008             {
01009                 if (badChannelList[X])
01010                 {
01011                     fprintf (midiQcLogPtr, "QC.T%d              %s \n", X, UNAV);
01012                     fprintf (midiQcLogPtr, "QC.DT%d             %s \n", X, UNAV);
01013                     fprintf (midiQcLogPtr, "QC.COT%d            %s \n", X, UNAV);
01014                     fprintf (midiQcLogPtr, "QC.DCOT%d           %s \n", X, UNAV);
01015                     continue;
01016                 }
01017             
01018                 fprintf (midiQcLogPtr, "QC.T%d              %f \n", X, (dispResult->trf)[X]);
01019                 fprintf (midiQcLogPtr, "QC.DT%d             %f \n", X, (dispResult->trfErr)[X]);
01020                 fprintf (midiQcLogPtr, "QC.COT%d            %f \n", X, 1.0/(dispResult->trf)[X]);
01021                 fprintf (midiQcLogPtr, "QC.DCOT%d           %f \n", X, 1.0/(dispResult->trfErr)[X]);
01022             }
01023         }
01024         else
01025         {
01026             //    Add Applied Transfer Functions
01027             for (X = 0; X < sizeInterf->iXWidth; X++)
01028             {
01029                 if (badChannelList[X])
01030                 {
01031                     fprintf (midiQcLogPtr, "QC.AT%d             %s \n", X, UNAV);
01032                     fprintf (midiQcLogPtr, "QC.DAT%d            %s \n", X, UNAV);
01033                 }
01034                 else
01035                 {
01036                     fprintf (midiQcLogPtr, "QC.AT%d             %f \n", X, (dispResult->trf)[X]);
01037                     fprintf (midiQcLogPtr, "QC.DAT%d            %f \n", X, (dispResult->trfErr)[X]);
01038                 }
01039             }
01040         }
01041     }
01042 
01043     //    Add uncalibrated Normal Visibilities
01044     for (X = 0; X < sizeInterf->iXWidth; X++)
01045     {
01046         if (badChannelList[X])
01047         {
01048             fprintf (midiQcLogPtr, "QC.UNCALV%d         %s \n", X, UNAV);
01049             fprintf (midiQcLogPtr, "QC.DUNCALV%d        %s \n", X, UNAV);
01050         }
01051         else
01052         {
01053             fprintf (midiQcLogPtr, "QC.UNCALV%d         %f \n", X, (dispResult->normVis2)[X]);
01054             fprintf (midiQcLogPtr, "QC.DUNCALV%d        %f \n", X, (dispResult->normVis2Err)[X]);
01055         }
01056     }
01057 
01058     //    Add Photometry parameters
01059     for (X = 0; X < sizeInterf->iXWidth; X++)
01060     {
01061         if (!(badChannelList[X]))
01062         {
01063             photA += (dispResult->photomA)[0][X];
01064             photAErr += (dispResult->photomAErr)[0][X];
01065             photB += (dispResult->photomB)[0][X];
01066             photBErr += (dispResult->photomBErr)[0][X];
01067             photI += (dispResult->photomI)[0][X];
01068             photIErr += (dispResult->photomIErr)[0][X];
01069             photNet2 += (dispResult->photomNet2)[X];
01070             photNet2Err += (dispResult->photomNet2Err)[X];
01071         }
01072     }
01073     if (sizeInterf->numOfChannelsProcessed)
01074     {
01075         photA /= sizeInterf->numOfChannelsProcessed;
01076         photAErr /= sizeInterf->numOfChannelsProcessed;
01077         photB /= sizeInterf->numOfChannelsProcessed;
01078         photBErr /= sizeInterf->numOfChannelsProcessed;
01079         photI /= sizeInterf->numOfChannelsProcessed;
01080         photIErr /= sizeInterf->numOfChannelsProcessed;
01081         photNet2 /= sizeInterf->numOfChannelsProcessed;
01082         photNet2Err /= sizeInterf->numOfChannelsProcessed;
01083     }
01084     fprintf (midiQcLogPtr, "QC.AV.PHOTA         %f \n", photA);
01085     fprintf (midiQcLogPtr, "QC.AV.DPHOTA        %f \n", photAErr);
01086     fprintf (midiQcLogPtr, "QC.AV.PHOTB         %f \n", photB);
01087     fprintf (midiQcLogPtr, "QC.AV.DPHOTB        %f \n", photBErr);
01088     fprintf (midiQcLogPtr, "QC.AV.PHOTI         %f \n", photI);
01089     fprintf (midiQcLogPtr, "QC.AV.DPHOTI        %f \n", photIErr);
01090     fprintf (midiQcLogPtr, "QC.AV.PHOTN         %f \n", photNet2);
01091     fprintf (midiQcLogPtr, "QC.AV.DPHOTN        %f \n", photNet2Err);
01092 
01093     //    Add Calibrated Visibilities
01094     if (dispResult->calibVisExists)
01095     {
01096         for (X = 0; X < sizeInterf->iXWidth; X++)
01097         {
01098             if (badChannelList[X])
01099             {
01100                 fprintf (midiQcLogPtr, "QC.CALV%d           %s \n", X, UNAV);
01101                 fprintf (midiQcLogPtr, "QC.DCALV%d          %s \n", X, UNAV);
01102             }
01103             else
01104             {
01105                 fprintf (midiQcLogPtr, "QC.CALV%d           %f \n", X, (dispResult->calibVis2)[X]);
01106                 fprintf (midiQcLogPtr, "QC.DCALV%d          %f \n", X, (dispResult->calibVis2Err)[X]);
01107             }
01108         }
01109     }
01110 
01111     //    Add Target keywords
01112     fprintf (midiQcLogPtr, "QC.TARG.DIAM        \"UNKNOWN\" \n");
01113     fprintf (midiQcLogPtr, "QC.TARG.DDIAM       \"UNKNOWN\" \n");
01114 
01115     //    Add UVW. The values of uvw will be added during pipeline execution
01116     fprintf (midiQcLogPtr, "QC.BL.UVW1    %f \n", uvw->uCoord);
01117     fprintf (midiQcLogPtr, "QC.BL.UVW2    %f \n", uvw->vCoord);
01118     fprintf (midiQcLogPtr, "QC.BL.UVW3    %f \n", uvw->wCoord);
01119 
01120     //    Add binned photometry measurements
01121     if (strcmp(sizeInterf->beamCombiner, "HIGH_SENS") == 0)
01122     {
01123         fprintf (midiQcLogPtr, "QC.PHOTA.TARG1  %0.2f \n", dispResult->photomATarg1);
01124         fprintf (midiQcLogPtr, "QC.PHOTB.TARG1  %0.2f \n", dispResult->photomBTarg1);
01125 
01126         fprintf (midiQcLogPtr, "QC.PHOTA.TARG2  %0.2f \n", dispResult->photomATarg2);
01127         fprintf (midiQcLogPtr, "QC.PHOTB.TARG2  %0.2f \n", dispResult->photomBTarg2);
01128 
01129         fprintf (midiQcLogPtr, "QC.PHOTA.TARG3  %0.2f \n", dispResult->photomATarg3);
01130         fprintf (midiQcLogPtr, "QC.PHOTB.TARG3  %0.2f \n", dispResult->photomBTarg3);
01131 
01132         fprintf (midiQcLogPtr, "QC.PHOTA.TOTAL1  %0.2f \n", dispResult->photomATotal1);
01133         fprintf (midiQcLogPtr, "QC.PHOTB.TOTAL1  %0.2f \n", dispResult->photomBTotal1);
01134 
01135         fprintf (midiQcLogPtr, "QC.PHOTA.TOTAL2  %0.2f \n", dispResult->photomATotal2);
01136         fprintf (midiQcLogPtr, "QC.PHOTB.TOTAL2  %0.2f \n", dispResult->photomBTotal2);
01137 
01138         fprintf (midiQcLogPtr, "QC.PHOTA.TOTAL3  %0.2f \n", dispResult->photomATotal3);
01139         fprintf (midiQcLogPtr, "QC.PHOTB.TOTAL3  %0.2f \n", dispResult->photomBTotal3);
01140     }
01141 
01142     //    Add binned uncalibrated visibilities
01143     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED1  %0.4f \n", dispResult->uncalVisBinned1);
01144     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED2  %0.4f \n", dispResult->uncalVisBinned2);
01145     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED3  %0.4f \n", dispResult->uncalVisBinned3);
01146     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED4  %0.4f \n", dispResult->uncalVisBinned4);
01147     fprintf (midiQcLogPtr, "QC.UNCAL.BINNED5  %0.4f \n", dispResult->uncalVisBinned5);
01148 
01149     //    Add number of changed target types
01150     fprintf (midiQcLogPtr, "QC.TARTYPE.INTERF.CHANGED   %ld \n", corrFrames->CorrInterf );
01151     fprintf (midiQcLogPtr, "QC.TARTYPE.PHOTOMA.CHANGED  %ld \n", corrFrames->CorrPhotomA );
01152     fprintf (midiQcLogPtr, "QC.TARTYPE.PHOTOMB.CHANGED  %ld \n", corrFrames->CorrPhotomB );
01153 
01154     
01155     //    Release memory
01156     free (messageBuffer);
01157     free (currentTime);
01158     free (textBuff);
01159     free (cleanString);
01160     free (stringTemp);
01161     free (classification);
01162 
01163     return;
01164 }
01165 /*****************************************************************************/
01166 
01167 
01168 
01169 /******************************************************************************
01170 *               European Southern Observatory
01171 *            VLTI MIDI Data Reduction Software
01172 *
01173 * Module name:  loadFrgOutputData
01174 * Input/Output: See function arguments to avoid duplication
01175 * Description:  Loads MIDI product data into appropriate data structures
01176 *
01177 *
01178 * History:
01179 * 11-Feb-05     (csabet) Created
01180 ******************************************************************************/
01181 void loadFrgOutputData (
01182     char            *obsCategory,    // In: Observation category
01183     char            *fileName,        // In: Input file name
01184     float           *waveCal,        // In: array of frequencies
01185     DispersedResult    *dispResult,    // In: Dispersed result
01186     IauExchange        *iauData,        // Ou: IAU Exchange data structure
01187     UVW                *uvw,            // Ou: Coordinates of the uvw
01188     int                *error)
01189 
01190 {
01191     //    Local Declarations
01192     //    ------------------
01193     const char        routine[] = "loadFrgOutputData";
01194     int                i, irec, iwave, itarg, mjdObs, *selection, extNumber;
01195     float            equinox;
01196     char            *stringQfits, *emptyString, *buffer, *observationData, *arrayName,
01197                     *instName, *cleanString;
01198     double            raEp0, decEp0, integrationTime;
01199     qfits_table        *arrayGeometry;
01200 /*     char               *qfitsRow=NULL; */
01201     short            *qfitsRow_int, targetId = 1;
01202     float            *qfitsRow_flt, nullFloat;
01203     double            *qfitsRow_dbl, utcTime;
01204 
01205     cpl_table      * table    =NULL;       
01206     char          ** tel_name =NULL;
01207     char          ** sta_name =NULL;
01208 
01209 
01210 
01211     //    Algorithm
01212     //    ---------
01213     *error = 0;
01214     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01215     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01216 
01217     //    Allocate memory
01218     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01219     emptyString = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01220     buffer = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01221     observationData = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01222     arrayName = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01223     instName = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
01224 
01225     //    Initialize values
01226     strcpy (emptyString, "NULL");
01227     set_fnan (&nullFloat);
01228 
01229     //    Get general parameters from the Header of the input FITS file
01230     //    -------------------------------------------------------------
01231     stringQfits = qfits_query_hdr (fileName, "DATE-OBS");
01232     if (stringQfits == NULL)
01233         sscanf (emptyString, "%s", observationData);
01234     else
01235     {
01236         cleanUpString (stringQfits, cleanString);
01237         sscanf (cleanString, "%s", observationData);
01238     }
01239 
01240     stringQfits = qfits_query_hdr (fileName, "EXPTIME");
01241     if (stringQfits == NULL) integrationTime = nullFloat;
01242     else sscanf (stringQfits, "%lf", &integrationTime);
01243 
01244     extNumber = getFitsExtensionNumber (fileName, "ARRAY_GEOMETRY", error);
01245     stringQfits = qfits_query_ext (fileName, "ARRNAME", extNumber);
01246     if (stringQfits == NULL)
01247         sscanf (emptyString, "%s", arrayName);
01248     else
01249     {
01250         cleanUpString (stringQfits, cleanString);
01251         sscanf (cleanString, "%s", arrayName);
01252     }
01253 
01254     stringQfits = qfits_query_hdr (fileName, "INSTRUME");
01255     if (stringQfits == NULL)
01256         sscanf (emptyString, "%s", instName);
01257     else
01258     {
01259         cleanUpString (stringQfits, cleanString);
01260         sscanf (cleanString, "%s", instName);
01261     }
01262 
01263     stringQfits = qfits_query_hdr (fileName, "RA");
01264     if (stringQfits == NULL) raEp0 = nullFloat;
01265     else sscanf (stringQfits, "%lf", &raEp0);
01266 
01267     stringQfits = qfits_query_hdr (fileName, "DEC");
01268     if (stringQfits == NULL) decEp0 = nullFloat;
01269     else sscanf (stringQfits, "%lf", &decEp0);
01270 
01271     stringQfits = qfits_query_hdr (fileName, "EQUINOX");
01272     if (stringQfits == NULL) equinox = nullFloat;
01273     else sscanf (stringQfits, "%f", &equinox);
01274 
01275     stringQfits = qfits_query_hdr (fileName, "MJD-OBS");
01276     if (stringQfits == NULL) mjdObs = nullFloat;
01277     else sscanf (stringQfits, "%d", &mjdObs);
01278 
01279     stringQfits = qfits_query_hdr (fileName, "UTC");
01280     if (stringQfits == NULL) utcTime = nullFloat;
01281     else sscanf (stringQfits, "%lf", &utcTime);
01282 
01283     //    Load info for OI_ARRAY table
01284     //    ----------------------------
01285     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_ARRAY data ... \n");
01286     if (diagnostic) fprintf (midiReportPtr, "Loading OI_ARRAY data ... \n");
01287 
01288     //    Get the required parameters from the ARRAY_GEOMETRY extension of the input FITS file
01289     sscanf (arrayName, "%s", iauData->array->arrname);
01290 
01291     //    Get extension number
01292     extNumber = getFitsExtensionNumber (fileName, "ARRAY_GEOMETRY", error);
01293     
01294     stringQfits = qfits_query_ext (fileName, "FRAME", extNumber);
01295     if (stringQfits == NULL)
01296         sscanf (emptyString, "%s", iauData->array->frame);
01297     else
01298     {
01299         cleanUpString (stringQfits, cleanString);
01300         sscanf (cleanString, "%s", iauData->array->frame);
01301     }
01302     
01303     stringQfits = qfits_query_ext (fileName, "ARRAYX", extNumber);
01304     if (stringQfits == NULL) iauData->array->arrayx = nullFloat;
01305     else sscanf (stringQfits, "%lf", &(iauData->array->arrayx));
01306 
01307     stringQfits = qfits_query_ext (fileName, "ARRAYY", extNumber);
01308     if (stringQfits == NULL) iauData->array->arrayy = nullFloat;
01309     else sscanf (stringQfits, "%lf", &(iauData->array->arrayy));
01310 
01311     stringQfits = qfits_query_ext (fileName, "ARRAYZ", extNumber);
01312     if (stringQfits == NULL) iauData->array->arrayz = nullFloat;
01313     else sscanf (stringQfits, "%lf", &(iauData->array->arrayz));
01314 
01315     //    Open the table
01316     arrayGeometry = qfits_table_open (fileName, extNumber);
01317     
01318     
01319     table = cpl_table_load(fileName, extNumber, 1);
01320     
01321     if (cpl_table_has_column(table, "TEL_NAME")) tel_name=cpl_table_get_data_string(table, "TEL_NAME");
01322     if (cpl_table_has_column(table, "STA_NAME")) sta_name=cpl_table_get_data_string(table, "STA_NAME");
01323     
01324     
01325 /*     for (i=0; i<cpl_table_get_nrow(table);i++){ */
01326 /*        cpl_msg_info(cpl_func, "tel_name: %s  sta_name: %s",tel_name[i], sta_name[i]); */
01327 /*     } */
01328     
01329 
01330     
01331 
01332 
01333     //    Load info for OI_ARRAY table
01334     //    ----------------------------
01335     selection = (int *) calloc (iauData->array->nelement, sizeof (int));
01336     for (i = 0; i < iauData->array->nelement; i++)
01337     {
01338         selection[i] = 1;
01339         sprintf ((iauData->array->elem)[i].tel_name, "%s", tel_name[i]);        
01340         sprintf ((iauData->array->elem)[i].sta_name, "%s", sta_name[i]);        
01341         if (diagnostic > 2){
01342            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]); 
01343            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]);
01344         }
01345  
01346 
01347 /*        qfitsRow = (char *) (qfits_query_column (arrayGeometry, 0, selection)); */
01348 /*         if (diagnostic > 2)cpl_msg_info(cpl_func,"qfitsRow    = %s \n", qfitsRow); */
01349 /*         removeNewLine (qfitsRow, cleanString); */
01350 /*         if (diagnostic > 2)cpl_msg_info(cpl_func,"cleanString = %s \n", cleanString); */
01351 /*         sprintf ((iauData->array->elem)[i].tel_name, "%s", cleanString); */
01352 /*         free (qfitsRow); */
01353 
01354         
01355 /*         qfitsRow = (char *) (qfits_query_column (arrayGeometry, 1, selection)); */
01356 /*         removeNewLine (qfitsRow, cleanString); */
01357 /*         sprintf ((iauData->array->elem)[i].sta_name, "%s", cleanString); */
01358 /*         free (qfitsRow); */
01359 
01360 
01361         qfitsRow_int = (short *) (qfits_query_column (arrayGeometry, 2, selection));
01362         (iauData->array->elem)[i].sta_index = qfitsRow_int[0];
01363         free (qfitsRow_int);
01364 
01365         qfitsRow_flt = (float *) (qfits_query_column (arrayGeometry, 3, selection));
01366         (iauData->array->elem)[i].diameter = qfitsRow_flt[0];
01367         free (qfitsRow_flt);
01368 
01369         qfitsRow_dbl = (double *) (qfits_query_column (arrayGeometry, 4, selection));
01370         (iauData->array->elem)[i].staxyz[0] = qfitsRow_dbl[0];
01371         (iauData->array->elem)[i].staxyz[1] = qfitsRow_dbl[1];
01372         (iauData->array->elem)[i].staxyz[2] = qfitsRow_dbl[2];
01373         free (qfitsRow_dbl);
01374 
01375         selection[i] = 0;
01376     }
01377 /*     Free table memory */
01378     cpl_table_delete(table);    
01379 
01380     sprintf (iauData->array->revision, "%s", IAUEXCHANGE_VERSION);
01381     free (selection);
01382     qfits_table_close (arrayGeometry);
01383 
01384     //    Load info for OI_TARGET table
01385     //    -----------------------------
01386     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_TARGET data ... \n");
01387     if (diagnostic) fprintf (midiReportPtr, "Loading OI_TARGET data ... \n");
01388 
01389     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TARG NAME");
01390     if (stringQfits == NULL)
01391         sscanf (emptyString, "%s", buffer);
01392     else
01393     {
01394         cleanUpString (stringQfits, cleanString);
01395         sscanf (cleanString, "%s", buffer);
01396     }
01397 
01398     for (itarg = 0; itarg < iauData->targets->ntarget; itarg++)
01399     {
01400         (iauData->targets->targ)[itarg].target_id = targetId;
01401         sscanf (buffer, "%s", (iauData->targets->targ)[itarg].target);
01402         (iauData->targets->targ)[itarg].raep0 = raEp0;
01403         (iauData->targets->targ)[itarg].decep0 = decEp0;
01404         (iauData->targets->targ)[itarg].equinox = equinox;
01405         (iauData->targets->targ)[itarg].ra_err = nullFloat;
01406         (iauData->targets->targ)[itarg].dec_err = nullFloat;
01407         (iauData->targets->targ)[itarg].sysvel = nullFloat;
01408         sscanf (emptyString, "%s", (iauData->targets->targ)[itarg].veltyp);
01409         sscanf (emptyString, "%s", (iauData->targets->targ)[itarg].veldef);
01410         (iauData->targets->targ)[itarg].pmra = nullFloat;
01411         (iauData->targets->targ)[itarg].pmdec = nullFloat;
01412         (iauData->targets->targ)[itarg].pmra_err = nullFloat;
01413         (iauData->targets->targ)[itarg].pmdec_err = nullFloat;
01414         (iauData->targets->targ)[itarg].parallax = nullFloat;
01415         (iauData->targets->targ)[itarg].para_err = nullFloat;
01416         sscanf (emptyString, "%s", (iauData->targets->targ)[itarg].spectyp);
01417     }
01418     sprintf (iauData->targets->revision, "%s", IAUEXCHANGE_VERSION);
01419 
01420 
01421     //    Load info for OI_WAVELENGTH table
01422     //    ---------------------------------
01423     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_WAVELENGTH data ... \n");
01424     if (diagnostic) fprintf (midiReportPtr, "Loading OI_WAVELENGTH data ... \n");
01425 
01426     //    Compute the parameters
01427     sscanf (instName, "%s", iauData->wavelength->insname);
01428     for (i = 0; i < (iauData->wavelength->nwave-1); i++)
01429     {
01430         (iauData->wavelength->eff_wave)[i] = 1.e-6 * waveCal[i];
01431         (iauData->wavelength->eff_band)[i] =  1.e-6 * fabs ((waveCal[1+i] - waveCal[i]));
01432     }
01433     //    Final row
01434     (iauData->wavelength->eff_wave)[iauData->wavelength->nwave-1] = 1.e-6 * 
01435         waveCal[iauData->wavelength->nwave-1];
01436     (iauData->wavelength->eff_band)[iauData->wavelength->nwave-1] =
01437         (iauData->wavelength->eff_band)[iauData->wavelength->nwave-2];
01438     sprintf (iauData->wavelength->revision, "%s", IAUEXCHANGE_VERSION);
01439 
01440     //    Get the uvw parameters
01441     computeUVW ((iauData->array->elem)[0].staxyz[0], (iauData->array->elem)[0].staxyz[1], (iauData->array->elem)[0].staxyz[2], 
01442         (iauData->array->elem)[1].staxyz[0], (iauData->array->elem)[1].staxyz[1], (iauData->array->elem)[1].staxyz[2],
01443         mjdObs, raEp0, decEp0, uvw);
01444 
01445     //    Load info for OI_VIS table
01446     //    --------------------------
01447     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_VIS data ... \n");
01448     if (diagnostic) fprintf (midiReportPtr, "Loading OI_VIS data ... \n");
01449 
01450     //    Get the required parameters from the FITS file
01451     sscanf (observationData, "%s", iauData->vis->date_obs);
01452     sscanf (arrayName, "%s", iauData->vis->arrname);
01453     sscanf (instName, "%s", iauData->vis->insname);
01454     for (irec = 0; irec < iauData->vis->numrec; irec++)
01455     {
01456         (iauData->vis->record)[irec].target_id = targetId;
01457         (iauData->vis->record)[irec].time = utcTime;
01458         (iauData->vis->record)[irec].mjd = mjdObs;
01459         (iauData->vis->record)[irec].int_time = integrationTime;
01460         (iauData->vis->record)[irec].ucoord = uvw->uCoord;
01461         (iauData->vis->record)[irec].vcoord = uvw->vCoord;
01462         (iauData->vis->record)[irec].sta_index[0] = (iauData->array->elem)[0].sta_index;
01463         (iauData->vis->record)[irec].sta_index[1] = (iauData->array->elem)[1].sta_index;
01464 
01465         if ((strcmp (obsCategory, "SCIENCE") == 0) && dispResult->calibVisExists)
01466         {
01467             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01468             {
01469                 if (badChannelList[iwave])
01470                 {
01471                     (iauData->vis->record)[irec].visamp[iwave] = nullFloat;
01472                     (iauData->vis->record)[irec].visamperr[iwave] = nullFloat;
01473                 }
01474                 else
01475                 {
01476                     (iauData->vis->record)[irec].visamp[iwave] = sqrt ((dispResult->calibVis2)[iwave]);
01477                     (iauData->vis->record)[irec].visamperr[iwave] = sqrt ((dispResult->calibVis2Err)[iwave]);
01478                 }                
01479                 (iauData->vis->record)[irec].visphi[iwave] = nullFloat;
01480                 (iauData->vis->record)[irec].visphierr[iwave] = nullFloat;
01481             }
01482         }
01483         else
01484         {
01485             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01486             {
01487                 if (badChannelList[iwave])
01488                 {
01489                     (iauData->vis->record)[irec].visamp[iwave] = nullFloat;
01490                     (iauData->vis->record)[irec].visamperr[iwave] = nullFloat;
01491                 }
01492                 else
01493                 {
01494                     (iauData->vis->record)[irec].visamp[iwave] = sqrt ((dispResult->normVis2)[iwave]);
01495                     (iauData->vis->record)[irec].visamperr[iwave] = sqrt ((dispResult->normVis2Err)[iwave]);
01496                 }                
01497                 (iauData->vis->record)[irec].visphi[iwave] = nullFloat;
01498                 (iauData->vis->record)[irec].visphierr[iwave] = nullFloat;
01499             }
01500         }
01501         sprintf ((iauData->vis->record)[irec].flag, "%s", "FALSE");
01502     }
01503     sprintf (iauData->vis->revision, "%s", IAUEXCHANGE_VERSION);
01504     iauData->vis->nwave = iauData->wavelength->nwave;
01505 
01506 
01507     //    Load info for OI_VIS2 table
01508     //    ---------------------------
01509     if (diagnostic)cpl_msg_info(cpl_func,"Loading OI_VIS2 data ... \n");
01510     if (diagnostic) fprintf (midiReportPtr, "Loading OI_VIS2 data ... \n");
01511 
01512     //    Get the required parameters from the FITS file
01513     sscanf (observationData, "%s", iauData->vis2->date_obs);
01514     sscanf (arrayName, "%s", iauData->vis2->arrname);
01515     sscanf (instName, "%s", iauData->vis2->insname);
01516     for(irec = 0; irec < iauData->vis2->numrec; irec++)
01517     {
01518         (iauData->vis2->record)[irec].target_id = targetId;
01519         (iauData->vis2->record)[irec].time = utcTime;
01520         (iauData->vis2->record)[irec].mjd = mjdObs;
01521         (iauData->vis2->record)[irec].int_time = integrationTime;
01522         (iauData->vis2->record)[irec].ucoord = uvw->uCoord;
01523         (iauData->vis2->record)[irec].vcoord = uvw->vCoord;
01524         (iauData->vis2->record)[irec].sta_index[0] = (iauData->array->elem)[0].sta_index;;
01525         (iauData->vis2->record)[irec].sta_index[1] = (iauData->array->elem)[1].sta_index;;
01526 
01527         if ((strcmp (obsCategory, "SCIENCE") == 0) && dispResult->calibVisExists)
01528         {
01529             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01530             {
01531                 if (badChannelList[iwave])
01532                 {
01533                     (iauData->vis2->record)[irec].vis2data[iwave] = nullFloat;
01534                     (iauData->vis2->record)[irec].vis2err[iwave] = nullFloat;
01535                 }
01536                 else
01537                 {
01538                     (iauData->vis2->record)[irec].vis2data[iwave] = (dispResult->calibVis2)[iwave];
01539                     (iauData->vis2->record)[irec].vis2err[iwave] = (dispResult->calibVis2Err)[iwave];
01540                 }                
01541             }
01542         }
01543         else
01544         {
01545             for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
01546             {
01547                 if (badChannelList[iwave])
01548                 {
01549                     (iauData->vis2->record)[irec].vis2data[iwave] = nullFloat;
01550                     (iauData->vis2->record)[irec].vis2err[iwave] = nullFloat;
01551                 }
01552                 else
01553                 {
01554                     (iauData->vis2->record)[irec].vis2data[iwave] = (dispResult->normVis2)[iwave];
01555                     (iauData->vis2->record)[irec].vis2err[iwave] = (dispResult->normVis2Err)[iwave];
01556                 }
01557             }
01558         }
01559         sprintf ((iauData->vis2->record)[irec].flag, "%s", "FALSE");
01560     }
01561     sprintf (iauData->vis2->revision, "%s", IAUEXCHANGE_VERSION);
01562     iauData->vis2->nwave = iauData->wavelength->nwave;
01563 
01564     //    Release memory
01565     free (instName);
01566     free (arrayName);
01567     free (observationData);
01568     free (emptyString);
01569     free (buffer);
01570     free (cleanString);
01571 
01572     return;
01573 
01574 }
01575 /*****************************************************************************/
01576 
01577 
01578 
01579 
01580 /******************************************************************************
01581 *               European Southern Observatory
01582 *            VLTI MIDI Data Reduction Software
01583 *
01584 * Module name:  writeFrgFitsFile
01585 * Input/Output: See function arguments to avoid duplication
01586 * Description:  Writes and creates appropriate tables into a new fits file in
01587 *               accordance with the IAU format
01588 *
01589 * History:
01590 * 11-Feb-05     (csabet) Created
01591 ******************************************************************************/
01592 void writeFrgFitsFile (
01593     char        *outFitsName,
01594     IauExchange    *iauData,
01595     int            *error)
01596 {
01597     //    Local Declarations
01598     //    ------------------
01599     const char  routine[] = "writeFrgFitsFile";
01600 
01601     //    Algorithm
01602     //    ---------
01603     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01604     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01605 
01606     if (diagnostic)cpl_msg_info(cpl_func,"Writing data into the output FITS file \n\n");
01607     if (diagnostic) fprintf (midiReportPtr, "Writing data into the output FITS file \n\n");
01608 
01609     //    Reset parameters
01610     *error = 0;
01611 
01612     //    Write OI_ARRAY table
01613     writeOiArray (outFitsName, iauData->array, error);
01614     if (*error)
01615     {
01616         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_ARRAY in the output FITS file");
01617         return;
01618     }
01619 
01620     //    Write OI_TARGET table
01621     writeOiTarget (outFitsName, iauData->targets, error);
01622     if (*error)
01623     {
01624         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_TARGET in the output FITS file");
01625         return;
01626     }
01627 
01628     //    Write OI_WAVELENGTH table
01629     writeOiWavelength (outFitsName, iauData->wavelength, error);
01630     if (*error)
01631     {
01632         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_WAVELENGTH in the output FITS file");
01633         return;
01634     }
01635 
01636     //    Write OI_VIS table
01637     writeOiVis (outFitsName, iauData->vis, error);
01638     if (*error)
01639     {
01640         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_VIS in the output FITS file");
01641         return;
01642     }
01643 
01644     //    Write OI_VIS2 table
01645     writeOiVis2 (outFitsName, iauData->vis2, error);
01646     if (*error)
01647     {
01648         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write OI_VIS2 in the output FITS file");
01649         return;
01650     }
01651 
01652     return;
01653 
01654 }
01655 /*****************************************************************************/
01656 

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