Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add open and close methods for NcFile #18

Merged
merged 1 commit into from
May 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions cxx4/ncFile.cpp
Original file line number Diff line number Diff line change
@@ -13,28 +13,43 @@ using namespace netCDF::exceptions;
NcFile::~NcFile()
{
// destructor may be called due to an exception being thrown
// hence throwing an exception from within a destructor
// hence throwing an exception from within a destructor
// causes undefined behaviour! so just printing a warning message
try
{
if (!nullObject)
ncCheck(nc_close(myId),__FILE__,__LINE__);
close();
}
catch (NcException &e)
catch (NcException &e)
{
cerr << e.what() << endl;
}
}

void NcFile::close()
{
if (!nullObject)
ncCheck(nc_close(myId),__FILE__,__LINE__);
nullObject = true;
}

// Constructor generates a null object.
NcFile::NcFile() :
NcFile::NcFile() :
NcGroup() // invoke base class constructor
{}

// constructor
NcFile::NcFile(const string& filePath, const FileMode fMode)
{
switch (fMode)
open(filePath, fMode);
}

// open a file from path and mode
void NcFile::open(const string& filePath, const FileMode fMode)
{
if (!nullObject)
close();

switch (fMode)
{
case NcFile::write:
ncCheck(nc_open(filePath.c_str(), NC_WRITE, &myId),__FILE__,__LINE__);
@@ -55,6 +70,14 @@ NcFile::NcFile(const string& filePath, const FileMode fMode)
// constructor with file type specified
NcFile::NcFile(const string& filePath, const FileMode fMode, const FileFormat fFormat )
{
open(filePath, fMode, fFormat);
}

void NcFile::open(const string& filePath, const FileMode fMode, const FileFormat fFormat )
{
if (!nullObject)
close();

int format;
switch (fFormat)
{
@@ -71,7 +94,7 @@ NcFile::NcFile(const string& filePath, const FileMode fMode, const FileFormat fF
format = NC_NETCDF4 | NC_CLASSIC_MODEL;
break;
}
switch (fMode)
switch (fMode)
{
case NcFile::write:
ncCheck(nc_open(filePath.c_str(), format | NC_WRITE, &myId),__FILE__,__LINE__);
45 changes: 33 additions & 12 deletions cxx4/ncFile.h
Original file line number Diff line number Diff line change
@@ -10,16 +10,16 @@
namespace netCDF
{

/*!
/*!
Class represents a netCDF root group.
The Ncfile class is the same as the NcGroup class with the additional functionality for opening
The Ncfile class is the same as the NcGroup class with the additional functionality for opening
and closing files.
*/
class NcFile : public NcGroup
{
public:
enum FileMode

enum FileMode
{
read, //!< File exists, open read-only.
write, //!< File exists, open for writing.
@@ -34,12 +34,12 @@ namespace netCDF
nc4, //!< (default) netCDF-4/HDF5 format, enhanced data model
nc4classic //!< netCDF-4/HDF5 format, classic data model
};


/*! Constructor generates a \ref isNull "null object". */
NcFile();

/*!
/*!
Opens a netCDF file.
\param filePath Name of netCDF optional path.
\param fMode The file mode:
@@ -49,19 +49,40 @@ namespace netCDF
- 'newFile' Create new file, fail it exists already.
*/
NcFile(const std::string& filePath, FileMode fMode);
/*!
Opens a netCDF file.
\param filePath Name of netCDF optional path.
\param fMode The file mode:
- 'read' File exists, open for read-only.
- 'write' File exists, open for writing.
- 'replace' Create new file, even it already exists.
- 'newFile' Create new file, fail it exists already.
*/
void open(const std::string& filePath, FileMode fMode);

/*!
/*!
Creates a netCDF file of a specified format.
\param filePath Name of netCDF optional path.
\param fMode The file mode:
- 'replace' Create new file, even it already exists.
- 'newFile' Create new file, fail it exists already.
*/
NcFile(const std::string& filePath, FileMode fMode, FileFormat fFormat);

/*!
Creates a netCDF file of a specified format.
\param filePath Name of netCDF optional path.
\param fMode The file mode:
- 'replace' Create new file, even it already exists.
- 'newFile' Create new file, fail it exists already.
*/
void open(const std::string& filePath, FileMode fMode, FileFormat fFormat);

//! Close a file before destructor call
void close();

/*! destructor */
virtual ~NcFile(); //closes file and releases all resources

//! Synchronize an open netcdf dataset to disk
void sync();

@@ -77,9 +98,9 @@ namespace netCDF
NcFile(const NcGroup& rhs);
NcFile(const NcFile& rhs);
};

}


#endif