MIDI Pipeline Reference Manual  2.8.3
procDspTrn.c
1 
2 /******************************************************************************
3 *******************************************************************************
4 * European Southern Observatory
5 * VLTI MIDI Maintenance Templates Software
6 *
7 * Module name: procDspTrn.c
8 * Description: Contains routines for processing the templates
9 *
10 * History:
11 * 16-Jun-04 (csabet) created
12 *******************************************************************************
13 ******************************************************************************/
14 
15 /******************************************************************************
16 * Compiler directives
17 ******************************************************************************/
18 
19 /******************************************************************************
20 * Include files
21 ******************************************************************************/
22 #include <math.h>
23 #include <stdio.h>
24 #include "midiGlobal.h"
25 #include "midiLib.h"
26 #include "imageProcessing.h"
27 #include "memoryHandling.h"
28 #include "createProdDspTrn.h"
29 #include "procDspTrn.h"
30 #include "errorHandling.h"
31 #include "midiFitsUtility.h"
32 #include "fitsAnalysisTec.h"
33 #include "diagnostics.h"
34 #include "qfits.h"
35 #include <cpl.h>
36 
37 /**********************************************************
38 * Constant definitions
39 **********************************************************/
40 
41 /**********************************************************
42 * Global Variables
43 **********************************************************/
44 
45 /*============================ C O D E A R E A ===========================*/
46 
47 
48 /******************************************************************************
49 * European Southern Observatory
50 * VLTI MIDI Maintenance Templates Software
51 *
52 * Module name: procDspTrn
53 * Input/Output: See function arguments to avoid duplication
54 * Description: Measures the transmission characteristics of the dispersive
55 * elements (Prism and Grism) in order to monitor the evolution of
56 * their coatings. This is the main routine for this application.
57 *
58 * History:
59 * 03-May-05 (csabet) Created
60 ******************************************************************************/
61 void procDspTrn (
62  MidiFiles *fileNames, // In: Pointer to file names
63  int *error) // Ou: Error status
64 {
65 
66  // Local Declarations
67  // ------------------
68  const char routine[] = "procDspTrn";
69  ImageFormat *format=NULL;
70  DispersiveTrans *trans=NULL;
71  int numOfFiles;
72  FILE *signaturePtr=NULL;
73 
74  // Algorithm
75  // ---------
76  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
77  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
78 
79  // Write a signature
80  signaturePtr = fopen ("MIDI_sig_trn.log", "w");
81  fclose (signaturePtr);
82 
83  // Reset status
84  *error = 0;
85  numOfFiles = 0;
86 
87  // Allocate memory
88  format = callocImageFormat ();
89 
90  analyseFitsDspTrn (fileNames, format, &numOfFiles, error);
91  if (*error)
92  {
93  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot analyse DSPTRN");
94  freeImageFormat (format);
95  return;
96  }
97 
98  trans = callocDspTrn (numOfFiles);
99  computeDspTrn (numOfFiles, fileNames, trans, error);
100  if (*error)
101  {
102  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot process DSPTRN");
103  freeDspTrn (trans);
104  freeImageFormat (format);
105  return;
106  }
107 
108  createDspTrnProd (fileNames, format, trans, error);
109  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create DSPTRN products");
110 
111  // Release memory
112  freeDspTrn (trans);
113  freeImageFormat (format);
114 
115  return;
116 }
117 
118 
119 /*----------------------------------------------------------------------------*/
131 /*----------------------------------------------------------------------------*/
132 static cpl_image *
133 image_filter_median( const cpl_image *image, int xwindow, int ywindow )
134 {
135  int nx = cpl_image_get_size_x(image);
136  int ny = cpl_image_get_size_y(image);
137  cpl_image *result = cpl_image_new( nx, ny, CPL_TYPE_FLOAT );
138  int x, y;
139 
140  if (result == NULL)
141  {
142  return NULL;
143  }
144 
145  for (y = 1; y <= ny; y++)
146  for (x = 1; x <= nx; x++)
147  {
148  double median;
149  int llx = x - (xwindow - 1) / 2;
150  int lly = y - (ywindow - 1) / 2;
151  int urx = x + (xwindow - 1) / 2;
152  int ury = y + (ywindow - 1) / 2;
153  if (llx < 1) llx = 1;
154  if (lly < 1) lly = 1;
155  if (urx > nx) urx = nx;
156  if (ury > ny) ury = ny;
157  median = cpl_image_get_median_window( image,
158  llx, lly,
159  urx, ury );
160 
161  cpl_image_set( result, x, y, median );
162  }
163 
164  return result;
165 }
166 
167 /*****************************************************************************/
168 
169 
170 
171 /******************************************************************************
172 * European Southern Observatory
173 * VLTI MIDI Maintenance Templates Software
174 *
175 * Module name: computeDspTrn
176 * Input/Output: See function arguments to avoid duplication
177 * Description: Measures the transmission characteristics of the dispersive
178 * elements (Prism and Grism) in order to monitor the evolution of
179 * their coatings.
180 * Observations are made by looking at a blackscreen (blackbody).
181 * 6 measurements are made with one empty file at the end.
182 * Files 1,2,3 are Photometry (Imaging) with filters and files 4,5,6
183 * are Spectroscopy (prism or grism) with filters (ArIII, NeII, SIV)
184 * The above sequence could be repeated with different exposure time.
185 * Each observation sequence is analysed independently. Saturation and
186 * noise limits need to be verified to guarantee the validity of the results.
187 *
188 * On the imaging observation, the position of the target (x1,y1,dx1,dy1)
189 * and the photometry (integral number of counts, P1) is measured for
190 * each filter. In addition the exposure time (T1), the maximum count
191 * level (m1) are being recorded. The background is removed from a
192 * region around the star.
193 *
194 * On the spectroscopy observations, the position y1 of the spectrum
195 * and extension should be the same as in the imaging mode. The background
196 * is removed from a region below and above the spectrum. A 1D fit
197 * is performed along each column to subtract the background.
198 * The signal is integrated over the full spectrum, yielding an integral
199 * number of counts (P2), In addition the exposure time (t2), the maximum
200 * count level (m2) are being recorded. The photometry data are copied
201 * from the corresponding intermediate QC product.
202 *
203 * Summary:
204 * 1. Find the coordinates of the target (x1, y1, dx1, dy1)
205 * 2. Find the coordinates of the background (xb1, yb1, dxb1, dyb1)
206 * 3. collapse each frame of files 1 to 6 containing the target with background removed
207 * 4. The above gives the "count" of sub-window containing the target
208 * 5. For each file get the integral count given by:
209 * P(file) = count / exposureTime
210 * 6. Dispersive Transmissions are given by
211 * alpha1 = P4/P1 alpha2 = P5/P2 alpha3 = P6/P3
212 *
213 * History:
214 * 18-Aug-04 (csabet) Created
215 ******************************************************************************/
216 void computeDspTrn (
217  int numOfFiles, // In: Number of data files
218  MidiFiles *fileNames, // In: Pointer to the MIDI file structure
219  DispersiveTrans *dspTrn, // Ou: Pointer to the dispersive transmission data structure
220  int *error) // Ou: Error status
221 {
222 
223  // Local Declarations
224  // ------------------
225  const char routine[] = "computeDspTrn";
226  char *fileTemp, *classification;
227  FILE *inFitsBatchPtr;
228  ImageFormat *format;
229  int localError, fileNumber, extNumOfImagingDataFile;
230 
231  // Algorithm
232  // ---------
233  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
234  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
235 
236  // Allocate memory
237  classification = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
238  fileTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
239  format = callocImageFormat ();
240 
241  // Reset status
242  *error = 0;
243  localError = 0;
244  fileNumber = 0;
245  dspTrn->exists = 0;
246  format->hasData = 0;
247 
248  // Open the list of files
249  if ((inFitsBatchPtr = fopen (fileNames->inFitsBatch, "r")) == NULL)
250  {
251  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,
252  "Cannot open input FITS file list\n No data preprocessing has been carried out for this batch");
253  freeImageFormat (format);
254  free (fileTemp);
255  free (classification);
256  *error = 1;
257  return;
258  }
259 
260  // Loop through the files
261  while (fgets (fileTemp, MAX_STRING_LENGTH, inFitsBatchPtr) != NULL)
262  {
263  sprintf (classification, "%s", "");
264  sscanf (fileTemp, "%s%s", fileNames->inFitsName, classification);
265  if (diagnostic)cpl_msg_info(cpl_func,"\n Processing file %s \n", fileNames->inFitsName);
266  fprintf(midiReportPtr, "\n Processing file %s \n", fileNames->inFitsName);
267 
268  // Get 'extNumOfImagingDataFile' extension number of IMAGING_DATA in input file
269  extNumOfImagingDataFile = findImagingDataExtension (fileNames->inFitsName, TAB_IMAGING_DATA, &localError);
270  if (localError)
271  {
272  *error = 1;
273  break;
274  }
275 
276  // Get Image Format parameters
277  if (extNumOfImagingDataFile > 0)
278  {
279  getImageFormat (fileNames->inFitsName, extNumOfImagingDataFile, format, &localError);
280  if (localError)
281  {
282  *error = 1;
283  break;
284  }
285  }
286  else format->hasData = 0;
287 
288  // Check if the file has data
289  if (format->hasData)
290  {
291  // Check Categ, Tech and Type and then compute the image size
292  if ((strcmp (format->obsCatg, "CALIB") == 0) &&
293  (strcmp (format->obsTech, "SPECTRUM") == 0) &&
294  (strcmp (format->obsType, "WAVE") == 0))
295  {
296  // Save beam combiner and sutter ID
297  sprintf (dspTrn->beamCombiner[fileNumber], "%s", format->beamCombiner);
298  sprintf (dspTrn->shutterId[fileNumber], "%s", format->shutterId);
299  sprintf (dspTrn->cameraId[fileNumber], "%s", format->cameraId);
300  sprintf (dspTrn->grismId[fileNumber], "%s", format->grismId);
301  sprintf (dspTrn->filterName[fileNumber], "%s", format->filterName);
302  if (diagnostic)cpl_msg_info(cpl_func," Beam combiner for file %d = %s\n", fileNumber+1, dspTrn->beamCombiner[fileNumber]);
303  if (diagnostic)cpl_msg_info(cpl_func," Shutter ID for file %d = %s\n", fileNumber+1, dspTrn->shutterId[fileNumber]);
304  if (diagnostic)cpl_msg_info(cpl_func," Camera ID for file %d = %s\n", fileNumber+1, dspTrn->cameraId[fileNumber]);
305  if (diagnostic)cpl_msg_info(cpl_func," Grism ID for file %d = %s\n", fileNumber+1, dspTrn->grismId[fileNumber]);
306  if (diagnostic)cpl_msg_info(cpl_func," Filter Name for file %d = %s\n", fileNumber+1, dspTrn->filterName[fileNumber]);
307  fprintf (midiReportPtr, " Beam combiner for file %d = %s\n", fileNumber+1, dspTrn->beamCombiner[fileNumber]);
308  fprintf (midiReportPtr, " Shutter ID for file %d = %s\n", fileNumber+1, dspTrn->shutterId[fileNumber]);
309  fprintf (midiReportPtr, " Camera ID for file %d = %s\n", fileNumber+1, dspTrn->cameraId[fileNumber]);
310  fprintf (midiReportPtr, " Grism ID for file %d = %s\n", fileNumber+1, dspTrn->grismId[fileNumber]);
311  fprintf (midiReportPtr, " Filter Name for file %d = %s\n", fileNumber+1, dspTrn->filterName[fileNumber]);
312 
313  // Compute Integral Flux
314  computeIntegralFlux (fileNumber, fileNames->inFitsName, extNumOfImagingDataFile, format, dspTrn, &localError);
315  if (localError) *error = 1;
316 
317  // Set the flags
318  dspTrn->exists = 1;
319 
320  // Increment file number but make sure it is not greater than the allocated
321  fileNumber++;
322  if (fileNumber > numOfFiles)
323  {
324  *error = 1;
325  break;
326  }
327  }
328  else
329  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, "The above file is not suitable for this task");
330  }
331  else
332  {
333  if (diagnostic)
334  {
335  sprintf (midiMessage, "No data tables in %s. Not processed", fileNames->inFitsName);
336  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
337  }
338  }
339  }
340 
341  // Check if processing has been carried out
342  if (dspTrn->exists && !(*error) && (fileNumber == numOfFiles))
343  {
344  cpl_msg_info(cpl_func,"\nDispersive Transmission Inventry: \n");
345  cpl_msg_info(cpl_func,"================================ \n");
346  cpl_msg_info(cpl_func," Expected number of data files = %d\n", numOfFiles);
347  cpl_msg_info(cpl_func," Number of data files processed = %d\n", fileNumber);
348  cpl_msg_info(cpl_func,"\n");
349 
350  fprintf (midiReportPtr, "\nDispersive Transmission Inventry: \n");
351  fprintf (midiReportPtr, "================================ \n");
352  fprintf (midiReportPtr, " Expected number of data files = %d\n", numOfFiles);
353  fprintf (midiReportPtr, " Number of data files processed = %d\n", fileNumber);
354  fprintf (midiReportPtr, "\n");
355 
356  // Compute Dispersive Transmission
357  // -------------------------------
358  computeDispersivity (dspTrn, &localError);
359  }
360 
361  if (*error || localError)
362  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot access Dispersive Transmission for this batch");
363 
364 
365  // Close the file list
366  fclose (inFitsBatchPtr);
367 
368  // Release memory
369  freeImageFormat (format);
370  free (fileTemp);
371  free (classification);
372 
373  return;
374 }
375 /*****************************************************************************/
376 
377 
378 
379 /******************************************************************************
380 * European Southern Observatory
381 * VLTI MIDI Maintenance Templates Software
382 *
383 * Module name: computeIntegralFlux
384 * Input/Output: See function arguments to avoid duplication
385 * Description: Integrates number of counts over the full spectrum. That is obtains
386 * one number for each frame and then one number for the file and divides
387 * by the exposure time.
388 *
389 * History:
390 * 18-Aug-04 (csabet) Created
391 ******************************************************************************/
392 void computeIntegralFlux (
393  int fileNumber, // In: File number
394  char *fileName, // In: Name of file to process
395  int extensionNumber,// In: Extension number of the IMAGE_DATA
396  ImageFormat *format, // In: Pointer to the image format
397  DispersiveTrans *dspTrn, // Ou: Pointer to the dispersive transmission data structure
398  int *error) // Ou: Error status
399 
400 {
401 
402  // Local Declarations
403  // ------------------
404  const char routine[] = "computeIntegralFlux";
405  qfits_table *pTable = NULL;
406  short int *inData;
407  char *tempStr, *string, *title, *dataName;
408  float numOfSubInteg, subIntegTime, targetRegionFlux;
409  int i, foundData = 0, scalingOffset, indexData, subWindowSize,
410  targetPixelCount;
411  float *aveImage;
412  MidiCoords *target;
413 
414  cpl_image *pImage = NULL;
415  int nx,ny;
416  cpl_image *background = NULL;
417 /* cpl_image *temp = NULL; */
418 
419 
420 
421  // Algorithm
422  // ---------
423  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
424  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
425 
426  // Reset status
427  *error = 0;
428  subWindowSize = format->iXWidth * format->iYWidth;
429 
430  // Open IMAGING_DATA = INDEX 2
431  pTable = qfits_table_open (fileName, extensionNumber);
432  if (!pTable)
433  {
434  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot load IMAGING_DATA");
435  *error = 1;
436  return;
437  }
438 
439  // Get data table information
440  for (i = 0; i < pTable->nc; i++)
441  {
442  if (strcmp (pTable->col[i].tlabel, "DATA1") == 0)
443  {
444  foundData = 1;
445  indexData = i;
446  }
447  }
448  if (foundData == 0)
449  {
450  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find requested columns in data FITS file");
451  qfits_table_close (pTable);
452  *error = 1;
453  return;
454  }
455 
456  // Read column DATA
457  inData = (short int*) qfits_query_column (pTable, indexData, NULL);
458 
459  // Read Scaling Offset
460  dataName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
461  for (i = 14; i < 25; i++)
462  {
463  sprintf (dataName, "TZERO%d", i);
464  tempStr = qfits_query_ext (fileName, dataName, extensionNumber);
465  if (tempStr != NULL)
466  {
467  if (diagnostic)cpl_msg_info(cpl_func,"Scaling Offset = %s\n", tempStr);
468  if (diagnostic) fprintf (midiReportPtr, "Scaling Offset = %s\n", tempStr);
469  sscanf (tempStr, "%d", &scalingOffset);
470  break;
471  }
472  }
473  if (tempStr == NULL)
474  {
475  scalingOffset = 0;
476  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Scaling Offset. It is set to 0");
477  }
478  free (dataName);
479 
480  // Create averaged image for the whole file. This also allows a check on existence of a target
481  aveImage = (float *) calloc (subWindowSize, sizeof(float));
482  createAveragedImage (inData, scalingOffset, format, aveImage);
483 
484  /*##############################################################################*/
485  /* Subtract the background column by column */
486 
487  pImage = cpl_image_wrap_float(format->iXWidth, format->iYWidth, aveImage);
488 
489  nx =format->iXWidth;
490  ny=format->iYWidth;
491 
492  background = image_filter_median( pImage, 1, 2*ny );
493  cpl_image_subtract ( pImage, background );
494 
495  cpl_image_unwrap(pImage);
496 
497  cpl_image_delete(background);
498  /*##############################################################################*/
499 
500 
501 
502 
503  // Create an image FITS file
504  title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
505  string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
506  sprintf (title, "AveImg%d", fileNumber+1);
507  sprintf (string, "file %d", fileNumber+1);
508  createFitsImage (string, title, fileName, format->iXWidth, format->iYWidth, aveImage);
509  free (string);
510  free (title);
511 
512  // Create plot file
513  if (plotFile)
514  {
515  title = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
516  string = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
517  sprintf (string, "3dAveImg%d", fileNumber+1);
518  sprintf (title, "Averaged Image, filter %s", format->filterName);
519  midiCreatePlotFile3D (string, title, "X", "Y", "Flux",
520  0, aveImage, format->iXWidth, format->iYWidth, "lines", "3");
521  free (title);
522  free (string);
523  }
524 
525  // Get Target coordinates and save the result
526  target = (MidiCoords *) calloc (1, sizeof (MidiCoords));
527  getDspTrnTargetCoords (fileNumber+1, dspTrn->grismId[fileNumber], aveImage, format, target, error);
528  if (*error)
529  {
530  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot determine integral flux");
531  qfits_table_close (pTable);
532  free (inData);
533  free (aveImage);
534  return;
535  }
536  dspTrn->target[fileNumber].xCoord = target->xCoord;
537  dspTrn->target[fileNumber].yCoord = target->yCoord;
538  dspTrn->target[fileNumber].dxCoord = target->dxCoord;
539  dspTrn->target[fileNumber].dyCoord = target->dyCoord;
540 
541  cpl_msg_info(cpl_func,"\nCoordinates and flux results in file %s: \n", fileName);
542  cpl_msg_info(cpl_func,"==================================== \n");
543  cpl_msg_info(cpl_func," Target coordinates = %f %f %f %f\n", target->xCoord, target->yCoord,
544  target->dxCoord, target->dyCoord);
545  fprintf (midiReportPtr, "\nCoordinates and flux results in file %s: \n", fileName);
546  fprintf (midiReportPtr, "==================================== \n");
547  fprintf (midiReportPtr, " Target coordinates = %f %f %f %f\n", target->xCoord, target->yCoord,
548  target->dxCoord, target->dyCoord);
549 
550  // Compute total flux in the target region
551  computeImageFlux (aveImage, format, target, &targetRegionFlux, &targetPixelCount, error);
552  cpl_msg_info(cpl_func," Target flux with background = %f\n", targetRegionFlux);
553  cpl_msg_info(cpl_func," Target pixel count = %d\n", targetPixelCount);
554  fprintf (midiReportPtr, " Target flux with background = %f\n", targetRegionFlux);
555  fprintf (midiReportPtr, " Target pixel count = %d\n", targetPixelCount);
556 
557  // Compute mean target flux
558  dspTrn->meanFlux[fileNumber] = targetRegionFlux / targetPixelCount;
559  cpl_msg_info(cpl_func," Target mean flux with background = %f\n", dspTrn->meanFlux[fileNumber]);
560  fprintf (midiReportPtr, " Target mean flux with background = %f\n", dspTrn->meanFlux[fileNumber]);
561 
562  // Get integration time
563  tempStr = qfits_query_hdr (fileName, "HIERARCH ESO DET DIT");
564  if (tempStr != NULL)
565  sscanf(tempStr, "%f", &subIntegTime);
566  else
567  {
568  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Integration time");
569  qfits_table_close (pTable);
570  free (inData);
571  *error = 1;
572  free (aveImage);
573  free (target);
574  return;
575  }
576  tempStr = qfits_query_hdr (fileName, "HIERARCH ESO DET NDIT");
577  if (tempStr != NULL)
578  sscanf(tempStr, "%f", &numOfSubInteg);
579  else
580  {
581  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Number of Integrations");
582  qfits_table_close (pTable);
583  free (inData);
584  *error = 1;
585  free (target);
586  free (aveImage);
587  return;
588  }
589  // Compute integral flux
590  dspTrn->integTime[fileNumber] = numOfSubInteg * subIntegTime;
591  dspTrn->integFlux[fileNumber] = dspTrn->meanFlux[fileNumber] / dspTrn->integTime[fileNumber];
592 
593  cpl_msg_info(cpl_func," Integration time = %f\n", dspTrn->integTime[fileNumber]);
594  fprintf (midiReportPtr, " Integration time = %f\n", dspTrn->integTime[fileNumber]);
595 
596  cpl_msg_info(cpl_func," Total integral flux = %f\n", dspTrn->integFlux[fileNumber]);
597  fprintf (midiReportPtr, " Total integral flux = %f\n", dspTrn->integFlux[fileNumber]);
598 
599  // Release memory
600  qfits_table_close (pTable);
601  free (inData);
602  free (aveImage);
603  free (target);
604 
605  return;
606 }
607 /*****************************************************************************/
608 
609 
610 
611 /******************************************************************************
612 * European Southern Observatory
613 * VLTI MIDI Maintenance Templates Software
614 *
615 * Module name: computeDispersivity
616 * Input/Output: See function arguments to avoid duplication
617 * Description:
618 *
619 * History:
620 * 18-Aug-04 (csabet) Created
621 ******************************************************************************/
622 void computeDispersivity (
623  DispersiveTrans *dspTrn, // Ou: Pointer to the dispersive transmission data structure
624  int *error) // Ou: Error status
625 {
626 
627  /* Local Declarations
628  --------------------*/
629  const char routine[] = "computeDispersivity";
630  float *dummy;
631 
632  /* Algorithm
633  -----------*/
634  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
635  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
636 
637  // Reset status
638  *error = 0;
639 
640  // Check keywords to make sure that correct parameters are used from files
641  if ((strcmp (dspTrn->filterName[0], dspTrn->filterName[3]) == 0) &&
642  (strcmp (dspTrn->grismId[0], "OPEN") == 0) &&
643  ((strcmp (dspTrn->grismId[3], "PRISM") == 0) || (strcmp (dspTrn->grismId[3], "GRISM") == 0)))
644  {
645  dspTrn->transmission[0] = dspTrn->integFlux[3] / dspTrn->integFlux[0];
646  cpl_msg_info(cpl_func," Dispersive Transmission for files 4 / 1 = %f\n", dspTrn->transmission[0]);
647  fprintf (midiReportPtr, " Dispersive Transmission for files 4 / 1 = %f\n", dspTrn->transmission[0]);
648  }
649  else
650  {
651  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Files 1 and 4 are not compatible");
652  *error = 1;
653  }
654 
655  if ((strcmp (dspTrn->filterName[1], dspTrn->filterName[4]) == 0) &&
656  (strcmp (dspTrn->grismId[1], "OPEN") == 0) &&
657  ((strcmp (dspTrn->grismId[4], "PRISM") == 0) || (strcmp (dspTrn->grismId[4], "GRISM") == 0)))
658  {
659  dspTrn->transmission[1] = dspTrn->integFlux[4] / dspTrn->integFlux[1];
660  cpl_msg_info(cpl_func," Dispersive Transmission for files 5 / 2 = %f\n", dspTrn->transmission[1]);
661  fprintf (midiReportPtr, " Dispersive Transmission for files 5 / 2 = %f\n", dspTrn->transmission[1]);
662  }
663  else
664  {
665  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Files 2 and 5 are not compatible");
666  *error = 1;
667  }
668 
669  if ((strcmp (dspTrn->filterName[2], dspTrn->filterName[5]) == 0) &&
670  (strcmp (dspTrn->grismId[2], "OPEN") == 0) &&
671  ((strcmp (dspTrn->grismId[5], "PRISM") == 0) || (strcmp (dspTrn->grismId[5], "GRISM") == 0)))
672  {
673  dspTrn->transmission[2] = dspTrn->integFlux[5] / dspTrn->integFlux[2];
674  cpl_msg_info(cpl_func," Dispersive Transmission for files 6 / 3 = %f\n", dspTrn->transmission[2]);
675  fprintf (midiReportPtr, " Dispersive Transmission for files 6 / 3 = %f\n", dspTrn->transmission[2]);
676  }
677  else
678  {
679  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Files 3 and 6 are not compatible");
680  *error = 1;
681  }
682 
683  if (plotFile)
684  {
685  dummy = (float *) calloc (3, sizeof (float));
686  dummy[0] = 1.0;
687  dummy[1] = 2.0;
688  dummy[2] = 3.0;
689 
690  midiCreatePlotFile2D2P ("TransmissivityProfile", "Transmissivity Profile",
691  "Spectroscopy / Photometry: 4/1, 5/2, 6/3", "Transmissivity", 0, dummy, dspTrn->transmission, 0, 3, 1);
692  free (dummy);
693  }
694  return;
695 }
696 /*****************************************************************************/
697 
698 
699 
700 /******************************************************************************
701 * European Southern Observatory
702 * VLTI MIDI Maintenance Templates Software
703 *
704 * Module name: getDspTrnTargetCoords
705 * Input/Output: See function arguments to avoid duplication
706 * Description: This routine uses a 2D Gaussian fit algorithm to search for the target
707 * The search area will be confined to a region around the global maximum.
708 * In order to distiguish a true global maximum with a bad pixel a filter
709 * must first be applied to the entire data set. The Gaussian fit will
710 * give the size as well as the coordinates of the target. We choose the
711 * size of the background region to be larger than the size of the target
712 * window
713 *
714 * History:
715 * 18-Aug-04 (csabet) Created
716 ******************************************************************************/
717 void getDspTrnTargetCoords (
718  int fileNumber, // In: File number
719  char *grismId, // In: Grism ID indicates Spectroscopy or Imaging
720  float *image, // In: Pointer to the image data
721  ImageFormat *format, // In: Pointer to the image format
722  MidiCoords *target, // Ou: Pointer to the target coordinate structures
723  int *error) // Ou: Error status
724 {
725 
726  // Local Declarations
727  // ------------------
728  const char routine[] = "getDspTrnTargetCoords";
729  int sizeSearch, dimension, xPinhole, yPinhole;
730  double xTarget, yTarget, sizeXTarget, sizeYTarget;
731 
732  // Algorithm
733  // ---------
734  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
735  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
736 
737  // Reset status
738  *error = 0;
739 
740  // Set dimensionality for a suitable Gaussian fit
741  if (strcmp (grismId, "OPEN") == 0) dimension = 2; // Imaging mode
742  else if ((strcmp (grismId, "GRISM") == 0) || (strcmp (grismId, "PRISM") == 0)) dimension = 1; // Spectroscopy mode
743  else
744  {
745  sprintf (midiMessage, "Unknown Grism ID %s: ", grismId);
746  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
747  *error = 1;
748  return;
749  }
750 
751  // If all is well then use the appropriate parameters to find the coordinates of the target
752  // Override dimensionality. CPL Gaussian fit relies on a given search.
753  dimension = 1;
754  sizeSearch = SIZE_SEARCH_DSP_TRN;
755  xPinhole = X_DSP_TRN;
756  yPinhole = Y_DSP_TRN;
757  midiGaussianFit (fileNumber, dimension, image, format->iXWidth, format->iYWidth, xPinhole, yPinhole,
758  sizeSearch, &xTarget, &yTarget, &sizeXTarget, &sizeYTarget, error);
759 
760  // Save output results
761  if (*error) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot determine target coordinates");
762  else
763  {
764  /* Fixed aperture used after background subtraction */
765  target->xCoord = xTarget - 7.;
766  target->yCoord = yTarget - 7.;
767  target->dxCoord = xTarget + 7.;
768  target->dyCoord = yTarget + 7.;
769 
770  /*
771  target->xCoord = xTarget - 0.5 * sizeXTarget;
772  target->yCoord = yTarget - 0.5 * sizeYTarget;
773  target->dxCoord = xTarget + 0.5 * sizeXTarget;
774  target->dyCoord = yTarget + 0.5 * sizeYTarget;
775  */
776  }
777 
778  return;
779 }
780 //*****************************************************************************
781