1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include "ncFile.h"
#include "ncCheck.h"
#include "ncException.h"
#include "ncByte.h"
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
using namespace netCDF;
using namespace netCDF::exceptions;
int g_ncid = -1;
// destructor
NcFile::~NcFile()
{
// destructor may be called due to an exception being thrown
// hence throwing an exception from within a destructor
// causes undefined behaviour! so just printing a warning message
try
{
close();
}
catch (NcException &e)
{
cerr << e.what() << endl;
}
}
void NcFile::close()
{
if (!nullObject) {
ncCheck(nc_close(myId),__FILE__,__LINE__);
g_ncid = -1;
}
nullObject = true;
}
// Constructor generates a null object.
NcFile::NcFile() :
NcGroup() // invoke base class constructor
{}
// constructor
NcFile::NcFile(const string& filePath, const FileMode 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__);
break;
case NcFile::read:
ncCheck(nc_open(filePath.c_str(), NC_NOWRITE, &myId),__FILE__,__LINE__);
break;
case NcFile::newFile:
ncCheck(nc_create(filePath.c_str(), NC_NETCDF4 | NC_NOCLOBBER, &myId),__FILE__,__LINE__);
break;
case NcFile::replace:
ncCheck(nc_create(filePath.c_str(), NC_NETCDF4 | NC_CLOBBER, &myId),__FILE__,__LINE__);
break;
}
g_ncid = myId;
nullObject=false;
}
// 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)
{
case NcFile::classic:
format = 0;
break;
case NcFile::classic64:
format = NC_64BIT_OFFSET;
break;
case NcFile::nc4:
format = NC_NETCDF4;
break;
case NcFile::nc4classic:
format = NC_NETCDF4 | NC_CLASSIC_MODEL;
break;
}
switch (fMode)
{
case NcFile::write:
ncCheck(nc_open(filePath.c_str(), format | NC_WRITE, &myId),__FILE__,__LINE__);
break;
case NcFile::read:
ncCheck(nc_open(filePath.c_str(), format | NC_NOWRITE, &myId),__FILE__,__LINE__);
break;
case NcFile::newFile:
ncCheck(nc_create(filePath.c_str(), format | NC_NOCLOBBER, &myId),__FILE__,__LINE__);
break;
case NcFile::replace:
ncCheck(nc_create(filePath.c_str(), format | NC_CLOBBER, &myId),__FILE__,__LINE__);
break;
}
g_ncid = myId;
nullObject=false;
}
// Synchronize an open netcdf dataset to disk
void NcFile::sync(){
ncCheck(nc_sync(myId),__FILE__,__LINE__);
}
// Leave define mode, used for classic model
void NcFile::enddef() {
ncCheck(nc_enddef(myId),__FILE__,__LINE__);
}
|