Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
NetCDFArchive.h
Go to the documentation of this file.
1 //===-- serialbox/core/archive/NetCDFArchive.h --------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #ifndef SERIALBOX_CORE_ARCHIVE_NETCDFARCHIVE_H
16 #define SERIALBOX_CORE_ARCHIVE_NETCDFARCHIVE_H
17 
19 #ifdef SERIALBOX_HAS_NETCDF
20 
22 #include "serialbox/core/Json.h"
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 namespace serialbox {
29 
35 class NetCDFArchive : public Archive {
36 public:
38  static const std::string Name;
39 
41  static const int Version;
42 
55  NetCDFArchive(OpenModeKind mode, const std::string& directory, const std::string& prefix);
56 
58  void readMetaDataFromJson();
59 
61  void writeMetaDataToJson();
62 
66  virtual FieldID write(const StorageView& storageView, const std::string& fieldID,
67  const std::shared_ptr<FieldMetainfoImpl> info) override;
68 
69  virtual void read(StorageView& storageView, const FieldID& fieldID,
70  std::shared_ptr<FieldMetainfoImpl> info) const override;
71 
72  virtual void updateMetaData() override;
73 
74  virtual OpenModeKind mode() const override { return mode_; }
75 
76  virtual std::string directory() const override { return directory_.string(); }
77 
78  virtual std::string prefix() const override { return prefix_; }
79 
80  virtual std::string name() const override { return NetCDFArchive::Name; }
81 
82  virtual std::string metaDataFile() const override { return metaDatafile_.string(); }
83 
84  virtual std::ostream& toStream(std::ostream& stream) const override;
85 
86  virtual void clear() override;
87 
88  virtual bool isReadingThreadSafe() const override { return false; }
89 
90  virtual bool isWritingThreadSafe() const override { return false; }
91 
92  virtual bool isSlicedReadingSupported() const override { return false; }
93 
95 
97  static std::unique_ptr<Archive> create(OpenModeKind mode, const std::string& directory,
98  const std::string& prefix);
99 
106  static void writeToFile(std::string filename, const StorageView& storageView,
107  const std::string& field);
108 
116  static void readFromFile(std::string filename, StorageView& storageView,
117  const std::string& field);
118 
119 private:
120  OpenModeKind mode_;
121  filesystem::path directory_;
122  std::string prefix_;
123  filesystem::path metaDatafile_;
124 
125  std::unordered_map<std::string, int> fieldMap_;
126  json::json json_;
127 };
128 
129 } // namespace serialbox
130 
131 #endif // SERIALBOX_HAS_NETCDF
132 
133 #endif
virtual void updateMetaData() override
Update the meta-data on disk.
virtual bool isSlicedReadingSupported() const override
Indicate whether the archive supports StorageViews with attached slices.
Definition: NetCDFArchive.h:92
virtual OpenModeKind mode() const override
Open-policy of the archive.
Definition: NetCDFArchive.h:74
static const int Version
Revision of the NetCDF archive.
Definition: NetCDFArchive.h:41
virtual FieldID write(const StorageView &storageView, const std::string &fieldID, const std::shared_ptr< FieldMetainfoImpl > info) override
Write the field given by storageView to disk.
virtual void clear() override
Clear the archive i.e remove all data from disk and reset the internal data-structures.
virtual std::string directory() const override
Directory to write/read files.
Definition: NetCDFArchive.h:76
virtual void read(StorageView &storageView, const FieldID &fieldID, std::shared_ptr< FieldMetainfoImpl > info) const override
Read the field identified by fieldID and given by storageView from disk.
static const std::string Name
Name of the NetCDF archive.
Definition: NetCDFArchive.h:38
Represent a mutable view to a multi-dimensional storage.
Definition: StorageView.h:33
Namespace of the serialbox library.
Definition: Archive.h:25
static void writeToFile(std::string filename, const StorageView &storageView, const std::string &field)
Directly write field (given by storageView) to file.
void writeMetaDataToJson()
Convert meta-data to JSON and serialize to file.
Uniquely identifiy a field.
Definition: FieldID.h:27
virtual std::ostream & toStream(std::ostream &stream) const override
Convert the archive to stream.
virtual std::string name() const override
Name of the archive.
Definition: NetCDFArchive.h:80
Archive based on NetCDF.
Definition: NetCDFArchive.h:35
virtual bool isReadingThreadSafe() const override
Indicate whether it&#39;s safe for multiple threads to call Archive::read.
Definition: NetCDFArchive.h:88
void readMetaDataFromJson()
Load meta-data from JSON file.
static void readFromFile(std::string filename, StorageView &storageView, const std::string &field)
Directly read field (given by storageView) from file.
OpenModeKind
Policy for opening files in the Serializer and Archive.
Definition: Type.h:40
NetCDFArchive(OpenModeKind mode, const std::string &directory, const std::string &prefix)
Initialize the archive.
static std::unique_ptr< Archive > create(OpenModeKind mode, const std::string &directory, const std::string &prefix)
Create a NetCDFArchive.
virtual std::string metaDataFile() const override
Full file path to the meta-data file.
Definition: NetCDFArchive.h:82
virtual std::string prefix() const override
Prefix of all files.
Definition: NetCDFArchive.h:78
virtual bool isWritingThreadSafe() const override
Indicate whether it&#39;s safe for multiple threads to call Archive::write.
Definition: NetCDFArchive.h:90
Abstract interface for Archives.
Definition: Archive.h:30