MIDI Pipeline Reference Manual  2.8.3
midiFitsUtility.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: midiFitsUtility.c
7 * Description: Contains general routines for FITS handling
8 *
9 * History:
10 * 25-Apr-03 (csabet) Created
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /******************************************************************************
15 * Compiler directives
16 ******************************************************************************/
17 
18 /******************************************************************************
19 * Include files
20 ******************************************************************************/
21 #include <sys/stat.h>
22 #include <stdio.h>
23 #include <cpl.h>
24 #include "midiGlobal.h"
25 #include "midiLib.h"
26 #include "midiFitsUtility.h"
27 #include "diagnostics.h"
28 #include "memoryHandling.h"
29 #include "errorHandling.h"
30 #include "fileHandling.h"
31 #include "qfits.h"
32 #include "qfits_rw.h"
33 
34 /**********************************************************
35 * Global Variables
36 **********************************************************/
37 
38 /**********************************************************
39 * Constant definitions
40 **********************************************************/
41 
42 /*============================ C O D E A R E A ===========================*/
43 
44 
45 
46 /******************************************************************************
47 * European Southern Observatory
48 * VLTI MIDI Data Reduction Software
49 *
50 * Module name: getImageFormat
51 * Input/Output: See function arguments to avoid duplication
52 * Description: This routine obtains all the image formats for later processing
53 *
54 * History:
55 * 14-Aug-03 (csabet) Created
56 ******************************************************************************/
57 void getImageFormat (
58  char *inFitsFile, // In: Name of the input FITS file
59  int extNumOfImagingDataFile, // In: Extension number of the IMAGING_DATA in input file
60  ImageFormat *format, // Ou: Pointer to format structure
61  int *error) // Ou: Error status
62 {
63 
64  // Local Declarations
65  // ------------------
66  const char routine[] = "getImageFormat";
67  char *sTemp, *cleanString, *stringQfits, *emptyString;
68  int numOfExtensions;
69  struct stat buf;
70 
71  // Algorithm
72  // ---------
73  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
74  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
75 
76  // Reset status
77  *error = 0;
78  format->numOfPinholes = 0;
79 
80  // Check if the file exist
81  if (stat (inFitsFile, &buf) == -1)
82  {
83 
84  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find requested FITS file");
85  *error = 1;
86  return;
87  }
88  else if ((qfits_is_fits (inFitsFile)) != 1) // Check if this is a FITS file
89  {
90  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Non-FITS file");
91  *error = 1;
92  return;
93  }
94 
95  // Check if the file has data tables
96  numOfExtensions = qfits_query_n_ext (inFitsFile);
97  if (numOfExtensions < 1)
98  {
99  format->hasData = 0;
100  return;
101  }
102  else format->hasData = 1;
103 
104  // Allocate memory
105  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
106  emptyString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
107  strcpy (emptyString, "UNKNOWN");
108 
109  // Get data Observation Category
110  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO DPR CATG");
111  if (stringQfits == NULL)
112  {
113  sprintf (format->obsCatg, "%s", emptyString);
114  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'DPR CATG'");
115  }
116  else
117  {
118  cleanUpString (stringQfits, cleanString);
119  sprintf (format->obsCatg, "%s", cleanString);
120  }
121 
122  // Get data Observation Technique
123  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO DPR TECH");
124  if (stringQfits == NULL)
125  {
126  sprintf (format->obsTech, "%s", emptyString);
127  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'DPR TECH'");
128  }
129  else
130  {
131  cleanUpString (stringQfits, cleanString);
132  sprintf (format->obsTech, "%s", cleanString);
133  }
134 
135  // Get data Observation Type
136  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO DPR TYPE");
137  if (stringQfits == NULL)
138  {
139  sprintf (format->obsType, "%s", emptyString);
140  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'DPR TYPE'");
141  }
142  else
143  {
144  cleanUpString (stringQfits, cleanString);
145  sprintf (format->obsType, "%s", cleanString);
146  }
147 
148  // Get Shutter ID
149  stringQfits = qfits_query_ext (inFitsFile, "HIERARCH ESO INS SHUT ID", 0);
150  if (stringQfits == NULL)
151  {
152  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'SHUT ID'");
153  sprintf (format->shutterId, "%s", emptyString);
154  }
155  else
156  {
157  cleanUpString (stringQfits, cleanString);
158  sprintf (format->shutterId, "%s", cleanString);
159  }
160 
161  // Get beam combiner
162  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS OPT1 ID");
163  if (stringQfits == NULL)
164  {
165  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'OPT1 ID'");
166  sprintf (format->beamCombiner, "%s", emptyString);
167  }
168  else
169  {
170  cleanUpString (stringQfits, cleanString);
171  sprintf (format->beamCombiner, "%s", cleanString);
172  }
173 
174  // Determine if data is dispesed using the beam combiner
175  if (strcmp (format->beamCombiner, "HIGH_SENS") == 0)
176  format->isDisp = 1;
177  else if (strcmp (cleanString, "SCI_PHOT") == 0)
178  format->isDisp = 1;
179  else if (strcmp (cleanString, "OPEN") == 0)
180  format->isDisp = 0;
181  else
182  {
183  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Beam Combiner 'OPT1 ID'");
184  sprintf (format->beamCombiner, "%s", emptyString);
185  }
186 
187  // Get target name
188  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO OBS TARG NAME");
189  if (stringQfits == NULL)
190  {
191  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'TARG NAME'");
192  sprintf (format->targetName, "%s", emptyString);
193  }
194  else
195  {
196  cleanUpString (stringQfits, cleanString);
197  sprintf (format->targetName, "%s", cleanString);
198  }
199 
200  // Get Grism ID
201  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS GRIS NAME");
202  if (stringQfits == NULL)
203  {
204  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'GRIS NAME'");
205  sprintf (format->grismId, "%s", emptyString);
206  }
207  else
208  {
209  cleanUpString (stringQfits, cleanString);
210  sprintf (format->grismId, "%s", cleanString);
211  }
212 
213  // Get Camera Name
214  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS CAM ID");
215  if (stringQfits == NULL)
216  {
217  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Camera ID");
218  sprintf (format->cameraId, "%s", emptyString);
219  }
220  else
221  {
222  cleanUpString (stringQfits, cleanString);
223  sprintf (format->cameraId, "%s", cleanString);
224  }
225 
226  // Get Filter ID
227  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS FILT NAME");
228  if (stringQfits == NULL)
229  {
230  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Filter Name");
231  sprintf (format->filterName, "%s", emptyString);
232  }
233  else
234  {
235  cleanUpString (stringQfits, cleanString);
236  sprintf (format->filterName, "%s", cleanString);
237  }
238 
239  // Get TPL Start
240  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO TPL START");
241  if (stringQfits == NULL)
242  {
243  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'TPL START'");
244  sprintf (format->tplStart, "%s", emptyString);
245  }
246  else
247  {
248  cleanUpString (stringQfits, cleanString);
249  sprintf (format->tplStart, "%s", cleanString);
250  }
251 
252  // Get TPL Name
253  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO TPL NAME");
254  if (stringQfits == NULL)
255  {
256  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'TPL NAME'");
257  sprintf (format->tplName, "%s", emptyString);
258  }
259  else
260  {
261  cleanUpString (stringQfits, cleanString);
262  sprintf (format->tplName, "%s", cleanString);
263  }
264 
265  // Get MIDI subwindow format from the IMAGING_DATA
266  sTemp = qfits_query_ext (inFitsFile, "TDIM14", extNumOfImagingDataFile);
267  if (sTemp == NULL) sTemp = qfits_query_ext (inFitsFile, "TDIM17", extNumOfImagingDataFile);
268  if (sTemp == NULL) sTemp = qfits_query_ext (inFitsFile, "TDIM20", extNumOfImagingDataFile);
269  if (sTemp == NULL)
270  {
271  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get TDIM14, TDIM17 or TDIM20");
272  *error = 1;
273  }
274  else sscanf (sTemp, "'(%d,%d) '", &(format->iXWidth), &(format->iYWidth));
275 
276  // Get number of frames
277  sTemp = qfits_query_ext (inFitsFile, "NAXIS2", extNumOfImagingDataFile);
278  if ((sTemp == NULL) || (!sscanf (sTemp, "%d", &(format->numOfFrames))))
279  {
280  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get NAXIS2");
281  *error = 1;
282  }
283 
284  // Get frames per scan
285  sTemp = qfits_query_ext (inFitsFile, "MAXSTEP", extNumOfImagingDataFile);
286  if ((sTemp == NULL) || (!sscanf (sTemp, "%d", &(format->framesPerScan))))
287  {
288  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get MAXSTEP");
289  *error = 1;
290  }
291 
292  // Compute sub-image size
293  format->subWindowSize = format->iXWidth * format->iYWidth;
294 
295 
296  // Get number of pinholes. Get SLIT Name
297  stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS SLIT NAME");
298  if (stringQfits == NULL)
299  {
300  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'SLIT NAME'");
301  cleanUpString (stringQfits, emptyString);
302  }
303  else
304  {
305  cleanUpString (stringQfits, cleanString);
306  sprintf (format->slitName, "%s", cleanString);
307  if (strcmp (cleanString, "SLIT_0.05") == 0) format->numOfPinholes = 1; // size = 0.05
308  else if (strcmp (cleanString, "SLIT_0.1") == 0) format->numOfPinholes = 1; // size = 0.1
309  else if (strcmp (cleanString, "SLIT_0.2") == 0) format->numOfPinholes = 1; // size = 0.2
310  else if (strcmp (cleanString, "S_0.4C") == 0) format->numOfPinholes = 1; // size = 0.4
311  else if (strcmp (cleanString, "T_0.07C") == 0) format->numOfPinholes = 3; // size = 0.07 centred
312  else if (strcmp (cleanString, "T_0.11C") == 0) format->numOfPinholes = 3; // size = 0.11 centred
313  else if (strcmp (cleanString, "T_0.17C") == 0) format->numOfPinholes = 3; // size = 0.17 centred
314  else if (strcmp (cleanString, "OPEN") == 0) format->numOfPinholes = 0;
315  else if (strcmp (cleanString, "FullField") == 0) format->numOfPinholes = 0;
316  else
317  {
318  if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown 'SLIT NAME'");
319  cleanUpString (stringQfits, emptyString);
320  }
321  }
322 
323  // Compute number of detector regions
324  format->numOfRegionsToProcess = 2;
325  if (strcmp (format->beamCombiner, "SCI_PHOT") == 0)
326  format->numOfDetectorRegions = 4;
327  else
328  format->numOfDetectorRegions = 2;
329 
330  // Log results
331  if (diagnostic)
332  {
333  cpl_msg_info(cpl_func,"FORMAT OF FILE %s \n", inFitsFile);
334  cpl_msg_info(cpl_func,"-------------- \n");
335  cpl_msg_info(cpl_func,"Batch Template Name = %s \n", batchTemplate);
336  cpl_msg_info(cpl_func,"FITS TPL NAME = %s \n", format->tplName);
337  cpl_msg_info(cpl_func,"Observation Category = %s \n", format->obsCatg);
338  cpl_msg_info(cpl_func,"Observation Technique = %s \n", format->obsTech);
339  cpl_msg_info(cpl_func,"Observation Type = %s \n", format->obsType);
340  cpl_msg_info(cpl_func,"Beam Combiner = %s \n", format->beamCombiner);
341  cpl_msg_info(cpl_func,"Filter Name = %s \n", format->filterName);
342  cpl_msg_info(cpl_func,"Shutter ID = %s \n", format->shutterId);
343  cpl_msg_info(cpl_func,"Grism ID = %s \n", format->grismId);
344  cpl_msg_info(cpl_func,"Target Name = %s \n", format->targetName);
345  cpl_msg_info(cpl_func,"Slit Name = %s \n", format->slitName);
346  cpl_msg_info(cpl_func,"TPL Start = %s \n", format->tplStart);
347  cpl_msg_info(cpl_func,"Number of Frames = %d \n", format->numOfFrames);
348  if (strcmp (format->shutterId, "ABOPEN") == 0)
349  cpl_msg_info(cpl_func,"Frames per scan = %d \n", format->framesPerScan);
350  cpl_msg_info(cpl_func,"X, Column dimension = %d \n", format->iXWidth);
351  cpl_msg_info(cpl_func,"Y, Column dimension = %d \n", format->iYWidth);
352  cpl_msg_info(cpl_func,"Sub-window size = %d \n", format->subWindowSize);
353  cpl_msg_info(cpl_func,"Number of Pinholes = %d \n", format->numOfPinholes);
354  cpl_msg_info(cpl_func,"Number of Detector Regions = %d \n", format->numOfDetectorRegions);
355  if (strcmp(format->beamCombiner, "SCI_PHOT") == 0)
356  {
357  cpl_msg_info(cpl_func,"Number of Interf Regions = %d \n", format->numOfRegionsToProcess);
358  cpl_msg_info(cpl_func,"Number of Photom A Regions = %d \n", format->numOfRegionsToProcess-1);
359  cpl_msg_info(cpl_func,"Number of Photom B Regions = %d \n", format->numOfRegionsToProcess-1);
360  }
361  else
362  {
363  cpl_msg_info(cpl_func,"Number of Regions to process = %d \n", format->numOfRegionsToProcess);
364  }
365  cpl_msg_info(cpl_func,"\n");
366  }
367 
368  fprintf (midiReportPtr, "FORMAT OF FILE %s \n", inFitsFile);
369  fprintf (midiReportPtr, "-------------- \n");
370  fprintf (midiReportPtr, "Batch Template Name = %s \n", batchTemplate);
371  fprintf (midiReportPtr, "FITS TPL NAME = %s \n", format->tplName);
372  fprintf (midiReportPtr, "Observation Category = %s \n", format->obsCatg);
373  fprintf (midiReportPtr, "Observation Technique = %s \n", format->obsTech);
374  fprintf (midiReportPtr, "Observation Type = %s \n", format->obsType);
375  fprintf (midiReportPtr, "Beam Combiner = %s \n", format->beamCombiner);
376  fprintf (midiReportPtr, "Filter Name = %s \n", format->filterName);
377  fprintf (midiReportPtr, "Shutter ID = %s \n", format->shutterId);
378  fprintf (midiReportPtr, "Grism ID = %s \n", format->grismId);
379  fprintf (midiReportPtr, "Target Name = %s \n", format->targetName);
380  fprintf (midiReportPtr, "Slit Name = %s \n", format->slitName);
381  fprintf (midiReportPtr, "TPL Start = %s \n", format->tplStart);
382  fprintf (midiReportPtr, "Number of Frames = %d \n", format->numOfFrames);
383  if (strcmp (format->shutterId, "ABOPEN") == 0)
384  fprintf (midiReportPtr, "Frames per scan = %d \n", format->framesPerScan);
385  fprintf (midiReportPtr, "X, Column dimension = %d \n", format->iXWidth);
386  fprintf (midiReportPtr, "Y, Column dimension = %d \n", format->iYWidth);
387  fprintf (midiReportPtr, "Sub-window size = %d \n", format->subWindowSize);
388  fprintf (midiReportPtr, "Number of Pinholes = %d \n", format->numOfPinholes);
389  fprintf (midiReportPtr, "Number of Detector Regions = %d \n", format->numOfDetectorRegions);
390  if (strcmp(format->beamCombiner, "SCI_PHOT") == 0)
391  {
392  fprintf (midiReportPtr, "Number of Interf Regions = %d \n", format->numOfRegionsToProcess);
393  fprintf (midiReportPtr, "Number of Photom A Regions = %d \n", format->numOfRegionsToProcess-1);
394  fprintf (midiReportPtr, "Number of Photom B Regions = %d \n", format->numOfRegionsToProcess-1);
395  }
396  else
397  {
398  fprintf (midiReportPtr, "Number of Regions to process = %d \n", format->numOfRegionsToProcess);
399  }
400  fprintf (midiReportPtr, "\n");
401 
402  // Check validity of the format
403  if (format->iXWidth <= 0 || format->iYWidth <= 0 || format->numOfFrames <= 0 || format->framesPerScan <= 0)
404  {
405  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Invalid file format");
406  *error = 1;
407  }
408 
409  /* Release memory */
410  free (cleanString);
411  free (emptyString);
412 
413  return;
414 }
415 /*****************************************************************************/
416 
417 
418 /******************************************************************************
419 * European Southern Observatory
420 * VLTI MIDI Data Reduction Software
421 *
422 * Module name: getFilterData
423 * Input/Output: See function arguments to avoid duplication
424 * Description:
425 *
426 * History:
427 * 21-Jul-03 (csabet) Created
428 ******************************************************************************/
429 void getFilterData (
430  char *fileName, // In: Name of the input FITS file
431  FilterData *filterInfo,// IO: Pointer to the filter data structure
432  int *error) // Ou: Error status
433 {
434 
435  /* Local Declarations
436  --------------------*/
437  const char routine[] = "getFilterData";
438  char *tempStr, *cleanString;
439 
440  // Algorithm
441  // ---------
442  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
443  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
444 
445  // Reset status
446  *error = 0;
447 
448  // Allocate memory
449  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
450 
451  // Set the default values
452  filterInfo->optFreqLo = N_BAND_FILT_LONG; // micron
453  filterInfo->optFreqHi = N_BAND_FILT_SHORT; // micron
454 
455  if (diagnostic)cpl_msg_info(cpl_func,"Extracting filter data from %s \n", fileName);
456  if (diagnostic) fprintf (midiReportPtr, "Extracting filter data from %s \n", fileName);
457 
458  // Get filter name
459  tempStr = qfits_query_hdr (fileName, "HIERARCH ESO INS FILT NAME");
460  if (tempStr == NULL)
461  {
462  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Extracting filter name");
463  cpl_msg_info(cpl_func,"Default Lower %f Higher and Lower %f micron are used \n", filterInfo->optFreqLo, filterInfo->optFreqHi);
464  fprintf(midiReportPtr, "Default Higher %f micron and Lower %f micron are used \n", filterInfo->optFreqLo, filterInfo->optFreqHi);
465  free (cleanString);
466  return;
467  }
468  else
469  {
470  cleanUpString (tempStr, cleanString);
471  sscanf (cleanString, "%s", filterInfo->filterName);
472  }
473 
474  if (strcmp (filterInfo->filterName, "Nband") == 0)
475  {
476  filterInfo->optFreqLo = N_BAND_FILT_LONG;
477  filterInfo->optFreqHi = N_BAND_FILT_SHORT;
478  }
479  else if (strcmp (filterInfo->filterName, "OPEN") == 0)
480  {
481  filterInfo->optFreqLo = N_BAND_FILT_LONG;
482  filterInfo->optFreqHi = N_BAND_FILT_SHORT;
483  }
484  else if (strcmp (filterInfo->filterName, "SiC") == 0)
485  {
486  filterInfo->optFreqLo = SIC_FILT_LONG;
487  filterInfo->optFreqHi = SIC_FILT_SHORT;
488  }
489  else if (strcmp (filterInfo->filterName, "N8.7") == 0)
490  {
491  filterInfo->optFreqLo = N8_7_FILT_LONG;
492  filterInfo->optFreqHi = N8_7_FILT_SHORT;
493  }
494  else if (strcmp (filterInfo->filterName, "ArIII") == 0)
495  {
496  filterInfo->optFreqLo = ArIII_FILT_LONG;
497  filterInfo->optFreqHi = ArIII_FILT_SHORT;
498  }
499  else if (strcmp (filterInfo->filterName, "SIV") == 0)
500  {
501  filterInfo->optFreqLo = SIV_FILT_LONG;
502  filterInfo->optFreqHi = SIV_FILT_SHORT;
503  }
504  else if (strcmp (filterInfo->filterName, "N11.3") == 0)
505  {
506  filterInfo->optFreqLo = N11_3_FILT_LONG;
507  filterInfo->optFreqHi = N11_3_FILT_SHORT;
508  }
509  else if (strcmp (filterInfo->filterName, "NeII") == 0)
510  {
511  filterInfo->optFreqLo = NEII_FILT_LONG;
512  filterInfo->optFreqHi = NEII_FILT_SHORT;
513  }
514  else
515  {
516  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Filter ID");
517  cpl_msg_info(cpl_func," Default Higher %f micron and Lower %f micron are used \n\n", filterInfo->optFreqLo, filterInfo->optFreqHi);
518  fprintf(midiReportPtr, " Default Higher %f micron and Lower %f micron are used \n\n", filterInfo->optFreqLo, filterInfo->optFreqHi);
519  free (cleanString);
520  return;
521  }
522 
523  if (diagnostic)cpl_msg_info(cpl_func,"Filter ID = %s \n", filterInfo->filterName);
524  if (diagnostic)cpl_msg_info(cpl_func,"Lower optical filter wavelength = %f micron \n", filterInfo->optFreqHi);
525  if (diagnostic)cpl_msg_info(cpl_func,"Upper optical filter wavelength = %f micron \n\n", filterInfo->optFreqLo);
526  fprintf (midiReportPtr, "Filter ID = %s \n", filterInfo->filterName);
527  fprintf (midiReportPtr, "Lower optical filter wavelength = %f micron \n", filterInfo->optFreqHi);
528  fprintf (midiReportPtr, "Upper optical filter wavelength = %f micron \n\n", filterInfo->optFreqLo);
529 
530  // Convert to THz
531  filterInfo->optFreqLo = SPEED_OF_LIGHT / filterInfo->optFreqLo;
532  filterInfo->optFreqHi = SPEED_OF_LIGHT / filterInfo->optFreqHi;
533 
534  free (cleanString);
535 
536  return;
537 }
538 /*****************************************************************************/
539 
540 
541 /******************************************************************************
542 * European Southern Observatory
543 * VLTI MIDI Data Reduction Software
544 *
545 * Module name: getFitsExtensionNumber
546 * Input/Output: See function arguments to avoid duplication
547 * Description: This routine returns the extension number of the given extension
548 *
549 * History:
550 * 04-Oct-04 (csabet) Created
551 ******************************************************************************/
552 int getFitsExtensionNumber ( // Ou: Extension number
553  char *fileName, // In: Name of the input FITS file
554  const char *extName, // In: Given extension name
555  int *error) // Ou: Error status
556 {
557 
558  /* Local Declarations
559  --------------------*/
560  const char routine[] = "getFitsExtensionNumber";
561  int i, numOfExtensions = 0, extNumber = -1, found = 0;
562  char *stringTemp, *cleanString;
563 
564  /* Algorithm
565  -----------*/
566  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
567  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
568 
569  // Reset status
570  *error = 0;
571 
572  // Allocate memory
573  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
574 
575  // Get the total number of extensions
576  numOfExtensions = qfits_query_n_ext (fileName);
577 
578  if (diagnostic)cpl_msg_info(cpl_func,"Number of extensions = %d\n", numOfExtensions);
579  if (diagnostic) fprintf(midiReportPtr, "Number of extensions = %d\n", numOfExtensions);
580 
581  for (i = 0; i < numOfExtensions+1; i++)
582  {
583  stringTemp = qfits_query_ext (fileName, "EXTNAME", i);
584  if (stringTemp != NULL)
585  {
586  cleanUpString (stringTemp, cleanString);
587 
588  if (strcmp (cleanString, extName) == 0)
589  {
590  if (diagnostic)cpl_msg_info(cpl_func,"%s extension number = %d\n", extName, i);
591  if (diagnostic) fprintf (midiReportPtr, "%s extension number = %d\n", extName, i);
592  extNumber = i;
593  found = 1;
594  break;
595  }
596  }
597  }
598 
599  if (found == 0)
600  {
601  sprintf (midiMessage, "Cannot find FITS '%s' extension", extName);
602  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
603  *error = 1;
604  }
605 
606  // Release memory
607  free (cleanString);
608 
609  return (extNumber);
610 }
611 /*****************************************************************************/
612 
613 
614 
615 
616 /******************************************************************************
617 * European Southern Observatory
618 * VLTI MIDI Data Reduction Software
619 *
620 * Module name: createMidiMaskFile
621 * Input/Output: See function arguments to avoid duplication
622 * Description: Creates a mask file from an input photometry file
623 *
624 * History:
625 * 26-Nov-03 (csabet) Created
626 ******************************************************************************/
627 void createMidiMaskFile (
628  int widthX, // In: Column dimension
629  int widthY, // In: Column dimension
630  char *inFileName, // In: Name of the input data file
631  short int *inData1, // In: Pointer to the input data
632  short int *inData2, // In: Pointer to the input data
633  int numOfFrames, // In: Number of frames
634  int subWindowSize, // In: Sizeof the sub-window
635  char *maskFileName, // In: Full path name of the mask file
636  int *error) // Ou: Error status
637 {
638 
639  /* Local Declarations
640  --------------------*/
641  const char routine[] = "createMidiMaskFile";
642  float *outData1, *outData2;
643  qfits_header *primHeadMask;
644  FILE *maskFitsPtr;
645  char *dateTime;
646 
647  /* Algorithm
648  -----------*/
649  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
650  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
651 
652  /* Reset status */
653  *error = 0;
654 
655  if (diagnostic)cpl_msg_info(cpl_func,"Creating mask FITS file %s \n", maskFileName);
656  fprintf (midiReportPtr, "Creating mask FITS file %s \n", maskFileName);
657 
658  /* Extract Primary Header from the input file */
659  if (diagnostic)cpl_msg_info(cpl_func,"Extracting primary header from input FITS file %s \n", inFileName);
660  if (diagnostic) fprintf(midiReportPtr, "Extracting primary header from input FITS file %s \n", inFileName);
661  primHeadMask = qfits_header_read (inFileName);
662  if (primHeadMask == NULL)
663  {
664  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot extract primary header from input FITS file");
665  cpl_msg_info(cpl_func,"\nCannot Create mask FITS file for batch %d \n", batchNumber);
666  fprintf (midiReportPtr, "\nCannot Create mask FITS file for batch %d \n", batchNumber);
667  *error = 1;
668  return;
669  }
670 
671  /* Update primary header for the Mask file */
672  dateTime = qfits_get_datetime_iso8601 ();
673  qfits_header_mod (primHeadMask, "DATE", dateTime, "Date and time this file was written");
674  qfits_header_mod (primHeadMask, "INSTRUME", "MIDI", "MIDI Mask created by DRS pipeline" );
675  qfits_header_add (primHeadMask, "EXTNAME", "Primary", "MIDI Mask primary header", "" );
676 
677  /* Create the mask file and dump the primary header and close the file */
678  maskFitsPtr = fopen (maskFileName, "w");
679  qfits_header_dump (primHeadMask, maskFitsPtr);
680  qfits_header_destroy (primHeadMask);
681  fclose (maskFitsPtr);
682 
683  /* Allocate memory */
684  outData1 = (float *) calloc (subWindowSize, sizeof (float));
685  outData2 = (float *) calloc (subWindowSize, sizeof (float));
686 
687  /* Load data */
688  loadMidiMaskData (numOfFrames, subWindowSize, inData1, inData2, outData1, outData2, error);
689 
690  /* Create the tables and write the data */
691  writeMidiMaskFile (widthX, widthY, maskFileName, subWindowSize, outData1, outData2, error);
692  if (!(*error))
693  {
694  cpl_msg_info(cpl_func,"Created Mask FITS file: %s \n", maskFileName);
695  fprintf (midiReportPtr, "Created Mask FITS file: %s \n", maskFileName);
696  }
697 
698  /* Release memory */
699  free (outData1);
700  free (outData2);
701 
702  return;
703 }
704 /*****************************************************************************/
705 
706 
707 
708 /******************************************************************************
709 * European Southern Observatory
710 * VLTI MIDI Data Reduction Software
711 *
712 * Module name: loadMidiMaskData
713 * Input/Output: See function arguments to avoid duplication
714 * Description: Uses the input data to compute masks and loads MIDI mask data
715 * into appropriate data structures
716 *
717 *
718 * History:
719 * 26-Nov-03 (csabet) Created
720 ******************************************************************************/
721 void loadMidiMaskData (
722  int numOfFrames, // In: Number of frames in the data
723  int subWindowSize, // In: Size of sub-window
724  short *inData1, // In: Pointer to input data
725  short *inData2, // In: Pointer to input data
726  float *outData1, // Ou: Pointer to output data
727  float *outData2, // Ou: Pointer to output data
728  int *error) // Ou: Status
729 {
730  /* Local Declarations
731  --------------------*/
732  const char routine[] = "loadMidiMaskData";
733  int i, j, pixel;
734  char *tempFileName;
735  FILE *tempFilePtr;
736 
737  /* Algorithm
738  -----------*/
739  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
740  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
741 
742  /* Reset status */
743  *error = 0;
744 
745  /* Create mask data */
746  for (i = 0; i < subWindowSize; i++)
747  {
748  outData1[i] = 1.0;
749  outData2[i] = 1.0;
750  }
751 
752  if (plotFile && diagnostic)
753  {
754  /* Allocate memory */
755  tempFileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
756 
757  sprintf (tempFileName, "%s%s.AvePhotomData1.plt", outFileDir, outRootName);
758  tempFilePtr = fopen (tempFileName, "w");
759  for (j = 0; j < numOfFrames; j++)
760  {
761  for (pixel = 0; pixel < subWindowSize; pixel++)
762  {
763  i = j * subWindowSize + pixel;
764  fprintf (tempFilePtr, "%d ", inData1[i]);
765  }
766  fprintf (tempFilePtr, "\n");
767  }
768  fclose (tempFilePtr);
769 
770  sprintf (tempFileName, "%s%s.AvePhotomData2.plt", outFileDir, outRootName);
771  tempFilePtr = fopen (tempFileName, "w");
772  for (j = 0; j < numOfFrames; j++)
773  {
774  for (pixel = 0; pixel < subWindowSize; pixel++)
775  {
776  i = j * subWindowSize + pixel;
777  fprintf (tempFilePtr, "%d ", inData2[i]);
778  }
779  fprintf (tempFilePtr, "\n");
780  }
781  fclose (tempFilePtr);
782 
783  sprintf (tempFileName, "%s%s.maskData1.plt", outFileDir, outRootName);
784  tempFilePtr = fopen (tempFileName, "w");
785  for (i = 0; i < subWindowSize; i++)
786  fprintf (tempFilePtr, "%5.4f\n", outData1[i]);
787  fclose (tempFilePtr);
788 
789  sprintf (tempFileName, "%s%s.maskData2.plt", outFileDir, outRootName);
790  tempFilePtr = fopen (tempFileName, "w");
791  for (i = 0; i < subWindowSize; i++)
792  fprintf (tempFilePtr, "%5.4f\n", outData2[i]);
793  fclose (tempFilePtr);
794 
795  /* release memory */
796  free (tempFileName);
797  }
798 
799  return;
800 
801 }
802 /*****************************************************************************/
803 
804 
805 /******************************************************************************
806 * European Southern Observatory
807 * VLTI MIDI Data Reduction Software
808 *
809 * Module name: writeMidiMaskFile
810 * Input/Output: See function arguments to avoid duplication
811 * Description: Writes and creates appropriate tables into a new mask fits file
812 *
813 * History:
814 * 26-Nov-03 (csabet) Created
815 ******************************************************************************/
816 void writeMidiMaskFile (
817  int widthX, // In: Column dimension
818  int widthY, // In: Column dimension
819  char *maskFitsName, // In: Name of the FITS file
820  int numOfElements, // In: Number of elements
821  float *data1, // In: Pointer to the data1
822  float *data2, // In: Pointer to the data2
823  int *error) // Ou: Status
824 {
825  /* Local Declarations
826  --------------------*/
827  const char routine[] = "writeMidiMaskFile";
828  int numOfRowsImagData = 1, numOfElementsImagData;
829 
830  /* Algorithm
831  -----------*/
832  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
833  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
834 
835  /* Reset status */
836  *error = 0;
837 
838  if (diagnostic)cpl_msg_info(cpl_func,"Writing data into the mask FITS file \n\n");
839  fprintf (midiReportPtr, "Writing data into the mask FITS file \n\n");
840 
841  /* Write IMAGING_DATA extension */
842  numOfElementsImagData = numOfElements;
843  writeMidiMaskImagData (widthX, widthY, maskFitsName, numOfElementsImagData, numOfRowsImagData,
844  data1, data2, error);
845 
846  /* Add other extensions if needed */
847  /* TBD */
848 
849  return;
850 
851 }
852 /*****************************************************************************/
853 
854 
855 
856 /******************************************************************************
857 * European Southern Observatory
858 * VLTI MIDI Data Reduction Software
859 *
860 * Module name: writeMidiMaskImagData
861 * Input/Output: See function arguments to avoid duplication
862 * Description: Writes IMAGING_DATA fits binary table in the MIDI mask file
863 * with an appropriate header.
864 *
865 *
866 * History:
867 * 27-Nov-03 (csabet) Created.
868 ******************************************************************************/
869 void writeMidiMaskImagData (
870  int iWidthX, // In: Column dimension
871  int iWidthY, // In: Column dimension
872  char *maskFitsName, // In: Name of the FITS file
873  int numOfElements, // In: Number of elements in data1 and data2
874  int numOfRows, // In: Number of rows for data1 and data2
875  float *data1, // In: Pointer to the data1
876  float *data2, // In: Pointer to the data2
877  int *error) // Ou: Status
878 {
879 
880  /* Local Declarations
881  --------------------*/
882  const char routine[] = "writeMidiMaskImagData";
883  qfits_table *maskTable;
884  int tableWidth = -1, numOfColumns = 2;
885  float **array;
886  qfits_col *col;
887  qfits_header *imagDataHeader;
888  FILE *maskFitsPtr;
889  char *tdim2, *tdim1;
890 
891  /* Algorithm
892  -----------*/
893  tableWidth = -1;
894 
895  /* Reset status */
896  *error = 0;
897 
898  /* Allocate memory */
899  tdim1 = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
900  tdim2 = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
901 
902  /* Open the mask file for writing */
903  maskFitsPtr = fopen (maskFitsName, "a");
904  if (maskFitsPtr == NULL)
905  {
906  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open mask file");
907  *error = 1;
908  free (tdim1);
909  free (tdim2);
910  return;
911  }
912 
913  /* Create a new table */
914  maskTable = qfits_table_new (maskFitsName, QFITS_BINTABLE, tableWidth, numOfColumns, numOfRows);
915  if (!maskTable)
916  {
917  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create table");
918  *error = 1;
919  free (tdim1);
920  free (tdim2);
921  fclose (maskFitsPtr);
922  return;
923  }
924 
925  /* Fill in the column information */
926  col = maskTable->col;
927  qfits_col_fill (col, numOfElements, 2, sizeof(float), TFITS_BIN_TYPE_E, "DATA1", " ", " ", " ",
928  0, 0.0, 0, 1.0, 0);
929  col++;
930  qfits_col_fill (col, numOfElements, 2, sizeof(float), TFITS_BIN_TYPE_E, "DATA2", " ", " ", " ",
931  0, 0.0, 0, 1.0, numOfElements* sizeof(float));
932 
933  /* Create the imaging data header */
934  imagDataHeader = qfits_table_ext_header_default(maskTable);
935  qfits_header_add (imagDataHeader, "EXTNAME", "IMAGING_DATA", "MIDI Mask Extension name", "" );
936 
937 
938  /* Add dimensionality to the extension header */
939  sprintf (tdim1, "'(%d,%d)'", iWidthX, iWidthY);
940  sprintf (tdim2, "'(%d,%d)'", iWidthX, iWidthY);
941  qfits_header_add (imagDataHeader, "TDIM1", tdim1, "Column dimensionality", "" );
942  qfits_header_add (imagDataHeader, "TDIM2", tdim2, "Column dimensionality", "" );
943 
944 
945 // qfits_header_consoledump (imagDataHeader);
946 
947  /* Allocate space for the array of data */
948  array = (float **) calloc (numOfColumns, sizeof(float *));
949  array[0] = data1;
950  array[1] = data2;
951 
952  if (qfits_table_append_xtension_hdr (maskFitsPtr, maskTable, (const void **)array, imagDataHeader) == -1)
953  *error = 1;
954 
955  qfits_header_destroy (imagDataHeader);
956  qfits_table_close (maskTable);
957  free (array);
958  fclose (maskFitsPtr);
959  free (tdim1);
960  free (tdim2);
961 
962  return;
963 }
964 /*****************************************************************************/
965 
966 
967 /******************************************************************************
968 * European Southern Observatory
969 * VLTI MIDI Data Reduction Software
970 *
971 * Module name: findImagingDataExtension
972 * Input/Output: See function arguments to avoid duplication
973 * Description: This routine returns the extension number of the IMAGING_DATA
974 *
975 * History:
976 * 14-Aug-03 (csabet) Created. Derived from pballest July 2003
977 ******************************************************************************/
978 int findImagingDataExtension ( // Ou: Extension number
979  char *fileName, // In: Name of the input FITS file
980  int defaultExtensionNumber, // In: efault extension number
981  int *error) // Ou: Error status
982 {
983  /* Local Declarations
984  --------------------*/
985  const char routine[] = "findImagingDataExtension";
986  int currentExtension = 0;
987  int lastExtensionNumber = 0;
988  char *stringTemp, *cleanString;
989 
990  /* Algorithm
991  -----------*/
992  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
993  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
994 
995  /* Reset status */
996  *error = 0;
997 
998  /* Allocate memory */
999  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
1000 
1001  if (diagnostic)
1002  {
1003  sprintf (midiMessage, "Searching for IMAGING_DATA extension in data file %s", fileName);
1004  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1005  }
1006 
1007  /* Get the total number of extensions */
1008  lastExtensionNumber = qfits_query_n_ext (fileName);
1009 
1010  if (diagnostic)
1011  {
1012  sprintf (midiMessage, "Number of extensions = %d", lastExtensionNumber);
1013  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1014  }
1015 
1016  if (defaultExtensionNumber <= lastExtensionNumber)
1017  {
1018  stringTemp = qfits_query_ext (fileName, "EXTNAME", defaultExtensionNumber);
1019  cleanUpString (stringTemp, cleanString);
1020 
1021  if (strcmp (cleanString, "IMAGING_DATA") == 0)
1022  {
1023  currentExtension = defaultExtensionNumber;
1024  if (diagnostic)
1025  {
1026  sprintf (midiMessage, "IMAGING_DATA extension number = %d", currentExtension);
1027  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1028  }
1029 
1030  /* Found. return extension number */
1031  free (cleanString);
1032  return (currentExtension);
1033  }
1034  }
1035 
1036  /* In all other cases we search through all extensions for the proper one */
1037  for (currentExtension = 1; currentExtension <= lastExtensionNumber; ++currentExtension)
1038  {
1039  stringTemp = qfits_query_ext (fileName, "EXTNAME", currentExtension);
1040  cleanUpString (stringTemp, cleanString);
1041  if (strcmp (cleanString, "IMAGING_DATA") == 0) break;
1042  }
1043 
1044  if (currentExtension <= lastExtensionNumber)
1045  {
1046  if (diagnostic)
1047  {
1048  sprintf (midiMessage, "IMAGING_DATA extension number = %d", currentExtension);
1049  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1050  }
1051  free (cleanString);
1052  return (currentExtension);
1053  }
1054  else
1055  {
1056  if (diagnostic)
1057  {
1058  sprintf (midiMessage, "No IMAGING_DATA extension in %s", fileName);
1059  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1060  }
1061  free (cleanString);
1062  return (-1); // Indicating no data
1063  }
1064 
1065  if (diagnostic)
1066  {
1067  sprintf (midiMessage, "IMAGING_DATA extension number = %d", currentExtension);
1068  midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
1069  }
1070 
1071  /* Release memory */
1072  free (cleanString);
1073 
1074  return (currentExtension);
1075 }
1076 /*****************************************************************************/
1077 
1078 
1079 
1080 /******************************************************************************
1081 * European Southern Observatory
1082 * VLTI MIDI Data Reduction Software
1083 *
1084 * Module name: selectMask
1085 * Input/Output: See function arguments to avoid duplication
1086 * Description: Selects a mask file from the information given in the input
1087 * FITS file. If this Mask file does not exist or is not a FITS
1088 * file the program terminates.
1089 *
1090 *
1091 * History:
1092 * 14-Aug-03 (csabet) Created. Derived from pballest July 2003
1093 ******************************************************************************/
1094 void selectMask (
1095  enum MaskMode maskMode, // In: Method of choosing mask file
1096  MidiFiles *fileNames, // In: Pointer to the file structure
1097  char *maskFile, // Ou: Full name of the mask
1098  int *error) // Ou: Error status
1099 {
1100 
1101  // Local Declarations
1102  // ------------------
1103  const char routine[] = "selectMask";
1104  char *qfitsTemp, *stringTemp;
1105  struct stat buf;
1106  FILE *temp1Ptr = NULL, *temp2Ptr = NULL;
1107  size_t stringLength;
1108  unsigned int i;
1109  char nextChar, *cleanString;
1110 
1111  // Algorithm
1112  // ---------
1113  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1114  if (diagnostic > 4) fprintf( midiReportPtr, "Invoking routine '%s' \n", routine);
1115 
1116  // Reset status
1117  *error = 0;
1118 
1119  // Allocate memory
1120  stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char ));
1121  cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char ));
1122 
1123  // Determine where to look for the mask
1124  switch (maskMode)
1125  {
1126  case SPECIFIED:
1127  // Remove prefix
1128 /* removePathName (fileNames->maskFileName, midiReportPtr); */
1129 /* sprintf (maskFile, "%s%s", fileNames->maskFileDir, fileNames->maskFileName); */
1130  sprintf (maskFile, "%s", fileNames->maskFileName);
1131  cpl_msg_info(cpl_func,"maskFile: %s",maskFile);
1132  break;
1133 
1134  case SELECTED:
1135  qfitsTemp = qfits_query_hdr (fileNames->inFitsName, "HIERARCH ESO DET FITSTPL");
1136  if (qfitsTemp != NULL)
1137  {
1138  cleanUpString (qfitsTemp, cleanString);
1139 
1140  /* Replace the prefix */
1141  stringLength = strlen (cleanString);
1142  temp1Ptr = fopen("temp1.grb", "w");
1143  fprintf (temp1Ptr, "%s\n", cleanString);
1144  fclose (temp1Ptr);
1145  temp1Ptr = fopen("temp1.grb", "r");
1146  temp2Ptr = fopen("temp2.grb", "w");
1147  for (i = 0; i < stringLength; i++)
1148  {
1149  fscanf (temp1Ptr, "%c", &nextChar);
1150  if (i > 7) fprintf (temp2Ptr, "%c", nextChar);
1151  }
1152  fclose (temp1Ptr);
1153  fclose (temp2Ptr);
1154  temp2Ptr = fopen("temp2.grb", "r");
1155  fscanf (temp2Ptr, "%s\n", stringTemp);
1156  fclose (temp2Ptr);
1157 
1158  remove ("temp1.grb");
1159  remove ("temp2.grb");
1160  sprintf (maskFile, "%s%s%s", fileNames->maskFileDir, "minrtsMask", stringTemp);
1161  }
1162  else
1163  {
1164  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read mask name");
1165  *error = 1;
1166  }
1167  break;
1168 
1169  case COMPUTED:
1170  sprintf (maskFile, "%s%s", fileNames->maskFileDir, fileNames->maskFileName);
1171  break;
1172 
1173  default:
1174  break;
1175  }
1176 
1177  /* Rewrite the mask file for record */
1178  sprintf (fileNames->maskFileName, "%s", maskFile);
1179 
1180  /* Check if the file exist */
1181  if (strcmp (maskFile, "") == 0)
1182  {
1183  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find a suitable mask file");
1184  *error = 1;
1185  }
1186  else if (stat (maskFile, &buf) == -1)
1187  {
1188  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find mask file");
1189  *error = 1;
1190  }
1191  else if ((qfits_is_fits (maskFile)) != 1) /* Check if this is a FITS file */
1192  {
1193  midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found Non-FITS file");
1194  *error = 1;
1195  }
1196 
1197  /* Release memory */
1198  free (stringTemp);
1199  free (cleanString);
1200 
1201  return;
1202 }
1203 /*****************************************************************************/
1204 
1205 
1206 /******************************************************************************
1207 * European Southern Observatory
1208 * VLTI MIDI Maintenance Templates Software
1209 *
1210 * Module name: midiInitFormat
1211 * Input/Output: See function arguments to avoid duplication
1212 * Description: Initialises image format
1213 *
1214 * History:
1215 * 10-Jul-05 (csabet) created
1216 ******************************************************************************/
1217 void midiInitFormat (
1218  ImageFormat *format) // Ou: Pointer to the image format structure
1219 {
1220 
1221  // Local Declarations
1222  // ------------------
1223  const char routine[] = "midiInitFormat";
1224 
1225  // Algorithm
1226  // ---------
1227  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1228  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
1229 
1230  format->hasData = 0;
1231  format->numOfDetectorRegions = 0;
1232  format->numOfRegionsToProcess = 0;
1233  format->numOfPinholes = 0;
1234  format->numOfFrames = 0;
1235  format->framesPerScan = 0;
1236  format->numOfScans = 0;
1237  format->subWindowSize = 0;
1238  format->iXWidth = 0;
1239  format->iYWidth = 0;
1240 
1241  return;
1242 }
1243 /******************************************************************************/
1244 
1245 
1246 
1247 /******************************************************************************
1248 * European Southern Observatory
1249 * VLTI MIDI Maintenance Templates Software
1250 *
1251 * Module name: midiCopyFormat
1252 * Input/Output: See function arguments to avoid duplication
1253 * Description: Copies image format
1254 *
1255 * History:
1256 * 10-Jul-05 (csabet) created
1257 ******************************************************************************/
1258 void midiCopyFormat (
1259  ImageFormat *formatIn, // In: Pointer to the image format structure
1260  ImageFormat *formatOut) // Ou: Pointer to the image format structure
1261 {
1262 
1263  // Local Declarations
1264  // ------------------
1265  const char routine[] = "midiCopyFormat";
1266 
1267  // Algorithm
1268  // ---------
1269  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1270  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
1271 
1272  sprintf (formatOut->obsCatg, "%s", formatIn->obsCatg);
1273  sprintf (formatOut->obsTech, "%s", formatIn->obsTech);
1274  sprintf (formatOut->obsType, "%s", formatIn->obsType);
1275  sprintf (formatOut->cameraId, "%s", formatIn->cameraId);
1276  sprintf (formatOut->beamCombiner, "%s", formatIn->beamCombiner);
1277  sprintf (formatOut->filterName, "%s", formatIn->filterName);
1278  sprintf (formatOut->shutterId, "%s", formatIn->shutterId);
1279  sprintf (formatOut->grismId, "%s", formatIn->grismId);
1280  sprintf (formatOut->targetName, "%s", formatIn->targetName);
1281  sprintf (formatOut->slitName, "%s", formatIn->slitName);
1282  sprintf (formatOut->tplStart, "%s", formatIn->tplStart);
1283  sprintf (formatOut->tplName, "%s", formatIn->tplName);
1284  formatOut->numOfDetectorRegions = formatIn->numOfDetectorRegions;
1285  formatOut->numOfRegionsToProcess = formatIn->numOfRegionsToProcess;
1286  formatOut->numOfPinholes = formatIn->numOfPinholes;
1287  formatOut->framesPerScan = formatIn->framesPerScan;
1288  formatOut->subWindowSize = formatIn->subWindowSize;
1289  formatOut->iXWidth = formatIn->iXWidth;
1290  formatOut->iYWidth = formatIn->iYWidth;
1291 
1292  return;
1293 }
1294 /******************************************************************************/
1295 
1296 
1297 
1298 /******************************************************************************
1299 * European Southern Observatory
1300 * VLTI MIDI Maintenance Templates Software
1301 *
1302 * Module name: midiReportFormat
1303 * Input/Output: See function arguments to avoid duplication
1304 * Description: Reports image format
1305 *
1306 * History:
1307 * 05-Dec-05 (csabet) created
1308 ******************************************************************************/
1309 void midiReportFormat (
1310  const char *type, // In: Type of file
1311  int isDisp, // In: Whether DISPERSED or not
1312  ImageFormat *format) // In: Pointer to the image format structure
1313 {
1314 
1315  // Local Declarations
1316  // ------------------
1317  const char routine[] = "midiReportFormat";
1318 
1319  // Algorithm
1320  // ---------
1321  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1322  if (diagnostic > 4) fprintf (midiReportPtr, "Invoking routine '%s' \n", routine);
1323 
1324  cpl_msg_info(cpl_func," \n");
1325  cpl_msg_info(cpl_func,"%s FORMAT \n", type);
1326  cpl_msg_info(cpl_func,"--------------------- \n");
1327  cpl_msg_info(cpl_func,"FITS Template Name = %s \n", format->tplName);
1328  cpl_msg_info(cpl_func,"Batch Template Name = %s \n", batchTemplate);
1329  cpl_msg_info(cpl_func,"Observation Category = %s \n", format->obsCatg);
1330  cpl_msg_info(cpl_func,"Observation Technique = %s \n", format->obsTech);
1331  cpl_msg_info(cpl_func,"Observation Type = %s \n", format->obsType);
1332  cpl_msg_info(cpl_func,"Camera ID = %s \n", format->cameraId);
1333  cpl_msg_info(cpl_func,"Beam Combiner = %s \n", format->beamCombiner);
1334  cpl_msg_info(cpl_func,"Filter ID = %s \n", format->filterName);
1335  cpl_msg_info(cpl_func,"Shutter ID = %s \n", format->shutterId);
1336  cpl_msg_info(cpl_func,"Grism ID = %s \n", format->grismId);
1337  cpl_msg_info(cpl_func,"Slit Name = %s \n", format->slitName);
1338  cpl_msg_info(cpl_func,"Target Name = %s \n", format->targetName);
1339  cpl_msg_info(cpl_func,"TPL Start = %s \n", format->tplStart);
1340  if (isDisp)
1341  cpl_msg_info(cpl_func,"Data is = DISPERSED \n");
1342  else
1343  cpl_msg_info(cpl_func,"Data is = UNDISPERSED \n");
1344  cpl_msg_info(cpl_func,"Number of Frames = %d \n", format->numOfFrames);
1345  if ((strcmp(format->beamCombiner, "SCI_PHOT") == 0) || (strcmp (format->shutterId, "ABOPEN") == 0))
1346  {
1347  cpl_msg_info(cpl_func,"Frames per scan = %d \n", format->framesPerScan);
1348  cpl_msg_info(cpl_func,"Number of Scans = %d \n", format->numOfScans);
1349  }
1350  cpl_msg_info(cpl_func,"X, Column dimension = %d \n", format->iXWidth);
1351  cpl_msg_info(cpl_func,"Y, Column dimension = %d \n", format->iYWidth);
1352  cpl_msg_info(cpl_func,"Sub-window size = %d \n", format->subWindowSize);
1353  cpl_msg_info(cpl_func,"Number of Pinholes = %d \n", format->numOfPinholes);
1354  cpl_msg_info(cpl_func,"Number of Detector Regions = %d \n", format->numOfDetectorRegions);
1355  cpl_msg_info(cpl_func,"Number of Regions to process = %d \n", format->numOfRegionsToProcess);
1356  cpl_msg_info(cpl_func,"\n");
1357 
1358  fprintf (midiReportPtr, " \n");
1359  fprintf (midiReportPtr, "%s FORMAT \n", type);
1360  fprintf (midiReportPtr, "--------------------- \n");
1361  fprintf (midiReportPtr, "FITS Template Name = %s \n", format->tplName);
1362  fprintf (midiReportPtr, "Batch Template Name = %s \n", batchTemplate);
1363  fprintf (midiReportPtr, "Observation Category = %s \n", format->obsCatg);
1364  fprintf (midiReportPtr, "Observation Technique = %s \n", format->obsTech);
1365  fprintf (midiReportPtr, "Observation Type = %s \n", format->obsType);
1366  fprintf (midiReportPtr, "Camera ID = %s \n", format->cameraId);
1367  fprintf (midiReportPtr, "Beam Combiner = %s \n", format->beamCombiner);
1368  fprintf (midiReportPtr, "Filter ID = %s \n", format->filterName);
1369  fprintf (midiReportPtr, "Shutter ID = %s \n", format->shutterId);
1370  fprintf (midiReportPtr, "Grism ID = %s \n", format->grismId);
1371  fprintf (midiReportPtr, "Slit Name = %s \n", format->slitName);
1372  fprintf (midiReportPtr, "Target Name = %s \n", format->targetName);
1373  fprintf (midiReportPtr, "TPL Start = %s \n", format->tplStart);
1374  if (isDisp)
1375  fprintf (midiReportPtr, "Data is = DISPERSED \n");
1376  else
1377  fprintf (midiReportPtr, "Data is = UNDISPERSED \n");
1378  fprintf (midiReportPtr, "Number of Frames = %d \n", format->numOfFrames);
1379  if ((strcmp(format->beamCombiner, "SCI_PHOT") == 0) || (strcmp (format->shutterId, "ABOPEN") == 0))
1380  {
1381  fprintf (midiReportPtr, "Frames per scan = %d \n", format->framesPerScan);
1382  fprintf (midiReportPtr, "Number of Scans = %d \n", format->numOfScans);
1383  }
1384  fprintf (midiReportPtr, "X, Column dimension = %d \n", format->iXWidth);
1385  fprintf (midiReportPtr, "Y, Column dimension = %d \n", format->iYWidth);
1386  fprintf (midiReportPtr, "Sub-window size = %d \n", format->subWindowSize);
1387  fprintf (midiReportPtr, "Number of Pinholes = %d \n", format->numOfPinholes);
1388  fprintf (midiReportPtr, "Number of Detector Regions = %d \n", format->numOfDetectorRegions);
1389  fprintf (midiReportPtr, "Number of Regions to process = %d \n", format->numOfRegionsToProcess);
1390  fprintf (midiReportPtr, "\n");
1391 
1392  return;
1393 }
1394 /******************************************************************************/
1395 
1396 
1397 /******************************************************************************
1398 * European Southern Observatory
1399 * VLTI MIDI Data Reduction Software
1400 *
1401 * Module name: selectChannels
1402 * Input/Output: See function arguments to avoid duplication
1403 * Description:
1404 *
1405 *
1406 * History:
1407 * 21-Jul-03 (csabet) Created
1408 ******************************************************************************/
1409 void selectChannels (
1410  int start, // In: Start region
1411  int end, // In: End region
1412  ImageFormat *format, // In: File format
1413  float **mask) // In: Mask file
1414 {
1415  // Local Declarations
1416  // ------------------
1417  const char routine[] = "selectChannels";
1418  int i, x, y, r, masked;
1419  float *array;
1420 
1421  // Algorithm
1422  // ---------
1423  if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking routine '%s' \n", routine);
1424  if (diagnostic > 4) fprintf(midiReportPtr, "Invoking routine '%s' \n", routine);
1425 
1426  cpl_msg_info(cpl_func,"\nSelecting active channels based on the given mask \n");
1427  cpl_msg_info(cpl_func,"------------------------------------------------- \n");
1428  fprintf(midiReportPtr, "\nSelecting active channels based on the given mask \n");
1429  fprintf(midiReportPtr, "------------------------------------------------- \n");
1430 
1431  // Allocate memory
1432  array = (float *) calloc (format->iXWidth, sizeof(float));
1433 
1434  for (r = start; r < end; r++)
1435  {
1436  for (y = 0; y < format->iYWidth; y++)
1437  {
1438  for (x = 0; x < format->iXWidth; x++)
1439  {
1440  i = x + y * format->iXWidth;
1441  if (mask[r][i] > array[x]) array[x] = mask[r][i];
1442  }
1443  }
1444  }
1445 
1446  masked = 0;
1447  for (x = 0; x < format->iXWidth; x++)
1448  {
1449  if (array[x] == 0.0)
1450  {
1451  masked++;
1452  badChannelList[x] |= BSL_MASKED_CHANNEL;
1453  }
1454  }
1455 
1456  cpl_msg_info(cpl_func,"Number of active channels = %3d \n\n", (format->iXWidth - masked));
1457  fprintf(midiReportPtr, "Number of active channels = %3d QCLOG \n\n", (format->iXWidth - masked));
1458 
1459  // Plot
1460  if (plotFile && diagnostic > 1) midiCreatePlotFile2D ("SelectedChannels", "Selected Channels",
1461  "Wavelength Channel", "Mask Profile", 0, array, 0, format->iXWidth, 1, 0);
1462 
1463  // Release memory
1464  free (array);
1465 
1466  return;
1467 }
1468 /*****************************************************************************/
1469