NetCDF-C++ 4.3.1
ncCheck.cpp
1#include <cstring>
2#include "netcdf.h"
3#include <ncException.h>
4using namespace std;
5using namespace netCDF::exceptions;
6
7// C++ API for netCDF4.
8namespace netCDF
9{
10 // function checks error code and if necessary throws appropriate exception.
11 void ncCheck(int retCode, const char* file, int line){
12 if (retCode==NC_NOERR)
13 return;
14
15 const char* msg = 0;
16 if (NC_ISSYSERR(retCode)){
17 msg = std::strerror(retCode);
18 msg = msg ? msg : "Unknown system error";
19 }else{
20 msg = nc_strerror(retCode);
21 }
22
23 switch(retCode) {
24 case NC_EBADID : throw NcBadId(msg,file,line);
25 case NC_ENFILE : throw NcNFile(msg,file,line);
26 case NC_EEXIST : throw NcExist(msg,file,line);
27 case NC_EINVAL : throw NcInvalidArg(msg,file,line);
28 case NC_EPERM : throw NcInvalidWrite(msg,file,line);
29 case NC_ENOTINDEFINE : throw NcNotInDefineMode(msg,file,line);
30 case NC_EINDEFINE : throw NcInDefineMode(msg,file,line);
31 case NC_EINVALCOORDS : throw NcInvalidCoords(msg,file,line);
32 case NC_EMAXDIMS : throw NcMaxDims(msg,file,line);
33 case NC_ENAMEINUSE : throw NcNameInUse(msg,file,line);
34 case NC_ENOTATT : throw NcNotAtt(msg,file,line);
35 case NC_EMAXATTS : throw NcMaxAtts(msg,file,line);
36 case NC_EBADTYPE : throw NcBadType(msg,file,line);
37 case NC_EBADDIM : throw NcBadDim(msg,file,line);
38 case NC_EUNLIMPOS : throw NcUnlimPos(msg,file,line);
39 case NC_EMAXVARS : throw NcMaxVars(msg,file,line);
40 case NC_ENOTVAR : throw NcNotVar(msg,file,line);
41 case NC_EGLOBAL : throw NcGlobal(msg,file,line);
42 case NC_ENOTNC : throw NcNotNCF(msg,file,line);
43 case NC_ESTS : throw NcSts(msg,file,line);
44 case NC_EMAXNAME : throw NcMaxName(msg,file,line);
45 case NC_EUNLIMIT : throw NcUnlimit(msg,file,line);
46 case NC_ENORECVARS : throw NcNoRecVars(msg,file,line);
47 case NC_ECHAR : throw NcChar(msg,file,line);
48 case NC_EEDGE : throw NcEdge(msg,file,line);
49 case NC_ESTRIDE : throw NcStride(msg,file,line);
50 case NC_EBADNAME : throw NcBadName(msg,file,line);
51 case NC_ERANGE : throw NcRange(msg,file,line);
52 case NC_ENOMEM : throw NcNoMem(msg,file,line);
53 case NC_EVARSIZE : throw NcVarSize(msg,file,line);
54 case NC_EDIMSIZE : throw NcDimSize(msg,file,line);
55 case NC_ETRUNC : throw NcTrunc(msg,file,line);
56
57 // The following are specific netCDF4 errors.
58 case NC_EHDFERR : throw NcHdfErr(msg,file,line);
59 case NC_ECANTREAD : throw NcCantRead(msg,file,line);
60 case NC_ECANTWRITE : throw NcCantWrite(msg,file,line);
61 case NC_ECANTCREATE : throw NcCantCreate(msg,file,line);
62 case NC_EFILEMETA : throw NcFileMeta(msg,file,line);
63 case NC_EDIMMETA : throw NcDimMeta(msg,file,line);
64 case NC_EATTMETA : throw NcAttMeta(msg,file,line);
65 case NC_EVARMETA : throw NcVarMeta(msg,file,line);
66 case NC_ENOCOMPOUND : throw NcNoCompound(msg,file,line);
67 case NC_EATTEXISTS : throw NcAttExists(msg,file,line);
68 case NC_ENOTNC4 : throw NcNotNc4(msg,file,line);
69 case NC_ESTRICTNC3 : throw NcStrictNc3(msg,file,line);
70 case NC_EBADGRPID : throw NcBadGroupId(msg,file,line);
71 case NC_EBADTYPID : throw NcBadTypeId(msg,file,line); // netcdf.h file inconsistent with documentation!!
72 case NC_EBADFIELD : throw NcBadFieldId(msg,file,line); // netcdf.h file inconsistent with documentation!!
73 // case NC_EUNKNAME : throw NcUnkownName("Cannot find the field id.",file,line); // netcdf.h file inconsistent with documentation!!
74
75 case NC_ENOGRP : throw NcEnoGrp(msg,file,line);
76 case NC_ELATEDEF : throw NcElateDef(msg,file,line);
77
78 default:
79 throw NcException(retCode, msg, file, line);
80 }
81 }
82
83 void ncCheckDefineMode(int ncid)
84 {
85 int status = nc_redef(ncid);
86 if (status != NC_EINDEFINE) ncCheck(status, __FILE__, __LINE__);
87 }
88
89 void ncCheckDataMode(int ncid)
90 {
91 int status = nc_enddef(ncid);
92 if (status != NC_ENOTINDEFINE) ncCheck(status, __FILE__, __LINE__);
93 }
94}
Class represents a netCDF atomic Char type.
Definition: ncChar.h:11
Thrown if attribute exists.
Definition: ncException.h:335
Thrown if attribute meta.
Definition: ncException.h:314
Thrown if an invalid dimension id or name.
Definition: ncException.h:139
Thrown if bad field id.
Definition: ncException.h:370
Thrown if bad group id.
Definition: ncException.h:356
Thrown if the specified netCDF ID does not refer to an open netCDF dataset.
Definition: ncException.h:42
Thrown if attribute or variable name contains illegal characters.
Definition: ncException.h:230
Thrown if bad type id.
Definition: ncException.h:363
Thrown if not a valid netCDF data type.
Definition: ncException.h:132
Thrown if cannot create.
Definition: ncException.h:293
Thrown if cannot read.
Definition: ncException.h:279
Thrown if cannot write.
Definition: ncException.h:286
Thrown if dim meta.
Definition: ncException.h:307
Thrown if invalid dimension size.
Definition: ncException.h:258
Thrown if edge+start exceeds dimension bound.
Definition: ncException.h:216
Thrown if an operation to set the chunking, endianness, fill of a NcVar object is issued after a call...
Definition: ncException.h:427
Thrown if cannot return a netCDF group.
Definition: ncException.h:384
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
Thrown if, having set NC_NOCLOBBER, the specified dataset already exists.
Definition: ncException.h:56
Thrown if file meta.
Definition: ncException.h:300
Thrown if the action is prohibited on the NC_GLOBAL varid.
Definition: ncException.h:167
Thrown if an error was reported by the HDF5 layer.
Definition: ncException.h:272
Thrown if operation not allowed in defined mode.
Definition: ncException.h:84
Thrown if not a netCDF id.
Definition: ncException.h:63
Index exceeds dimension bound.
Definition: ncException.h:97
Thrown if invalid argument.
Definition: ncException.h:70
Thrown if Nc_MAX_ATTRS is exceeded.
Definition: ncException.h:125
Thrown if NC_MAX_DIMS is exceeded.
Definition: ncException.h:104
Thrown if NC_MAX_NAME is exceeded.
Definition: ncException.h:188
Thrown if NC_MAX_VARS is exceeded.
Definition: ncException.h:153
Thrown if too many netcdf files are open.
Definition: ncException.h:49
Thrown if string match to name is in use.
Definition: ncException.h:111
Thrown if no compound.
Definition: ncException.h:328
Thrown if memory allocation (malloc) failure.
Definition: ncException.h:244
Thrown if nc_rec op when there are no record vars.
Definition: ncException.h:202
Thrown if attribute is not found.
Definition: ncException.h:118
Thrown if operation not allowed in data mode.
Definition: ncException.h:77
Thrown if not a netCDF file.
Definition: ncException.h:174
Thrown if attempting netcdf-4 operation on netcdf-3 file.
Definition: ncException.h:342
Thrown if variable is not found.
Definition: ncException.h:160
Thrown if math result not representable.
Definition: ncException.h:237
Thrown if attempting netcdf-4 operation on strict nc3 netcdf-4 file.
Definition: ncException.h:349
Thrown if illegal stride.
Definition: ncException.h:223
Thrown if in FORTRAN, string is too short.
Definition: ncException.h:181
Thrown if file likely truncated or possibly corrupted.
Definition: ncException.h:265
Thrown if Nc_UNLIMITED is in the wrong index.
Definition: ncException.h:146
Thrown if NC_UNLIMITED size is already in use.
Definition: ncException.h:195
Thrown if variable meta.
Definition: ncException.h:321
Thrown if one or more variable sizes violate format constraints.
Definition: ncException.h:251
Exception classes.
Definition: ncException.h:16
C++ API for netCDF4.
Definition: ncAtt.h:10
void ncCheckDataMode(int ncid)
Function checks if the file (group) is in data mode.
Definition: ncCheck.cpp:89
void ncCheckDefineMode(int ncid)
Function checks if the file (group) is in define mode.
Definition: ncCheck.cpp:83
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition: ncCheck.cpp:11

Return to the Main Unidata NetCDF page.
Generated on Wed Nov 10 2021 15:25:08 for NetCDF-C++. NetCDF is a Unidata library.