createProdFrgUndisp.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  createProdFrgUndisp.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 "midiFitsUtility.h"
00032 #include "createProdFrgUndisp.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 /*============================ C O D E    A R E A ===========================*/
00063 
00064 
00065 
00066 
00067 /******************************************************************************
00068 *               European Southern Observatory
00069 *            VLTI MIDI Data Reduction Software
00070 *
00071 * Module name:  createFrgProdUndisp
00072 * Input/Output: See function arguments to avoid duplication
00073 * Description:  Creates an output fits file and copies keywords from the
00074 *               input header to the output header
00075 *
00076 *
00077 * History:
00078 * 21-Apr-03     (csabet) Created
00079 ******************************************************************************/
00080 void createFrgProdUndisp (
00081     MidiFiles                *fileNames,
00082     ImageFormat                *format,
00083     FilterData                *filterInfo,
00084     RawVisibility            *rawVis,
00085     TransferFunction        *trfFunction,
00086     CalibratorParam            *calibrator,
00087     CalibratedVisibility    *calibVis,
00088     PhotometryResult        *photom,
00089     int                        *error)
00090 {
00091 
00092     /*  Local Declarations
00093     --------------------*/
00094     const char      routine[] = "createFrgProdUndisp";
00095     int                iwave, numOfTelescopes, extNumber;
00096     char            *stringQfits, *classification, *stringTemp;
00097     FILE            *inFitsBatchPtr = NULL;
00098     IauExchange     *iauData;
00099     UVW             *uvw;
00100     float           *visAmp, *visSqrd;
00101     
00102     /*  Algorithm
00103     -----------*/
00104     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00105     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00106 
00107    cpl_msg_info(cpl_func,"\nCreating Product files for batch  %d \n", batchNumber);
00108    cpl_msg_info(cpl_func,"-------------------------------- \n");
00109     fprintf (midiReportPtr, "\nCreating Product files for batch  %d \n", batchNumber);
00110     fprintf (midiReportPtr, "-------------------------------- \n");
00111 
00112     /*  Reset status */
00113     *error = 0;
00114 
00115     /* Allocate memory */
00116     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00117     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00118 
00119     /*  Open the list of files */
00120     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00121     {
00122         *error = 1;
00123         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00124         free (stringTemp);
00125         free (classification);
00126         return;
00127     }
00128 
00129     /*  Read the name of the first file in the list and get it's full path name */
00130     fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr);
00131     sprintf (classification, "%s", "");
00132     sscanf (stringTemp, "%s%s", fileNames->inFitsName, classification);
00133     fclose (inFitsBatchPtr);
00134 
00135     /* Copy keywords from the raw FITS file into the QC log */
00136     createQcLog (fileNames->inFitsName, error);
00137     if (*error)
00138     {
00139         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot copy keywords to QC log");
00140         free (stringTemp);
00141         free (classification);
00142         return;
00143     }
00144 
00145     /*  Allocate memory for data structures relating to IAU exchange product FITS file
00146     --------------------------------------------------------------------------------*/
00147     /*  Get number of array elements. Number of telescopes */
00148     extNumber = getFitsExtensionNumber (fileNames->inFitsName, "IMAGING_DATA", error);
00149     if (*error)
00150     {
00151         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00152             "Cannot get IMAGING_DATA extension number from input FITS file");
00153         free (stringTemp);
00154         free (classification);
00155         return;
00156     }
00157     stringQfits = qfits_query_ext (fileNames->inFitsName, "MAXTEL", extNumber);
00158     if (stringQfits == NULL)
00159     {
00160         *error = 1;
00161         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read MAXTEL from input FITS file");
00162         free (stringTemp);
00163         free (classification);
00164         return;
00165     }
00166     else sscanf (stringQfits, "%d", &numOfTelescopes);
00167 
00168     /* Allocate memory */
00169     iauData = callocIauExchange (numOfTelescopes, 1);
00170     uvw = (UVW *) calloc (1, sizeof (UVW));
00171     visAmp = (float *) calloc (iauData->wavelength->nwave, sizeof (float));
00172     visSqrd = (float *) calloc (iauData->wavelength->nwave, sizeof (float));
00173 
00174     /* Get default visibilities */
00175     for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
00176     {
00177         visAmp[iwave] = rawVis->vis;
00178         visSqrd[iwave] = rawVis->visSqrd;
00179     }
00180 
00181     /* Overwite if calibrated visibility is available */
00182     if ((strcmp (format->obsCatg, "SCIENCE") == 0) && calibVis->exists)
00183     {
00184         for (iwave = 0; iwave < iauData->wavelength->nwave; iwave++)
00185         {
00186             visAmp[iwave] = calibVis->vis[iwave];
00187             visSqrd[iwave] = calibVis->visSqrd[iwave];
00188         }
00189     }
00190 
00191     /* Load output data into the IAU exchange structures */
00192     loadFrgOutputDataUndisp (fileNames->inFitsName, filterInfo, visAmp, visSqrd, iauData->array, iauData->targets,
00193         iauData->wavelength, iauData->vis, iauData->vis2, uvw, error);
00194     if (*error == 1)
00195     {
00196         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load data structure");
00197         freeIauExchange (iauData);
00198         free (uvw);
00199         free (visAmp);
00200         free (visSqrd);
00201         free (stringTemp);
00202         free (classification);
00203         return;
00204     }
00205 
00206     /* Add product info to QC log */
00207     addProdInfoToFrgQcLogUndisp (format, fileNames, rawVis, calibVis, uvw, photom,
00208         filterInfo, trfFunction, calibrator, error);
00209     if (*error == 1)
00210     {
00211         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot add product info to QC log");
00212         freeIauExchange (iauData);
00213         free (uvw);
00214         free (visAmp);
00215         free (visSqrd);
00216         free (stringTemp);
00217         free (classification);
00218         return;
00219     }
00220 
00221     /*  Create the primary header extension */
00222     createFrgPrimHeadUndisp (fileNames, format, rawVis, calibVis,
00223         uvw, photom, filterInfo, trfFunction, calibrator, error);
00224     if (*error == 1)
00225     {
00226         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create Primary Header extension");
00227         freeIauExchange (iauData);
00228         free (uvw);
00229         free (visAmp);
00230         free (visSqrd);
00231         free (stringTemp);
00232         free (classification);
00233         return;
00234     }
00235 
00236     /*  At this stage output FITS file has probably been created with appropriate
00237     header. Now we create the tables and write the data in accordance with the IAU exchange */
00238     writeFrgFitsFileUndisp (fileNames->outFitsName, iauData->array, iauData->targets, iauData->wavelength,
00239         iauData->vis, iauData->vis2, error);
00240     if (*error == 1)
00241     {
00242         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot write product FITS file");
00243         freeIauExchange (iauData);
00244         free (uvw);
00245         free (visAmp);
00246         free (visSqrd);
00247         free (stringTemp);
00248         free (classification);
00249         return;
00250     }
00251 
00252     if (diagnostic)cpl_msg_info(cpl_func,"Created Output FITS file: %s \n", fileNames->outFitsName);
00253     fprintf (midiReportPtr, "Created Output FITS file: %s \n", fileNames->outFitsName);
00254 
00255     /*  Release memory */
00256     freeIauExchange (iauData);
00257     free (uvw);
00258     free (visAmp);
00259     free (visSqrd);
00260     free (stringTemp);
00261     free (classification);
00262 
00263     return;
00264 }
00265 /*****************************************************************************/
00266 
00267 
00268 
00269 
00270 /******************************************************************************
00271 *               European Southern Observatory
00272 *            VLTI MIDI Data Reduction Software
00273 *
00274 * Module name:  createFrgPrimHeadUndisp
00275 * Input/Output: See function arguments to avoid duplication
00276 * Description:  Creates the primary header
00277 *
00278 *
00279 * History:
00280 * 02-May-03     (csabet) Created
00281 ******************************************************************************/
00282 void createFrgPrimHeadUndisp (
00283     MidiFiles                *fileNames,
00284     ImageFormat                *format,
00285     RawVisibility            *rawVis,
00286     CalibratedVisibility    *calibVis,
00287     UVW                        *uvw,
00288     PhotometryResult        *photom,
00289     FilterData                *filterInfo,
00290     TransferFunction        *trfFunction,
00291     CalibratorParam            *calibrator,
00292     int                        *error)
00293 {
00294 
00295     /*  Local Declarations
00296     --------------------*/
00297     const char      routine[] = "createFrgPrimHeadUndisp";
00298     qfits_header    *outFitsHeader;
00299     FILE            *inFitsBatchPtr, *outFitsPtr;
00300     int             i;
00301     char            *textBuff, *stringQfits, *messageBuffer, *currentTime, *cleanString,
00302                     *stringTemp, *classification;
00303     unsigned int    stringLength;
00304     time_t          now;
00305     struct tm       *newTime;
00306     struct stat     buf;
00307 
00308     /*  Algorithm
00309     -----------*/
00310     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00311     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00312 
00313     *error = 0;
00314 
00315     /*  Allocate memory */
00316     textBuff = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00317     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00318     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00319     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00320     messageBuffer = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00321     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00322 
00323     /*  Get current time/date */
00324     now = time(NULL);
00325     newTime = gmtime (&now);
00326     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00327 
00328     /*  Copy primary header from the raw file to the output header */
00329     outFitsHeader = qfits_header_read (fileNames->inFitsName);
00330     if (outFitsHeader == NULL)
00331     {
00332         *error = 1;
00333         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load header from the input FITS file");
00334         free (messageBuffer);
00335         free (currentTime);
00336         free (cleanString);
00337         free (stringTemp);
00338         free (classification);
00339         free (textBuff);
00340         return;
00341     }
00342 
00343     /*  Now write all the required product keaywords to the output header
00344     -------------------------------------------------------------------*/
00345     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TYPE", format->obsType, "MIDI pipeline product type", "");
00346     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO CATG", "REDUCED_UNDISPERSED", "Pipeline product category", NULL);
00347     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO ARCFILE", fileNames->archFileName, "Arcfile name of first raw file", "");
00348     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO PIPEDATE", currentTime, "Pipeline run date", "");
00349     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO VERSION", MIDI_PIPE_VERSION, "Pipeline version", "");
00350     qfits_header_add (outFitsHeader, "PIPEFILE", fileNames->pipeFileName, "Pipeline product file name", "");
00351     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO DID", MIDI_QC_DIC_VERSION, "QC dictionary version", "");
00352 
00353     /*  Open the list of files */
00354     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00355     {
00356         *error = 1;
00357         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00358         free (messageBuffer);
00359         free (currentTime);
00360         free (cleanString);
00361         free (stringTemp);
00362         free (classification);
00363         free (textBuff);
00364         return;
00365     }
00366 
00367     /* Loop through the list of files and write into the primary header */
00368     i = 1;
00369     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00370     {
00371         sprintf (classification, "%s", "");
00372         sscanf (stringTemp, "%s%s", messageBuffer, classification);
00373 
00374         /* If classification is not given get it from the raw file */
00375         if (strcmp (classification, "") == 0)
00376         {
00377             stringQfits = qfits_query_hdr (messageBuffer, "HIERARCH ESO DPR CATG");
00378             if (stringQfits == NULL)
00379             {
00380                 sprintf (classification, "%s", "UNKNOWN");
00381                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get Observation Category");
00382             }
00383             else
00384             {
00385                 cleanUpString (stringQfits, cleanString);
00386                 sprintf (classification, "%s", cleanString);
00387             }
00388         }
00389         removePathName (messageBuffer, midiReportPtr);
00390         sprintf (textBuff, "HIERARCH ESO PRO REC1 RAW%d NAME", i);
00391         qfits_header_add (outFitsHeader, textBuff, messageBuffer, "FITS file name", "");
00392         sprintf (textBuff, "HIERARCH ESO PRO REC1 RAW%d CATG", i++);
00393         qfits_header_add (outFitsHeader, textBuff, classification, "Observation Categoty", "");
00394     }
00395     fclose (inFitsBatchPtr);
00396 
00397     /*  Write the mask file */
00398     sprintf (messageBuffer, "%s", fileNames->maskFileName);
00399     removePathName (messageBuffer, midiReportPtr);
00400     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO REC2 RAW1 NAME", messageBuffer, "Pipeline Mask File", "");
00401 
00402     /*  Write the transfer function file */
00403     if (strcmp (format->obsCatg, "CALIB") == 0) sprintf (messageBuffer, "%s", fileNames->trfNameWrite);
00404     else sprintf (messageBuffer, "%s", fileNames->trfNameRead);
00405     removePathName (messageBuffer, midiReportPtr);
00406     stringLength = strlen (messageBuffer);
00407     sprintf (textBuff, "r.");
00408     strncat (textBuff, messageBuffer, stringLength-4);
00409     sprintf (messageBuffer, "%s_tpl_0001.dummy", textBuff);
00410     qfits_header_add (outFitsHeader, "HIERARCH ESO PRO REC2 RAW2 NAME", messageBuffer, "Transfer Function File", "");
00411 
00412     /*  Scan parameters */
00413     sprintf (messageBuffer, "%d", format->numOfScansProcessed);
00414     qfits_header_add (outFitsHeader, "HIERARCH ESO QC SCN PROCESSED", messageBuffer, "Number of scans processed", "");
00415     sprintf (messageBuffer, "%d", format->numOfScansRejected);
00416     qfits_header_add (outFitsHeader, "HIERARCH ESO QC SCN REJECTED", messageBuffer, "Number of scans rejected", "");
00417 
00418     /*  Add Filter information */
00419     sprintf (messageBuffer, "%f", filterInfo->optFreqLo);
00420     qfits_header_add (outFitsHeader, "HIERARCH ESO QC WAVE MIN", messageBuffer, "Filter minimum wavelength in micron", "");
00421     sprintf (messageBuffer, "%f", filterInfo->optFreqHi);
00422     qfits_header_add (outFitsHeader, "HIERARCH ESO QC WAVE MAX", messageBuffer, "Filter maximum wavelength in micron", "");
00423 
00424     /*  Transfer functions */
00425     if (trfFunction->exists)
00426     {
00427         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TRF", "AVAILABLE", "Flag for Transfer Function availability", NULL);
00428         if (strcmp (format->obsCatg, "CALIB") == 0)
00429         {
00430             sprintf (messageBuffer, "%f", calibrator->calibVisSqrd);
00431             qfits_header_add (outFitsHeader, "HIERARCH ESO QC THEOVIS", messageBuffer, "Theoretical visibility", "");
00432             sprintf (messageBuffer, "%f", calibrator->calibVisSqrdErr);
00433             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DTHEOVIS", messageBuffer, "(sigma) Error on theoretical visibility", "");
00434 
00435             sprintf (messageBuffer, "%f", trfFunction->trf[3]);
00436             qfits_header_add (outFitsHeader, "HIERARCH ESO QC T0", messageBuffer, "Measured transfer function (ratio of square visibilities)", "");
00437             sprintf (messageBuffer, "%f", trfFunction->trf[4]);
00438             qfits_header_add (outFitsHeader, "HIERARCH ESO QC T1", messageBuffer, "Measured transfer function (ratio of square visibilities)", "");
00439             sprintf (messageBuffer, "%f", trfFunction->trf[5]);
00440             qfits_header_add (outFitsHeader, "HIERARCH ESO QC T2", messageBuffer, "Measured transfer function (ratio of square visibilities)", "");
00441 
00442             sprintf (messageBuffer, "%f", trfFunction->trfErr[3]);
00443             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DT0", messageBuffer, "(sigma) Error on measured transfer function", "");
00444             sprintf (messageBuffer, "%f", trfFunction->trfErr[4]);
00445             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DT1", messageBuffer, "(sigma) Error on measured transfer function", "");
00446             sprintf (messageBuffer, "%f", trfFunction->trfErr[5]);
00447             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DT2", messageBuffer, "(sigma) Error on measured transfer function", "");
00448 
00449             sprintf (messageBuffer, "%f", 1.0/trfFunction->trf[3]);
00450             qfits_header_add (outFitsHeader, "HIERARCH ESO QC COT0", messageBuffer, "Inverse transfer function", "");
00451             sprintf (messageBuffer, "%f", 1.0/trfFunction->trf[4]);
00452             qfits_header_add (outFitsHeader, "HIERARCH ESO QC COT1", messageBuffer, "Inverse transfer function", "");
00453             sprintf (messageBuffer, "%f", 1.0/trfFunction->trf[5]);
00454             qfits_header_add (outFitsHeader, "HIERARCH ESO QC COT2", messageBuffer, "Inverse transfer function", "");
00455 
00456             sprintf (messageBuffer, "%f", 1.0/trfFunction->trfErr[3]);
00457             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DCOT0", messageBuffer, "(sigma) Error on inverse transfer function", "");
00458             sprintf (messageBuffer, "%f", 1.0/trfFunction->trfErr[4]);
00459             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DCOT1", messageBuffer, "(sigma) Error on inverse transfer function", "");
00460             sprintf (messageBuffer, "%f", 1.0/trfFunction->trfErr[5]);
00461             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DCOT2", messageBuffer, "(sigma) Error on inverse transfer function", "");
00462         }
00463         else
00464         {
00465             sprintf (messageBuffer, "%f", trfFunction->trf[3]);
00466             qfits_header_add (outFitsHeader, "HIERARCH ESO QC AT0", messageBuffer, "Applied transfer function", "");
00467             sprintf (messageBuffer, "%f", trfFunction->trf[4]);
00468             qfits_header_add (outFitsHeader, "HIERARCH ESO QC AT1", messageBuffer, "Applied transfer function", "");
00469             sprintf (messageBuffer, "%f", trfFunction->trf[5]);
00470             qfits_header_add (outFitsHeader, "HIERARCH ESO QC AT2", messageBuffer, "Applied transfer function", "");
00471             sprintf (messageBuffer, "%f", trfFunction->trfErr[3]);
00472             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DAT0", messageBuffer, "(sigma) Error on applied transfer function", "");
00473             sprintf (messageBuffer, "%f", trfFunction->trfErr[4]);
00474             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DAT1", messageBuffer, "(sigma) Error on applied transfer function", "");
00475             sprintf (messageBuffer, "%f", trfFunction->trfErr[5]);
00476             qfits_header_add (outFitsHeader, "HIERARCH ESO QC DAT2", messageBuffer, "(sigma) Error on applied transfer function", "");
00477         }
00478     }
00479     else
00480         qfits_header_add (outFitsHeader, "HIERARCH ESO PRO TRF", UNAV, "Flag for Transfer Function availability", NULL);
00481 
00482     /*  Visibilities */
00483     sprintf (messageBuffer, "%f", rawVis->visSqrd);
00484     qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCALV0", messageBuffer, "Uncalibrated square visibility", "");
00485     sprintf (messageBuffer, "%f", rawVis->visSqrd1);
00486     qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCALV1", messageBuffer, "Ucalibrated square visibility", "");
00487     sprintf (messageBuffer, "%f", rawVis->visSqrd2);
00488     qfits_header_add (outFitsHeader, "HIERARCH ESO QC UNCALV2", messageBuffer, "Uncalibrated square visibility", "");
00489     sprintf (messageBuffer, "%f", rawVis->visSqrdErr);
00490     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DUNCALV0", messageBuffer, "Variance of uncalibrated square visibility", "");
00491     sprintf (messageBuffer, "%f", rawVis->visSqrd1Err);
00492     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DUNCALV1", messageBuffer, "Variance of uncalibrated square visibility", "");
00493     sprintf (messageBuffer, "%f", rawVis->visSqrd2Err);
00494     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DUNCALV2", messageBuffer, "Variance of uncalibrated square visibility", "");
00495     if (calibVis->exists)
00496     {
00497         sprintf (messageBuffer, "%f", calibVis->visSqrd[0]);
00498         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALV0", messageBuffer, "Calibrated square visibility", "");
00499         sprintf (messageBuffer, "%f", calibVis->visSqrd[1]);
00500         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALV1", messageBuffer, "Calibrated square visibility", "");
00501         sprintf (messageBuffer, "%f", calibVis->visSqrd[2]);
00502         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALV2", messageBuffer, "Calibrated square visibility", "");
00503         sprintf (messageBuffer, "%f", calibVis->visSqrdErr[0]);
00504         qfits_header_add (outFitsHeader, "HIERARCH ESO QC DCALV0", messageBuffer, "Variance of calibrated square visibility", "");
00505         sprintf (messageBuffer, "%f", calibVis->visSqrdErr[1]);
00506         qfits_header_add (outFitsHeader, "HIERARCH ESO QC DCALV1", messageBuffer, "Variance of calibrated square visibility", "");
00507         sprintf (messageBuffer, "%f", calibVis->visSqrdErr[2]);
00508         qfits_header_add (outFitsHeader, "HIERARCH ESO QC DCALV2", messageBuffer, "Variance of calibrated square visibility", "");
00509     }
00510 
00511     /*  Photometry parameters */
00512     sprintf (messageBuffer, "%f", photom->photomA[0]);
00513     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA0", messageBuffer, "Number of photons", "");
00514     sprintf (messageBuffer, "%f", photom->photomAErr[0]);
00515     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DPHOTA0", messageBuffer, "(sigma) Error in number of photons", "");
00516     sprintf (messageBuffer, "%f", photom->photomA[1]);
00517     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA1", messageBuffer, "Number of photons", "");
00518     sprintf (messageBuffer, "%f", photom->photomAErr[1]);
00519     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DPHOTA1", messageBuffer, "(sigma) Error in number of photons", "");
00520     sprintf (messageBuffer, "%f", photom->photomB[0]);
00521     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB0", messageBuffer, "Number of photons", "");
00522     sprintf (messageBuffer, "%f", photom->photomBErr[0]);
00523     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DPHOTB0", messageBuffer, "(sigma) Error in number of photons", "");
00524     sprintf (messageBuffer, "%f", photom->photomB[1]);
00525     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB1", messageBuffer, "Number of photons", "");
00526     sprintf (messageBuffer, "%f", photom->photomBErr[1]);
00527     qfits_header_add (outFitsHeader, "HIERARCH ESO QC DPHOTB1", messageBuffer, "(sigma) Error in number of photons", "");
00528     sprintf (messageBuffer, "%f", photom->ratioPhotomA0toA1);
00529     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTA01", messageBuffer, "Ratio of number of photons", "");
00530     sprintf (messageBuffer, "%f", photom->ratioPhotomB0toB1);
00531     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PHOTB01", messageBuffer, "Ratio of number of photons", "");
00532     sprintf (messageBuffer, "%f", photom->chop2ChopAErr[0]);
00533     qfits_header_add (outFitsHeader, "HIERARCH ESO QC RMS CHOPA0", messageBuffer, "RMS of chopping cycles", "");
00534     sprintf (messageBuffer, "%f", photom->chop2ChopAErr[1]);
00535     qfits_header_add (outFitsHeader, "HIERARCH ESO QC RMS CHOPA1", messageBuffer, "RMS of chopping cycles", "");
00536     sprintf (messageBuffer, "%f", photom->chop2ChopBErr[0]);
00537     qfits_header_add (outFitsHeader, "HIERARCH ESO QC RMS CHOPB0", messageBuffer, "RMS of chopping cycles", "");
00538     sprintf (messageBuffer, "%f", photom->chop2ChopBErr[1]);
00539     qfits_header_add (outFitsHeader, "HIERARCH ESO QC RMS CHOPB1", messageBuffer, "RMS of chopping cycles", "");
00540 
00541     /*  Add Calibrator keywords */
00542     if (strcmp (format->obsCatg, "CALIB") == 0)
00543     {
00544         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB POS", "UNKNOWN", "Position of the calibrator entry", "");
00545         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB NAME", calibrator->calibName, "Name of the calibrator entry", "");
00546         sprintf (messageBuffer, "%f", calibrator->calibRA);
00547         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB RA", messageBuffer, "Calibrator Right Ascension", "");
00548         sprintf (messageBuffer, "%f", calibrator->calibDEC);
00549         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DEC", messageBuffer, "Calibrator Declination", "");
00550         sprintf (messageBuffer, "%f", RAD_TO_ARCSEC * calibrator->calibDistance);
00551         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DIST", messageBuffer, "Calibrator Radial Distance in arcsec", "");
00552         sprintf (messageBuffer, "%f", calibrator->calibDiameter);
00553         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DIAM", messageBuffer, "Calibrator Diameter in milliarcsec", "");
00554         sprintf (messageBuffer, "%f", calibrator->calibDiameterErr);
00555         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DDIAM", messageBuffer, "(sigma) Error on calibrator Diameter in milliarcsec", "");
00556         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB FLAG", "UNKNOWN", "Calibrator quality flag", "");
00557         sprintf (messageBuffer, "%f", calibrator->calibVisSqrd);
00558         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB VIS", messageBuffer, "Calibrator estimated square visibility", "");
00559         sprintf (messageBuffer, "%f", calibrator->calibVisSqrdErr);
00560         qfits_header_add (outFitsHeader, "HIERARCH ESO QC CALIB DVIS", messageBuffer, "Variance of calibrator estimated square visibility", "");
00561     }
00562 
00563     /*  Add Target keywords */
00564     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARG DIAM", "UNKNOWN", "Estimated diameter of the target (mas, Un.Disk model)", "");
00565     qfits_header_add (outFitsHeader, "HIERARCH ESO QC TARG DDIAM", "UNKNOWN", "(sigma) Error on estimated target diameter in milliarcsec", "");
00566 
00567     /*  Add UVW. The values of uvw will be added during pipeline execution */
00568     sprintf (messageBuffer, "%f", uvw->uCoord);
00569     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW1", messageBuffer, "u component of UVW vector", "");
00570     sprintf (messageBuffer, "%f", uvw->vCoord);
00571     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW2", messageBuffer, "v component of UVW vector", "");
00572     sprintf (messageBuffer, "%f", uvw->wCoord);
00573     qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL UVW3", messageBuffer, "w component of UVW vector", "");
00574 
00575     /*  Baseline parameters */
00576     if (trfFunction->exists)
00577     {
00578         if (strcmp (format->obsCatg, "CALIB") == 0)
00579         {
00580             sprintf (messageBuffer, "%f", calibrator->calibPblAverage);
00581             qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL LENGTH", messageBuffer, "Baseline vector length", "");
00582             sprintf (messageBuffer, "%f", calibrator->calibParangAverage);
00583             qfits_header_add (outFitsHeader, "HIERARCH ESO QC BL ANGLE", messageBuffer, "Baseline vector angle", "");
00584         }
00585     }
00586 
00587     /*  Place Holders */
00588     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH1", "TBD", "TBD", "");
00589     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH2", "TBD", "TBD", "");
00590     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH3", "TBD", "TBD", "");
00591     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH4", "TBD", "TBD", "");
00592     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH5", "TBD", "TBD", "");
00593     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH6", "TBD", "TBD", "");
00594     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH7", "TBD", "TBD", "");
00595     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH8", "TBD", "TBD", "");
00596     qfits_header_add (outFitsHeader, "HIERARCH ESO QC PLACEH9", "TBD", "TBD", "");
00597 
00598     /*  Create the output fits file */
00599     if (stat (fileNames->outFitsName, &buf) == 0)   /*  If the file exist delete it */
00600         remove (fileNames->outFitsName);
00601     outFitsPtr = fopen (fileNames->outFitsName, "w");
00602     if (!outFitsPtr)
00603     {
00604         *error = 1;
00605         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create output FITS file");
00606         free (messageBuffer);
00607         free (currentTime);
00608         free (cleanString);
00609         free (stringTemp);
00610         free (classification);
00611         free (textBuff);
00612         return;
00613     }
00614 
00615     /*  Write header into the output file */
00616     qfits_header_sort (&outFitsHeader);
00617     qfits_header_dump (outFitsHeader, outFitsPtr);
00618     qfits_header_destroy (outFitsHeader);
00619     fclose (outFitsPtr);
00620 
00621     /*  release memory */
00622     free (messageBuffer);
00623     free (currentTime);
00624     free (cleanString);
00625     free (stringTemp);
00626     free (classification);
00627     free (textBuff);
00628 
00629     return;
00630 }
00631 /*****************************************************************************/
00632 
00633 
00634 
00635 
00636 /******************************************************************************
00637 *               European Southern Observatory
00638 *            VLTI MIDI Data Reduction Software
00639 *
00640 * Module name:  addProdInfoToFrgQcLogUndisp
00641 * Input/Output: See function arguments to avoid duplication
00642 * Description:  Adds product information to QC log
00643 *
00644 *
00645 * History:
00646 * 16-Jan-04     (csabet) Created
00647 ******************************************************************************/
00648 void addProdInfoToFrgQcLogUndisp (
00649     ImageFormat                *format,
00650     MidiFiles               *fileNames,
00651     RawVisibility           *rawVis,
00652     CalibratedVisibility    *calibVis,
00653     UVW                        *uvw,
00654     PhotometryResult        *photom,
00655     FilterData                *filterInfo,
00656     TransferFunction        *trfFunction,
00657     CalibratorParam            *calibrator,
00658     int                        *error)
00659 {
00660 
00661     /*  Local Declarations
00662     --------------------*/
00663     const char      routine[] = "addProdInfoToFrgQcLogUndisp";
00664     int             i;
00665     FILE            *inFitsBatchPtr;
00666     char            *stringQfits, *messageBuffer, *currentTime, *textBuff, *cleanString,
00667                     *stringTemp, *classification;
00668     unsigned int    stringLength;
00669     time_t          now;
00670     struct tm       *newTime;
00671 
00672     /*  Algorithm
00673     -----------*/
00674     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00675     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00676 
00677     /* Reset parameters */
00678     *error = 0;
00679 
00680     /*  Allocate memory */
00681     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00682     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00683     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00684     textBuff = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00685     messageBuffer = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00686     currentTime = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00687 
00688     /*  Get current time/date */
00689     now = time(NULL);
00690     newTime = gmtime (&now);
00691     strftime (currentTime, MIN_STRING_LENGTH, "%a %d %b %Y at %H:%M:%S", newTime);
00692 
00693     //    Add keyword to QC log file
00694     fprintf (midiQcLogPtr, "PRO.TYPE            \"%s\" \n", format->obsType);
00695     fprintf (midiQcLogPtr, "PRO.CATG            \"REDUCED_UNDISPERSED\" \n");
00696     fprintf (midiQcLogPtr, "PRO.ARCFILE         \"%s\" \n", fileNames->archFileName);
00697     fprintf (midiQcLogPtr, "PRO.PIPEDATE        \"%s\" \n", currentTime);
00698     fprintf (midiQcLogPtr, "PRO.VERSION         \"%s\" \n", MIDI_PIPE_VERSION);
00699     fprintf (midiQcLogPtr, "PRO.PIPEFILE        \"%s\" \n", fileNames->pipeFileName);
00700     fprintf (midiQcLogPtr, "PRO.DID             \"%s\" \n", MIDI_QC_DIC_VERSION);
00701 
00702     /*  Open the list of files */
00703     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00704     {
00705         *error = 1;
00706         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00707         free (messageBuffer);
00708         free (currentTime);
00709         free (cleanString);
00710         free (stringTemp);
00711         free (classification);
00712         free (textBuff);
00713         return;
00714     }
00715 
00716     /* Loop through the list of files and write into the QC log */
00717     i = 1;
00718     while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00719     {
00720         sprintf (classification, "%s", "");
00721         sscanf (stringTemp, "%s%s", messageBuffer, classification);
00722 
00723         /* If classification is not given get it from the raw file */
00724         if (strcmp (classification, "") == 0)
00725         {
00726             stringQfits = qfits_query_hdr (messageBuffer, "HIERARCH ESO DPR CATG");
00727             if (stringQfits == NULL)
00728             {
00729                 sprintf (classification, "%s", "UNKNOWN");
00730                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get Observation Category");
00731             }
00732             else
00733             {
00734                 cleanUpString (stringQfits, cleanString);
00735                 sprintf (classification, "%s", cleanString);
00736             }
00737         }
00738         removePathName (messageBuffer, midiReportPtr);
00739         fprintf (midiQcLogPtr, "PRO.REC1.RAW%d.NAME  \"%s\" \n", i, messageBuffer);
00740         fprintf (midiQcLogPtr, "PRO.REC1.RAW%d.CATG  \"%s\" \n", i++, classification);
00741     }
00742     fclose (inFitsBatchPtr);
00743 
00744     /* Add name of Mask file to QC log file */
00745     sprintf (messageBuffer, "%s", fileNames->maskFileName);
00746     removePathName (messageBuffer, midiReportPtr);
00747     fprintf (midiQcLogPtr, "PRO.REC2.RAW1.NAME  \"%s\" \n", messageBuffer);
00748 
00749     /* Add name of Transfer Function file to QC log file */
00750     if (strcmp (format->obsCatg, "CALIB") == 0) sprintf (messageBuffer, "%s", fileNames->trfNameWrite);
00751     else sprintf (messageBuffer, "%s", fileNames->trfNameRead);
00752     removePathName (messageBuffer, midiReportPtr);
00753     stringLength = strlen (messageBuffer);
00754     sprintf (textBuff, "r.");
00755     strncat (textBuff, messageBuffer, stringLength-4);
00756     sprintf (messageBuffer, "%s_tpl_0001.dummy", textBuff);
00757     fprintf (midiQcLogPtr, "PRO.REC2.RAW2.NAME  \"%s\" \n", messageBuffer);
00758 
00759     /*  Scan parameters */
00760     fprintf (midiQcLogPtr, "QC.SCN.PROCESSED %d \n", format->numOfScansProcessed);
00761     fprintf (midiQcLogPtr, "QC.SCN.REJECTED  %d \n", format->numOfScansRejected);
00762 
00763     /* Add keyword to QC log file */
00764     fprintf (midiQcLogPtr, "QC.FREQ.MIN         %f \n", filterInfo->optFreqLo);
00765     fprintf (midiQcLogPtr, "QC.FREQ.MAX         %f \n", filterInfo->optFreqHi);
00766 
00767     /*  Transfer functions */
00768     if (trfFunction->exists)
00769     {
00770         if (strcmp (format->obsCatg, "CALIB") == 0)
00771         {
00772             /* Add QC log */
00773             fprintf (midiQcLogPtr, "QC.THEOVIS          %f \n", calibrator->calibVisSqrd);
00774             fprintf (midiQcLogPtr, "QC.DTHEOVIS         %f \n", calibrator->calibVisSqrdErr);
00775             fprintf (midiQcLogPtr, "QC.T0               %f \n", trfFunction->trf[3]);
00776             fprintf (midiQcLogPtr, "QC.T1               %f \n", trfFunction->trf[4]);
00777             fprintf (midiQcLogPtr, "QC.T2               %f \n", trfFunction->trf[5]);
00778             fprintf (midiQcLogPtr, "QC.DT0              %f \n", trfFunction->trfErr[3]);
00779             fprintf (midiQcLogPtr, "QC.DT1              %f \n", trfFunction->trfErr[4]);
00780             fprintf (midiQcLogPtr, "QC.DT2              %f \n", trfFunction->trfErr[5]);
00781             fprintf (midiQcLogPtr, "QC.COT0             %f \n", 1.0/trfFunction->trf[3]);
00782             fprintf (midiQcLogPtr, "QC.COT1             %f \n", 1.0/trfFunction->trf[4]);
00783             fprintf (midiQcLogPtr, "QC.COT2             %f \n", 1.0/trfFunction->trf[5]);
00784             fprintf (midiQcLogPtr, "QC.DCOT0            %f \n", 1.0/trfFunction->trfErr[3]);
00785             fprintf (midiQcLogPtr, "QC.DCOT1            %f \n", 1.0/trfFunction->trfErr[4]);
00786             fprintf (midiQcLogPtr, "QC.DCOT2            %f \n", 1.0/trfFunction->trfErr[5]);
00787         }
00788         else
00789         {
00790             /* Add QC log */
00791             fprintf (midiQcLogPtr, "QC.AT0              %f \n", trfFunction->trf[3]);
00792             fprintf (midiQcLogPtr, "QC.AT1              %f \n", trfFunction->trf[4]);
00793             fprintf (midiQcLogPtr, "QC.AT2              %f \n", trfFunction->trf[5]);
00794             fprintf (midiQcLogPtr, "QC.DAT0             %f \n", trfFunction->trfErr[3]);
00795             fprintf (midiQcLogPtr, "QC.DAT1             %f \n", trfFunction->trfErr[4]);
00796             fprintf (midiQcLogPtr, "QC.DAT2             %f \n", trfFunction->trfErr[5]);
00797         }
00798     }
00799 
00800     /*  Visibilities */
00801     fprintf (midiQcLogPtr, "QC.UNCALV0          %f \n", rawVis->visSqrd);
00802     fprintf (midiQcLogPtr, "QC.UNCALV1          %f \n", rawVis->visSqrd1);
00803     fprintf (midiQcLogPtr, "QC.UNCALV2          %f \n", rawVis->visSqrd2);
00804     fprintf (midiQcLogPtr, "QC.DUNCALV0         %f \n", rawVis->visSqrdErr);
00805     fprintf (midiQcLogPtr, "QC.DUNCALV1         %f \n", rawVis->visSqrd1Err);
00806     fprintf (midiQcLogPtr, "QC.DUNCALV2         %f \n", rawVis->visSqrd2Err);
00807     if (calibVis->exists)
00808     {
00809         fprintf (midiQcLogPtr, "QC.CALV0            %f \n", calibVis->visSqrd[0]);
00810         fprintf (midiQcLogPtr, "QC.CALV1            %f \n", calibVis->visSqrd[1]);
00811         fprintf (midiQcLogPtr, "QC.CALV2            %f \n", calibVis->visSqrd[2]);
00812         fprintf (midiQcLogPtr, "QC.DCALV0           %f \n", calibVis->visSqrdErr[0]);
00813         fprintf (midiQcLogPtr, "QC.DCALV1           %f \n", calibVis->visSqrdErr[1]);
00814         fprintf (midiQcLogPtr, "QC.DCALV2           %f \n", calibVis->visSqrdErr[2]);
00815     }
00816 
00817     /*  Photometry parameters */
00818     fprintf (midiQcLogPtr, "QC.PHOTA0           %f \n", photom->photomA[0]);
00819     fprintf (midiQcLogPtr, "QC.DPHOTA0          %f \n", photom->photomAErr[0]);
00820     fprintf (midiQcLogPtr, "QC.PHOTA1           %f \n", photom->photomA[1]);
00821     fprintf (midiQcLogPtr, "QC.DPHOTA1          %f \n", photom->photomAErr[1]);
00822     fprintf (midiQcLogPtr, "QC.PHOTB0           %f \n", photom->photomB[0]);
00823     fprintf (midiQcLogPtr, "QC.DPHOTB0          %f \n", photom->photomBErr[0]);
00824     fprintf (midiQcLogPtr, "QC.PHOTB1           %f \n", photom->photomB[1]);
00825     fprintf (midiQcLogPtr, "QC.DPHOTB1          %f \n", photom->photomBErr[1]);
00826     fprintf (midiQcLogPtr, "QC.PHOTA01          %f \n", photom->ratioPhotomA0toA1);
00827     fprintf (midiQcLogPtr, "QC.PHOTB01          %f \n", photom->ratioPhotomB0toB1);
00828     fprintf (midiQcLogPtr, "QC.RMS.CHOPA0       %f \n", photom->chop2ChopAErr[0]);
00829     fprintf (midiQcLogPtr, "QC.RMS.CHOPA1       %f \n", photom->chop2ChopAErr[1]);
00830     fprintf (midiQcLogPtr, "QC.RMS.CHOPB0       %f \n", photom->chop2ChopBErr[0]);
00831     fprintf (midiQcLogPtr, "QC.RMS.CHOPB1       %f \n", photom->chop2ChopBErr[1]);
00832 
00833     /*  Add Calibrator keywords */
00834     if (strcmp (format->obsCatg, "CALIB") == 0)
00835     {
00836         fprintf (midiQcLogPtr, "QC.CALIB.POS        \"UNKNOWN\" \n");
00837         fprintf (midiQcLogPtr, "QC.CALIB.NAME       \"%s\" \n", calibrator->calibName);
00838         fprintf (midiQcLogPtr, "QC.CALIB.RA         %f \n", calibrator->calibRA);
00839         fprintf (midiQcLogPtr, "QC.CALIB.DEC        %f \n", calibrator->calibDEC);
00840         fprintf (midiQcLogPtr, "QC.CALIB.DIST       %f \n", RAD_TO_ARCSEC * calibrator->calibDistance);
00841         fprintf (midiQcLogPtr, "QC.CALIB.DIAM       %f \n", calibrator->calibDiameter);
00842         fprintf (midiQcLogPtr, "QC.CALIB.DDIAM      %f \n", calibrator->calibDiameterErr);
00843         fprintf (midiQcLogPtr, "QC.CALIB.FLAG       \"UNKNOWN\" \n");
00844         fprintf (midiQcLogPtr, "QC.CALIB.VIS        %f \n", calibrator->calibVisSqrd);
00845         fprintf (midiQcLogPtr, "QC.CALIB.DVIS       %f \n", calibrator->calibVisSqrdErr);
00846     }
00847 
00848     /*  Add Target keywords */
00849     fprintf (midiQcLogPtr, "QC.TARG.DIAM        \"UNKNOWN\" \n");
00850     fprintf (midiQcLogPtr, "QC.TARG.DDIAM       \"UNKNOWN\" \n");
00851 
00852     /*  Add UVW. The values of uvw will be added during pipeline execution */
00853     fprintf (midiQcLogPtr, "QC.BL.UVW1    %f \n", uvw->uCoord);
00854     fprintf (midiQcLogPtr, "QC.BL.UVW2    %f \n", uvw->vCoord);
00855     fprintf (midiQcLogPtr, "QC.BL.UVW3    %f \n", uvw->wCoord);
00856 
00857     /*  Baseline parameters */
00858     if (trfFunction->exists)
00859     {
00860         if (strcmp (format->obsCatg, "CALIB") == 0)
00861         {
00862             fprintf (midiQcLogPtr, "QC.BL.LENGTH  %f \n", calibrator->calibPblAverage);
00863             fprintf (midiQcLogPtr, "QC.BL.ANGLE   %f \n", calibrator->calibParangAverage);
00864         }
00865     }
00866 
00867     /*  release memory */
00868     free (messageBuffer);
00869     free (currentTime);
00870     free (textBuff);
00871     free (cleanString);
00872     free (stringTemp);
00873     free (classification);
00874 
00875     return;
00876 }
00877 /*****************************************************************************/
00878 
00879 
00880 
00881 /******************************************************************************
00882 *               European Southern Observatory
00883 *            VLTI MIDI Data Reduction Software
00884 *
00885 * Module name:  loadFrgOutputDataUndisp
00886 * Input/Output: See function arguments to avoid duplication
00887 * Description:  Loads MIDI product data into appropriate data structures
00888 *
00889 *
00890 * History:
00891 * 04-Nov-03     (csabet) Created
00892 ******************************************************************************/
00893 void loadFrgOutputDataUndisp (
00894     char            *fileName,
00895     FilterData        *filterInfo,
00896     float            *visAmp,
00897     float            *visSqrd,
00898     OiArray            *array,
00899     OiTarget        *targets,
00900     OiWavelength    *wave,
00901     OiVis            *vis,
00902     OiVis2            *vis2,
00903     UVW                *uvw,    //    Ou: Coordinates of the uvw
00904     int                *error)
00905 {
00906     //    Local Declarations
00907     //    ------------------
00908     const char      routine[] = "loadFrgOutputDataUndisp";
00909     int                i, irec, iwave, itarg, mjdObs, *selection, extNumber;
00910     float           effectiveWave, effectiveBand, equinox;
00911     char            *stringQfits, *emptyString, *buffer, *observationData, *arrayName,
00912                     *instName, *cleanString;
00913     double          raEp0, decEp0, integrationTime;
00914     qfits_table     *arrayGeometry;
00915     char               *qfitsRow;
00916     short           *qfitsRow_int, targetId = 1;
00917     float           *qfitsRow_flt, nullFloat;
00918     double          *qfitsRow_dbl, utcTime;
00919 
00920     //    Algorithm
00921     //    ---------
00922     *error = 0;
00923     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00924     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00925 
00926     //    Allocate memory
00927     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00928     emptyString = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00929     buffer = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00930     observationData = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00931     arrayName = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00932     instName = (char *) calloc (MIN_STRING_LENGTH, sizeof (char));
00933 
00934     //    Initialize values
00935     strcpy (emptyString, "NULL");
00936     set_fnan (&nullFloat);
00937 
00938     //    Get general parameters from the Header of the input FITS file
00939     //    -------------------------------------------------------------
00940     stringQfits = qfits_query_hdr (fileName, "DATE-OBS");
00941     if (stringQfits == NULL)
00942         sscanf (emptyString, "%s", observationData);
00943     else
00944     {
00945         cleanUpString (stringQfits, cleanString);
00946         sscanf (cleanString, "%s", observationData);
00947     }
00948 
00949 
00950     stringQfits = qfits_query_hdr (fileName, "EXPTIME");
00951     if (stringQfits == NULL) integrationTime = nullFloat;
00952     else sscanf (stringQfits, "%lf", &integrationTime);
00953 
00954     extNumber = getFitsExtensionNumber (fileName, "ARRAY_GEOMETRY", error);
00955     if (*error)
00956     {
00957         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00958             "Cannot get ARRAY_GEOMETRY extension number from input FITS file");
00959         free (instName);
00960         free (arrayName);
00961         free (observationData);
00962         free (emptyString);
00963         free (buffer);
00964         free (cleanString);
00965         return;
00966     }
00967     
00968     stringQfits = qfits_query_ext (fileName, "ARRNAME", extNumber);
00969     if (stringQfits == NULL)
00970         sscanf (emptyString, "%s", arrayName);
00971     else
00972     {
00973         cleanUpString (stringQfits, cleanString);
00974         sscanf (cleanString, "%s", arrayName);
00975     }
00976 
00977     stringQfits = qfits_query_hdr (fileName, "INSTRUME");
00978     if (stringQfits == NULL)
00979         sscanf (emptyString, "%s", instName);
00980     else
00981     {
00982         cleanUpString (stringQfits, cleanString);
00983         sscanf (cleanString, "%s", instName);
00984     }
00985 
00986     stringQfits = qfits_query_hdr (fileName, "RA");
00987     if (stringQfits == NULL) raEp0 = nullFloat;
00988     else sscanf (stringQfits, "%lf", &raEp0);
00989 
00990     stringQfits = qfits_query_hdr (fileName, "DEC");
00991     if (stringQfits == NULL) decEp0 = nullFloat;
00992     else sscanf (stringQfits, "%lf", &decEp0);
00993 
00994     stringQfits = qfits_query_hdr (fileName, "EQUINOX");
00995     if (stringQfits == NULL) equinox = nullFloat;
00996     else sscanf (stringQfits, "%f", &equinox);
00997 
00998     stringQfits = qfits_query_hdr (fileName, "MJD-OBS");
00999     if (stringQfits == NULL) mjdObs = nullFloat;
01000     else sscanf (stringQfits, "%d", &mjdObs);
01001 
01002     stringQfits = qfits_query_hdr (fileName, "UTC");
01003     if (stringQfits == NULL) utcTime = nullFloat;
01004     else sscanf (stringQfits, "%lf", &utcTime);
01005 
01006     //    Load info for OI_ARRAY table
01007     //    ----------------------------
01008     if (diagnostic)cpl_msg_info(cpl_func,"Loading oi_array data ... \n");
01009     if (diagnostic) fprintf (midiReportPtr, "Loading oi_array data ... \n");
01010 
01011     //    Get the required parameters from the ARRAY_GEOMETRY extension of the input FITS file
01012     sscanf (arrayName, "%s", array->arrname);
01013 
01014     //    Get extension number
01015     extNumber = getFitsExtensionNumber (fileName, "ARRAY_GEOMETRY", error);
01016     if (*error)
01017     {
01018         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
01019             "Cannot get ARRAY_GEOMETRY extension number from input FITS file");
01020         free (instName);
01021         free (arrayName);
01022         free (observationData);
01023         free (emptyString);
01024         free (buffer);
01025         free (cleanString);
01026         return;
01027     }
01028     
01029     stringQfits = qfits_query_ext (fileName, "FRAME", extNumber);
01030     if (stringQfits == NULL)
01031         sscanf (emptyString, "%s", array->frame);
01032     else
01033     {
01034         cleanUpString (stringQfits, cleanString);
01035         sscanf (cleanString, "%s", array->frame);
01036     }
01037     
01038     stringQfits = qfits_query_ext (fileName, "ARRAYX", extNumber);
01039     if (stringQfits == NULL) array->arrayx = nullFloat;
01040     else sscanf (stringQfits, "%lf", &(array->arrayx));
01041 
01042     stringQfits = qfits_query_ext (fileName, "ARRAYY", extNumber);
01043     if (stringQfits == NULL) array->arrayy = nullFloat;
01044     else sscanf (stringQfits, "%lf", &(array->arrayy));
01045 
01046     stringQfits = qfits_query_ext (fileName, "ARRAYZ", extNumber);
01047     if (stringQfits == NULL) array->arrayz = nullFloat;
01048     else sscanf (stringQfits, "%lf", &(array->arrayz));
01049 
01050     //    Open the table
01051     arrayGeometry = qfits_table_open (fileName, extNumber);
01052 
01053     //    Load info for OI_ARRAY table
01054     //    ----------------------------
01055     selection = (int *) calloc (array->nelement, sizeof (int));
01056     for (i = 0; i < array->nelement; i++)
01057     {
01058         selection[i] = 1;
01059         qfitsRow = (char *) (qfits_query_column (arrayGeometry, 0, selection));
01060         sprintf (array->elem[i].tel_name, "%s", qfitsRow);
01061         free (qfitsRow);
01062 
01063         qfitsRow = (char *) (qfits_query_column (arrayGeometry, 1, selection));
01064         sprintf (array->elem[i].sta_name, "%s", qfitsRow);
01065         free (qfitsRow);
01066 
01067         qfitsRow_int = (short *) (qfits_query_column (arrayGeometry, 2, selection));
01068         array->elem[i].sta_index = qfitsRow_int[0];
01069         free (qfitsRow_int);
01070 
01071         qfitsRow_flt = (float *) (qfits_query_column (arrayGeometry, 3, selection));
01072         array->elem[i].diameter = qfitsRow_flt[0];
01073         free (qfitsRow_flt);
01074 
01075         qfitsRow_dbl = (double *) (qfits_query_column (arrayGeometry, 4, selection));
01076         array->elem[i].staxyz[0] = qfitsRow_dbl[0];
01077         array->elem[i].staxyz[1] = qfitsRow_dbl[1];
01078         array->elem[i].staxyz[2] = qfitsRow_dbl[2];
01079         free (qfitsRow_dbl);
01080 
01081         selection[i] = 0;
01082     }
01083     sprintf (array->revision, "%s", IAUEXCHANGE_VERSION);
01084     free (selection);
01085     qfits_table_close (arrayGeometry);
01086 
01087 
01088     //    Load info for OI_TARGET table
01089     //    -----------------------------
01090     if (diagnostic)cpl_msg_info(cpl_func,"Loading oi_target data ... \n");
01091     if (diagnostic) fprintf (midiReportPtr, "Loading oi_target data ... \n");
01092 
01093     stringQfits = qfits_query_hdr (fileName, "HIERARCH ESO OBS TARG NAME");
01094     if (stringQfits == NULL)
01095         sscanf (emptyString, "%s", buffer);
01096     else
01097     {
01098         cleanUpString (stringQfits, cleanString);
01099         sscanf (cleanString, "%s", buffer);
01100     }
01101 
01102     for (itarg = 0; itarg < targets->ntarget; itarg++)
01103     {
01104         targets->targ[itarg].target_id = targetId;
01105         sscanf (buffer, "%s", targets->targ[itarg].target);
01106         targets->targ[itarg].raep0 = raEp0;
01107         targets->targ[itarg].decep0 = decEp0;
01108         targets->targ[itarg].equinox = equinox;
01109         targets->targ[itarg].ra_err = nullFloat;
01110         targets->targ[itarg].dec_err = nullFloat;
01111         targets->targ[itarg].sysvel = nullFloat;
01112         sscanf (emptyString, "%s", targets->targ[itarg].veltyp);
01113         sscanf (emptyString, "%s", targets->targ[itarg].veldef);
01114         targets->targ[itarg].pmra = nullFloat;
01115         targets->targ[itarg].pmdec = nullFloat;
01116         targets->targ[itarg].pmra_err = nullFloat;
01117         targets->targ[itarg].pmdec_err = nullFloat;
01118         targets->targ[itarg].parallax = nullFloat;
01119         targets->targ[itarg].para_err = nullFloat;
01120         sscanf (emptyString, "%s", targets->targ[itarg].spectyp);
01121     }
01122     sprintf (targets->revision, "%s", IAUEXCHANGE_VERSION);
01123 
01124 
01125     //    Load info for OI_WAVELENGTH table
01126     //    ---------------------------------
01127     if (diagnostic)cpl_msg_info(cpl_func,"Loading oi_wavelength data ... \n");
01128     if (diagnostic) fprintf (midiReportPtr, "Loading oi_wavelength data ... \n");
01129 
01130     //    Compute the parameters
01131     effectiveWave = 1.e-6 * (SPEED_OF_LIGHT /
01132         (0.5 * (filterInfo->optFreqLo + filterInfo->optFreqHi)));
01133     effectiveBand = 1.e-6 * (SPEED_OF_LIGHT /
01134         (filterInfo->optFreqHi - filterInfo->optFreqLo));
01135 
01136     sscanf (instName, "%s", wave->insname);
01137     for (i = 0; i < wave->nwave; i++)
01138     {
01139         wave->eff_wave[i] = effectiveWave;
01140         wave->eff_band[i] = effectiveBand;
01141     }
01142     sprintf (wave->revision, "%s", IAUEXCHANGE_VERSION);
01143 
01144     //    Get the uvw parameters
01145     computeUVW (array->elem[0].staxyz[0], array->elem[0].staxyz[1], array->elem[0].staxyz[2],
01146         array->elem[1].staxyz[0], array->elem[1].staxyz[1], array->elem[1].staxyz[2],
01147         mjdObs, raEp0, decEp0, uvw);
01148 
01149     //    Load info for OI_VIS table
01150     //    --------------------------
01151     if (diagnostic)cpl_msg_info(cpl_func,"Loading oi_vis data ... \n");
01152     if (diagnostic) fprintf (midiReportPtr, "Loading oi_vis data ... \n");
01153 
01154     //    Get the required parameters from the FITS file
01155     sscanf (observationData, "%s", vis->date_obs);
01156     sscanf (arrayName, "%s", vis->arrname);
01157     sscanf (instName, "%s", vis->insname);
01158     for (irec = 0; irec < vis->numrec; irec++)
01159     {
01160         vis->record[irec].target_id = targetId;
01161         vis->record[irec].time = utcTime;
01162         vis->record[irec].mjd = mjdObs;
01163         vis->record[irec].int_time = integrationTime;
01164         vis->record[irec].ucoord = uvw->uCoord;
01165         vis->record[irec].vcoord = uvw->vCoord;
01166         vis->record[irec].sta_index[0] = array->elem[0].sta_index;
01167         vis->record[irec].sta_index[1] = array->elem[1].sta_index;
01168 
01169         for (iwave = 0; iwave < wave->nwave; iwave++)
01170         {
01171             vis->record[irec].visamp[iwave] = visAmp[iwave];
01172             vis->record[irec].visamperr[iwave] = nullFloat;
01173             vis->record[irec].visphi[iwave] = nullFloat;
01174             vis->record[irec].visphierr[iwave] = nullFloat;
01175         }
01176         sprintf (vis->record[irec].flag, "%s", "FALSE");
01177     }
01178     sprintf (vis->revision, "%s", IAUEXCHANGE_VERSION);
01179     vis->nwave = wave->nwave;
01180 
01181 
01182     //    Load info for OI_VIS2 table
01183     //    ---------------------------
01184     if (diagnostic)cpl_msg_info(cpl_func,"Loading oi_vis2 data ... \n");
01185     if (diagnostic) fprintf (midiReportPtr, "Loading oi_vis2 data ... \n");
01186 
01187     //    Get the required parameters from the FITS file
01188     sscanf (observationData, "%s", vis2->date_obs);
01189     sscanf (arrayName, "%s", vis2->arrname);
01190     sscanf (instName, "%s", vis2->insname);
01191     for(irec = 0; irec < vis2->numrec; irec++)
01192     {
01193         vis2->record[irec].target_id = targetId;
01194         vis2->record[irec].time = utcTime;
01195         vis2->record[irec].mjd = mjdObs;
01196         vis2->record[irec].int_time = integrationTime;
01197         vis2->record[irec].ucoord = uvw->uCoord;
01198         vis2->record[irec].vcoord = uvw->vCoord;
01199         vis2->record[irec].sta_index[0] = array->elem[0].sta_index;;
01200         vis2->record[irec].sta_index[1] = array->elem[1].sta_index;;
01201 
01202         for(iwave = 0; iwave < wave->nwave; iwave++)
01203         {
01204             vis2->record[irec].vis2data[iwave] = visSqrd[iwave];
01205             vis2->record[irec].vis2err[iwave] = nullFloat;
01206         }
01207         sprintf (vis2->record[irec].flag, "%s", "FALSE");
01208     }
01209     sprintf (vis2->revision, "%s", IAUEXCHANGE_VERSION);
01210     vis2->nwave = wave->nwave;
01211 
01212 
01213     //    Release memory
01214     free (instName);
01215     free (arrayName);
01216     free (observationData);
01217     free (emptyString);
01218     free (buffer);
01219     free (cleanString);
01220 
01221     return;
01222 
01223 }
01224 /*****************************************************************************/
01225 
01226 
01227 
01228 /******************************************************************************
01229 *               European Southern Observatory
01230 *            VLTI MIDI Data Reduction Software
01231 *
01232 * Module name:  writeFrgFitsFileUndisp
01233 * Input/Output: See function arguments to avoid duplication
01234 * Description:  Writes and creates appropriate tables into a new fits file in
01235 *               accordance with the IAU format
01236 *
01237 * History:
01238 * 02-May-03     (csabet) Created
01239 ******************************************************************************/
01240 void writeFrgFitsFileUndisp (
01241     char            *outFitsName,
01242     OiArray            *array,
01243     OiTarget        *targets,
01244     OiWavelength    *wavelength,
01245     OiVis            *vis,
01246     OiVis2            *vis2,
01247     int                *error)
01248 {
01249     /*  Local Declarations
01250     --------------------*/
01251     const char  routine[] = "writeFrgFitsFileUndisp";
01252 
01253     /*  Algorithm
01254     -----------*/
01255     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01256     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01257 
01258     if (diagnostic)cpl_msg_info(cpl_func,"Writing data into the output FITS file \n\n");
01259     if (diagnostic) fprintf (midiReportPtr, "Writing data into the output FITS file \n\n");
01260 
01261     /*  Reset parameters */
01262     *error = 0;
01263 
01264     /*  Write OiArray table */
01265     writeOiArray (outFitsName, array, error);
01266     if (*error)
01267     {
01268         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
01269             "Cannot write OI_ARRAY in the output FITS file");
01270         return;
01271     }
01272 
01273     /*  Write OiTarget table */
01274     writeOiTarget (outFitsName, targets, error);
01275     if (*error)
01276     {
01277         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
01278             "Cannot write OI_TARGET in the output FITS file");
01279         return;
01280     }
01281 
01282     /*  Write OiWavelength table */
01283     writeOiWavelength (outFitsName, wavelength, error);
01284     if (*error)
01285     {
01286         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
01287             "Cannot write OI_WAVELENGTH in the output FITS file");
01288         return;
01289     }
01290 
01291     /*  Write OiVis table */
01292     writeOiVis (outFitsName, vis, error);
01293     if (*error)
01294     {
01295         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
01296             "Cannot write OI_VIS in the output FITS file");
01297         return;
01298     }
01299 
01300     /*  Write OiVis2 table */
01301     writeOiVis2 (outFitsName, vis2, error);
01302     if (*error)
01303     {
01304         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
01305             "Cannot write OI_VIS2 in the output FITS file");
01306         return;
01307     }
01308 
01309     return;
01310 
01311 }
01312 /*****************************************************************************/
01313 

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