DETMON Pipeline Reference Manual  1.2.7
irplib_match_cats.c
1 /* $Id: irplib_match_cats.c,v 1.10 2009-12-18 10:44:48 cgarcia Exp $
2  *
3  * This file is part of the irplib package
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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2009-12-18 10:44:48 $
24  * $Revision: 1.10 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 
37 #include <math.h>
38 #include <cpl.h>
39 #include <cpl_table.h>
40 
41 #include "irplib_match_cats.h"
42 
43 #define FILENAME_SZBUF 1024
44 
45 
46 /*----------------------------------------------------------------------------*/
47 /* Private functions
48  */
49 /*----------------------------------------------------------------------------*/
50 
51 cpl_error_code irplib_match_cats_get_all_matching_pairs
52 (cpl_table ** catalogues,
53  int ncats,
54  cpl_table * matching_sets,
55  int (*binary_match_condition)
56  (cpl_table * catalogue1,
57  cpl_table * catalogue2,
58  int iobj1,
59  int iobj2) );
60 
61 cpl_error_code irplib_match_cats_get_all_matches_cresc
62 (cpl_table ** catalogues,
63  cpl_array * cat_index_begin,
64  cpl_array * cats_idx_set,
65  int mincat_match,
66  cpl_table * matching_sets);
67 
68 cpl_error_code irplib_match_cats_iterate_on_cat
69 (cpl_table ** catalogues,
70  cpl_array * cats_idx_set,
71  int icat_iterate,
72  cpl_array * valid_iobjs,
73  int mincat_match,
74  cpl_table * matching_sets,
75  cpl_table * less_minmatch_sets);
76 
77 cpl_error_code irplib_match_cats_filter_obj_to_iter
78 (cpl_array * cats_idx_set,
79  int order_begin,
80  cpl_table * matches_set,
81  cpl_array * excluded_objs,
82  int itercat_nobj);
83 
84 int irplib_match_cats_match_condition
85 (cpl_table ** catalogues,
86  int * cats_idx_set_ptr,
87  int ncats);
88 
89 int irplib_match_count_nonmatched
90 (int * cats_idx_set_ptr,
91  int ncats);
92 
93 int nCombinations;
94 int nFilter;
95 
96 /*----------------------------------------------------------------------------*/
100 /*----------------------------------------------------------------------------*/
101 
104 /*---------------------------------------------------------------------------*/
124 /*---------------------------------------------------------------------------*/
125 cpl_table * irplib_match_cat_pairs
126 (cpl_table ** catalogues,
127  int ncats,
128  int (*binary_match_condition)
129  (cpl_table * catalogue1,
130  cpl_table * catalogue2,
131  int iobj1,
132  int iobj2) )
133 {
134  cpl_table * matching_sets;
135 
136  //Initialize the solution
137  matching_sets = cpl_table_new(0);
138  cpl_table_new_column_array(matching_sets, "MATCHING_SETS",
139  CPL_TYPE_INT, ncats);
140 
141  irplib_match_cats_get_all_matching_pairs
142  (catalogues, ncats, matching_sets, binary_match_condition);
143 
144  return matching_sets;
145 }
146 
147 cpl_error_code irplib_match_cats_get_all_matching_pairs
148 (cpl_table ** catalogues,
149  int ncats,
150  cpl_table * matching_sets,
151  int (*binary_match_condition)
152  (cpl_table * catalogue1,
153  cpl_table * catalogue2,
154  int iobj1,
155  int iobj2) )
156 {
157  int icat1;
158  int icat2;
159 
160  nCombinations = 0;
161  nFilter = 0;
162 
163  for(icat1 = 0; icat1 < ncats ; ++icat1)
164  for(icat2 = icat1 + 1 ; icat2 < ncats ; ++icat2)
165  {
166  int iobj1;
167  int iobj2;
168  int nobj1;
169  int nobj2;
170 
171  nobj1 = cpl_table_get_nrow(catalogues[icat1]);
172  nobj2 = cpl_table_get_nrow(catalogues[icat2]);
173 
174  for(iobj1 = 0; iobj1 < nobj1 ; ++iobj1)
175  for(iobj2 = 0 ; iobj2 < nobj2 ; ++iobj2)
176  {
177  ++nCombinations;
178  if(binary_match_condition(catalogues[icat1],
179  catalogues[icat2],
180  iobj1, iobj2))
181  {
182  cpl_array * cats_idx_set;
183  int icat;
184 
185  ++nFilter;
186  cats_idx_set = cpl_array_new(ncats, CPL_TYPE_INT);
187  for(icat = 0; icat < ncats; ++icat)
188  {
189  if(icat == icat1)
190  cpl_array_set_int(cats_idx_set, icat, iobj1);
191  else if(icat == icat2)
192  cpl_array_set_int(cats_idx_set, icat, iobj2);
193  else
194  cpl_array_set_int(cats_idx_set, icat, -1);
195  }
196 
197  cpl_table_set_size(matching_sets,
198  cpl_table_get_nrow(matching_sets)+1);
199  cpl_table_set_array(matching_sets,"MATCHING_SETS",
200  cpl_table_get_nrow(matching_sets)-1,
201  cats_idx_set);
202  cpl_array_delete(cats_idx_set);
203  }
204  }
205  }
206 
207  return CPL_ERROR_NONE;
208 }
209