LOOS 4.1.0
The Lightweight Object Oriented Structural analysis library/toolkit
Loading...
Searching...
No Matches
trajwriter.hpp
1/*
2 This file is part of LOOS.
3
4 LOOS (Lightweight Object-Oriented Structure library)
5 Copyright (c) 2014, Tod D. Romo, Alan Grossfield
6 Department of Biochemistry and Biophysics
7 School of Medicine & Dentistry, University of Rochester
8
9 This package (LOOS) is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation under version 3 of the License.
12
13 This package is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22
23#if !defined(LOOS_TRAJWRITER_HPP)
24#define LOOS_TRAJWRITER_HPP
25
26#include <iostream>
27#include <string>
28#include <stdexcept>
29
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
33
34
35#include <loos_defs.hpp>
36#include <AtomicGroup.hpp>
37#include <exceptions.hpp>
38
39
40namespace loos {
41
42
44
54 public:
55
57 TrajectoryWriter(const std::string& fname, const bool append = false)
58 : _filename(fname), appending_(false) {
59 struct stat statbuf;
60
61 if (append && !stat(fname.c_str(), &statbuf))
62 openStream(fname, true);
63 else
64 openStream(fname);
65 }
66
67
69
75 TrajectoryWriter(std::iostream* s, const bool append = false)
76 : _filename("stream"), appending_(append), delete_(false) {}
77
78
79 virtual ~TrajectoryWriter() {
80 if (delete_)
81 delete stream_;
82 }
83
84
86 virtual void setComments(const std::vector<std::string>& comments) { }
87
89 virtual void setComments(const std::string& s) {
90 std::vector<std::string> c(1, s);
91 setComments(c);
92 }
93
95 virtual void writeFrame(const AtomicGroup& model) =0;
96
98
102 virtual void writeFrame(const AtomicGroup& model, const uint step, const double time) {
103 writeFrame(model);
104 }
105
107 virtual bool hasFrameStep() const { return(false); }
108
110 virtual bool hasFrameTime() const { return(false); }
111
113 virtual bool hasComments() const { return(false); }
114
116
120 virtual uint framesWritten() const =0;
121
123 bool isAppending() const { return(appending_); }
124
125 protected:
126 std::iostream* stream_;
127 std::string _filename;
128 bool appending_;
129 bool delete_;
130
131 private:
132
133
134 // Handle opening up a stream to a file... If it exists and we
135 // are asked to append, seek to the end of the file.
136
137 void openStream(const std::string& fname, const bool append = false) {
138
139
140
141 std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary;
142 if (append)
143 mode |= std::ios_base::in;
144 else
145 mode |= std::ios_base::trunc;
146
147 stream_ = new std::fstream(fname.c_str(), mode);
148 if (append) {
149 stream_->seekp(0, std::ios_base::end);
150 // Check to see if file is empty...
151 if (stream_->tellp() == 0)
152 appending_ = false;
153 else
154 appending_ = true;
155 }
156
157 if (!stream_->good())
158 throw(FileOpenError(fname, "Error while opening output trajectory file"));
159
160
161 delete_ = true; // Delete the stream pointer when dtor called
162 }
163
164
165 };
166
167
168
169}
170
171
172
173
174#endif
Class for handling groups of Atoms (pAtoms, actually)
Definition AtomicGroup.hpp:108
Base class for writing trajectories.
Definition trajwriter.hpp:53
virtual bool hasFrameTime() const
Can format write time on a per-frame basis?
Definition trajwriter.hpp:110
virtual uint framesWritten() const =0
Total frames in output file.
virtual bool hasFrameStep() const
Can format write step on a per-frame basis?
Definition trajwriter.hpp:107
virtual void setComments(const std::vector< std::string > &comments)
Set comments in metadata (not all formats support)
Definition trajwriter.hpp:86
virtual void setComments(const std::string &s)
Set comment in metadata (not all formats support)
Definition trajwriter.hpp:89
virtual void writeFrame(const AtomicGroup &model)=0
Wirte a single frame.
bool isAppending() const
Returns true if appending to an existing trajectory.
Definition trajwriter.hpp:123
virtual bool hasComments() const
Does format support comments in metadata?
Definition trajwriter.hpp:113
virtual void writeFrame(const AtomicGroup &model, const uint step, const double time)
Write a single frame specifying the step and timepoint.
Definition trajwriter.hpp:102
TrajectoryWriter(std::iostream *s, const bool append=false)
Write a trajectory to a stream.
Definition trajwriter.hpp:75
TrajectoryWriter(const std::string &fname, const bool append=false)
Write a trajectory to a file, optionally appending.
Definition trajwriter.hpp:57
Namespace for most things not already encapsulated within a class.
Definition version.cpp:3