procDetLin.c

00001 
00002 /******************************************************************************
00003 *******************************************************************************
00004 *               European Southern Observatory
00005 *          VLTI MIDI Maintenance Templates Software
00006 *
00007 * Module name:  procDetLin.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 <cpl.h>
00025 #include "midiGlobal.h"
00026 #include "midiLib.h"
00027 #include "imageProcessing.h"
00028 #include "memoryHandling.h"
00029 #include "errorHandling.h"
00030 #include "createProdDetLin.h"
00031 #include "midiFitsUtility.h"
00032 #include "fitsAnalysisTec.h"
00033 #include "procDetLin.h"
00034 #include "statistics.h"
00035 #include "diagnostics.h"
00036 #include "cpl_polynomial.h"
00037 #include "qfits.h"
00038 #include "midi_cplutils.h"
00039 #include "midi_cplupgrade.h"
00040 
00041 /**********************************************************
00042 *   Constant definitions
00043 **********************************************************/
00044 
00045 /**********************************************************
00046 *   Global Variables 
00047 **********************************************************/
00048 
00049 /*============================ C O D E    A R E A ===========================*/
00050 
00051 
00052 
00053 /******************************************************************************
00054 *               European Southern Observatory
00055 *          VLTI MIDI Maintenance Templates Software
00056 *
00057 * Module name:  procDetLin
00058 * Input/Output: See function arguments to avoid duplication
00059 * Description:  Computes the detector linearity. This is the main routine for this
00060 *                application.
00061 *
00062 * History:      
00063 * 03-May-05     (csabet) Created
00064 * 09-May-06     (csabet) Modified to characterise each pixel
00065 ******************************************************************************/
00066 void procDetLin (
00067     MidiFiles    *fileNames,    // In: Pointer to file names
00068     int            *error)        // Ou: Error status
00069 {
00070 
00071     //    Local Declarations
00072     //    ------------------
00073     const char      routine[] = "procDetLin";
00074     ImageFormat        *format=NULL;
00075     DetLinearity     *linearity=NULL;
00076     int                numOfFiles;
00077     FILE            *signaturePtr=NULL;
00078     
00079     //    Algorithm
00080     //    ---------
00081     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00082     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00083 
00084     //    Write a signature
00085     signaturePtr = fopen ("MIDI_sig_lin.log", "w");
00086     fclose (signaturePtr);
00087 
00088     //    Reset status
00089     *error = 0;
00090     numOfFiles = 0;
00091     
00092     //    Allocate memory
00093     format = callocImageFormat ();
00094     
00095     analyseFitsDetLin (fileNames, format, &numOfFiles, error);
00096     if (*error)
00097     {
00098         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse DETLIN");
00099         freeImageFormat (format);
00100         return;
00101     }
00102     
00103     linearity = callocDetLin (numOfFiles, format);
00104     computeDetLin (numOfFiles, fileNames, format, linearity, error);
00105     if (*error)
00106     {
00107         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot process DETLIN");
00108         freeDetLin (linearity);
00109         freeImageFormat (format);
00110         return;
00111     }
00112     
00113     createDetLinProd (fileNames, format, numOfFiles, linearity, error);
00114     if (*error)    midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create DETLIN products");
00115 
00116     //    Release memory
00117     freeDetLin (linearity);
00118     freeImageFormat (format);
00119 
00120     return; 
00121 }
00122 /*****************************************************************************/
00123 
00124 
00125 /******************************************************************************
00126 *               European Southern Observatory
00127 *          VLTI MIDI Maintenance Templates Software
00128 *
00129 * Module name:  computeDetLin
00130 * Input/Output: See function arguments to avoid duplication
00131 * Description:  Computes the detector linearity. 
00132 *                Compute average flux for each pixel in a given file. Exclude saturated pixels.
00133 *                Find coefficients of a polynomial describing the linearity over the integration 
00134 *                time.
00135 *
00136 * History:      
00137 * 15-Jun-04     (csabet) Created
00138 * 09-May-06     (csabet) Modified to characterise each pixel
00139 ******************************************************************************/
00140 void computeDetLin (
00141     int                numOfFiles,    // In: Number of data files
00142     MidiFiles        *fileNames,    // In: Pointer to the MIDI file structure
00143     ImageFormat        *format,    // In: Data format
00144     DetLinearity     *linearity,    // Ou: Pointer to the detector linearity data structure
00145     int                *error)        // Ou: Error status
00146 {
00147 
00148     //    Local Declarations
00149     //    ------------------
00150     const char  routine[] = "computeDetLin";
00151     char        *fileTemp, *classification, *firstFitsFile;
00152     FILE        *inFitsBatchPtr;
00153     ImageFormat    *formatLocal;
00154     int            localError, fileNumber, extNumOfImagingDataFile;
00155     
00156     //    Algorithm
00157     //    ---------
00158     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00159     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00160 
00161     //    Allocate memory
00162     classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00163     firstFitsFile = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00164     fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00165     formatLocal = callocImageFormat ();
00166 
00167     //    Reset status
00168     *error = 0;
00169     localError = 0;
00170     fileNumber = 0;
00171     linearity->exists = 0;
00172     formatLocal->hasData = 0;
00173             
00174     //    Open the list of files
00175     if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00176     {
00177         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, 
00178             "Cannot open input FITS file list\n                 No compression has been carried out for this batch");
00179         freeImageFormat (formatLocal);
00180         free (fileTemp);
00181         free (classification);
00182         *error = 1;
00183         return;
00184     }
00185 
00186     //    Loop through the files and analyse
00187     while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00188     {
00189         sprintf (classification, "%s", "");
00190         sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
00191         if (diagnostic)cpl_msg_info(cpl_func,"\n   Processing file   %s \n", fileNames->inFitsName);
00192         fprintf(midiReportPtr, "\n   Processing file   %s \n", fileNames->inFitsName);
00193 
00194         //    Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
00195         extNumOfImagingDataFile  = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, &localError);
00196         if (localError) 
00197         {
00198             *error = 1;
00199             break;
00200         }
00201 
00202         //    Get Image Format parameters
00203         if (extNumOfImagingDataFile > 0)
00204         {
00205             getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, formatLocal, &localError);
00206             if (localError) 
00207             {
00208                 *error = 1;
00209                 break;
00210             }
00211         }
00212         else formatLocal->hasData = 0;
00213 
00214         //    Check if the file has data
00215         if (formatLocal->hasData)
00216         {
00217             //    Check Categ, Tech and Type and then compute the image size
00218             if ((strcmp (formatLocal->obsCatg, "CALIB") == 0) &&
00219                 ((strcmp (formatLocal->obsTech, "IMAGE") == 0) || 
00220                 (strcmp (formatLocal->obsTech, "SPECTRUM") == 0)) &&
00221                 (strcmp (formatLocal->obsType, "FLAT") == 0))
00222             {
00223                 //    Save grism ID
00224                 sprintf (linearity->grismId, "%s", formatLocal->grismId);
00225                 if (diagnostic)cpl_msg_info(cpl_func,"   Grism ID for file %d           = %s\n", fileNumber+1, formatLocal->grismId);
00226                 fprintf (midiReportPtr, "   Grism ID for file %d           = %s\n", fileNumber+1, formatLocal->grismId);
00227                 
00228                 //    Create and display averaged images
00229                 createAndDisplayImageDetLin (fileNumber, fileNames->inFitsName, extNumOfImagingDataFile, 
00230                     format, linearity, &localError);
00231                 if (localError) *error = 1;
00232                     
00233                 //    Calculate mean of counts. Ignore this file if pixels are saturated
00234                 getIntegrationTime (fileNumber, fileNames->inFitsName, linearity, &localError);
00235                 if (localError) *error = 1;
00236                         
00237                 //    Set the flags
00238                 linearity->exists = 1;
00239 
00240                 //    Increment file number but make sure it is not greater than the allocated
00241                 fileNumber++;
00242                 if (fileNumber > numOfFiles)
00243                 {
00244                     *error = 1;
00245                     break;
00246                 }
00247                 
00248                 //    Save name of the first FITS file
00249                 if (fileNumber == 1) sprintf (firstFitsFile, "%s", fileNames->inFitsName);
00250             }
00251             else
00252                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, "The above file is not suitable for this task");
00253         }
00254         else
00255         {
00256             if (diagnostic)
00257             {
00258                 sprintf (midiMessage, "No data tables in %s. Not processed", fileNames->inFitsName);
00259                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00260             }
00261         }
00262     }
00263 
00264     
00265     //    Check if processing has been carried out
00266     if ((linearity->exists) && !(*error) && (fileNumber == numOfFiles))
00267     {
00268        cpl_msg_info(cpl_func,"\nDetector Linearity Inventry: \n");
00269        cpl_msg_info(cpl_func,"=========================== \n");
00270        cpl_msg_info(cpl_func,"   Expected number of data files  = %d\n", numOfFiles);
00271        cpl_msg_info(cpl_func,"   Number of data files processed = %d\n", fileNumber);
00272        cpl_msg_info(cpl_func,"\n");
00273 
00274         fprintf (midiReportPtr, "\nDetector Linearity Inventry: \n");
00275         fprintf (midiReportPtr, "=========================== \n");
00276         fprintf (midiReportPtr, "   Expected number of data files  = %d\n", numOfFiles);
00277         fprintf (midiReportPtr, "   Number of data files processed = %d\n", fileNumber);
00278         fprintf (midiReportPtr, "\n");
00279 
00280         //    Assess linerity
00281         //    ---------------
00282         assessLinearity (format, firstFitsFile, linearity, error);
00283     }
00284 
00285     if (*error || localError)
00286         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot assess Detector Linearity");
00287 
00288     //    Close the file list
00289     fclose (inFitsBatchPtr);
00290     
00291     //    Release memory
00292     freeImageFormat (formatLocal);
00293     free (fileTemp);
00294     free (classification);
00295     free (firstFitsFile);
00296 
00297     return; 
00298 }
00299 /*****************************************************************************/
00300 
00301 
00302 /******************************************************************************
00303 *               European Southern Observatory
00304 *          VLTI MIDI Maintenance Templates Software
00305 *
00306 * Module name:  createAndDisplayImageDetLin
00307 * Input/Output: See function arguments to avoid duplication
00308 * Description:  Creates averaged images for display and later processing
00309 *                purposes.
00310 *
00311 * History:      
00312 * 08-May-06     (csabet) Created
00313 ******************************************************************************/
00314 void createAndDisplayImageDetLin ( 
00315     int                fileNumber,            // In: File number
00316     char            *fileName,            // In: Name of file to process
00317     int                extensionNumber,    // In: Extension number of the IMAGE_DATA
00318     ImageFormat        *format,            // In: Pointer to the image format
00319     DetLinearity     *linearity,            // Ou: Pointer to the detector linearity data structure
00320     int                *error)                // Ou: Error status
00321 
00322 {
00323   
00324     //    Local Declarations
00325     //    ------------------
00326     const char  routine[] = "createAndDisplayImageDetLin";
00327     qfits_table *pTable  = NULL;
00328     short int   *inData;
00329     char        *tempStr, *string, *title, *dataName;
00330     int         i, foundData = 0, scalingOffset, 
00331                 indexData;
00332        
00333     //    Algorithm
00334     //    ---------
00335     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00336     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00337     
00338     //    Reset status
00339     *error = 0;
00340     linearity->saturated[fileNumber] = 0;
00341     
00342     //    Open IMAGING_DATA = INDEX 2
00343     pTable = qfits_table_open (fileName, extensionNumber);
00344     if (!pTable)
00345     {
00346         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load IMAGING_DATA");
00347         *error = 1;
00348         return;
00349     }
00350         
00351     //    Get data table information
00352     for (i = 0; i < pTable->nc; i++)
00353     {
00354         if (strcmp (pTable->col[i].tlabel, "DATA1") == 0)
00355         {
00356             foundData = 1;
00357             indexData = i;
00358         }
00359     }
00360     if (foundData == 0)
00361     {
00362         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find requested columns in data FITS file");
00363         qfits_table_close (pTable);
00364         *error = 1;
00365         return;
00366     }
00367 
00368     //    Read column DATA
00369     inData = (short int*) qfits_query_column (pTable, indexData, NULL); 
00370 
00371     //    Get the scaling offset
00372     dataName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00373     for (i = 14; i < 25; i++)
00374     {
00375         sprintf (dataName, "TZERO%d", i);
00376         tempStr = qfits_query_ext (fileName, dataName, extensionNumber);
00377         if (tempStr != NULL)
00378         {
00379             if (diagnostic)cpl_msg_info(cpl_func,"Scaling Offset = %s\n", tempStr);
00380             if (diagnostic) fprintf (midiReportPtr, "Scaling Offset = %s\n", tempStr);
00381             sscanf (tempStr, "%d", &scalingOffset);
00382             break;
00383         }
00384     }
00385     if (tempStr == NULL)
00386     {
00387         scalingOffset = 0;
00388         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Scaling Offset. It is set to 0");
00389     }
00390     free (dataName);
00391     
00392     //    Create averaged image for the whole file. This also allows a check on existence of a target
00393     createAveragedImage (inData, scalingOffset,    format, linearity->aveImage[fileNumber]);
00394 
00395     //    Create an image FITS file
00396     title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00397     string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00398     sprintf (title, "AveImg%d", fileNumber+1);
00399     sprintf (string, "file %d", fileNumber+1);
00400     createFitsImage (string, title, fileName, format->iXWidth, format->iYWidth, linearity->aveImage[fileNumber]);
00401 
00402     //    Create plot file
00403     if (plotFile)
00404     {
00405         sprintf (string, "3dAveImg%d", fileNumber+1);
00406         sprintf (title, "Averaged Image, file %d", fileNumber+1);
00407         midiCreatePlotFile3D (string, title, "X", "Y", "Flux", 
00408             0, linearity->aveImage[fileNumber], format->iXWidth, format->iYWidth, "lines", "3");
00409     }
00410 
00411     //    Create an averaged image. This time excluding the saturated pixels
00412     checkSaturationDetLin (inData, scalingOffset, format, linearity->aveImage[fileNumber], 
00413         &(linearity->saturated[fileNumber]));
00414 
00415     //    Create plot file excluding saturated pixels
00416     if (diagnostic > 2)
00417     {
00418         sprintf (string, "3dAveImgClean%d", fileNumber+1);
00419         sprintf (title, "Averaged Image. Excluding saturated pixels, file %d", fileNumber+1);
00420         if (plotFile) midiCreatePlotFile3D (string, title, "X", "Y", "Flux", 
00421             0, linearity->aveImage[fileNumber], format->iXWidth, format->iYWidth, "lines", "3");
00422     }
00423     free (title);
00424     free (string);
00425     
00426     //    Release memory
00427     qfits_table_close (pTable);
00428     free (inData);
00429 
00430     return; 
00431 }
00432 /*****************************************************************************/
00433 
00434 
00435 /******************************************************************************
00436 *               European Southern Observatory
00437 *          VLTI MIDI Maintenance Templates Software
00438 *
00439 * Module name:  getIntegrationTime
00440 * Input/Output: See function arguments to avoid duplication
00441 * Description:  Retrieves Integration time as well as the Number of Integrations.
00442 *
00443 * History:      
00444 * 09-May-06     (csabet) Created
00445 ******************************************************************************/
00446 void getIntegrationTime ( 
00447     int                fileNumber,    // In: File number
00448     char            *fileName,    // In: Name of file to process
00449     DetLinearity    *linearity,    // Ou: Pointer to the detector linearity data structure
00450     int                *error)        // Ou: Error status
00451 
00452 {
00453  
00454     //    Local Declarations
00455     //    ------------------
00456     const char  routine[] = "getIntegrationTime";
00457     char        *tempStr;
00458     float         numOfSubInteg, subIntegTime;
00459          
00460     //    Algorithm
00461     //    ---------
00462     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00463     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00464     
00465     //    Reset status
00466     *error = 0;
00467 
00468     //    Get integration time
00469     tempStr = qfits_query_hdr (fileName, "HIERARCH ESO DET DIT");
00470     if (tempStr != NULL)
00471         sscanf(tempStr, "%f", &subIntegTime);
00472     else
00473     {
00474         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Integration time");
00475         *error = 1;
00476         return;
00477     }
00478     tempStr = qfits_query_hdr (fileName, "HIERARCH ESO DET NDIT");
00479     if (tempStr != NULL)
00480         sscanf(tempStr, "%f", &numOfSubInteg);
00481     else
00482     {
00483         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Number of Integrations");
00484         *error = 1;
00485         return;
00486     }
00487     linearity->integTime[fileNumber] = numOfSubInteg * subIntegTime;
00488 
00489     return; 
00490 }
00491 /*****************************************************************************/
00492 
00493 
00494 
00495 /******************************************************************************
00496 *               European Southern Observatory
00497 *          VLTI MIDI Maintenance Templates Software
00498 *
00499 * Module name:  assessLinearity
00500 * Input/Output: See function arguments to avoid duplication
00501 * Description:  The dependence of the count rate on the integration time and therewith
00502 *                the number of detected photons is over wide range a linear function.
00503 *                It is known, that the function above a count rate of 50000 up to
00504 *                saturation (65536 counts) differs from linarity.
00505 *                Fit a second or third order polynomial to the count levels as a function 
00506 *                of the actual exposure time. That is y = A0 + A1 * t + A2 * t^2 + A3 * t^3
00507 *                The values of the coeficients are the required relevant QC parameters.
00508 *
00509 *                1. Now we have the following 10 images (im)
00510 *                    linearity->aveImage[im][pix]    im = 0 to 9, pix = 0 to subWinSize
00511 *
00512 *                2. We know which file has saturation from
00513 *                    linearity->saturated[im]        im = 0 to 9
00514 *
00515 *                3. We have Integration Time
00516 *                    linearity->integTime[im]        im = 0 to 9
00517 *
00518 *                4. Mean flux for each pixel (pix) of each image (im) is of course
00519 *                    given directly from (1) above. That is
00520 *                    linearity->aveImage[im][pix]    im = 0 to 9, pix = 0 to subWinSize
00521 *
00522 *                5. We can first create a 3D plot of raw linearity for each pixel. The
00523 *                    releveant axes are:
00524 *                    x = Integration Time (given from 3 above)
00525 *                    y = Pixel position                 pix = 0 to subWinSize
00526 *                    z = Mean Flux
00527 *
00528 *                6. For each pixel in 5 above we can compute the linearity coefficients:
00529 *                    A0[pix], A1[pix], A2[pix], A3[pix]
00530 *
00531 *                7. Finally we find the averages of the above coefficients over a desired
00532 *                    region of the image.
00533 *
00534 * History:      
00535 * 23-Jun-04     (csabet) Created
00536 * 09-May-06     (csabet) Modified to characterise each pixel
00537 ******************************************************************************/
00538 void assessLinearity (
00539     ImageFormat        *format,        // In: Pointer to the image format
00540     char            *firstFitsFile,    // In: Name of first FITS file
00541     DetLinearity    *linearity,        // Ou: Pointer to the detector linearity data structure
00542     int                *error)            // Ou: Error status
00543 {
00544     //  Local Declarations
00545     //    ------------------
00546     const char      routine[] = "assessLinearity";
00547     cpl_polynomial  *coefficients;
00548     cpl_vector        *xPosition, *values;
00549     int                j, f, x, y, p, xLength, yLength, xCoord, dxCoord, yCoord, dyCoord,
00550                     polyDeg = 3, numOfUnsatImages, imageSize, subImageSize;
00551     cpl_size        power=0;
00552     double            *integTime, *pixel, *cA0, *cA1, *cA2, *cA3, *linFit;
00553     char            *fileName, *title;
00554     float            *buffer;
00555     
00556     //    Statistics for pixel regression test
00557     float            sigData = 0.0, coeff1, coeff2, siga, sigb, chi2, q;
00558     int                sigDataAvailable = 0;            
00559     
00560     //  Algorithm
00561     //    ---------
00562     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00563     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00564 
00565     //    Reset status
00566     *error = 0;
00567 
00568     //    Count number of unsaturated images
00569     numOfUnsatImages = 0;
00570     for (j = 0; j < linearity->size; j++)
00571     {
00572         if (!(linearity->saturated[j])) numOfUnsatImages++;
00573     }
00574 
00575     //    Compute linearity coefficients for each pixel in a well illuminated central area
00576     //    --------------------------------------------------------------------------------
00577     xLength = DET_LIN_WIN_PU * format->iXWidth;
00578     if (xLength < DET_LIN_WIN_MIN) xLength = format->iXWidth;
00579     xCoord = (format->iXWidth - xLength) / 2;
00580     dxCoord = format->iXWidth - xCoord;
00581     xLength = dxCoord - xCoord;    // Avoing round-off error
00582 
00583     yLength = DET_LIN_WIN_PU * format->iYWidth;
00584     if (yLength < DET_LIN_WIN_MIN) yLength = format->iYWidth;
00585     yCoord = (format->iYWidth - yLength) / 2;
00586     dyCoord = format->iYWidth - yCoord;
00587     yLength = dyCoord - yCoord;    // Avoing round-off error
00588 
00589     imageSize = format->iXWidth * format->iYWidth;
00590 
00591     //    Load values of the window for QC log
00592     linearity->winx = xCoord;
00593     linearity->windx = dxCoord;
00594     linearity->winy = yCoord;
00595     linearity->windy = dyCoord;
00596     if (diagnostic)
00597     {
00598        cpl_msg_info(cpl_func,"\nSelected window size \n");
00599        cpl_msg_info(cpl_func,"-------------------- \n");
00600        cpl_msg_info(cpl_func,"xCoord  = %3d \n", linearity->winx);
00601        cpl_msg_info(cpl_func,"dxCoord = %3d \n", linearity->windx);
00602        cpl_msg_info(cpl_func,"yCoord  = %3d \n", linearity->winy);
00603        cpl_msg_info(cpl_func,"dyCoord = %3d \n", linearity->windy);
00604     }
00605     fprintf (midiReportPtr, "\nSelected window size QCLOG \n");
00606     fprintf (midiReportPtr, "-------------------- QCLOG \n");
00607     fprintf (midiReportPtr, "xCoord  = %3d QCLOG \n", linearity->winx);
00608     fprintf (midiReportPtr, "dxCoord = %3d QCLOG \n", linearity->windx);
00609     fprintf (midiReportPtr, "yCoord  = %3d QCLOG \n", linearity->winy);
00610     fprintf (midiReportPtr, "dyCoord = %3d QCLOG \n", linearity->windy);
00611     
00612     //    Allocate memory
00613     fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00614     title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00615     buffer = (float *) calloc (imageSize, sizeof (float));
00616     cA0 = (double *) calloc (imageSize, sizeof (double));
00617     cA1 = (double *) calloc (imageSize, sizeof (double));
00618     cA2 = (double *) calloc (imageSize, sizeof (double));
00619     cA3 = (double *) calloc (imageSize, sizeof (double));
00620     linFit = (double *) calloc (numOfUnsatImages, sizeof (double));
00621     
00622     //    Create a sub-image from each unsaturated image
00623     for (j = 0; j < numOfUnsatImages; j++)
00624     {
00625         for (y = 0; y < format->iYWidth; y++)
00626         {
00627             f = y * format->iXWidth;
00628             for (x = 0; x < format->iXWidth; x++)
00629             {
00630                 if (!((x >= xCoord) && (x < dxCoord) &&    (y >= yCoord) && (y < dyCoord)))
00631                 {
00632                     p = f + x;
00633                     linearity->aveImage[j][p] = 0.0;
00634                 }
00635             }
00636         }
00637         if (plotFile && diagnostic)
00638         {
00639             sprintf (fileName, "3dCentralImage%d", j+1);
00640             sprintf (title, "Averaged central image, file %d", j+1);
00641             midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0, linearity->aveImage[j], 
00642                 format->iXWidth, format->iYWidth, "lines", "3");
00643         }
00644     }
00645 
00646     //    Allocate memory
00647     pixel = (double *) calloc (numOfUnsatImages, sizeof (double));
00648     integTime = (double *) calloc (numOfUnsatImages, sizeof (double));
00649     
00650     //    For all unsaturated images Load integration time into a buffer
00651     for (j = 0; j < numOfUnsatImages; j++)
00652             integTime[j] = (double) (linearity->integTime[j]);
00653 
00654     for (y = 0; y < format->iYWidth; y++)
00655     {
00656         f = y * format->iXWidth;
00657         for (x = 0; x < format->iXWidth; x++)
00658         {
00659             //    For all unsaturated images Load pixel flux into a buffer
00660             if ((x >= xCoord) && (x < dxCoord) &&
00661                 (y >= yCoord) && (y < dyCoord))
00662             {
00663                 p = f + x;
00664                 for (j = 0; j < numOfUnsatImages; j++)
00665                     pixel[j] = (double) (linearity->aveImage[j][p]);
00666                 
00667                 //    Plot raw linearity and delete plot file
00668                 if (plotFile && diagnostic > 2)
00669                 {
00670                     sprintf (title, "Flux versus integration time. Pixel %d, %d", x, y);
00671                     midiCreatePlotFileDouble2D2P ("temp1", title, "Integration time", "Pixel flux",
00672                         1, integTime, pixel, 0,    numOfUnsatImages, 1, "lines 3");
00673                 }
00674             
00675                 //    Compute coefficients of a 3rd order polynomial
00676                 xPosition = cpl_vector_wrap (numOfUnsatImages, integTime);
00677                 values = cpl_vector_wrap (numOfUnsatImages, pixel);
00678                 coefficients = cpl_polynomial_fit_1d_create (xPosition, values, polyDeg, NULL);
00679                 power = 0;
00680                 cA0[p] = cpl_polynomial_get_coeff (coefficients, &power);
00681                 power = 1;
00682                 cA1[p] = cpl_polynomial_get_coeff (coefficients, &power);
00683                 power = 2;
00684                 cA2[p] = cpl_polynomial_get_coeff (coefficients, &power);
00685                 power = 3;
00686                 cA3[p] = cpl_polynomial_get_coeff (coefficients, &power);
00687                 cpl_vector_unwrap (xPosition);
00688                 cpl_vector_unwrap (values);
00689                 cpl_polynomial_delete (coefficients);
00690 
00691                 //    Plot fit linearity and delete plot file
00692                 if (plotFile && diagnostic > 2)
00693                 {
00694                     //    Fill in the result
00695                     for (j = 0; j < numOfUnsatImages; j++)
00696                         linFit[j] = cA0[p] + cA1[p] * j + cA2[p] * (j * j) + cA3[p] * (j * j * j);
00697 
00698                     sprintf (title, "Flux versus integration time (polynomial fit). Pixel %d, %d", x, y);
00699                     midiCreatePlotFileDouble2D2P ("temp1", title, "Integration time", "Pixel flux",
00700                         1, integTime, linFit, 0, numOfUnsatImages, 1, "lines 1");
00701                 }
00702             
00703                 //    Now do a regression test (degree of deviation from linearity)
00704                 midiGetLinearFit (integTime, pixel, numOfUnsatImages, sigData, sigDataAvailable, 
00705                     &coeff1, &coeff2, &siga, &sigb, &chi2, &q, error);
00706                 
00707                 //    Load a measure of linear deviation. Here we use the
00708                 //    combined standard deviation of the linear coefficients
00709                 linearity->deviation[p] = sqrt ((siga+sigb)/2.0);
00710                 
00711                 if (diagnostic > 2)
00712                 {
00713                    cpl_msg_info(cpl_func,"\nPixel (%d, %d) statistics \n", x, y);
00714                    cpl_msg_info(cpl_func,"------------------------- \n");
00715                    cpl_msg_info(cpl_func,"coeff1 =   %f \n", coeff1);
00716                    cpl_msg_info(cpl_func,"coeff2 =   %f \n", coeff2);
00717                    cpl_msg_info(cpl_func,"siga   =   %f \n", siga);
00718                    cpl_msg_info(cpl_func,"sigb   =   %f \n", sigb);
00719                    cpl_msg_info(cpl_func,"chi2   =   %f \n", chi2);
00720                    cpl_msg_info(cpl_func,"q      =   %f \n", q);
00721                     fprintf (midiReportPtr, "\nPixel (%d, %d) statistics \n", x, y);
00722                     fprintf (midiReportPtr, "------------------------- \n");
00723                     fprintf (midiReportPtr, "coeff1 =   %f \n", coeff1);
00724                     fprintf (midiReportPtr, "coeff2 =   %f \n", coeff2);
00725                     fprintf (midiReportPtr, "siga   =   %f \n", siga);
00726                     fprintf (midiReportPtr, "sigb   =   %f \n", sigb);
00727                     fprintf (midiReportPtr, "chi2   =   %f \n", chi2);
00728                     fprintf (midiReportPtr, "q      =   %f \n", q);
00729                 }
00730             }
00731         }
00732     }
00733 
00734     //    Create plots and FITS images of coefficients
00735     if (plotFile && diagnostic)
00736     {
00737         midiCreatePlotFileDouble3D ("3dCoefficientMapA0", "Map of A0 coefficients", "X", "Y", "Coefficient value", 
00738             0, cA0, format->iXWidth, format->iYWidth, "points", "1");
00739         midiCreatePlotFileDouble3D ("3dCoefficientMapA1", "Map of A1 coefficients", "X", "Y", "Coefficient value", 
00740             0, cA1, format->iXWidth, format->iYWidth, "points", "1");
00741         midiCreatePlotFileDouble3D ("3dCoefficientMapA2", "Map of A2 coefficients", "X", "Y", "Coefficient value", 
00742             0, cA2, format->iXWidth, format->iYWidth, "points", "1");
00743         midiCreatePlotFileDouble3D ("3dCoefficientMapA3", "Map of A3 coefficients", "X", "Y", "Coefficient value", 
00744             0, cA3, format->iXWidth, format->iYWidth, "points", "1");
00745     }
00746     for (p = 0; p < imageSize; p++)    buffer[p] = (float) (cA0[p]);
00747     removeDc (imageSize, buffer, buffer);
00748     createFitsImage ("Coefficients A0", "CoefficientMapA0", firstFitsFile, format->iXWidth, format->iYWidth, buffer);
00749     for (p = 0; p < imageSize; p++)    buffer[p] = (float) (cA1[p]);
00750     removeDc (imageSize, buffer, buffer);
00751     createFitsImage ("Coefficients A1", "CoefficientMapA1", firstFitsFile, format->iXWidth, format->iYWidth, buffer);
00752     for (p = 0; p < imageSize; p++)    buffer[p] = (float) (cA2[p]);
00753     removeDc (imageSize, buffer, buffer);
00754     createFitsImage ("Coefficients A2", "CoefficientMapA2", firstFitsFile, format->iXWidth, format->iYWidth, buffer);
00755     for (p = 0; p < imageSize; p++)    buffer[p] = (float) (cA3[p]);
00756     removeDc (imageSize, buffer, buffer);
00757     createFitsImage ("Coefficients A3", "CoefficientMapA3", firstFitsFile, format->iXWidth, format->iYWidth, buffer);
00758 
00759     //    Create plot and FITS image of the linear deviation
00760     if (plotFile) midiCreatePlotFile3D ("3dLinearitySigmaMap", "Map of Linearity Standard Deviation", "X", "Y", "Sigma", 
00761         0, linearity->deviation, format->iXWidth, format->iYWidth, "lines", "1");
00762 
00763     //    Suitably prepare image for FITS
00764     removeDc (imageSize, linearity->deviation, buffer);
00765     createFitsImage ("Map of Linearity Standard Deviation", "LinearitySigmaMap", firstFitsFile, format->iXWidth, 
00766         format->iYWidth, buffer);
00767 
00768     //    Compute mean value of the coefficients and the overall linearity deviation
00769     linearity->meanCoeffA0 = 0.0;
00770     linearity->meanCoeffA1 = 0.0;
00771     linearity->meanCoeffA2 = 0.0;
00772     linearity->meanCoeffA3 = 0.0;
00773     subImageSize = 0;
00774     for (y = 0; y < format->iYWidth; y++)
00775     {
00776         f = y * format->iXWidth;
00777         for (x = 0; x < format->iXWidth; x++)
00778         {
00779             if ((x >= xCoord) && (x < dxCoord) && (y >= yCoord) && (y < dyCoord))
00780             {
00781                 p = f + x;
00782                 linearity->meanCoeffA0 += cA0[p];
00783                 linearity->meanCoeffA1 += cA1[p];
00784                 linearity->meanCoeffA2 += cA2[p];
00785                 linearity->meanCoeffA3 += cA3[p];
00786                 linearity->meanSigma += linearity->deviation[p];
00787                 subImageSize++;
00788             }
00789         }
00790     }
00791     if (subImageSize)
00792     {
00793         linearity->meanCoeffA0 /= subImageSize;
00794         linearity->meanCoeffA1 /= subImageSize;
00795         linearity->meanCoeffA2 /= subImageSize;
00796         linearity->meanCoeffA3 /= subImageSize;
00797         linearity->meanSigma /= subImageSize;
00798     }
00799     
00800    cpl_msg_info(cpl_func,"\nMean coefficients for polynomial fit: y = a0 + a1 * t + a2 * t^2 + a3 * t^3 \n");
00801    cpl_msg_info(cpl_func,"--------------------------------------------------------------------------- \n");
00802    cpl_msg_info(cpl_func,"a0, a1, a2, a3 = %f, %f, %f, %f \n", 
00803         linearity->meanCoeffA0, linearity->meanCoeffA1, linearity->meanCoeffA2, linearity->meanCoeffA3);
00804     fprintf (midiReportPtr, "\nMean coefficients for polynomial fit: y = a0 + a1 * t + a2 * t^2 + a3 * t^3 (QCLOG) \n");
00805     fprintf (midiReportPtr, "--------------------------------------------------------------------------- (QCLOG) \n");
00806     fprintf (midiReportPtr, "a0, a1, a2, a3 = %f, %f, %f, %f (QCLOG) \n", 
00807         linearity->meanCoeffA0, linearity->meanCoeffA1, linearity->meanCoeffA2, linearity->meanCoeffA3);
00808 
00809    cpl_msg_info(cpl_func,"\nMean linearity standard deviation = %f \n\n", linearity->meanSigma);
00810     fprintf (midiReportPtr, "\nMean linearity standard deviation = %f (QCLOG) \n\n", linearity->meanSigma);
00811 
00812     //    Plot linear fit of the mean
00813     for (j = 0; j < numOfUnsatImages; j++)
00814     {
00815         linFit[j] = linearity->meanCoeffA0 + linearity->meanCoeffA1 * j + linearity->meanCoeffA2 * (j * j) + 
00816             linearity->meanCoeffA3 * (j * j * j);
00817         linearity->mean[j] = (float) linFit[j];
00818         
00819     }
00820     midiCreatePlotFileDouble2D2P ("MeanFluxVersusTimePolyFit", "Meam flux versus time (polynomial fit)", 
00821         "Integration time", "Mean flux", 0, integTime, linFit, 0, numOfUnsatImages, 1, "lines 1");
00822     
00823     //    Release memory
00824     free (linFit);
00825     free (cA0);
00826     free (cA1);
00827     free (cA2);
00828     free (cA3);
00829     free (fileName);
00830     free (title);
00831     free (pixel);
00832     free (integTime);
00833     free (buffer);
00834      
00835     return;
00836 
00837 }
00838 /*****************************************************************************/
00839 
00840 
00841 
00842 /******************************************************************************
00843 *               European Southern Observatory
00844 *          VLTI MIDI Maintenance Templates Software
00845 *
00846 * Module name:  checkSaturationDetLin
00847 * Input/Output: See function arguments to avoid duplication
00848 * Description:  Checks if any pixel is saturated. If so this exposure
00849 *                will not be included in the later processing
00850 *
00851 * History:      
00852 * 05-May-06     (csabet) Created
00853 ******************************************************************************/
00854 void checkSaturationDetLin (
00855     short int    *inData,        // In: Pointer to the input data
00856     float        scalingOffset,    // In: Scaling Offset
00857     ImageFormat    *format,        // In: Pointer to the image format
00858     float        *image,            // Ou: Output image
00859     int            *saturated)        // Ou: Saturation indicator
00860 
00861 {
00862     //  Local Declarations
00863     //    ------------------
00864     const char  routine[] = "checkSaturationDetLin";
00865     int            i, count, pixel, frame, imageSize;
00866     
00867     //  Algorithm
00868     //    ---------
00869     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00870     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
00871     
00872     //    Reset status
00873     imageSize = format->iXWidth * format->iYWidth;
00874     *saturated = 0;
00875     
00876     for (pixel = 0; pixel < imageSize; pixel++)
00877     {
00878         //    Along the time axis (frame)
00879         image[pixel] = 0;
00880         count = 0;
00881         for (frame = 0; frame < format->numOfFrames; frame++)
00882         {
00883             //    Load each pixel data along the time axis
00884             i = frame * imageSize + pixel;
00885             if (isnan (inData[i]))
00886             {
00887                 sprintf (midiMessage, "Found bad pixel %d on frame %d", frame, pixel);
00888                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00889             }
00890             else if (((float) (inData[i] + scalingOffset)) >= PIXEL_SATURATION)
00891             {
00892                 if (diagnostic > 2)
00893                 {
00894                     sprintf (midiMessage, "Found saturated pixel %d on frame %d", frame, pixel);
00895                     midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00896                 }
00897                 *saturated = 1;
00898             }
00899             else
00900             {
00901                 count++;
00902                 image[pixel] += (float) (inData[i] + scalingOffset);
00903             }
00904         }
00905 
00906         //    Compute average image
00907         if (count) image[pixel] /= count;
00908     }
00909 
00910     if (*saturated)
00911     {
00912         sprintf (midiMessage, "Found saturated pixels in this image");
00913         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00914     }
00915 
00916     return; 
00917 }
00918 /*****************************************************************************/

Generated on 5 Mar 2013 for MIDI Pipeline Reference Manual by  doxygen 1.6.1