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) {
657 cpl_propertylist *plist1 = NULL;
660 if (cpl_propertylist_is_empty(plist0)) {
664 plist0 = cpl_propertylist_duplicate(plist0);
671 cpl_propertylist_erase(plist0,
"BSCALE");
672 cpl_propertylist_erase(plist0,
"BZERO");
673 cpl_propertylist_erase(plist0,
"BUNIT");
675 cpl_propertylist_erase(plist0,
"DATAMIN");
676 cpl_propertylist_erase(plist0,
"DATAMAX");
683 cpl_propertylist_erase_regexp(plist0,
"^CRPIX[0-9]$", 0);
684 cpl_propertylist_erase_regexp(plist0,
"^CRVAL[0-9]$", 0);
685 cpl_propertylist_erase_regexp(plist0,
"^CDELT[0-9]$", 0);
686 cpl_propertylist_erase_regexp(plist0,
"^CTYPE[0-9]$", 0);
694 plist1 = cpl_propertylist_duplicate(plist0);
702 cpl_propertylist_erase(plist0,
"EXTNAME");
704 cpl_propertylist_erase(plist1,
"DATAMD5");
705 cpl_propertylist_erase(plist1,
"INHERIT");
706 cpl_propertylist_erase(plist1,
"PIPEFILE");
707 cpl_propertylist_erase(plist1, GIALIAS_ANCESTOR);
714 code = cpl_table_save(table, plist0, plist1, filename, CPL_IO_CREATE);
716 if (code != CPL_ERROR_NONE) {
717 cpl_propertylist_delete(plist0);
720 cpl_propertylist_delete(plist1);
726 cpl_propertylist_delete(plist0);
729 cpl_propertylist_delete(plist1);
763 cpl_table *table = NULL;
764 cpl_propertylist *plist = NULL;
767 cx_assert(
self != NULL);
769 if (filename == NULL) {
780 plist = cpl_propertylist_duplicate(plist);
787 cpl_propertylist_erase(plist,
"BSCALE");
788 cpl_propertylist_erase(plist,
"BZERO");
789 cpl_propertylist_erase(plist,
"BUNIT");
791 cpl_propertylist_erase(plist,
"DATAMIN");
792 cpl_propertylist_erase(plist,
"DATAMAX");
793 cpl_propertylist_erase(plist,
"DATAMD5");
794 cpl_propertylist_erase(plist,
"INHERIT");
795 cpl_propertylist_erase(plist,
"PIPEFILE");
796 cpl_propertylist_erase(plist, GIALIAS_ANCESTOR);
802 cpl_propertylist_erase_regexp(plist,
"^CRPIX[0-9]$", 0);
803 cpl_propertylist_erase_regexp(plist,
"^CRVAL[0-9]$", 0);
804 cpl_propertylist_erase_regexp(plist,
"^CDELT[0-9]$", 0);
805 cpl_propertylist_erase_regexp(plist,
"^CTYPE[0-9]$", 0);
809 cpl_propertylist_update_string(plist, GIALIAS_EXTNAME,
id);
810 cpl_propertylist_set_comment(plist, GIALIAS_EXTNAME,
811 "FITS Extension name");
814 if (cpl_propertylist_is_empty(plist)) {
819 code = cpl_table_save(table, NULL, plist, filename, CPL_IO_EXTEND);
821 if (code != CPL_ERROR_NONE) {
822 cpl_propertylist_delete(plist);
828 cpl_propertylist_delete(plist);
845 const cpl_frameset *set)
850 cpl_propertylist *properties = NULL;
859 if (properties == NULL) {
cxint giraffe_table_add_info(GiTable *table, const GiRecipeInfo *info, const cpl_frameset *set)
Add additional frame information to a table.
cxint giraffe_table_is_empty(GiTable *self)
Check whether a Giraffe table is empty.
cxint giraffe_add_frameset_info(cpl_propertylist *plist, const cpl_frameset *set, cxint sequence)
Add frameset specific information to a property list.
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
cxint giraffe_table_set(GiTable *self, cpl_table *table)
Sets the table data.
void giraffe_table_delete(GiTable *self)
Destroys a Giraffe table.
cxint giraffe_table_attach(GiTable *self, const cxchar *filename, cxint position, const cxchar *id)
Attach a Giraffe table to a file.
GiTable * giraffe_table_new(void)
Creates a new, empty Giraffe table.
void giraffe_table_clear(GiTable *self)
Clears a Giraffe table.
cxint giraffe_table_set_properties(GiTable *self, cpl_propertylist *properties)
Attaches a property list to an table.
cxint giraffe_table_load(GiTable *self, const cxchar *filename, cxint position, const cxchar *id)
Reads a data set from a file into a Giraffe table.
GiTable * giraffe_table_duplicate(const GiTable *src)
Duplicate a Giraffe table.
cxint giraffe_table_save(GiTable *self, const cxchar *filename)
Write a Giraffe table to a file.
cxint giraffe_table_copy_matrix(GiTable *table, const cxchar *name, cpl_matrix *matrix)
Copies matrix elements into a table.
cxint giraffe_add_recipe_info(cpl_propertylist *plist, const GiRecipeInfo *info)
Add recipe specific information to a property list.
cpl_propertylist * giraffe_table_get_properties(const GiTable *self)
Gets the table properties.
GiTable * giraffe_table_create(cpl_table *table, cpl_propertylist *properties)
Creates a Giraffe table from a table and a property list.