File: write_array.c

package info (click to toggle)
libcgns 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,740 kB
  • ctags: 4,493
  • sloc: ansic: 46,717; fortran: 1,341; sh: 368; makefile: 259
file content (71 lines) | stat: -rw-r--r-- 2,032 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
# include <unistd.h>
#endif
#include "utils.h"

int main (int argc, char **argv)
{
    int na, narrays = 100, arraysize = 1024;
    int cgfile, cgbase;
    char name[33];
    float *array;
    double start, end;
    static char *fname = "array.cgns";

    if (argc > 1) {
        narrays = atoi (argv[1]);
        if (argc > 2)
            arraysize = atoi (argv[2]);
    }
    printf ("writing %d arrays of size %d\n", narrays, arraysize);
    array = (float *) malloc (arraysize * sizeof(float));
    if (NULL == array) {
        fprintf (stderr, "malloc failed\n");
        exit (1);
    }
    for (na = 0; na < arraysize; na++)
        array[na] = (float)na;

    unlink (fname);
    start = elapsed_time ();
    if (cg_open (fname, CG_MODE_WRITE, &cgfile) ||
        cg_base_write (cgfile, "Base", 3, 3, &cgbase) ||
        cg_goto (cgfile, cgbase, NULL) ||
        cg_user_data_write ("Data") ||
        cg_goto (cgfile, cgbase, "UserDefinedData_t", 1, NULL))
        cg_error_exit();

    for (na = 1; na <= narrays; na++) {
        sprintf (name, "Array%d", na);
        if (cg_array_write (name, RealSingle, 1, &arraysize, array))
            cg_error_exit ();
    }
    if (cg_close(cgfile)) cg_error_exit();
    end = elapsed_time ();
    printf ("time = %g secs, size = %g Mb\n",
        end - start, file_size(fname));
#if 0
    puts ("rewriting the file");
    fflush (stdout);
    start = elapsed_time ();
    if (cg_open (fname, CG_MODE_MODIFY, &cgfile)) cg_error_exit();
    cgbase = 1;

    if (cg_goto (cgfile, cgbase, "UserDefinedData_t", 1, NULL))
        cg_error_exit();
    for (na = 1; na <= narrays; na++) {
        sprintf (name, "Array%d", na);
        if (cg_array_write (name, RealSingle, 1, &arraysize, array))
            cg_error_exit ();
    }
    if (cg_close(cgfile)) cg_error_exit();
    end = elapsed_time ();
    printf ("time = %g secs, size = %g Mb\n",
        end - start, file_size(fname));
#endif
    return 0;
}