IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 22, 2011, 4:11:23 PM (15 years ago)
Author:
rhenders
Message:

Using new Fits class and Config class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/src/Config.c

    r30147 r31011  
    1 /** @file ippToPspsConfig.c
     1/** @file Config.c
    22 *
    33 *  @ingroup ippToPsps
     
    77 */
    88
    9 #include "ippToPspsConfig.h"
     9#include "Config.h"
    1010#include <ctype.h>
    1111#include <libxml/parser.h>
    1212#include <libxml/tree.h>
    1313
    14 // Gets PS type from string
     14#include "Fits.h"
     15
     16/**
     17   Gets PS type from string
     18   */
    1519static int ippToPsps_GetDataType(char *typename) {
    1620
     
    2832}
    2933
    30 // finds a column within this table
    31 static IppToPspsConfig_Column* ippToPspsConfig_findColumn(IppToPspsConfig_Table* table, const char* name) {
    32 
    33     IppToPspsConfig_Column* column = NULL;
     34/**
     35  Finds a column within this table
     36  */
     37static Column* findColumn(Table* table, const char* name) {
     38
     39    Column* column = NULL;
    3440
    3541    for (uint32_t i=0; i<table->numOfColumns; i++) {
     
    4652}
    4753
    48 // finds a table from the array of tables
    49 static IppToPspsConfig_Table* ippToPspsConfig_findTable(IppToPspsConfig* this, const char* name) {
    50 
    51     IppToPspsConfig_Table* table = NULL;
     54/**
     55  Finds a table from the array of tables
     56  */
     57static Table* findTable(Config* this, const char* name) {
     58
     59    Table* table = NULL;
    5260
    5361    for (uint32_t i=0; i<this->numOfTables; i++) {
     
    6472}
    6573
    66 // gets an attribute value from XML node
    67 static bool ippToPspsConfig_getAttribute(xmlNode* node, const char* attName, char* _value) {
     74/**
     75  Gets an attribute value from XML node
     76  */
     77static bool getAttribute(xmlNode* node, const char* attName, char* _value) {
    6878
    6979    xmlChar* value = xmlGetProp(node, (const xmlChar*)attName);
     
    7585}
    7686
    77 // creates a FITS binary table and populates it with defaults based on definition in schema XML
    78 static bool ippToPspsConfig_createTable(IppToPspsConfig_Table* table, fitsfile* fitsOut, const long nRows) {
     87/**
     88  Creates a FITS binary table and populates it with defaults based on definition in schema XML
     89  */
     90static bool createTable(Table* table, Fits* fitsOut, const long nRows) {
    7991
    8092    if (!table) return false;
     
    98110        switch (table->columns[i].pspsType) {
    99111
    100             case TBYTE:
    101                 sprintf(colTypes[i],"1B");
    102                 break;
    103             case TSHORT:
    104                 sprintf(colTypes[i],"1I");
    105                 break;
    106             case TLONG:
    107                 sprintf(colTypes[i],"1J"); // TODO
    108                 break;
    109             case TLONGLONG:
    110                 sprintf(colTypes[i],"1K");
    111                 break;
    112             case TFLOAT:
    113                 sprintf(colTypes[i],"1E");
    114                 break;
    115             case TDOUBLE:
    116                 sprintf(colTypes[i],"1D");
    117                 break;
    118             case TSTRING:
    119                 sprintf(colTypes[i],"32A"); // TODO width?
    120                 break;
     112            case TBYTE: sprintf(colTypes[i],"1B");
     113                        break;
     114            case TSHORT: sprintf(colTypes[i],"1I");
     115                         break;
     116            case TLONG: sprintf(colTypes[i],"1J"); // TODO
     117                        break;
     118            case TLONGLONG: sprintf(colTypes[i],"1K");
     119                            break;
     120            case TFLOAT: sprintf(colTypes[i],"1E");
     121                         break;
     122            case TDOUBLE: sprintf(colTypes[i],"1D");
     123                          break;
     124            case TSTRING: sprintf(colTypes[i],"32A"); // TODO width?
     125                          break;
    121126
    122127            default:
    123                 break;
     128                          break;
    124129        }
    125130
     
    127132    }
    128133
    129     int status = 0;
    130     if (fits_create_tbl(fitsOut, BINARY_TBL, nRows, nCols, colNames, colTypes, NULL, table->name, &status)) {
    131 
    132         fits_report_error(stderr, status);
    133         psError(PS_ERR_IO,"Unable to create table: %s", table->name);
    134     }
     134    fitsOut->createBinaryTable(fitsOut, nRows, nCols, colNames, colTypes, table->name);
    135135
    136136    for (i=0;i<nCols;i++) free(colTypes[i]);
     
    166166            case TBYTE:
    167167                for(j=0;j<nRows;j++) int8col[j] = (int8_t)tempLong;
    168                 fits_write_col(fitsOut, TBYTE, col, 1, 1, nRows, int8col, &status);
     168                fitsOut->writeColumn(fitsOut, TBYTE, col, 1, 1, nRows, int8col);
    169169                break;
    170170            case TSHORT:
    171171                for(j=0;j<nRows;j++) int16col[j] = (int16_t)tempLong;
    172                 fits_write_col(fitsOut, TSHORT, col, 1, 1, nRows, int16col, &status);
     172                fitsOut->writeColumn(fitsOut, TSHORT, col, 1, 1, nRows, int16col);
    173173                break;
    174174            case TLONG:
    175175                for(j=0;j<nRows;j++) longcol[j] = (long)tempLong;
    176                 fits_write_col(fitsOut, TLONG, col, 1, 1, nRows, longcol, &status);
     176                fitsOut->writeColumn(fitsOut, TLONG, col, 1, 1, nRows, longcol);
    177177                break;
    178178
    179179            case TLONGLONG:
    180180                for(j=0;j<nRows;j++) longcol[j] = tempLong;
    181                 fits_write_col(fitsOut, TLONGLONG, col, 1, 1, nRows, longcol, &status);
     181                fitsOut->writeColumn(fitsOut, TLONGLONG, col, 1, 1, nRows, longcol);
    182182                break;
    183183
    184184            case TFLOAT:
    185185                for(j=0;j<nRows;j++) floatcol[j] = (float)tempDouble;
    186                 fits_write_col(fitsOut, TFLOAT, col, 1, 1, nRows, floatcol, &status);
     186                fitsOut->writeColumn(fitsOut, TFLOAT, col, 1, 1, nRows, floatcol);
    187187                break;
    188188
    189189            case TDOUBLE:
    190190                for(j=0;j<nRows;j++) doublecol[j] = tempDouble;
    191                 fits_write_col(fitsOut, TDOUBLE, col, 1, 1, nRows, doublecol, &status);
     191                fitsOut->writeColumn(fitsOut, TDOUBLE, col, 1, 1, nRows, doublecol);
    192192                break;
    193193
    194194            case TSTRING:
    195195                for(j=0;j<nRows;j++) strcpy(strcol[j], tempStr);
    196                 fits_write_col(fitsOut, TSTRING, col, 1, 1, nRows, strcol, &status);
     196                fitsOut->writeColumn(fitsOut, TSTRING, col, 1, 1, nRows, strcol);
    197197                break;
    198198
     
    214214}
    215215
    216 // count number of children with the provided name
    217 static int ippToPspsConfig_countChildren(xmlNode* rootNode, const char* name) {
     216/*
     217   Counts number of children with the provided name
     218   */
     219static int countChildren(xmlNode* rootNode, const char* name) {
    218220
    219221    int count = 0;
     
    228230}
    229231
    230 // searches through this table and finds attribute value for a provided key
    231 static bool ippToPspsConfig_getRowAttribute(IppToPspsConfig* this, xmlNode* tableNode,
     232/**
     233  Searches through this table and finds attribute value for a provided key
     234  */
     235static bool getRowAttribute(Config* this, xmlNode* tableNode,
    232236        const char* keyName, const char* keyValue,
    233237        const char* attName, char* attValue) {
     
    243247
    244248            if (strcmp((const char*)node->name, "row")==0) {
    245             //psLogMsg("ippToPsps", PS_LOG_INFO, " Looking for key '%s'",  keyName );
    246 
    247                 if (!ippToPspsConfig_getAttribute(node, keyName, buffer)) continue;
    248             //psLogMsg("ippToPsps", PS_LOG_INFO, "Found key '%s' value '%s'",  keyName, buffer );
     249                //psLogMsg("ippToPsps", PS_LOG_INFO, " Looking for key '%s'",  keyName );
     250
     251                if (!getAttribute(node, keyName, buffer)) continue;
     252                //psLogMsg("ippToPsps", PS_LOG_INFO, "Found key '%s' value '%s'",  keyName, buffer );
    249253
    250254                if (strcmp(buffer, keyValue) != 0) continue;
    251                 ippToPspsConfig_getAttribute(node, attName, attValue);
     255                getAttribute(node, attName, attValue);
    252256                //printf("FOUND %s %s %s %s \n", keyName, buffer, attName, attValue );
    253257                return true;
     
    261265}
    262266
    263 // opens an XML file and returns the document
    264 static xmlDoc* ippToPspsConfig_openXmlFile(const char* path) {
     267/*
     268   opens an XML file and returns the document
     269   */
     270static xmlDoc* openXmlFile(const char* path) {
    265271
    266272    xmlDoc* doc = xmlReadFile(path, NULL, 0);
     
    269275}
    270276
    271 // closes an XML file
    272 static bool ippToPspsConfig_closeXmlFile(xmlDoc* doc) {
     277/*
     278   closes an XML file
     279   */
     280static bool closeXmlFile(xmlDoc* doc) {
    273281
    274282    xmlFreeDoc(doc);
     
    278286}
    279287
    280 // gets filter ID for this filter name from 'init' data
    281 static bool ippToPspsConfig_getInitValue(
    282         IppToPspsConfig* this,
     288/**
     289  Gets a value from the 'init' data
     290  */
     291static bool getInitValue(
     292        Config* this,
    283293        const char* tableName,
    284294        const char* keyName, const char* keyValue,
     
    290300    psStringAppend(&path, "%s/../init/data.xml", this->configsPath); // TODO nasty
    291301
    292     xmlDoc* doc = ippToPspsConfig_openXmlFile(path);
     302    xmlDoc* doc = openXmlFile(path);
    293303
    294304    if (doc == NULL) {
     
    313323
    314324            if (strcmp((const char*)node->name, "table")!=0) continue;
    315             if (!ippToPspsConfig_getAttribute(node, "name", tempStr)) continue;
     325            if (!getAttribute(node, "name", tempStr)) continue;
    316326            if (strcmp(tempStr, tableName)!=0) continue;
    317327
    318             ret = ippToPspsConfig_getRowAttribute(this, node, keyName, keyValue, attName, attValue);
     328            ret = getRowAttribute(this, node, keyName, keyValue, attName, attValue);
    319329            break;
    320330        }
     
    322332    }
    323333
    324     ippToPspsConfig_closeXmlFile(doc);
     334    closeXmlFile(doc);
    325335
    326336    return ret;
    327337}
    328338
    329 // gets survey ID from survey name
    330 bool ippToPspsConfig_getSurveyId(IppToPspsConfig* this, const char* surveyType, int8_t* surveyId) {
     339/*
     340   Gets survey ID from survey name
     341   */
     342static bool getSurveyId(Config* this, const char* surveyType, int8_t* surveyId) {
    331343
    332344    char buffer[10];
    333     if (!ippToPspsConfig_getInitValue(this, "Survey", "name", surveyType, "surveyID", buffer)) {
     345    if (!getInitValue(this, "Survey", "name", surveyType, "surveyID", buffer)) {
    334346
    335347        *surveyId = -1;
     
    341353}
    342354
    343 // gets filter ID from filter type
    344 bool ippToPspsConfig_getFilterId(IppToPspsConfig* this, const char* filterType, int8_t* filterId) {
     355/*
     356   Gets filter ID from filter type
     357   */
     358static bool getFilterId(Config* this, const char* filterType, int8_t* filterId) {
    345359
    346360    char tmp[2];
     
    348362    tmp[1] = '\0';
    349363    char buffer[10];
    350     if (!ippToPspsConfig_getInitValue(this, "Filter", "filterType", tmp, "filterID", buffer)) {
     364    if (!getInitValue(this, "Filter", "filterType", tmp, "filterID", buffer)) {
    351365
    352366        *filterId = -1;
     
    358372}
    359373
    360 // populate the provided table with data from XML
    361 static bool ippToPspsConfig_populateTable(IppToPspsConfig_Table* table, xmlNode* rootElement, fitsfile *fitsOut) {
     374/**
     375  Populates the provided table with data from XML
     376  */
     377static bool populateTable(Table* table, xmlNode* rootElement, Fits *fitsOut) {
    362378
    363379    char tempStr[100];
     
    370386
    371387            if (strcmp((const char*)node->name, "table")!=0) continue;
    372             if (!ippToPspsConfig_getAttribute(node, "name", tempStr)) continue;
     388            if (!getAttribute(node, "name", tempStr)) continue;
    373389            if (strcmp(tempStr, table->name)==0) {tableNode = node; break;}
    374390        }
     
    382398
    383399    // now count the number of rows
    384     int nRows = ippToPspsConfig_countChildren(tableNode, "row");
     400    int nRows = countChildren(tableNode, "row");
    385401    if (nRows < 1) return false;
    386     if (!ippToPspsConfig_createTable(table, fitsOut, nRows)) return false;
     402    if (!createTable(table, fitsOut, nRows)) return false;
    387403
    388404    int rows=0;
     
    411427            col = i+1;
    412428
    413             if (!ippToPspsConfig_getAttribute(node, table->columns[i].pspsName, tempStr)) continue;
     429            if (!getAttribute(node, table->columns[i].pspsName, tempStr)) continue;
    414430
    415431            strncpy(strcol[0], tempStr, 32);
     
    420436                case TBYTE:
    421437                    bytecol[0] = atoi(tempStr);
    422                     fits_write_col(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, bytecol, &status);
     438                    fitsOut->writeColumn(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, bytecol);
    423439                    break;
    424440                case TSHORT:
    425441                    shortcol[0] = atoi(tempStr);
    426                     fits_write_col(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, shortcol, &status);
     442                    fitsOut->writeColumn(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, shortcol);
    427443                    break;
    428444                case TLONG:
    429445                case TLONGLONG:
    430446                    longcol[0] = atol(tempStr);
    431                     fits_write_col(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, longcol, &status);
     447                    fitsOut->writeColumn(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, longcol);
    432448                    break;
    433449                case TFLOAT:
    434450                    floatcol[0] = atof(tempStr);
    435                     fits_write_col(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, floatcol, &status);
     451                    fitsOut->writeColumn(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, floatcol);
    436452                    break;
    437453                case TDOUBLE:
    438454                    doublecol[0] = atof(tempStr);
    439                     fits_write_col(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, doublecol, &status);
     455                    fitsOut->writeColumn(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, doublecol);
    440456                    break;
    441457                case TSTRING:
    442                     fits_write_col(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, strcol, &status);
     458                    fitsOut->writeColumn(fitsOut, table->columns[i].pspsType, col, rows, 1, 1, strcol);
    443459                    break;
    444460
     
    460476}
    461477
    462 // loads a table description from XML
    463 static bool ippToPspsConfig_loadTableDescription(IppToPspsConfig_Table* table, xmlNode* tableNode) {
     478/**
     479  Loads a table description from XML
     480  */
     481static bool loadTableDescription(Table* table, xmlNode* tableNode) {
    464482
    465483    bool ret = true;
    466484    char tableName[100];
    467     ippToPspsConfig_getAttribute(tableNode, "name", tableName);
     485    getAttribute(tableNode, "name", tableName);
    468486    strcpy(table->name, tableName);
    469487
    470488    // count how many columns we have coming in and allocate memory for them
    471     table->numOfColumns = ippToPspsConfig_countChildren(tableNode, "column");
    472     table->columns = calloc(table->numOfColumns, sizeof(IppToPspsConfig_Column));
     489    table->numOfColumns = countChildren(tableNode, "column");
     490    table->columns = calloc(table->numOfColumns, sizeof(Column));
    473491    //    psLogMsg("ippToPsps", PS_LOG_INFO, " Found '%d' columns", table->numOfColumns);
    474492
     
    484502
    485503        // column name
    486         ippToPspsConfig_getAttribute(node, "name", buffer);
     504        getAttribute(node, "name", buffer);
    487505        sprintf(table->columns[columnNum].pspsName, buffer);
    488506
    489507        // column type
    490         ippToPspsConfig_getAttribute(node, "type", buffer);
     508        getAttribute(node, "type", buffer);
    491509        table->columns[columnNum].pspsType = ippToPsps_GetDataType(buffer);
    492510
    493511        // default value
    494         ippToPspsConfig_getAttribute(node, "default", buffer);
     512        getAttribute(node, "default", buffer);
    495513        sprintf(table->columns[columnNum].defaultValue, buffer);
    496514
    497515        // comment
    498         ippToPspsConfig_getAttribute(node, "comment", buffer);
     516        getAttribute(node, "comment", buffer);
    499517        sprintf(table->columns[columnNum].comment, buffer);
    500518
     
    523541}
    524542
    525 // read table shapes from XML file
    526 bool ippToPspsConfig_loadTableDescriptions(IppToPspsConfig* this) {
     543/**
     544  Reads table shapes from XML file
     545  */
     546static bool loadTableDescriptions(Config* this) {
    527547
    528548    bool ret = true;
     
    531551    psStringAppend(&path, "%s/tables.xml", this->configsPath);
    532552
    533     xmlDoc* doc = ippToPspsConfig_openXmlFile(path);
     553    xmlDoc* doc = openXmlFile(path);
    534554
    535555    if (doc == NULL) {
     
    549569
    550570    // count how many tables we have coming in and allocate memory for them
    551     this->numOfTables = ippToPspsConfig_countChildren(rootElement, "table");
    552     this->tables = calloc(this->numOfTables, sizeof(IppToPspsConfig_Table));
     571    this->numOfTables = countChildren(rootElement, "table");
     572    this->tables = calloc(this->numOfTables, sizeof(Table));
    553573    //    psLogMsg("ippToPsps", PS_LOG_INFO, " Found '%d' tables", this->numOfTables);
    554574
    555575    xmlNode* node = NULL;
    556     IppToPspsConfig_Table* table = NULL;
     576    Table* table = NULL;
    557577
    558578    int tableNum = 0;
     
    565585                table = this->tables+tableNum;
    566586
    567                 if (!ippToPspsConfig_loadTableDescription(table, node)) ret = false;
     587                if (!loadTableDescription(table, node)) ret = false;
    568588                else tableNum++;
    569589            }
     
    574594        psError(PS_ERR_IO, false, "Mismatch between number of tables expected (%d) and those found (%d)", this->numOfTables, tableNum);
    575595
    576     ippToPspsConfig_closeXmlFile(doc);
     596    closeXmlFile(doc);
    577597
    578598    return ret;
    579599}
    580600
    581 // loads mappings from XML for a particular table
    582 static bool ippToPspsConfig_loadTableMappings(IppToPspsConfig* this, xmlNode* tableNode) {
     601/**
     602  Loads mappings from XML for a particular table
     603  */
     604static bool loadTableMappings(Config* this, xmlNode* tableNode) {
    583605
    584606    bool ret = false;
     
    587609
    588610    char tableName[100];
    589     ippToPspsConfig_getAttribute(tableNode, "name", tableName);
    590 
    591     IppToPspsConfig_Table* table = ippToPspsConfig_findTable(this, tableName);
    592     IppToPspsConfig_Column* column = NULL;
     611    getAttribute(tableNode, "name", tableName);
     612
     613    Table* table = findTable(this, tableName);
     614    Column* column = NULL;
    593615    if (!table) return false;
    594616
     
    603625
    604626                // column type
    605                 ippToPspsConfig_getAttribute(node, "pspsName", buffer);
    606                 column = ippToPspsConfig_findColumn(table, buffer);
     627                getAttribute(node, "pspsName", buffer);
     628                column = findColumn(table, buffer);
    607629
    608630                if (!column) continue;
    609631
    610632                // IPP name
    611                 ippToPspsConfig_getAttribute(node, "ippName", buffer);
     633                getAttribute(node, "ippName", buffer);
    612634                strcpy(column->ippName, buffer);
    613635
    614636                // IPP type
    615                 ippToPspsConfig_getAttribute(node, "ippType", buffer);
     637                getAttribute(node, "ippType", buffer);
    616638                column->ippType = ippToPsps_GetDataType(buffer);
    617639
     
    626648}
    627649
    628 // reads table mappings from XML file
    629 static bool ippToPspsConfig_loadMappings(IppToPspsConfig* this) {
     650/**
     651  Reads table mappings from XML file
     652  */
     653static bool loadMappings(Config* this) {
    630654
    631655    bool ret = true;
     
    634658    psStringAppend(&path, "%s/map.xml", this->configsPath);
    635659
    636     xmlDoc* doc = ippToPspsConfig_openXmlFile(path);
     660    xmlDoc* doc = openXmlFile(path);
    637661
    638662    if (doc == NULL) {
     
    657681            if (strcmp((const char*)node->name, "table")==0) {
    658682
    659                 if (!ippToPspsConfig_loadTableMappings(this, node)) ret = false;
     683                if (!loadTableMappings(this, node)) ret = false;
    660684            }
    661685        }
    662686    }
    663687
    664     ippToPspsConfig_closeXmlFile(doc);
     688    closeXmlFile(doc);
    665689
    666690    return ret;
    667691}
    668692
    669 // populate the provided table with data from XML
    670 bool ippToPspsConfig_populateFromFile(IppToPspsConfig* this, fitsfile *fitsOut) {
     693/**
     694  Populate the provided table with data from XML
     695  */
     696static bool populateFromFile(Config* this, Fits *fitsOut) {
    671697
    672698    bool ret = true;
     
    674700    psStringAppend(&path, "%s/data.xml", this->configsPath);
    675701
    676     xmlDoc* doc = ippToPspsConfig_openXmlFile(path);
     702    xmlDoc* doc = openXmlFile(path);
    677703
    678704    if (doc == NULL) {
     
    690716
    691717    for (uint32_t i=0; i<this->numOfTables; i++) {
    692         if (!ippToPspsConfig_populateTable(this->tables+i, rootElement, fitsOut)) {
     718        if (!populateTable(this->tables+i, rootElement, fitsOut)) {
    693719            ret = false;
    694720        }
    695721    }
    696722
    697     ippToPspsConfig_closeXmlFile(doc);
     723    closeXmlFile(doc);
    698724
    699725    return ret;
    700726}
    701727
    702 // gets the contents of a FITS column vector
    703 bool ippToPspsConfig_getColumnVector(
    704         int col,
    705         long row,
    706         int type,
    707         int repeat,
    708         float* vector,
    709         fitsfile *fitsIn) {
    710 
    711     int status = 0;
    712     int anynull = 0; 
    713 
    714     fits_read_col(fitsIn, type, col, row, 1, repeat, NULL, vector, &anynull, &status);
    715 
    716     if (status) {
    717         psError(PS_ERR_IO, false, "Failed to read vector column %d row %ld", col, row);
    718         return false;
    719     }
    720 
    721     int i;
    722     for(i=0; i<repeat; i++) printf("JJJ %f\n", vector[i]);
    723 
    724     return true;
    725 }
    726 
    727 // gets metadata about a column
    728 bool ippToPspsConfig_getFitsColumnMeta(
    729         char* name,
    730         int* colNum,
    731         int* type,
    732         long* repeat,
    733         fitsfile *fitsIn) {
    734 
    735     int status = 0;
    736     fits_get_colnum(fitsIn, CASESEN, name, colNum, &status);
    737     if (status) {
    738         psError(PS_ERR_IO, false, "Unable to read col '%s'", name);
    739         return false;
    740     }
    741 
    742     status = 0;
    743     fits_get_eqcoltype(fitsIn, *colNum, type, repeat, NULL, &status);
    744     if (status) {
    745         psError(PS_ERR_IO, false, "Unable to read type info for '%s'", name);
    746         return false;
    747     }
    748 
    749     return true;
    750 }
    751 
    752 // populate with data from another FITS table into this one
    753 static bool ippToPspsConfig_populateTableFromFits(
    754         IppToPspsConfig_Table* table,
    755         fitsfile *fitsIn,
    756         fitsfile *fitsOut,
     728/**
     729  Populate with data from another FITS table into this one
     730  */
     731static bool populateTableFromFits(
     732        Table* table,
     733        Fits *fitsIn,
     734        Fits *fitsOut,
    757735        const long nRows,
    758736        const bool fromHeader) {
     
    770748        strcol[i] = (char*)calloc(50,sizeof(char)); // TODO 20? size issue
    771749
    772     int8_t int8null = -99;
    773     int16_t int16null = -999;
    774     long longnull = -999;
    775     float floatnull = -999.0;
    776     double doublenull = -999.0;
    777 
    778     int anynull = 0;
    779     int readStatus = 0;
    780     int writeStatus = 0;
    781 
    782750    // first loop round all columns and get IPP col numbers for provided column names TODO only do once, first time in
    783751    if(!fromHeader) {
     
    787755            if (strlen(table->columns[i].ippName) < 1) continue;
    788756
    789 int dummy;
    790 
    791             if (!ippToPspsConfig_getFitsColumnMeta(
    792                         table->columns[i].ippName,
    793                         &table->columns[i].ippColNum,
    794           //              &table->columns[i].ippType,  TODO getting wrong type for some reason
    795                         &dummy,
    796                         &table->columns[i].ippRepeat,
    797                         fitsIn)) {return false;}
     757            int dummy; // TODO getting wrong type for some reason
     758            if (!fitsIn->getColumnMeta(fitsIn, table->columns[i].ippName, &table->columns[i].ippColNum, &dummy, &table->columns[i].ippRepeat)) return false;
    798759        }
    799760    }
     
    802763    for (uint32_t i=0; i<table->numOfColumns; i++) {
    803764
    804         readStatus = 0;
    805         writeStatus = 0;
    806 
    807765        if (!table->columns[i].usingDefault) {
    808766
     
    813771                case TBYTE:
    814772                    if (fromHeader) {
    815                         fits_read_key(fitsIn, TBYTE, table->columns[i].ippName, &int8col[0], NULL, &readStatus);
    816                         if (!isfinite(int8col[0])) int8col[0] = int8null;
     773                        fitsIn->getHeaderKeyValue(fitsIn, TBYTE, table->columns[i].ippName, &int8col[0]);
    817774                    }
    818                     else fits_read_col(fitsIn, TBYTE, table->columns[i].ippColNum, 1, 1, nRows, &int8null, int8col, &anynull, &readStatus);
    819                     fits_write_col(fitsOut, TBYTE, col, 1, 1, nRows, int8col, &writeStatus);
     775                    else fitsIn->readColumn(fitsIn, TBYTE, table->columns[i].ippColNum, 1, 1, nRows, int8col);
     776                    fitsOut->writeColumn(fitsOut, TBYTE, col, 1, 1, nRows, int8col);
    820777                    break;
    821778                case TSHORT:
    822779                    if (fromHeader) {
    823                         fits_read_key(fitsIn, TSHORT, table->columns[i].ippName, &int16col[0], NULL, &readStatus);
    824                         if (!isfinite(int16col[0])) int16col[0] = int16null;
     780                        fitsIn->getHeaderKeyValue(fitsIn, TSHORT, table->columns[i].ippName, &int16col[0]);
    825781                    }
    826                     else fits_read_col(fitsIn, TSHORT, table->columns[i].ippColNum, 1, 1, nRows, &int16null, int16col, &anynull, &readStatus);
    827                     fits_write_col(fitsOut, TSHORT, col, 1, 1, nRows, int16col, &writeStatus);
     782                    else fitsIn->readColumn(fitsIn, TSHORT, table->columns[i].ippColNum, 1, 1, nRows, int16col);
     783                    fitsOut->writeColumn(fitsOut, TSHORT, col, 1, 1, nRows, int16col);
    828784                    break;
    829785                case TLONG:
    830786                    if (fromHeader) {
    831                         fits_read_key(fitsIn, TLONG, table->columns[i].ippName, &longcol[0], NULL, &readStatus);
    832                         if (!isfinite(longcol[0])) longcol[0] = longnull;
     787                        fitsIn->getHeaderKeyValue(fitsIn, TLONG, table->columns[i].ippName, &longcol[0]);
    833788                    }
    834                     else fits_read_col(fitsIn, TLONG, table->columns[i].ippColNum, 1, 1, nRows, &longnull, longcol, &anynull, &readStatus);
    835                     fits_write_col(fitsOut, TLONG, col, 1, 1, nRows, longcol, &writeStatus);
     789                    else fitsIn->readColumn(fitsIn, TLONG, table->columns[i].ippColNum, 1, 1, nRows, longcol);
     790                    fitsOut->writeColumn(fitsOut, TLONG, col, 1, 1, nRows, longcol);
    836791                    break;
    837792                case TLONGLONG:
    838793                    if (fromHeader) {
    839                         fits_read_key(fitsIn, TLONGLONG, table->columns[i].ippName, &longcol[0], NULL, &readStatus);
    840                         if (!isfinite(longcol[0])) longcol[0] = longnull;
     794                        fitsIn->getHeaderKeyValue(fitsIn, TLONGLONG, table->columns[i].ippName, &longcol[0]);
    841795                    }
    842                     else fits_read_col(fitsIn, TLONGLONG, table->columns[i].ippColNum, 1, 1, nRows, &longnull, longcol, &anynull, &readStatus);
    843                     fits_write_col(fitsOut, TLONGLONG, col, 1, 1, nRows, longcol, &writeStatus);
     796                    else fitsIn->readColumn(fitsIn, TLONGLONG, table->columns[i].ippColNum, 1, 1, nRows, longcol);
     797                    fitsOut->writeColumn(fitsOut, TLONGLONG, col, 1, 1, nRows, longcol);
    844798                    break;
    845799                case TFLOAT:
    846800                    if (fromHeader) {
    847                         fits_read_key(fitsIn, TFLOAT, table->columns[i].ippName,&floatcol[0], NULL, &readStatus);
    848                         if (!isfinite(floatcol[0])) floatcol[0] = floatnull;
     801                        fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, table->columns[i].ippName,&floatcol[0]);
    849802                    }
    850                     else fits_read_col(fitsIn, TFLOAT, table->columns[i].ippColNum, 1, 1, nRows, &floatnull, floatcol, &anynull, &readStatus);
    851                     fits_write_col(fitsOut, TFLOAT, col, 1, 1, nRows, floatcol, &writeStatus);
     803                    else fitsIn->readColumn(fitsIn, TFLOAT, table->columns[i].ippColNum, 1, 1, nRows, floatcol);
     804                    fitsOut->writeColumn(fitsOut, TFLOAT, col, 1, 1, nRows, floatcol);
    852805                    break;
    853806                case TDOUBLE:
    854807                    if (fromHeader) {
    855                         fits_read_key(fitsIn, TDOUBLE, table->columns[i].ippName, &doublecol[0], NULL, &readStatus);
    856                         if (!isfinite(doublecol[0])) doublecol[0] = doublenull;
     808                        fitsIn->getHeaderKeyValue(fitsIn, TDOUBLE, table->columns[i].ippName, &doublecol[0]);
    857809                    }
    858                     else fits_read_col(fitsIn, TDOUBLE, table->columns[i].ippColNum, 1, 1, nRows, &doublenull, doublecol, &anynull, &readStatus);
    859                     fits_write_col(fitsOut, TDOUBLE, col, 1, 1, nRows, doublecol, &writeStatus);
     810                    else fitsIn->readColumn(fitsIn, TDOUBLE, table->columns[i].ippColNum, 1, 1, nRows, doublecol);
     811                    fitsOut->writeColumn(fitsOut, TDOUBLE, col, 1, 1, nRows, doublecol);
    860812                    break;
    861813                case TSTRING:
    862                     if (fromHeader) fits_read_key(fitsIn, TSTRING, table->columns[i].ippName, strcol[0], NULL, &readStatus);
    863                     else fits_read_col(fitsIn, TSTRING, table->columns[i].ippColNum, 1, 1, nRows, " ", strcol, &anynull, &readStatus);
    864                     fits_write_col(fitsOut, TSTRING, col, 1, 1, nRows, strcol, &writeStatus);
     814                    if (fromHeader) fitsIn->getHeaderKeyValue(fitsIn, TSTRING, table->columns[i].ippName, strcol[0]);
     815                    else fitsIn->readColumn(fitsIn, TSTRING, table->columns[i].ippColNum, 1, 1, nRows, strcol);
     816                    fitsOut->writeColumn(fitsOut, TSTRING, col, 1, 1, nRows, strcol);
    865817                    break;
    866818
     
    870822                    break;
    871823            }
    872 
    873             // TODO need these errors, but strange error handling runs out of memory
    874             //if (readStatus) {
    875             //    psError(PS_ERR_IO, false, "Unable to read col num '%d' col name '%s', type %d",
    876             //        table->columns[i].ippColNum, table->columns[i].ippName, table->columns[i].ippType);
    877             //    fits_report_error(stderr, readStatus);
    878             //}
    879             //if (writeStatus) {
    880             //    psError(PS_ERR_IO, false, "Unable to write col '%s'", table->columns[i].pspsName );
    881             //    fits_report_error(stderr, writeStatus);
    882             //}
    883824        }
    884825    }
     
    895836}
    896837
    897 // creates and populates a table
    898 bool ippToPspsConfig_writeTable(
    899         IppToPspsConfig* this,
    900         fitsfile* fitsIn,
    901         fitsfile* fitsOut,
     838/**
     839  Creates and populates a table
     840  */
     841static bool createAndPopulateTable(
     842        Config* this,
     843        Fits* fitsIn,
     844        Fits* fitsOut,
    902845        const long nRows,
    903846        const char* tableName,
    904         const bool fromHeader) {
    905 
    906 
    907     IppToPspsConfig_Table* table = ippToPspsConfig_findTable(this, tableName);
     847        const int fromHeader) {
     848
     849    Table* table = findTable(this, tableName);
    908850    if (!table) return false;
    909851
    910     if(!ippToPspsConfig_createTable(table, fitsOut, nRows)) return false;
    911     return ippToPspsConfig_populateTableFromFits(table, fitsIn, fitsOut, nRows, fromHeader);
    912 }
    913 
    914 // Destructor.
    915 void ippToPspsConfig_Destructor(IppToPspsConfig* this) {
     852    if(!createTable(table, fitsOut, nRows)) return false;
     853    return populateTableFromFits(table, fitsIn, fitsOut, nRows, fromHeader);
     854}
     855
     856/**
     857  Destructor.
     858  */
     859static void destroy(Config* this) {
    916860
    917861    if (this != NULL ) {
     
    927871}
    928872
    929 // Constructor. Loads IPP -> PSPS mappings from a csv
    930 IppToPspsConfig* ippToPspsConfig_Constructor(const char* path) {
    931 
    932     IppToPspsConfig* this = NULL;
    933 
    934     this = malloc(sizeof(IppToPspsConfig));
     873/**
     874  Constructor. Loads IPP -> PSPS mappings from an XML file
     875  */
     876Config* new_Config(const char* path) {
     877
     878    Config* this = (Config*)calloc(1, sizeof(Config));
    935879
    936880    this->configsPath = NULL;
    937881    psStringAppend(&this->configsPath, path);
    938882
     883    // method pointers
     884    this->getFilterId = getFilterId;
     885    this->getSurveyId = getSurveyId;
     886    this->createAndPopulateTable = createAndPopulateTable;
     887    this->populateFromFile = populateFromFile;
     888    this->destroy = destroy;
     889
    939890    // load tables descriptions from XML
    940     if (ippToPspsConfig_loadTableDescriptions(this)) {
     891    if (loadTableDescriptions(this)) {
    941892
    942893        // load any mappings we may have from XML       
    943         ippToPspsConfig_loadMappings(this);
     894        loadMappings(this);
    944895    }
    945896
Note: See TracChangeset for help on using the changeset viewer.