00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <cpl.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <math.h>
00026 #include "midiGlobal.h"
00027 #include "midiLib.h"
00028 #include "memoryHandling.h"
00029 #include "errorHandling.h"
00030 #include "calibration.h"
00031 #include "fitsAnalysisFrgSP.h"
00032 #include "imageProcessing.h"
00033 #include "createProdFrg.h"
00034 #include "transferFunction.h"
00035 #include "visibility.h"
00036 #include "diagnostics.h"
00037 #include "estimationFrg.h"
00038 #include "photometry.h"
00039 #include "preProcFrgSP.h"
00040 #include "procFrgSP.h"
00041 #include "procFrgHS.h"
00042 #include "statistics.h"
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 void procFrgSP (
00077 MidiFiles *fileNames,
00078 UserOptions *options,
00079 int *error,
00080 cpl_parameterlist *parlist,
00081 cpl_frameset *frameset)
00082 {
00083
00084
00085
00086 const char routine[] = "procFrgSP";
00087 ImageFormat *formatInterf=NULL;
00088 ImageFormat *formatPhotomA=NULL;
00089 ImageFormat *formatPhotomB=NULL;
00090 CompressedData *compressedInterf=NULL, *compressedPhotomA=NULL, *compressedPhotomB=NULL;
00091 FilterData *filterInfo=NULL;
00092 DispersedResult *dispResult=NULL;
00093 CorrectedFrames *corrFrames=NULL;
00094
00095
00096 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00097 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00098
00099
00100 *error = 0;
00101 if (options->processing == UNDISPERSED)
00102 {
00103 *error = 1;
00104 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00105 "'UNDISPERSED' is an invalid processing request for SCI_PHOT data");
00106 return;
00107 }
00108
00109
00110 formatInterf = callocImageFormat ();
00111 formatPhotomA = callocImageFormat ();
00112 formatPhotomB = callocImageFormat ();
00113 corrFrames = cpl_calloc (1, sizeof (CorrectedFrames));
00114
00115 corrFrames->CorrInterf=0;
00116 corrFrames->CorrPhotomA=0;
00117 corrFrames->CorrPhotomB=0;
00118
00119
00120
00121 analyseFitsFrgSP (fileNames, formatInterf, formatPhotomA, formatPhotomB, error);
00122 if (*error)
00123 {
00124 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse FITS files");
00125 freeImageFormat (formatInterf);
00126 freeImageFormat (formatPhotomA);
00127 freeImageFormat (formatPhotomB);
00128 return;
00129 }
00130
00131
00132
00133
00134 compressedInterf = callocMidiCompressed (formatInterf);
00135 compressedPhotomA = callocMidiCompressed (formatPhotomA);
00136 compressedPhotomB = callocMidiCompressed (formatPhotomB);
00137 badChannelList = (int *) calloc (formatInterf->iXWidth, sizeof (int));
00138 filterInfo = callocFilterInfo();
00139
00140
00141
00142 preProcFrgSP (options, filterInfo, fileNames, compressedInterf,
00143 compressedPhotomA, compressedPhotomB, formatInterf, formatPhotomA, formatPhotomB, error);
00144 if (*error)
00145 {
00146 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress FITS files");
00147 freeCompressedData (formatInterf, compressedInterf);
00148 freeCompressedData (formatPhotomA, compressedPhotomA);
00149 freeCompressedData (formatPhotomB, compressedPhotomB);
00150 free (badChannelList);
00151 freeImageFormat (formatInterf);
00152 freeImageFormat (formatPhotomA);
00153 freeImageFormat (formatPhotomB);
00154 freeFilterInfo (filterInfo);
00155 return;
00156 }
00157
00158
00159 checkDelayLineJumps ("INTERF", formatInterf, compressedInterf);
00160 checkDelayLineJumps ("PHOTOMA", formatPhotomA, compressedPhotomA);
00161 checkDelayLineJumps ("PHOTOMB", formatPhotomB, compressedPhotomB);
00162
00163
00164 checkDelayLineConsistencies ("INTERF", "PHOTOMA", compressedInterf, compressedPhotomA, formatInterf, formatPhotomA, error);
00165 checkDelayLineConsistencies ("INTERF", "PHOTOMB", compressedInterf, compressedPhotomB, formatInterf, formatPhotomB, error);
00166 if (*error)
00167 {
00168 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "SCI_PHOT processing abandonned");
00169 freeCompressedData (formatInterf, compressedInterf);
00170 freeCompressedData (formatPhotomA, compressedPhotomA);
00171 freeCompressedData (formatPhotomB, compressedPhotomB);
00172 free (badChannelList);
00173 freeImageFormat (formatInterf);
00174 freeImageFormat (formatPhotomA);
00175 freeImageFormat (formatPhotomB);
00176 freeFilterInfo (filterInfo);
00177 return;
00178 }
00179
00180
00181
00182 reportInterfChopping ("INTERF", formatInterf, compressedInterf);
00183 reportPhotomChopping ("PHOTOMA", formatPhotomA, compressedPhotomA);
00184 reportPhotomChopping ("PHOTOMB", formatPhotomB, compressedPhotomB);
00185 displayInterfChoppingDisp ("INTERF", formatInterf, compressedInterf);
00186 displayPhotomChoppingDisp ("PHOTOMA", formatPhotomA, compressedPhotomA);
00187 displayPhotomChoppingDisp ("PHOTOMB", formatPhotomB, compressedPhotomB);
00188
00189
00190 dispResult = callocDispResult (formatInterf->iXWidth);
00191 cleanupFrgSP (options->processing, formatInterf, formatPhotomA, formatPhotomB, compressedInterf,
00192 compressedPhotomA, compressedPhotomB, error, corrFrames);
00193 if (*error)
00194 {
00195 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "SCI_PHOT processing abandonned");
00196 freeCompressedData (formatInterf, compressedInterf);
00197 freeCompressedData (formatPhotomA, compressedPhotomA);
00198 freeCompressedData (formatPhotomB, compressedPhotomB);
00199 free (badChannelList);
00200 freeImageFormat (formatInterf);
00201 freeImageFormat (formatPhotomA);
00202 freeImageFormat (formatPhotomB);
00203 freeFilterInfo (filterInfo);
00204 freeDispResult (dispResult);
00205 return;
00206 }
00207
00208
00209 prepareWaterfallDisplay (fileNames, formatInterf, compressedInterf);
00210
00211
00212 applyOpticalCompensation (fileNames, formatInterf, compressedInterf,
00213 compressedPhotomA, compressedPhotomB, error, parlist, frameset);
00214
00215
00216
00217 if (strcmp (formatInterf->obsCatg, "CALIB") == 0)
00218 calibrateWaveUsingPolynom (fileNames, formatInterf);
00219
00220
00221
00222
00223
00224 postProcFringe (fileNames, filterInfo, compressedInterf, compressedPhotomA,
00225 compressedPhotomB, formatInterf, formatPhotomA,
00226 formatPhotomB, dispResult, error,
00227 parlist, frameset, corrFrames);
00228 if (*error)
00229 {
00230 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "SCI_PHOT processing abandonned");
00231 freeCompressedData (formatInterf, compressedInterf);
00232 freeCompressedData (formatPhotomA, compressedPhotomA);
00233 freeCompressedData (formatPhotomB, compressedPhotomB);
00234 free (badChannelList);
00235 freeImageFormat (formatInterf);
00236 freeImageFormat (formatPhotomA);
00237 freeImageFormat (formatPhotomB);
00238 freeFilterInfo (filterInfo);
00239 freeDispResult (dispResult);
00240 return;
00241 }
00242
00243
00244 freeCompressedData (formatInterf, compressedInterf);
00245 freeCompressedData (formatPhotomA, compressedPhotomA);
00246 freeCompressedData (formatPhotomB, compressedPhotomB);
00247 free (badChannelList);
00248 freeImageFormat (formatInterf);
00249 freeImageFormat (formatPhotomA);
00250 freeImageFormat (formatPhotomB);
00251 freeFilterInfo (filterInfo);
00252 freeDispResult (dispResult);
00253 cpl_free(corrFrames);
00254 return;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 void cleanupFrgSP (
00271 enum ProcessingMode processing,
00272 ImageFormat *formatInterf,
00273 ImageFormat *formatPhotomA,
00274 ImageFormat *formatPhotomB,
00275 CompressedData *compressedInterf,
00276 CompressedData *compressedPhotomA,
00277 CompressedData *compressedPhotomB,
00278 int *error,
00279 CorrectedFrames *corrFrames)
00280 {
00281
00282
00283
00284 const char routine[] = "cleanupFrgSP";
00285 int transitions, f, R, X, n, localError;
00286 double *array, fluxAve;
00287 float *arrayF;
00288 char *fileName, *title;
00289
00290
00291
00292 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00293 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00294
00295 cpl_msg_info(cpl_func,"\nCleaning up SCI_PHOT files for batch %d \n", batchNumber);
00296 cpl_msg_info(cpl_func,"------------------------------------ \n");
00297 fprintf (midiReportPtr, "\nCleaning up SCI_PHOT files for batch %d \n", batchNumber);
00298 fprintf (midiReportPtr, "------------------------------------ \n");
00299
00300
00301 localError = 0;
00302 *error = 0;
00303 transitions = 0;
00304
00305
00306
00307 transitions = countTransitions ('T', formatInterf->numOfFrames, compressedInterf->tarType);
00308 sprintf (midiMessage, "Target Transitions for Interf = %d", transitions);
00309 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00310 if (transitions < 10)
00311 {
00312 sprintf (midiMessage, "Interferometry data is not chopped");
00313 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00314 *error = 1;
00315 }
00316
00317
00318
00319 transitions = countTransitions ('T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
00320 sprintf (midiMessage, "Target Transitions for Photom A = %d", transitions);
00321 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00322 if (transitions < 10)
00323 {
00324 sprintf (midiMessage, "Photometry A data is not chopped");
00325 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00326 *error = 1;
00327 }
00328
00329
00330
00331 transitions = countTransitions ('T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
00332 sprintf (midiMessage, "Target Transitions for Photom B = %d", transitions);
00333 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00334 if (transitions < 10)
00335 {
00336 sprintf (midiMessage, "Photometry B data is not chopped");
00337 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00338 *error = 1;
00339 }
00340
00341
00342 if (!(*error))
00343 {
00344 formatInterf->chopped = 1;
00345 formatPhotomA->chopped = 1;
00346 formatPhotomB->chopped = 1;
00347 }
00348 else
00349 {
00350 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot cleanup SCI_PHOT files");
00351 *error = 1;
00352 return;
00353 }
00354
00355
00356
00357
00358
00359
00360 corrFrames->CorrInterf=correctTarType("INTERF", compressedInterf->tarType,
00361 compressedInterf->time, formatInterf->numOfFrames, &localError);
00362 if (localError) *error = 1;
00363
00364 corrFrames->CorrPhotomA=correctTarType("PHOTOMA", compressedPhotomA->tarType,
00365 compressedPhotomA->time, formatPhotomA->numOfFrames, &localError);
00366 if (localError) *error = 1;
00367
00368 corrFrames->CorrPhotomB=correctTarType("PHOTOMB", compressedPhotomB->tarType,
00369 compressedPhotomB->time, formatPhotomB->numOfFrames, &localError);
00370 if (*error || localError)
00371 {
00372 *error = 1;
00373 return;
00374 }
00375
00376
00377
00378 removeSkyBackground ("INTERF", processing, formatInterf, compressedInterf, &localError);
00379 if (localError) *error = 1;
00380 removeSkyBackground ("PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
00381 if (localError) *error = 1;
00382 removeSkyBackground ("PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
00383 if (formatInterf->numOfFrames != formatPhotomA->numOfFrames ||
00384 formatInterf->numOfFrames != formatPhotomB->numOfFrames || *error || localError)
00385 {
00386 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unequal numOfFrames");
00387 *error = 1;
00388 return;
00389 }
00390
00391
00392 for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00393 {
00394 for (X = 0; X < formatInterf->iXWidth; X++)
00395 {
00396 if (badChannelList[X])
00397 continue;
00398
00399 for (f = 0; f < formatInterf->numOfFrames; f++)
00400 {
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 if (compressedInterf->tarType[f] != 'T')
00414 compressedInterf->rejectList[X][f] |= BSL_SKY_SCAN;
00415 }
00416 }
00417 }
00418
00419 for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00420 {
00421 for (X = 0; X < formatPhotomA->iXWidth; X++)
00422 {
00423 if (badChannelList[X])
00424 continue;
00425
00426 for (f = 0; f < formatPhotomA->numOfFrames; f++)
00427 {
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 if (compressedPhotomA->tarType[f] != 'T')
00446 compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
00447 }
00448 }
00449 }
00450
00451 for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00452 {
00453 for (X = 0; X < formatPhotomB->iXWidth; X++)
00454 {
00455 if (badChannelList[X])
00456 continue;
00457
00458 for (f = 0; f < formatPhotomB->numOfFrames; f++)
00459 {
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 if (compressedPhotomB->tarType[f] != 'T')
00478 compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
00479 }
00480 }
00481 }
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 if (plotFile)
00494 {
00495 array = (double *) calloc (formatInterf->numOfFrames, sizeof (double));
00496
00497 n = 0;
00498 fluxAve = 0.0;
00499 for (f = 0; f < formatInterf->numOfFrames; f++)
00500 {
00501 if (compressedInterf->tarType[f] == 'T')
00502 {
00503 array[n] = compressedInterf->iFringe1[f];
00504 fluxAve += array[n];
00505 n++;
00506 }
00507 }
00508 fluxAve /= n;
00509 cpl_msg_info(cpl_func,"Interf DATA2 Average Flux = %f \n", fluxAve);
00510 midiCreatePlotFileDouble2D ("InterfCleanDATA2", "Interferometry DATA2 (Sky Removed)",
00511 "Frame", "Flux", 0, array, 0, n, 1);
00512
00513 n = 0;
00514 fluxAve = 0.0;
00515 for (f = 0; f < formatInterf->numOfFrames; f++)
00516 {
00517 if (compressedInterf->tarType[f] == 'T')
00518 {
00519 array[n] = compressedInterf->iFringe2[f];
00520 fluxAve += array[n];
00521 n++;
00522 }
00523 }
00524 fluxAve /= n;
00525 cpl_msg_info(cpl_func,"Interf DATA3 Average Flux = %f \n", fluxAve);
00526 midiCreatePlotFileDouble2D ("InterfCleanDATA3", "Interferometry DATA3 (Sky Removed)",
00527 "Frame", "Flux", 0, array, 0, n, 1);
00528
00529 n = 0;
00530 for (f = 0; f < formatInterf->numOfFrames; f++)
00531 {
00532 if (compressedInterf->tarType[f] == 'T')
00533 {
00534 array[n] = compressedInterf->iFringe[f];
00535 n++;
00536 }
00537 }
00538 midiCreatePlotFileDouble2D ("InterfCleanDATA2_DATA3", "Interferometry DATA2-DATA3 (Sky Removed)",
00539 "Frame", "Flux", 0, array, 0, n, 1);
00540
00541 n = 0;
00542 fluxAve = 0.0;
00543 for (f = 0; f < formatPhotomA->numOfFrames; f++)
00544 {
00545 if (compressedPhotomA->tarType[f] == 'T')
00546 {
00547 array[n] = compressedPhotomA->iFringe1[f];
00548 fluxAve += array[n];
00549 n++;
00550 }
00551 }
00552 fluxAve /= n;
00553 cpl_msg_info(cpl_func,"PhotomA DATA1 Average Flux = %f \n", fluxAve);
00554 midiCreatePlotFileDouble2D ("PhotomACleanDATA1", "Photometry A DATA1 (Sky Removed)",
00555 "Frame", "Flux", 0, array, 0, n, 1);
00556
00557 n = 0;
00558 fluxAve = 0.0;
00559 for (f = 0; f < formatPhotomB->numOfFrames; f++)
00560 {
00561 if (compressedPhotomB->tarType[f] == 'T')
00562 {
00563 array[n] = compressedPhotomB->iFringe1[f];
00564 fluxAve += array[n];
00565 n++;
00566 }
00567 }
00568 fluxAve /= n;
00569 cpl_msg_info(cpl_func,"PhotomB DATA4 Average Flux = %f \n", fluxAve);
00570 midiCreatePlotFileDouble2D ("PhotomBCleanDATA4", "Photometry B DATA4 (Sky Removed)",
00571 "Frame", "Flux", 0, array, 0, n, 1);
00572
00573 free (array);
00574 }
00575
00576
00577 if (plotFile && diagnostic > 4)
00578 {
00579 arrayF = (float *) calloc (formatInterf->numOfFrames, sizeof (float));
00580 fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00581 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00582
00583 for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00584 {
00585 for (X = 0; X < formatInterf->iXWidth; X++)
00586 {
00587 if (badChannelList[X])
00588 continue;
00589
00590 n = 0;
00591 for (f = 0; f < formatInterf->numOfFrames; f++)
00592 {
00593 if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
00594 {
00595 arrayF[n] = compressedInterf->iDispFringe[R][X][f];
00596 n++;
00597 }
00598 }
00599 sprintf (fileName, "InterfCleanDATA%d_X%d", R+2, X+1);
00600 sprintf (title, "Interferometry DATA%d for Channel %d (Sky Removed)", R+2, X+1);
00601 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00602 }
00603 }
00604 for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00605 {
00606 for (X = 0; X < formatPhotomA->iXWidth; X++)
00607 {
00608 if (badChannelList[X])
00609 continue;
00610
00611 n = 0;
00612 for (f = 0; f < formatPhotomA->numOfFrames; f++)
00613 {
00614 if (compressedPhotomA->tarType[f] == 'T' && !(compressedPhotomA->rejectList[X][f]))
00615 {
00616 arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
00617 n++;
00618 }
00619 }
00620 sprintf (fileName, "PhotomACleanDATA1_X%d", X+1);
00621 sprintf (title, "Photometry A DATA1 for Channel %d (Sky Removed)", X+1);
00622 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00623 }
00624 }
00625 for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00626 {
00627 for (X = 0; X < formatPhotomB->iXWidth; X++)
00628 {
00629 if (badChannelList[X])
00630 continue;
00631
00632 n = 0;
00633 for (f = 0; f < formatPhotomB->numOfFrames; f++)
00634 {
00635 if (compressedPhotomB->tarType[f] == 'T' && !(compressedPhotomB->rejectList[X][f]))
00636 {
00637 arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
00638 n++;
00639 }
00640 }
00641 sprintf (fileName, "PhotomBCleanDATA4_X%d", X+1);
00642 sprintf (title, "Photometry B DATA4 for Channel %d (Sky Removed)", X+1);
00643 midiCreatePlotFile2D (fileName, title, "Frame", "Flux", 1, arrayF, 0, n, 1, 0);
00644 }
00645 }
00646 free (fileName);
00647 free (title);
00648 free (arrayF);
00649 }
00650
00651
00652
00653
00654
00655 if (plotFile)
00656 {
00657 arrayF = (float *) calloc (formatInterf->iXWidth, sizeof (float));
00658 fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00659 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00660
00661 for (R = 0; R < formatInterf->numOfRegionsToProcess; R++)
00662 {
00663 for (X = 0; X < formatInterf->iXWidth; X++)
00664 {
00665 arrayF[X]=0;
00666 if (badChannelList[X])
00667 continue;
00668
00669 n = 0;
00670 for (f = 0; f < formatInterf->numOfFrames; f++)
00671 {
00672 if (compressedInterf->tarType[f] == 'T' && !(compressedInterf->rejectList[X][f]))
00673 {
00674 arrayF[X] += compressedInterf->iDispFringe[R][X][f];
00675 n++;
00676 }
00677 }
00678 arrayF[X] /=(float)n;
00679 }
00680
00681 sprintf (fileName, "MeanInterfCleanDATA%d", R+1);
00682 sprintf (title, "Mean Interferometry DATA%d (Sky Removed)", R+1);
00683 midiCreatePlotFile2D (fileName, title, "Channel", "Flux", 0, arrayF, 0, formatInterf->iXWidth, 1, 0);
00684 }
00685 free (fileName);
00686 free (title);
00687 free (arrayF);
00688 }
00689
00690
00691
00692 finaliseRejectListInterf ("Combined Interferometry", formatInterf, compressedInterf, error);
00693 if (*error) return;
00694 finaliseRejectListPhotom ("Photometry A", formatPhotomA, compressedPhotomA, error);
00695 if (*error) return;
00696 finaliseRejectListPhotom ("Photometry B", formatPhotomB, compressedPhotomB, error);
00697 if (*error) return;
00698
00699 return;
00700
00701 }
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718 void applyOpticalCompensation (
00719 MidiFiles *fileNames,
00720 ImageFormat *format,
00721 CompressedData *compressedInterf,
00722 CompressedData *compressedPhotomA,
00723 CompressedData *compressedPhotomB,
00724 int *error,
00725 cpl_parameterlist *parlist,
00726 cpl_frameset *frameset)
00727 {
00728
00729
00730
00731 const char routine[] = "applyOpticalCompensation";
00732
00733 TransferRatios *trr;
00734 int i, X, n;
00735
00736
00737 char * tag=NULL;
00738 char * softag=NULL;
00739 cpl_frame * cur_frame;
00740 cpl_table * table;
00741 float * kappamatrix11=NULL;
00742 float * kappamatrix12=NULL;
00743 float * kappamatrix21=NULL;
00744 float * kappamatrix22=NULL;
00745 float * kappamatrix11_error=NULL;
00746 float * kappamatrix12_error=NULL;
00747 float * kappamatrix21_error=NULL;
00748 float * kappamatrix22_error=NULL;
00749 int dimenkappamatrix=0;
00750 cpl_errorstate prestate = cpl_errorstate_get();
00751
00752
00753
00754
00755 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00756 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00757
00758
00759 *error = 0;
00760
00761
00762 cpl_msg_info(cpl_func,"\nApplying Optical Compensation for batch %d \n", batchNumber);
00763 cpl_msg_info(cpl_func,"--------------------------------------- \n");
00764 fprintf (midiReportPtr, "\nApplying Optical Compensation for batch %d \n", batchNumber);
00765 fprintf (midiReportPtr, "--------------------------------------- \n");
00766
00767
00768
00769 {
00770
00771 if (strcmp(format->grismId,"GRISM")==0){
00772 softag = cpl_sprintf("%s", "MIDI_KAPPAMATRIX_GRISM");
00773 }
00774 if (strcmp(format->grismId,"PRISM")==0){
00775 softag = cpl_sprintf("%s", "MIDI_KAPPAMATRIX_PRISM");
00776 }
00777
00778
00779 cur_frame = cpl_frameset_get_first(frameset);
00780 if (cur_frame == NULL) {
00781 cpl_msg_error(cpl_func, "No frame found in the SOF");
00782 return ;
00783
00784 }
00785
00786 while(cur_frame)
00787 {
00788
00789 tag = (char*)cpl_frame_get_tag(cur_frame);
00790 if (strcmp(tag, softag)) {
00791 cur_frame = cpl_frameset_get_next( frameset );
00792 continue;
00793 }
00794 else{
00795 cpl_msg_info(cpl_func, "Kappamatrix found in the SOF: %s\n",cpl_frame_get_filename(cur_frame));
00796 break;
00797 }
00798
00799 }
00800
00801 if (strcmp(tag, softag)) {
00802 cpl_msg_info(cpl_func, "No Kappamatrix found in the SOF!");
00803 cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00804 *error = 1;
00805 return;
00806 }
00807
00808 cpl_free(softag);
00809
00810
00811
00812 prestate = cpl_errorstate_get();
00813 if((table = cpl_table_load(cpl_frame_get_filename(cur_frame), 1, 1))==NULL)
00814 {
00815 cpl_msg_info(cpl_func, "No siutable table found in the file %s\n",cpl_frame_get_filename(cur_frame));
00816 cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00817 cpl_errorstate_set(prestate);
00818 *error = 1;
00819 return;
00820 }
00821
00822
00823
00824 if (cpl_table_has_column(table, "kappam11"))
00825 {
00826 dimenkappamatrix=cpl_table_get_column_dimensions(table, "kappam11");
00827 kappamatrix11=cpl_table_get_data_float(table, "kappam11");
00828 kappamatrix12=cpl_table_get_data_float(table, "kappam12");
00829 kappamatrix21=cpl_table_get_data_float(table, "kappam21");
00830 kappamatrix22=cpl_table_get_data_float(table, "kappam22");
00831 kappamatrix11_error=cpl_table_get_data_float(table, "kappam11_error");
00832 kappamatrix12_error=cpl_table_get_data_float(table, "kappam12_error");
00833 kappamatrix21_error=cpl_table_get_data_float(table, "kappam21_error");
00834 kappamatrix22_error=cpl_table_get_data_float(table, "kappam22_error");
00835
00836 }
00837 else{
00838 cpl_msg_info(cpl_func, "No siutable table column found in the file %s\n",cpl_frame_get_filename(cur_frame));
00839 cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00840 *error = 1;
00841 return;
00842 }
00843
00844 trr = callocTransferRatios (format->iXWidth);
00845
00846 cpl_msg_info(cpl_func,"Dimension: %d",dimenkappamatrix);
00847
00848 for (i=0; i<format->iXWidth;i++){
00849
00850 trr->ka1[i]=kappamatrix11[i] ;
00851 trr->siga1[i]=kappamatrix11_error[i] ;
00852 trr->ka2[i]=kappamatrix12[i] ;
00853 trr->siga2[i]=kappamatrix12_error[i] ;
00854 trr->kb1[i]=kappamatrix21[i] ;
00855 trr->sigb1[i]=kappamatrix21_error[i] ;
00856 trr->kb2[i]=kappamatrix22[i] ;
00857 trr->sigb2[i]=kappamatrix22_error[i] ;
00858 cpl_msg_info(cpl_func,"%3d %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f \n", i,
00859 trr->ka1[i], trr->siga1[i], trr->ka2[i], trr->siga2[i],
00860 trr->kb1[i], trr->sigb1[i], trr->kb2[i], trr->sigb2[i]);
00861 }
00862
00863 for (i=0; i<format->iXWidth;i++){
00864 if (!(trr->ka1[i]) || !(trr->ka2[i]) || !(trr->kb1[i]) || !(trr->kb2[i]))
00865 {
00866 badChannelList[i] |= BSL_DATA_ERROR;
00867 cpl_msg_info(cpl_func,"%3d <-- %s --> \n", i, UNAV);
00868 fprintf (midiReportPtr, "%3d <-- %s --> QCLOG \n", i, UNAV);
00869 }
00870 }
00871
00872
00873
00874
00875 n = 0;
00876 for (X = 0; X < format->iXWidth; X++)
00877 {
00878 if (!(badChannelList[X]))
00879 n++;
00880 }
00881 if (!n)
00882 {
00883 cpl_msg_warning(cpl_func, "Cannot find any valid Transfer Ratios");
00884 cpl_msg_warning(cpl_func, "Subsequent computational values are without Optical Compensation");
00885 *error = 1;
00886 freeTransferRatios (trr);
00887 return;
00888 }
00889
00890
00891 getCompensatedBeams (trr, format, compressedInterf, compressedPhotomA, compressedPhotomB);
00892 freeTransferRatios (trr);
00893 cpl_table_delete(table);
00894
00895 return;
00896 }
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042 }
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058 void getCompensatedBeams (
01059 TransferRatios *trr,
01060 ImageFormat *format,
01061 CompressedData *compressedInterf,
01062 CompressedData *compressedPhotomA,
01063 CompressedData *compressedPhotomB)
01064 {
01065
01066
01067
01068 const char routine[] = "getCompensatedBeams";
01069 int f, X, R;
01070 double compensate;
01071
01072
01073
01074 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
01075 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
01076
01077
01078 for (R = 0; R < format->numOfRegionsToProcess; R++)
01079 {
01080
01081 for (X = 0; X < format->iXWidth; X++)
01082 {
01083 if (badChannelList[X])
01084 continue;
01085
01086 for (f = 0; f < format->numOfFrames; f++)
01087 {
01088
01089 if (!((compressedInterf->rejectList[X])[f]) )
01090 {
01091
01092
01093
01094
01095
01096
01097 if (R == 0)
01098 {
01099 if(trr->ka1[X] > 0 && trr->ka2[X] > 0 && trr->kb1[X] > 0 && trr->kb2[X] > 0)
01100 {
01101
01102
01103
01104
01105
01106 ((compressedPhotomA->iDispFringe[0])[X])[f]=(((compressedPhotomA->iDispFringe[0])[X])[f])*(trr->ka1[X] + trr->kb1[X]);
01107 ((compressedPhotomB->iDispFringe[0])[X])[f]=(((compressedPhotomB->iDispFringe[0])[X])[f])*(trr->ka2[X] + trr->kb2[X]);
01108 }
01109 else
01110 {
01111 ((compressedPhotomA->iDispFringe[0])[X])[f]=(((compressedPhotomA->iDispFringe[0])[X])[f]);
01112 ((compressedPhotomB->iDispFringe[0])[X])[f]=(((compressedPhotomB->iDispFringe[0])[X])[f]);
01113 }
01114
01115
01116
01117 }
01118 continue;
01119
01120 if (R == 0)
01121 {
01122 compensate=1. / ( (sqrt(((compressedPhotomA->iDispFringe[R])[X])[f] *
01123 ((compressedPhotomB->iDispFringe[R])[X])[f])) *
01124 (sqrt(trr->ka1[X] * trr->kb1[X])) );
01125 if(isnan(compensate) || isinf(compensate))
01126 {
01127 compensate=0.;
01128
01129
01130 }
01131 ((compressedInterf->iDispFringe[R])[X])[f] = ((compressedInterf->iDispFringe[R])[X])[f] * compensate;
01132 }
01133 else
01134 {
01135 compensate=1. / ( (sqrt(((compressedPhotomA->iDispFringe[R-1])[X])[f] *
01136 ((compressedPhotomB->iDispFringe[R-1])[X])[f])) *
01137 (sqrt(trr->ka2[X] * trr->kb2[X])) );
01138
01139 if(isnan(compensate) || isinf(compensate))
01140
01141 {
01142 compensate=0.;
01143
01144
01145 }
01146
01147 ((compressedInterf->iDispFringe[R])[X])[f] =
01148 ((compressedInterf->iDispFringe[R])[X])[f] * compensate;
01149
01150 }
01151 }
01152 else
01153 {
01154 if (diagnostic > 2)
01155 {
01156 sprintf (midiMessage, "Interferometry data in region %d, channel %3d, frame %4d cannot be compensated",
01157 R, X, f);
01158 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01159 }
01160 }
01161 }
01162 }
01163 }
01164
01165 return;
01166 }
01167