GRAVI Pipeline Reference Manual  1.2.3
gravi_utils.c
1 /* $Id: gravi_utils.c,v 1.10 2011/05/31 06:10:40 nazouaoui Exp $
2  *
3  * This file is part of the GRAVI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
29 /*-----------------------------------------------------------------------------
30  Includes
31  -----------------------------------------------------------------------------*/
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #include <cpl.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <math.h>
41 #include <time.h>
42 
43 #include "gravi_data.h"
44 
45 #include "gravi_dfs.h"
46 #include "gravi_pfits.h"
47 #include "gravi_cpl.h"
48 
49 #include "gravi_utils.h"
50 
51 /*-----------------------------------------------------------------------------
52  Gloval variable
53  -----------------------------------------------------------------------------*/
54 
55 /* Baseline: [6 bases][tel1, tel2] */
56 int GRAVI_BASE_TEL[6][2] = { {0,1}, {0,2}, {0,3}, {1,2}, {1,3}, {2,3} };
57 char GRAVI_BASE_NAME[6][3] = {"12","13","14","23","24","34"};
58 
59 /* Closing triangle: [6 bases][2 closing triangle][base1, base2] */
60 int GRAVI_TRI_BASE[6][2][2] = { {{1,3},{2,4}}, {{0,3},{2,5}}, {{0,4},{1,5}}, {{0,1},{4,5}}, {{0,2},{3,5}}, {{1,2},{3,4}}};
61 int GRAVI_TRI_SIGN[6][2][2] = { {{1,-1},{1,-1}}, {{1,1},{1,-1}}, {{1,1},{1,1}}, {{-1,1},{1,-1}}, {{-1,1},{1,1}}, {{-1,1},{-1,1}} };
62 
63 /* Closing telescope: [3 closures][tel1, tel2, tel3] */
64 int GRAVI_CLO_TEL[4][3] = {{0,1,2}, {0,1,3}, {0,2,3}, {1,2,3}};
65 char GRAVI_CLO_NAME[4][4] = {"123", "124", "134", "234"};
66 
67 /* Baseline in closure: [3 closures][base1, base2, -base3] */
68 int GRAVI_CLO_BASE[4][3] = {{0,3,1}, {0,4,2}, {1,5,2}, {3,5,4}};
69 
70 /* DATA and DATAERR region names */
71 char GRAVI_DATA[50][7] =
72  {"DATA1", "DATA2", "DATA3", "DATA4", "DATA5", "DATA6", "DATA7", "DATA8", "DATA9", "DATA10",
73  "DATA11","DATA12","DATA13","DATA14","DATA15","DATA16","DATA17","DATA18","DATA19","DATA20",
74  "DATA21","DATA22","DATA23","DATA24","DATA25","DATA26","DATA27","DATA28","DATA29","DATA30",
75  "DATA31","DATA32","DATA33","DATA34","DATA35","DATA36","DATA37","DATA38","DATA39","DATA40",
76  "DATA41","DATA42","DATA43","DATA44","DATA45","DATA46","DATA47","DATA48","DATA49","DATA50"};
77 
78 /* DATA and DATAERR region names */
79 char GRAVI_DATAERR[50][10] =
80  {"DATAERR1", "DATAERR2", "DATAERR3", "DATAERR4", "DATAERR5", "DATAERR6", "DATAERR7", "DATAERR8", "DATAERR9", "DATAERR10",
81  "DATAERR11","DATAERR12","DATAERR13","DATAERR14","DATAERR15","DATAERR16","DATAERR17","DATAERR18","DATAERR19","DATAERR20",
82  "DATAERR21","DATAERR22","DATAERR23","DATAERR24","DATAERR25","DATAERR26","DATAERR27","DATAERR28","DATAERR29","DATAERR30",
83  "DATAERR31","DATAERR32","DATAERR33","DATAERR34","DATAERR35","DATAERR36","DATAERR37","DATAERR38","DATAERR39","DATAERR40",
84  "DATAERR41","DATAERR42","DATAERR43","DATAERR44","DATAERR45","DATAERR46","DATAERR47","DATAERR48","DATAERR49","DATAERR50"};
85 
86 
87 /* IP of GRAVITY beams */
88 int GRAVI_LABINPUT[4] = {7,5,3,1};
89 
90 /*-----------------------------------------------------------------------------
91  Function code
92  -----------------------------------------------------------------------------*/
93 
94 /*----------------------------------------------------------------------------*/
102 /*----------------------------------------------------------------------------*/
103 
104 const char * gravi_get_license(void)
105 {
106  const char * gravi_license =
107  "This file is part of the GRAVI Instrument Pipeline\n"
108  "Copyright (C) 2002,2003 European Southern Observatory\n"
109  "\n"
110  "This program is free software; you can redistribute it and/or modify\n"
111  "it under the terms of the GNU General Public License as published by\n"
112  "the Free Software Foundation; either version 2 of the License, or\n"
113  "(at your option) any later version.\n"
114  "\n"
115  "This program is distributed in the hope that it will be useful,\n"
116  "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
117  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
118  "GNU General Public License for more details.\n"
119  "\n"
120  "You should have received a copy of the GNU General Public License\n"
121  "along with this program; if not, write to the Free Software\n"
122  "Foundation, Inc., 59 Temple Place, Suite 330, Boston, \n"
123  "MA 02111-1307 USA" ;
124  return gravi_license ;
125 }
126 
127 cpl_error_code gravi_msg_warning (const char * component, const char * msg)
128 {
129  cpl_msg_warning (component,"***********************************************");
130  cpl_msg_warning (component," ");
131  cpl_msg_warning (component," %s ", msg);
132  cpl_msg_warning (component," ");
133  cpl_msg_warning (component,"***********************************************");
134  return CPL_ERROR_NONE;
135 }
136 
137 /*---------------------------------------------------------------------------*/
151 /*---------------------------------------------------------------------------*/
152 
153 cpl_table * gravi_table_oi_create (int nwave, int nrow, const char * oi_name)
154 {
155  gravi_msg_function_start(0);
156  cpl_ensure (nwave>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
157  cpl_ensure (nrow>0, CPL_ERROR_ILLEGAL_INPUT, NULL);
158  cpl_ensure (oi_name, CPL_ERROR_NULL_INPUT, NULL);
159 
160  /* We init most of the column */
161  int init = 1;
162 
163  cpl_table * oi_table = NULL;
164 
165  /* Creating the columns */
166 
167  if (!strcmp(oi_name, GRAVI_OI_VIS_EXT)){
168  oi_table = cpl_table_new(nrow * 6);
169 
170  cpl_table_new_column (oi_table, "TARGET_ID", CPL_TYPE_INT);
171  cpl_table_set_column_savetype(oi_table, "TARGET_ID", CPL_TYPE_SHORT);
172  if (init) cpl_table_fill_column_window_int (oi_table, "TARGET_ID", 0, nrow * 6, 0);
173 
174  cpl_table_new_column (oi_table, "TIME", CPL_TYPE_DOUBLE);
175  if (init) cpl_table_fill_column_window_double (oi_table, "TIME", 0, nrow * 6, 0);
176  cpl_table_set_column_unit (oi_table, "TIME", "s");
177 
178  cpl_table_new_column (oi_table, "MJD", CPL_TYPE_DOUBLE);
179  if (init) cpl_table_fill_column_window_double (oi_table, "MJD", 0, nrow * 6, 0.0);
180  cpl_table_set_column_unit (oi_table, "MJD", "day");
181 
182  cpl_table_new_column (oi_table, "INT_TIME", CPL_TYPE_DOUBLE);
183  if(init) cpl_table_fill_column_window_double (oi_table, "INT_TIME", 0, nrow * 6, 0.0);
184  cpl_table_set_column_unit (oi_table, "INT_TIME", "s");
185 
186  cpl_table_new_column_array (oi_table, "VISDATA", CPL_TYPE_DOUBLE_COMPLEX, nwave);
187  cpl_table_set_column_unit (oi_table, "VISDATA", "e");
188  cpl_table_new_column_array (oi_table, "VISERR", CPL_TYPE_DOUBLE_COMPLEX, nwave);
189  cpl_table_set_column_unit (oi_table, "VISERR", "e");
190 
191  cpl_table_new_column_array (oi_table, "VISAMP", CPL_TYPE_DOUBLE, nwave);
192  cpl_table_new_column_array (oi_table, "VISAMPERR", CPL_TYPE_DOUBLE, nwave);
193 
194  cpl_table_new_column_array (oi_table, "VISPHI", CPL_TYPE_DOUBLE, nwave);
195  cpl_table_set_column_unit (oi_table, "VISPHI", "deg");
196 
197  cpl_table_new_column_array (oi_table, "VISPHIERR", CPL_TYPE_DOUBLE, nwave);
198  cpl_table_set_column_unit (oi_table, "VISPHIERR", "deg");
199 
200  cpl_table_new_column_array (oi_table, "RVIS", CPL_TYPE_DOUBLE, nwave);
201  cpl_table_set_column_unit (oi_table, "RVIS", "e");
202 
203  cpl_table_new_column_array (oi_table, "RVISERR", CPL_TYPE_DOUBLE, nwave);
204  cpl_table_set_column_unit (oi_table, "RVISERR", "e");
205 
206  cpl_table_new_column_array (oi_table, "IVIS", CPL_TYPE_DOUBLE, nwave);
207  cpl_table_set_column_unit (oi_table, "IVIS", "e");
208 
209  cpl_table_new_column_array (oi_table, "IVISERR", CPL_TYPE_DOUBLE, nwave);
210  cpl_table_set_column_unit (oi_table, "IVISERR", "e");
211 
212  cpl_table_new_column (oi_table, "UCOORD", CPL_TYPE_DOUBLE);
213  if(init) cpl_table_fill_column_window_double (oi_table, "UCOORD", 0, nrow * 6, 0.0);
214  cpl_table_set_column_unit (oi_table, "UCOORD", "m");
215 
216  cpl_table_new_column (oi_table, "VCOORD", CPL_TYPE_DOUBLE);
217  if(init) cpl_table_fill_column_window_double (oi_table, "VCOORD", 0, nrow * 6, 0.0);
218  cpl_table_set_column_unit (oi_table, "VCOORD", "m");
219 
220  cpl_table_new_column_array (oi_table, "STA_INDEX", CPL_TYPE_INT, 2);
221  cpl_table_set_column_savetype(oi_table, "STA_INDEX", CPL_TYPE_SHORT);
222  }
223  else if (!strcmp(oi_name, GRAVI_OI_VIS2_EXT)){
224  oi_table = cpl_table_new(nrow * 6);
225  cpl_table_new_column (oi_table, "TARGET_ID", CPL_TYPE_INT);
226  cpl_table_set_column_savetype(oi_table, "TARGET_ID", CPL_TYPE_SHORT);
227  if(init) cpl_table_fill_column_window_int (oi_table, "TARGET_ID", 0, nrow * 6, 0);
228  cpl_table_new_column (oi_table, "TIME", CPL_TYPE_DOUBLE);
229  if(init) cpl_table_fill_column_window_double (oi_table, "TIME", 0, nrow * 6, 0);
230  cpl_table_set_column_unit (oi_table, "TIME", "s");
231  cpl_table_new_column (oi_table, "MJD", CPL_TYPE_DOUBLE);
232  if(init) cpl_table_fill_column_window_double (oi_table, "MJD", 0, nrow * 6, 0.0);
233  cpl_table_set_column_unit (oi_table, "MJD", "day");
234  cpl_table_new_column (oi_table, "INT_TIME", CPL_TYPE_DOUBLE);
235  if(init) cpl_table_fill_column_window_double (oi_table, "INT_TIME",
236  0, nrow * 6, 0.0);
237  cpl_table_set_column_unit (oi_table, "INT_TIME", "s");
238  cpl_table_new_column_array (oi_table, "VIS2DATA",
239  CPL_TYPE_DOUBLE, nwave);
240 
241  cpl_table_new_column_array (oi_table, "VIS2ERR",
242  CPL_TYPE_DOUBLE, nwave);
243  cpl_table_new_column (oi_table, "UCOORD", CPL_TYPE_DOUBLE);
244  if(init) cpl_table_fill_column_window_double (oi_table, "UCOORD",
245  0, nrow * 6, 0.0);
246  cpl_table_set_column_unit (oi_table, "UCOORD", "m");
247  cpl_table_new_column (oi_table, "VCOORD", CPL_TYPE_DOUBLE);
248  if(init) cpl_table_fill_column_window_double (oi_table, "VCOORD",
249  0, nrow * 6, 0.0);
250  cpl_table_set_column_unit (oi_table, "VCOORD", "m");
251 
252  cpl_table_new_column_array (oi_table, "STA_INDEX", CPL_TYPE_INT, 2);
253  cpl_table_set_column_savetype(oi_table, "STA_INDEX", CPL_TYPE_SHORT);
254  }
255  else if (!strcmp(oi_name, GRAVI_OI_T3_EXT)){
256  oi_table = cpl_table_new(nrow * 4);
257  cpl_table_new_column (oi_table, "TARGET_ID", CPL_TYPE_INT);
258  cpl_table_set_column_savetype(oi_table, "TARGET_ID", CPL_TYPE_SHORT);
259  if(init) cpl_table_fill_column_window_int (oi_table, "TARGET_ID",
260  0, nrow * 4, 0);
261  cpl_table_new_column (oi_table, "TIME", CPL_TYPE_DOUBLE);
262  if(init) cpl_table_fill_column_window_double (oi_table, "TIME", 0, nrow * 4, 0);
263  cpl_table_set_column_unit (oi_table, "TIME", "s");
264  cpl_table_new_column (oi_table, "MJD", CPL_TYPE_DOUBLE);
265  if(init) cpl_table_fill_column_window_double (oi_table, "MJD", 0, nrow * 4, 0.0);
266  cpl_table_set_column_unit (oi_table, "MJD", "day");
267  cpl_table_new_column (oi_table, "INT_TIME", CPL_TYPE_DOUBLE);
268  cpl_table_set_column_unit (oi_table, "INT_TIME", "s");
269  if(init) cpl_table_fill_column_window_double (oi_table, "INT_TIME",
270  0, nrow * 4, 0.0);
271  cpl_table_new_column_array (oi_table, "T3AMP", CPL_TYPE_DOUBLE, nwave);
272  cpl_table_new_column_array (oi_table, "T3AMPERR", CPL_TYPE_DOUBLE, nwave);
273  cpl_table_new_column_array (oi_table, "T3PHI", CPL_TYPE_DOUBLE, nwave);
274  cpl_table_set_column_unit (oi_table, "T3PHI", "deg");
275  cpl_table_new_column_array (oi_table, "T3PHIERR",
276  CPL_TYPE_DOUBLE, nwave);
277  cpl_table_set_column_unit (oi_table, "T3PHIERR", "deg");
278  cpl_table_new_column (oi_table, "U1COORD", CPL_TYPE_DOUBLE);
279  if(init) cpl_table_fill_column_window_double (oi_table, "U1COORD",
280  0, nrow * 4, 0.0);
281  cpl_table_set_column_unit (oi_table, "U1COORD", "m");
282  cpl_table_new_column (oi_table, "V1COORD", CPL_TYPE_DOUBLE);
283  if(init) cpl_table_fill_column_window_double (oi_table, "V1COORD",
284  0, nrow * 4, 0.0);
285  cpl_table_set_column_unit (oi_table, "V1COORD", "m");
286  cpl_table_new_column (oi_table, "U2COORD", CPL_TYPE_DOUBLE);
287  if(init) cpl_table_fill_column_window_double (oi_table, "U2COORD",
288  0, nrow * 4, 0.0);
289  cpl_table_set_column_unit (oi_table, "U2COORD", "m");
290  cpl_table_new_column (oi_table, "V2COORD", CPL_TYPE_DOUBLE);
291  if(init) cpl_table_fill_column_window_double (oi_table, "V2COORD",
292  0, nrow * 4, 0.0);
293  cpl_table_set_column_unit (oi_table, "V2COORD", "m");
294  cpl_table_new_column_array (oi_table, "STA_INDEX", CPL_TYPE_INT, 3);
295  cpl_table_set_column_savetype(oi_table, "STA_INDEX", CPL_TYPE_SHORT);
296  }
297  else if (!strcmp(oi_name, GRAVI_OI_FLUX_EXT)) {
298  oi_table = cpl_table_new(nrow * 4);
299  cpl_table_new_column (oi_table, "TARGET_ID", CPL_TYPE_INT);
300  cpl_table_set_column_savetype(oi_table, "TARGET_ID", CPL_TYPE_SHORT);
301  if(init) cpl_table_fill_column_window_int (oi_table, "TARGET_ID",
302  0, nrow * 4, 0);
303  cpl_table_new_column (oi_table, "TIME", CPL_TYPE_DOUBLE);
304  cpl_table_set_column_unit (oi_table, "TIME", "s");
305  if(init) cpl_table_fill_column_window_double (oi_table, "TIME", 0, nrow * 4, 0);
306  cpl_table_new_column (oi_table, "MJD", CPL_TYPE_DOUBLE);
307  cpl_table_set_column_unit (oi_table, "MJD", "day");
308  if(init) cpl_table_fill_column_window_double (oi_table, "MJD", 0, nrow * 4, 0.0);
309  cpl_table_new_column (oi_table, "INT_TIME", CPL_TYPE_DOUBLE);
310  cpl_table_set_column_unit (oi_table, "INT_TIME", "s");
311  if(init) cpl_table_fill_column_window_double (oi_table, "INT_TIME",
312  0, nrow * 4, 0.0);
313  cpl_table_new_column_array (oi_table, "FLUX",CPL_TYPE_DOUBLE, nwave);
314  cpl_table_set_column_unit (oi_table, "FLUX", "e");
315  cpl_table_new_column_array (oi_table, "FLUXERR",CPL_TYPE_DOUBLE, nwave);
316  cpl_table_set_column_unit (oi_table, "FLUXERR", "e");
317  cpl_table_new_column (oi_table, "STA_INDEX", CPL_TYPE_INT);
318  cpl_table_set_column_savetype(oi_table, "STA_INDEX", CPL_TYPE_SHORT);
319  if(init) cpl_table_fill_column_window_int (oi_table, "STA_INDEX",
320  0, nrow * 4, 0);
321  }
322  else {
323  cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
324  "The name of the table is incorrect");
325  return NULL;
326  }
327 
328  /* Set the flags */
329  cpl_table_new_column_array (oi_table, "FLAG", CPL_TYPE_INT, nwave);
330  cpl_table_set_column_savetype(oi_table, "FLAG", CPL_TYPE_BOOL);
331  cpl_array * bool_array = cpl_array_new(nwave, CPL_TYPE_INT);
332  cpl_array_fill_window_int(bool_array, 0, nwave, CPL_FALSE);
333  cpl_table_fill_column_window_array (oi_table, "FLAG",
334  0, cpl_table_get_nrow(oi_table), bool_array);
335  cpl_array_delete (bool_array);
336 
337  CPLCHECK_NUL ("Cannot create oi_table");
338 
339  gravi_msg_function_exit(0);
340  return oi_table;
341 }
342 
343 /*---------------------------------------------------------------------------*/
359 /*---------------------------------------------------------------------------*/
360 
361 int gravi_region_get_base (cpl_table *imaging_detector, int region)
362 {
363  cpl_ensure (imaging_detector, CPL_ERROR_NULL_INPUT, -1);
364  cpl_ensure (region>=0 && region<48, CPL_ERROR_ILLEGAL_INPUT, -1);
365 
366  const char * regname_i;
367  int base = 0;
368  const char * bases[6]={"12","13","14","23","24","34"};
369  const char * bases_inv[6]={"21","31","41","32","42","43"};
370 
371 
372  /* get the regname of region */
373  regname_i = cpl_table_get_string(imaging_detector, "REGNAME", region);
374 
375  for (base=0; base<6; base++){
376  if (!(strncmp(bases[base], regname_i, 2)) || !(strncmp(bases_inv[base], regname_i, 2))){
377  return base;
378  }
379  }
380  cpl_msg_error (cpl_func, "Cannot find the baseline of region %s", regname_i);
381  return -1;
382 }
383 
384 /*---------------------------------------------------------------------------*/
397 /*---------------------------------------------------------------------------*/
398 
399 int gravi_region_get_base_sign (cpl_table *imaging_detector, int base)
400 {
401  cpl_ensure (imaging_detector, CPL_ERROR_NULL_INPUT, 0);
402  cpl_ensure (base>=0 && base<=5, CPL_ERROR_ILLEGAL_INPUT, 0);
403 
404  cpl_size n_region = cpl_table_get_nrow (imaging_detector);
405  const char * basename = GRAVI_BASE_NAME[base];
406 
407  /* Loop on region */
408  for (cpl_size reg = 0; reg < n_region; reg ++) {
409  const char * regname = cpl_table_get_string (imaging_detector, "REGNAME", reg);
410 
411  /* Check if this region match the requested base */
412  if ( (regname[0] == basename[0] && regname[1] == basename[1]) ||
413  (regname[0] == basename[1] && regname[1] == basename[0]) ) {
414 
415  /* Get the port array */
416  const cpl_array * port = cpl_table_get_array (imaging_detector, "PORTS", reg);
417  cpl_ensure (port, CPL_ERROR_ILLEGAL_INPUT, 0);
418 
419  /* Check the port order */
420  if (cpl_array_get_int (port, 0, NULL) < cpl_array_get_int (port, 1, NULL)) return 1;
421  if (cpl_array_get_int (port, 0, NULL) > cpl_array_get_int (port, 1, NULL)) return -1;
422  }
423  }
424 
425  return 0;
426 }
427 
428 
429 /*---------------------------------------------------------------------------*/
443 /*---------------------------------------------------------------------------*/
444 
445 int gravi_region_get_pol (cpl_table *imaging_detector, int region)
446 {
447  cpl_ensure (imaging_detector, CPL_ERROR_NULL_INPUT, -1);
448  cpl_ensure (region>=0 && region<48, CPL_ERROR_ILLEGAL_INPUT, -1);
449 
450  /* get the regname of region */
451  const char * regname_i;
452  regname_i = cpl_table_get_string (imaging_detector, "REGNAME", region);
453 
454  /* check its name */
455  if (regname_i[5] == 'S' || regname_i[5] == 'C') return 0;
456  if (regname_i[5] == 'P') return 1;
457 
458  cpl_msg_error (cpl_func, "Cannot find the polarisation of region %s", regname_i);
459  return -1;
460 }
461 
462 /*---------------------------------------------------------------------------*/
477 /*---------------------------------------------------------------------------*/
478 
479 int gravi_region_get_phaseid (cpl_table *imaging_detector, int region)
480 {
481  cpl_ensure (imaging_detector, CPL_ERROR_NULL_INPUT, -1);
482  cpl_ensure (region>=0 && region<48, CPL_ERROR_ILLEGAL_INPUT, -1);
483 
484  /* get the regname of region */
485  const char * regname_i;
486  regname_i = cpl_table_get_string (imaging_detector, "REGNAME", region);
487 
488  /* check its name */
489  if (regname_i[3] == 'A') return 0;
490  if (regname_i[3] == 'B') return 1;
491  if (regname_i[3] == 'C') return 2;
492  if (regname_i[3] == 'D') return 3;
493 
494  cpl_msg_error (cpl_func, "Cannot find the phaseid of region %s", regname_i);
495  return -1;
496 }
497 
498 /*---------------------------------------------------------------------------*/
510 /*---------------------------------------------------------------------------*/
511 
512 int gravi_region_get_tel (cpl_table *imaging_detector, int region, int beam)
513 {
514  cpl_ensure (imaging_detector, CPL_ERROR_NULL_INPUT, -1);
515  cpl_ensure (region>=0 && region<48, CPL_ERROR_ILLEGAL_INPUT, -1);
516  cpl_ensure (beam==0 || beam==1, CPL_ERROR_ILLEGAL_INPUT, -1);
517 
518  /* get the regname of region */
519  const char * regname_i;
520  regname_i = cpl_table_get_string (imaging_detector, "REGNAME", region);
521  cpl_ensure (regname_i, CPL_ERROR_ILLEGAL_INPUT, -1);
522 
523  /* Memory to read */
524  char *ptr, str[2] = "0";
525  str[0] = regname_i[beam];
526 
527  /* Read the telescope */
528  long tel = strtol (str, &ptr, 10) - 1;
529 
530  cpl_ensure (tel>=0 && tel<4, CPL_ERROR_ILLEGAL_INPUT, -1);
531  return (int)tel;
532 }
533 
534 /*---------------------------------------------------------------------------*/
549 /*---------------------------------------------------------------------------*/
550 
551 char gravi_region_get_phase (cpl_table *imaging_detector, int region)
552 {
553  cpl_ensure (imaging_detector, CPL_ERROR_NULL_INPUT, -1);
554  cpl_ensure (region>=0 && region<48, CPL_ERROR_ILLEGAL_INPUT, -1);
555 
556  /* get the regname of region */
557  const char * regname_i;
558  regname_i = cpl_table_get_string (imaging_detector, "REGNAME", region);
559 
560  /* check its name */
561  // return regname_i[3];
562  if (regname_i[3] == 'A') return 'A';
563  if (regname_i[3] == 'B') return 'B';
564  if (regname_i[3] == 'C') return 'C';
565  if (regname_i[3] == 'D') return 'D';
566 
567  cpl_msg_error (cpl_func, "Cannot find the phase of region %s", regname_i);
568  return -1;
569 }
570 
571 /*---------------------------------------------------------------------------*/
582 /*---------------------------------------------------------------------------*/
583 
584 int gravi_get_region (cpl_table *img_det, int base, char phase, int pol)
585 {
586  cpl_ensure (img_det, CPL_ERROR_NULL_INPUT, -1);
587 
588  for (cpl_size reg = 0 ; reg < cpl_table_get_nrow (img_det); reg++) {
589  if ( gravi_region_get_pol (img_det, reg) == pol &&
590  gravi_region_get_phase (img_det, reg) == phase &&
591  gravi_region_get_base (img_det, reg) == base ) return reg;
592  }
593 
594  return -1;
595 }
596 
597 
598 /*---------------------------------------------------------------------------*/
609 /*---------------------------------------------------------------------------*/
610 
611 int gravi_wave_get_nlambda(cpl_table *wave_data, double lambda_min, double lambda_max)
612 {
613  cpl_ensure (wave_data, CPL_ERROR_NULL_INPUT, -1);
614  cpl_ensure (lambda_max>lambda_min, CPL_ERROR_ILLEGAL_INPUT, -1);
615  cpl_ensure (lambda_min>1e-6 && lambda_min<3e-6, CPL_ERROR_ILLEGAL_INPUT, -1);
616  cpl_ensure (lambda_max>1e-6 && lambda_max<3e-6, CPL_ERROR_ILLEGAL_INPUT, -1);
617 
618  int n_wave = cpl_table_get_column_depth (wave_data, "DATA1");
619  int n_region = cpl_table_get_ncol (wave_data);
620  int n_element;
621  double wave, max_wave=0., min_wave = 3*pow(10, -6);
622  double sc_medium_res, sc_high_res, sc_low_res;
623  cpl_size ind_min=0, ind_max=n_wave-1;
624  char* regname;
625 
626  /* find first pixel */
627  while ((lambda_min-max_wave >= 0.0001*pow(10, -6)) && (ind_min < n_wave)) {
628  for (int region=0; region < n_region; region++){
629  regname = GRAVI_DATA[region];
630  wave=cpl_array_get(cpl_table_get_array(wave_data, regname, 0), ind_min, NULL);
631  if (wave >=max_wave) max_wave=wave;
632  }
633  ind_min++;
634  }
635  ind_min--;
636 
637  CPLCHECK_INT ("Cannot get first pixel");
638 
639  /* find last pixel */
640  while ((lambda_max-min_wave <= -0.0001*pow(10, -6)) && (ind_max >= 0)) {
641  for (int region=0; region < n_region; region++){
642  regname = GRAVI_DATA[region];
643  wave=cpl_array_get(cpl_table_get_array(wave_data, regname, 0), ind_max, NULL);
644  if (wave <=min_wave) min_wave=wave;
645  }
646  ind_max--;
647  }
648  ind_max++;
649 
650  CPLCHECK_INT ("Cannot get last pixel");
651 
652  /* Compute the mean spectral resolution */
653  cpl_array * all_steps = cpl_array_new (n_region * (n_wave-1), CPL_TYPE_DOUBLE);
654  for (int region=0; region < n_region; region++) {
655  const cpl_array * data = cpl_table_get_array (wave_data, GRAVI_DATA[region], 0);
656  for (wave=0; wave < n_wave-1; wave ++)
657  cpl_array_set (all_steps, region*(n_wave-1)+wave, fabs (cpl_array_get (data, wave+1, NULL) - cpl_array_get (data, wave, NULL) ));
658  }
659 
660  double res = cpl_array_get_median (all_steps);
661  FREE (cpl_array_delete, all_steps);
662 
663  /* n_element valid for the FT case or default if SC spectral channel change too much from nominal */
664  n_element = ind_max-ind_min+1;
665 
666  /* Constant wavelength channel width for FT - pkervell 16/03/2016 */
667  /* Nominal spectral channel width for FT in meters */
668  /* ft_res = 0.082308 * pow(10, -6);*/
669  /* Allow for a +/- 5% variation margin around nominal value
670  if (( res >= 0.95*ft_res) && (res <= 1.05*ft_res)) {
671  res = ft_res;
672  int n_element = ind_max-ind_min+1;
673  cpl_msg_info (cpl_func, "FT spectral resolution res = %e m, n_element = %i", res, n_element);
674  }*/
675 
676  /* Constant wavelength channel width for SC - pkervell 16/03/2016 */
677  /* Nominal spectral channel width for SC in meters */
678  sc_low_res = 0.0418181818 * pow(10, -6);
679  sc_medium_res = 0.0022009569 * pow(10, -6);
680  sc_high_res = 0.0002642248 * pow(10, -6);
681 
682  /* SC low resolution case */
683  /* Not implemented in low resolution due to tighter wavelength limits than 1.99-2.45 microns */
684  if (( res >= 0.95*sc_low_res) && (res <= 1.05*sc_low_res)) {
685  n_element = round(((lambda_max-lambda_min)/sc_low_res) + 2); // plus two, because it is always better to have a little margin at low resolution, especially with the 3 pixels interpolation
686  cpl_msg_info (cpl_func, "Chosen SC LOW spectral resolution element = %e m, n_element = %i", sc_low_res, n_element);
687  }
688 
689  /* SC medium resolution case */
690  /* Allow for a +/- 5% variation margin around nominal value */
691  if ((res >= 0.95*sc_medium_res ) && (res <= 1.05*sc_medium_res)) {
692  n_element = round(((lambda_max-lambda_min)/sc_medium_res) + 1);
693  cpl_msg_info (cpl_func, "Chosen SC MEDIUM fixed spectral element = %e m, n_element = %i", sc_medium_res, n_element);
694  }
695 
696 
697  /* SC high resolution case */
698  /* Allow for a +/- 10% variation margin around nominal value */
699  if ((res >= 0.9*sc_high_res ) && (res <= 1.1*sc_high_res)) {
700  n_element = round(((lambda_max-lambda_min)/sc_high_res) + 1);
701  cpl_msg_info (cpl_func, "Chosen SC HIGH fixed spectral element = %e m, n_element = %i", sc_high_res, n_element);
702  }
703 
704  CPLCHECK_INT ("Cannot compute nlambda");
705 
706  cpl_msg_info (cpl_func, "min=%e, max=%e, n=%f, res=%e, n=%i", lambda_min, lambda_max, (lambda_max-lambda_min)/res, res, n_element);
707 
708  // add security here, or the interpolation will crash sometimes
709  if (n_element > n_wave)
710  n_element = n_wave;
711 
712  return n_element;
713 }
714 
715 /*---------------------------------------------------------------------------*/
725 /*---------------------------------------------------------------------------*/
726 
727 int * gravi_image_extract_dimension (cpl_image * img_profile)
728 {
729  cpl_ensure (img_profile, CPL_ERROR_NULL_INPUT, NULL);
730 
731 // /* Collapse */
732 // cpl_image * collapse_img = cpl_image_collapse_create (img_profile, 0);
733 // cpl_size nx = cpl_image_get_size_x (collapse_img);
734 //
735 // /* Median filter */
736 // cpl_size size = nx < 60 ? 2 : 5;
737 // cpl_mask * kernel = cpl_mask_new (size, 1);
738 // cpl_mask_not (kernel);
739 //
740 // cpl_image * filt_img = cpl_image_duplicate (collapse_img);
741 // cpl_image_filter_mask (filt_img, collapse_img, kernel,
742 // CPL_FILTER_MEDIAN, CPL_BORDER_FILTER);
743 //
744 // /* Search for limits */
745 // double max = cpl_image_get_max (filt_img) * 0.1;
746 // double imin = nx, imax = 0;
747 // for (cpl_size i = 1; i <= nx; nx++) {
748 // if (cpl_image_get (filt_img, i, 1) > max) {
749 // if (i < imin) imin = i;
750 // if (i > imax) imax = i;
751 // }
752 // }
753 //
754 // /* Return */
755 // int dim = cpl_malloc (2 * sizeof (int));
756 // dim[0] = imin;
757 // dim[1] = imax - imin + 1;
758 //
759  double sig;
760  cpl_vector * vector, * vect, * vect_mean;
761 
762  cpl_size nx = cpl_image_get_size_x (img_profile);
763  cpl_size ny = cpl_image_get_size_y (img_profile);
764 
765  vect_mean = cpl_vector_new_from_image_row (img_profile, 1);
766  for (cpl_size i = 2; i <= ny; i++) {
767  vect = cpl_vector_new_from_image_row (img_profile, i);
768  cpl_vector_add (vect_mean, vect);
769  cpl_vector_delete (vect);
770  }
771 
772  cpl_vector_divide_scalar (vect_mean, ny);
773  if (nx < 60) { // case for LOW res
774  vector = cpl_vector_filter_median_create (vect_mean, 2);
775  }
776  else{ // case for MED and HIGH
777  vector = cpl_vector_filter_median_create (vect_mean, 5);
778  }
779 
780  double max = cpl_vector_get_max (vector);
781  sig = max * 0.10; // cut the edge to 10 % of the flux
782 
783  double sum = 0;
784  int i_2 = nx, i_1 = 0;
785  for (cpl_size i = 1; i < nx - 1; i++){
786  if (cpl_vector_get (vector, i) > sig){
787  sum ++;
788  if ((cpl_vector_get (vector, i - 1) > sig) && (cpl_vector_get (vector, i + 1) < sig))
789  i_2 = i;
790  }
791  }
792 
793  for (cpl_size i = 1; i < nx - 1; i++){
794  if (cpl_vector_get (vector, nx - i) > sig){
795  sum ++;
796  if ((cpl_vector_get (vector, nx - i - 1) < sig) && (cpl_vector_get (vector, nx - i + 1) > sig))
797  i_1 = nx - i;
798  }
799  }
800 
801  /* increase by 1 pixels */
802  i_1 -= 1;
803  i_2 += 2;
804  if (i_1 < 0) i_1 = 0;
805  if (i_2 >= nx) i_2 = nx-1;
806 
807  /* Fill output */
808  int * dim = cpl_malloc (2 * sizeof (int));
809  dim [0] = i_1 + 1;
810  dim [1] = i_2 - i_1;
811 
812  cpl_vector_delete (vect_mean);
813  cpl_vector_delete (vector);
814 
815  return dim;
816 }
817 
818 
819 /*----------------------------------------------------------------------------*/
826 /*----------------------------------------------------------------------------*/
827 
828 short gravi_sta_index(int gravi_input, cpl_table * optical_train_table, cpl_table * array_geometry_table)
829 {
830  gravi_msg_function_start(0);
831  cpl_ensure (optical_train_table, CPL_ERROR_NULL_INPUT, 0);
832  cpl_ensure (array_geometry_table, CPL_ERROR_NULL_INPUT, 0);
833 
834  short lab_input=9;
835  switch (gravi_input) {
836  case 1:
837  lab_input = GRAVI_LABINPUT_1;
838  break;
839  case 2:
840  lab_input = GRAVI_LABINPUT_2;
841  break;
842  case 3:
843  lab_input = GRAVI_LABINPUT_3;
844  break;
845  case 4:
846  lab_input = GRAVI_LABINPUT_4;
847  break;
848  default:
849  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
850  "impossible GRAVITY input");
851  return 0;
852  }
853 
854  cpl_msg_debug(cpl_func, "Start function");
855 
856  /* Get tel_name corresponding to lab_input in optical_train_table */
857  int row, null;
858  for (row=0; row < cpl_table_get_nrow(optical_train_table); ++row) {
859  if (cpl_table_get_int(optical_train_table, "VALUE2", row, &null) ==
860  lab_input) {
861  break;
862  }
863  }
864  if (row >= cpl_table_get_nrow(optical_train_table)) {
865  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
866  "lab input not found in optical train table");
867  return 0;
868  }
869 
870  char * tel_name;
871  tel_name = cpl_sprintf ("%s",cpl_table_get_string(optical_train_table, "TEL_NAME", row));
872 
873  /* trim tel_name */
874  {
875  int cur = strlen (tel_name);
876  while (cur) {
877  if (tel_name[cur-1] == ' ') {
878  --cur;
879  tel_name[cur]=0;
880  } else break;
881  }
882  }
883 
884  /* Get STA_INDEX corresponding to tel_name in array_geometry_table */
885  char * cur_name = NULL;
886  int found=0;
887 
888  for (row = 0; (!found) && (row < cpl_table_get_nrow(optical_train_table)); ++row) {
889 
890  cur_name = cpl_sprintf ("%s",cpl_table_get_string(array_geometry_table, "TEL_NAME", row));
891 
892  /* trim cur_name */
893  int cur = strlen (cur_name);
894  while (cur) {
895  if (cur_name[cur-1] == ' ') {
896  --cur;
897  cur_name[cur]=0;
898  } else break;
899  }
900  if (!strcmp (tel_name, cur_name)) found=1;
901  FREE (cpl_free, cur_name);
902  if (found) break;
903  }
904 
905  FREE (cpl_free, tel_name);
906 
907  if (!found) {
908  cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
909  "telescope not found in array geometry");
910  return 0;
911  }
912 
913 
914  short res = cpl_table_get_int(array_geometry_table, "STA_INDEX", row, &null);
915 
916  cpl_msg_debug(cpl_func, "gravi_input=%i; lab_input=%i; sta_index=%i", gravi_input, lab_input, res);
917  cpl_msg_debug(cpl_func, "End function");
918 
919  gravi_msg_function_exit(0);
920  return res;
921 }
922 
923 int gravi_get_shutter (cpl_propertylist * header, int tel)
924 {
925  cpl_ensure (header, CPL_ERROR_ILLEGAL_INPUT, -1);
926 
927  char key[90];
928  sprintf (key, SHUTTER_KEY"%d ST", tel+1);
929 
930  /* Try the old and the new shutter name */
931  if ( !cpl_propertylist_has (header, key) ) {
932  sprintf (key, GRAVI_SHUTTER_KEY"1%d ST", tel+1);
933 
934  if ( !cpl_propertylist_has (header, key) ) {
935  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT, "Missing shutter");
936  return -1;
937  }
938  }
939 
940  return cpl_propertylist_get_bool (header, key);
941 }
942 
943 int gravi_check_shutter (cpl_propertylist * header, int t0, int t1, int t2, int t3)
944 {
945  cpl_ensure (header, CPL_ERROR_ILLEGAL_INPUT, -1);
946 
947  if ( gravi_get_shutter (header, 0) == t0 &&
948  gravi_get_shutter (header, 1) == t1 &&
949  gravi_get_shutter (header, 2) == t2 &&
950  gravi_get_shutter (header, 3) == t3 )
951  return 1;
952 
953  return 0;
954 }
955 
956 int gravi_get_shutter_id (cpl_propertylist * header)
957 {
958  cpl_ensure (header, CPL_ERROR_ILLEGAL_INPUT, -1);
959 
960  if (gravi_check_shutter (header,1,0,0,0)) return 0;
961  if (gravi_check_shutter (header,0,1,0,0)) return 1;
962  if (gravi_check_shutter (header,0,0,1,0)) return 2;
963  if (gravi_check_shutter (header,0,0,0,1)) return 3;
964 
965  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
966  "More than one shutter open, or none");
967  return -1;
968 }
969 
970 int gravi_get_shutter_baseid (cpl_propertylist * header)
971 {
972  cpl_ensure (header, CPL_ERROR_ILLEGAL_INPUT, -1);
973 
974  if (gravi_check_shutter (header,1,1,0,0)) return 0;
975  if (gravi_check_shutter (header,1,0,1,0)) return 1;
976  if (gravi_check_shutter (header,1,0,0,1)) return 2;
977  if (gravi_check_shutter (header,0,1,1,0)) return 3;
978  if (gravi_check_shutter (header,0,1,0,1)) return 4;
979  if (gravi_check_shutter (header,0,0,1,1)) return 5;
980 
981  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
982  "More than two shutter open, or less");
983  return -1;
984 }
985 
986 int gravi_data_check_shutter_beam (gravi_data ** datas, int nb_datas)
987 {
988  cpl_ensure (datas, CPL_ERROR_ILLEGAL_INPUT, -1);
989 
990  /* Wrong number of flats */
991  if ( nb_datas != 4 ) return 0;
992 
993  /* Count the single-shutter open for each tel */
994  int is_open[] = {0,0,0,0};
995  for (cpl_size f = 0 ; f < 4 ; f++) {
996  cpl_propertylist * header = gravi_data_get_header (datas[f]);
997  is_open[0] += gravi_check_shutter (header,1,0,0,0);
998  is_open[1] += gravi_check_shutter (header,0,1,0,0);
999  is_open[2] += gravi_check_shutter (header,0,0,1,0);
1000  is_open[3] += gravi_check_shutter (header,0,0,0,1);
1001  }
1002 
1003  cpl_msg_info (cpl_func,"is_open = %i %i %i %i", is_open[0], is_open[1], is_open[2], is_open[3]);
1004 
1005  /* Check all open, and once only */
1006  int check = 1;
1007  for (cpl_size f = 0 ; f < 4 ; f++) if (is_open[f] != 1) check *= 0;
1008 
1009  return check;
1010 }
1011 
1012 
1013 cpl_size gravi_spectrum_get_nwave (const cpl_table * table)
1014 {
1015  return cpl_table_get_column_depth (table,"DATA1");
1016 }
1017 
1018 cpl_size gravi_spectrum_get_nregion (const cpl_table * table)
1019 {
1020  cpl_ensure (table, CPL_ERROR_NULL_INPUT, -1);
1021 
1022  cpl_size nregion = 0;
1023  for (cpl_size col = 0; col < 48 ; col++) {
1024  nregion += cpl_table_has_column (table, GRAVI_DATA[col]);
1025  }
1026 
1027  return nregion;
1028 }
1029 
1030 /*----------------------------------------------------------------------------*/
1040 /*----------------------------------------------------------------------------*/
1041 
1042 double gravi_spectrum_get_flux (const cpl_table * table)
1043 {
1044  cpl_ensure (table, CPL_ERROR_NULL_INPUT, 0);
1045 
1046  cpl_size n_reg = gravi_spectrum_get_nregion (table);
1047  cpl_size n_row = cpl_table_get_nrow (table);
1048  double flux = 0.0;
1049 
1050  for (cpl_size reg = 0; reg < n_reg ; reg++) {
1051 
1052  cpl_size size = cpl_table_get_column_depth (table, GRAVI_DATA[reg]);
1053  cpl_array ** arrays = cpl_table_get_data_array ((cpl_table *)table,GRAVI_DATA[reg]);
1054 
1055  for (cpl_size row=0; row < n_row ; row++)
1056  flux += cpl_array_get_mean (arrays[row]) * size;
1057  }
1058 
1059  return flux;
1060 }
1061 
1062 /*---------------------------------------------------------------------------*/
1079 /*---------------------------------------------------------------------------*/
1080 
1081 cpl_vector * gravi_compute_envelope (const cpl_vector *opd, int wave, int nwave)
1082 {
1083  cpl_ensure (opd, CPL_ERROR_NULL_INPUT, NULL);
1084  cpl_ensure (wave>=0 && wave<nwave, CPL_ERROR_ILLEGAL_INPUT, NULL);
1085 
1086  /* Compute delta_lambda and lambda from experience */
1087  double delta_lambda = (nwave == 5) ? 0.13 : 0.45 / nwave * 3;
1088  double lambda = 2.0 + 0.45 / nwave * wave;
1089 
1090  /* Compute coherent length */
1091  double coh_len= (lambda*lambda) / delta_lambda * 1.e-6;
1092 
1093  long nrow = cpl_vector_get_size (opd);
1094  cpl_vector * envelope = cpl_vector_new (nrow);
1095 
1096  /* Gaussian enveloppe */
1097  for (long row = 0; row < nrow; row++){
1098  double value = cpl_vector_get (opd, row);
1099  cpl_vector_set (envelope, row, exp(-1*(value*value)/(coh_len*coh_len/2.)));
1100  CPLCHECK_NUL ("Cannot compute envelope");
1101  }
1102 
1103  return envelope;
1104 }
1105 
1106 /*----------------------------------------------------------------------------*/
1115 /*----------------------------------------------------------------------------*/
1116 
1117 double gravi_imagelist_get_flux (const cpl_imagelist * imglist)
1118 {
1119  cpl_ensure (imglist, CPL_ERROR_NULL_INPUT, 0);
1120 
1121  cpl_size size = cpl_imagelist_get_size (imglist);
1122  double flux = 0.0;
1123 
1124  for (cpl_size row = 0; row < size; row ++)
1125  flux += cpl_image_get_flux (cpl_imagelist_get_const (imglist, row));
1126 
1127  return flux;
1128 }
1129 
1130 /*----------------------------------------------------------------------------*/
1143 /*----------------------------------------------------------------------------*/
1144 
1145 cpl_error_code gravi_lkdt_get_sequence (cpl_table * oi_table,
1146  cpl_size ntel,
1147  cpl_size *first, cpl_size *nobs)
1148 {
1149  cpl_ensure_code (oi_table, CPL_ERROR_NULL_INPUT);
1150 
1151  cpl_size cfirst = 0, cnobs = 0;
1152  cpl_size nrow = cpl_table_get_nrow (oi_table) / ntel;
1153  double * lkdt = cpl_table_get_data_double (oi_table, "LKDT_MET_FC");
1154  cpl_ensure_code (lkdt, CPL_ERROR_ILLEGAL_INPUT);
1155 
1156  for (cpl_size row = 1; row < nrow; row++) {
1157 
1158  /* If change of lockdate, we start counting again */
1159  for (cpl_size tel = 0; tel < ntel; tel++)
1160  if (lkdt[row*ntel+tel] != lkdt[(row-1)*ntel+tel]) cfirst = row;
1161 
1162  /* If more than before, we save this sequence */
1163  cnobs = row - cfirst + 1;
1164  if (cnobs > *nobs) {*nobs = cnobs; *first = cfirst;}
1165  }
1166 
1167  return CPL_ERROR_NONE;
1168 }
1169 
1170 int gravi_conf_get_iss (int gravi_beam, cpl_propertylist * header)
1171 {
1172  cpl_ensure (gravi_beam >=0 && gravi_beam < 4, CPL_ERROR_ILLEGAL_INPUT, 0);
1173  cpl_ensure (header, CPL_ERROR_NULL_INPUT, 0);
1174 
1175  int iss = 0;
1176  char name[100];
1177 
1178  for (iss = 1; iss<=4; iss++) {
1179  sprintf (name, "ESO ISS CONF INPUT%i", iss);
1180  if ( cpl_propertylist_get_int (header, name) ==
1181  GRAVI_LABINPUT[gravi_beam]) return iss;
1182  }
1183 
1184  cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
1185  "Could not find CONF INPUT for beam %i",
1186  gravi_beam);
1187 
1188  return 0;
1189 }
1190 
1191 const char * gravi_conf_get_telname (int gravi_beam, cpl_propertylist * header)
1192 {
1193  cpl_ensure (gravi_beam >=0 && gravi_beam < 4, CPL_ERROR_ILLEGAL_INPUT, NULL);
1194  cpl_ensure (header, CPL_ERROR_NULL_INPUT, NULL);
1195  char name[100];
1196 
1197  for (int iss = 1; iss<=4; iss++) {
1198 
1199  sprintf (name, "ESO ISS CONF INPUT%i", iss);
1200  if ( !cpl_propertylist_has (header, name)) continue;
1201 
1202  if ( cpl_propertylist_get_int (header, name) ==
1203  GRAVI_LABINPUT[gravi_beam]) {
1204 
1205  sprintf (name, "ESO ISS CONF T%iNAME", iss);
1206  if ( !cpl_propertylist_has (header, name)) continue;
1207 
1208  return cpl_propertylist_get_string (header, name);
1209  }
1210  }
1211 
1212  cpl_msg_debug (cpl_func,"Telscope name cannot be find in header");
1213  return NULL;
1214 }
1215 
1216 
1217 cpl_error_code gravi_dump_the_boss (double ra, double dec)
1218 {
1219  /* GC coordinates in [rad] */
1220  double c_ra = gravi_ra_to_rad ("17:45:40.03599");
1221  double c_dec = gravi_dec_to_rad ("-29:00:28.1699");
1222 
1223  /* Distance in [rad] */
1224  double dist = acos ( sin (c_dec) * sin (dec) + cos (c_dec) * cos (dec) * cos (ra - c_ra) );
1225 
1226  /* Distance in [as] */
1227  dist = dist / CPL_MATH_PI * 180 * 3600;
1228  cpl_msg_info (cpl_func, "Distance from GC: %g [as]", dist);
1229 
1230  if (dist > 20.0) return CPL_ERROR_NONE;
1231 
1232  printf ("***************************************************************************************************\n");
1233  printf (" \n");
1234  printf (" You are reducing data on the Galactic Center !!!! \n");
1235  printf (" \n");
1236  printf (" MAY THE FRINGES BE WITH YOU !!!! \n");
1237  printf (" \n");
1238  printf (" \n");
1239  printf (" \n");
1240  printf (" `...` ``` ` ```` ```` \n");
1241  printf (" `....:/:::--://+/:--:/::--.-`````` \n");
1242  printf (" `.-://::.-----::/+oohyso/++:/:::.-.-://-.` \n");
1243  printf (" ```.:/:+o+/::/+ooossoo+oosso++::+/:://:+:osso//-`` \n");
1244  printf (" ```.:+ooos+:/+shddhhhysosooso+o++ooo/+sso+////:///:-` \n");
1245  printf (" ``.-:+yso/::+shdhhddhysssyhyhyyyysoyso+shys+o////::++:.` \n");
1246  printf (" `.-/+++syo//+syhdddddhhyyhddhhyyyhysyhyysyhdss+o+/o++:/+/:-.` \n");
1247  printf (" `:/ss++oo/osyhddddmmmdddhddmdhhyhhhhhhhhyssyyoo/s+:+hoo/:://-.` \n");
1248  printf (" `-+ss+oo++oyddmmmmmNNmddddddddhhhdhhhhhhyyyyy+o++so/ssyys+/+o+-.` \n");
1249  printf (" `.+oysssosshdmmmmmmmdmddddhhddddddysssssyysyso+///+osysyoyyo+os+:. \n");
1250  printf (" `-/syosyyydmmdddhhhyhhhhhhyhhddhso////+ssoo+/:----/:+oshs+ss++sso/` \n");
1251  printf (" `:oyyohdhdmmdyyyysssssooo+oo+++/:----:/+::::-...---../ohho/so/osoo:` \n");
1252  printf (" `./yyyhmdNNmhyssssoo+++//:::::-------------.......--..-/yyy/+ssoooo+-` \n");
1253  printf (" `.ohshmmmNNdyoooooo++///::-:------------..........-----:shss+oooo+so/. \n");
1254  printf (" `-syshmmNNNdsooooo+++//:::--------------...........----:+hhsss/++++o/. \n");
1255  printf (" `+syhdmNNNhsoooo++++/::::-----.-------..............---/dhyso++++oo:. \n");
1256  printf (" -yhhmmNNNhyooo+++++//::::--....-------...-........---:/ddhs/+/o+o+:`` \n");
1257  printf (" `shdmNMNmdysoo+++++/////:::------------------......---:smh+/+/++o/.` \n");
1258  printf (" `oydmNMMmhyo+++++ooo+++++///::---:::::://///////:...--:sNd++o:/o+:. \n");
1259  printf (" `+ydmmNMNhy++ooyhhhhdddddhyo++/:-://+shdddddhhyys+-..-:sNdoss//o/-` \n");
1260  printf (" osmdmNMmhsoyyddmmmdddmmmmdhyy+:::/oshdmmmddhhhyso+/--:sNmhhy++o:`` \n");
1261  printf (" +hmmNNNmyohhhhhhhyyhhhdhhdhdh+:--:oyhddhdddhhyysso/:::smmdhhoo/-` \n");
1262  printf (" `yhmNNmmhsyhyhhdmmmNmdyhdddddhs:--+ydyhhdhdmmmddhso+/y/+yhhhs+//+` \n");
1263  printf (" `yhhdmNNdoshshdddhhddhssyhydmd+-.-/ydysyysyhhyosyhy+:s-/dmh+///ss. \n");
1264  printf (" .syyhdNNh+oysssyyyyyssssyyyhhy/-.-:ossoosyysso++//////-:sds/:--/o. \n");
1265  printf (" -sssyhddo+oooosssssssssssssyys/-..-:///++osoo++///::----/s/+/---+` \n");
1266  printf (" `oosshyy++ooo+++ooooo++++ossso+--.-::::-::/+///::-------:+/so+/--` \n");
1267  printf (" :+ossshsosooo++////:://+osso+/:-..-:::-----------------//-:+/:-. \n");
1268  printf (" -+ososhhsyssoo+///::::/+sso++:--...-::---------:::--::/o:--://-` \n");
1269  printf (" -++osshmhyyysoo+//////+osso++/---...-::---------::::::sy/::::--` \n");
1270  printf (" `o++sshdhyyyyysooo++/+osyssso///:--:/:+/---:---::::::/ys/:/:.--` \n");
1271  printf (" :/osyhhyyyyyyssooo++oyyyhdmhysssoohy++so/:::::/:::::::+o+::... \n");
1272  printf (" `+oossyysyyhyyssooosyssshdmmddddyso/::/oyo/////////::://::---` \n");
1273  printf (" .+sssoosyyyyyysoosyssssyhhhhhhhs++/////oys++++++//::/+///:.` \n");
1274  printf (" `..`.syhyyyysssyyssssssssooss+//::////ohs++++++/::/.``` \n");
1275  printf (" +yyyhyysssssyyssssyssoooooo++///+osyo++++//:-:` \n");
1276  printf (" -syyhyysossyhdmmdhhyyyyysyshddhyssss+++++/:::: \n");
1277  printf (" `syyyyysosssyhddyso+//+++o+oso+/++o+//+++/:::. \n");
1278  printf (" :yyyyysossssssssso++++++/++//////++/+++//::. \n");
1279  printf (" `+yyyyysssssssyssssssosso+///////++++++///-` \n");
1280  printf (" `syyyhyysssssyyyysssssso++///////++/++//:. \n");
1281  printf (" .syhhhyysyyssssooooo+++///////////+++//:. \n");
1282  printf (" .shhyyyssssooo++///:::::/::/://++o+//:-. \n");
1283  printf (" `+yyyyssooooo+/////:::::::://++oo+/::-. \n");
1284  printf (" -yhyyssssso++/+++++//////++osso//::--` \n");
1285  printf (" -hhhhyyyyysooosssooo++oosyyso+/:::--. \n");
1286  printf (" /hddddddhhhyyyyyyyyyyyhhyys+//:::---. \n");
1287  printf (" `ohdmmmmmmmmmmmmdddddhhyyso+//:::----. \n");
1288  printf (" .yhddmmmmmmmmdddhyyyyssso+///:::----.` \n");
1289  printf (" :hhdddddddhhhyyyssoooooo++/:::::--....` \n");
1290  printf (" `ohdhhhhhhyyyysssooooooo++//::::---.--.` \n");
1291  printf (" .yhhhyyyyyyyyyyssssssoo++//:::::--......` \n");
1292  printf (" `.-:hhdhhyyyyhhyyyyssssoo+++//:::::---.......-+ssoo- \n");
1293  printf (" ``.-/oyhmsmmhdddhyhyyyyysssssooo+++///:::::---......:+dNNNd. \n");
1294  printf (" ```.-/+syhmNNNNNydMdhhhdhyyssssssoooooooo+++///::::------:+ymNNNmdy/```` \n");
1295  printf (" ``.--:/+oyhdmNNNNNNNNNNNNoMMhhhhhhyssssssooosooo+++++////:::---:+ymNNNNmy/-:oyhho/-` \n");
1296  printf (" ``-/sydmmmmNNNNNNNNNNNNNNNNNNdyMNhyhhhhyssooooooooooo++++++/////::+ymNNNNNh+-:odmmmmmmmmds: \n");
1297  printf (" ./sdmmmmNNNNNNNNNNNNNNNNNNNNNNNsdMNyyhhhhhysssooooooso+++++++++//+ydNMMMNms:-odNNNNNNmmmmmmmm \n");
1298  printf (" `-odmmNNNNNNNNNNNNNNNNNNNNNNNNNNMNomMNyyhhhhhhyyyssooooooo++++++++ydNMMMMNh+-+hNNNNNNNNNNNmmmmmm \n");
1299  printf (" /ymNNNNNNNNNNNNNNNNNNNNNNNNNNNNNMMM+mMMmhhhhhhhyyysssooooooo+++oydNMMMMNms:/ymNNNNNNNNNNNNNNNmmmm \n");
1300  printf (" NNNNNNNNNNNNNNNNMMMNNNNNNNNNNNNNMMMshMMMmdhhhhhyyyyssoooooooshmNMMMMMNh+:smNNNNNNNNNNNNNNNNNNNmmm \n");
1301  printf (" NNNNNMMMNNNNNNMMMMMMNNNNNNNNNNNMMMMd+MMMMmdhhhhyyyysoooooshmMMMMMMNh+:odNNNNNNNNNNNNNNNNNNNNNNmmm \n");
1302  printf (" NMNNMMMMMMMMMMMMMMMMMMMMMNNNNMMMMMMN:mMMMNmhhdhhhyysooydNMMMMMMNy+:odNMMNNNNNNNNNNNNNNNNNNNNNNNmm \n");
1303  printf (" MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMy/MMMMMdhhhyyyyhdNMMMMMNms//sdNMMMNNMNNNNNNNNNNNNNNNNNNNNNNNm \n");
1304  printf (" MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN/oMMMMMmdhhmNMMMMMMNho/+yNMMMMMMMMMNNNNNNNNNNNNNNNNNNNNNNNNN \n");
1305  printf (" coutesis: The GRAVITY pipeline team \n");
1306  printf ("***************************************************************************************************\n");
1307  return CPL_ERROR_NONE;
1308 }
1309 
short gravi_sta_index(int gravi_input, cpl_table *optical_train_table, cpl_table *array_geometry_table)
Retrieve STA_INDEX corresponding to a given input.
Definition: gravi_utils.c:828
int gravi_region_get_pol(cpl_table *imaging_detector, int region)
Return the polarisation id of a region.
Definition: gravi_utils.c:445
int gravi_region_get_base(cpl_table *imaging_detector, int region)
Return the base of a region.
Definition: gravi_utils.c:361
int gravi_region_get_base_sign(cpl_table *imaging_detector, int base)
Return the sign of a base by looking at the PORT order.
Definition: gravi_utils.c:399
cpl_error_code gravi_lkdt_get_sequence(cpl_table *oi_table, cpl_size ntel, cpl_size *first, cpl_size *nobs)
Return the longuest sequence with constant LKDT.
Definition: gravi_utils.c:1145
double gravi_imagelist_get_flux(const cpl_imagelist *imglist)
Return the total flux in imagelist.
Definition: gravi_utils.c:1117
cpl_vector * gravi_compute_envelope(const cpl_vector *opd, int wave, int nwave)
Compute the envelope value.
Definition: gravi_utils.c:1081
int gravi_get_region(cpl_table *img_det, int base, char phase, int pol)
Find the region matching base, phase and pol.
Definition: gravi_utils.c:584
int * gravi_image_extract_dimension(cpl_image *img_profile)
Compute startx and nx of the illuminated part of the image.
Definition: gravi_utils.c:727
int gravi_region_get_phaseid(cpl_table *imaging_detector, int region)
Return the phase id of a region.
Definition: gravi_utils.c:479
const char * gravi_get_license(void)
Get the pipeline copyright and license.
Definition: gravi_utils.c:104
double gravi_spectrum_get_flux(const cpl_table *table)
Return the total flux in DATA# regions.
Definition: gravi_utils.c:1042
char gravi_region_get_phase(cpl_table *imaging_detector, int region)
Return the phase character of a region.
Definition: gravi_utils.c:551
int gravi_region_get_tel(cpl_table *imaging_detector, int region, int beam)
Return the telescope id (0,1,2,3) in a beam of a region.
Definition: gravi_utils.c:512
int gravi_wave_get_nlambda(cpl_table *wave_data, double lambda_min, double lambda_max)
Get the number of spectral element between lambdamin et lambdamax.
Definition: gravi_utils.c:611
cpl_table * gravi_table_oi_create(int nwave, int nrow, const char *oi_name)
Create the oi table (oi_vis, oi_vis2, oi_t3)
Definition: gravi_utils.c:153