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

qfits_table* qfits_table_open ( const char *  filename,
int  xtnum 
)

Read a FITS extension.

Parameters:
filename Name of the FITS file to examine.
xtnum Extension number to read (starting from 1).
Returns:
Pointer to newly allocated qfits_table structure.
Read a FITS table from a given file name and extension, and return a newly allocated qfits_table structure.

Definition at line 409 of file qfits_table.c.

References col, nc, nr, qfits_col_fill(), qfits_get_datinfo(), qfits_is_fits(), qfits_is_table(), qfits_pretty_string(), qfits_query_ext(), qfits_table_close(), and qfits_table_new().

{
    qfits_table     *   tload ;
    qfits_col       *   curr_col ;
    char            *   str_val ;
    char                keyword[FITSVALSZ] ;
    /* Table infos  */
    int                 table_type ;
    int                 nb_col ;
    int                 table_width ;
    int                 nb_raws ;
    /* Column infos */
    char                label[FITSVALSZ] ;
    char                unit[FITSVALSZ] ;
    char                disp[FITSVALSZ] ;
    char                nullval[FITSVALSZ] ;
    int                 atom_nb ;
    int                 atom_dec_nb ;
    int                 atom_size ;
    tfits_type          atom_type ;
    int                 offset_beg ;
    int                 data_size ;
    int                 theory_size ;
    int                 zero_present ;
    int                 scale_present ;
    float               zero ;
    float               scale ;
    
    /* For ASCII tables */
    int                    col_pos ;    
    int                    next_col_pos ;
    
    /* For X type */
    int                    nb_bits ;
        
    int                    i ;
    
     /* See if 'filename' is a fits file  */
    if (qfits_is_fits(filename) != 1) {
        qfits_error("[%s] is not FITS", filename) ;
        return NULL ;
    }
        
    /* Identify a table and get the table type : ASCII or BIN */
    if ((table_type = qfits_is_table(filename, xtnum))==0) {
        qfits_error("[%s] extension %d is not a table", filename, xtnum) ;
        return NULL ;
    }
    
    /* Get number of columns and allocate them: nc <-> TFIELDS */
    if ((str_val = qfits_query_ext(filename, "TFIELDS", xtnum)) == NULL) {
        qfits_error("cannot read TFIELDS in [%s]:[%d]", filename, xtnum) ;
        return NULL ;
    }
    nb_col = atoi(str_val) ;

    /* Get the width in bytes of the table */
    if ((str_val = qfits_query_ext(filename, "NAXIS1", xtnum)) == NULL) {
        qfits_error("cannot read NAXIS1 in [%s]:[%d]", filename, xtnum) ;
        return NULL ;
    }
    table_width = atoi(str_val) ;
    
    /* Get the number of raws */
    if ((str_val = qfits_query_ext(filename, "NAXIS2", xtnum)) == NULL) {
        qfits_error("cannot read NAXIS2 in [%s]:[%d]", filename, xtnum) ;
        return NULL ;
    }
    nb_raws = atoi(str_val) ;

    /* Create the table object */
    tload = qfits_table_new(filename, table_type, table_width, nb_col, nb_raws);
    
    /* Initialize offset_beg */
    if (qfits_get_datinfo(filename, xtnum, &offset_beg, &data_size)!=0) {
        qfits_error("cannot find data start in [%s]:[%d]", filename, xtnum);
        qfits_table_close(tload);
        return NULL ;
    }
    
    /* Loop on all columns and get column descriptions  */
    curr_col = tload->col ;
    for (i=0 ; i<tload->nc ; i++) {
        /* label <-> TTYPE     */
        sprintf(keyword, "TTYPE%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) {
            label[0] = (char)0 ;
        } else strcpy(label, qfits_pretty_string(str_val)) ;
        
        /* unit <-> TUNIT */
        sprintf(keyword, "TUNIT%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) {
            unit[0] = (char)0 ;
        } else strcpy(unit, qfits_pretty_string(str_val)) ;

        /* disp <-> TDISP */
        sprintf(keyword, "TDISP%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) {
            disp[0] = (char)0 ;
        } else strcpy(disp, qfits_pretty_string(str_val)) ;

        /* nullval <-> TNULL */
        sprintf(keyword, "TNULL%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum)) == NULL) {
            nullval[0] = (char)0 ;
        } else strcpy(nullval, qfits_pretty_string(str_val)) ;
    
        /* atom_size, atom_nb, atom_dec_nb, atom_type    <-> TFORM */
        sprintf(keyword, "TFORM%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum))==NULL) {
            qfits_error("cannot read [%s] in [%s]:[%d]", keyword, filename, 
                    xtnum);
            qfits_table_close(tload);
            return NULL ;
        }
        /* Interpret the type in header */
        if (qfits_table_interpret_type(qfits_pretty_string(str_val), 
                        &(atom_nb), 
                        &(atom_dec_nb),
                        &(atom_type), 
                        table_type) == -1) {
            qfits_error("cannot interpret the type: %s", str_val) ;
            qfits_table_close(tload) ;
            return NULL ;
        }
        
        /* Set atom_size */
        switch (atom_type) {
            case TFITS_BIN_TYPE_A:
            case TFITS_BIN_TYPE_L:
            case TFITS_BIN_TYPE_B:
                atom_size = 1 ;
                break ;
            case TFITS_BIN_TYPE_I:
                atom_size = 2 ;
                break ;
            case TFITS_BIN_TYPE_J:
            case TFITS_BIN_TYPE_E:
            case TFITS_ASCII_TYPE_I:
            case TFITS_ASCII_TYPE_E:
            case TFITS_ASCII_TYPE_F:
                atom_size = 4 ;
                break ;
            case TFITS_BIN_TYPE_C:
            case TFITS_BIN_TYPE_P:
                atom_size = 4 ;
                atom_nb *= 2 ;
                break ;
            case TFITS_BIN_TYPE_D:
            case TFITS_ASCII_TYPE_D:
                atom_size = 8 ;
                break ;
            case TFITS_BIN_TYPE_M:
                atom_size = 8 ;
                atom_nb *= 2 ;
                break ;
            case TFITS_BIN_TYPE_X:
                atom_size = 1 ;
                nb_bits = atom_nb ;
                atom_nb = (int)((nb_bits - 1)/ 8) + 1 ;
                break ;
            case TFITS_ASCII_TYPE_A:
                atom_size = atom_nb ;
                break ;
            default:
                qfits_error("unrecognized type") ;
                qfits_table_close(tload) ;
                return NULL ;
                break ;
        }
    
        /* zero <-> TZERO */
        sprintf(keyword, "TZERO%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum)) != NULL) {
            zero = (float)atof(str_val) ;
            zero_present = 1 ;    
        } else {
            zero = (float)0.0 ;
            zero_present = 0 ;    
        }
        
        /* scale <-> TSCAL */
        sprintf(keyword, "TSCAL%d", i+1) ;
        if ((str_val=qfits_query_ext(filename, keyword, xtnum)) != NULL) {
            scale = (float)atof(str_val) ;
            scale_present = 1 ;
        } else {
            scale = (float)1.0 ;
            scale_present = 0 ;
        }

        /* Fill the current column object */
        qfits_col_fill(curr_col, atom_nb, atom_dec_nb, atom_size, atom_type, 
                label, unit, nullval, disp, zero_present, zero, scale_present, 
                scale, offset_beg) ;
        
        /* Compute offset_beg but for the last column */
        if (i < tload->nc - 1) {
            if (table_type == QFITS_ASCIITABLE) {
                /* column width <-> TBCOLi and TBCOLi+1 */
                sprintf(keyword, "TBCOL%d", i+1) ;
                if ((str_val=qfits_query_ext(filename, keyword, xtnum))==NULL) {
                    qfits_error("cannot read [%s] in [%s]", keyword, filename);
                    qfits_table_close(tload);
                    return NULL ;
                }
                col_pos = atoi(qfits_pretty_string(str_val)) ;
                
                sprintf(keyword, "TBCOL%d", i+2) ;
                if ((str_val=qfits_query_ext(filename, keyword, xtnum))==NULL){
                    qfits_error("cannot read [%s] in [%s]", keyword, filename) ;
                    qfits_table_close(tload) ;
                    return NULL ;
                }
                next_col_pos = atoi(qfits_pretty_string(str_val)) ;
                offset_beg += (int)(next_col_pos - col_pos) ;
            } else if (table_type == QFITS_BINTABLE) {
                offset_beg += atom_nb * atom_size ;
            }
        }
        curr_col++ ;
    }

    /* Check that the theoritical data size is not far from the measured */
    /* one by more than 2880 */
    theory_size = qfits_compute_table_width(tload)*tload->nr ;
    if (data_size < theory_size) {
        qfits_error("Uncoherent data sizes") ;
        qfits_table_close(tload) ;
        return NULL ;
    }
    
    /* Return  */
    return tload ;
}


Generated by  Doxygen 1.6.0   Back to index