35 #include <cxmessages.h>
38 #include <cpl_error.h>
39 #include <cpl_array.h>
55 cpl_propertylist *properties;
64 _giraffe_table_clear(GiTable *
self)
68 cpl_table_delete(self->data);
72 if (self->properties) {
73 cpl_propertylist_delete(self->properties);
74 self->properties = NULL;
96 GiTable *
self = cx_calloc(1,
sizeof(GiTable));
99 self->properties = NULL;
126 GiTable *
self = NULL;
131 self->data = cpl_table_duplicate(table);
137 self->properties = cpl_propertylist_duplicate(properties);
138 if (!self->properties) {
166 _giraffe_table_clear(
self);
187 GiTable *
self = NULL;
195 self = cx_calloc(1,
sizeof(GiTable));
198 self->properties = cpl_propertylist_duplicate(properties);
202 self->data = cpl_table_duplicate(data);
232 _giraffe_table_clear(
self);
271 const cxchar *fctid =
"giraffe_table_copy_matrix";
277 cpl_array *labels = NULL;
278 cpl_table *_table = NULL;
281 cx_assert(table != NULL);
287 nrow = cpl_matrix_get_nrow(matrix);
288 ncol = cpl_matrix_get_ncol(matrix);
289 cx_assert(nrow > 0 && ncol > 0);
293 labels = cpl_table_get_column_names(_table);
294 cx_assert(cpl_array_get_size(labels) > 0);
299 if (cpl_table_has_column(_table, name) == 0) {
301 cpl_array_delete(labels);
302 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
315 while (strcmp(cpl_array_get_string(labels, count), name) != 0) {
324 if (cpl_table_get_nrow(_table) != nrow ||
325 cpl_table_get_ncol(_table) - count < ncol) {
327 cpl_array_delete(labels);
328 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
337 cxdouble *elements = cpl_matrix_get_data(matrix);
340 for (i = 0; i < ncol; ++i) {
342 const cxchar *label = cpl_array_get_string(labels, count + i);
344 cpl_type type = cpl_table_get_column_type(_table, label);
352 for (j = 0; j < nrow; ++j) {
353 cpl_table_set_int(_table, label, j,
354 (cxint) elements[j * ncol + i]);
363 for (j = 0; j < nrow; ++j) {
364 cpl_table_set_float(_table, label, j,
365 (cxfloat) elements[j * ncol + i]);
370 case CPL_TYPE_DOUBLE:
374 for (j = 0; j < nrow; ++j) {
375 cpl_table_set_double(_table, label, j,
376 elements[j * ncol + i]);
381 case CPL_TYPE_STRING:
384 cpl_array_delete(labels);
385 cpl_error_set(fctid, CPL_ERROR_INVALID_TYPE);
396 cpl_array_delete(labels);
423 return self->properties == NULL ? 1 : 0;
444 cx_assert(
self != NULL);
467 cpl_table *tmp = NULL;
471 cx_assert(table != NULL);
474 cpl_table_delete(tmp);
477 self->data = cpl_table_duplicate(table);
500 cx_assert(
self != NULL);
502 return self->properties;
531 if (self->properties) {
532 cpl_propertylist_delete(self->properties);
533 self->properties = NULL;
536 self->properties = cpl_propertylist_duplicate(properties);
538 return self->properties ? 0 : 1;
574 const cxchar *fctid =
"giraffe_table_load";
577 if (!
self || !filename) {
581 self->data = cpl_table_load((cxchar *)filename, position, 0);
583 if (cpl_error_get_code() == CPL_ERROR_NULL_INPUT) {
584 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
591 self->properties = cpl_propertylist_load(filename, position);
593 if (self->properties == NULL) {
594 _giraffe_table_clear(
self);
601 if (self->properties &&
602 cpl_propertylist_has(self->properties, GIALIAS_EXTNAME)) {
606 magic = cpl_propertylist_get_string(self->properties,
609 if (strcmp(
id, magic) == 0) {
616 _giraffe_table_clear(
self);
617 cpl_error_set(fctid, CPL_ERROR_BAD_FILE_FORMAT);
647 if (filename == NULL) {
659 if (cpl_propertylist_is_empty(plist)) {
663 plist = cpl_propertylist_duplicate(plist);
670 cpl_propertylist_erase_regexp(plist,
"^CRPIX[0-9]$", 0);
671 cpl_propertylist_erase_regexp(plist,
"^CRVAL[0-9]$", 0);
672 cpl_propertylist_erase_regexp(plist,
"^CDELT[0-9]$", 0);
673 cpl_propertylist_erase_regexp(plist,
"^CTYPE[0-9]$", 0);
676 code = cpl_table_save(table, plist, plist, filename, CPL_IO_CREATE);
678 if (code != CPL_ERROR_NONE) {
679 cpl_propertylist_delete(plist);
685 cpl_propertylist_delete(plist);
719 cpl_table *table = NULL;
720 cpl_propertylist *plist = NULL;
723 cx_assert(
self != NULL);
725 if (filename == NULL) {
736 plist = cpl_propertylist_duplicate(plist);
743 cpl_propertylist_erase_regexp(plist,
"^CRPIX[0-9]$", 0);
744 cpl_propertylist_erase_regexp(plist,
"^CRVAL[0-9]$", 0);
745 cpl_propertylist_erase_regexp(plist,
"^CDELT[0-9]$", 0);
746 cpl_propertylist_erase_regexp(plist,
"^CTYPE[0-9]$", 0);
750 cpl_propertylist_update_string(plist, GIALIAS_EXTNAME,
id);
751 cpl_propertylist_set_comment(plist, GIALIAS_EXTNAME,
752 "FITS Extension name");
755 if (cpl_propertylist_is_empty(plist)) {
760 code = cpl_table_save(table, NULL, plist, filename, CPL_IO_EXTEND);
762 if (code != CPL_ERROR_NONE) {
763 cpl_propertylist_delete(plist);
769 cpl_propertylist_delete(plist);
786 const cpl_frameset *
set)
791 cpl_propertylist *properties = NULL;
800 if (properties == NULL) {