procDspTrn.c

00001 
00002 /******************************************************************************
00003 *******************************************************************************
00004 *               European Southern Observatory
00005 *          VLTI MIDI Maintenance Templates Software
00006 *
00007 * Module name:  procDspTrn.c
00008 * Description:  Contains routines for processing the templates
00009 *
00010 * History:      
00011 * 16-Jun-04     (csabet) created
00012 *******************************************************************************
00013 ******************************************************************************/
00014 
00015 /******************************************************************************
00016 *   Compiler directives
00017 ******************************************************************************/
00018 
00019 /******************************************************************************
00020 *   Include files
00021 ******************************************************************************/
00022 #include <math.h>
00023 #include <stdio.h>
00024 #include "midiGlobal.h"
00025 #include "midiLib.h"
00026 #include "imageProcessing.h"
00027 #include "memoryHandling.h"
00028 #include "createProdDspTrn.h"
00029 #include "procDspTrn.h"
00030 #include "errorHandling.h"
00031 #include "midiFitsUtility.h"
00032 #include "fitsAnalysisTec.h"
00033 #include "diagnostics.h"
00034 #include "qfits.h"
00035 #include <cpl.h>
00036 
00037 /**********************************************************
00038 *   Constant definitions
00039 **********************************************************/
00040 
00041 /**********************************************************
00042 *   Global Variables 
00043 **********************************************************/
00044 
00045 /*============================ C O D E    A R E A ===========================*/
00046 
00047 
00048 /******************************************************************************
00049 *               European Southern Observatory
00050 *          VLTI MIDI Maintenance Templates Software
00051 *
00052 * Module name:  procDspTrn
00053 * Input/Output: See function arguments to avoid duplication
00054 * Description:  Measures the transmission characteristics of the dispersive 
00055 *                elements (Prism and Grism) in order to monitor the evolution of 
00056 *                their coatings. This is the main routine for this application.
00057 *
00058 * History:      
00059 * 03-May-05     (csabet) Created
00060 ******************************************************************************/
00061 void procDspTrn (
00062     MidiFiles    *fileNames,    // In: Pointer to file names
00063     int            *error)        // Ou: Error status
00064 {
00065 
00066     //    Local Declarations
00067     //    ------------------
00068     const char      routine[] = "procDspTrn";
00069     ImageFormat        *format=NULL;
00070     DispersiveTrans    *trans=NULL;
00071     int                numOfFiles;
00072     FILE            *signaturePtr=NULL;
00073     
00074     //    Algorithm
00075     //    ---------
00076     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00077     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00078 
00079     //    Write a signature
00080     signaturePtr = fopen ("MIDI_sig_trn.log", "w");
00081     fclose (signaturePtr);
00082 
00083     //    Reset status
00084     *error = 0;
00085     numOfFiles = 0;
00086     
00087     //    Allocate memory
00088     format = callocImageFormat ();
00089     
00090     analyseFitsDspTrn (fileNames, format, &numOfFiles, error);
00091     if (*error)
00092     {
00093         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse DSPTRN");
00094         freeImageFormat (format);
00095         return;
00096     }
00097     
00098     trans = callocDspTrn (numOfFiles);
00099     computeDspTrn (numOfFiles, fileNames, trans, error);
00100     if (*error)
00101     {
00102         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot process DSPTRN");
00103         freeDspTrn (trans);
00104         freeImageFormat (format);
00105         return;
00106     }
00107     
00108     createDspTrnProd (fileNames, format, trans, error);
00109     if (*error)    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create DSPTRN products");
00110 
00111     //    Release memory
00112     freeDspTrn (trans);
00113     freeImageFormat (format);
00114 
00115     return; 
00116 }
00117 
00118 
00119 /*----------------------------------------------------------------------------*/
00131 /*----------------------------------------------------------------------------*/
00132 static cpl_image *
00133 image_filter_median( const cpl_image *image, int xwindow, int ywindow )
00134 {
00135     int nx = cpl_image_get_size_x(image);
00136     int ny = cpl_image_get_size_y(image);
00137     cpl_image *result = cpl_image_new( nx, ny, CPL_TYPE_FLOAT );
00138     int x, y;
00139 
00140     if (result == NULL)
00141     {
00142         return NULL;
00143     }
00144 
00145     for (y = 1; y <= ny; y++)
00146     for (x = 1; x <= nx; x++)
00147     {
00148         double median;
00149         int llx = x - (xwindow - 1) / 2;
00150         int lly = y - (ywindow - 1) / 2;
00151         int urx = x + (xwindow - 1) / 2;
00152         int ury = y + (ywindow - 1) / 2;
00153         if (llx <  1) llx = 1;
00154         if (lly <  1) lly = 1;
00155         if (urx > nx) urx = nx;
00156         if (ury > ny) ury = ny;
00157         median = cpl_image_get_median_window( image,
00158                           llx, lly,
00159                           urx, ury );
00160 
00161         cpl_image_set( result, x, y, median );
00162     }
00163 
00164     return result;
00165 }
00166 
00167 /*****************************************************************************/
00168 
00169 
00170 
00171 /******************************************************************************
00172 *               European Southern Observatory
00173 *          VLTI MIDI Maintenance Templates Software
00174 *
00175 * Module name:  computeDspTrn
00176 * Input/Output: See function arguments to avoid duplication
00177 * Description:  Measures the transmission characteristics of the dispersive 
00178 *                elements (Prism and Grism) in order to monitor the evolution of 
00179 *                their coatings.
00180 *                   Observations are made by looking at a blackscreen (blackbody). 
00181 *                6 measurements are made with one empty file at the end. 
00182 *                Files 1,2,3 are Photometry (Imaging) with filters and files 4,5,6
00183 *                are Spectroscopy (prism or grism) with filters (ArIII, NeII, SIV)
00184 *                The above sequence could be repeated with different exposure time. 
00185 *                Each observation sequence is analysed independently. Saturation and
00186 *                   noise limits need to be verified to guarantee the validity of the results.
00187 *
00188 *                On the imaging observation, the position of the target (x1,y1,dx1,dy1) 
00189 *                   and the photometry (integral number of counts, P1) is measured for
00190 *                each filter. In addition the exposure time (T1), the maximum count 
00191 *                   level (m1) are being recorded. The background is removed from a 
00192 *                region around the star. 
00193 *
00194 *                  On the spectroscopy observations, the position y1 of the spectrum
00195 *                and extension should be the same as in the imaging mode. The background
00196 *                is removed from a region below and above the spectrum. A 1D fit
00197 *                is performed along each column to subtract the background. 
00198 *                  The signal is integrated over the full spectrum, yielding an integral 
00199 *                  number of counts (P2), In addition the exposure time (t2), the maximum 
00200 *                count level (m2) are being recorded. The photometry data are copied
00201 *                from the corresponding intermediate QC product. 
00202 *
00203 *                Summary:
00204 *                1. Find the coordinates of the target (x1, y1, dx1, dy1) 
00205 *                2. Find the coordinates of the background (xb1, yb1, dxb1, dyb1) 
00206 *                3. collapse each frame of files 1 to 6 containing the target with background removed
00207 *                4. The above gives the "count" of sub-window containing the target
00208 *                5. For each file get the integral count given by:
00209 *                    P(file) = count / exposureTime
00210 *                6. Dispersive Transmissions are given by
00211 *                    alpha1 = P4/P1    alpha2 = P5/P2    alpha3 = P6/P3
00212 *
00213 * History:
00214 * 18-Aug-04     (csabet) Created
00215 ******************************************************************************/
00216 void computeDspTrn (
00217     int                numOfFiles,    // In:    Number of data files
00218     MidiFiles        *fileNames,    // In: Pointer to the MIDI file structure
00219     DispersiveTrans    *dspTrn,    // Ou: Pointer to the dispersive transmission data structure
00220     int                *error)        // Ou:    Error status
00221 {
00222   
00223     //    Local Declarations
00224     //    ------------------
00225     const char  routine[] = "computeDspTrn";
00226     char                        *fileTemp, *classification;
00227     FILE                    *inFitsBatchPtr;
00228     ImageFormat             *format;
00229     int                     localError, fileNumber, extNumOfImagingDataFile;
00230     
00231     //    Algorithm
00232     //    ---------
00233     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00234     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00235 
00236     //    Allocate memory
00237     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00238     fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00239     format = callocImageFormat ();
00240 
00241     //    Reset status
00242     *error = 0;
00243     localError = 0;
00244     fileNumber = 0;
00245     dspTrn->exists = 0;
00246     format->hasData = 0;
00247         
00248     //    Open the list of files
00249     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00250     {
00251         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00252             "Cannot open input FITS file list\n                 No data preprocessing has been carried out for this batch");
00253         freeImageFormat (format);
00254         free (fileTemp);
00255         free (classification);
00256         *error = 1;
00257         return;
00258     }
00259 
00260     //    Loop through the files
00261     while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00262     {
00263         sprintf (classification, "%s", "");
00264         sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
00265         if (diagnostic)cpl_msg_info(cpl_func,"\n   Processing file   %s \n", fileNames->inFitsName);
00266         fprintf(midiReportPtr, "\n   Processing file   %s \n", fileNames->inFitsName);
00267 
00268         //    Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
00269         extNumOfImagingDataFile  = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, &localError);
00270         if (localError) 
00271         {
00272             *error = 1;
00273             break;
00274         }
00275 
00276         //    Get Image Format parameters
00277         if (extNumOfImagingDataFile > 0)
00278         {
00279             getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, format, &localError);
00280             if (localError) 
00281             {
00282                 *error = 1;
00283                 break;
00284             }
00285         }
00286         else format->hasData = 0;
00287 
00288         //    Check if the file has data
00289         if (format->hasData)
00290         {
00291             //    Check Categ, Tech and Type and then compute the image size
00292             if ((strcmp (format->obsCatg, "CALIB") == 0) &&
00293                 (strcmp (format->obsTech, "SPECTRUM") == 0) &&
00294                 (strcmp (format->obsType, "WAVE") == 0))
00295             {
00296                 //    Save beam combiner and sutter ID
00297                 sprintf (dspTrn->beamCombiner[fileNumber], "%s", format->beamCombiner);
00298                 sprintf (dspTrn->shutterId[fileNumber], "%s", format->shutterId);
00299                 sprintf (dspTrn->cameraId[fileNumber], "%s", format->cameraId);
00300                 sprintf (dspTrn->grismId[fileNumber], "%s", format->grismId);
00301                 sprintf (dspTrn->filterName[fileNumber], "%s", format->filterName);
00302                 if (diagnostic)cpl_msg_info(cpl_func,"   Beam combiner for file %d      = %s\n", fileNumber+1, dspTrn->beamCombiner[fileNumber]);
00303                 if (diagnostic)cpl_msg_info(cpl_func,"   Shutter ID for file %d         = %s\n", fileNumber+1, dspTrn->shutterId[fileNumber]);
00304                 if (diagnostic)cpl_msg_info(cpl_func,"   Camera ID for file %d          = %s\n", fileNumber+1, dspTrn->cameraId[fileNumber]);
00305                 if (diagnostic)cpl_msg_info(cpl_func,"   Grism ID for file %d           = %s\n", fileNumber+1, dspTrn->grismId[fileNumber]);
00306                 if (diagnostic)cpl_msg_info(cpl_func,"   Filter Name for file %d        = %s\n", fileNumber+1, dspTrn->filterName[fileNumber]);
00307                 fprintf (midiReportPtr, "   Beam combiner for file %d      = %s\n", fileNumber+1, dspTrn->beamCombiner[fileNumber]);
00308                 fprintf (midiReportPtr, "   Shutter ID for file %d         = %s\n", fileNumber+1, dspTrn->shutterId[fileNumber]);
00309                 fprintf (midiReportPtr, "   Camera ID for file %d          = %s\n", fileNumber+1, dspTrn->cameraId[fileNumber]);
00310                 fprintf (midiReportPtr, "   Grism ID for file %d           = %s\n", fileNumber+1, dspTrn->grismId[fileNumber]);
00311                 fprintf (midiReportPtr, "   Filter Name for file %d        = %s\n", fileNumber+1, dspTrn->filterName[fileNumber]);
00312 
00313                 //    Compute Integral Flux
00314                 computeIntegralFlux (fileNumber, fileNames->inFitsName, extNumOfImagingDataFile, format, dspTrn, &localError);
00315                 if (localError) *error = 1;
00316                 
00317                 //    Set the flags
00318                 dspTrn->exists = 1;
00319 
00320                 //    Increment file number but make sure it is not greater than the allocated
00321                 fileNumber++;
00322                 if (fileNumber > numOfFiles)
00323                 {
00324                     *error = 1;
00325                     break;
00326                 }                
00327             }
00328             else
00329                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, "The above file is not suitable for this task");
00330         }
00331         else
00332         {
00333             if (diagnostic)
00334             {
00335                 sprintf (midiMessage, "No data tables in %s. Not processed", fileNames->inFitsName);
00336                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00337             }
00338         }
00339     }
00340 
00341     //    Check if processing has been carried out
00342     if (dspTrn->exists && !(*error) && (fileNumber == numOfFiles))
00343     {
00344        cpl_msg_info(cpl_func,"\nDispersive Transmission Inventry: \n");
00345        cpl_msg_info(cpl_func,"================================ \n");
00346        cpl_msg_info(cpl_func,"   Expected number of data files  = %d\n", numOfFiles);
00347        cpl_msg_info(cpl_func,"   Number of data files processed = %d\n", fileNumber);
00348        cpl_msg_info(cpl_func,"\n");
00349 
00350         fprintf (midiReportPtr, "\nDispersive Transmission Inventry: \n");
00351         fprintf (midiReportPtr, "================================ \n");
00352         fprintf (midiReportPtr, "   Expected number of data files  = %d\n", numOfFiles);
00353         fprintf (midiReportPtr, "   Number of data files processed = %d\n", fileNumber);
00354         fprintf (midiReportPtr, "\n");
00355 
00356         //    Compute Dispersive Transmission
00357         //    -------------------------------
00358         computeDispersivity (dspTrn, &localError);
00359     }
00360     
00361     if (*error || localError)
00362         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot access Dispersive Transmission for this batch");
00363 
00364     
00365     //    Close the file list
00366     fclose (inFitsBatchPtr);
00367     
00368     //    Release memory
00369     freeImageFormat (format);
00370     free (fileTemp);
00371     free (classification);    
00372 
00373     return; 
00374 }
00375 /*****************************************************************************/
00376 
00377 
00378 
00379 /******************************************************************************
00380 *               European Southern Observatory
00381 *          VLTI MIDI Maintenance Templates Software
00382 *
00383 * Module name:  computeIntegralFlux
00384 * Input/Output: See function arguments to avoid duplication
00385 * Description:  Integrates number of counts over the full spectrum. That is obtains
00386 *                one number for each frame and then one number for the file and divides
00387 *                by the exposure time.
00388 *
00389 * History:      
00390 * 18-Aug-04     (csabet) Created
00391 ******************************************************************************/
00392 void computeIntegralFlux ( 
00393     int                fileNumber,        //    In:    File number
00394     char            *fileName,        //  In: Name of file to process
00395     int                extensionNumber,//    In:    Extension number of the IMAGE_DATA
00396     ImageFormat        *format,        //    In:    Pointer to the image format
00397     DispersiveTrans    *dspTrn,        //  Ou: Pointer to the dispersive transmission data structure
00398     int                *error)            //    Ou:    Error status
00399 
00400 {
00401   
00402     //  Local Declarations
00403     //    ------------------
00404     const char  routine[] = "computeIntegralFlux";
00405     qfits_table *pTable  = NULL;
00406     short int   *inData;
00407     char        *tempStr, *string, *title, *dataName;
00408     float         numOfSubInteg, subIntegTime, targetRegionFlux;
00409     int         i, foundData = 0, scalingOffset, indexData, subWindowSize,
00410                 targetPixelCount;
00411     float        *aveImage;
00412     MidiCoords    *target;
00413 
00414     cpl_image *pImage = NULL;    
00415     int nx,ny;
00416     cpl_image  *background = NULL;
00417 /*     cpl_image  *temp       = NULL; */
00418 
00419     
00420     
00421     //  Algorithm
00422     //    ---------
00423     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00424     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00425     
00426     //    Reset status
00427     *error = 0;
00428     subWindowSize = format->iXWidth * format->iYWidth;
00429         
00430     //  Open IMAGING_DATA = INDEX 2
00431     pTable = qfits_table_open (fileName, extensionNumber);
00432     if (!pTable)
00433     {
00434         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load IMAGING_DATA");
00435         *error = 1;
00436         return;
00437     }
00438         
00439     //    Get data table information
00440     for (i = 0; i < pTable->nc; i++)
00441     {
00442         if (strcmp (pTable->col[i].tlabel, "DATA1") == 0)
00443         {
00444             foundData = 1;
00445             indexData = i;
00446         }
00447     }
00448     if (foundData == 0)
00449     {
00450         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find requested columns in data FITS file");
00451         qfits_table_close (pTable);
00452         *error = 1;
00453         return;
00454     }
00455 
00456     //  Read column DATA
00457     inData = (short int*) qfits_query_column (pTable, indexData, NULL); 
00458 
00459     //    Read Scaling Offset
00460     dataName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00461     for (i = 14; i < 25; i++)
00462     {
00463         sprintf (dataName, "TZERO%d", i);
00464         tempStr = qfits_query_ext (fileName, dataName, extensionNumber);
00465         if (tempStr != NULL)
00466         {
00467             if (diagnostic)cpl_msg_info(cpl_func,"Scaling Offset = %s\n", tempStr);
00468             if (diagnostic) fprintf (midiReportPtr, "Scaling Offset = %s\n", tempStr);
00469             sscanf (tempStr, "%d", &scalingOffset);
00470             break;
00471         }
00472     }
00473     if (tempStr == NULL)
00474     {
00475         scalingOffset = 0;
00476         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Scaling Offset. It is set to 0");
00477     }
00478     free (dataName);
00479 
00480     //    Create averaged image for the whole file. This also allows a check on existence of a target
00481     aveImage = (float *) calloc (subWindowSize, sizeof(float));
00482     createAveragedImage (inData, scalingOffset,    format, aveImage);
00483 
00484         /*##############################################################################*/
00485         /* Subtract the background column by column */
00486 
00487         pImage = cpl_image_wrap_float(format->iXWidth, format->iYWidth, aveImage);  
00488 
00489         nx =format->iXWidth;
00490         ny=format->iYWidth;
00491         
00492         background  = image_filter_median( pImage, 1, 2*ny );
00493         cpl_image_subtract ( pImage, background );
00494         
00495         cpl_image_unwrap(pImage);
00496     
00497         cpl_image_delete(background);
00498         /*##############################################################################*/
00499 
00500 
00501 
00502 
00503     //    Create an image FITS file
00504     title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00505     string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00506     sprintf (title, "AveImg%d", fileNumber+1);
00507     sprintf (string, "file %d", fileNumber+1);
00508     createFitsImage (string, title, fileName, format->iXWidth, format->iYWidth, aveImage);
00509     free (string);
00510     free (title);
00511 
00512     //    Create plot file
00513     if (plotFile)
00514     {
00515         title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00516         string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00517         sprintf (string, "3dAveImg%d", fileNumber+1);
00518         sprintf (title, "Averaged Image, filter %s", format->filterName);
00519         midiCreatePlotFile3D (string, title, "X", "Y", "Flux", 
00520             0, aveImage, format->iXWidth, format->iYWidth, "lines", "3");
00521         free (title);
00522         free (string);
00523     }
00524     
00525     //    Get Target coordinates and save the result
00526     target = (MidiCoords *) calloc (1, sizeof (MidiCoords));
00527     getDspTrnTargetCoords (fileNumber+1, dspTrn->grismId[fileNumber], aveImage, format, target, error);
00528     if (*error)
00529     {
00530         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot determine integral flux");
00531         qfits_table_close (pTable);
00532         free (inData);
00533         free (aveImage);
00534         return;
00535     }
00536     dspTrn->target[fileNumber].xCoord = target->xCoord;
00537     dspTrn->target[fileNumber].yCoord = target->yCoord;
00538     dspTrn->target[fileNumber].dxCoord = target->dxCoord;
00539     dspTrn->target[fileNumber].dyCoord = target->dyCoord;
00540 
00541    cpl_msg_info(cpl_func,"\nCoordinates and flux results in file %s: \n", fileName);
00542    cpl_msg_info(cpl_func,"==================================== \n");
00543    cpl_msg_info(cpl_func,"   Target coordinates                       = %f %f %f %f\n", target->xCoord, target->yCoord, 
00544         target->dxCoord, target->dyCoord);
00545     fprintf (midiReportPtr, "\nCoordinates and flux results in file %s: \n", fileName);
00546     fprintf (midiReportPtr, "==================================== \n");
00547     fprintf (midiReportPtr, "   Target coordinates                       = %f %f %f %f\n", target->xCoord, target->yCoord, 
00548         target->dxCoord, target->dyCoord);
00549 
00550     //    Compute total flux in the target region
00551     computeImageFlux (aveImage, format, target, &targetRegionFlux, &targetPixelCount, error);
00552    cpl_msg_info(cpl_func,"   Target flux with background              = %f\n", targetRegionFlux);
00553    cpl_msg_info(cpl_func,"   Target pixel count                       = %d\n", targetPixelCount);
00554     fprintf (midiReportPtr, "   Target flux with background              = %f\n", targetRegionFlux);
00555     fprintf (midiReportPtr, "   Target pixel count                       = %d\n", targetPixelCount);
00556 
00557     //    Compute mean target flux
00558     dspTrn->meanFlux[fileNumber] = targetRegionFlux / targetPixelCount;
00559    cpl_msg_info(cpl_func,"   Target mean flux with background         = %f\n", dspTrn->meanFlux[fileNumber]);
00560     fprintf (midiReportPtr, "   Target mean flux with background         = %f\n", dspTrn->meanFlux[fileNumber]);
00561     
00562     //    Get integration time
00563     tempStr = qfits_query_hdr (fileName, "HIERARCH ESO DET DIT");
00564     if (tempStr != NULL)
00565         sscanf(tempStr, "%f", &subIntegTime);
00566     else
00567     {
00568         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Integration time");
00569         qfits_table_close (pTable);
00570         free (inData);
00571         *error = 1;
00572         free (aveImage);
00573         free (target);
00574         return;
00575     }
00576     tempStr = qfits_query_hdr (fileName, "HIERARCH ESO DET NDIT");
00577     if (tempStr != NULL)
00578         sscanf(tempStr, "%f", &numOfSubInteg);
00579     else
00580     {
00581         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Number of Integrations");
00582         qfits_table_close (pTable);
00583         free (inData);
00584         *error = 1;
00585         free (target);
00586         free (aveImage);
00587         return;
00588     }
00589     //    Compute integral flux
00590     dspTrn->integTime[fileNumber] = numOfSubInteg * subIntegTime;
00591     dspTrn->integFlux[fileNumber] = dspTrn->meanFlux[fileNumber] / dspTrn->integTime[fileNumber];
00592 
00593    cpl_msg_info(cpl_func,"   Integration time                         = %f\n", dspTrn->integTime[fileNumber]);
00594     fprintf (midiReportPtr, "   Integration time                         = %f\n", dspTrn->integTime[fileNumber]);
00595 
00596    cpl_msg_info(cpl_func,"   Total integral flux                      = %f\n", dspTrn->integFlux[fileNumber]);
00597     fprintf (midiReportPtr, "   Total integral flux                      = %f\n", dspTrn->integFlux[fileNumber]);
00598     
00599     //    Release memory
00600     qfits_table_close (pTable);
00601     free (inData);
00602     free (aveImage);
00603     free (target);
00604 
00605     return; 
00606 }
00607 /*****************************************************************************/
00608 
00609 
00610 
00611 /******************************************************************************
00612 *               European Southern Observatory
00613 *          VLTI MIDI Maintenance Templates Software
00614 *
00615 * Module name:  computeDispersivity
00616 * Input/Output: See function arguments to avoid duplication
00617 * Description:  
00618 *
00619 * History:      
00620 * 18-Aug-04     (csabet) Created
00621 ******************************************************************************/
00622 void computeDispersivity (
00623     DispersiveTrans    *dspTrn,    //  Ou: Pointer to the dispersive transmission data structure
00624     int                *error)        //    Ou:    Error status
00625 {
00626   
00627     /*  Local Declarations
00628     --------------------*/
00629     const char  routine[] = "computeDispersivity";
00630     float        *dummy;
00631     
00632     /*  Algorithm
00633     -----------*/
00634     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00635     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00636 
00637     //    Reset status
00638     *error = 0;
00639 
00640     //     Check keywords to make sure that correct parameters are used from files
00641     if ((strcmp (dspTrn->filterName[0], dspTrn->filterName[3]) == 0) &&
00642         (strcmp (dspTrn->grismId[0], "OPEN") == 0) && 
00643         ((strcmp (dspTrn->grismId[3], "PRISM") == 0) || (strcmp (dspTrn->grismId[3], "GRISM") == 0)))
00644     {
00645         dspTrn->transmission[0] = dspTrn->integFlux[3] / dspTrn->integFlux[0];
00646        cpl_msg_info(cpl_func,"   Dispersive Transmission for files 4 / 1 = %f\n", dspTrn->transmission[0]);
00647         fprintf (midiReportPtr, "   Dispersive Transmission for files 4 / 1 = %f\n", dspTrn->transmission[0]);
00648     }
00649     else
00650     {
00651         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Files 1 and 4 are not compatible");
00652         *error = 1;
00653     }
00654         
00655     if ((strcmp (dspTrn->filterName[1], dspTrn->filterName[4]) == 0) &&
00656         (strcmp (dspTrn->grismId[1], "OPEN") == 0) && 
00657         ((strcmp (dspTrn->grismId[4], "PRISM") == 0) || (strcmp (dspTrn->grismId[4], "GRISM") == 0)))
00658     {
00659         dspTrn->transmission[1] = dspTrn->integFlux[4] / dspTrn->integFlux[1];
00660        cpl_msg_info(cpl_func,"   Dispersive Transmission for files 5 / 2 = %f\n", dspTrn->transmission[1]);
00661         fprintf (midiReportPtr, "   Dispersive Transmission for files 5 / 2 = %f\n", dspTrn->transmission[1]);
00662     }
00663     else
00664     {
00665         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Files 2 and 5 are not compatible");
00666         *error = 1;
00667     }
00668 
00669     if ((strcmp (dspTrn->filterName[2], dspTrn->filterName[5]) == 0) &&
00670         (strcmp (dspTrn->grismId[2], "OPEN") == 0) && 
00671         ((strcmp (dspTrn->grismId[5], "PRISM") == 0) || (strcmp (dspTrn->grismId[5], "GRISM") == 0)))
00672     {
00673         dspTrn->transmission[2] = dspTrn->integFlux[5] / dspTrn->integFlux[2];
00674        cpl_msg_info(cpl_func,"   Dispersive Transmission for files 6 / 3 = %f\n", dspTrn->transmission[2]);
00675         fprintf (midiReportPtr, "   Dispersive Transmission for files 6 / 3 = %f\n", dspTrn->transmission[2]);
00676     }
00677     else
00678     {
00679         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Files 3 and 6 are not compatible");
00680         *error = 1;
00681     }
00682 
00683     if (plotFile) 
00684     {
00685         dummy = (float *) calloc (3, sizeof (float));
00686         dummy[0] = 1.0;
00687         dummy[1] = 2.0;
00688         dummy[2] = 3.0;
00689         
00690         midiCreatePlotFile2D2P ("TransmissivityProfile", "Transmissivity Profile", 
00691             "Spectroscopy / Photometry: 4/1, 5/2, 6/3", "Transmissivity", 0, dummy, dspTrn->transmission, 0, 3, 1);
00692         free (dummy);
00693     }
00694     return; 
00695 }
00696 /*****************************************************************************/
00697 
00698 
00699 
00700 /******************************************************************************
00701 *               European Southern Observatory
00702 *          VLTI MIDI Maintenance Templates Software
00703 *
00704 * Module name:  getDspTrnTargetCoords
00705 * Input/Output: See function arguments to avoid duplication
00706 * Description:  This routine uses a 2D Gaussian fit algorithm to search for the target
00707 *                The search area will be confined to a region around the global maximum.
00708 *                In order to distiguish a true global maximum with a bad pixel a filter
00709 *                must first be applied to the entire data set. The Gaussian fit will
00710 *                give the size as well as the coordinates of the target. We choose the
00711 *                size of the background region to be larger than the size of the target 
00712 *                window
00713 *
00714 * History:      
00715 * 18-Aug-04     (csabet) Created
00716 ******************************************************************************/
00717 void getDspTrnTargetCoords (
00718     int            fileNumber,    // In: File number
00719     char        *grismId,    // In: Grism ID indicates Spectroscopy or Imaging
00720     float           *image,        // In: Pointer to the image data
00721     ImageFormat *format,    // In: Pointer to the image format
00722     MidiCoords    *target,    // Ou: Pointer to the target coordinate structures
00723     int            *error)        // Ou: Error status
00724 {
00725   
00726     //  Local Declarations
00727     //    ------------------
00728     const char  routine[] = "getDspTrnTargetCoords";
00729     int            sizeSearch, dimension, xPinhole, yPinhole;
00730     double        xTarget, yTarget, sizeXTarget, sizeYTarget;
00731 
00732     //  Algorithm
00733     //    ---------
00734     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00735     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00736 
00737     //    Reset status
00738     *error = 0;
00739 
00740     //    Set dimensionality for a suitable Gaussian fit
00741     if (strcmp (grismId, "OPEN") == 0) dimension = 2; // Imaging mode
00742     else if ((strcmp (grismId, "GRISM") == 0) || (strcmp (grismId, "PRISM") == 0)) dimension = 1; // Spectroscopy mode
00743     else
00744     {
00745         sprintf (midiMessage, "Unknown Grism ID %s: ", grismId);
00746         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00747         *error = 1;
00748         return;
00749     }
00750 
00751     //    If all is well then use the appropriate parameters to find the coordinates of the target
00752     //    Override dimensionality. CPL Gaussian fit relies on a given search.
00753     dimension = 1;
00754     sizeSearch = SIZE_SEARCH_DSP_TRN;
00755     xPinhole = X_DSP_TRN;
00756     yPinhole = Y_DSP_TRN;
00757     midiGaussianFit (fileNumber, dimension, image, format->iXWidth, format->iYWidth, xPinhole, yPinhole, 
00758         sizeSearch, &xTarget, &yTarget, &sizeXTarget, &sizeYTarget, error);
00759 
00760     //    Save output results
00761     if (*error)    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot determine target coordinates");
00762     else
00763     {
00764            /* Fixed aperture used after background subtraction */
00765            target->xCoord  = xTarget - 7.;
00766            target->yCoord  = yTarget - 7.;
00767            target->dxCoord = xTarget + 7.;
00768            target->dyCoord = yTarget + 7.;
00769 
00770            /*
00771            target->xCoord = xTarget - 0.5 * sizeXTarget;
00772            target->yCoord = yTarget - 0.5 * sizeYTarget;
00773            target->dxCoord = xTarget + 0.5 * sizeXTarget;
00774            target->dyCoord = yTarget + 0.5 * sizeYTarget;
00775            */
00776     }
00777 
00778     return; 
00779 }
00780 //*****************************************************************************
00781 

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