00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <cpl.h>
00025 #include <math.h>
00026 #include "midiGlobal.h"
00027 #include "midiLib.h"
00028 #include "fft.h"
00029 #include "errorHandling.h"
00030 #include "memoryHandling.h"
00031 #include "midiFitsUtility.h"
00032 #include "fitsAnalysisKappa.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
00058 void analyseFitsKappa (
00059 MidiFiles *fileNames,
00060 ImageFormat *formatInterfA,
00061 ImageFormat *formatInterfB,
00062 ImageFormat *formatPhotomA,
00063 ImageFormat *formatPhotomB,
00064 int *error)
00065 {
00066
00067
00068
00069 const char routine[] = "analyseFitsKappa";
00070 char *fileTemp, *classification;
00071 FILE *inFitsBatchPtr=NULL;
00072 int extNumOfImagingDataFile, aprioriDataIsDisp=0;
00073 ImageFormat *localFormat=NULL;
00074 char *cleanString=NULL, *aprioriCatg=NULL;
00075 unsigned int numOfPhotomA, numOfPhotomB;
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,"\nAnalysing batch %d \n", batchNumber);
00083 cpl_msg_info(cpl_func,"--------------- \n");
00084 fprintf (midiReportPtr, "\nAnalysing batch %d \n", batchNumber);
00085 fprintf (midiReportPtr, "--------------- \n");
00086
00087
00088 *error = 0;
00089
00090
00091 classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00092 cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00093 fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00094 aprioriCatg = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00095 localFormat = callocImageFormat ();
00096
00097
00098 if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
00099 {
00100 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open input FITS file list");
00101 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "No analysis has been carried out for this batch");
00102 free (fileTemp);
00103 free (cleanString);
00104 freeImageFormat (localFormat);
00105 free (classification);
00106 *error = 1;
00107 return;
00108 }
00109
00110
00111 numOfPhotomA = 0;
00112 midiInitFormat (formatPhotomA);
00113 numOfPhotomB = 0;
00114 midiInitFormat (formatPhotomB);
00115
00116
00117 while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
00118 {
00119 sprintf (classification, "%s", "");
00120 sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
00121 if (diagnostic)cpl_msg_info(cpl_func,"\nAnalysing file %s \n", fileNames->inFitsName);
00122 fprintf(midiReportPtr, "\nAnalysing file %s \n", fileNames->inFitsName);
00123
00124
00125 extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, error);
00126 if (*error) break;
00127
00128
00129 if (extNumOfImagingDataFile > 0)
00130 {
00131 getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, localFormat, error);
00132 if (*error) break;
00133 }
00134 else localFormat->hasData = 0;
00135
00136
00137 if (localFormat->hasData)
00138 {
00139
00140 if (numOfPhotomA == 0 && numOfPhotomA == 0)
00141 {
00142 aprioriDataIsDisp = localFormat->isDisp;
00143 sprintf (aprioriCatg, "%s", localFormat->obsCatg);
00144 }
00145
00146
00147 if (localFormat->isDisp != aprioriDataIsDisp)
00148 {
00149 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent Beam Combiner");
00150 *error = 1;
00151 break;
00152 }
00153 else
00154 aprioriDataIsDisp = localFormat->isDisp;
00155
00156
00157 if (strcmp (localFormat->obsCatg, aprioriCatg) != 0)
00158 {
00159 sprintf (midiMessage, "Inconsistent Observation Category. Expected: %s", aprioriCatg);
00160 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00161 *error = 1;
00162 break;
00163 }
00164 else
00165 sprintf (aprioriCatg, "%s", localFormat->obsCatg);
00166
00167
00168 if (strcmp (localFormat->obsTech, "INTERFEROMETRY") == 0)
00169 {
00170 if (diagnostic > 1) midiReportInfo (midiReportPtr,
00171 routine, __FILE__, __LINE__, "The above Interferometry file will not be processed");
00172 }
00173 else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00174 (strcmp(localFormat->shutterId,"AOPEN") == 0))
00175 {
00176
00177 if (numOfPhotomA == 0)
00178 {
00179 formatPhotomA->hasData = 1;
00180 formatPhotomA->chopped = 1;
00181 midiCopyFormat (localFormat, formatPhotomA);
00182 formatPhotomA->numOfRegionsToProcess = 1;
00183 formatInterfA->hasData = 1;
00184 formatInterfA->chopped = 1;
00185 midiCopyFormat (localFormat, formatInterfA);
00186 }
00187
00188
00189 formatPhotomA->numOfFrames += localFormat->numOfFrames;
00190 formatInterfA->numOfFrames += localFormat->numOfFrames;
00191
00192
00193 numOfPhotomA++;
00194 }
00195 else if ((strcmp (localFormat->obsTech, "IMAGE,WINDOW,CHOPNOD") == 0) &&
00196 (strcmp(localFormat->shutterId,"BOPEN") == 0))
00197 {
00198
00199 if (numOfPhotomB == 0)
00200 {
00201 formatPhotomB->hasData = 1;
00202 formatPhotomB->chopped = 1;
00203 midiCopyFormat (localFormat, formatPhotomB);
00204 formatPhotomB->numOfRegionsToProcess = 1;
00205 formatInterfB->hasData = 1;
00206 formatInterfB->chopped = 1;
00207 midiCopyFormat (localFormat, formatInterfB);
00208 }
00209
00210
00211 formatPhotomB->numOfFrames += localFormat->numOfFrames;
00212 formatInterfB->numOfFrames += localFormat->numOfFrames;
00213
00214
00215 numOfPhotomB++;
00216 }
00217 else
00218 {
00219 sprintf (midiMessage, "Unknown Category, Tech or Type in ... %s. Not processed",
00220 fileNames->inFitsName);
00221 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00222 }
00223 }
00224 else
00225 {
00226 if (diagnostic)
00227 {
00228 sprintf (midiMessage, "No data tables in ... %s. Not processed", fileNames->inFitsName);
00229 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00230 }
00231 }
00232 }
00233
00234
00235 if (fclose (inFitsBatchPtr))
00236 midiReportError (midiReportPtr, routine, __FILE__, __LINE__, "Cannot close batch file");
00237
00238
00239 if (!(*error) && (numOfPhotomA > 0) && (numOfPhotomB > 0) && (numOfPhotomA == numOfPhotomB))
00240 {
00241
00242 if ((formatInterfA->framesPerScan == 0) || (formatInterfB->framesPerScan == 0) ||
00243 (formatPhotomA->framesPerScan == 0) || (formatPhotomB->framesPerScan == 0))
00244 {
00245 if (fileTemp) free (fileTemp);
00246 if (localFormat) freeImageFormat (localFormat);
00247 if (cleanString) free (cleanString);
00248 if (classification) free (classification);
00249 if (aprioriCatg) free (aprioriCatg);
00250 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Potential singularity detected");
00251 *error = 1;
00252 return;
00253 }
00254
00255
00256 formatInterfA->numOfScans = formatInterfA->numOfFrames / formatInterfA->framesPerScan;
00257 formatInterfB->numOfScans = formatInterfB->numOfFrames / formatInterfB->framesPerScan;
00258 formatPhotomA->numOfScans = formatPhotomA->numOfFrames / formatPhotomA->framesPerScan;
00259 formatPhotomB->numOfScans = formatPhotomB->numOfFrames / formatPhotomB->framesPerScan;
00260
00261
00262 formatInterfA->numOfFrames = formatInterfA->numOfScans * formatInterfA->framesPerScan;
00263 formatInterfB->numOfFrames = formatInterfB->numOfScans * formatInterfB->framesPerScan;
00264 formatPhotomA->numOfFrames = formatPhotomA->numOfScans * formatPhotomA->framesPerScan;
00265 formatPhotomB->numOfFrames = formatPhotomB->numOfScans * formatPhotomB->framesPerScan;
00266
00267
00268 formatInterfA->fftsize = determineFFTsize (formatInterfA->framesPerScan);
00269 formatInterfB->fftsize = determineFFTsize (formatInterfB->framesPerScan);
00270
00271
00272 formatInterfA->allSpectrumLength = REGIONS_UNDISPERSED * formatInterfA->numOfScans * (formatInterfA->fftsize/2);
00273 formatInterfB->allSpectrumLength = REGIONS_UNDISPERSED * formatInterfB->numOfScans * (formatInterfB->fftsize/2);
00274
00275
00276 midiReportFormat ("INTERFEROMETRY A", aprioriDataIsDisp, formatInterfA);
00277 midiReportFormat ("INTERFEROMETRY B", aprioriDataIsDisp, formatInterfB);
00278 midiReportFormat ("PHOTOMETRY A", aprioriDataIsDisp, formatPhotomA);
00279 midiReportFormat ("PHOTOMETRY B", aprioriDataIsDisp, formatPhotomB);
00280
00281 sprintf (midiMessage, "%d useful Photometry A and %d useful Photometry B files detected",
00282 numOfPhotomA, numOfPhotomB);
00283 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00284 }
00285 else
00286 {
00287 sprintf (fileTemp, "%d", batchNumber);
00288 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Inconsistent batch files");
00289 *error = 1;
00290 }
00291
00292
00293 if (!(*error))
00294 {
00295 if (!aprioriDataIsDisp)
00296 {
00297 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
00298 "Data is UNDISPERSED\n Algorithm for processing this type of data is not ready");
00299 *error = 1;
00300 }
00301 }
00302
00303
00304 if (fileTemp) free (fileTemp);
00305 if (localFormat) freeImageFormat (localFormat);
00306 if (cleanString) free (cleanString);
00307 if (classification) free (classification);
00308 if (aprioriCatg) free (aprioriCatg);
00309
00310 return;
00311 }
00312
00313