Changeset 7538 for trunk/psLib/src/fits/psFitsTable.c
- Timestamp:
- Jun 13, 2006, 12:05:23 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/fits/psFitsTable.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/fits/psFitsTable.c
r7465 r7538 7 7 * @author Robert DeSonia, MHPCC 8 8 * 9 * @version $Revision: 1.1 2$ $Name: not supported by cvs2svn $10 * @date $Date: 2006-06-1 0 00:07:10$9 * @version $Revision: 1.13 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2006-06-13 22:05:23 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 605 605 606 606 bool psFitsWriteTable(psFits* fits, 607 psMetadata* header,607 const psMetadata* header, 608 608 const psArray* table, 609 609 const char *extname) … … 611 611 psFitsMoveLast(fits); 612 612 return psFitsInsertTable(fits, header, table, extname, true); 613 614 #if 0 615 613 } 614 615 bool psFitsInsertTable(psFits* fits, 616 const psMetadata* header, 617 const psArray* table, 618 const char *extname, 619 bool after) 620 { 616 621 int status = 0; 617 622 … … 698 703 699 704 //create list of column names and types. 705 if (columns->n > 0) { // If we're going decrement columns->n (why?), at least free the memory 706 psFree(columns->data[columns->n - 1]); 707 } 700 708 columns->n--; 701 709 psArray* columnNames = psArrayAlloc(columns->n); … … 718 726 } 719 727 720 // Create the table 721 fits_create_tbl(fits->fd, 722 BINARY_TBL, 723 table->n, // number of rows in table 724 columns->n, // number of columns in table 725 (char**)columnNames->data, // names of the columns 726 (char**)columnTypes->data, // format of the columns 727 NULL, // physical unit of columns 728 extname, // extension name 729 &status); 728 // Create the table HDU 729 int hdus = psFitsGetSize(fits); // Number of HDUs in file 730 if (hdus == 0) { 731 // We're creating the first extension 732 fits_create_tbl(fits->fd, 733 BINARY_TBL, 734 table->n, // number of rows in table 735 columns->n, // number of columns in table 736 (char**)columnNames->data, // names of the columns 737 (char**)columnTypes->data, // format of the columns 738 NULL, // physical unit of columns 739 (char*)extname, // extension name; casting away const because cfitsio is horrible 740 &status); 741 } else { 742 if (!after) { 743 if (psFitsGetExtNum(fits) == 0) { 744 // We're creating a replacement primary HDU. 745 // Set status to signal fits_insert_img to insert a new primary HDU 746 status = PREPEND_PRIMARY; 747 } else { 748 // Move back one to perform an insert after the previous HDU 749 psFitsMoveExtNum(fits, -1, true); 750 } 751 } 752 // Insert the table 753 fits_insert_btbl(fits->fd, 754 table->n, // number of rows in table 755 columns->n, // number of columns in table 756 (char**)columnNames->data, // names of the columns 757 (char**)columnTypes->data, // format of the columns 758 NULL, // physical unit of columns 759 (char*)extname, // extension name; casting away const because cfitsio is horrible 760 0, 761 &status); 762 } 730 763 psFree(columnNames); 731 764 psFree(columnTypes); … … 816 849 817 850 return true; 818 #endif819 }820 821 bool psFitsInsertTable(psFits* fits,822 psMetadata* header,823 const psArray* table,824 const char *extname,825 bool after)826 {827 int status = 0;828 829 if (fits == NULL) {830 psError(PS_ERR_BAD_PARAMETER_NULL, true,831 PS_ERRORTEXT_psFits_NULL);832 return false;833 }834 835 if (table == NULL) {836 psError(PS_ERR_BAD_PARAMETER_NULL, true,837 PS_ERRORTEXT_psFits_IMAGE_NULL);838 return false;839 }840 841 int rows = table->n;842 if (rows < 1) {843 // no table data, what can I do?844 psError(PS_ERR_BAD_PARAMETER_SIZE, true,845 PS_ERRORTEXT_psFits_TABLE_EMPTY);846 return false;847 }848 849 // find all the columns needed850 psArray* columns = psArrayAlloc(((psMetadata*)table->data[0])->list->n);851 columns->n=0;852 853 // find the unique items in the array of metadata 'rows'854 psMetadataItem* item;855 for (int row=0; row < rows; row++) {856 psMetadata* rowMeta = table->data[row];857 if (rowMeta != NULL) {858 psListIterator* iter = psListIteratorAlloc(rowMeta->list,859 PS_LIST_HEAD,true);860 while ( (item=psListGetAndIncrement(iter)) != NULL) {861 if (PS_DATA_IS_PRIMITIVE(item->type) ||862 item->type == PS_DATA_STRING ||863 item->type == PS_DATA_VECTOR) {864 bool found = false;865 psMetadataItem* fItem = NULL;866 int n;867 for (n=0; n < columns->n && ! found; n++) {868 fItem = (psMetadataItem*)(columns->data[n]);869 if (strcmp(item->name, fItem->name) == 0) {870 found = true;871 break;872 }873 }874 if (! found) {875 psArrayAdd(columns, columns->nalloc, item);876 } else if (item->type == PS_DATA_STRING &&877 strlen(fItem->data.V) < strlen(item->data.V)) {878 // got to keep the longest string value as to know what size to create the table column879 psMemDecrRefCounter(fItem);880 columns->data[n] = psMemIncrRefCounter(item);881 columns->n++;882 883 } else if (item->type == PS_DATA_VECTOR &&884 ((psVector*)(fItem->data.V))->n < ((psVector*)(item->data.V))->n) {885 psMemDecrRefCounter(fItem);886 columns->data[n] = psMemIncrRefCounter(item);887 columns->n++;888 }889 } else {890 // unsupported type -- treating as an error891 psError(PS_ERR_BAD_PARAMETER_TYPE, true,892 "Unsupported data type (%d) for Metadata Item '%s' in row %d.",893 item->type, item->name, row);894 psFree(iter);895 psFree(columns);896 return false;897 }898 }899 psFree(iter);900 }901 }902 903 if (columns->n == 0) { // no table columns found904 psError(PS_ERR_BAD_PARAMETER_SIZE, true,905 "Did not find any column data to write to a table.");906 psFree(columns);907 return false;908 }909 910 //create list of column names and types.911 if (columns->n > 0) { // If we're going decrement columns->n (why?), at least free the memory912 psFree(columns->data[columns->n - 1]);913 }914 columns->n--;915 psArray* columnNames = psArrayAlloc(columns->n);916 psArray* columnTypes = psArrayAlloc(columns->n);917 for (int n=0; n < columns->n; n++) {918 columnNames->data[n] = psMemIncrRefCounter(((psMetadataItem*)columns->data[n])->name);919 columnNames->n++;920 columnTypes->n++;921 if ( ! getMetadataTForm((psMetadataItem*)columns->data[n],922 (char**)&(columnTypes->data[n]),1) ) {923 psError(PS_ERR_UNKNOWN, true,924 "Failed to determine the FITS data type of '%s' (type=%d).",925 columnNames->data[n],926 ((psMetadataItem*)columns->data[n])->type);927 psFree(columns);928 psFree(columnNames);929 psFree(columnTypes);930 return false;931 }932 }933 934 // Create the table HDU935 int hdus = psFitsGetSize(fits); // Number of HDUs in file936 if (hdus == 0) {937 // We're creating the first extension938 fits_create_tbl(fits->fd,939 BINARY_TBL,940 table->n, // number of rows in table941 columns->n, // number of columns in table942 (char**)columnNames->data, // names of the columns943 (char**)columnTypes->data, // format of the columns944 NULL, // physical unit of columns945 (char*)extname, // extension name; casting away const because cfitsio is horrible946 &status);947 } else {948 if (!after) {949 if (psFitsGetExtNum(fits) == 0) {950 // We're creating a replacement primary HDU.951 // Set status to signal fits_insert_img to insert a new primary HDU952 status = PREPEND_PRIMARY;953 } else {954 // Move back one to perform an insert after the previous HDU955 psFitsMoveExtNum(fits, -1, true);956 }957 }958 // Insert the table959 fits_insert_btbl(fits->fd,960 table->n, // number of rows in table961 columns->n, // number of columns in table962 (char**)columnNames->data, // names of the columns963 (char**)columnTypes->data, // format of the columns964 NULL, // physical unit of columns965 (char*)extname, // extension name; casting away const because cfitsio is horrible966 0,967 &status);968 }969 psFree(columnNames);970 psFree(columnTypes);971 972 // Write header973 if (header && !psFitsWriteHeader(fits, header)) {974 psError(PS_ERR_IO, false, "Unable to write FITS header.\n");975 return false;976 }977 978 // fill in the table elements with data979 for (int n = 0; n < columns->n; n++) {980 int row;981 item = columns->data[n];982 if (PS_DATA_IS_PRIMITIVE(item->type) ) {983 psVector* col = psVectorAlloc(table->n, item->type);984 int dataSize = PSELEMTYPE_SIZEOF(item->type);985 int dataType;986 convertPsTypeToFits(item->type, NULL,NULL,&dataType);987 for (row = 0; row < table->n; row++) {988 psMetadataItem* dataItem = psMetadataLookup(989 table->data[row],990 item->name);991 memcpy(&col->data.U8[row*dataSize],&dataItem->data,dataSize);992 }993 fits_write_col(fits->fd,994 dataType,995 n+1, // column number996 1, // firstrow997 1, // firstelem998 table->n, // nelements999 col->data.U8,1000 &status);1001 psFree(col);1002 } else if (item->type == PS_DATA_STRING) {1003 psArray* colArray = psArrayAlloc(table->n);1004 for (row = 0; row < table->n; row++) {1005 colArray->data[row] = psStringCopy(psMetadataLookupStr(NULL,1006 table->data[row],1007 item->name));1008 colArray->n++;1009 }1010 fits_write_col_str(fits->fd,1011 n+1,1012 1,1013 1,1014 table->n,1015 (char**)colArray->data,1016 &status);1017 psFree(colArray);1018 } else if (item->type == PS_DATA_VECTOR) {1019 psVector* vec = (psVector*)(item->data.V);1020 psVector* col = psVectorAlloc(table->n*vec->n, vec->type.type);1021 int dataSize = PSELEMTYPE_SIZEOF(vec->type.type)*vec->n;1022 int dataType;1023 convertPsTypeToFits(vec->type.type, NULL,NULL,&dataType);1024 for (row = 0; row < table->n; row++) {1025 psMetadataItem* dataItem = psMetadataLookup(1026 table->data[row],1027 item->name);1028 memcpy(&col->data.U8[row*dataSize],1029 ((psVector*)(dataItem->data.V))->data.U8,1030 dataSize);1031 }1032 fits_write_col(fits->fd,1033 dataType,1034 n+1, // column number1035 1, // firstrow1036 1, // firstelem1037 table->n*vec->n, // nelements1038 col->data.U8,1039 &status);1040 psFree(col);1041 }1042 if (status != 0) {1043 char fitsErr[MAX_STRING_LENGTH];1044 (void)fits_get_errstatus(status, fitsErr);1045 psError(PS_ERR_IO, true,1046 PS_ERRORTEXT_psFits_WRITE_FAILED,1047 fitsErr);1048 psFree(columns);1049 return false;1050 }1051 1052 }1053 1054 psFree(columns);1055 1056 return true;1057 851 } 1058 852
Note:
See TracChangeset
for help on using the changeset viewer.
