MIDI Pipeline Reference Manual  2.8.3
midi_cplutils.c
1 /******************************************************************************
2 *******************************************************************************
3 * European Southern Observatory
4 * VLTI MIDI Data Reduction Software
5 *
6 * Module name: midicplutils.c
7 * Description: reads a table column into an imagelist
8 *
9 * History:
10 * 01-Mai-09 (agabasch) Created
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /******************************************************************************
15 * Include files
16 ******************************************************************************/
17 
18 #include "midi_cplutils.h"
19 #include <cpl.h>
20 #include <string.h>
21 #include "midi_cplupgrade.h"
22 
23 int timetable_to_cpltable(const char * columname,
24  cpl_table * intable,
25  cpl_table * outtable)
26 {
27 
28  int dimen_in=0, dimen_out=0;
29  cpl_table * dummy_table=NULL;
30  cpl_errorstate prestate = cpl_errorstate_get();
31 
32  /* Load extension Imaging Data */
33 
34 
35  dimen_out=cpl_table_get_nrow(outtable);
36  dimen_in =cpl_table_get_nrow(intable);
37 
38  dummy_table=cpl_table_new(dimen_in);
39  cpl_table_duplicate_column(dummy_table, columname, intable, columname);
40  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
41 
42 /* cpl_table_dump_structure (intable,NULL); */
43 /* cpl_table_dump_structure (outtable,NULL); */
44 /* cpl_table_dump_structure (dummy_table,NULL); */
45 
46  cpl_table_insert(outtable, dummy_table, dimen_out+1);
47  if(dummy_table!=NULL){
48  cpl_table_delete(dummy_table);
49  }
50  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
51 
52  return (int)cpl_error_get_code();
53 }
54 
55 
56 
57 
58 int table_to_imglst(const char * columname,
59  cpl_imagelist * imglst,
60  cpl_table * table)
61 {
62 
63  int dimenDATA=0;
64  int i=0, counter=0;
65  cpl_array * array_data=NULL;
66  cpl_image * image_data_int=NULL;
67  cpl_errorstate prestate = cpl_errorstate_get();
68  cpl_type ctype;
69 
70  /* Load extension Imaging Data */
71 
72  dimenDATA=cpl_table_get_column_dimensions(table, columname);
73  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
74  if (dimenDATA != 2) {
75  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
76  "DATA has a wrong dimension");
77  }
78  ctype=cpl_table_get_column_type(table, columname);
79 /* cpl_msg_info(cpl_func, "Type of the table column: %d",ctype); */
80 
81  counter=cpl_imagelist_get_size(imglst);
82 
83 
84  for (i=0; i<cpl_table_get_nrow(table);i++){
85 
86  array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
87  if(ctype&CPL_TYPE_INT){
88  image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
89  }
90  if(ctype&CPL_TYPE_FLOAT){
91  image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
92  }
93  /* Cast the image to float */
94 /* image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT); */
95 
96 /* Append the image to the imagelists */
97 
98  cpl_imagelist_set(imglst,cpl_image_cast(image_data_int, CPL_TYPE_FLOAT),counter++);
99 
100  /*Unwrap processed image manualy if nor Target nor Sky*/
101  if(image_data_int!=NULL){
102  cpl_image_unwrap(image_data_int);
103  }
104 
105 
106  }
107 
108 
109 
110  return (int)cpl_error_get_code();
111 }
112 /*----------------------------------------------------------------------------*/
119 /*----------------------------------------------------------------------------*/
120 
121 int midi_check_sof(cpl_frameset * frameset, const char * tag)
122 {
123  char * tag_file=NULL;
124  cpl_frame * cur_frame=NULL;
125  int NumberOfFiles=0;
126 
127  cur_frame = cpl_frameset_get_first(frameset);
128  if (cur_frame == NULL) {
129  return (int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
130  "SOF does not have any file");
131  }
132 
133 /* Walk through the whole Set of Frames SOF */
134 
135  NumberOfFiles=0;
136 
137  while(cur_frame)
138  {
139  tag_file = (char*)cpl_frame_get_tag(cur_frame);
140 
141  if (strcmp(tag_file, tag)) {
142  cur_frame = cpl_frameset_get_next( frameset );
143  continue;
144  }
145  if (strcmp(tag_file, tag)==0) {
146  NumberOfFiles+=1;
147  }
148  cur_frame = cpl_frameset_get_next( frameset );
149  }
150 
151  return NumberOfFiles;
152 
153 }
154 /*----------------------------------------------------------------------------*/
202 /*----------------------------------------------------------------------------*/
203 
204 cpl_polynomial * midi_polynomial_fit_1d_create(
205  const cpl_vector * x_pos,
206  const cpl_vector * values,
207  int degree,
208  double * mse)
209 {
210  cpl_polynomial * fit1d;
211  cpl_matrix * samppos;
212  cpl_vector * fitresidual;
213 
214  cpl_size degree_tmp=0;
215 
216  fit1d = cpl_polynomial_new(1);
217  samppos = cpl_matrix_wrap(1, cpl_vector_get_size(x_pos),
218  cpl_vector_get_data((cpl_vector *)x_pos));
219  fitresidual = cpl_vector_new(cpl_vector_get_size(x_pos));
220  cpl_polynomial_fit(fit1d, samppos, NULL, values, NULL,
221  CPL_FALSE, NULL, &degree_tmp);
222 
223  degree=(int)(degree_tmp);
224 
225  cpl_vector_fill_polynomial_fit_residual(fitresidual, values, NULL, fit1d,
226  samppos, NULL);
227  cpl_matrix_unwrap(samppos);
228 
229  if(mse!=NULL){
230  *mse = cpl_vector_product(fitresidual, fitresidual)
231  /(double)(cpl_vector_get_size(fitresidual));
232  }
233  cpl_vector_delete(fitresidual);
234  return fit1d;
235 }