00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 void procDetLin (
00067 MidiFiles *fileNames,
00068 int *error)
00069 {
00070
00071
00072
00073 const char routine[] = "procDetLin";
00074 ImageFormat *format=NULL;
00075 DetLinearity *linearity=NULL;
00076 int numOfFiles;
00077 FILE *signaturePtr=NULL;
00078
00079
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
00085 signaturePtr = fopen ("MIDI_sig_lin.log", "w");
00086 fclose (signaturePtr);
00087
00088
00089 *error = 0;
00090 numOfFiles = 0;
00091
00092
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
00117 freeDetLin (linearity);
00118 freeImageFormat (format);
00119
00120 return;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 void computeDetLin (
00141 int numOfFiles,
00142 MidiFiles *fileNames,
00143 ImageFormat *format,
00144 DetLinearity *linearity,
00145 int *error)
00146 {
00147
00148
00149
00150 const char routine[] = "computeDetLin";
00151 char *fileTemp, *classification, *firstFitsFile;
00152 FILE *inFitsBatchPtr;
00153 ImageFormat *formatLocal;
00154 int localError, fileNumber, extNumOfImagingDataFile;
00155
00156
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
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
00168 *error = 0;
00169 localError = 0;
00170 fileNumber = 0;
00171 linearity->exists = 0;
00172 formatLocal->hasData = 0;
00173
00174
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
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
00195 extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, &localError);
00196 if (localError)
00197 {
00198 *error = 1;
00199 break;
00200 }
00201
00202
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
00215 if (formatLocal->hasData)
00216 {
00217
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
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
00229 createAndDisplayImageDetLin (fileNumber, fileNames->inFitsName, extNumOfImagingDataFile,
00230 format, linearity, &localError);
00231 if (localError) *error = 1;
00232
00233
00234 getIntegrationTime (fileNumber, fileNames->inFitsName, linearity, &localError);
00235 if (localError) *error = 1;
00236
00237
00238 linearity->exists = 1;
00239
00240
00241 fileNumber++;
00242 if (fileNumber > numOfFiles)
00243 {
00244 *error = 1;
00245 break;
00246 }
00247
00248
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
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
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
00289 fclose (inFitsBatchPtr);
00290
00291
00292 freeImageFormat (formatLocal);
00293 free (fileTemp);
00294 free (classification);
00295 free (firstFitsFile);
00296
00297 return;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 void createAndDisplayImageDetLin (
00315 int fileNumber,
00316 char *fileName,
00317 int extensionNumber,
00318 ImageFormat *format,
00319 DetLinearity *linearity,
00320 int *error)
00321
00322 {
00323
00324
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
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
00339 *error = 0;
00340 linearity->saturated[fileNumber] = 0;
00341
00342
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
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
00369 inData = (short int*) qfits_query_column (pTable, indexData, NULL);
00370
00371
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
00393 createAveragedImage (inData, scalingOffset, format, linearity->aveImage[fileNumber]);
00394
00395
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
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
00412 checkSaturationDetLin (inData, scalingOffset, format, linearity->aveImage[fileNumber],
00413 &(linearity->saturated[fileNumber]));
00414
00415
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
00427 qfits_table_close (pTable);
00428 free (inData);
00429
00430 return;
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 void getIntegrationTime (
00447 int fileNumber,
00448 char *fileName,
00449 DetLinearity *linearity,
00450 int *error)
00451
00452 {
00453
00454
00455
00456 const char routine[] = "getIntegrationTime";
00457 char *tempStr;
00458 float numOfSubInteg, subIntegTime;
00459
00460
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
00466 *error = 0;
00467
00468
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
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 void assessLinearity (
00539 ImageFormat *format,
00540 char *firstFitsFile,
00541 DetLinearity *linearity,
00542 int *error)
00543 {
00544
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
00557 float sigData = 0.0, coeff1, coeff2, siga, sigb, chi2, q;
00558 int sigDataAvailable = 0;
00559
00560
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
00566 *error = 0;
00567
00568
00569 numOfUnsatImages = 0;
00570 for (j = 0; j < linearity->size; j++)
00571 {
00572 if (!(linearity->saturated[j])) numOfUnsatImages++;
00573 }
00574
00575
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;
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;
00588
00589 imageSize = format->iXWidth * format->iYWidth;
00590
00591
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
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
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
00647 pixel = (double *) calloc (numOfUnsatImages, sizeof (double));
00648 integTime = (double *) calloc (numOfUnsatImages, sizeof (double));
00649
00650
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
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
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
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
00692 if (plotFile && diagnostic > 2)
00693 {
00694
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
00704 midiGetLinearFit (integTime, pixel, numOfUnsatImages, sigData, sigDataAvailable,
00705 &coeff1, &coeff2, &siga, &sigb, &chi2, &q, error);
00706
00707
00708
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
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
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
00764 removeDc (imageSize, linearity->deviation, buffer);
00765 createFitsImage ("Map of Linearity Standard Deviation", "LinearitySigmaMap", firstFitsFile, format->iXWidth,
00766 format->iYWidth, buffer);
00767
00768
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
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
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
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854 void checkSaturationDetLin (
00855 short int *inData,
00856 float scalingOffset,
00857 ImageFormat *format,
00858 float *image,
00859 int *saturated)
00860
00861 {
00862
00863
00864 const char routine[] = "checkSaturationDetLin";
00865 int i, count, pixel, frame, imageSize;
00866
00867
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
00873 imageSize = format->iXWidth * format->iYWidth;
00874 *saturated = 0;
00875
00876 for (pixel = 0; pixel < imageSize; pixel++)
00877 {
00878
00879 image[pixel] = 0;
00880 count = 0;
00881 for (frame = 0; frame < format->numOfFrames; frame++)
00882 {
00883
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
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