27 #include "midiGlobal.h"
30 #include "statistics.h"
31 #include "diagnostics.h"
32 #include "photometry.h"
33 #include "errorHandling.h"
60 CompressedData *compressed,
69 const char routine[] =
"estimatePhotom";
70 int region, X, regionId;
77 if (diagnostic > 4)cpl_msg_info(cpl_func,
"\nInvoking routine '%s' \n", routine);
78 if (diagnostic > 4) fprintf(midiReportPtr,
"\nInvoking routine '%s' \n", routine);
84 if (format->numOfFrames < 2)
86 cpl_msg_info(cpl_func,
"Not enough data. Expected more than 1. Found %d", format->numOfFrames);
87 fprintf (midiReportPtr,
"Not enough data. Expected more than 1. Found %d", format->numOfFrames);
93 if (!(format->chopped))
99 cpl_msg_info(cpl_func,
"\n");
100 fprintf (midiReportPtr,
"\n");
102 for (region = 0; region < format->numOfRegionsToProcess; region++)
105 arrayPlot = (
float *) calloc (format->iXWidth,
sizeof(
float));
107 if (strcmp (format->beamCombiner,
"SCI_PHOT") == 0)
109 if (key ==
'A') regionId = 1;
110 else if (key ==
'B') regionId = 4;
111 else regionId = region+2;
116 if (diagnostic)cpl_msg_info(cpl_func,
"Running dispersed Photom %c, for region %d\n", key, regionId);
117 fprintf (midiReportPtr,
"Running dispersed Photom %c, for region %d\n", key, regionId);
119 for (X = 0; X < format->iXWidth; X++)
121 if (badChannelList[X])
124 if (strcmp (format->beamCombiner,
"SCI_PHOT") == 0)
126 photom[region][X] = getChopPhotomSP (X, (((compressed->iDispFringe)[region])[X]),
127 format->numOfScans, format->framesPerScan, compressed->rejectList[X], &(photomErr[region][X]),
132 photom[region][X] = getChopPhotom (X, (((compressed->iDispFringe)[region])[X]),
133 compressed->tarType, &(photomErr[region][X]), &choppingRMS, format->numOfFrames);
136 if (isnan(photom[region][X]))
137 badChannelList[X] |= BSL_DATA_ERROR;
141 if (isnan(photom[region][X]))
144 arrayPlot[X] = photom[region][X];
150 title = (
char *) calloc (MAX_STRING_LENGTH,
sizeof (
char));
151 string = (
char *) calloc (MAX_STRING_LENGTH,
sizeof (
char));
152 sprintf (
string,
"Photom%cDATA%d", key, regionId);
153 sprintf (title,
"Photometry %c, for Region %d", key, regionId);
154 midiCreatePlotFile2D (
string, title,
"Channel",
"Photometry", 0, photom[region], 0, format->iXWidth, 1, 0);
155 sprintf (
string,
"Photom%cErrDATA%d", key, regionId);
156 sprintf (title,
"Photometry %c Error, for Region %d", key, regionId);
157 midiCreatePlotFile2D (
string, title,
"Channel",
"Photometry Error", 0, photomErr[region], 0, format->iXWidth, 1, 0);
181 float getChopPhotomSP (
193 const char routine[] =
"getChopPhotomSP";
194 int f, j, s, numOfAccum, length;
195 float accum, *results, photom, variance, rms;
201 results = (
float *) calloc (numOfScans,
sizeof (
float));
205 for (s = 0; s < numOfScans; s++)
209 for (j = 0; j < framesPerScan; j++)
211 f = j + s * framesPerScan;
212 if (!(rejectList[f]))
214 accum += (float) compressed[f];
220 results[length] = accum / ((float) numOfAccum);
228 photom = signalMean (results, 0, length);
229 variance = signalVariance (results, 0, length, &rms);
235 *errRMS = rms / sqrt((
float)length);
239 *choppingRMS = rms / photom;
243 if (photom <= 0.0 || !length)
245 photom = NOT_A_NUMBER;
246 sprintf (midiMessage,
"Cannot compute photometry for channel %3d", X);
247 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
250 badChannelList[X] |= BSL_DATA_ERROR;
273 float getChopPhotom (
275 float *collapsedData,
284 const char routine[] =
"getChopPhotom";
286 int index, numOfaccum;
287 float accum, accum2, *results, photom, mean, variance, rms;
298 results = (
float *) calloc (numData,
sizeof (
float));
301 for (index = 0; index < numData; index++)
304 if ((targetType[index] != lastType) && numOfaccum)
306 if (targetType[index] ==
'T')
308 results[index] = accum / ((float) numOfaccum);
317 if (targetType[index] ==
'T')
319 accum += (float) collapsedData[index];
324 lastType = targetType[index];
331 for (index = 0; index < numData; index++)
333 if (results[index] != 0.F)
335 accum += results[index];
336 accum2 += results[index] * results[index];
344 mean = accum / ((float) numOfaccum);
345 variance = (accum2 - mean * mean * ((float) numOfaccum)) / ((
float) (numOfaccum - 1));
349 rms = sqrt(variance);
353 *errRMS = rms / sqrt((
float)numOfaccum);
358 *choppingRMS = rms / photom;
362 if (photom <= 0.0 || !numOfaccum)
364 photom = NOT_A_NUMBER;
365 sprintf (midiMessage,
"Cannot compute photometry for channel %3d", X);
366 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
369 badChannelList[X] |= BSL_DATA_ERROR;
393 void estimatePhotomUndisp (
395 CompressedData *inData,
408 for (region = 0; region < format->numOfRegionsToProcess; region++)
411 thisdata = inData->iFringe1;
413 thisdata = inData->iFringe2;
415 results[region] = getChopPhotomUndisp ((
double *)thisdata, inData->tarType, errs+region,
416 choppingRMS+region, format->numOfFrames);
420 cpl_msg_info(cpl_func,
"Photometric measurement for region %d: %f \n", region+1, results[region]);
421 cpl_msg_info(cpl_func,
"Relative chop-to-chop rms: %4.1f%% \n", 100.F*choppingRMS[region]);
422 fprintf (midiReportPtr,
"Photometric measurement for region %d: %f \n", region+1, results[region]);
423 fprintf(midiReportPtr,
"Relative chop-to-chop rms: %4.1f%% \n", 100.F*choppingRMS[region]);
445 float getChopPhotomUndisp (
446 double *collapsedData,
455 const char routine[] =
"getChopPhotomUndisp";
457 int index, numOfaccum;
458 float accum, accum2, *results, photom, mean, variance, rms;
469 results = (
float *) calloc (numData,
sizeof (
float));
472 for (index = 0; index < numData; index++)
475 if ((targetType[index] != lastType) && numOfaccum)
477 if (targetType[index] ==
'T')
479 results[index] = accum / ((float) numOfaccum);
488 if (targetType[index] ==
'T')
490 accum += (float) collapsedData[index];
495 lastType = targetType[index];
502 for (index = 0; index < numData; index++)
504 if (results[index] != 0.F)
506 accum += results[index];
507 accum2 += results[index] * results[index];
513 mean = accum / ((float) numOfaccum);
514 variance = (accum2 - mean * mean * ((float) numOfaccum)) / ((
float) (numOfaccum - 1));
518 rms = sqrt(variance);
522 *err = rms / sqrt((
float)numOfaccum);
527 *choppingRMS = rms / photom;
532 *choppingRMS = NOT_A_NUMBER;
533 sprintf (midiMessage,
"Singularity detected. Values in this batch are unreliable");
534 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
564 void computeBinnedPhotom (
565 CompressedData *photomA,
566 CompressedData *photomB,
579 const char routine[] =
"computeBinnedPhotom";
587 *photomA1 = *photomA2 = *photomA3 = 0.0;
588 *photomB1 = *photomB2 = *photomB3 = 0.0;
590 if (strcmp(format->grismId,
"PRISM") == 0)
592 for (i = 36; i < 51; i++)
594 if (badChannelList[i])
597 for (j = 0; j < format->numOfFrames; j++)
599 if (photomA->tarType[j] ==
'T' && !(photomA->rejectList[i][j]))
600 *photomA3 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
601 if (photomB->tarType[j] ==
'T' && !(photomB->rejectList[i][j]))
602 *photomB3 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
605 for (i = 88; i < 99; i++)
607 if (badChannelList[i])
610 for (j = 0; j < format->numOfFrames; j++)
612 if (photomA->tarType[j] ==
'T' && !(photomA->rejectList[i][j]))
613 *photomA2 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
614 if (photomB->tarType[j] ==
'T' && !(photomB->rejectList[i][j]))
615 *photomB2 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
618 for (i = 113; i < 122; i++)
620 if (badChannelList[i])
623 for (j = 0; j < format->numOfFrames; j++)
625 if (photomA->tarType[j] ==
'T' && !(photomA->rejectList[i][j]))
626 *photomA1 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
627 if (photomB->tarType[j] ==
'T' && !(photomB->rejectList[i][j]))
628 *photomB1 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
632 else if (strcmp(format->grismId,
"GRISM") == 0)
634 for (i = 52; i < 69; i++)
636 if (badChannelList[i])
639 for (j = 0; j < format->numOfFrames; j++)
641 if (photomA->tarType[j] ==
'T' && !(photomA->rejectList[i][j]))
642 *photomA1 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
643 if (photomB->tarType[j] ==
'T' && !(photomB->rejectList[i][j]))
644 *photomB1 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
647 for (i = 116; i < 137; i++)
649 if (badChannelList[i])
652 for (j = 0; j < format->numOfFrames; j++)
654 if (photomA->tarType[j] ==
'T' && !(photomA->rejectList[i][j]))
655 *photomA2 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
656 if (photomB->tarType[j] ==
'T' && !(photomB->rejectList[i][j]))
657 *photomB2 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
660 for (i = 217; i < 245; i++)
662 if (badChannelList[i])
665 for (j = 0; j < format->numOfFrames; j++)
667 if (photomA->tarType[j] ==
'T' && !(photomA->rejectList[i][j]))
668 *photomA3 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
669 if (photomB->tarType[j] ==
'T' && !(photomB->rejectList[i][j]))
670 *photomB3 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
676 sprintf (midiMessage,
"Unknown GRISM ID ... %s", format->grismId);
677 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);