Logo Search packages:      
Sourcecode: qfits version File versions  Download package

unsigned char* qfits_query_column ( const qfits_table th,
int  colnum,
const int *  selection 
)

Extract data from a column in a FITS table.

Parameters:
th Allocated qfits_table
colnum Number of the column to extract (from 0 to colnum-1)
selection boolean array to define the selected rows
Returns:
unsigned char array
If selection is NULL, select the complete column.

Extract a column from a FITS table and return the data as a bytes array. The returned array type and size are determined by the column object in the qfits_table and by the selection parameter.

Returned array size in bytes is: nbselected * col->natoms * col->atom_size

Numeric types are correctly understood and byte-swapped if needed, to be converted to the local machine type.

NULL values have to be handled by the caller.

The returned object must be deallocated with qfits_free().

Definition at line 688 of file qfits_table.c.

References qfits_col::atom_nb, qfits_col::atom_size, col, filename, nr, qfits_col::off_beg, qfits_swap_bytes(), qfits_col::readable, tab_t, and tab_w.

Referenced by qfits_query_column_data(), and qfits_query_column_nulls().

{
    char            *    start ;
    qfits_col       *   col ;
    int                    field_size ;
    unsigned char   *   array ;
    unsigned char   *   r ;
    unsigned char   *   inbuf ;
    int                 table_width ;
    int                 nb_rows ;
    size_t              size ;
    int                 i ;
   
    if (th->tab_w == -1) {
        /* Compute the table width in bytes */
        if ((table_width = qfits_compute_table_width(th)) == -1) {
            qfits_error("cannot compute the table width") ;
            return NULL ;
        }
    } else table_width = th->tab_w ;
   
    /* Compute the number of selected rows */
    nb_rows = 0 ;
    if (selection == NULL) {
        nb_rows = th->nr ;
    } else {
        for (i=0 ; i<th->nr ; i++) if (selection[i] == 1) nb_rows++ ;
    }
    
    /* Pointer to requested column */
    col = th->col + colnum ;

    /* Test if column is empty */
    if (nb_rows * col->atom_size * col->atom_nb == 0) col->readable = 0 ;
    
    /* Test if column is readable */
    if (col->readable == 0)  return NULL ;

    /* Compute the size in bytes of one field stored in the file */
    if ((field_size=qfits_table_get_field_size(th->tab_t,col))==-1) return NULL;
    
    /* Load input file */
    if ((start=qfits_falloc((char *)(th->filename), 0, &size))==NULL) {
        qfits_error("cannot open table for query [%s]", th->filename);
        return NULL ;
    }
   
    /* Allocate data array */
    array = qfits_malloc(nb_rows * field_size * sizeof(char)) ; 
            
    /* Position the input pointer at the begining of the column data */
    r = array ;
    inbuf = (unsigned char*)start + col->off_beg ;
   
    /* Copy the values in array */
    if (selection == NULL) {
        /* No selection : get the complete column */
        for (i=0 ; i<th->nr ; i++) {
            /* Copy all atoms on this field into array */
            memcpy(r, inbuf, field_size);
            r += field_size ;
            /* Jump to next line */
            inbuf += table_width ;
        }
    } else {
        /* Get only the selected rows */
        for (i=0 ; i<th->nr ; i++) {
            if (selection[i] == 1) {
                /* Copy all atoms on this field into array */
                memcpy(r, inbuf, field_size);
                r += field_size ;
            }
            /* Jump to next line */
            inbuf += table_width ;
        }
    }
    qfits_fdealloc(start, 0, size) ;

    /* SWAP the bytes if necessary */
#ifndef WORDS_BIGENDIAN
    if (th->tab_t == QFITS_BINTABLE) {
        r = array ;
        for (i=0 ; i<nb_rows * col->atom_nb ; i++) {
            qfits_swap_bytes(r, col->atom_size);
            r += col->atom_size ;
        }
    }
#endif

     /* Return allocated and converted array */
    return array ;
}


Generated by  Doxygen 1.6.0   Back to index