SINFONI Pipeline Reference Manual  2.6.0
sinfo_utils_wrappers.c
1 
2 /* *
3  * This file is part of the ESO SINFO Pipeline *
4  * Copyright (C) 2004,2005 European Southern Observatory *
5  * *
6  * This library 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, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
19  * */
20 
21 
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25 
27 /*---------------------------------------------------------------------------*/
35 /*--------------------------------------------------------------------------*/
36 
37 
38 #include <sinfo_utils_wrappers.h>
39 #include <sinfo_functions.h>
40 #include <sinfo_dump.h>
41 #include <sinfo_utils.h>
42 #include <sinfo_error.h>
43 #include "sinfo_msg.h"
44 /*---------------------------------------------------------------------------*/
45 
46 /*---------------------------------------------------------------------------*/
58 /*---------------------------------------------------------------------------*/
59 cpl_error_code
60 sinfo_sort_table_1(cpl_table *t, const char *column, cpl_boolean reverse)
61 {
62  cpl_propertylist *plist = NULL;
63 
64  assure(t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
65  assure(cpl_table_has_column(t, column), CPL_ERROR_ILLEGAL_INPUT,
66  "No column '%s'", column);
67 
68  check(( plist = cpl_propertylist_new(),
69  cpl_propertylist_append_bool(plist, column, reverse)),
70  "Could not create property list for sorting");
71 
72  check( cpl_table_sort(t, plist), "Could not sort table");
73 
74  cleanup:
75  sinfo_free_propertylist(&plist);
76  return cpl_error_get_code();
77 }
78 
79 /*---------------------------------------------------------------------------*/
95 /*---------------------------------------------------------------------------*/
96 cpl_error_code
97 sinfo_sort_table_2(cpl_table *t, const char *column1, const char *column2,
98  cpl_boolean reverse1, cpl_boolean reverse2)
99 {
100  cpl_propertylist *plist = NULL;
101 
102  assure(t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
103  assure(cpl_table_has_column(t, column1), CPL_ERROR_ILLEGAL_INPUT,
104  "No column '%s'", column1);
105  assure(cpl_table_has_column(t, column2), CPL_ERROR_ILLEGAL_INPUT,
106  "No column '%s'", column2);
107 
108  check(( plist = cpl_propertylist_new(),
109  cpl_propertylist_append_bool(plist, column1, reverse1),
110  cpl_propertylist_append_bool(plist, column2, reverse2)),
111  "Could not create property list for sorting");
112  check( cpl_table_sort(t, plist), "Could not sort table");
113 
114  cleanup:
115  sinfo_free_propertylist(&plist);
116  return cpl_error_get_code();
117 }
118 
119 /*---------------------------------------------------------------------------*/
136 /*---------------------------------------------------------------------------*/
137 cpl_table *
138 sinfo_extract_table_rows(const cpl_table *t, const char *column,
139  cpl_table_select_operator operator, double value)
140 {
141  cpl_table *result = NULL;
142  assure( t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
143  assure( cpl_table_has_column(t, column), CPL_ERROR_INCOMPATIBLE_INPUT,
144  "No such column: %s", column);
145 
146  /* 1. Extract (duplicate) the entire table
147  2. remove rows *not* satisfying the criterion */
148 
149  check(result = cpl_table_duplicate(t),"selecting");
150  check(sinfo_select_table_rows(result, column, operator, value),"select");
151  check(cpl_table_not_selected(result),"Inverses selection");
152  check(cpl_table_erase_selected(result),"erase selection");//problems
153 
154  /*
155  check(( result = cpl_table_duplicate(t),
156  sinfo_select_table_rows(result, column, operator, value),
157  cpl_table_not_selected(result), // Inverses selection
158  cpl_table_erase_selected(result)),
159  "Error extracting rows");
160  */
161 
162  cleanup:
163  if (cpl_error_get_code() != CPL_ERROR_NONE)
164  {
165  sinfo_free_table(&result);
166  }
167  return result;
168 }
169 
170 
171 /*---------------------------------------------------------------------------*/
188 /*---------------------------------------------------------------------------*/
189 
190 int
191 sinfo_select_table_rows(cpl_table *t, const char *column,
192  cpl_table_select_operator operator, double value)
193 {
194  int result = 0;
195  cpl_type type;
196  assure( t != NULL, CPL_ERROR_NULL_INPUT, "Null table");
197  assure( cpl_table_has_column(t, column), CPL_ERROR_INCOMPATIBLE_INPUT,
198  "No such column: %s", column);
199 
200  type = cpl_table_get_column_type(t, column);
201 
202  assure( type == CPL_TYPE_DOUBLE ||
203  type == CPL_TYPE_INT, CPL_ERROR_INVALID_TYPE,
204  "Column '%s' must be double or int. %s found", column,
205  sinfo_tostring_cpl_type(type));
206 
207  check( cpl_table_select_all(t), "Error selecting rows");
208 
209  if (type == CPL_TYPE_DOUBLE)
210  {
211  result = cpl_table_and_selected_double(t, column, operator, value);
212  }
213  else if (type == CPL_TYPE_INT)
214  {
215  result = cpl_table_and_selected_int (t, column, operator,
216  sinfo_round_double(value));
217  }
218  else { /*impossible*/ passure(CPL_FALSE, ""); }
219 
220  cleanup:
221  return result;
222 
223 }
224 
229 /*---------------------------------------------------------------------------*/
230 /* TODO: not used */
231 void sinfo_free_parameter(cpl_parameter **p)
232 {if(p){cpl_parameter_delete(*p); *p = NULL;}}
233 
234 
235 
240 /*---------------------------------------------------------------------------*/
241 /* TODO: not used */
242 void sinfo_free_apertures(cpl_apertures **a)
243 {if(a){cpl_apertures_delete(*a); *a = NULL;}}
244 /*---------------------------------------------------------------------------*/
245 
246 /*---------------------------------------------------------------------------*/
251 /*---------------------------------------------------------------------------*/
252 void sinfo_free_image(cpl_image **i) {if(i){cpl_image_delete(*i); *i = NULL;}}
253 
254 
255 /*---------------------------------------------------------------------------*/
260 /*---------------------------------------------------------------------------*/
261 void sinfoni_free_vector(cpl_vector **v) {if(v){cpl_vector_delete(*v); *v = NULL;}}
262 
263 
264 /*---------------------------------------------------------------------------*/
269 /*---------------------------------------------------------------------------*/
270 void sinfo_free_array(cpl_array **i) {if(i){cpl_array_delete(*i); *i = NULL;}}
271 
272 /*---------------------------------------------------------------------------*/
277 /*---------------------------------------------------------------------------*/
278 void sinfo_free_mask(cpl_mask **m) {if(m){cpl_mask_delete(*m); *m = NULL;}}
279 /*---------------------------------------------------------------------------*/
284 /*---------------------------------------------------------------------------*/
285 void sinfo_free_imagelist(cpl_imagelist **i)
286 {if(i){cpl_imagelist_delete(*i); *i = NULL;}}
287 /*---------------------------------------------------------------------------*/
292 /*---------------------------------------------------------------------------*/
293 void sinfo_free_table(cpl_table **t) {if(t){cpl_table_delete(*t); *t = NULL;}}
294 /*---------------------------------------------------------------------------*/
299 /*---------------------------------------------------------------------------*/
300 void sinfo_free_propertylist(cpl_propertylist **p)
301 {if(p){cpl_propertylist_delete(*p); *p = NULL;}}
302 /*---------------------------------------------------------------------------*/
307 /*---------------------------------------------------------------------------*/
308 void sinfo_free_polynomial(cpl_polynomial **p)
309 {if(p){cpl_polynomial_delete(*p); *p = NULL;}}
310 /*---------------------------------------------------------------------------*/
315 /*---------------------------------------------------------------------------*/
316 /* similar also present in svd.c */
317 void sinfoni_free_matrix(cpl_matrix **m)
318 {if(m){cpl_matrix_delete(*m); *m = NULL;}}
319 
320 /*---------------------------------------------------------------------------*/
325 /*---------------------------------------------------------------------------*/
326 /* TODO: not used */
327 void sinfo_free_parameterlist(cpl_parameterlist **p)
328 {if(p){cpl_parameterlist_delete(*p); *p = NULL;}}
329 /*----------------------------------------------------------------------------*/
334 /*---------------------------------------------------------------------------*/
335 void sinfo_free_frameset(cpl_frameset **f)
336 {if(f){cpl_frameset_delete(*f); *f = NULL;}}
337 
338 
339 /*---------------------------------------------------------------------------*/
344 /*---------------------------------------------------------------------------*/
345 void sinfo_free_frame(cpl_frame **f)
346 {if(f){cpl_frame_delete(*f); *f = NULL;}}
347 
348 
349 /*---------------------------------------------------------------------------*/
354 /*---------------------------------------------------------------------------*/
355 void sinfo_free_int(int **i) {if(i){cpl_free(*i); *i = NULL;}}
356 
357 
358 /*---------------------------------------------------------------------------*/
363 /*---------------------------------------------------------------------------*/
364 void sinfo_free_float(float **f) {if(f){cpl_free(*f); *f = NULL;}}
365 
366 
367 
368 /*---------------------------------------------------------------------------*/
373 /*---------------------------------------------------------------------------*/
374 void sinfo_free_double(double **d) {if(d){cpl_free(*d); *d = NULL;}}
375 
376 
377 /*---------------------------------------------------------------------------*/
382 /*---------------------------------------------------------------------------*/
383 void sinfo_free_array_imagelist(cpl_imagelist ***a)
384 {if(*a){cpl_free(*a); *a = NULL;}}
385 
386 /*---------------------------------------------------------------------------*/
391 /*---------------------------------------------------------------------------*/
392 void sinfo_free_array_image(cpl_image ***a) {if(*a){cpl_free(*a); *a = NULL;}}
393 
394 
395 /*---------------------------------------------------------------------------*/
401 /*---------------------------------------------------------------------------*/
402 void sinfo_free_image_array(cpl_image ***a, const int n)
403 {
404 
405  if((*a) != NULL) {
406  for (int i=0; i < n; i++) {
407  if((*a)[i] != NULL) {
408  sinfo_free_image(&(*a)[i]);
409  (*a)[i]=NULL;
410  }
411  }
412  sinfo_free_array_image(&(*a));
413  *a=NULL;
414  }
415 }
416 
417 /*---------------------------------------------------------------------------*/
423 /*---------------------------------------------------------------------------*/
424 void sinfo_free_float_array(float ***a, const int n)
425 {
426 
427  if((*a) != NULL) {
428  for (int i=0; i < n; i++) {
429  if((*a)[i] != NULL) {
430  sinfo_free_float(&(*a)[i]);
431  (*a)[i]=NULL;
432  }
433  }
434  cpl_free(*a);
435  *a=NULL;
436  }
437 }
438 
439 /*---------------------------------------------------------------------------*/
444 /*---------------------------------------------------------------------------*/
445 
446 void
447 sinfo_free_my_vector(cpl_vector **v) {if(v){cpl_vector_delete(*v);*v = NULL;}}
448 
449 
450 /*---------------------------------------------------------------------------*/
455 /*---------------------------------------------------------------------------*/
456 
457 void
458 sinfo_free_bivector(cpl_bivector **bv) {
459  if(bv){
460  cpl_bivector_delete(*bv);
461  *bv = NULL;
462  }
463 }
464 
465 /*---------------------------------------------------------------------------*/
470 /*---------------------------------------------------------------------------*/
471 /* TODO: not used */
472 void sinfo_free_stats(cpl_stats **s) {if(s){cpl_stats_delete(*s); *s = NULL;}}
473 /*---------------------------------------------------------------------------*/
478 /*---------------------------------------------------------------------------*/
479 void
480 sinfo_unwrap_vector(cpl_vector **v) {if(v){cpl_vector_unwrap(*v); *v = NULL;}}
481 
482 /*---------------------------------------------------------------------------*/
487 /*---------------------------------------------------------------------------*/
488 void sinfo_unwrap_matrix(cpl_matrix **m)
489 {if(m){cpl_matrix_unwrap(*m); *m = NULL;}}
490 
491 /*---------------------------------------------------------------------------*/
496 /*---------------------------------------------------------------------------*/
497 /* TODO: not used */
498 void sinfo_unwrap_bivector_vectors(cpl_bivector **b)
499 {if(b){cpl_bivector_unwrap_vectors(*b); *b = NULL;}}
500