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

int* qfits_query_column_nulls ( const qfits_table th,
int  colnum,
const int *  selection,
int *  nb_vals,
int *  nb_nulls 
)

Detect NULL values in a column.

Parameters:
th Allocated qfits_table
colnum Number of the column to check (from 0 to colnum-1)
selection Array to identify selected rows
nb_vals Gives the size of the output array
nb_nulls Gives the number of detected null values
Returns:
array with 1 for NULLs and 0 for non-NULLs The returned object must be deallocated with qfits_free().

Definition at line 1310 of file qfits_table.c.

References qfits_col::atom_nb, qfits_col::atom_type, col, nr, qfits_col::nullval, qfits_query_column(), and qfits_col::readable.

{
    int             *    out_array ;
    qfits_col       *   col ;
    unsigned char    *    in_array ;
    void            *    tmp_array ;
    char            *    field ;
    int                 nb_rows ;
    int                 i ;

    /* Initialize */
    *nb_nulls = 0 ;
    *nb_vals = 0 ;

    /* Get 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 readable */
    if (col->readable == 0) return NULL ;

    /* Handle each type separately */
    switch(col->atom_type) {
        case TFITS_ASCII_TYPE_A:
        case TFITS_ASCII_TYPE_D:
        case TFITS_ASCII_TYPE_E:
        case TFITS_ASCII_TYPE_F:
        case TFITS_ASCII_TYPE_I:
        in_array = (unsigned char*)qfits_query_column(th, colnum, selection) ;
        out_array = qfits_calloc(nb_rows, sizeof(int));
        *nb_vals = nb_rows ;
        field = qfits_malloc((col->atom_nb+1)*sizeof(char)) ;
        for (i=0 ; i<nb_rows ; i++) {
            /* Copy all atoms of the field into 'field' */
            memcpy(field, &in_array[i*col->atom_nb], col->atom_nb);
            field[col->atom_nb]=(char)0 ;
            /* Test if a NULL val is encoutered */
            if (!strcmp(col->nullval, qfits_strstrip(field))) {
                out_array[i] = 1 ;    
                (*nb_nulls)++ ;
            } 
        }
        qfits_free(field) ;
        if (in_array != NULL) qfits_free(in_array) ;
        break ;
            
        case TFITS_BIN_TYPE_A:
        /* No NULL values */
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        break ;
        
        case TFITS_BIN_TYPE_L:
        case TFITS_BIN_TYPE_X:
        case TFITS_BIN_TYPE_P:
        /* No NULL values */
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        break ;
            
        case TFITS_BIN_TYPE_D:
        case TFITS_BIN_TYPE_M:
        tmp_array = (double*)qfits_query_column(th, colnum, selection) ;
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        for (i=0 ; i<nb_rows * col->atom_nb ; i++) {
            if (qfits_isnan(((double*)tmp_array)[i]) || 
                qfits_isinf(((double*)tmp_array)[i])) {
                out_array[i] = 1 ;
                (*nb_nulls)++ ;
            }
        }
        if (tmp_array != NULL) qfits_free(tmp_array) ;
        break ;
        
        case TFITS_BIN_TYPE_E:
        case TFITS_BIN_TYPE_C:
        tmp_array = (float*)qfits_query_column(th, colnum, selection) ;
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        for (i=0 ; i<nb_rows * col->atom_nb ; i++) {
            if (qfits_isnan(((float*)tmp_array)[i]) || 
                qfits_isinf(((float*)tmp_array)[i])) {
                out_array[i] = 1 ;
                (*nb_nulls)++ ;
            }
        }
        if (tmp_array != NULL) qfits_free(tmp_array) ;
        break ;
        
        case TFITS_BIN_TYPE_B:
        tmp_array = (unsigned char*)qfits_query_column(th, colnum, selection) ;
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        for (i=0 ; i<nb_rows * col->atom_nb ; i++) {
            if (((col->nullval)[0] != (char)0) &&
                (atoi(col->nullval)==(int)((unsigned char*)tmp_array)[i])) {
                out_array[i] = 1 ;
                (*nb_nulls)++ ;
            }
        }
        if (tmp_array != NULL) qfits_free(tmp_array) ;
        break ;
            
        case TFITS_BIN_TYPE_I:
        tmp_array = (short*)qfits_query_column(th, colnum, selection) ;
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        for (i=0 ; i<nb_rows * col->atom_nb ; i++) {
            if (((col->nullval)[0] != (char)0) &&
                (atoi(col->nullval)==(int)((short*)tmp_array)[i])) {     
                out_array[i] = 1 ;
                (*nb_nulls)++ ;
            }
        }
        if (tmp_array != NULL) qfits_free(tmp_array) ;
        break ;
            
        case TFITS_BIN_TYPE_J:
        tmp_array = (int*)qfits_query_column(th, colnum, selection) ;
        out_array = qfits_calloc(nb_rows * col->atom_nb, sizeof(int)) ;
        *nb_vals = nb_rows * col->atom_nb ;
        for (i=0 ; i<nb_rows * col->atom_nb ; i++) {
            if (((col->nullval)[0] != (char)0) &&
                (atoi(col->nullval)==((int*)tmp_array)[i])) {     
                out_array[i] = 1 ;
                (*nb_nulls)++ ;
            }
        }
        if (tmp_array != NULL) qfits_free(tmp_array) ;
        break ;
            
        default:
        qfits_error("unrecognized data type") ;
        return NULL ;
    }
    return out_array ;    
}


Generated by  Doxygen 1.6.0   Back to index