File: test_tfits.c

package info (click to toggle)
qfits 6.2.0-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,976 kB
  • ctags: 1,019
  • sloc: ansic: 11,631; sh: 8,330; makefile: 131
file content (344 lines) | stat: -rw-r--r-- 12,457 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* $Id: test_tfits.c,v 1.18 2006/04/27 13:08:43 yjung Exp $
 *
 * This file is part of the ESO QFITS Library
 * Copyright (C) 2001-2004 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Author: yjung $
 * $Date: 2006/04/27 13:08:43 $
 * $Revision: 1.18 $
 * $Name: qfits-6_2_0 $
 */

/*---------------------------------------------------------------------------
                                   Includes
 ---------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>

#include "qfits_header.h"
#include "qfits_table.h"
#include "qfits_memory.h"

/*---------------------------------------------------------------------------
                                   Define
 ---------------------------------------------------------------------------*/

#define BIN_TABLE_NAME          "/bintable.tfits"
#define ASCII_TABLE_NAME        "/asciitable.tfits"
#define TMP_TABLE_NAME          "tmp_table.tfits"

/*---------------------------------------------------------------------------
                               Functions prototypes
 ---------------------------------------------------------------------------*/

static int qfits_test_table(char *, int) ;
static void say(char * fmt, ...) ;
static void fail(char * fmt, ...) ;

/*---------------------------------------------------------------------------
                                Functions
 ---------------------------------------------------------------------------*/

/* Print out a comment */
static void say(char * fmt, ...)
{
    va_list ap ;
    fprintf(stdout, "qtest:\t\t");
    va_start(ap, fmt);
    vfprintf(stdout, fmt, ap);
    va_end(ap);
    fprintf(stdout, "\n");
}
 
