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