NetCDF4 C++ API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NcFile.h
Go to the documentation of this file.
1 
17 #ifndef INC_netcdf_NcFile_h
18 #define INC_netcdf_NcFile_h
19 
20 #include "NcConfig.h"
21 #include "NcGroup.h"
22 #include "NcVersion.h"
23 
24 namespace netcdf {
25 
31 class NcFile : public NcGroup
32 {
33  public:
34 
35  enum FileMode
36  {
40  New
41  };
42 
44  {
49  };
50 
51 #ifdef USE_PARALLEL
52 
55  NcFile (
56  MPI::Comm & mpiComm,
57  MPI::Info & mpiInfo,
58  std::string const fileName,
59  FileMode const fileMode,
60  size_t const * const bufrsizeptr = NULL,
61  size_t const initialsize = 0);
62 
65  NcFile (
66  std::string const fileName,
67  FileMode const fileMode,
68  FileFormat const fileFormat = NetCDF4,
69  size_t const * const bufrsizeptr = NULL,
70  size_t const initialsize = 0);
71 
74  NcFile (
75  std::string const fileName,
76  FileMode const fileMode = ReadOnly);
77 
80  bool IsParallel () const { return mpi.IsInitialized(); }
81 
82  // no check if initialized
83  MPI::Comm & Comm () { return mpi.Comm(); }
84  MPI::Info & Info () { return mpi.Info(); }
85 
86 #else
87 
90  NcFile (
91  std::string const fileName,
92  FileMode const fileMode = ReadOnly);
93 
96  NcFile (
97  std::string const fileName,
98  FileMode const fileMode,
99  FileFormat const fileFormat,
100  size_t const * const bufrsizeptr = NULL,
101  size_t const initialsize = 0);
102 
105  bool IsParallel () const { return false; }
106 
107 #endif
108 
109  FileFormat GetFormat ();
110  bool IsNetCDF3 ();
111 
112  std::string GetFileName () const;
113  FileMode GetFileMode () const { return ncFileMode; }
114  void Sync();
115 
116  void ToDefineMode ();
117  void ToDataMode ();
118 
121  void Close ();
122 
123  virtual ~NcFile(); //closes file and releases all resources
124 
125 
126  private:
127 
128  friend class NcGroup;
129 
130  static char const * const rootGroupName;
131 
132  std::string const netcdfFileName;
133  FileMode ncFileMode;
134 
135  #ifdef USE_PARALLEL
136  NcMPI mpi;
137  #endif
138 
139  int GetOpenMode (
140  FileMode const fileMode,
141  FileFormat const fileFormat );
142 
143  void CheckAccess (); // throws if invalid access
144 
145  void ThrowOpenOrCreateError (
146  std::string const & what,
147  std::string const & fileName,
148  FileMode const fileMode,
149  FileFormat const fileFormat);
150 
151  // members not allowed
152  NcFile(const NcFile& rhs);
153  NcFile & operator= (const NcFile& rhs);
154 
155 }; // class NcFile
156 
157 
158 inline bool NcFile::IsNetCDF3 ()
159 {
160  FileFormat const fileFormat = GetFormat();
161  return ( fileFormat == Classic || fileFormat == Offset64Bits );
162 }
163 
164 //------------------------------------------------------------------------------
165 // Helper functions for NcFile
166 //------------------------------------------------------------------------------
167 
169 std::string const & GetFileModeStr (NcFile::FileMode const fileMode);
170 
172 std::string const & GetFileFormatStr (NcFile::FileFormat const fileFormat);
173 
174 
175 } // namespace netcdf
176 
177 #endif // INC_netcdf_NcFile_h
(default) netCDF-4/HDF5 format, enhanced data model
Definition: NcFile.h:47
FileMode GetFileMode() const
Definition: NcFile.h:113
void Close()
Close the netCDF file. For parallel-netCDF, this must be called before MPI::Finalize! ...
Definition: NcFile.cpp:303
MPI::Info & Info()
Definition: NcMPI.h:55
MPI::Comm & Comm()
Definition: NcFile.h:83
MPI::Info & Info()
Definition: NcFile.h:84
void ToDefineMode()
put NetCDF3 dataset into define mode
Definition: NcFile.cpp:413
bool IsNetCDF3()
true if file format is a NetCDF3 format
Definition: NcFile.h:158
Create new file; fail if already exists.
Definition: NcFile.h:40
FileMode
Definition: NcFile.h:35
char const *const fileName
Definition: Test_Parallel.cpp:17
FileFormat GetFormat()
get NetCDF file format
Definition: NcFile.cpp:399
std::string const & GetFileFormatStr(NcFile::FileFormat const fileFormat)
Get a string matching the file format.
Definition: NcFile.cpp:442
std::string GetFileName() const
get copy of file name + path
Definition: NcFile.cpp:315
NcFile(MPI::Comm &mpiComm, MPI::Info &mpiInfo, std::string const fileName, FileMode const fileMode, size_t const *const bufrsizeptr=NULL, size_t const initialsize=0)
Opens existing netCDF file for parallel MPI I/O; format is NetCDF4. Throws std::exception if failed...
Definition: NcFile.cpp:101
void ToDataMode()
put NetCDF3 dataset into data mode
Definition: NcFile.cpp:423
Manages data related to the MPI implementation.
Definition: NcMPI.h:29
Classic format, classic data model.
Definition: NcFile.h:45
std::string const & GetFileModeStr(NcFile::FileMode const fileMode)
Get a string matching the file mode.
Definition: NcFile.cpp:433
netCDF-4/HDF5 format, classic data model
Definition: NcFile.h:48
MPI::Comm & Comm()
Definition: NcMPI.h:54
Create new file, even if already exists.
Definition: NcFile.h:39
FileFormat
Definition: NcFile.h:43
64-bit offset format, classic data model
Definition: NcFile.h:46
virtual ~NcFile()
Definition: NcFile.cpp:309
bool IsParallel() const
true if doing parallel IO. This version is parallel version.
Definition: NcFile.h:80
File exists, open for read, write, append.
Definition: NcFile.h:38
Manages the opening, creating, and closing of netCDF files. Represents the netCDF root group...
Definition: NcFile.h:31
void Sync()
synchronize to disk
Definition: NcFile.cpp:325
File exists, open read-only.
Definition: NcFile.h:37
bool IsInitialized() const
Definition: NcMPI.h:57
Represents a netCDF group.
Definition: NcGroup.h:36