/* Print out an error message */
static void fail(char * fmt, ...)
{
    va_list ap ;
    fprintf(stderr, "qtest: error: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
}

/* Test a bin table : read it, write it, read it, and compare the 2 reads */
static int qfits_test_table(
        char    *    name,
        int            extnum)
{
    qfits_header    *    qh ;
    qfits_table        *    qt ;
    void            **    one_column_table ;
    void            *    column_after_wr ;
    qfits_table        *    qt_tmp ;
    int                    nb_val ;
    int                    err ;
    double                ddiff ;
    int                    idiff ;

    int                    i, j ;

    /* Initialize */
    err = 0 ;    

    /* Create a default primary header */
    if ((qh = qfits_table_prim_header_default()) == NULL) {
        fail("cannot create a default primary header") ;
        err++ ;
        return err ;
    }
    
    printf("----- %s\n", name) ;
    /* Read the test table */
    if ((qt = qfits_table_open(name, extnum)) == NULL) {
        fail("cannot load infos from the input table") ;
        qfits_header_destroy(qh) ;
        err++ ;
        return err ;
    }
    
    /* For each column */
    for (i=0 ; i<qt->nc ; i++) {
        say("Test the %dth column", i+1) ;
        /* Create the data array */
        one_column_table = qfits_malloc(1*sizeof(void*)) ; 

        /* Load the ith column data */
        if ((one_column_table[0] = qfits_query_column_data(qt, 
                        i, NULL, NULL)) == NULL) {
            qfits_free(one_column_table) ;
            /* The 6th column is empty - normal if not loaded */
            if ((i == 5) && (qt->tab_t == QFITS_BINTABLE)) {
                say("Column %d is empty", i+1) ;
            } else {
                err++ ;
                fail("cannot load the data from the %dth column", i+1) ;
            }
            continue ;
        }
        
        /* Create a tmp table object */
        qt_tmp = qfits_table_new(TMP_TABLE_NAME, qt->tab_t, -1, 1, qt->nr) ;
        qfits_col_fill(qt_tmp->col, (&(qt->col)[i])->atom_nb, 
                (&(qt->col)[i])->atom_dec_nb, (&(qt->col)[i])->atom_size, 
                (&(qt->col)[i])->atom_type, (&(qt->col)[i])->tlabel, 
                (&(qt->col)[i])->tunit, (&(qt->col)[i])->nullval, 
                (&(qt->col)[i])->tdisp, 0, 0.0, 0, 1.0, 0) ;
            
        /* Write a one column table */
        if (qfits_save_table_hdrdump((const void **)one_column_table, qt_tmp, 
                    qh) == -1) {
            fail("cannot save a table to file") ;
            qfits_free(one_column_table) ;
            qfits_table_close(qt_tmp) ;
            err++ ;
            continue ;
        }
        /* Destroy the tmp table object */
        qfits_table_close(qt_tmp) ;
        
        /* Read the one column table */
        if ((qt_tmp = qfits_table_open(TMP_TABLE_NAME, 1)) == NULL) {
            fail("cannot load infos from the generated table"); 
            qfits_free(one_column_table) ;
            err++ ;
            continue ;
        }
    
        /* Load the column data */
        if ((column_after_wr = qfits_query_column_data(qt_tmp, 
                        0, NULL, NULL)) == NULL) {
            fail("cannot load the data from the generated table") ;
            qfits_free(one_column_table) ;
            qfits_table_close(qt_tmp) ;
            err++ ;
            continue ;
        }
        
        /* Destroy the tmp table object */
        remove(qt_tmp->filename) ;    
        qfits_table_close(qt_tmp) ;

        /* Test the diff between column_after_wr and one_column_table[0] */
        switch ((&(qt->col)[i])->atom_type) {
            case TFITS_BIN_TYPE_A :
            case TFITS_BIN_TYPE_L :
            case TFITS_ASCII_TYPE_A :
                /* Set the number of values in the data array */
                nb_val = qt->nr * (&(qt->col)[i])->atom_nb ;
                idiff = strncmp((char*)column_after_wr, 
                        (char*)one_column_table[0], nb_val) ;
                if (idiff == 0) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            case TFITS_BIN_TYPE_B :
            case TFITS_BIN_TYPE_X :
                /* Set the number of values in the data array */
                nb_val = qt->nr * (&(qt->col)[i])->atom_nb ;
                idiff = 0 ;
                for (j=0 ; j<nb_val ; j++) {
                    idiff += ((int)((unsigned char*)column_after_wr)[j] -
                        (int)((unsigned char*)one_column_table[0])[j]) ;
                }
                if (idiff == 0) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            case TFITS_BIN_TYPE_D :
            case TFITS_BIN_TYPE_M :
                /* Set the number of values in the data array */
                nb_val = qt->nr * (&(qt->col)[i])->atom_nb ;
                ddiff = 0.0 ;
                for (j=0 ; j<nb_val ; j++) {
                    ddiff += ((double*)column_after_wr)[j] -
                        ((double*)one_column_table[0])[j] ;
                }
                if (fabs(ddiff) < 1e-6) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            case TFITS_BIN_TYPE_E :
            case TFITS_BIN_TYPE_C :
                /* Set the number of values in the data array */
                nb_val = qt->nr * (&(qt->col)[i])->atom_nb ;
                ddiff = 0.0 ;
                for (j=0 ; j<nb_val ; j++) {
                    ddiff += (double)(((float*)column_after_wr)[j] -
                        ((float*)one_column_table[0])[j]) ;
                }
                if (fabs(ddiff) < 1e-6) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            case TFITS_BIN_TYPE_I :
                /* Set the number of values in the data array */
                nb_val = qt->nr * (&(qt->col)[i])->atom_nb ;
                idiff = 0 ;
                for (j=0 ; j<nb_val ; j++) {
                    idiff += (int)(((short*)column_after_wr)[j] -
                        ((short*)one_column_table[0])[j]) ;
                }
                if (idiff == 0) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            case TFITS_BIN_TYPE_P :
            case TFITS_BIN_TYPE_J :
                /* Set the number of values in the data array */
                nb_val = qt->nr * (&(qt->col)[i])->atom_nb ;
                idiff = 0 ;
                for (j=0 ; j<nb_val ; j++) {
                    idiff += ((int*)column_after_wr)[j] -
                        ((int*)one_column_table[0])[j] ;
                }
                if (idiff == 0) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            case TFITS_ASCII_TYPE_E :
            case TFITS_ASCII_TYPE_F :
                /* Set the number of values in the data array */
                nb_val = qt->nr ;
                ddiff = 0.0 ;
                for (j=0 ; j<nb_val ; j++) {
                    ddiff += (double)(((float*)column_after_wr)[j] -
                        ((float*)one_column_table[0])[j]) ;
                }
                say("Check the cumulated difference : %g", fabs(ddiff)) ; 
                break ;
            case TFITS_ASCII_TYPE_D :
                /* Set the number of values in the data array */
                nb_val = qt->nr ;
                ddiff = 0.0 ;
                for (j=0 ; j<nb_val ; j++) {
                    ddiff += (double)((double*)column_after_wr)[j] -
                        ((double*)one_column_table[0])[j] ;
                }
                say("Check the cumulated difference : %g", fabs(ddiff)) ; 
                break ;
            case TFITS_ASCII_TYPE_I :
                /* Set the number of values in the data array */
                nb_val = qt->nr ;
                idiff = 0 ;
                for (j=0 ; j<nb_val ; j++) {
                    idiff += ((int)((unsigned char*)column_after_wr)[j] -
                        (int)((unsigned char*)one_column_table[0])[j]) ;
                }
                if (idiff == 0) say("Columns are identical...ok") ;
                else { fail("Columns are not the same") ; err++ ;}
                break ;
            default :
                err++ ;
                fail("Column type not recognized") ;
                break ;
        }
    
        /* Free */
        qfits_free(one_column_table[0]) ;
        qfits_free(one_column_table) ;
        qfits_free(column_after_wr) ;
    }

    /* Delete the table */
    qfits_table_close(qt) ;

    /* Delete the fits header */
    qfits_header_destroy(qh) ;

    return err ;
}

/*---------------------------------------------------------------------------
                                Main
 ---------------------------------------------------------------------------*/

int main(int argc, char * argv[])
{
    int         err ;
    char        filename[512] ;
    char    *   srcdir ;

    /* Initialize */
    err=0 ;

    /* Get the src directory */
    srcdir = strdup(getenv("srcdir")) ;
    
    /* 
     *    Test on BINARY tables
     */
    say("Test the BINARY table") ;
    strcat(filename, srcdir) ;
    strcat(filename, BIN_TABLE_NAME) ;
    err += qfits_test_table(filename, 1) ;    
    
    /* 
     *    Test on ASCII tables
     */
    say("Test the ASCII table") ;
    filename[0] = (char)0 ;
    strcat(filename, srcdir) ;
    strcat(filename, ASCII_TABLE_NAME) ;
    err += qfits_test_table(filename, 1) ;    
    
    free(srcdir) ;
    fprintf(stderr, "total error(s): %d\n", err);
    return err ;
}