File: read_metadata.c

package info (click to toggle)
pnetcdf 1.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,812 kB
  • sloc: ansic: 85,298; f90: 10,707; fortran: 9,283; cpp: 8,864; makefile: 3,084; perl: 2,833; sh: 2,538; yacc: 1,227; lex: 216
file content (135 lines) | stat: -rw-r--r-- 3,858 bytes parent folder | download | duplicates (5)
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
/*
 *  Copyright (C) 2018, Northwestern University and Argonne National Laboratory
 *  See COPYRIGHT notice in top-level directory.
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This examples demonstrates how to enumerate all variable, dimension,
 * and attributes in a BP file using PnetCDF.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* strcpy() */
#include <unistd.h> /* getopt() */
#include <mpi.h>
#include "pnetcdf.h"
#include <math.h>

static int verbose;

/* This is the name of the data file we will read. */
#define FILE_NAME "../../test/adios/attributes.bp"

#define ERR {if(err!=NC_NOERR){printf("Error at %s:%d : %s\n", __FILE__,__LINE__, ncmpi_strerror(err));nerrs++;}}

static void
usage(char *argv0)
{
    char *help =
    "Usage: %s [-h] | [-q] filename\n"
    "       [-h] Print help\n"
    "       [-q] Quiet mode (reports when fail)\n"
    "       filename - input BP file name\n";
    fprintf(stderr, help, argv0);
}

int main(int argc, char** argv) {
    extern int optind;
    char filename[256];
    int i, j, rank, nprocs, err, nerrs = 0;
    int ncid, natt, ndim, nudim, nvar;
    int dimids[1024];
    char name[NC_MAX_NAME + 1];
    nc_type vtype;
    MPI_Offset len;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

    verbose = 1;

    /* get command-line arguments */
    while ((i = getopt(argc, argv, "hq")) != EOF)
        switch(i) {
            case 'q': verbose = 0;
                      break;
            case 'h':
            default:  if (rank==0) usage(argv[0]);
                      MPI_Finalize();
                      return 1;
        }

    if (argv[optind] != NULL)
        snprintf(filename, 256, "%s", argv[optind]);
    else {
        if (rank==0) {
            printf("Error: input file is required\n");
            usage(argv[0]);
        }
        MPI_Finalize();
        return 1;
    }

    /* Open ADIOS BP file as if opening a netcdf file
     * PnetCDF can only read BP files for now, NC_NOWRITE must be set
     */
    err = ncmpi_open(MPI_COMM_WORLD, filename, NC_NOWRITE, MPI_INFO_NULL,
                        &ncid);
    ERR

    err = ncmpi_inq(ncid, &ndim, &nvar, &natt, &nudim);
    ERR
    if (verbose)
        printf("ndim: %d, nvar: %d, natt: %d, nudim: %d\n", ndim, nvar, natt,
               nudim);

    for(i = 0; i < ndim; i++){
        err = ncmpi_inq_dim(ncid, i, name, &len);
        if (verbose)
            printf("Dim %d: name = %s, length = %llu\n", i, name, len);
    }

    for(i = 0; i < natt; i++){
        err = ncmpi_inq_attname (ncid, NC_GLOBAL, i, name);
        ERR
        err = ncmpi_inq_att(ncid, NC_GLOBAL, name, NULL, &len);
        ERR
        if (verbose)
            printf("Att %d: name = %s, length = %llu\n", i, name, len);
    }

    for(i = 0; i < nvar; i++){
        err = ncmpi_inq_var(ncid, i, name, &vtype, &ndim, NULL, &natt);
        ERR
        if (verbose)
            printf("Var %d: name = %s, ndim = %d, natt = %d, dimmids = ", i,
                   name,  ndim, natt);
        if (ndim < 1024){
            err = ncmpi_inq_var(ncid, i, name, &vtype, &ndim, dimids, &natt);
            ERR
            if (verbose) {
                for (j=0; j<ndim; j++)
                    printf("%d, ", dimids[j]);
            }
        }
        if (verbose) printf("\n");
        for(j = 0; j < natt; j++){
            err = ncmpi_inq_attname (ncid, i, j, name);
            ERR
            err = ncmpi_inq_att(ncid, i, name, NULL, &len);
            ERR
            if (verbose)
                printf("\tAtt %d: name = %s, length = %llu\n", j, name, len);
        }
    }

    ncmpi_close(ncid);

    MPI_Finalize();

    return 0;
}