GIRAFFE Pipeline Reference Manual

giwlresiduals.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2006 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 02110-1301 USA
19  */
20 
21 /*
22  * $Author$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include <cxmap.h>
33 #include <cxstring.h>
34 #include <cxstrutils.h>
35 
36 #include <cpl_error.h>
37 #include <cpl_propertylist.h>
38 
39 #include "gialias.h"
40 #include "gitable.h"
41 #include "gierror.h"
42 #include "gichebyshev.h"
43 #include "giwlresiduals.h"
44 
45 
54 struct GiWlResidualData {
55 
56  cxint ssn;
57  GiChebyshev2D *fit;
58 
59 };
60 
61 typedef struct GiWlResidualData GiWlResidualData;
62 
63 
64 struct GiWlResiduals {
65 
66  cx_map *data;
67 
68 };
69 
70 
71 inline static cxint
72 _giraffe_compare_int(cxcptr first, cxcptr second)
73 {
74 
75  cxint *_first = (cxint *)first;
76  cxint *_second = (cxint *)second;
77 
78  return *_first - *_second;
79 
80 }
81 
82 
83 inline static void
84 _giraffe_wlresidualdata_delete(GiWlResidualData *self)
85 {
86 
87  if (self) {
88 
89  if (self->fit) {
90  giraffe_chebyshev2d_delete(self->fit);
91  }
92 
93  cx_free(self);
94 
95  }
96 
97  return;
98 
99 }
100 
101 
102 inline static GiWlResidualData *
103 _giraffe_wlresiduals_get(const GiWlResiduals *self, cxsize idx)
104 {
105 
106  cx_map_const_iterator position;
107 
108  GiWlResidualData *data = NULL;
109 
110 
111  position = cx_map_begin(self->data);
112 
113  if (position == cx_map_end(self->data)) {
114  return NULL;
115  }
116 
117  if (idx > 0) {
118 
119  cxsize i;
120 
121  for (i = 1; i < idx; i++) {
122  position = cx_map_next(self->data, position);
123  }
124 
125  }
126 
127  data = cx_map_get_value(self->data, position);
128 
129  return data;
130 
131 }
132 
133 
134 inline static void
135 _giraffe_wlresiduals_insert(GiWlResiduals *self, cxint ssn,
136  const GiChebyshev2D *fit)
137 {
138 
139  GiWlResidualData *data = cx_calloc(1, sizeof *data);
140 
141 
142  data->ssn = ssn;
143  data->fit = (GiChebyshev2D *)fit;
144 
145  cx_map_insert(self->data, &data->ssn, data);
146 
147  return;
148 
149 }
150 
151 
152 GiWlResiduals *
153 giraffe_wlresiduals_new(void)
154 {
155 
156  GiWlResiduals *self = cx_calloc(1, sizeof *self);
157 
158  if (self) {
159 
160  self->data = cx_map_new(_giraffe_compare_int, NULL,
161  (cx_free_func)_giraffe_wlresidualdata_delete);
162  cx_assert(cx_map_empty(self->data));
163 
164  }
165 
166  return self;
167 
168 }
169 
170 
171 GiWlResiduals *
172 giraffe_wlresiduals_clone(const GiWlResiduals *other)
173 {
174 
175  GiWlResiduals *self = NULL;
176 
177 
178  if (other != NULL) {
179 
180  self = giraffe_wlresiduals_new();
181 
182  if (!cx_map_empty(other->data)) {
183 
184  cx_map_const_iterator position = cx_map_begin(other->data);
185 
186 
187  while (position != cx_map_end(other->data)) {
188 
189  GiWlResidualData *data = cx_map_get_value(other->data,
190  position);
191 
192  GiChebyshev2D *fit = giraffe_chebyshev2d_clone(data->fit);
193 
194 
195  _giraffe_wlresiduals_insert(self, data->ssn, fit);
196 
197  position = cx_map_next(other->data, position);
198 
199  }
200 
201  }
202 
203  cx_assert(cx_map_size(self->data) == cx_map_size(other->data));
204 
205  }
206 
207  return self;
208 
209 }
210 
211 
212 GiWlResiduals *
213 giraffe_wlresiduals_create(GiTable *wlsolution)
214 {
215 
216  const cxchar *const fctid = "giraffe_wlresiduals_create";
217 
218 
219  cpl_propertylist *properties = NULL;
220 
221  cpl_table *residuals = NULL;
222 
223  GiWlResiduals *self = giraffe_wlresiduals_new();
224 
225 
226  if (wlsolution == NULL) {
227 
228  cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
229  giraffe_wlresiduals_delete(self);
230 
231  return NULL;
232 
233  }
234 
235  properties = giraffe_table_get_properties(wlsolution);
236 
237  if (properties == NULL) {
238 
239  cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
240  giraffe_wlresiduals_delete(self);
241 
242  return NULL;
243  }
244 
245  residuals = giraffe_table_get(wlsolution);
246 
247  if (residuals == NULL) {
248 
249  cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
250  giraffe_wlresiduals_delete(self);
251 
252  return NULL;
253  }
254 
255  if (!cpl_table_has_column(residuals, "XMIN") ||
256  !cpl_table_has_column(residuals, "XMAX") ||
257  !cpl_table_has_column(residuals, "YMIN") ||
258  !cpl_table_has_column(residuals, "YMAX")) {
259 
260 // cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
261 // giraffe_wlresiduals_delete(self);
262 
263 // return NULL;
264 
265  cpl_table_new_column(residuals, "XMIN", CPL_TYPE_DOUBLE);
266  cpl_table_new_column(residuals, "XMAX", CPL_TYPE_DOUBLE);
267  cpl_table_new_column(residuals, "YMIN", CPL_TYPE_DOUBLE);
268  cpl_table_new_column(residuals, "YMAX", CPL_TYPE_DOUBLE);
269 
270  cpl_table_set_double(residuals, "XMIN", 0, 0.);
271  cpl_table_set_double(residuals, "XMAX", 0, 4096.);
272  cpl_table_set_double(residuals, "YMIN", 0, 0.);
273  cpl_table_set_double(residuals, "YMAX", 0, 2048.);
274 
275  }
276 
277 // else {
278 
279  if (1) {
280 
281  cxint i;
282  cxint xorder = 0;
283  cxint yorder = 0;
284  cxint nc = 0;
285 
286  cx_string *label = NULL;
287 
288  cpl_matrix *coeffs = NULL;
289 
290 
291  if (!cpl_propertylist_has(properties, GIALIAS_WSOL_XRORDER)) {
292 
293  cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
294  giraffe_wlresiduals_delete(self);
295 
296  return NULL;
297 
298  }
299  else {
300 
301 
302  const cxchar *s =
303  cpl_propertylist_get_string(properties, GIALIAS_WSOL_XRORDER);
304 
305  cxchar **orders = cx_strsplit(s, ":", 3);
306 
307  if (orders[1] == NULL) {
308 
309  cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
310  giraffe_wlresiduals_delete(self);
311  cx_strfreev(orders);
312 
313  return NULL;
314  }
315 
316  xorder = strtol(orders[0], NULL, 10);
317  yorder = strtol(orders[1], NULL, 10);
318 
319  cx_strfreev(orders);
320 
321  }
322 
323 
324  nc = (xorder + 1) * (yorder + 1);
325 
326  label = cx_string_new();
327  coeffs = cpl_matrix_new(xorder + 1, yorder + 1);
328 
329 
330  for (i = 0; i < cpl_table_get_nrow(residuals); i++) {
331 
332  cxint j;
333  cxint l = 0;
334  cxint ssn = cpl_table_get_int(residuals, "SSN", i, NULL);
335 
336  cxdouble ax = cpl_table_get(residuals, "XMIN", i, NULL);
337  cxdouble bx = cpl_table_get(residuals, "XMAX", i, NULL);
338  cxdouble ay = cpl_table_get(residuals, "YMIN", i, NULL);
339  cxdouble by = cpl_table_get(residuals, "YMAX", i, NULL);
340 
341  GiChebyshev2D *fit = NULL;
342 
343 
344  for (j = 0; j <= xorder; j++) {
345 
346  cxint k;
347 
348  for (k = 0; k <= yorder; k++) {
349 
350  cxdouble coeff = 0.;
351 
352  cx_string_sprintf(label, "XC%-d", l);
353  coeff = cpl_table_get(residuals, cx_string_get(label),
354  i, NULL);
355 
356  cpl_matrix_set(coeffs, j, k, coeff);
357  ++l;
358 
359  }
360 
361  }
362 
363  fit = giraffe_chebyshev2d_new(xorder, yorder);
364  giraffe_chebyshev2d_set(fit, ax, bx, ay, by, coeffs);
365 
366  _giraffe_wlresiduals_insert(self, ssn, fit);
367 
368  }
369 
370  cpl_matrix_delete(coeffs);
371  cx_string_delete(label);
372 
373  }
374 
375  return self;
376 
377 }
378 
379 
380 void
381 giraffe_wlresiduals_delete(GiWlResiduals *self)
382 {
383 
384  if (self) {
385 
386  if (self->data != NULL) {
387  cx_map_delete(self->data);
388  }
389 
390  cx_free(self);
391 
392  }
393 
394  return;
395 
396 }
397 
398 
399 cxsize
400 giraffe_wlresiduals_get_size(const GiWlResiduals *self)
401 {
402 
403  cx_assert(self != NULL);
404 
405  return cx_map_size(self->data);
406 
407 }
408 
409 
410 cxint
411 giraffe_wlresiduals_get_subslit(const GiWlResiduals *self, cxsize idx)
412 {
413 
414  const GiWlResidualData *data = NULL;
415 
416 
417  cx_assert(self != NULL);
418 
419  data = _giraffe_wlresiduals_get(self, idx);
420 
421  if (data == NULL) {
422  return -1;
423  }
424 
425  return data->ssn;
426 
427 }
428 
429 
430 GiChebyshev2D *
431 giraffe_wlresiduals_get_element(const GiWlResiduals *self, cxsize idx)
432 {
433 
434  const GiWlResidualData *data = NULL;
435 
436 
437  cx_assert(self != NULL);
438 
439  data = _giraffe_wlresiduals_get(self, idx);
440 
441  if (data == NULL) {
442  return NULL;
443  }
444 
445  return data->fit;
446 
447 }
448 
449 
450 cxint
451 giraffe_wlresiduals_set(GiWlResiduals *self, cxint ssn,
452  const GiChebyshev2D *residuals)
453 {
454 
455  cx_assert(self != NULL);
456 
457  if (residuals == NULL) {
458  return 1;
459  }
460 
461  cx_map_erase(self->data, &ssn);
462  _giraffe_wlresiduals_insert(self, ssn , residuals);
463 
464  return 0;
465 
466 }
467 
468 GiChebyshev2D *
469 giraffe_wlresiduals_get(const GiWlResiduals *self, cxint ssn)
470 {
471 
472  const GiWlResidualData *data = NULL;
473 
474 
475  cx_assert(self != NULL);
476 
477  data = cx_map_get(self->data, &ssn);
478  return data->fit;
479 
480 }
481 
482 
483 cpl_table *
484 giraffe_wlresiduals_table(const GiWlResiduals *self)
485 {
486 
487  const cxchar *label = "SSN";
488 
489  cxint i = 0;
490  cxint xorder = 0;
491  cxint yorder = 0;
492  cxint ncoeff = 0;
493 
494  cx_string *s = NULL;
495 
496  cx_map_const_iterator position;
497 
498  cpl_propertylist *sorting_order = NULL;
499 
500  cpl_table *residuals = NULL;
501 
502  const GiWlResidualData *data = NULL;
503 
504 
505  cx_assert(self != NULL);
506 
507  if (cx_map_empty(self->data)) {
508  return NULL;
509  }
510 
511  position = cx_map_begin(self->data);
512 
513  data = cx_map_get_value(self->data, position);
514  cx_assert(data != NULL);
515 
516  giraffe_chebyshev2d_get_order(data->fit, &xorder, &yorder);
517  ncoeff = (xorder + 1) * (yorder + 1);
518 
519 
520  residuals = cpl_table_new(cx_map_size(self->data));
521 
522  s = cx_string_new();
523 
524  giraffe_error_push();
525 
526  cpl_table_new_column(residuals, label, CPL_TYPE_INT);
527 
528  cpl_table_new_column(residuals, "XMIN", CPL_TYPE_DOUBLE);
529  cpl_table_new_column(residuals, "XMAX", CPL_TYPE_DOUBLE);
530  cpl_table_new_column(residuals, "YMIN", CPL_TYPE_DOUBLE);
531  cpl_table_new_column(residuals, "YMAX", CPL_TYPE_DOUBLE);
532 
533 
534  for (i = 0; i < ncoeff; i++) {
535 
536  cx_string_sprintf(s, "XC%-d", i);
537  cpl_table_new_column(residuals, cx_string_get(s), CPL_TYPE_DOUBLE);
538 
539  }
540 
541  if (cpl_error_get_code() != CPL_ERROR_NONE) {
542 
543  cpl_table_delete(residuals);
544  cx_string_delete(s);
545 
546  return NULL;
547  }
548 
549  giraffe_error_pop();
550 
551 
552  i = 0;
553 
554  while (position != cx_map_end(self->data)) {
555 
556  cxint ssn = 0;
557 
558  cxsize j;
559  cxsize l = 0;
560  cxsize nx = 0;
561  cxsize ny = 0;
562 
563  cxdouble ax = 0.;
564  cxdouble bx = 0.;
565  cxdouble ay = 0.;
566  cxdouble by = 0.;
567 
568  cpl_matrix *_coeffs = NULL;
569 
570  const GiChebyshev2D *fit = NULL;
571 
572 
573  data = cx_map_get_value(self->data, position);
574 
575  ssn = data->ssn;
576 
577  fit = data->fit;
578  cx_assert(fit != NULL);
579 
580  _coeffs = (cpl_matrix *)giraffe_chebyshev2d_coeffs(fit);
581  giraffe_chebyshev2d_get_range(fit, &ax, &bx, &ay, &by);
582 
583  cpl_table_set_int(residuals, label, i , ssn);
584 
585  cpl_table_set_double(residuals, "XMIN", i, ax);
586  cpl_table_set_double(residuals, "XMAX", i, bx);
587  cpl_table_set_double(residuals, "YMIN", i, ay);
588  cpl_table_set_double(residuals, "YMAX", i, by);
589 
590  nx = cpl_matrix_get_nrow(_coeffs);
591  ny = cpl_matrix_get_ncol(_coeffs);
592 
593  cx_assert(nx * ny == (cxsize)((xorder + 1) * (yorder + 1)));
594 
595  for (j = 0; (cxsize)j < nx; j++) {
596 
597  cxint k;
598 
599  for (k = 0; (cxsize)k < ny; k++) {
600 
601  cxdouble value = cpl_matrix_get(_coeffs, j, k);
602 
603  cx_string_sprintf(s, "XC%-" CX_PRINTF_FORMAT_SIZE_TYPE, l);
604  cpl_table_set_double(residuals, cx_string_get(s), i, value);
605  ++l;
606 
607  }
608 
609  }
610 
611  position = cx_map_next(self->data, position);
612  ++i;
613 
614  }
615 
616  cx_string_delete(s);
617  s = NULL;
618 
619  sorting_order = cpl_propertylist_new();
620  cpl_propertylist_append_bool(sorting_order, label, 0);
621 
622  cpl_table_sort(residuals, sorting_order);
623 
624  cpl_propertylist_delete(sorting_order);
625  sorting_order = NULL;
626 
627  return residuals;
628 
629 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.12.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Mar 24 2014 11:43:53 by doxygen 1.8.2 written by Dimitri van Heesch, © 1997-2004