00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <cpl.h>
00025 #include <stdlib.h>
00026 #include <math.h>
00027 #include "midiGlobal.h"
00028 #include <string.h>
00029 #include "midiLib.h"
00030 #include "statistics.h"
00031 #include "diagnostics.h"
00032 #include "photometry.h"
00033 #include "errorHandling.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 void estimatePhotom(
00059 char key,
00060 CompressedData *compressed,
00061 ImageFormat *format,
00062 float **photom,
00063 float **photomErr,
00064 int *error)
00065 {
00066
00067
00068
00069 const char routine[] = "estimatePhotom";
00070 int region, X, regionId;
00071 float choppingRMS;
00072 char *string, *title;
00073 float *arrayPlot;
00074
00075
00076
00077 if (diagnostic > 4)cpl_msg_info(cpl_func,"\nInvoking routine '%s' \n", routine);
00078 if (diagnostic > 4) fprintf(midiReportPtr, "\nInvoking routine '%s' \n", routine);
00079
00080
00081 *error = 0;
00082
00083
00084 if (format->numOfFrames < 2)
00085 {
00086 cpl_msg_info(cpl_func,"Not enough data. Expected more than 1. Found %d", format->numOfFrames);
00087 fprintf (midiReportPtr, "Not enough data. Expected more than 1. Found %d", format->numOfFrames);
00088 *error = 1;
00089 return;
00090 }
00091
00092
00093 if (!(format->chopped))
00094 {
00095 *error = 1;
00096 return;
00097 }
00098
00099 cpl_msg_info(cpl_func,"\n");
00100 fprintf (midiReportPtr, "\n");
00101
00102 for (region = 0; region < format->numOfRegionsToProcess; region++)
00103 {
00104 if (plotFile)
00105 arrayPlot = (float *) calloc (format->iXWidth, sizeof(float));
00106
00107 if (strcmp (format->beamCombiner, "SCI_PHOT") == 0)
00108 {
00109 if (key == 'A') regionId = 1;
00110 else if (key == 'B') regionId = 4;
00111 else regionId = region+2;
00112 }
00113 else
00114 regionId = region+1;
00115
00116 if (diagnostic)cpl_msg_info(cpl_func, "Running dispersed Photom %c, for region %d\n", key, regionId);
00117 fprintf (midiReportPtr, "Running dispersed Photom %c, for region %d\n", key, regionId);
00118
00119 for (X = 0; X < format->iXWidth; X++)
00120 {
00121 if (badChannelList[X])
00122 continue;
00123
00124 if (strcmp (format->beamCombiner, "SCI_PHOT") == 0)
00125 {
00126 photom[region][X] = getChopPhotomSP (X, (((compressed->iDispFringe)[region])[X]),
00127 format->numOfScans, format->framesPerScan, compressed->rejectList[X], &(photomErr[region][X]),
00128 &choppingRMS);
00129 }
00130 else
00131 {
00132 photom[region][X] = getChopPhotom (X, (((compressed->iDispFringe)[region])[X]),
00133 compressed->tarType, &(photomErr[region][X]), &choppingRMS, format->numOfFrames);
00134 }
00135
00136 if (isnan(photom[region][X]))
00137 badChannelList[X] |= BSL_DATA_ERROR;
00138
00139 if (plotFile)
00140 {
00141 if (isnan(photom[region][X]))
00142 arrayPlot[X] = 0.0;
00143 else
00144 arrayPlot[X] = photom[region][X];
00145 }
00146 }
00147
00148 if (plotFile)
00149 {
00150 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00151 string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00152 sprintf (string, "Photom%cDATA%d", key, regionId);
00153 sprintf (title, "Photometry %c, for Region %d", key, regionId);
00154 midiCreatePlotFile2D (string, title, "Channel", "Photometry", 0, photom[region], 0, format->iXWidth, 1, 0);
00155 sprintf (string, "Photom%cErrDATA%d", key, regionId);
00156 sprintf (title, "Photometry %c Error, for Region %d", key, regionId);
00157 midiCreatePlotFile2D (string, title, "Channel", "Photometry Error", 0, photomErr[region], 0, format->iXWidth, 1, 0);
00158 free (string);
00159 free (title);
00160 free (arrayPlot);
00161 }
00162 }
00163
00164 return;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 float getChopPhotomSP (
00182 int X,
00183 float *compressed,
00184 int numOfScans,
00185 int framesPerScan,
00186 int *rejectList,
00187 float *errRMS,
00188 float *choppingRMS)
00189 {
00190
00191
00192
00193 const char routine[] = "getChopPhotomSP";
00194 int f, j, s, numOfAccum, length;
00195 float accum, *results, photom, variance, rms;
00196
00197
00198
00199
00200
00201 results = (float *) calloc (numOfScans, sizeof (float));
00202
00203
00204 length = 0;
00205 for (s = 0; s < numOfScans; s++)
00206 {
00207 numOfAccum = 0;
00208 accum = 0.F;
00209 for (j = 0; j < framesPerScan; j++)
00210 {
00211 f = j + s * framesPerScan;
00212 if (!(rejectList[f]))
00213 {
00214 accum += (float) compressed[f];
00215 numOfAccum++;
00216 }
00217 }
00218 if (numOfAccum)
00219 {
00220 results[length] = accum / ((float) numOfAccum);
00221 length++;
00222 }
00223 }
00224
00225
00226 if (length)
00227 {
00228 photom = signalMean (results, 0, length);
00229 variance = signalVariance (results, 0, length, &rms);
00230
00231
00232 if (variance == 0.0)
00233 rms = 0.1F;
00234
00235 *errRMS = rms / sqrt((float)length);
00236
00237
00238 if (photom > 0.0)
00239 *choppingRMS = rms / photom;
00240 }
00241
00242
00243 if (photom <= 0.0 || !length)
00244 {
00245 photom = NOT_A_NUMBER;
00246 sprintf (midiMessage, "Cannot compute photometry for channel %3d", X);
00247 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00248
00249
00250 badChannelList[X] |= BSL_DATA_ERROR;
00251 }
00252
00253
00254 free(results);
00255
00256 return (photom);
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 float getChopPhotom (
00274 int X,
00275 float *collapsedData,
00276 char *targetType,
00277 float *errRMS,
00278 float *choppingRMS,
00279 int numData)
00280 {
00281
00282
00283
00284 const char routine[] = "getChopPhotom";
00285 char lastType;
00286 int index, numOfaccum;
00287 float accum, accum2, *results, photom, mean, variance, rms;
00288
00289
00290
00291
00292
00293 lastType = 'U';
00294 accum = 0.F;
00295 numOfaccum = 0;
00296
00297
00298 results = (float *) calloc (numData, sizeof (float));
00299
00300
00301 for (index = 0; index < numData; index++)
00302 {
00303
00304 if ((targetType[index] != lastType) && numOfaccum)
00305 {
00306 if (targetType[index] == 'T')
00307 {
00308 results[index] = accum / ((float) numOfaccum);
00309
00310
00311 numOfaccum = 0;
00312 accum = 0.F;
00313 }
00314 }
00315
00316
00317 if (targetType[index] == 'T')
00318 {
00319 accum += (float) collapsedData[index];
00320 numOfaccum++;
00321 }
00322
00323
00324 lastType = targetType[index];
00325 }
00326
00327
00328 accum = 0.F;
00329 accum2 = 0.F;
00330 numOfaccum = 0;
00331 for (index = 0; index < numData; index++)
00332 {
00333 if (results[index] != 0.F)
00334 {
00335 accum += results[index];
00336 accum2 += results[index] * results[index];
00337 numOfaccum++;
00338 }
00339 }
00340
00341
00342 if (numOfaccum)
00343 {
00344 mean = accum / ((float) numOfaccum);
00345 variance = (accum2 - mean * mean * ((float) numOfaccum)) / ((float) (numOfaccum - 1));
00346
00347
00348 if (variance > 0.0)
00349 rms = sqrt(variance);
00350 else
00351 rms = 0.1F;
00352
00353 *errRMS = rms / sqrt((float)numOfaccum);
00354 photom = mean;
00355
00356
00357 if (photom > 0.0)
00358 *choppingRMS = rms / photom;
00359 }
00360
00361
00362 if (photom <= 0.0 || !numOfaccum)
00363 {
00364 photom = NOT_A_NUMBER;
00365 sprintf (midiMessage, "Cannot compute photometry for channel %3d", X);
00366 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00367
00368
00369 badChannelList[X] |= BSL_DATA_ERROR;
00370 }
00371
00372
00373 free(results);
00374
00375 return (photom);
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 void estimatePhotomUndisp (
00394 ImageFormat *format,
00395 CompressedData *inData,
00396 float *errs,
00397 float *choppingRMS,
00398 float *results)
00399 {
00400
00401
00402
00403 int region;
00404 double *thisdata;
00405
00406
00407
00408 for (region = 0; region < format->numOfRegionsToProcess; region++)
00409 {
00410 if (region == 0)
00411 thisdata = inData->iFringe1;
00412 else
00413 thisdata = inData->iFringe2;
00414
00415 results[region] = getChopPhotomUndisp ((double *)thisdata, inData->tarType, errs+region,
00416 choppingRMS+region, format->numOfFrames);
00417
00418 if (diagnostic > 1)
00419 {
00420 cpl_msg_info(cpl_func,"Photometric measurement for region %d: %f \n", region+1, results[region]);
00421 cpl_msg_info(cpl_func,"Relative chop-to-chop rms: %4.1f%% \n", 100.F*choppingRMS[region]);
00422 fprintf (midiReportPtr, "Photometric measurement for region %d: %f \n", region+1, results[region]);
00423 fprintf(midiReportPtr, "Relative chop-to-chop rms: %4.1f%% \n", 100.F*choppingRMS[region]);
00424 }
00425 }
00426
00427 return;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 float getChopPhotomUndisp (
00446 double *collapsedData,
00447 char *targetType,
00448 float *err,
00449 float *choppingRMS,
00450 int numData)
00451 {
00452
00453
00454
00455 const char routine[] = "getChopPhotomUndisp";
00456 char lastType;
00457 int index, numOfaccum;
00458 float accum, accum2, *results, photom, mean, variance, rms;
00459
00460
00461
00462
00463
00464 lastType = 'U';
00465 accum = 0.F;
00466 numOfaccum = 0;
00467
00468
00469 results = (float *) calloc (numData, sizeof (float));
00470
00471
00472 for (index = 0; index < numData; index++)
00473 {
00474
00475 if ((targetType[index] != lastType) && numOfaccum)
00476 {
00477 if (targetType[index] == 'T')
00478 {
00479 results[index] = accum / ((float) numOfaccum);
00480
00481
00482 numOfaccum = 0;
00483 accum = 0.F;
00484 }
00485 }
00486
00487
00488 if (targetType[index] == 'T')
00489 {
00490 accum += (float) collapsedData[index];
00491 numOfaccum++;
00492 }
00493
00494
00495 lastType = targetType[index];
00496 }
00497
00498
00499 accum = 0.F;
00500 accum2 = 0.F;
00501 numOfaccum = 0;
00502 for (index = 0; index < numData; index++)
00503 {
00504 if (results[index] != 0.F)
00505 {
00506 accum += results[index];
00507 accum2 += results[index] * results[index];
00508 numOfaccum++;
00509 }
00510 }
00511
00512
00513 mean = accum / ((float) numOfaccum);
00514 variance = (accum2 - mean * mean * ((float) numOfaccum)) / ((float) (numOfaccum - 1));
00515
00516
00517 if (variance > 0.0)
00518 rms = sqrt(variance);
00519 else
00520 rms = 0.1F;
00521
00522 *err = rms / sqrt((float)numOfaccum);
00523 photom = mean;
00524
00525
00526 if (photom > 0.0)
00527 *choppingRMS = rms / photom;
00528 else
00529 {
00530
00531
00532 *choppingRMS = NOT_A_NUMBER;
00533 sprintf (midiMessage, "Singularity detected. Values in this batch are unreliable");
00534 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00535 }
00536
00537
00538 free(results);
00539
00540 return (photom);
00541 }
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564 void computeBinnedPhotom (
00565 CompressedData *photomA,
00566 CompressedData *photomB,
00567 ImageFormat *format,
00568 float *photomA1,
00569 float *photomA2,
00570 float *photomA3,
00571 float *photomB1,
00572 float *photomB2,
00573 float *photomB3,
00574 int *error)
00575 {
00576
00577
00578
00579 const char routine[] = "computeBinnedPhotom";
00580 int i, j;
00581
00582
00583
00584
00585
00586 *error = 0;
00587 *photomA1 = *photomA2 = *photomA3 = 0.0;
00588 *photomB1 = *photomB2 = *photomB3 = 0.0;
00589
00590 if (strcmp(format->grismId, "PRISM") == 0)
00591 {
00592 for (i = 36; i < 51; i++)
00593 {
00594 if (badChannelList[i])
00595 continue;
00596
00597 for (j = 0; j < format->numOfFrames; j++)
00598 {
00599 if (photomA->tarType[j] == 'T' && !(photomA->rejectList[i][j]))
00600 *photomA3 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
00601 if (photomB->tarType[j] == 'T' && !(photomB->rejectList[i][j]))
00602 *photomB3 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
00603 }
00604 }
00605 for (i = 88; i < 99; i++)
00606 {
00607 if (badChannelList[i])
00608 continue;
00609
00610 for (j = 0; j < format->numOfFrames; j++)
00611 {
00612 if (photomA->tarType[j] == 'T' && !(photomA->rejectList[i][j]))
00613 *photomA2 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
00614 if (photomB->tarType[j] == 'T' && !(photomB->rejectList[i][j]))
00615 *photomB2 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
00616 }
00617 }
00618 for (i = 113; i < 122; i++)
00619 {
00620 if (badChannelList[i])
00621 continue;
00622
00623 for (j = 0; j < format->numOfFrames; j++)
00624 {
00625 if (photomA->tarType[j] == 'T' && !(photomA->rejectList[i][j]))
00626 *photomA1 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
00627 if (photomB->tarType[j] == 'T' && !(photomB->rejectList[i][j]))
00628 *photomB1 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
00629 }
00630 }
00631 }
00632 else if (strcmp(format->grismId, "GRISM") == 0)
00633 {
00634 for (i = 52; i < 69; i++)
00635 {
00636 if (badChannelList[i])
00637 continue;
00638
00639 for (j = 0; j < format->numOfFrames; j++)
00640 {
00641 if (photomA->tarType[j] == 'T' && !(photomA->rejectList[i][j]))
00642 *photomA1 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
00643 if (photomB->tarType[j] == 'T' && !(photomB->rejectList[i][j]))
00644 *photomB1 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
00645 }
00646 }
00647 for (i = 116; i < 137; i++)
00648 {
00649 if (badChannelList[i])
00650 continue;
00651
00652 for (j = 0; j < format->numOfFrames; j++)
00653 {
00654 if (photomA->tarType[j] == 'T' && !(photomA->rejectList[i][j]))
00655 *photomA2 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
00656 if (photomB->tarType[j] == 'T' && !(photomB->rejectList[i][j]))
00657 *photomB2 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
00658 }
00659 }
00660 for (i = 217; i < 245; i++)
00661 {
00662 if (badChannelList[i])
00663 continue;
00664
00665 for (j = 0; j < format->numOfFrames; j++)
00666 {
00667 if (photomA->tarType[j] == 'T' && !(photomA->rejectList[i][j]))
00668 *photomA3 += ( (((photomA->iDispFringe)[0])[i])[j] + (((photomA->iDispFringe)[1])[i])[j] );
00669 if (photomB->tarType[j] == 'T' && !(photomB->rejectList[i][j]))
00670 *photomB3 += ( (((photomB->iDispFringe)[0])[i])[j] + (((photomB->iDispFringe)[1])[i])[j] );
00671 }
00672 }
00673 }
00674 else
00675 {
00676 sprintf (midiMessage, "Unknown GRISM ID ... %s", format->grismId);
00677 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00678 *error = 1;
00679 return;
00680 }
00681
00682 return;
00683 }
00684
00685