OpenZGY/C++ API and Internals (ALPHA)
Access seismic data stored in ZGY format.
meta.h
Go to the documentation of this file.
1 // Copyright 2017-2020, Schlumberger
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
79 #pragma once
80 
81 #include "../declspec.h"
82 #include "../exception.h"
83 #include "file.h"
84 #include "enum.h"
85 #include "lookuptable.h"
86 
87 #include <cstdint>
88 #include <memory>
89 #include <ostream>
90 #include <sstream>
91 #include <iomanip>
92 #include <vector>
93 #include <cstring>
94 
95 namespace InternalZGY {
96 #if 0
97 }
98 #endif
99 
127 {
128 public:
129  std::string filename;
130  std::array<std::int64_t,3> size;
131  std::array<std::int64_t,3> bricksize;
132  RawDataType datatype;
133  std::array<float,2> datarange;
134  RawVerticalDimension zunitdim;
135  RawHorizontalDimension hunitdim;
136  std::string zunitname, hunitname;
137  double zunitfactor, hunitfactor;
138  float zstart, zinc;
139  std::array<float,2> annotstart, annotinc;
140  std::array<std::array<double,2>,4> corners;
141 
142 public:
144  : filename("")
145  , size{0,0,0}
146  , bricksize{64,64,64}
147  , datatype(RawDataType::Float32)
148  , datarange{0, -1}
149  , zunitdim(RawVerticalDimension::Unknown)
150  , hunitdim(RawHorizontalDimension::Unknown)
151  , zunitname("")
152  , hunitname("")
153  , zunitfactor(1.0)
154  , hunitfactor(1.0)
155  , zstart(0)
156  , zinc(0)
157  , annotstart{0,0}
158  , annotinc{0,0}
159  , corners{0,0,0,0,0,0,0,0}
160  {
161  }
162 };
163 
164 class IInfoHeaderAccess;
165 class IHistHeaderAccess;
166 
180 {
181 public:
182  typedef std::vector<std::uint8_t> podbytes_t;
183  virtual podbytes_t podbytes() const = 0;
184  virtual void dump(std::ostream& out, const std::string& prefix = "") = 0;
185  // read(), byteswap(), calculate() not useful to abstract at this level.
186 };
187 
189 // FileHeader //////////////////////////////////////////////////////////
191 
193 {
194 public:
195  virtual void read(const std::shared_ptr<FileADT>& file, std::int64_t offset) = 0;
196  virtual void byteswap() = 0;
197 public:
198  virtual std::array<std::uint8_t,4> magic() const = 0;
199  virtual std::uint32_t version() const = 0;
200 };
201 
203 // OffsetHeader ////////////////////////////////////////////////////////
205 
207 {
208 public:
209  virtual void read(const std::shared_ptr<FileADT>& file, std::int64_t offset) = 0;
210  virtual void byteswap() = 0;
211  virtual void calculate(const std::shared_ptr<IInfoHeaderAccess>& ih) = 0;
212 public:
213  virtual std::int64_t infoff() const = 0;
214  virtual std::int64_t stroff() const = 0;
215  virtual std::int64_t alphalupoff() const = 0;
216  virtual std::int64_t bricklupoff() const = 0;
217  virtual std::int64_t histoff() const = 0;
218  virtual std::int64_t infsize() const = 0;
219  virtual std::int64_t histsize() const = 0;
220  virtual std::int64_t alphalupsize() const = 0;
221  virtual std::int64_t bricklupsize() const = 0;
222 };
223 
225 // InfoHeader //////////////////////////////////////////////////////////
227 
229 {
230 public:
231  virtual void read(const std::shared_ptr<FileADT>& file, std::int64_t offset, std::int64_t size) = 0;
232  virtual void byteswap() = 0;
233  virtual void calculate_cache() = 0;
234  virtual void calculate_read(const podbytes_t& slbuf, const std::shared_ptr<IHistHeaderAccess>& hh) = 0;
235  virtual podbytes_t calculate_write() = 0;
236 public:
237  virtual std::array<std::int64_t,3> bricksize() const = 0;
238  virtual std::array<float,2> codingrange() const = 0;
239  virtual std::array<std::uint8_t,16>dataid() const = 0;
240  virtual std::array<std::uint8_t,16>verid() const = 0;
241  virtual std::array<std::uint8_t,16>previd() const = 0;
242  virtual std::string srcname() const = 0;
243  virtual std::string srcdesc() const = 0;
244  virtual RawDataType srctype() const = 0;
245  virtual std::array<std::int64_t,3> size() const = 0;
246  virtual std::array<float,3> orig() const = 0;
247  virtual std::array<float,3> inc() const = 0;
248  virtual std::int64_t scnt() const = 0;
249  virtual double ssum() const = 0;
250  virtual double sssq() const = 0;
251  virtual float smin() const = 0;
252  virtual float smax() const = 0;
253  virtual std::array<float,4> gpiline() const = 0;
254  virtual std::array<float,4> gpxline() const = 0;
255  virtual std::array<double,4> gpx() const = 0;
256  virtual std::array<double,4> gpy() const = 0;
257  virtual RawDataType datatype() const = 0;
258  virtual std::string hprjsys() const = 0;
259  virtual RawHorizontalDimension hdim() const = 0;
260  virtual double hunitfactor() const = 0;
261  virtual std::string hunitname() const = 0;
262  virtual RawVerticalDimension vdim() const = 0;
263  virtual double vunitfactor() const = 0;
264  virtual std::string vunitname() const = 0;
265  virtual std::uint32_t slbufsize() const = 0;
266  virtual const std::array<std::array<double,2>,4>& ocp_index() const = 0;
267  virtual const std::array<std::array<double,2>,4>& ocp_annot() const = 0;
268  virtual const std::array<std::array<double,2>,4>& ocp_world() const = 0;
269  virtual std::int32_t nlods() const = 0;
270  // The next three are derived but cannot possibly change after being set,
271  // so returning a reference to cached data shold be safe.
272  virtual const std::vector<std::array<std::int64_t,3>>& lodsizes() const = 0;
273  virtual const std::vector<std::int64_t>& alphaoffsets() const = 0;
274  virtual const std::vector<std::int64_t>& brickoffsets() const = 0;
275  // More derived stuff, TODO-Performance may be cached.
276  virtual std::int64_t bytesperalpha() const = 0;
277  virtual std::int64_t bytesperbrick() const = 0;
278  virtual std::int64_t bytespersample() const = 0;
279  virtual std::array<double,2> storagetofloat() const = 0;
280  virtual double storagetofloat_slope() const = 0;
281  virtual double storagetofloat_intercept() const = 0;
282  virtual double defaultstorage() const = 0;
283  virtual double defaultvalue() const = 0;
284  // Write support.
285  virtual void setstats(std::int64_t scnt, double ssum, double sssq,
286  float smin, float smax) = 0;
287 };
288 
290 // HistHeader //////////////////////////////////////////////////////////
292 
294 {
295 public:
296  virtual void read(const std::shared_ptr<FileADT>& file, std::int64_t offset, std::int64_t size) = 0;
297  virtual void byteswap() = 0;
298  virtual void calculate() = 0;
299 public:
300  virtual std::int64_t bincount() const = 0;
301  virtual std::int64_t samplecount() const = 0;
302  virtual double minvalue() const = 0;
303  virtual double maxvalue() const = 0;
304  virtual const std::int64_t* bins() const = 0;
305  // Write support.
306  virtual void sethisto(double minvalue, double maxvalue,
307  const std::int64_t* bins, std::int64_t bincount) = 0;
308 };
309 
311 // AlphaLUP, BrickLUP //////////////////////////////////////////////////
313 
315 {
316 public:
317  virtual void read(const std::shared_ptr<FileADT>& file, std::int64_t offset, std::int64_t size) = 0;
318  virtual void byteswap() = 0;
319 public:
320  virtual std::uint64_t lookupLinearIndex(std::int64_t index) const = 0;
321  virtual std::vector<std::uint64_t>& lup() = 0;
322  virtual std::vector<std::uint64_t>& lupend() = 0;
323  virtual const std::vector<std::uint64_t>& lup() const = 0;
324  virtual const std::vector<std::uint64_t>& lupend() const = 0;
325 };
326 
328 // HeaderAccessFactory (for all known header types) //////////////////
330 
332 {
333  HeaderAccessFactory() = delete;
334  HeaderAccessFactory(const HeaderAccessFactory&) = delete;
335 public:
336  static std::shared_ptr<IFileHeaderAccess> createFileHeader();
337  static std::shared_ptr<IOffsetHeaderAccess> createOffsetHeader(std::uint32_t);
338  static std::shared_ptr<IInfoHeaderAccess> createInfoHeader(std::uint32_t);
339  static std::shared_ptr<IHistHeaderAccess> createHistHeader(std::uint32_t);
340  static std::shared_ptr<ILookupTableAccess> createAlphaLookup(std::uint32_t);
341  static std::shared_ptr<ILookupTableAccess> createBrickLookup(std::uint32_t);
342 };
343 
345 // ZgyInternalMeta (this is the top level) ///////////////////////////
347 
351 class OPENZGY_TEST_API ZgyInternalMeta
352 {
353 public:
354  typedef std::function<bool(int, const std::string&)> LoggerFn;
355 
356 private:
357  std::shared_ptr<IFileHeaderAccess> _fh;
358  std::shared_ptr<IOffsetHeaderAccess> _oh;
359  std::shared_ptr<IInfoHeaderAccess> _ih;
360  std::shared_ptr<IHistHeaderAccess> _hh;
361  std::shared_ptr<ILookupTableAccess> _alup;
362  std::shared_ptr<ILookupTableAccess> _blup;
363  LoggerFn _loggerfn;
364  bool _is_bad;
365 
366 public:
367  // Actually internal. Used by OpenZGY::ZgyMeta, ZgyInternalBulk, GenLodC.
368  const IFileHeaderAccess& fh() const { return *_fh; }
369  //const IOffsetHeaderAccess& oh() const { return *_oh; }
370  const IInfoHeaderAccess& ih() const { return *_ih; }
371  const IHistHeaderAccess& hh() const { return *_hh; }
372  const ILookupTableAccess& alup() const { return *_alup; }
373  const ILookupTableAccess& blup() const { return *_blup; }
374  // Writing a file needs writable lookup tables and
375  // writable statistics and histogram. Stats are inside
376  // the info header so this grants a bit more than needed.
377  IInfoHeaderAccess& ih() { return *_ih; }
378  IHistHeaderAccess& hh() { return *_hh; }
379  ILookupTableAccess& alup() { return *_alup; }
380  ILookupTableAccess& blup() { return *_blup; }
381 
382 public:
383  ZgyInternalMeta();
384  ZgyInternalMeta(const ZgyInternalMeta&) = delete;
385  ZgyInternalMeta& operator=(const ZgyInternalMeta&) = delete;
386  explicit ZgyInternalMeta(const std::shared_ptr<FileADT>& file,
387  const LoggerFn& logger = LoggerFn());
388  explicit ZgyInternalMeta(const ZgyInternalWriterArgs& args,
389  const LoggerFn& logger = LoggerFn());
390 public:
391  void dump(std::ostream& out, const std::string& prefix = "");
392  bool errorflag() const { return _is_bad; }
393  void set_errorflag(bool flag) { _is_bad = flag; }
394  LoggerFn set_logger(const LoggerFn& logger) {
395  LoggerFn old = _loggerfn;
396  _loggerfn = logger;
397  return old;
398  }
399  void flushMeta(const std::shared_ptr<FileADT>& file);
400 private:
401  bool _logger(int priority, const std::string& ss = std::string()) const;
402  bool _logger(int priority, const std::ios& ss) const;
403  void initFromOpenFile(const std::shared_ptr<FileADT>&);
404  void initFromScratch(const ZgyInternalWriterArgs&);
405  ZgyInternalWriterArgs validateCreateNewFile(const ZgyInternalWriterArgs&);
406  class ErrorsWillCorruptFile;
407 };
408 
409 } // namespace
InternalZGY::ZgyInternalMeta
Definition: meta.h:351
InternalZGY::IHistHeaderAccess
Definition: meta.h:293
lookuptable.h
Static methods to assist working with lookup tables.
InternalZGY::IOffsetHeaderAccess
Definition: meta.h:206
InternalZGY::ZgyInternalWriterArgs
Internal counterpart to OpenZGY::ZgyWriterArgs.
Definition: meta.h:126
InternalZGY::HeaderAccessFactory
Definition: meta.h:331
enum.h
enums and type aliases not visible to the public API.
InternalZGY::IFileHeaderAccess
Definition: meta.h:192
InternalZGY::RawHorizontalDimension
RawHorizontalDimension
Definition: enum.h:129
InternalZGY::RawDataType
RawDataType
Definition: enum.h:95
InternalZGY::IInfoHeaderAccess
Definition: meta.h:228
file.h
Low level I/O, abstract layer.
InternalZGY::ZgyInternalMeta::ErrorsWillCorruptFile
Definition: meta.cpp:1524
InternalZGY::ILookupTableAccess
Definition: meta.h:314
InternalZGY
Implementation not visible to clients.
InternalZGY::RawVerticalDimension
RawVerticalDimension
Definition: enum.h:142
InternalZGY::IHeaderAccess
Definition: meta.h:179