SINFONI Pipeline Reference Manual  2.5.2
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 void sinfo_free_parameter(cpl_parameter **p)
231 {if(p){cpl_parameter_delete(*p); *p = NULL;}}
232 
233 
234 
239 /*---------------------------------------------------------------------------*/
240 void sinfo_free_apertures(cpl_apertures **a)
241 {if(a){cpl_apertures_delete(*a); *a = NULL;}}
242 /*---------------------------------------------------------------------------*/
243 
244 /*---------------------------------------------------------------------------*/
249 /*---------------------------------------------------------------------------*/
250 void sinfo_free_image(cpl_image **i) {if(i){cpl_image_delete(*i); *i = NULL;}}
251 
252 
253 /*---------------------------------------------------------------------------*/
258 /*---------------------------------------------------------------------------*/
259 void sinfoni_free_vector(cpl_vector **v) {if(v){cpl_vector_delete(*v); *v = NULL;}}
260 
261 
262 /*---------------------------------------------------------------------------*/
267 /*---------------------------------------------------------------------------*/
268 void sinfo_free_array(cpl_array **i) {if(i){cpl_array_delete(*i); *i = NULL;}}
269 
270 /*---------------------------------------------------------------------------*/
275 /*---------------------------------------------------------------------------*/
276 void sinfo_free_mask(cpl_mask **m) {if(m){cpl_mask_delete(*m); *m = NULL;}}
277 /*---------------------------------------------------------------------------*/
282 /*---------------------------------------------------------------------------*/
283 void sinfo_free_imagelist(cpl_imagelist **i)
284 {if(i){cpl_imagelist_delete(*i); *i = NULL;}}
285 /*---------------------------------------------------------------------------*/
290 /*---------------------------------------------------------------------------*/
291 void sinfo_free_table(cpl_table **t) {if(t){cpl_table_delete(*t); *t = NULL;}}
292 /*---------------------------------------------------------------------------*/
297 /*---------------------------------------------------------------------------*/
298 void sinfo_free_propertylist(cpl_propertylist **p)
299 {if(p){cpl_propertylist_delete(*p); *p = NULL;}}
300 /*---------------------------------------------------------------------------*/
305 /*---------------------------------------------------------------------------*/
306 void sinfo_free_polynomial(cpl_polynomial **p)
307 {if(p){cpl_polynomial_delete(*p); *p = NULL;}}
308 /*---------------------------------------------------------------------------*/
313 /*---------------------------------------------------------------------------*/
314 /* similar also present in svd.c */
315 void sinfoni_free_matrix(cpl_matrix **m)
316 {if(m){cpl_matrix_delete(*m); *m = NULL;}}
317 
318 /*---------------------------------------------------------------------------*/
323 /*---------------------------------------------------------------------------*/
324 void sinfo_free_parameterlist(cpl_parameterlist **p)
325 {if(p){cpl_parameterlist_delete(*p); *p = NULL;}}
326 /*----------------------------------------------------------------------------*/
331 /*---------------------------------------------------------------------------*/
332 void sinfo_free_frameset(cpl_frameset **f)
333 {if(f){cpl_frameset_delete(*f); *f = NULL;}}
334 
335 
336 /*---------------------------------------------------------------------------*/
341 /*---------------------------------------------------------------------------*/
342 void sinfo_free_frame(cpl_frame **f)
343 {if(f){cpl_frame_delete(*f); *f = NULL;}}
344 
345 
346 /*---------------------------------------------------------------------------*/
351 /*---------------------------------------------------------------------------*/
352 void sinfo_free_int(int **i) {if(i){cpl_free(*i); *i = NULL;}}
353 
354 
355 /*---------------------------------------------------------------------------*/
360 /*---------------------------------------------------------------------------*/
361 void sinfo_free_float(float **f) {if(f){cpl_free(*f); *f = NULL;}}
362 
363 
364 
365 /*---------------------------------------------------------------------------*/
370 /*---------------------------------------------------------------------------*/
371 void sinfo_free_double(double **d) {if(d){cpl_free(*d); *d = NULL;}}
372 
373 
374 /*---------------------------------------------------------------------------*/
379 /*---------------------------------------------------------------------------*/
380 void sinfo_free_array_imagelist(cpl_imagelist ***a)
381 {if(*a){cpl_free(*a); *a = NULL;}}
382 
383 /*---------------------------------------------------------------------------*/
388 /*---------------------------------------------------------------------------*/
389 void sinfo_free_array_image(cpl_image ***a) {if(*a){cpl_free(*a); *a = NULL;}}
390 
391 
392 /*---------------------------------------------------------------------------*/
398 /*---------------------------------------------------------------------------*/
399 void sinfo_free_image_array(cpl_image ***a, const int n)
400 {
401  int i=0;
402  if((*a) != NULL) {
403  for (i=0; i < n; i++) {
404  if((*a)[i] != NULL) {
405  sinfo_free_image(&(*a)[i]);
406  (*a)[i]=NULL;
407  }
408  }
409  sinfo_free_array_image(&(*a));
410  *a=NULL;
411  }
412 }
413 
414 /*---------------------------------------------------------------------------*/
420 /*---------------------------------------------------------------------------*/
421 void sinfo_free_float_array(float ***a, const int n)
422 {
423  int i=0;
424  if((*a) != NULL) {
425  for (i=0; i < n; i++) {
426  if((*a)[i] != NULL) {
427  sinfo_free_float(&(*a)[i]);
428  (*a)[i]=NULL;
429  }
430  }
431  cpl_free(*a);
432  *a=NULL;
433  }
434 }
435 
436 /*---------------------------------------------------------------------------*/
441 /*---------------------------------------------------------------------------*/
442 
443 void
444 sinfo_free_my_vector(cpl_vector **v) {if(v){cpl_vector_delete(*v);*v = NULL;}}
445 
446 
447 /*---------------------------------------------------------------------------*/
452 /*---------------------------------------------------------------------------*/
453 
454 void
455 sinfo_free_bivector(cpl_bivector **bv) {
456  if(bv){
457  cpl_bivector_delete(*bv);
458  *bv = NULL;
459  }
460 }
461 
462 /*---------------------------------------------------------------------------*/
467 /*---------------------------------------------------------------------------*/
468 void sinfo_free_stats(cpl_stats **s) {if(s){cpl_stats_delete(*s); *s = NULL;}}
469 /*---------------------------------------------------------------------------*/
474 /*---------------------------------------------------------------------------*/
475 void
476 sinfo_unwrap_vector(cpl_vector **v) {if(v){cpl_vector_unwrap(*v); *v = NULL;}}
477 
478 /*---------------------------------------------------------------------------*/
483 /*---------------------------------------------------------------------------*/
484 void sinfo_unwrap_matrix(cpl_matrix **m)
485 {if(m){cpl_matrix_unwrap(*m); *m = NULL;}}
486 
487 /*---------------------------------------------------------------------------*/
492 /*---------------------------------------------------------------------------*/
493 void sinfo_unwrap_bivector_vectors(cpl_bivector **b)
494 {if(b){cpl_bivector_unwrap_vectors(*b); *b = NULL;}}
495