IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 31010


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

Changes to Fits class

Location:
trunk/ippToPsps/src
Files:
3 edited

Legend:

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

    r30861 r31010  
    1919    if (fits_close_file(this->file, &status)) {
    2020
    21         psError(PS_ERR_IO, false, "Unable to close FITS file");
     21        psError(PS_ERR_IO, false, "* Fits: Unable to close FITS file");
    2222        fits_report_error(stderr, status);
    2323    }
    24 
    25 printf("destroyed...\n");
    26 
    27     return;
     24    else
     25        psLogMsg("ippToPsps", PS_LOG_INFO, "* Fits: Closed '%s'", this->path);
     26}
     27
     28/**
     29  Gets a header key value
     30  */
     31static bool getHeaderKeyValue(Fits* this, int type, char* name, void* value) {
     32
     33    int status = 0;
     34    fits_read_key(this->file, type, name, value, NULL, &status);
     35
     36    if (status) {
     37
     38        psError(PS_ERR_IO, false, "* Fits: Could not get value for header key '%s'\n", name);
     39        return false;
     40    }
     41
     42    return true;
     43}
     44
     45/*
     46   Gets the contents of a column vector
     47   */
     48static bool getColumnVector(
     49        Fits* this,
     50        int col,
     51        long row,
     52        int type,
     53        int repeat,
     54        float* vector) {
     55
     56    int status = 0;
     57    int anynull = 0;
     58
     59    // remember to add 1 to row number since cfitsio is the stupidest libary ever written
     60    fits_read_col(this->file, type, col, row+1, 1, repeat, NULL, vector, &anynull, &status);
     61
     62    if (status) {
     63
     64        psError(PS_ERR_IO, false, "* Fits: Failed to read vector column %d row %ld", col, row);
     65        return false;
     66    }
     67
     68    return true;
    2869}
    2970
     
    3778    if (status) {
    3879
    39         psError(PS_ERR_IO, false, "Could not get column number for '%s'\n", name);
    40         return false;
    41     }
    42 
    43     return true;
    44 }
     80        psError(PS_ERR_IO, false, "* Fits: Could not get column number for '%s'\n", name);
     81        return false;
     82    }
     83
     84    return true;
     85}
     86
     87/*
     88   Gets metadata about a column
     89   */
     90bool getColumnMeta(
     91        Fits* this,
     92        char* name,
     93        int* colNum,
     94        int* type,
     95        long* repeat) {
     96
     97    int status = 0;
     98    fits_get_colnum(this->file, CASESEN, name, colNum, &status);
     99    if (status) {
     100
     101        psError(PS_ERR_IO, false, "* Fits: Unable to read col '%s'", name);
     102        return false;
     103    }
     104
     105    status = 0;
     106    fits_get_eqcoltype(this->file, *colNum, type, repeat, NULL, &status);
     107    if (status) {
     108
     109        psError(PS_ERR_IO, false, "* Fits: Unable to read type info for '%s'", name);
     110        return false;
     111    }
     112
     113    return true;
     114}
     115
    45116
    46117/**
     
    52123    if (fits_movnam_hdu(this->file, type, name, 0, &status)) {
    53124
    54         psError(PS_ERR_IO, false, "Can't move to table extension named '%s'\n", name);
     125        psError(PS_ERR_IO, false, "* Fits: Can't move to table extension named '%s'\n", name);
    55126    }
    56127
     
    61132  Moves to a certain header extension
    62133  */
    63 bool moveToHeader(Fits* this, char* name) {
     134static bool moveToHeader(Fits* this, char* name) {
    64135
    65136    return moveToExtension(this, name, IMAGE_HDU);
     
    69140  Moves to a certain binary table extension
    70141  */
    71 bool moveToBinaryTable(Fits* this, char* name) {
     142static bool moveToBinaryTable(Fits* this, char* name) {
    72143
    73144    return moveToExtension(this, name, BINARY_TBL);
     
    75146
    76147/**
     148  Counts the rows in the current table extension
     149  */
     150static bool countRows(Fits* this, long* count) {
     151
     152    int status = 0;
     153    if (fits_get_num_rows(this->file, count, &status)) {
     154
     155        psError(PS_ERR_IO, false, "* Fits: Count not count rows in this table\n");
     156        return false;
     157    }
     158
     159    return true;
     160}
     161
     162/**
     163  Moves to a certain binary table extension and counts rows within
     164  */
     165static bool moveToBinaryTableAndCountRows(Fits* this, char* name, long* count) {
     166
     167    if (!moveToExtension(this, name, BINARY_TBL)) return false;
     168    return countRows(this, count);
     169}
     170
     171/**
     172  Deletes this file.
     173  */
     174static bool delete(Fits* this) {
     175
     176    if (remove(this->path) == -1) {
     177
     178        psError(PS_ERR_UNKNOWN, false, "* Fits: Unable to delete '%s'", this->path);
     179        return false;
     180    }
     181
     182    return true;
     183}
     184
     185/**
     186  Returns the file path.
     187  */
     188static char* getPath(Fits* this) {
     189
     190    return this->path;
     191}
     192
     193/**
    77194  Returns the file pointer.
    78195  */
     
    83200
    84201/**
    85   Constructor. Returns a new Fits object.
    86   */
    87 Fits* new_Fits(char* path) {
    88 
    89     Fits* this = (Fits*)calloc(1, sizeof(Fits));
    90 
    91     int status = 0;
    92     if (fits_open_file(&this->file, path, READONLY, &status)) {
    93 
    94         psError(PS_ERR_IO, false, "Unable to open FITS file here %s\n", path);
    95     }
     202  Deletes a bunch of rows from the current binary table
     203  */
     204static bool deleteRows(Fits* this, long *rowlist, long nrows) {
     205
     206    int status = 0;
     207    fits_delete_rowlist(this->file, rowlist, nrows, &status);
     208    if (status) {
     209
     210        psError(PS_ERR_IO, false, "* Fits: Unable to delet rows using rowlist \n");
     211        return false;
     212    }
     213
     214    return true;
     215}
     216
     217/**
     218  Reads a column from the current binary table
     219  */
     220static bool readColumn(
     221        Fits* this,
     222        int datatype,
     223        int colnum,
     224        LONGLONG firstrow,
     225        LONGLONG firstelem,
     226        LONGLONG nelements,
     227        void* coldata)  {
     228
     229    int anynull = 0;
     230    int status = 0;
     231
     232    if (datatype == TBYTE) {
     233
     234        int8_t nullval = -99;
     235        fits_read_col(this->file, datatype, colnum, firstrow, firstelem, nelements, &nullval, coldata, &anynull, &status);
     236    }
     237    else if (datatype == TSHORT) {
     238
     239        int16_t nullval = -999;
     240        fits_read_col(this->file, datatype, colnum, firstrow, firstelem, nelements, &nullval, coldata, &anynull, &status);
     241    }
     242    else if (datatype == TLONG || datatype == TLONGLONG) {
     243
     244        long nullval = -999;
     245        fits_read_col(this->file, datatype, colnum, firstrow, firstelem, nelements, &nullval, coldata, &anynull, &status);
     246    }
     247    else if (datatype == TFLOAT) {
     248
     249        float nullval = -999.0;
     250        fits_read_col(this->file, datatype, colnum, firstrow, firstelem, nelements, &nullval, coldata, &anynull, &status);
     251    }
     252    else if (datatype == TDOUBLE) {
     253
     254        double nullval = -999.0;
     255        fits_read_col(this->file, datatype, colnum, firstrow, firstelem, nelements, &nullval, coldata, &anynull, &status);
     256    }
     257    else if (datatype == TSTRING) {
     258
     259        char nullval[20];
     260        strcpy(nullval, "null");
     261        fits_read_col(this->file, datatype, colnum, firstrow, firstelem, nelements, &nullval, coldata, &anynull, &status);
     262    }
     263    else {
     264
     265        psError(PS_ERR_IO, false, "* Fits: Don't support datatype '%d' yet\n", datatype);
     266        return false;
     267    }
     268
     269    if (status) {
     270
     271        psError(PS_ERR_IO, false, "* Fits: Unable to read column data from column %d\n", colnum);
     272        return false;
     273
     274    }
     275
     276    return true;
     277}
     278
     279/**
     280  Reads a column from the current binary table providing column name
     281  */
     282static bool readColumnUsingName(
     283        Fits* this,
     284        int datatype,
     285        char* colname,
     286        LONGLONG firstrow,
     287        LONGLONG firstelem,
     288        LONGLONG nelements,
     289        void* coldata)  {
     290
     291    int colnum;
     292    if (!getColumnNumber(this, colname, &colnum)) return false;
     293
     294    return  readColumn(this, datatype, colnum, firstrow, firstelem, nelements, coldata);
     295}
     296
     297/**
     298  Writes a column to the current binary table
     299  */
     300static bool writeColumn(
     301        Fits* this,
     302        int datatype,
     303        int colnum,
     304        LONGLONG firstrow,
     305        LONGLONG firstelem,
     306        LONGLONG nelements,
     307        void* coldata) {
     308
     309    int status = 0;
     310    fits_write_col(this->file, datatype, colnum, firstrow, firstelem, nelements, coldata, &status);
     311    if (status) {
     312
     313        psError(PS_ERR_IO, false, "* Fits: Unable to write column data for column %d\n", colnum);
     314        return false;
     315
     316    }
     317
     318    return true;
     319}
     320
     321/**
     322  Creates a new binary table
     323  */
     324static bool createBinaryTable(
     325        Fits* this,
     326        LONGLONG nRows,
     327        int nCols,
     328        char *names[],
     329        char *types[],
     330        char *name) {
     331
     332    int status = 0;
     333    fits_create_tbl(this->file, BINARY_TBL, nRows, nCols, names, types, NULL, name, &status);
     334
     335    if (status) {
     336
     337        psError(PS_ERR_IO,"* Fits: Unable to create table: '%s'", name);
     338        return false;
     339    }
     340
     341
     342    return true;
     343}
     344
     345/**
     346  Initilization common to all constructors
     347  */
     348static void init(Fits* this) {
    96349
    97350    // method pointers
    98351    this->getFilePtr = getFilePtr;
     352    this->getPath = getPath;
     353    this->countRows = countRows;
     354    this->getColumnMeta = getColumnMeta;
    99355    this->getColumnNumber = getColumnNumber;
     356    this->getColumnVector = getColumnVector;
     357    this->readColumn = readColumn;
     358    this->readColumnUsingName = readColumnUsingName;
     359    this->getHeaderKeyValue = getHeaderKeyValue;
    100360    this->moveToExtension = moveToExtension;
    101361    this->moveToHeader = moveToHeader;
    102362    this->moveToBinaryTable = moveToBinaryTable;
     363    this->moveToBinaryTableAndCountRows = moveToBinaryTableAndCountRows;
     364    this->deleteRows = deleteRows;
     365    this->createBinaryTable = createBinaryTable;
     366    this->writeColumn = writeColumn;
     367    this->delete = delete;
    103368    this->destroy = destroy;
    104369
    105370    assert(this);
    106371
     372}
     373
     374/**
     375  Constructor.
     376
     377  Returns a new Fits object representing an existing FITS file.
     378
     379*/
     380Fits* existing_Fits(char* path) {
     381
     382    Fits* this = (Fits*)calloc(1, sizeof(Fits));
     383
     384    int status = 0;
     385    strcpy(this->path, path);
     386
     387    if (fits_open_file(&this->file, this->path, READONLY, &status)) {
     388
     389        psError(PS_ERR_IO, false, "* Fits: Unable to open FITS file here %s\n", path);
     390        this->file = NULL;
     391    }
     392
     393    init(this);
     394
    107395    return this;
    108396}
    109397
    110 
     398/**
     399  Constructor.
     400
     401  Returns a new Fits object representing a new FITS file.
     402  */
     403Fits* new_Fits(char* path) {
     404
     405    Fits* this = (Fits*)calloc(1, sizeof(Fits));
     406
     407    int status = 0;
     408    strcpy(this->path, path);
     409
     410    if (fits_create_file(&this->file, this->path, &status)) {
     411
     412        psError(PS_ERR_IO, false, "* Fits: Unable to create file here '%s'", path);
     413        this->file = NULL;
     414    }
     415
     416    init(this);
     417
     418    return this;
     419}
     420
     421
  • trunk/ippToPsps/src/Fits.h

    r30861 r31010  
    1515
    1616/**
     17
    1718  Class that encapsulates a FITS file, making an OO interface to the dreaded cfitsio
    1819
     
    2122
    2223    // fields
    23     fitsfile* file;
     24    fitsfile* file;             // cfitsio file pointer
     25    char path[1000];            // FITS path
    2426
    25     // methods
     27    // accessor methods
    2628    fitsfile* (*getFilePtr)();
     29    char* (*getPath)();
     30    bool (*countRows)();
     31    bool (*getColumnMeta)();
    2732    bool (*getColumnNumber)();
     33    bool (*getColumnVector)();
     34    bool (*readColumn)();
     35    bool (*readColumnUsingName)();
     36    bool (*getHeaderKeyValue)();
     37
     38    // mutators
    2839    bool (*moveToExtension)();
    2940    bool (*moveToHeader)();
    3041    bool (*moveToBinaryTable)();
     42    bool (*moveToBinaryTableAndCountRows)();
     43    bool (*createBinaryTable)();
     44    bool (*writeColumn)();
     45    bool (*deleteRows)();
     46    bool (*delete)();
     47
     48    // destructor
    3149    void (*destroy)();
    3250
    3351} Fits;
    3452
     53// constructors
     54Fits* existing_Fits(char* path);
    3555Fits* new_Fits(char* path);
    3656
  • trunk/ippToPsps/src/Makefile.am

    r30147 r31010  
    3131        Batch.c \
    3232        Version.c \
     33        Fits.c \
    3334        Config.c
    3435
     
    4041        Batch.c \
    4142        Version.c \
     43        Fits.c \
    4244        Config.c
    4345
     
    4951        Batch.c \
    5052        Version.c \
     53        Fits.c \
    5154        Config.c
    5255
Note: See TracChangeset for help on using the changeset viewer.