NetCDF-C++  4.3.1-developer
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 using namespace std;
9 using namespace netCDF;
10 using namespace netCDF::exceptions;
11 
12 int g_ncid = -1;
13 
14 // destructor
15 NcFile::~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 
30 void NcFile::close()
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.
41 NcFile::NcFile() :
42  NcGroup() // invoke base class constructor
43 {}
44 
45 // constructor
46 NcFile::NcFile(const string& filePath, const FileMode fMode)
47 {
48  open(filePath, fMode);
49 }
50 
51 // open a file from path and mode
52 void NcFile::open(const string& filePath, const FileMode fMode)
53 {
54  if (!nullObject)
55  close();
56 
57  switch (fMode)
58  {
59  case NcFile::write:
60  ncCheck(nc_open(filePath.c_str(), NC_WRITE, &myId),__FILE__,__LINE__);
61  break;
62  case NcFile::read:
63  ncCheck(nc_open(filePath.c_str(), NC_NOWRITE, &myId),__FILE__,__LINE__);
64  break;
65  case NcFile::newFile:
66  ncCheck(nc_create(filePath.c_str(), NC_NETCDF4 | NC_NOCLOBBER, &myId),__FILE__,__LINE__);
67  break;
68  case NcFile::replace:
69  ncCheck(nc_create(filePath.c_str(), NC_NETCDF4 | NC_CLOBBER, &myId),__FILE__,__LINE__);
70  break;
71  }
72 
73  g_ncid = myId;
74 
75  nullObject=false;
76 }
77 
78 // constructor with file type specified
79 NcFile::NcFile(const string& filePath, const FileMode fMode, const FileFormat fFormat )
80 {
81  open(filePath, fMode, fFormat);
82 }
83 
84 void NcFile::open(const string& filePath, const FileMode fMode, const FileFormat fFormat )
85 {
86  if (!nullObject)
87  close();
88 
89  int format;
90  switch (fFormat)
91  {
92  case NcFile::classic:
93  format = 0;
94  break;
95  case NcFile::classic64:
96  format = NC_64BIT_OFFSET;
97  break;
98  case NcFile::nc4:
99  format = NC_NETCDF4;
100  break;
101  case NcFile::nc4classic:
102  format = NC_NETCDF4 | NC_CLASSIC_MODEL;
103  break;
104  }
105  switch (fMode)
106  {
107  case NcFile::write:
108  ncCheck(nc_open(filePath.c_str(), format | NC_WRITE, &myId),__FILE__,__LINE__);
109  break;
110  case NcFile::read:
111  ncCheck(nc_open(filePath.c_str(), format | NC_NOWRITE, &myId),__FILE__,__LINE__);
112  break;
113  case NcFile::newFile:
114  ncCheck(nc_create(filePath.c_str(), format | NC_NOCLOBBER, &myId),__FILE__,__LINE__);
115  break;
116  case NcFile::replace:
117  ncCheck(nc_create(filePath.c_str(), format | NC_CLOBBER, &myId),__FILE__,__LINE__);
118  break;
119  }
120 
121  g_ncid = myId;
122  nullObject=false;
123 }
124 
125 // Synchronize an open netcdf dataset to disk
127  ncCheck(nc_sync(myId),__FILE__,__LINE__);
128 }
129 
130 // Leave define mode, used for classic model
132  ncCheck(nc_enddef(myId),__FILE__,__LINE__);
133 }
netCDF-4/HDF5 format, classic data model
Definition: ncFile.h:35
File exists, open for writing.
Definition: ncFile.h:25
C++ API for netCDF4.
Definition: ncAtt.h:9
bool nullObject
assignment operator
Definition: ncGroup.h:570
void open(const std::string &filePath, FileMode fMode)
Opens a netCDF file.
64-bit offset format, classic data model
Definition: ncFile.h:33
void enddef()
Leave define mode, used for classic model.
Definition: ncFile.cpp:131
NcFile()
Constructor generates a null object.
Definition: ncFile.cpp:41
Class represents a netCDF group.
Definition: ncGroup.h:27
Exception classes.
Definition: ncException.h:15
Create new file, fail if already exists.
Definition: ncFile.h:27
Classic format, classic data model.
Definition: ncFile.h:32
void sync()
Synchronize an open netcdf dataset to disk.
Definition: ncFile.cpp:126
void close()
Close a file before destructor call.
Definition: ncFile.cpp:30
Base object is thrown if a netCDF exception is encountered.
Definition: ncException.h:24
void ncCheck(int retCode, const char *file, int line)
Function checks error code and if necessary throws an exception.
Definition: ncCheck.cpp:11
(default) netCDF-4/HDF5 format, enhanced data model
Definition: ncFile.h:34
File exists, open read-only.
Definition: ncFile.h:24
Create new file, even if already exists.
Definition: ncFile.h:26

Return to the Main Unidata NetCDF page.
Generated on Fri Nov 11 2016 15:28:29 for NetCDF-C++. NetCDF is a Unidata library.