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