00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define SIMULATE_DATA_x // Only used when proper data was not available
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <cpl.h>
00025 #include <math.h>
00026 #include "midiGlobal.h"
00027 #include "midiLib.h"
00028 #include "memoryHandling.h"
00029 #include "errorHandling.h"
00030 #include "midiFitsUtility.h"
00031 #include "diagnostics.h"
00032 #include "preProcKappa.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 void preProcKappa (
00058 UserOptions *options,
00059 FilterData *filterInfo,
00060 MidiFiles *fileNames,
00061 CompressedData *compressedInterfA,
00062 CompressedData *compressedInterfB,
00063 CompressedData *compressedPhotomA,
00064 CompressedData *compressedPhotomB,
00065 ImageFormat *formatInterfA,
00066 ImageFormat *formatInterfB,
00067 ImageFormat *formatPhotomA,
00068 ImageFormat *formatPhotomB,
00069 int *error)
00070 {
00071
00072
00073
00074 const char routine[] = "preProcKappa";
00075 FILE *inFitsBatchPtr=NULL;
00076 int newFile, extNumOfImagingDataFile, extNumOfImagingDataMask, R;
00077 char *maskFile, *cleanString, *classification, *stringTemp, *fileName, *title;
00078 unsigned int loopCount = 0;
00079 ImageFormat *localFormat;
00080 enum ObsTechnique obsTech;
00081
00082
00083
00084 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00085 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00086
00087 cpl_msg_info(cpl_func,"\nReducing data of batch %d \n", batchNumber);
00088 cpl_msg_info(cpl_func,"---------------------- \n");
00089 fprintf (midiReportPtr, "\nReducing data of batch %d \n", batchNumber);
00090 fprintf (midiReportPtr, "---------------------- \n");
00091
00092
00093 *error = 0;
00094 compressedInterfA->exists = 0;
00095 compressedInterfB->exists = 0;
00096 compressedPhotomA->exists = 0;
00097 compressedPhotomB->exists = 0;
00098 newFile = 1;
00099 obsTech = UNKNOWN;
00100
00101
00102 stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00103 classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00104 cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00105 localFormat = callocImageFormat ();
00106 maskFile = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00107
00108
00109 if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00110 {
00111 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00112 "Cannot open input FITS file list. No compression has been carried out for this batch");
00113 free (cleanString);
00114 free (localFormat);
00115 free (stringTemp);
00116 free (classification);
00117 freeImageFormat (localFormat);
00118 *error = 1;
00119 return;
00120 }
00121
00122
00123 while (fgets (stringTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00124 {
00125 sprintf (classification, "%s", "");
00126 sscanf (stringTemp, "%s%s", fileNames->inFitsName, classification);
00127 cpl_msg_info(cpl_func,"\nProcessing file %s \n", fileNames->inFitsName);
00128 fprintf (midiReportPtr, "\nProcessing file %s \n", fileNames->inFitsName);
00129
00130
00131 extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
00132 if (*error) break;
00133
00134
00135 if (extNumOfImagingDataFile > 0)
00136 {
00137 getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
00138 if (*error) break;
00139 }
00140 else localFormat->hasData = 0;
00141
00142
00143 if (loopCount == 0)
00144 {
00145 getFilterData (fileNames->inFitsName, filterInfo, error);
00146 if (*error) break;
00147 else fprintf (midiReportPtr, "First file of batch = %s (QCLOG)\n", fileNames->inFitsName);
00148
00149
00150 selectMask (options->maskMode, fileNames, maskFile, error);
00151 if (*error) break;
00152
00153
00154 extNumOfImagingDataMask = findImagingDataExtension (maskFile, MASK_IMAGING_DATA, error);
00155 if (*error) break;
00156
00157 cpl_msg_info(cpl_func,"Mask File is %s\n", maskFile);
00158 fprintf( midiReportPtr, "Mask File is %s\n", maskFile);
00159 }
00160
00161
00162 if (localFormat->hasData)
00163 {
00164
00165 if ((strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0))
00166 {
00167 if (diagnostic > 1) midiReportInfo (midiReportPtr,
00168 routine, __FILE__, __LINE__, "The above Interferometry file will not be processed");
00169 obsTech = INTERF;
00170 }
00171 else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00172 (strcmp(localFormat->shutterId,"AOPEN") == 0))
00173 {
00174
00175 if (obsTech != PHOTOMA) newFile = 1;
00176 else newFile = 0;
00177
00178
00179 obsTech = PHOTOMA;
00180
00181
00182 if ((formatPhotomA->numOfDetectorRegions != localFormat->numOfDetectorRegions) ||
00183 (formatPhotomA->iXWidth != localFormat->iXWidth) ||
00184 (formatPhotomA->iYWidth != localFormat->iYWidth))
00185 {
00186 midiReportWarning (midiReportPtr,
00187 routine, __FILE__, __LINE__, "Expected format is incorrect");
00188 *error = 1;
00189 break;
00190 }
00191
00192
00193
00194 organiseKappa ("AOPEN", newFile, extNumOfImagingDataFile, fileNames,
00195 extNumOfImagingDataMask, maskFile, localFormat, formatInterfA,
00196 compressedInterfA, compressedPhotomA, error);
00197 if (*error) break;
00198 newFile = 0;
00199 }
00200 else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00201 (strcmp(localFormat->shutterId,"BOPEN") == 0))
00202 {
00203
00204 if (obsTech != PHOTOMB) newFile = 1;
00205 else newFile = 0;
00206
00207
00208 obsTech = PHOTOMB;
00209
00210
00211 if ((formatPhotomA->numOfDetectorRegions != localFormat->numOfDetectorRegions) ||
00212 (formatPhotomA->iXWidth != localFormat->iXWidth) ||
00213 (formatPhotomA->iYWidth != localFormat->iYWidth))
00214 {
00215 midiReportWarning (midiReportPtr,
00216 routine, __FILE__, __LINE__, "Expected format is incorrect");
00217 *error = 1;
00218 break;
00219 }
00220
00221
00222
00223 organiseKappa ("BOPEN", newFile, extNumOfImagingDataFile, fileNames,
00224 extNumOfImagingDataMask, maskFile, localFormat, formatInterfB,
00225 compressedInterfB, compressedPhotomB, error);
00226 if (*error) break;
00227 newFile = 0;
00228 }
00229 else
00230 {
00231 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Catg, Type or Tech");
00232 *error = 1;
00233 break;
00234 }
00235 }
00236 else
00237 {
00238 sprintf (midiMessage, "No data tables in %s. Not processed", fileNames->inFitsName);
00239 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00240 }
00241 loopCount++;
00242 }
00243
00244
00245 fclose (inFitsBatchPtr);
00246 free (cleanString);
00247 free (stringTemp);
00248 free (classification);
00249 freeImageFormat (localFormat);
00250 free (maskFile);
00251
00252
00253 cpl_msg_info(cpl_func,"\nCompression status \n");
00254 cpl_msg_info(cpl_func,"------------------ \n");
00255 if (compressedInterfA->exists)cpl_msg_info(cpl_func,"Created Compressed Interferometry A Data\n");
00256 if (compressedInterfB->exists)cpl_msg_info(cpl_func,"Created Compressed Interferometry B Data\n");
00257 if (compressedPhotomA->exists)cpl_msg_info(cpl_func,"Created Compressed Photometry A Data\n");
00258 if (compressedPhotomB->exists)cpl_msg_info(cpl_func,"Created Compressed Photometry B Data\n");
00259 cpl_msg_info(cpl_func,"\n");
00260
00261 fprintf (midiReportPtr, "\nCompression status \n");
00262 fprintf (midiReportPtr, "------------------ \n");
00263 if (compressedInterfA->exists) fprintf (midiReportPtr, "Created Compressed Interferometry A Data\n");
00264 if (compressedInterfA->exists) fprintf (midiReportPtr, "Created Compressed Interferometry B Data\n");
00265 if (compressedPhotomA->exists) fprintf (midiReportPtr, "Created Compressed Photometry A Data\n");
00266 if (compressedPhotomB->exists) fprintf (midiReportPtr, "Created Compressed Photometry B Data\n");
00267 fprintf (midiReportPtr, "\n");
00268
00269 if (!(compressedInterfA->exists) || !(compressedInterfB->exists) ||
00270 !(compressedPhotomA->exists) || !(compressedPhotomB->exists) || *error)
00271 {
00272 *error = 1;
00273 sprintf (midiMessage,
00274 "Cannot continue. Need the following compressed data: InterfA, InterfB, PhotomA, PhotomB");
00275 midiReportWarning (midiReportPtr,
00276 routine, __FILE__, __LINE__, midiMessage);
00277 return;
00278 }
00279
00280
00281 if (diagnostic > 1 && plotFile)
00282 {
00283 fileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00284 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00285
00286 for (R = 0; R < formatInterfA->numOfRegionsToProcess; R++)
00287 {
00288 sprintf (fileName, "3dInterfADATA%d", R+2);
00289 sprintf (title, "Interferometry A DATA %d (Masked and Sky removed)", R+2);
00290 midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0,
00291 compressedInterfA->image[R], formatInterfA->iXWidth, formatInterfA->iYWidth, "lines", "3");
00292 }
00293
00294 for (R = 0; R < formatInterfB->numOfRegionsToProcess; R++)
00295 {
00296 sprintf (fileName, "3dInterfBDATA%d", R+2);
00297 sprintf (title, "Interferometry B DATA %d (Masked and Sky removed)", R+2);
00298 midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0,
00299 compressedInterfB->image[R], formatInterfB->iXWidth, formatInterfB->iYWidth, "lines", "3");
00300 }
00301
00302 for (R = 0; R < formatPhotomA->numOfRegionsToProcess; R++)
00303 {
00304 sprintf (fileName, "3dPhotomADATA%d", R+1);
00305 sprintf (title, "Photometry A DATA %d (Masked and Sky removed)", R+1);
00306 midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0,
00307 compressedPhotomA->image[R], formatPhotomA->iXWidth, formatPhotomA->iYWidth, "lines", "3");
00308 }
00309
00310 for (R = 0; R < formatPhotomB->numOfRegionsToProcess; R++)
00311 {
00312 sprintf (fileName, "3dPhotomBDATA%d", R+4);
00313 sprintf (title, "Photometry B DATA %d (Masked and Sky removed)", R+4);
00314 midiCreatePlotFile3D (fileName, title, "X", "Y", "Flux", 0,
00315 compressedPhotomB->image[R], formatPhotomB->iXWidth, formatPhotomB->iYWidth, "lines", "3");
00316 }
00317
00318 free (fileName);
00319 free (title);
00320 }
00321
00322 return;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 void organiseKappa (
00342 const char *shutter,
00343 int newFile,
00344 int extNumOfImagingDataFile,
00345 MidiFiles *fileNames,
00346 int extNumOfImagingDataMask,
00347 char *maskFile,
00348 ImageFormat *localFormat,
00349 ImageFormat *formatInterf,
00350 CompressedData *compressedInterf,
00351 CompressedData *compressedPhotom,
00352 int *error)
00353 {
00354
00355
00356
00357 const char routine[] = "organiseKappa";
00358 int frame0;
00359
00360
00361
00362 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00363 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00364
00365
00366 *error = 0;
00367
00368
00369 frame0 = compressKappa (shutter, fileNames->inFitsName, maskFile,
00370 extNumOfImagingDataFile, extNumOfImagingDataMask, compressedInterf,
00371 compressedPhotom, newFile, localFormat, formatInterf->numOfFrames, error);
00372 if (*error) return;
00373
00374
00375 if (diagnostic)
00376 {
00377 sprintf (midiMessage, "\nWrote %d frames into Interf and Photom, starting at frame %d\nThus now %d frames total,"
00378 " expecting %d altogether. Error=%d \n\n", localFormat->numOfFrames, frame0,
00379 localFormat->numOfFrames+frame0, formatInterf->numOfFrames, *error);
00380 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00381 }
00382
00383
00384 if ((frame0 + localFormat->numOfFrames) == formatInterf->numOfFrames)
00385 {
00386 compressedInterf->exists = 1;
00387 compressedPhotom->exists = 1;
00388 }
00389
00390 return;
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 int compressKappa (
00428 const char *shutter,
00429 char *inFitsFile,
00430 char *maskFile,
00431 int extNumOfImagingDataFile,
00432 int extNumOfImagingDataMask,
00433 CompressedData *compressedInterf,
00434 CompressedData *compressedPhotom,
00435 int newSet,
00436 ImageFormat *format,
00437 int numOfFramesMax,
00438 int *error)
00439 {
00440
00441
00442
00443 const char routine[] = "compressKappa";
00444 qfits_table *pTable = NULL, *pMask = NULL;
00445 short int **inData, *inSteppingPhase;
00446 float **inMask, accum, current,
00447 *normalization;
00448
00449 char **inTARTYP = NULL, *tempStr, fitsColumnString[10], *dataName, *title=NULL, *fileString=NULL;
00450 double *inTIME = NULL;
00451 int frame0 = - 1;
00452 double (*inLOCALOPD)[2] = NULL, (*inOPD)[2] = NULL;
00453 static int aprioriSteppingPhase, iF, channelSelected = 0;
00454 static double zeroTime;
00455 int i, k, *foundData, foundSteppingPhase = 0, indexSteppingPhase, scalingOffset, *indexData,
00456 *indexMask, maskWidthX, maskWidthY, maskSubWindow, F, X, Y, R,
00457 *indexTARTYP, *foundTARTYP, startframe=0, frameOffset, indexOPD= -1, indexLOCALOPD= -1,
00458 indexTIME= -1, maxstep,
00459 tartypMult=2;
00460
00461
00462
00463
00464 int startRegion, endRegion, i2, fst, snd, found;
00465
00466
00467
00468
00469 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00470 if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
00471
00472
00473 *error = 0;
00474 if (format->numOfDetectorRegions != 4)
00475 {
00476 *error = 1;
00477 sprintf (midiMessage, "Incorrect number of regions. Expected 4, found %d", format->numOfDetectorRegions);
00478 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00479 return (-1);
00480 }
00481
00482
00483 if (strcmp (shutter, "AOPEN") == 0)
00484 {
00485 startRegion = 0;
00486 endRegion = format->numOfDetectorRegions - 1;
00487 }
00488 else
00489 {
00490 startRegion = 1;
00491 endRegion = format->numOfDetectorRegions;
00492 }
00493
00494
00495 inData = (short int **) calloc (format->numOfDetectorRegions, sizeof (short int *));
00496 inTARTYP = (char **) calloc (format->numOfDetectorRegions, sizeof (char *));
00497 inMask = (float **) calloc (format->numOfDetectorRegions, sizeof (float *));
00498 foundData = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00499 indexData = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00500 foundTARTYP = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00501 indexTARTYP = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00502 indexMask = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00503 normalization = (float *) calloc (format->iXWidth, sizeof (float));
00504 dataName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00505
00506
00507 if (newSet)
00508 {
00509 aprioriSteppingPhase = 0;
00510 iF = 0;
00511 }
00512
00513
00514 if ((iF + format->numOfFrames) > numOfFramesMax)
00515 format->numOfFrames = numOfFramesMax - iF;
00516
00517
00518 pMask = qfits_table_open (maskFile, extNumOfImagingDataMask);
00519 if (!pMask)
00520 {
00521 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load Mask's IMAGING_DATA");
00522 free (inData);
00523 free (inTARTYP);
00524 free (inMask);
00525 free (foundData);
00526 free (indexData);
00527 free (foundTARTYP);
00528 free (indexTARTYP);
00529 free (indexMask);
00530 free (normalization);
00531 free (dataName);
00532 *error = 1;
00533 return (-1);
00534 }
00535 pTable = qfits_table_open (inFitsFile, extNumOfImagingDataFile);
00536 if (!pTable)
00537 {
00538 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load Data's IMAGING_DATA");
00539 qfits_table_close (pMask);
00540 free (inData);
00541 free (inTARTYP);
00542 free (inMask);
00543 free (foundData);
00544 free (indexData);
00545 free (foundTARTYP);
00546 free (indexTARTYP);
00547 free (indexMask);
00548 free (normalization);
00549 free (dataName);
00550 *error = 1;
00551 return (-1);
00552 }
00553
00554
00555 for (R = startRegion; R < endRegion; R++)
00556 {
00557 foundData[R] = 0;
00558 indexData[R] = 0;
00559 }
00560 for (i = 0; i < pTable->nc; i++)
00561 {
00562 for (R = startRegion; R < endRegion; R++)
00563 {
00564 sprintf (dataName, "DATA%d", R+1);
00565 if (strcmp (pTable->col[i].tlabel, dataName) == 0)
00566 {
00567 foundData[R] = 1;
00568 indexData[R] = i;
00569 if (diagnostic)
00570 {
00571 cpl_msg_info(cpl_func,"Found 'DATA%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00572 fprintf(midiReportPtr, "Found 'DATA%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00573 }
00574 }
00575
00576 sprintf (dataName, "TARTYP%d", R+1);
00577 if (strcmp (pTable->col[i].tlabel, dataName) == 0)
00578 {
00579 foundTARTYP[R] = 1;
00580 indexTARTYP[R] = i;
00581 if (diagnostic)
00582 {
00583 cpl_msg_info(cpl_func,"Found 'TARTYP%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00584 fprintf(midiReportPtr, "Found 'TARTYP%d' at column %d in data file %s \n", R+1, i+1, inFitsFile);
00585 }
00586 }
00587 }
00588 if (strcmp (pTable->col[i].tlabel, "STEPPING_PHASE") == 0)
00589 {
00590 foundSteppingPhase = 1;
00591 indexSteppingPhase = i;
00592 }
00593 if (strcmp (pTable->col[i].tlabel, "OPD") == 0)
00594 {
00595 indexOPD = i;
00596 }
00597 if (strcmp (pTable->col[i].tlabel, "LOCALOPD") == 0)
00598 {
00599 indexLOCALOPD = i;
00600 }
00601 if (strcmp (pTable->col[i].tlabel, "TIME") == 0)
00602 {
00603 indexTIME = i;
00604 }
00605 }
00606
00607
00608 if (foundSteppingPhase == 0)
00609 {
00610 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find STEPPING_PHASE in data FITS file");
00611 *error = 1;
00612 }
00613 if (indexOPD < 0)
00614 {
00615 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find column for OPD in data FITS file");
00616 *error = 1;
00617 }
00618 if (indexLOCALOPD < 0)
00619 {
00620 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find column for LOCALOPD in data FITS file");
00621 *error = 1;
00622 }
00623 if (indexTIME < 0)
00624 {
00625 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find column for TIME in data FITS file");
00626 *error = 1;
00627 }
00628 for (R = startRegion; R < endRegion; R++)
00629 {
00630 if (foundData[R] == 0)
00631 {
00632 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00633 "Cannot find requested DATA column in data FITS file");
00634 *error = 1;
00635 }
00636 if (foundTARTYP[R] == 0)
00637 {
00638 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00639 "Cannot find requested TARTYP column in data FITS file");
00640 *error = 1;
00641 }
00642 }
00643
00644
00645 for (R = startRegion; R < endRegion; R++)
00646 {
00647 foundData[R] = 0;
00648 indexMask[R] = 0;
00649 }
00650 for (i = 0; i < pMask->nc; i++)
00651 {
00652 for (R = startRegion; R < endRegion; R++)
00653 {
00654 sprintf (dataName, "DATA%d", R+1);
00655 if (strcmp (pMask->col[i].tlabel, dataName) == 0)
00656 {
00657 foundData[R] = 1;
00658 indexMask[R] = i;
00659 if (diagnostic)
00660 {
00661 cpl_msg_info(cpl_func,"Found 'DATA%d' at column %d in mask file %s \n", R+1, i+1, maskFile);
00662 fprintf(midiReportPtr, "Found 'DATA%d' at column %d in mask file %s \n", R+1, i+1, maskFile);
00663 }
00664 }
00665 }
00666 }
00667
00668
00669 for (R = startRegion; R < endRegion; R++)
00670 {
00671 if (foundData[R] == 0)
00672 {
00673 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00674 "Cannot find requested DATA column in mask FITS file");
00675 *error = 1;
00676 }
00677 }
00678
00679
00680 sprintf (fitsColumnString, "TDIM%d", indexMask[1]+1);
00681 tempStr = qfits_query_ext (maskFile, fitsColumnString, extNumOfImagingDataMask);
00682 sscanf (tempStr, "'(%d,%d) '", &maskWidthX, &maskWidthY);
00683 maskSubWindow = maskWidthX * maskWidthY;
00684 if (diagnostic)cpl_msg_info(cpl_func,"Mask sub-window size = %d\n", maskSubWindow);
00685 if (diagnostic)cpl_msg_info(cpl_func,"Data sub-window size = %d\n", format->subWindowSize);
00686 fprintf (midiReportPtr, "Mask sub-window size = %d\n", maskSubWindow);
00687 fprintf (midiReportPtr, "Data sub-window size = %d\n", format->subWindowSize);
00688 if (maskSubWindow != format->subWindowSize)
00689 {
00690 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Mask has incompatible sub window size");
00691 *error = 1;
00692 }
00693
00694
00695 tempStr = qfits_query_ext (inFitsFile, "MAXSTEP", extNumOfImagingDataFile);
00696 if (tempStr != NULL)
00697 {
00698 if (diagnostic)cpl_msg_info(cpl_func,"MAXSTEP = %s\n", tempStr);
00699 fprintf( midiReportPtr, "MAXSTEP = %s\n", tempStr);
00700 sscanf (tempStr, "%d", &maxstep);
00701 }
00702 else
00703 {
00704 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read MAXSTEP");
00705 *error = 1;
00706 }
00707
00708
00709 inSteppingPhase = (short int*)qfits_query_column (pTable, indexSteppingPhase, NULL);
00710 if (inSteppingPhase[0] != ((aprioriSteppingPhase % format->framesPerScan) + 1))
00711 {
00712 sprintf (midiMessage, "Incorrect Stepping Phase. Expected %d to %d. Instead found %d to %d",
00713 aprioriSteppingPhase+1, maxstep, inSteppingPhase[0], maxstep-1);
00714 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00715 *error = 1;
00716 }
00717
00718
00719 if (*error)
00720 {
00721 qfits_table_close (pTable);
00722 qfits_table_close (pMask);
00723 free (inData);
00724 free (inTARTYP);
00725 free (inMask);
00726 free (foundData);
00727 free (indexData);
00728 free (foundTARTYP);
00729 free (indexTARTYP);
00730 free (indexMask);
00731 free (inSteppingPhase);
00732 free (normalization);
00733 free (dataName);
00734 return (-1);
00735 }
00736
00737
00738 for (R = startRegion; R < endRegion; R++)
00739 {
00740 inData[R] = (short int*) qfits_query_column (pTable, indexData[R], NULL);
00741 inTARTYP[R] = (char *) qfits_query_column (pTable, indexTARTYP[R], NULL);
00742
00743 found = 0;
00744 for (F = 0; F < format->numOfFrames; F++)
00745 {
00746
00747 if (newSet)
00748 {
00749 if (inTARTYP[R][F*tartypMult] == 'U')
00750 found = 1;
00751 if (!found)
00752 inTARTYP[R][F*tartypMult] = 'U';
00753 }
00754
00755
00756 if (strcmp (shutter, "AOPEN") == 0)
00757 {
00758 if (R == 0)
00759 (compressedPhotom->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00760 else if (R == 1)
00761 (compressedInterf->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00762 }
00763 else
00764 {
00765 if (R == 1)
00766 (compressedInterf->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00767 else if (R == 3)
00768 (compressedPhotom->tarType)[iF+F] = inTARTYP[R][F*tartypMult];
00769 }
00770 }
00771 }
00772
00773
00774 if(indexTIME >= 0)
00775 {
00776 startframe = 0;
00777 inTIME = (double *) qfits_query_column (pTable, indexTIME, NULL);
00778
00779 if (iF == 0) zeroTime = inTIME[0];
00780
00781
00782 if (iF == 0)
00783 {
00784 for (startframe = 0; startframe < ARB_NUM_OF_FRAMES; startframe++)
00785 {
00786 if ((zeroTime = inTIME[startframe]) > 1.0)
00787 break;
00788 }
00789 }
00790 if (startframe)
00791 {
00792 if (diagnostic)
00793 {
00794 cpl_msg_info(cpl_func,"\nLOOK: frames 0 - %d had ZERO for their time field!!\n", startframe-1);
00795 fprintf(midiReportPtr, "\nLOOK: frames 0 - %d had ZERO for their time field!!\n", startframe-1);
00796 }
00797 }
00798
00799 for (F = startframe; F < format->numOfFrames; F++)
00800 {
00801 compressedInterf->time[iF+F] = (float)(inTIME[F] - zeroTime);
00802 compressedPhotom->time[iF+F] = (float)(inTIME[F] - zeroTime);
00803 if (isnan (compressedInterf->time[iF+F]) ||
00804 isnan (compressedPhotom->time[iF+F]))
00805 {
00806
00807 for (i = 0; i < format->iXWidth; i++)
00808 {
00809 compressedInterf->rejectList[i][iF+F] |= BSL_TIME_ERROR;
00810 compressedPhotom->rejectList[i][iF+F] |= BSL_TIME_ERROR;
00811 }
00812 sprintf (midiMessage, "inTIME has an INVALID value at frame %d", iF+F);
00813 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00814 }
00815 }
00816 }
00817
00818
00819 if (indexLOCALOPD >= 0)
00820 {
00821 inLOCALOPD = (double (*)[2]) qfits_query_column (pTable, indexLOCALOPD, NULL);
00822
00823 for (F = 0; F < format->numOfFrames; F++)
00824 {
00825 compressedInterf->localOPD[iF+F] = (float)(inLOCALOPD[F][0] + inLOCALOPD[F][1]);
00826 compressedPhotom->localOPD[iF+F] = (float)(inLOCALOPD[F][0] + inLOCALOPD[F][1]);
00827 if (isnan (compressedInterf->localOPD[iF+F]) ||
00828 isnan (compressedPhotom->localOPD[iF+F]))
00829 {
00830
00831 for (i = 0; i < format->iXWidth; i++)
00832 {
00833 compressedInterf->rejectList[i][iF+F] |= BSL_LOCALOPD_ERROR;
00834 compressedPhotom->rejectList[i][iF+F] |= BSL_LOCALOPD_ERROR;
00835 }
00836 sprintf (midiMessage, "localOPD has an INVALID value at frame %d", iF+F);
00837 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00838 }
00839 }
00840 }
00841
00842
00843 if (indexOPD >= 0)
00844 {
00845 inOPD = (double (*)[2]) qfits_query_column (pTable, indexOPD, NULL);
00846
00847 for (F = 0; F < format->numOfFrames; F++)
00848 {
00849 (compressedInterf->bigDL)[iF+F] = (float)(inOPD[F][0] + inOPD[F][1]);
00850 (compressedPhotom->bigDL)[iF+F] = (float)(inOPD[F][0] + inOPD[F][1]);
00851 if (isnan (compressedInterf->bigDL[iF+F]) ||
00852 isnan (compressedPhotom->bigDL[iF+F]))
00853 {
00854
00855 for (i = 0; i < format->iXWidth; i++)
00856 {
00857 compressedInterf->rejectList[i][iF+F] |= BSL_OPD_ERROR;
00858 compressedPhotom->rejectList[i][iF+F] |= BSL_OPD_ERROR;
00859 }
00860 sprintf (midiMessage, "bigDL has an INVALID value at frame %d", iF+F);
00861 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00862 }
00863 }
00864 }
00865
00866
00867 for (R = startRegion; R < endRegion; R++)
00868 {
00869 inMask[R] = (float*) qfits_query_column (pMask, indexMask[R], NULL);
00870
00871
00872 if (diagnostic && plotFile && newSet)
00873 {
00874 fileString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00875 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00876 sprintf (fileString, "3dMaskDATA%d", R+1);
00877 sprintf (title, "3D Mask DATA %d", R+1);
00878 midiCreatePlotFile3D (fileString, title, "X", "Y", "Flux", 0,
00879 inMask[R], format->iXWidth, format->iYWidth, "lines", "3");
00880 free (fileString);
00881 free (title);
00882 }
00883 }
00884
00885
00886 if (newSet || !channelSelected)
00887 {
00888 selectChannels (startRegion, endRegion, format, inMask);
00889 channelSelected = 1;
00890 }
00891
00892
00893 for (i = 14; i < 25; i++)
00894 {
00895 sprintf (dataName, "TZERO%d", i);
00896 tempStr = qfits_query_ext (inFitsFile, dataName, extNumOfImagingDataFile);
00897 if (tempStr != NULL)
00898 {
00899 if (diagnostic)cpl_msg_info(cpl_func,"Scaling Offset = %s\n", tempStr);
00900 if (diagnostic) fprintf (midiReportPtr, "Scaling Offset = %s\n", tempStr);
00901 sscanf (tempStr, "%d", &scalingOffset);
00902 break;
00903 }
00904 }
00905 if (tempStr == NULL)
00906 {
00907 scalingOffset = 0;
00908 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Scaling Offset. It is set to 0");
00909 }
00910
00911
00912 for (R = startRegion; R < endRegion; R++)
00913 {
00914
00915 for (X = 0; X < format->iXWidth; X++)
00916 {
00917 accum = 0.0F;
00918 for (Y = 0; Y < format->iYWidth; Y++)
00919 accum += inMask[R][Y * format->iXWidth + X];
00920
00921 if (accum > 0.0)
00922 normalization[X] = 1.F/accum;
00923 else
00924 normalization[X] = 1.F;
00925 }
00926
00927 for (F = 0; F < format->numOfFrames; F++)
00928 {
00929 frameOffset = F * format->subWindowSize;
00930 for (X = 0; X < format->iXWidth; X++)
00931 {
00932 accum = 0.0F;
00933 for (Y = 0; Y < format->iYWidth; Y++)
00934 {
00935 k = Y * format->iXWidth + X;
00936 i = frameOffset + k;
00937
00938 #ifdef SIMULATE_DATA
00939
00940 if ((compressedInterf->tarType[F] == 'T') || (compressedPhotom->tarType[F] == 'T'))
00941 {
00942 if ((R == 1) || (R == 2))
00943 inData[R][i] = 20000;
00944 else
00945 inData[R][i] = 30000;
00946 }
00947 else
00948 {
00949 if ((R == 1) || (R == 2))
00950 inData[R][i] = 7000;
00951 else
00952 inData[R][i] = 10000;
00953 }
00954 #endif
00955
00956 if (isnan (inData[R][i]))
00957 {
00958 compressedInterf->rejectList[X][iF+F] |= BSL_DATA_ERROR;
00959 compressedPhotom->rejectList[X][iF+F] |= BSL_DATA_ERROR;
00960 sprintf (midiMessage, "inData has an INVALID value at frame %d", (iF+F));
00961 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00962 }
00963 else
00964 {
00965 current = (inData[R][i] + scalingOffset) * inMask[R][k];
00966 accum += current;
00967 }
00968 }
00969
00970
00971 if ((R == 0) && (strcmp (shutter, "AOPEN") == 0))
00972 {
00973 (((compressedPhotom->iDispFringe)[R])[X])[iF+F] =
00974 accum * normalization[X];
00975 (compressedPhotom->iFringe1)[iF+F] += accum;
00976 }
00977 if (R == 1)
00978 {
00979 (((compressedInterf->iDispFringe)[R-1])[X])[iF+F] =
00980 accum * normalization[X];
00981 (compressedInterf->iFringe1)[iF+F] += accum;
00982 (compressedInterf->iFringe)[iF+F] += accum;
00983 }
00984 if (R == 2)
00985 {
00986 (((compressedInterf->iDispFringe)[R-1])[X])[iF+F] =
00987 accum * normalization[X];
00988 (compressedInterf->iFringe2)[iF+F] += accum;
00989 (compressedInterf->iFringe)[iF+F] -= accum;
00990 }
00991 if ((R == 3) && (strcmp (shutter, "BOPEN") == 0))
00992 {
00993 (((compressedPhotom->iDispFringe)[R-3])[X])[iF+F] =
00994 accum * normalization[X];
00995 (compressedPhotom->iFringe1)[iF+F] += accum;
00996 }
00997 }
00998 }
00999
01000
01001 if (diagnostic > 1)
01002 {
01003 for (F = 0; F < format->numOfFrames-maxstep; F++)
01004 {
01005 fst = F * format->subWindowSize;
01006 snd = (F+maxstep) * format->subWindowSize;
01007 if (compressedInterf->tarType[F] != 'U')
01008 {
01009 for (Y = 0; Y < format->iYWidth; Y++)
01010 {
01011 for (X = 0; X < format->iXWidth; X++)
01012 {
01013 k = X * format->iYWidth + Y;
01014 i = fst + k;
01015 i2 = snd + k;
01016 if ((compressedInterf->tarType[F] == 'T') && (compressedInterf->tarType[F+maxstep] == 'S'))
01017 current = (inData[R][i] + scalingOffset) - (inData[R][i2] + scalingOffset);
01018 else if ((compressedInterf->tarType[F] == 'S') && (compressedInterf->tarType[F+maxstep] == 'T'))
01019 current = (inData[R][i2] + scalingOffset) - (inData[R][i] + scalingOffset);
01020
01021
01022 if ((R == 0) && (strcmp (shutter, "AOPEN") == 0))
01023 compressedPhotom->image[R][k] += (current * inMask[R][k]);
01024 if (R == 1 || R == 2)
01025 compressedInterf->image[R-1][k] += (current * inMask[R][k]);
01026 if ((R == 3) && (strcmp (shutter, "BOPEN") == 0))
01027 compressedPhotom->image[R-3][k] += (current * inMask[R][k]);
01028 }
01029 }
01030 }
01031 }
01032 F += maxstep;
01033 }
01034 }
01035
01036
01037 aprioriSteppingPhase = inSteppingPhase[F-1];
01038 if (aprioriSteppingPhase == format->framesPerScan) aprioriSteppingPhase = 0;
01039
01040 frame0 = iF;
01041 iF += format->numOfFrames;
01042
01043
01044 for (R = 0; R < format->numOfDetectorRegions; R++) free (inData[R]);
01045 for (R = 0; R < format->numOfDetectorRegions; R++) free (inTARTYP[R]);
01046 for (R = 0; R < format->numOfDetectorRegions; R++) free (inMask[R]);
01047 if (pMask) qfits_table_close (pMask);
01048 if (pTable) qfits_table_close (pTable);
01049 if (inLOCALOPD) free(inLOCALOPD);
01050 if (inOPD) free(inOPD);
01051 if (inTIME) free(inTIME);
01052 free (inData);
01053 free (inTARTYP);
01054 free (inMask);
01055 free (inSteppingPhase);
01056 free (foundData);
01057 free (indexData);
01058 free (foundTARTYP);
01059 free (indexTARTYP);
01060 free (indexMask);
01061 free (normalization);
01062 free (dataName);
01063
01064 return (frame0);
01065 }
01066
01067