00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <cpl.h>
00024 #include <math.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include "midiGlobal.h"
00028 #include "qfits.h"
00029 #include "midiLib.h"
00030 #include "imageProcessing.h"
00031 #include "midiFitsUtility.h"
00032 #include "createProdAcq.h"
00033 #include "diagnostics.h"
00034 #include "memoryHandling.h"
00035 #include "errorHandling.h"
00036 #include "procAcq.h"
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 procAcq (
00063 MidiFiles *fileNames,
00064 int *error)
00065 {
00066
00067
00068
00069 const char routine[] = "procAcq";
00070 char *fileTemp, *classification;
00071 FILE *inFitsBatchPtr;
00072 ImageFormat *format;
00073 int extNumOfImagingDataFile;
00074 ImageQuality *image=NULL;
00075 FILE *signaturePtr=NULL;
00076
00077
00078
00079 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00080 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00081
00082 cpl_msg_info(cpl_func,"\nProcessing Image Quality for batch %d \n", batchNumber);
00083 cpl_msg_info(cpl_func,"---------------------------------- \n");
00084 fprintf (midiReportPtr, "\nProcessing Image Quality for batch %d \n", batchNumber);
00085 fprintf (midiReportPtr, "---------------------------------- \n");
00086
00087
00088 signaturePtr = fopen ("MIDI_sig_acq.log", "w");
00089 fclose (signaturePtr);
00090
00091
00092 classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00093 fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00094 format = callocImageFormat ();
00095
00096
00097 *error = 0;
00098
00099
00100 if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00101 {
00102 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00103 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "No data preprocessing has been carried out for this batch");
00104 freeImageFormat (format);
00105 free (fileTemp);
00106 free (classification);
00107 *error = 1;
00108 return;
00109 }
00110
00111
00112 fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr);
00113 sprintf (classification, "%s", "");
00114 sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
00115 if (diagnostic)cpl_msg_info(cpl_func,"\n Processing file %s \n", fileNames->inFitsName);
00116 fprintf(midiReportPtr, "\n Processing file %s \n", fileNames->inFitsName);
00117
00118
00119 extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
00120 if (*error || !extNumOfImagingDataFile)
00121 {
00122 fclose (inFitsBatchPtr);
00123 freeImageFormat (format);
00124 free (fileTemp);
00125 free (classification);
00126 return;
00127 }
00128
00129
00130 getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, format, error);
00131 if (*error || !(format->hasData))
00132 {
00133 fclose (inFitsBatchPtr);
00134 freeImageFormat (format);
00135 free (fileTemp);
00136 free (classification);
00137 return;
00138 }
00139
00140
00141 if (((strcmp (format->obsCatg, "CALIB") == 0) ||
00142 (strcmp (format->obsCatg, "SCIENCE") == 0)) &&
00143 ((strcmp (format->obsTech, "IMAGE,WINDOW") == 0) ||
00144 (strcmp (format->obsType, "COARSE,OBJECT") == 0)))
00145 {
00146
00147 image = callocImageQuality (format);
00148 compressAcq (fileNames->inFitsName, extNumOfImagingDataFile, image, format, error);
00149 if (*error)
00150 {
00151 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot compress data in FITS file");
00152 fclose (inFitsBatchPtr);
00153 freeImageQuality (format, image);
00154 freeImageFormat (format);
00155 free (fileTemp);
00156 free (classification);
00157 return;
00158 }
00159
00160
00161 assessImageQuality (fileNames->inFitsName, image, format, error);
00162 if (*error)
00163 {
00164 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot assess image quality in FITS file");
00165 fclose (inFitsBatchPtr);
00166 freeImageQuality (format, image);
00167 freeImageFormat (format);
00168 free (fileTemp);
00169 free (classification);
00170 return;
00171 }
00172
00173
00174 createAcqProd (fileNames, format, image, error);
00175 if (*error)
00176 {
00177 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create product FITS file");
00178 fclose (inFitsBatchPtr);
00179 freeImageQuality (format, image);
00180 freeImageFormat (format);
00181 free (fileTemp);
00182 free (classification);
00183 return;
00184 }
00185 freeImageQuality (format, image);
00186 }
00187 else
00188 {
00189 *error = 1;
00190 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Catg, Type, or Tech");
00191 }
00192
00193
00194 fclose (inFitsBatchPtr);
00195
00196
00197 freeImageFormat (format);
00198 free (fileTemp);
00199 free (classification);
00200
00201 return;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 void compressAcq (
00219 char *inFitsFile,
00220 int extNumOfImagingDataFile,
00221 ImageQuality *compressed,
00222 ImageFormat *format,
00223 int *error)
00224 {
00225
00226
00227
00228 const char routine[] = "compressAcq";
00229 qfits_table *pTable=NULL;
00230 short int **inData;
00231 char *inTarType=NULL, *tempStr, *dataName, lastTT;
00232 int i, pixel, *foundData, scalingOffset, *indexData, numOfSkyFrames, numOfFramesUsed,
00233 frame, region, indexTarType, foundTarType=0, transitions, numOfUndefinedFrames, numOfBadFrames,
00234 tartypMult = 2;
00235
00236
00237
00238
00239
00240
00241
00242
00243 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00244 if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
00245
00246
00247 *error = 0;
00248
00249
00250 inData = (short int **) calloc (format->numOfDetectorRegions, sizeof (short int *));
00251 foundData = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00252 indexData = (int *) calloc (format->numOfDetectorRegions, sizeof (int));
00253 dataName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00254
00255 pTable = qfits_table_open (inFitsFile, extNumOfImagingDataFile);
00256 if (!pTable)
00257 {
00258 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load IMAGING_DATA from");
00259 free (inData);
00260 free (foundData);
00261 free (indexData);
00262 free (dataName);
00263 *error = 1;
00264 return;
00265 }
00266
00267
00268 for (region = 0; region < format->numOfDetectorRegions; region++)
00269 {
00270 foundData[region] = 0;
00271 indexData[region] = 0;
00272 }
00273 for (i = 0; i < pTable->nc; i++)
00274 {
00275 for (region = 0; region < format->numOfDetectorRegions; region++)
00276 {
00277 sprintf (dataName, "DATA%d", region+1);
00278 if (strcmp (pTable->col[i].tlabel, dataName) == 0)
00279 {
00280 foundData[region] = 1;
00281 indexData[region] = i;
00282 if (diagnostic)cpl_msg_info(cpl_func,"Found 'DATA%d' at column %d in data file %s \n", region+1, i+1, inFitsFile);
00283 if (diagnostic) fprintf(midiReportPtr, "Found 'DATA%d' at column %d in data file %s \n", region+1, i+1, inFitsFile);
00284 }
00285 }
00286 if (strcmp (pTable->col[i].tlabel, "TARTYP2") == 0)
00287 {
00288 foundTarType = 1;
00289 indexTarType = i;
00290 }
00291 }
00292
00293
00294 if (foundTarType == 0)
00295 {
00296 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find TARTYP2 in data FITS file");
00297 *error = 1;
00298 }
00299 for (region = 0; region < format->numOfDetectorRegions; region++)
00300 {
00301 if (foundData[region] == 0)
00302 {
00303 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find requested DATA column in data FITS file");
00304 *error = 1;
00305 }
00306 }
00307
00308
00309 if (*error)
00310 {
00311 qfits_table_close (pTable);
00312 free (inData);
00313 free (foundData);
00314 free (indexData);
00315 free (dataName);
00316 return;
00317 }
00318
00319
00320 inTarType = (char *) qfits_query_column (pTable, indexTarType, NULL);
00321 for (frame = 0; frame < format->numOfFrames; frame++)
00322 compressed->tarType[frame] = inTarType[frame*tartypMult];
00323
00324
00325 transitions = 0;
00326 lastTT = compressed->tarType[0];
00327 for (frame = 0; frame < format->numOfFrames; frame++)
00328 {
00329 if (compressed->tarType[frame] == lastTT) continue;
00330 transitions++;
00331 lastTT = compressed->tarType[frame];
00332 }
00333 if ((format->chopped = (transitions > 10)))
00334 {
00335 if (diagnostic)cpl_msg_info(cpl_func,"\nAcquision data is CHOPPED, since %d 'TARTYP2' transitions were found \n", transitions);
00336 fprintf (midiReportPtr, "\nAcquision data is CHOPPED, since %d 'TARTYP2' transitions were found \n", transitions);
00337 }
00338 else
00339 {
00340 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Acquisition data is NOT chopped");
00341 *error = 1;
00342 qfits_table_close (pTable);
00343 free (inData);
00344 free (foundData);
00345 free (indexData);
00346 free (dataName);
00347 return;
00348 }
00349
00350
00351 for (region = 0; region < format->numOfDetectorRegions; region++)
00352 inData[region] = (short int*) qfits_query_column (pTable, indexData[region], NULL);
00353
00354
00355 for (i = 14; i < 25; i++)
00356 {
00357 sprintf (dataName, "TZERO%d", i);
00358 tempStr = qfits_query_ext (inFitsFile, dataName, extNumOfImagingDataFile);
00359 if (tempStr != NULL)
00360 {
00361 if (diagnostic)cpl_msg_info(cpl_func,"Scaling Offset = %s\n", tempStr);
00362 if (diagnostic) fprintf (midiReportPtr, "Scaling Offset = %s\n", tempStr);
00363 sscanf (tempStr, "%d", &scalingOffset);
00364 break;
00365 }
00366 }
00367 if (tempStr == NULL)
00368 {
00369 scalingOffset = 0;
00370 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Scaling Offset. It is set to 0");
00371 }
00372
00373
00374 for (region = 0; region < format->numOfDetectorRegions; region++)
00375 {
00376 for (pixel = 0; pixel < format->subWindowSize; pixel++)
00377 {
00378
00379 numOfUndefinedFrames = 0;
00380 numOfBadFrames = 0;
00381 numOfFramesUsed = 0;
00382 numOfSkyFrames = 0;
00383 (compressed->aveImage)[region][pixel] = 0.0;
00384 (compressed->aveImageSky)[region][pixel] = 0.0;
00385 for (frame = 0; frame < format->numOfFrames; frame++)
00386 {
00387
00388 if (compressed->tarType[frame] == 'U')
00389 {
00390 if (diagnostic > 2 && !pixel)
00391 {
00392 sprintf (midiMessage, "Found undefined, 'U' 'TARTYP2' at frame %d of DATA%d in %s \n",
00393 frame+1, region+1, inFitsFile);
00394 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00395 }
00396 numOfUndefinedFrames++;
00397 continue;
00398 }
00399
00400
00401 i = frame * format->subWindowSize + pixel;
00402 if (isnan (inData[region][i]))
00403 {
00404 numOfBadFrames++;
00405 sprintf (midiMessage, "Found bad pixel %d of DATA%d in %s \n", i, region, inFitsFile);
00406 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00407 }
00408 else
00409 {
00410 numOfFramesUsed++;
00411 (compressed->aveImage)[region][pixel] += (float) (inData[region][i]);
00412 if (compressed->tarType[frame] == 'S')
00413 {
00414 numOfSkyFrames++;
00415 (compressed->aveImageSky)[region][pixel] += (float) (inData[region][i]);
00416 }
00417 }
00418 }
00419
00420
00421 if (diagnostic && !pixel)
00422 {
00423 cpl_msg_info(cpl_func,"\nData Statistics \n");
00424 cpl_msg_info(cpl_func,"--------------- \n");
00425 cpl_msg_info(cpl_func,"Number of frames used in DATA%d = %d \n", region+1, numOfFramesUsed);
00426 cpl_msg_info(cpl_func,"Number of Sky frames in DATA%d = %d \n", region+1, numOfSkyFrames);
00427 cpl_msg_info(cpl_func,"Number of Bad frames in DATA%d = %d \n", region+1, numOfBadFrames);
00428 cpl_msg_info(cpl_func,"Number of Undefined frames in DATA%d = %d \n", region+1, numOfUndefinedFrames);
00429 fprintf (midiReportPtr, "\nData Statistics \n");
00430 fprintf (midiReportPtr, "--------------- \n");
00431 fprintf (midiReportPtr, "Number of frames used in DATA%d = %d \n", region+1, numOfFramesUsed);
00432 fprintf (midiReportPtr, "Number of Sky frames in DATA%d = %d \n", region+1, numOfSkyFrames);
00433 fprintf (midiReportPtr, "Number of Bad frames in DATA%d = %d \n", region+1, numOfBadFrames);
00434 fprintf (midiReportPtr, "Number of Undefined frames in DATA%d = %d \n", region+1, numOfUndefinedFrames);
00435 }
00436 if (numOfFramesUsed) (compressed->aveImage)[region][pixel] /= numOfFramesUsed;
00437 (compressed->aveImage)[region][pixel] += scalingOffset;
00438 if (numOfSkyFrames) (compressed->aveImageSky)[region][pixel] /= numOfSkyFrames;
00439 (compressed->aveImageSky)[region][pixel] += scalingOffset;
00440 (compressed->aveImage)[region][pixel] -= (compressed->aveImageSky)[region][pixel];
00441 }
00442 }
00443
00444
00445 for (region = 0; region < format->numOfDetectorRegions; region++) free (inData[region]);
00446 if (pTable) qfits_table_close (pTable);
00447 if (inTarType) free(inTarType);
00448 free (inData);
00449 free (foundData);
00450 free (indexData);
00451 free (dataName);
00452
00453 return;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 void assessImageQuality (
00471 char *fileName,
00472 ImageQuality *compressed,
00473 ImageFormat *format,
00474 int *error)
00475
00476 {
00477
00478
00479
00480 const char routine[] = "assessImageQuality";
00481 int region, searchSize;
00482 char *title=NULL, *fileString=NULL;
00483 MidiCoords *target;
00484
00485
00486
00487 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00488 if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
00489
00490
00491 *error = 0;
00492 searchSize = SIZE_SEARCH_ACQ;
00493
00494
00495 target = (MidiCoords *) calloc (1, sizeof (MidiCoords));
00496
00497
00498 for (region = 0; region < format->numOfDetectorRegions; region++)
00499 {
00500
00501 if (diagnostic)
00502 {
00503 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00504 fileString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00505 sprintf (title , "AveImgSkyDATA%d", region+1);
00506 sprintf (fileString , "region %d", region+1);
00507 createFitsImage (fileString, title, fileName, format->iXWidth, format->iYWidth,
00508 (compressed->aveImageSky)[region]);
00509 sprintf (title , "AveImgDATA%d", region+1);
00510 createFitsImage (fileString, title, fileName, format->iXWidth, format->iYWidth,
00511 (compressed->aveImage)[region]);
00512 free (fileString);
00513 free (title);
00514 }
00515
00516 if (plotFile)
00517 {
00518 fileString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00519 title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00520 sprintf (fileString, "3dImgDATA%d", region+1);
00521 sprintf (title, "Dark removed, region %d", region+1);
00522 midiCreatePlotFile3D (fileString, title, "X", "Y", "Flux", 0,
00523 (compressed->aveImage)[region], format->iXWidth, format->iYWidth, "lines", "3");
00524 if (diagnostic)
00525 {
00526 sprintf (fileString, "3dImgSkyDATA%d", region+1);
00527 sprintf (title, "Sky, region %d", region+1);
00528 midiCreatePlotFile3D (fileString, title, "X", "Y", "Flux", 0,
00529 (compressed->aveImageSky)[region], format->iXWidth, format->iYWidth, "lines", "3");
00530 }
00531 free (fileString);
00532 free (title);
00533 }
00534
00535
00536 midiGetFWHM (region+1, (compressed->aveImage)[region],
00537 format->iXWidth, format->iYWidth, searchSize, &(compressed->coordX[region]),
00538 &(compressed->coordY[region]), &(compressed->sizeX[region]), &(compressed->sizeY[region]), error);
00539 cpl_msg_info(cpl_func,"Target Coordinates and size (X, Y, X-Width, Y-Width): %0.2f, %0.2f, %0.2f %0.2f \n",
00540 compressed->coordX[region], compressed->coordY[region], compressed->sizeX[region], compressed->sizeY[region]);
00541 fprintf (midiReportPtr, "Target Coordinates and size (X, Y, X-Width, Y-Width): %0.2f, %0.2f, %0.2f %0.2f \n",
00542 compressed->coordX[region], compressed->coordY[region], compressed->sizeX[region], compressed->sizeY[region]);
00543
00544
00545 target->xCoord = compressed->coordX[region] - (0.5 * compressed->sizeX[region]);
00546 target->yCoord = compressed->coordY[region] - (0.5 * compressed->sizeY[region]);
00547 target->dxCoord = compressed->coordX[region] + (0.5 * compressed->sizeX[region]);
00548 target->dyCoord = compressed->coordY[region] + (0.5 * compressed->sizeY[region]);
00549
00550
00551
00552
00553
00554 computeImageFlux ((compressed->aveImage)[region], format, target, &((compressed->targetFlux)[region]),
00555 &((compressed->targetPixelCount)[region]), error);
00556 cpl_msg_info(cpl_func,"Target flux in region %d = %0.2f \n", region+1, (compressed->targetFlux)[region]);
00557 cpl_msg_info(cpl_func,"Target pixel count in region %d = %d \n", region+1, (compressed->targetPixelCount)[region]);
00558 fprintf (midiReportPtr, "Target flux in region %d = %0.2f \n", region+1, (compressed->targetFlux)[region]);
00559 fprintf (midiReportPtr, "Target pixel count in region %d = %d \n", region+1, (compressed->targetPixelCount)[region]);
00560 }
00561
00562
00563 free (target);
00564
00565 return;
00566 }
00567
00568