21 #include <sys/types.h>
29 #include "midiGlobal.h"
32 #include "memoryHandling.h"
33 #include "errorHandling.h"
34 #include "diagnostics.h"
35 #include "imageProcessing.h"
36 #include "calibration.h"
37 #include "statistics.h"
38 #include "fitsAnalysisKappa.h"
39 #include "preProcKappa.h"
40 #include "procKappa.h"
41 #include "procFrgHS.h"
42 #include "createProdKappa.h"
94 const char routine[] =
"procKappa";
95 ImageFormat *formatInterfA=NULL, *formatInterfB=NULL,
96 *formatPhotomA=NULL, *formatPhotomB=NULL;
97 CompressedData *compressedInterfA=NULL, *compressedInterfB=NULL,
98 *compressedPhotomA=NULL, *compressedPhotomB=NULL;
100 FilterData *filterInfo=NULL;
104 if (diagnostic > 4)cpl_msg_info(cpl_func,
"Invoking routine '%s' \n", routine);
105 if (diagnostic > 4) fprintf(midiReportPtr,
"Invoking routine '%s' \n", routine);
109 if (options->processing == UNDISPERSED)
112 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
113 "'UNDISPERSED' is an invalid processing request for SCI_PHOT data");
118 formatInterfA = callocImageFormat ();
119 formatInterfB = callocImageFormat ();
120 formatPhotomA = callocImageFormat ();
121 formatPhotomB = callocImageFormat ();
125 analyseFitsKappa (fileNames, formatInterfA, formatInterfB,
126 formatPhotomA, formatPhotomB, error);
129 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Cannot analyse FITS files");
130 freeImageFormat (formatInterfA);
131 freeImageFormat (formatInterfB);
132 freeImageFormat (formatPhotomA);
133 freeImageFormat (formatPhotomB);
140 compressedInterfA = callocMidiCompressed (formatInterfA);
141 compressedInterfB = callocMidiCompressed (formatInterfB);
142 compressedPhotomA = callocMidiCompressed (formatPhotomA);
143 compressedPhotomB = callocMidiCompressed (formatPhotomB);
144 badChannelList = (
int *) calloc (formatInterfA->iXWidth, sizeof (
int));
145 filterInfo = callocFilterInfo();
148 preProcKappa (options, filterInfo, fileNames, compressedInterfA, compressedInterfB,
149 compressedPhotomA, compressedPhotomB, formatInterfA, formatInterfB, formatPhotomA,
150 formatPhotomB, error);
153 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Cannot compress FITS files");
154 freeCompressedData (formatInterfA, compressedInterfA);
155 freeCompressedData (formatInterfB, compressedInterfB);
156 freeCompressedData (formatPhotomA, compressedPhotomA);
157 freeCompressedData (formatPhotomB, compressedPhotomB);
158 free (badChannelList);
159 freeImageFormat (formatInterfA);
160 freeImageFormat (formatInterfB);
161 freeImageFormat (formatPhotomA);
162 freeImageFormat (formatPhotomB);
163 freeFilterInfo (filterInfo);
168 checkDelayLineJumps (
"INTERFA", formatInterfA, compressedInterfA);
169 checkDelayLineJumps (
"INTERFB", formatInterfB, compressedInterfB);
170 checkDelayLineJumps (
"PHOTOMA", formatPhotomA, compressedPhotomA);
171 checkDelayLineJumps (
"PHOTOMB", formatPhotomB, compressedPhotomB);
174 checkDelayLineConsistencies (
"INTERFA",
"PHOTOMA", compressedInterfA, compressedPhotomA,
175 formatInterfA, formatPhotomA, error);
176 checkDelayLineConsistencies (
"INTERFB",
"PHOTOMB", compressedInterfB, compressedPhotomB,
177 formatInterfB, formatPhotomB, error);
180 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Kappa calculation abandonned");
181 freeCompressedData (formatInterfA, compressedInterfA);
182 freeCompressedData (formatInterfB, compressedInterfB);
183 freeCompressedData (formatPhotomA, compressedPhotomA);
184 freeCompressedData (formatPhotomB, compressedPhotomB);
185 free (badChannelList);
186 freeImageFormat (formatInterfA);
187 freeImageFormat (formatInterfB);
188 freeImageFormat (formatPhotomA);
189 freeImageFormat (formatPhotomB);
190 freeFilterInfo (filterInfo);
196 reportInterfChopping (
"INTERFA", formatInterfA, compressedInterfA);
197 reportInterfChopping (
"INTERFB", formatInterfB, compressedInterfB);
198 reportPhotomChopping (
"PHOTOMA", formatPhotomA, compressedPhotomA);
199 reportPhotomChopping (
"PHOTOMB", formatPhotomB, compressedPhotomB);
200 displayInterfChoppingDisp (
"INTERFA", formatInterfA, compressedInterfA);
201 displayInterfChoppingDisp (
"INTERFB", formatInterfB, compressedInterfB);
202 displayPhotomChoppingDisp (
"PHOTOMA", formatPhotomA, compressedPhotomA);
203 displayPhotomChoppingDisp (
"PHOTOMB", formatPhotomB, compressedPhotomB);
206 cleanupKappa (options->processing, formatInterfA, formatInterfB, formatPhotomA, formatPhotomB,
207 compressedInterfA, compressedInterfB, compressedPhotomA, compressedPhotomB, error);
210 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Kappa calculation abandonned");
211 freeCompressedData (formatInterfA, compressedInterfA);
212 freeCompressedData (formatInterfB, compressedInterfB);
213 freeCompressedData (formatPhotomA, compressedPhotomA);
214 freeCompressedData (formatPhotomB, compressedPhotomB);
215 free (badChannelList);
216 freeImageFormat (formatInterfA);
217 freeImageFormat (formatInterfB);
218 freeImageFormat (formatPhotomA);
219 freeImageFormat (formatPhotomB);
220 freeFilterInfo (filterInfo);
225 trr = callocTransferRatios (formatInterfA->iXWidth);
226 midiComputeKappaCoeff (compressedInterfA, compressedInterfB, compressedPhotomA,
227 compressedPhotomB, formatInterfA, formatInterfB, formatPhotomA, formatPhotomB, trr, error);
230 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Kappa calculation abandonned");
231 freeCompressedData (formatInterfA, compressedInterfA);
232 freeCompressedData (formatInterfB, compressedInterfB);
233 freeCompressedData (formatPhotomA, compressedPhotomA);
234 freeCompressedData (formatPhotomB, compressedPhotomB);
235 free (badChannelList);
236 freeImageFormat (formatInterfA);
237 freeImageFormat (formatInterfB);
238 freeImageFormat (formatPhotomA);
239 freeImageFormat (formatPhotomB);
240 freeTransferRatios (trr);
241 freeFilterInfo (filterInfo);
247 reportRejectList (formatInterfA, compressedInterfA);
256 freeCompressedData (formatInterfA, compressedInterfA);
257 freeCompressedData (formatInterfB, compressedInterfB);
258 freeCompressedData (formatPhotomA, compressedPhotomA);
259 freeCompressedData (formatPhotomB, compressedPhotomB);
260 free (badChannelList);
261 freeImageFormat (formatInterfA);
262 freeImageFormat (formatInterfB);
263 freeImageFormat (formatPhotomA);
264 freeImageFormat (formatPhotomB);
265 freeTransferRatios (trr);
266 freeFilterInfo (filterInfo);
285 enum ProcessingMode processing,
286 ImageFormat *formatInterfA,
287 ImageFormat *formatInterfB,
288 ImageFormat *formatPhotomA,
289 ImageFormat *formatPhotomB,
290 CompressedData *compressedInterfA,
291 CompressedData *compressedInterfB,
292 CompressedData *compressedPhotomA,
293 CompressedData *compressedPhotomB,
299 const char routine[] =
"cleanupKappa";
300 int transitions, f, X, R, n, localError;
301 double *array, fluxAve;
303 char *fileName, *title;
307 if (diagnostic > 4)cpl_msg_info(cpl_func,
"Invoking routine '%s' \n", routine);
308 if (diagnostic > 4) fprintf(midiReportPtr,
"Invoking routine '%s' \n", routine);
310 cpl_msg_info(cpl_func,
"\nCleaning up KAPPA files for batch %d \n", batchNumber);
311 cpl_msg_info(cpl_func,
"--------------------------------- \n");
312 fprintf (midiReportPtr,
"\nCleaning up KAPPA files for batch %d \n", batchNumber);
313 fprintf (midiReportPtr,
"--------------------------------- \n");
322 transitions = countTransitions (
'T', formatInterfA->numOfFrames, compressedInterfA->tarType);
323 sprintf (midiMessage,
"Target Transitions for Interf A = %d", transitions);
324 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
325 if (transitions < 10)
327 sprintf (midiMessage,
"Interferometry A data is not chopped");
328 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
334 transitions = countTransitions (
'T', formatInterfB->numOfFrames, compressedInterfB->tarType);
335 sprintf (midiMessage,
"Target Transitions for Interf B = %d", transitions);
336 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
337 if (transitions < 10)
339 sprintf (midiMessage,
"Interferometry B data is not chopped");
340 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
346 transitions = countTransitions (
'T', formatPhotomA->numOfFrames, compressedPhotomA->tarType);
347 sprintf (midiMessage,
"Target Transitions for Photom A = %d", transitions);
348 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
349 if (transitions < 10)
351 sprintf (midiMessage,
"Photometry A data is not chopped");
352 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
358 transitions = countTransitions (
'T', formatPhotomB->numOfFrames, compressedPhotomB->tarType);
359 sprintf (midiMessage,
"Target Transitions for Photom B = %d", transitions);
360 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
361 if (transitions < 10)
363 sprintf (midiMessage,
"Photometry B data is not chopped");
364 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
371 formatInterfA->chopped = 1;
372 formatInterfB->chopped = 1;
373 formatPhotomA->chopped = 1;
374 formatPhotomB->chopped = 1;
378 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Cannot cleanup KAPPA files");
384 correctTarType (
"INTERFA", compressedInterfA->tarType, compressedInterfA->time, formatInterfA->numOfFrames, &localError);
385 if (localError) *error = 1;
386 correctTarType (
"INTERFB", compressedInterfB->tarType, compressedInterfB->time, formatInterfB->numOfFrames, &localError);
387 if (localError) *error = 1;
388 correctTarType (
"PHOTOMA", compressedPhotomA->tarType, compressedPhotomA->time, formatPhotomA->numOfFrames, &localError);
389 if (localError) *error = 1;
390 correctTarType (
"PHOTOMB", compressedPhotomB->tarType, compressedPhotomB->time, formatPhotomB->numOfFrames, &localError);
391 if (*error || localError)
399 removeSkyBackground (
"INTERFA", processing, formatInterfA, compressedInterfA, &localError);
400 if (localError) *error = 1;
401 removeSkyBackground (
"INTERFB", processing, formatInterfB, compressedInterfB, &localError);
402 if (localError) *error = 1;
403 removeSkyBackground (
"PHOTOMA", processing, formatPhotomA, compressedPhotomA, &localError);
404 if (localError) *error = 1;
405 removeSkyBackground (
"PHOTOMB", processing, formatPhotomB, compressedPhotomB, &localError);
406 if (formatInterfA->numOfFrames != formatPhotomA->numOfFrames ||
407 formatInterfA->numOfFrames != formatPhotomB->numOfFrames ||
408 formatInterfA->numOfFrames != formatInterfB->numOfFrames || *error || localError)
411 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Unequal numOfFrames");
416 for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
418 for (X = 0; X < formatInterfA->iXWidth; X++)
420 if (badChannelList[X])
423 for (f = 0; f < formatInterfA->numOfFrames; f++)
425 if ((compressedInterfA->tarType[f] ==
'T') &&
426 (((compressedInterfA->iDispFringe)[R])[X])[f] <= 0.0)
428 compressedInterfA->rejectList[X][f] |= BSL_DATA_ERROR;
431 sprintf (midiMessage,
"Negative Flux at: R %d X %3d F %4d InterfA %10.2f",
432 R, X, f, (((compressedInterfA->iDispFringe)[R])[X])[f]);
433 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
437 if (compressedInterfA->tarType[f] !=
'T')
438 compressedInterfA->rejectList[X][f] |= BSL_SKY_SCAN;
443 for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
445 for (X = 0; X < formatInterfB->iXWidth; X++)
447 if (badChannelList[X])
450 for (f = 0; f < formatInterfB->numOfFrames; f++)
452 if ((compressedInterfB->tarType[f] ==
'T') &&
453 (((compressedInterfB->iDispFringe)[R])[X])[f] <= 0.0)
455 compressedInterfB->rejectList[X][f] |= BSL_DATA_ERROR;
458 sprintf (midiMessage,
"Negative Flux at: R %d X %3d F %4d InterfB %10.2f",
459 R, X, f, (((compressedInterfB->iDispFringe)[R])[X])[f]);
460 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
463 if (compressedInterfB->tarType[f] !=
'T')
464 compressedInterfB->rejectList[X][f] |= BSL_SKY_SCAN;
469 for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
471 for (X = 0; X < formatPhotomA->iXWidth; X++)
473 if (badChannelList[X])
476 for (f = 0; f < formatPhotomA->numOfFrames; f++)
478 if ((compressedPhotomA->tarType[f] ==
'T') &&
479 (((compressedPhotomA->iDispFringe)[R])[X])[f] <= 0.0)
481 compressedPhotomA->rejectList[X][f] |= BSL_DATA_ERROR;
484 sprintf (midiMessage,
"Negative Flux at: R %d X %3d F %4d PhotomA %10.2f",
485 R, X, f, (((compressedPhotomA->iDispFringe)[R])[X])[f]);
486 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
489 if (compressedPhotomA->tarType[f] !=
'T')
490 compressedPhotomA->rejectList[X][f] |= BSL_SKY_SCAN;
495 for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
497 for (X = 0; X < formatPhotomB->iXWidth; X++)
499 if (badChannelList[X])
502 for (f = 0; f < formatPhotomB->numOfFrames; f++)
504 if ((compressedPhotomB->tarType[f] ==
'T') &&
505 (((compressedPhotomB->iDispFringe)[R])[X])[f] <= 0.0)
507 compressedPhotomB->rejectList[X][f] |= BSL_DATA_ERROR;
510 sprintf (midiMessage,
"Negative Flux at: R %d X %3d F %4d PhotomB %10.2f",
511 R, X, f, (((compressedPhotomB->iDispFringe)[R])[X])[f]);
512 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
515 if (compressedPhotomB->tarType[f] !=
'T')
516 compressedPhotomB->rejectList[X][f] |= BSL_SKY_SCAN;
522 if (plotFile && diagnostic > 1)
524 array = (
double *) calloc (formatInterfA->numOfFrames, sizeof (
double));
528 for (f = 0; f < formatInterfA->numOfFrames; f++)
530 if (compressedInterfA->tarType[f] ==
'T')
532 array[n] = compressedInterfA->iFringe1[f];
538 cpl_msg_info(cpl_func,
"InterfA DATA2 Average Flux = %f \n", fluxAve);
539 midiCreatePlotFileDouble2D (
"InterfACleanDATA2",
"Interferometry A DATA2 (Sky Removed)",
540 "Frame",
"Flux", 0, array, 0, n, 1);
544 for (f = 0; f < formatInterfA->numOfFrames; f++)
546 if (compressedInterfA->tarType[f] ==
'T')
548 array[n] = compressedInterfA->iFringe2[f];
554 cpl_msg_info(cpl_func,
"InterfA DATA3 Average Flux = %f \n", fluxAve);
555 midiCreatePlotFileDouble2D (
"InterfACleanDATA3",
"Interferometry A DATA3 (Sky Removed)",
556 "Frame",
"Flux", 0, array, 0, n, 1);
559 for (f = 0; f < formatInterfA->numOfFrames; f++)
561 if (compressedInterfA->tarType[f] ==
'T')
563 array[n] = compressedInterfA->iFringe[f];
567 midiCreatePlotFileDouble2D (
"InterfACleanDATA2_DATA3",
"Interferometry A DATA2-DATA3 (Sky Removed)",
568 "Frame",
"Flux", 0, array, 0, n, 1);
572 for (f = 0; f < formatInterfB->numOfFrames; f++)
574 if (compressedInterfB->tarType[f] ==
'T')
576 array[n] = compressedInterfB->iFringe1[f];
582 cpl_msg_info(cpl_func,
"InterfB DATA2 Average Flux = %f \n", fluxAve);
583 midiCreatePlotFileDouble2D (
"InterfBCleanDATA2",
"Interferometry B DATA2 (Sky Removed)",
584 "Frame",
"Flux", 0, array, 0, n, 1);
588 for (f = 0; f < formatInterfB->numOfFrames; f++)
590 if (compressedInterfB->tarType[f] ==
'T')
592 array[n] = compressedInterfB->iFringe2[f];
598 cpl_msg_info(cpl_func,
"InterfB DATA3 Average Flux = %f \n", fluxAve);
599 midiCreatePlotFileDouble2D (
"InterfBCleanDATA3",
"Interferometry B DATA3 (Sky Removed)",
600 "Frame",
"Flux", 0, array, 0, n, 1);
603 for (f = 0; f < formatInterfB->numOfFrames; f++)
605 if (compressedInterfB->tarType[f] ==
'T')
607 array[n] = compressedInterfB->iFringe[f];
611 midiCreatePlotFileDouble2D (
"InterfBCleanDATA2_DATA3",
"Interferometry B DATA2-DATA3 (Sky Removed)",
612 "Frame",
"Flux", 0, array, 0, n, 1);
616 for (f = 0; f < formatPhotomA->numOfFrames; f++)
618 if (compressedPhotomA->tarType[f] ==
'T')
620 array[n] = compressedPhotomA->iFringe1[f];
626 cpl_msg_info(cpl_func,
"PhotomA DATA1 Average Flux = %f \n", fluxAve);
627 midiCreatePlotFileDouble2D (
"PhotomACleanDATA1",
"Photometry A DATA1 (Sky Removed)",
628 "Frame",
"Flux", 0, array, 0, n, 1);
632 for (f = 0; f < formatPhotomB->numOfFrames; f++)
634 if (compressedPhotomB->tarType[f] ==
'T')
636 array[n] = compressedPhotomB->iFringe1[f];
642 cpl_msg_info(cpl_func,
"PhotomB DATA4 Average Flux = %f \n", fluxAve);
643 midiCreatePlotFileDouble2D (
"PhotomBCleanDATA4",
"Photometry B DATA4 (Sky Removed)",
644 "Frame",
"Flux", 0, array, 0, n, 1);
650 if (plotFile && diagnostic > 4)
652 arrayF = (
float *) calloc (formatInterfA->numOfFrames, sizeof (
float));
653 fileName = (
char *) calloc (MAX_STRING_LENGTH,
sizeof (
char));
654 title = (
char *) calloc (MAX_STRING_LENGTH,
sizeof (
char));
656 for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
658 for (X = 0; X < formatInterfA->iXWidth; X++)
660 if (badChannelList[X])
664 for (f = 0; f < formatInterfA->numOfFrames; f++)
666 if (compressedInterfA->tarType[f] ==
'T' && !(compressedInterfA->rejectList[X][f]))
668 arrayF[n] = compressedInterfA->iDispFringe[R][X][f];
672 sprintf (fileName,
"InterfACleanDATA%d_X%d", R+2, X+1);
673 sprintf (title,
"Interferometry A DATA%d for Channel %d (Sky Removed)", R+2, X+1);
674 midiCreatePlotFile2D (fileName, title,
"Frame",
"Flux", 1, arrayF, 0, n, 1, 0);
677 for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
679 for (X = 0; X < formatInterfB->iXWidth; X++)
681 if (badChannelList[X])
685 for (f = 0; f < formatInterfB->numOfFrames; f++)
687 if (compressedInterfB->tarType[f] ==
'T' && !(compressedInterfB->rejectList[X][f]))
689 arrayF[n] = compressedInterfB->iDispFringe[R][X][f];
693 sprintf (fileName,
"InterfBCleanDATA%d_X%d", R+2, X+1);
694 sprintf (title,
"Interferometry B DATA%d for Channel %d (Sky Removed)", R+2, X+1);
695 midiCreatePlotFile2D (fileName, title,
"Frame",
"Flux", 1, arrayF, 0, n, 1, 0);
698 for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
700 for (X = 0; X < formatPhotomA->iXWidth; X++)
702 if (badChannelList[X])
706 for (f = 0; f < formatPhotomA->numOfFrames; f++)
708 if (compressedPhotomA->tarType[f] ==
'T' && !(compressedPhotomA->rejectList[X][f]))
710 arrayF[n] = compressedPhotomA->iDispFringe[R][X][f];
714 sprintf (fileName,
"PhotomACleanDATA1_X%d", X+1);
715 sprintf (title,
"Photometry A DATA1 for Channel %d (Sky Removed)", X+1);
716 midiCreatePlotFile2D (fileName, title,
"Frame",
"Flux", 1, arrayF, 0, n, 1, 0);
719 for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
721 for (X = 0; X < formatPhotomB->iXWidth; X++)
723 if (badChannelList[X])
727 for (f = 0; f < formatPhotomB->numOfFrames; f++)
729 if (compressedPhotomB->tarType[f] ==
'T' && !(compressedPhotomB->rejectList[X][f]))
731 arrayF[n] = compressedPhotomB->iDispFringe[R][X][f];
735 sprintf (fileName,
"PhotomBCleanDATA4_X%d", X+1);
736 sprintf (title,
"Photometry B DATA4 for Channel %d (Sky Removed)", X+1);
737 midiCreatePlotFile2D (fileName, title,
"Frame",
"Flux", 1, arrayF, 0, n, 1, 0);
746 finaliseRejectListPhotom (
"Interferometry A", formatInterfA, compressedInterfA, error);
748 finaliseRejectListPhotom (
"Interferometry B", formatInterfB, compressedInterfB, error);
750 finaliseRejectListPhotom (
"Photometry A", formatPhotomA, compressedPhotomA, error);
752 finaliseRejectListPhotom (
"Photometry B", formatPhotomB, compressedPhotomB, error);
813 void midiComputeKappaCoeff (
814 CompressedData *compressedIA,
815 CompressedData *compressedIB,
816 CompressedData *compressedPA,
817 CompressedData *compressedPB,
818 ImageFormat *formatIA,
819 ImageFormat *formatIB,
820 ImageFormat *formatPA,
821 ImageFormat *formatPB,
828 const char routine[] =
"midiComputeKappaCoeff";
829 int f, x, numOfFrames, nzc, minFrames;
830 float *kappArray, *totalFluxA, *totalFluxB, variance;
831 KappaCoefficients *kc;
836 if (diagnostic > 4)cpl_msg_info(cpl_func,
"Invoking routine '%s' \n", routine);
837 if (diagnostic > 4) fprintf(midiReportPtr,
"Invoking routine '%s' \n", routine);
839 cpl_msg_info(cpl_func,
"\nComputing the Kappa Coefficients for batch %d \n", batchNumber);
840 cpl_msg_info(cpl_func,
"------------------------------------------ \n");
841 fprintf (midiReportPtr,
"\nComputing the Kappa Coefficients for batch %d \n", batchNumber);
842 fprintf (midiReportPtr,
"------------------------------------------ \n");
848 if ((formatIA->numOfFrames != formatIB->numOfFrames) ||
849 (formatIA->numOfFrames != formatPA->numOfFrames) ||
850 (formatIA->numOfFrames != formatPB->numOfFrames) ||
851 (formatPA->numOfFrames != formatPB->numOfFrames))
854 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Inconsistent frame counts");
857 numOfFrames = formatIA->numOfFrames;
858 minFrames = MIN_FRAMES_PER_SCAN * compressedIA->numOfTargetChops;
862 kappArray = (
float *) calloc (numOfFrames,
sizeof (
float));
863 totalFluxA = (
float *) calloc (numOfFrames,
sizeof (
float));
864 totalFluxB = (
float *) calloc (numOfFrames,
sizeof (
float));
865 string = (
char *) calloc (MAX_STRING_LENGTH,
sizeof (
char));
866 kc = callocKappaCoefficients (formatIA->iXWidth);
870 cpl_msg_info(cpl_func,
"Channel k11 sig11 k21 sig21 k31 sig31 k22 sig22 k32 sig32 k42 sig42 \n");
871 fprintf (midiReportPtr,
"Channel k11 sig11 k21 sig21 k31 sig31 k22 sig22 k32 sig32 k42 sig42 \n");
872 for (x = 0; x < formatIA->iXWidth; x++)
874 if (badChannelList[x])
876 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
877 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
882 for (f = 0; f < numOfFrames; f++)
884 if ((compressedIA->tarType[f] ==
'T') &&
885 (compressedPA->tarType[f] ==
'T') &&
886 !(compressedIA->rejectList[x][f]) &&
887 !(compressedPA->rejectList[x][f]))
889 totalFluxA[f] = compressedIA->iDispFringe[0][x][f] + compressedIA->iDispFringe[1][x][f] +
890 compressedPA->iDispFringe[0][x][f];
893 if ((compressedIB->tarType[f] ==
'T') &&
894 (compressedPB->tarType[f] ==
'T') &&
895 !(compressedIB->rejectList[x][f]) &&
896 !(compressedPB->rejectList[x][f]))
898 totalFluxB[f] = compressedIB->iDispFringe[0][x][f] + compressedIB->iDispFringe[1][x][f] +
899 compressedPB->iDispFringe[0][x][f];
905 for (f = 0; f < numOfFrames; f++)
907 if ((compressedPA->tarType[f] ==
'T') &&
908 (((compressedPA->iDispFringe[0])[x])[f] > 0.0) &&
909 (totalFluxA[f] > 0.0) &&
910 !(compressedPA->rejectList[x][f]))
913 kappArray[nzc] = ((compressedPA->iDispFringe[0])[x])[f] / totalFluxA[f];
918 kc->k11[x] = signalMean (kappArray, 0, nzc);
919 variance = signalVariance (kappArray, 0, nzc, &(kc->sig11[x]));
920 if (plotFile && diagnostic > 2)
922 sprintf (
string,
"Kappa11 Array, Channel %d", x);
923 midiCreatePlotFile2D (
"Kappa11Array",
string,
"Frame",
"Flux", 1, kappArray, 0, nzc, 1, 1);
928 badChannelList[x] |= BSL_DATA_ERROR;
929 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
930 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
936 for (f = 0; f < numOfFrames; f++)
938 if ((compressedIA->tarType[f] ==
'T') &&
939 (((compressedIA->iDispFringe[0])[x])[f] > 0.0) &&
940 (totalFluxA[f] > 0.0) &&
941 !(compressedIA->rejectList[x][f]))
944 kappArray[nzc] = ((compressedIA->iDispFringe[0])[x])[f] / totalFluxA[f];
949 kc->k21[x] = signalMean (kappArray, 0, nzc);
950 variance = signalVariance (kappArray, 0, nzc, &(kc->sig21[x]));
951 if (plotFile && diagnostic > 2)
953 sprintf (
string,
"Kappa21 Array, Channel %d", x);
954 midiCreatePlotFile2D (
"Kappa21Array",
string,
"Frame",
"Flux", 1, kappArray, 0, nzc, 1, 1);
959 badChannelList[x] |= BSL_DATA_ERROR;
960 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
961 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
967 for (f = 0; f < numOfFrames; f++)
969 if ((compressedIA->tarType[f] ==
'T') &&
970 (((compressedIA->iDispFringe[1])[x])[f] > 0.0) &&
971 (totalFluxA[f] > 0.0) &&
972 !(compressedIA->rejectList[x][f]))
975 kappArray[nzc] = ((compressedIA->iDispFringe[1])[x])[f] / totalFluxA[f];
980 kc->k31[x] = signalMean (kappArray, 0, nzc);
981 variance = signalVariance (kappArray, 0, nzc, &(kc->sig31[x]));
982 if (plotFile && diagnostic > 2)
984 sprintf (
string,
"Kappa31 Array, Channel %d", x);
985 midiCreatePlotFile2D (
"Kappa31Array",
string,
"Frame",
"Flux", 1, kappArray, 0, nzc, 1, 1);
990 badChannelList[x] |= BSL_DATA_ERROR;
991 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
992 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
998 for (f = 0; f < numOfFrames; f++)
1000 if ((compressedIB->tarType[f] ==
'T') &&
1001 (((compressedIB->iDispFringe[0])[x])[f] > 0.0) &&
1002 (totalFluxB[f] > 0.0) &&
1003 !(compressedIB->rejectList[x][f]))
1006 kappArray[nzc] = ((compressedIB->iDispFringe[0])[x])[f] / totalFluxB[f];
1009 if (nzc > minFrames)
1011 kc->k22[x] = signalMean (kappArray, 0, nzc);
1012 variance = signalVariance (kappArray, 0, nzc, &(kc->sig22[x]));
1013 if (plotFile && diagnostic > 2)
1015 sprintf (
string,
"Kappa22 Array, Channel %d", x);
1016 midiCreatePlotFile2D (
"Kappa22Array",
string,
"Frame",
"Flux", 1, kappArray, 0, nzc, 1, 1);
1021 badChannelList[x] |= BSL_DATA_ERROR;
1022 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
1023 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
1029 for (f = 0; f < numOfFrames; f++)
1031 if ((compressedIB->tarType[f] ==
'T') &&
1032 (((compressedIB->iDispFringe[1])[x])[f] > 0.0) &&
1033 (totalFluxB[f] > 0.0) &&
1034 !(compressedIB->rejectList[x][f]))
1037 kappArray[nzc] = ((compressedIB->iDispFringe[1])[x])[f] / totalFluxB[f];
1040 if (nzc > minFrames)
1042 kc->k32[x] = signalMean (kappArray, 0, nzc);
1043 variance = signalVariance (kappArray, 0, nzc, &(kc->sig32[x]));
1044 if (plotFile && diagnostic > 2)
1046 sprintf (
string,
"Kappa32 Array, Channel %d", x);
1047 midiCreatePlotFile2D (
"Kappa32Array",
string,
"Frame",
"Flux", 1, kappArray, 0, nzc, 1, 1);
1052 badChannelList[x] |= BSL_DATA_ERROR;
1053 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
1054 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
1060 for (f = 0; f < numOfFrames; f++)
1062 if ((compressedPB->tarType[f] ==
'T') &&
1063 (((compressedPB->iDispFringe[0])[x])[f] > 0.0) &&
1064 (totalFluxB[f] > 0.0) &&
1065 !(compressedPB->rejectList[x][f]))
1068 kappArray[nzc] = ((compressedPB->iDispFringe[0])[x])[f] / totalFluxB[f];
1071 if (nzc > minFrames)
1073 kc->k42[x] = signalMean (kappArray, 0, nzc);
1074 variance = signalVariance (kappArray, 0, nzc, &(kc->sig42[x]));
1075 if (plotFile && diagnostic > 2)
1077 sprintf (
string,
"Kappa42 Array, Channel %d", x);
1078 midiCreatePlotFile2D (
"Kappa42Array",
string,
"Frame",
"Flux", 1, kappArray, 0, nzc, 1, 1);
1083 badChannelList[x] |= BSL_DATA_ERROR;
1084 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
1085 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
1088 cpl_msg_info(cpl_func,
"%3d %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f \n",
1089 x, kc->k11[x], kc->sig11[x], kc->k21[x], kc->sig21[x], kc->k31[x], kc->sig31[x], kc->k22[x], kc->sig22[x],
1090 kc->k32[x], kc->sig32[x], kc->k42[x], kc->sig42[x]);
1091 fprintf (midiReportPtr,
"%3d %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f \n",
1092 x, kc->k11[x], kc->sig11[x], kc->k21[x], kc->sig21[x], kc->k31[x], kc->sig31[x], kc->k22[x], kc->sig22[x],
1093 kc->k32[x], kc->sig32[x], kc->k42[x], kc->sig42[x]);
1098 smoothKappaCoefficiets (formatIA, kc, error);
1101 freeKappaCoefficients (kc);
1110 computeTransferRatios (formatIA, kc, trr, error);
1113 freeKappaCoefficients (kc);
1141 void computeTransferRatios (
1142 ImageFormat *format,
1143 KappaCoefficients *kc,
1150 const char routine[] =
"computeTransferRatios";
1155 if (diagnostic > 4)cpl_msg_info(cpl_func,
"Invoking routine '%s' \n", routine);
1156 if (diagnostic > 4) fprintf(midiReportPtr,
"Invoking routine '%s' \n", routine);
1163 cpl_msg_info(cpl_func,
"Channel ka1 siga1 ka2 siga2 kb1 sigb1 kb2 sigb2 \n");
1164 fprintf (midiReportPtr,
"Channel ka1 siga1 ka2 siga2 kb1 sigb1 kb2 sigb2 \n");
1166 for (x = 0; x < format->iXWidth; x++)
1168 if (badChannelList[x])
1170 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
1171 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
1175 if (((kc->k11[x] <= 0.0) || (kc->k11[x] >= 1.0)) ||
1176 ((kc->k21[x] <= 0.0) || (kc->k21[x] >= 1.0)) ||
1177 ((kc->k31[x] <= 0.0) || (kc->k31[x] >= 1.0)) ||
1178 ((kc->k22[x] <= 0.0) || (kc->k22[x] >= 1.0)) ||
1179 ((kc->k32[x] <= 0.0) || (kc->k32[x] >= 1.0)) ||
1180 ((kc->k42[x] <= 0.0) || (kc->k42[x] >= 1.0)))
1182 badChannelList[x] |= BSL_DATA_ERROR;
1185 sprintf (midiMessage,
1186 "Cannot compute Transfer Ratios for Channel %3d. Values of Kappa Matrix are unreasonable", x);
1187 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1189 cpl_msg_info(cpl_func,
"%3d <-- %s --> \n", x, UNAV);
1190 fprintf (midiReportPtr,
"%3d <-- %s --> \n", x, UNAV);
1195 tr->ka1[x] = kc->k21[x] / kc->k11[x];
1196 tr->siga1[x] = kc->sig21[x] / fabs(kc->k11[x]) + kc->sig11[x] * (fabs(kc->k21[x]) / (kc->k11[x] * kc->k11[x]));
1197 tr->ka2[x] = kc->k31[x] / kc->k11[x];
1198 tr->siga2[x] = kc->sig31[x] / fabs(kc->k11[x]) + kc->sig11[x] * (fabs(kc->k31[x]) / (kc->k11[x] * kc->k11[x]));
1199 tr->kb1[x] = kc->k22[x] / kc->k42[x];
1200 tr->sigb1[x] = kc->sig22[x] / fabs(kc->k42[x]) + kc->sig42[x] * (fabs(kc->k22[x]) / (kc->k42[x] * kc->k42[x]));
1201 tr->kb2[x] = kc->k32[x] / kc->k42[x];
1202 tr->sigb2[x] = kc->sig32[x] / fabs(kc->k42[x]) + kc->sig42[x] * (fabs(kc->k32[x]) / (kc->k42[x] * kc->k42[x]));
1204 cpl_msg_info(cpl_func,
"%3d %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f \n",
1205 x, tr->ka1[x], tr->siga1[x], tr->ka2[x], tr->siga2[x], tr->kb1[x], tr->sigb1[x], tr->kb2[x], tr->sigb2[x]);
1206 fprintf (midiReportPtr,
"%3d %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f \n",
1207 x, tr->ka1[x], tr->siga1[x], tr->ka2[x], tr->siga2[x], tr->kb1[x], tr->sigb1[x], tr->kb2[x], tr->sigb2[x]);
1218 if (plotFile && diagnostic > 1)
1220 midiCreatePlotFile2D (
"Ka1Ratio",
"Ka1 Ratio",
"Channel",
"Flux", 1, tr->ka1, 0, format->iXWidth, 1, 0);
1221 midiCreatePlotFile2D (
"Ka1Sig",
"Ka1 Error",
"Channel",
"Flux", 1, tr->siga1, 0, format->iXWidth, 1, 0);
1223 midiCreatePlotFile2D (
"Ka2Ratio",
"Ka2 Ratio",
"Channel",
"Flux", 1, tr->ka2, 0, format->iXWidth, 1, 0);
1224 midiCreatePlotFile2D (
"Ka2Sig",
"Ka2 Error",
"Channel",
"Flux", 1, tr->siga2, 0, format->iXWidth, 1, 0);
1226 midiCreatePlotFile2D (
"Kb1Ratio",
"Kb1 Ratio",
"Channel",
"Flux", 1, tr->kb1, 0, format->iXWidth, 1, 0);
1227 midiCreatePlotFile2D (
"Kb1Sig",
"Kb1 Error",
"Channel",
"Flux", 1, tr->sigb1, 0, format->iXWidth, 1, 0);
1229 midiCreatePlotFile2D (
"Kb2Ratio",
"Kb2 Ratio",
"Channel",
"Flux", 1, tr->kb2, 0, format->iXWidth, 1, 0);
1230 midiCreatePlotFile2D (
"Kb2Sig",
"Kb2 Error",
"Channel",
"Flux", 1, tr->sigb2, 0, format->iXWidth, 1, 0);
1254 void smoothKappaCoefficiets (
1255 ImageFormat *format,
1256 KappaCoefficients *kc,
1262 const char routine[] =
"smoothKappaCoefficiets";
1267 if (diagnostic > 4)cpl_msg_info(cpl_func,
"Invoking routine '%s' \n", routine);
1268 if (diagnostic > 4) fprintf(midiReportPtr,
"Invoking routine '%s' \n", routine);
1275 for (x = 0; x < format->iXWidth; x++)
1277 if (!(badChannelList[x]))
1279 if ((kc->sig11[x] > KAPPA_NOISE_LIMIT * kc->k11[x]) || (kc->sig21[x] > KAPPA_NOISE_LIMIT * kc->k21[x]) ||
1280 (kc->sig31[x] > KAPPA_NOISE_LIMIT * kc->k31[x]) || (kc->sig22[x] > KAPPA_NOISE_LIMIT * kc->k22[x]) ||
1281 (kc->sig32[x] > KAPPA_NOISE_LIMIT * kc->k32[x]) || (kc->sig42[x] > KAPPA_NOISE_LIMIT * kc->k42[x]))
1282 badChannelList[x] |= BSL_DATA_ERROR;
1289 if (plotFile && diagnostic > 1 && n)
1291 midiCreatePlotFile2D (
"Kappa11VersX",
"k11 Versus X",
"X",
"k11", 1, kc->k11, 0, format->iXWidth, 1, 0);
1292 midiCreatePlotFile2D (
"Kappa21VersX",
"k21 Versus X",
"X",
"k21", 1, kc->k21, 0, format->iXWidth, 1, 0);
1293 midiCreatePlotFile2D (
"Kappa31VersX",
"k31 Versus X",
"X",
"k31", 1, kc->k31, 0, format->iXWidth, 1, 0);
1294 midiCreatePlotFile2D (
"Kappa22VersX",
"k22 Versus X",
"X",
"k22", 1, kc->k22, 0, format->iXWidth, 1, 0);
1295 midiCreatePlotFile2D (
"Kappa32VersX",
"k32 Versus X",
"X",
"k32", 1, kc->k32, 0, format->iXWidth, 1, 0);
1296 midiCreatePlotFile2D (
"Kappa42VersX",
"k42 Versus X",
"X",
"k42", 1, kc->k42, 0, format->iXWidth, 1, 0);
1302 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"High error bars on all channels");
1303 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
"Computation of the Kappa Coefficients is abandonned");
1307 for (x = 0; x < format->iXWidth; x++)
1310 if (badChannelList[x] & 1023)
1318 midiReportTbd (midiReportPtr, routine, __FILE__, __LINE__,
"Routine TBD");