forked from Unidata/netcdf-c
/
tst_default_format_pnetcdf.c
156 lines (125 loc) · 4.42 KB
/
tst_default_format_pnetcdf.c
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
/*! \file
Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018
University Corporation for Atmospheric Research/Unidata.
See \ref copyright file for more info.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>
#include <netcdf_par.h>
#define ERR { \
if (err != exp_err) { \
printf("Error at %s line %d: %s\n", __FILE__, __LINE__, \
nc_strerror(err)); \
nerrs++; \
} \
}
static int default_format;
static const char*
formatstr(int format)
{
const char* str = NULL;
switch (format) {
case NC_FORMAT_CLASSIC: str="NC_FORMAT_CLASSIC"; break;
case NC_FORMAT_64BIT_OFFSET: str="NC_FORMAT_64BIT_OFFSET"; break;
case NC_FORMAT_64BIT_DATA: str="NC_FORMAT_64BIT_DATA"; break;
case NC_FORMAT_NETCDF4: str="NC_FORMAT_NETCDF4"; break;
case NC_FORMAT_NETCDF4_CLASSIC:
str="NC_FORMAT_NETCDF4_CLASSIC";break;
default: break;
}
return str;
}
static int
create_check_pnetcdf(char *fname, int cmode, int exp_format)
{
int nerrs=0, err, exp_err=NC_NOERR, ncid, format;
const char *exp_str;
#ifndef USE_PNETCDF
exp_err = NC_ENOTBUILT;
#endif
exp_str = formatstr(exp_format);
#ifndef ENABLE_CDF5
if (cmode & NC_64BIT_DATA) exp_err = NC_ENOTBUILT;
#endif
/* create a file */
cmode |= NC_CLOBBER;
err = nc_create_par(fname, cmode, MPI_COMM_WORLD, MPI_INFO_NULL, &ncid); ERR
if (exp_err == NC_ENOTBUILT) return 0;
err = nc_close(ncid); ERR
/* open the file and check its format */
err = nc_open(fname, NC_NOWRITE, &ncid); ERR
err = nc_inq_format(ncid, &format); ERR
if (format != exp_format) {
const char *f_str="", *d_str="";
f_str = formatstr(format);
d_str = formatstr(default_format);
printf("Error at %s line %d: default is %s and expect %s but got %s\n",
__FILE__, __LINE__, d_str, exp_str, f_str);
nerrs++;
}
err = nc_close(ncid); ERR
return nerrs;
}
int main(int argc, char *argv[])
{
char *fname="tst_default_format_pnetcdf.nc";
int err, exp_err=NC_NOERR, nerrs=0, ncid, cmode;
MPI_Init(&argc, &argv);
if (argc == 2) fname = argv[1];
default_format = NC_FORMAT_CLASSIC;
/* check illegal cmode */
cmode = NC_64BIT_OFFSET | NC_64BIT_DATA;
err = nc_create_par(fname, cmode, MPI_COMM_WORLD, MPI_INFO_NULL, &ncid);
if (err != NC_EINVAL) {
printf("Error at %s line %d: expect NC_EINVAL but got %d\n",
__FILE__, __LINE__, err);
nerrs++;
}
#ifdef USE_NETCDF4
/* check illegal cmode */
cmode = NC_NETCDF4 | NC_64BIT_OFFSET;
err = nc_create_par(fname, cmode, MPI_COMM_WORLD, MPI_INFO_NULL, &ncid);
if (err != NC_EINVAL) {
printf("Error at %s line %d: expect NC_EINVAL but got %d\n",
__FILE__, __LINE__, err);
nerrs++;
}
#endif
/* create a file in CDF1 format */
cmode = 0;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_CLASSIC);
/* create a file in CDF2 format */
cmode = NC_64BIT_OFFSET;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_64BIT_OFFSET);
#ifdef ENABLE_CDF5
/* create a file in CDF5 format */
cmode = NC_64BIT_DATA;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_64BIT_DATA);
#endif
/* set default file format to NC_FORMAT_64BIT_OFFSET ------------------*/
default_format = NC_FORMAT_64BIT_OFFSET;
err = nc_set_default_format(default_format, NULL); ERR
/* create a file in default format */
cmode = 0;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_64BIT_OFFSET);
#ifdef ENABLE_CDF5
/* create a file in CDF5 format (this should ignore default) */
cmode = NC_64BIT_DATA;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_64BIT_DATA);
/* set default file format to NC_FORMAT_64BIT_DATA --------------------*/
default_format = NC_FORMAT_64BIT_DATA;
err = nc_set_default_format(default_format, NULL); ERR
/* create a file in default format */
cmode = 0;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_64BIT_DATA);
#endif
/* create a file in CDF2 format (this should ignore default) */
cmode = NC_64BIT_OFFSET;
nerrs += create_check_pnetcdf(fname, cmode, NC_FORMAT_64BIT_OFFSET);
MPI_Finalize();
return (nerrs > 0);
}