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 "midiGlobal.h"
00025 #include "midiLib.h"
00026 #include "fft.h"
00027 #include "errorHandling.h"
00028 #include "memoryHandling.h"
00029 #include "midiFitsUtility.h"
00030 #include "fitsAnalysisFrgHS.h"
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 void analyseFitsFrgHS (
00056 MidiFiles *fileNames,
00057 ImageFormat *formatInterf,
00058 ImageFormat *formatPhotomA,
00059 ImageFormat *formatPhotomB,
00060 int *error)
00061 {
00062
00063
00064
00065 const char routine[] = "analyseFitsFrgHS";
00066 char *fileTemp, *classification;
00067 FILE *inFitsBatchPtr=NULL;
00068 int extNumOfImagingDataFile, aprioriDataIsDisp=0;
00069 ImageFormat *localFormat=NULL;
00070 char *cleanString=NULL, *aprioriCatg=NULL;
00071 unsigned int numOfInterf, numOfPhotomA, numOfPhotomB, numOfAcq;
00072 ImageFormat *formatAcq;
00073
00074
00075
00076 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00077 if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
00078
00079 cpl_msg_info(cpl_func,"\nAnalysing batch %d \n", batchNumber);
00080 cpl_msg_info(cpl_func,"--------------- \n");
00081 fprintf (midiReportPtr, "\nAnalysing batch %d \n", batchNumber);
00082 fprintf (midiReportPtr, "--------------- \n");
00083
00084
00085 *error = 0;
00086
00087
00088 classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00089 cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00090 fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00091 aprioriCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00092 localFormat = callocImageFormat ();
00093 formatAcq = callocImageFormat ();
00094
00095
00096 if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00097 {
00098 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00099 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "No analysis has been carried out for this batch");
00100 free (fileTemp);
00101 free (cleanString);
00102 freeImageFormat (localFormat);
00103 freeImageFormat (formatAcq);
00104 free (classification);
00105 *error = 1;
00106 return;
00107 }
00108
00109
00110 numOfInterf = 0;
00111 midiInitFormat (formatInterf);
00112 numOfPhotomA = 0;
00113 midiInitFormat (formatPhotomA);
00114 numOfPhotomB = 0;
00115 midiInitFormat (formatPhotomB);
00116 numOfAcq = 0;
00117 midiInitFormat (formatAcq);
00118
00119
00120 while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00121 {
00122 sprintf (classification, "%s", "");
00123 sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
00124 if (diagnostic)cpl_msg_info(cpl_func,"\nAnalysing file %s \n", fileNames->inFitsName);
00125 fprintf(midiReportPtr, "\nAnalysing file %s \n", fileNames->inFitsName);
00126
00127
00128 extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
00129 if (*error) break;
00130
00131
00132 if (extNumOfImagingDataFile > 0)
00133 {
00134 getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
00135 if (*error) break;
00136 }
00137 else localFormat->hasData = 0;
00138
00139
00140 if (localFormat->hasData)
00141 {
00142
00143 if (((strcmp (localFormat->obsCatg, "CALIB") == 0) ||
00144 (strcmp (localFormat->obsCatg, "SCIENCE") == 0)) &&
00145 ((strcmp (localFormat->obsTech, "IMAGE,WINDOW") == 0) ||
00146 (strcmp (localFormat->obsType, "COARSE,OBJECT") == 0)))
00147 {
00148
00149 formatAcq->hasData = 1;
00150 formatAcq->chopped = 0;
00151 midiCopyFormat (localFormat, formatAcq);
00152
00153
00154 formatAcq->numOfFrames += localFormat->numOfFrames;
00155
00156
00157 numOfAcq++;
00158 break;
00159 }
00160
00161
00162 if (numOfPhotomA == 0 && numOfPhotomA == 0 && numOfInterf == 0)
00163 {
00164 aprioriDataIsDisp = localFormat->isDisp;
00165 sprintf (aprioriCatg, "%s", localFormat->obsCatg);
00166 }
00167
00168
00169 if (localFormat->isDisp != aprioriDataIsDisp)
00170 {
00171 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent Beam Combiner");
00172 *error = 1;
00173 break;
00174 }
00175 else
00176 aprioriDataIsDisp = localFormat->isDisp;
00177
00178
00179 if (strcmp (localFormat->obsCatg, aprioriCatg) != 0)
00180 {
00181 sprintf (midiMessage, "Inconsistent Observation Category. Expected: %s", aprioriCatg);
00182 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00183 *error = 1;
00184 break;
00185 }
00186 else
00187 sprintf (aprioriCatg, "%s", localFormat->obsCatg);
00188
00189
00190 if (strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0)
00191 {
00192
00193 if (numOfInterf == 0)
00194 {
00195 formatInterf->hasData = 1;
00196 formatInterf->chopped = 0;
00197 midiCopyFormat (localFormat, formatInterf);
00198 }
00199
00200
00201 formatInterf->numOfFrames += localFormat->numOfFrames;
00202
00203
00204 numOfInterf++;
00205 }
00206
00207 else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00208 (strcmp(localFormat->shutterId,"AOPEN") == 0))
00209 {
00210
00211 if (numOfPhotomA == 0)
00212 {
00213 formatPhotomA->hasData = 1;
00214 formatPhotomA->chopped = 1;
00215 midiCopyFormat (localFormat, formatPhotomA);
00216 }
00217
00218
00219 formatPhotomA->numOfFrames += localFormat->numOfFrames;
00220
00221
00222 numOfPhotomA++;
00223 }
00224 else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00225 (strcmp(localFormat->shutterId,"BOPEN") == 0))
00226 {
00227
00228 if (numOfPhotomB == 0)
00229 {
00230 formatPhotomB->hasData = 1;
00231 formatPhotomB->chopped = 1;
00232 midiCopyFormat (localFormat, formatPhotomB);
00233 }
00234
00235
00236 formatPhotomB->numOfFrames += localFormat->numOfFrames;
00237
00238
00239 numOfPhotomB++;
00240 }
00241 else
00242 {
00243 sprintf (midiMessage, "Unknown Category, Tech or Type in ... %s. Not processed", fileNames->inFitsName);
00244 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00245 }
00246 }
00247 else
00248 {
00249 if (diagnostic)
00250 {
00251 sprintf (midiMessage, "No data tables in ... %s. Not processed", fileNames->inFitsName);
00252 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00253 }
00254 }
00255 }
00256
00257
00258 if (fclose (inFitsBatchPtr))
00259 midiReportError (midiReportPtr, routine, __FILE__, __LINE__, "Cannot close batch file");
00260
00261
00262 if (!(*error) && (numOfPhotomA > 0) &&
00263 (numOfPhotomB > 0) && (numOfInterf > 0) &&
00264 (numOfPhotomA == numOfPhotomB))
00265 {
00266
00267 formatInterf->numOfScans = formatInterf->numOfFrames / formatInterf->framesPerScan;
00268
00269
00270 formatInterf->fftsize = determineFFTsize (formatInterf->framesPerScan);
00271
00272
00273 formatInterf->allSpectrumLength = REGIONS_UNDISPERSED * formatInterf->numOfScans * (formatInterf->fftsize/2);
00274
00275
00276 midiReportFormat ("INTERFEROMETRY", aprioriDataIsDisp, formatInterf);
00277 midiReportFormat ("PHOTOMETRY A", aprioriDataIsDisp, formatPhotomA);
00278 midiReportFormat ("PHOTOMETRY B", aprioriDataIsDisp, formatPhotomB);
00279
00280 sprintf (midiMessage, "%d useful Photometry and %d useful Interferometry files detected",
00281 numOfPhotomA+numOfPhotomB, numOfInterf);
00282 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00283 }
00284 else if (!(*error) && (numOfAcq > 0))
00285 {
00286
00287 formatAcq->numOfScans = formatAcq->numOfFrames / formatAcq->framesPerScan;
00288
00289
00290 midiReportAcqFormat (formatAcq);
00291 }
00292 else
00293 {
00294 sprintf (fileTemp, "%d", batchNumber);
00295 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent batch files");
00296 *error = 1;
00297 }
00298
00299
00300 if (!(*error))
00301 {
00302 if (!numOfAcq)
00303 {
00304 if (!aprioriDataIsDisp)
00305 {
00306 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00307 "Data is UNDISPERSED\n Algorithm for processing this type of data is not ready");
00308 *error = 1;
00309 }
00310 }
00311 }
00312
00313
00314 if (fileTemp) free (fileTemp);
00315 if (localFormat) freeImageFormat (localFormat);
00316 if (formatAcq) freeImageFormat (formatAcq);
00317 if (cleanString) free (cleanString);
00318 if (classification) free (classification);
00319 if (aprioriCatg) free (aprioriCatg);
00320
00321 return;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 void midiReportAcqFormat (
00339 ImageFormat *formatAcq)
00340 {
00341
00342
00343
00344 const char routine[] = "midiReportAcqFormat";
00345
00346
00347
00348 if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
00349 if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
00350
00351 cpl_msg_info(cpl_func,"ACQUISITION FORMAT \n");
00352 cpl_msg_info(cpl_func,"------------------ \n");
00353 cpl_msg_info(cpl_func,"FITS Template Name = %s \n", formatAcq->tplName);
00354 cpl_msg_info(cpl_func,"Batch Template Name = %s \n", batchTemplate);
00355 cpl_msg_info(cpl_func,"Observation Category = %s \n", formatAcq->obsCatg);
00356 cpl_msg_info(cpl_func,"Observation Technique = %s \n", formatAcq->obsTech);
00357 cpl_msg_info(cpl_func,"Observation Type = %s \n", formatAcq->obsType);
00358 cpl_msg_info(cpl_func,"Camera ID = %s \n", formatAcq->cameraId);
00359 cpl_msg_info(cpl_func,"Beam Combiner = %s \n", formatAcq->beamCombiner);
00360 cpl_msg_info(cpl_func,"Filter ID = %s \n", formatAcq->filterName);
00361 cpl_msg_info(cpl_func,"Shutter ID = %s \n", formatAcq->shutterId);
00362 cpl_msg_info(cpl_func,"Grism ID = %s \n", formatAcq->grismId);
00363 cpl_msg_info(cpl_func,"Slit Name = %s \n", formatAcq->slitName);
00364 cpl_msg_info(cpl_func,"Target Name = %s \n", formatAcq->targetName);
00365 cpl_msg_info(cpl_func,"TPL Start = %s \n", formatAcq->tplStart);
00366 cpl_msg_info(cpl_func,"Number of Frames = %d \n", formatAcq->numOfFrames);
00367 cpl_msg_info(cpl_func,"Frames per scan = %d \n", formatAcq->framesPerScan);
00368 cpl_msg_info(cpl_func,"Number of Scans = %d \n", formatAcq->numOfScans);
00369 cpl_msg_info(cpl_func,"X, Column dimension = %d \n", formatAcq->iXWidth);
00370 cpl_msg_info(cpl_func,"Y, Column dimension = %d \n", formatAcq->iYWidth);
00371 cpl_msg_info(cpl_func,"Sub-window size = %d \n", formatAcq->subWindowSize);
00372 cpl_msg_info(cpl_func,"Number of Pinholes = %d \n", formatAcq->numOfPinholes);
00373 cpl_msg_info(cpl_func,"Number of Detector Regions = %d \n", formatAcq->numOfDetectorRegions);
00374 cpl_msg_info(cpl_func,"\n");
00375
00376 fprintf (midiReportPtr, "ACQUISITION FORMAT \n");
00377 fprintf (midiReportPtr, "------------------ \n");
00378 fprintf (midiReportPtr, "FITS Template Name = %s \n", formatAcq->tplName);
00379 fprintf (midiReportPtr, "Batch Template Name = %s \n", batchTemplate);
00380 fprintf (midiReportPtr, "Observation Category = %s \n", formatAcq->obsCatg);
00381 fprintf (midiReportPtr, "Observation Technique = %s \n", formatAcq->obsTech);
00382 fprintf (midiReportPtr, "Observation Type = %s \n", formatAcq->obsType);
00383 fprintf (midiReportPtr, "Camera ID = %s \n", formatAcq->cameraId);
00384 fprintf (midiReportPtr, "Beam Combiner = %s \n", formatAcq->beamCombiner);
00385 fprintf (midiReportPtr, "Filter ID = %s \n", formatAcq->filterName);
00386 fprintf (midiReportPtr, "Shutter ID = %s \n", formatAcq->shutterId);
00387 fprintf (midiReportPtr, "Grism ID = %s \n", formatAcq->grismId);
00388 fprintf (midiReportPtr, "Slit Name = %s \n", formatAcq->slitName);
00389 fprintf (midiReportPtr, "Target Name = %s \n", formatAcq->targetName);
00390 fprintf (midiReportPtr, "TPL Start = %s \n", formatAcq->tplStart);
00391 fprintf (midiReportPtr, "Number of Frames = %d \n", formatAcq->numOfFrames);
00392 fprintf (midiReportPtr, "Frames per scan = %d \n", formatAcq->framesPerScan);
00393 fprintf (midiReportPtr, "Number of Scans = %d \n", formatAcq->numOfScans);
00394 fprintf (midiReportPtr, "X, Column dimension = %d \n", formatAcq->iXWidth);
00395 fprintf (midiReportPtr, "Y, Column dimension = %d \n", formatAcq->iYWidth);
00396 fprintf (midiReportPtr, "Sub-window size = %d \n", formatAcq->subWindowSize);
00397 fprintf (midiReportPtr, "Number of Pinholes = %d \n", formatAcq->numOfPinholes);
00398 fprintf (midiReportPtr, "Number of Detector Regions = %d \n", formatAcq->numOfDetectorRegions);
00399 fprintf (midiReportPtr, "\n");
00400
00401 return;
00402 }
00403
00404
00405