NetCDF-C++ 4.3.1
ncFile.cpp
1#include "ncFile.h"
2#include "ncCheck.h"
3#include "ncException.h"
4#include "ncByte.h"
5#include<iostream>
6#include<string>
7#include<sstream>
8using namespace std;
9using namespace netCDF;
10using namespace netCDF::exceptions;
11
12int g_ncid = -1;
13
14// destructor
15NcFile::~NcFile()
16{
17 // destructor may be called due to an exception being thrown
18 // hence throwing an exception from within a destructor
19 // causes undefined behaviour! so just printing a warning message
20 try
21 {
22 close();
23 }
24 catch (NcException &e)
25 {
26 cerr << e.what() << endl;
27 }
28}
29
31{
32 if (!nullObject) {
33 ncCheck(nc_close(myId),__FILE__,__LINE__);
34 g_ncid = -1;
35 }
36
37 nullObject = true;
38}
39
40// Constructor generates a null object.
42 NcGroup() // invoke base class constructor
43{}
44
45// constructor
46NcFile::NcFile(const string& filePath, const FileMode fMode)
47{
48 open(filePath, fMode);
49}
50
51// constructor
52NcFile::NcFile(const string& filePath, const int ncFileFlags)
53{
54 open(filePath, ncFileFlags);
55}
56
63void NcFile::open(const string& filePath, int ncFileFlags) {
64 if(!nullObject)
65 close();
66
67 ncCheck(nc_open(filePath.c_str(), ncFileFlags, &myId),__FILE__,__LINE__);
68 g_ncid = myId;
69
70 nullObject=false;
71}
72
73
74// open a file from path and mode
75void NcFile::open(const string& filePath, const FileMode fMode)
76{
77 if (!nullObject)
78 close();
79
80
81
82 switch (fMode)
83 {
84 case NcFile::write:
85 ncCheck(nc_open(filePath.c_str(), NC_WRITE, &myId),__FILE__,__LINE__);
86 break;
87 case NcFile::read:
88 ncCheck(nc_open(filePath.c_str(), NC_NOWRITE, &myId),__FILE__,__LINE__);
89 break;
90 case NcFile::newFile:
91 ncCheck(nc_create(filePath.c_str(), NC_NETCDF4 | NC_NOCLOBBER, &myId),__FILE__,__LINE__);
92 break;
93 case NcFile::replace:
94 ncCheck(nc_create(filePath.c_str(), NC_NETCDF4 | NC_CLOBBER, &myId),__FILE__,__LINE__);
95 break;
96 }
97
98 g_ncid = myId;
99
100 nullObject=false;
101}
102
103// constructor with file type specified
104NcFile::NcFile(const string& filePath, const FileMode fMode, const FileFormat fFormat )
105{
106 open(filePath, fMode, fFormat);
107}
108
109
114void NcFile::create(const string& filePath, const int ncFileFlags) {
115 if(!nullObject)
116 close();
117
118 ncCheck(nc_create(filePath.c_str(),ncFileFlags,&myId),__FILE__,__LINE__);
119
120 g_ncid = myId;
121
122 nullObject=false;
123}
124
125void NcFile::open(const string& filePath, const FileMode fMode, const FileFormat fFormat )
126{
127 if (!nullObject)
128 close();
129
130 int format;
131 switch (fFormat)
132 {
133 case NcFile::classic:
134 format = 0;
135 break;
137 format = NC_64BIT_OFFSET;
138 break;
139 case NcFile::nc4:
140 format = NC_NETCDF4;
141 break;
143 format = NC_NETCDF4 | NC_CLASSIC_MODEL;
144 break;
145 }
146 switch (fMode)
147 {
148 case NcFile::write:
149 ncCheck(nc_open(filePath.c_str(), format | NC_WRITE, &myId),__FILE__,__LINE__);
150 break;
151 case NcFile::read:
152 ncCheck(nc_open(filePath.c_str(), format | NC_NOWRITE, &myId),__FILE__,__LINE__);
153 break;
154 case NcFile::newFile:
155 ncCheck(nc_create(filePath.c_str(), format | NC_NOCLOBBER, &myId),__FILE__,__LINE__);
156 break;
157 case NcFile::replace:
158 ncCheck(nc_create(filePath.c_str(), format | NC_CLOBBER, &myId),__FILE__,__LINE__);
159 break;
160 }
161
162 g_ncid = myId;
163 nullObject=false;
164}
165
166// Synchronize an open netcdf dataset to disk
168 ncCheck(nc_sync(myId),__FILE__,__LINE__);
169}
170// Set fill mode for netCDF dataset open for writing and return current fill mode
171void NcFile::set_Fill(int fillmode, int *old_modep){
172 ncCheck(nc_set_fill(myId, fillmode, old_modep),__FILE__,__LINE__);
173}
174
175
176// Put open netCDF dataset into define mode
178 ncCheck(nc_redef(myId),__FILE__,__LINE__);
179}
180
181// Leave define mode, used for classic model
183 ncCheck(nc_enddef(myId),__FILE__,__LINE__);
184}
NcFile()
Constructor generates a null object.
Definition: ncFile.cpp:41
void sync()
Synchronize an open netcdf dataset to disk.
Definition: ncFile.cpp:167
void enddef()
Leave define mode, used for classic model.
Definition: ncFile.cpp:182
void set_Fill(int fillmode, int *old_modep)
Elect a fill parameter different that the one currently being used.
Definition: ncFile.cpp:171
@ newFile
Create new file, fail if already exists.
Definition: ncFile.h:27
@ read
File exists, open read-only.
Definition: ncFile.h:24
@ write
File exists, open for writing.
Definition: ncFile.h:25
@ replace
Create new file, even if already exists.
Definition: ncFile.h:26
void open(const std::string &filePath, int ncFileFlags)
Opens a netCDF file.
void close()
Close a file before destructor call.
Definition: ncFile.cpp:30
void create(const std::string &filePath, int ncFileFlags)
Create a netCDF file.
Definition: ncFile.cpp:114
void redef()
Redefine variable.
Definition: ncFile.cpp:177
@ classic
Classic format, classic data model.
Definition: ncFile.h:32
@ classic64
64-bit offset format, classic data model
Definition: ncFile.h:33
@ nc4
(default) netCDF-4/HDF5 format, enhanced data model
Definition: ncFile.h:34
@ nc4classic
netCDF-4/HDF5 format, classic data model
Definition: ncFile.h:35
Class represents a netCDF group.
Definition: ncGroup.h:28
bool nullObject
assignment operator
Definition: ncGroup.h:570
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
Exception classes.
Definition: ncException.h:16
C++ API for netCDF4.
Definition: ncAtt.h:10
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.