OpenZGY/C++ API and Internals (ALPHA)
Access seismic data stored in ZGY format.
bulk.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 
15 #pragma once
16 
17 #include "../declspec.h"
18 #include "enum.h"
19 
20 #include <memory>
21 #include <functional>
22 #include <array>
23 #include <vector>
24 #include <tuple>
25 #include <sstream>
26 
32 namespace Test { class TestBulk; }
33 namespace InternalZGY {
34 #if 0
35 }
36 #endif
37 
38 class FileADT;
39 class ZgyInternalMeta;
40 class DataBuffer;
41 struct IJK;
42 struct LutInfoEx;
43 
48 class OPENZGY_TEST_API ZgyInternalBulk
49 {
50  friend class Test::TestBulk;
51 public:
52  typedef std::function<bool(int, const std::string&)> LoggerFn;
53 
54 private:
56  std::shared_ptr<FileADT> _file;
57  std::shared_ptr<ZgyInternalMeta> _metadata;
58  UpdateMode _update_mode;
59  bool _compressed_write; // If true: do not align bricks.
60  bool _is_bad; // If true: instance not usable.
61  double _written_sample_min;
62  double _written_sample_max;
63  LoggerFn _loggerfn;
64 
65 public:
67  const std::shared_ptr<FileADT>& file,
68  const std::shared_ptr<ZgyInternalMeta>& metadata,
69  bool compressed_write,
70  const LoggerFn& logger = LoggerFn());
71 
72  std::pair<bool,double> readConstantValue(
73  const std::array<std::int64_t,3>& start,
74  const std::array<std::int64_t,3>& size,
75  int32_t lod, bool as_float);
76 
77  void readToExistingBuffer(
78  const std::shared_ptr<DataBuffer>& data,
79  const std::array<std::int64_t,3>& start,
80  int32_t lod, bool as_float);
81 
82  std::shared_ptr<DataBuffer> readToNewBuffer(
83  const std::array<std::int64_t,3>& start,
84  const std::array<std::int64_t,3>& size,
85  int32_t lod, bool as_float, bool check_constant);
86 
87  void writeRegion(
88  const std::shared_ptr<DataBuffer>& data,
89  const std::array<std::int64_t,3>& start,
90  int32_t lod, bool is_storage,
91  const compressor_t& compressor);
92 
93 public: // actually internal
94 
95  bool errorflag() const { return _is_bad; }
96  void set_errorflag(bool flag) { _is_bad = flag; }
97  LoggerFn set_logger(const LoggerFn& logger) {
98  LoggerFn old = _loggerfn;
99  _loggerfn = logger;
100  return old;
101  }
102  std::array<double,2> valueRangeWritten() const {
103  return std::array<double,2>{_written_sample_min, _written_sample_max};
104  }
105 
106 private:
107 
108  bool _logger(int priority, const std::string& ss = std::string()) const;
109 
110  bool _logger(int priority, const std::ios& ss) const;
111 
112  void _validateUserPosition(
113  const std::array<std::int64_t,3>& start,
114  const std::array<std::int64_t,3>& size,
115  int32_t lod);
116 
117  std::shared_ptr<DataBuffer> _scaleDataToFloat(
118  const std::shared_ptr<DataBuffer>& data);
119 
120  std::shared_ptr<DataBuffer> _scaleDataToStorage(
121  const std::shared_ptr<DataBuffer>& data);
122 
123  static double _decodeConstant(std::uint32_t in, RawDataType dtype);
124 
125  static std::int32_t _encodeConstant(double in, RawDataType dtype);
126 
127  std::vector<LutInfoEx> _partsNeeded(
128  const std::array<std::int64_t,3>& start,
129  const std::array<std::int64_t,3>& size,
130  int32_t lod);
131 
132  void _deliverOneBrick(
133  const std::shared_ptr<DataBuffer>& result,
134  const std::array<std::int64_t,3>& start,
135  const std::array<std::int64_t,3>& startpos,
136  const void *raw, std::int64_t rawsize,
137  BrickStatus brickstatus, bool as_float);
138 
139  // --- WRITE SUPPORT ---
140 
141  std::array<std::int64_t,3> _usedPartOfBrick(
142  const std::array<std::int64_t,3>& size,
143  const std::array<std::int64_t,3>& brickpos,
144  std::int32_t lod);
145 
146  bool _isUsedPartOfBrickAllConstant(
147  const std::shared_ptr<DataBuffer>& data,
148  const std::array<std::int64_t,3>& brickpos,
149  int32_t lod);
150 
151  static void _setPaddingToEdge(
152  const std::shared_ptr<DataBuffer>& data,
153  const std::array<std::int64_t,3>& used,
154  int modulo, int dim);
155 
156  static void _setPaddingToConst(
157  const std::shared_ptr<DataBuffer>& data,
158  const std::array<std::int64_t,3>& used,
159  double missingvalue, int dim);
160 
161  static void _setPaddingSamples(
162  const std::shared_ptr<DataBuffer>& data,
163  const std::array<std::int64_t,3>& used,
164  double missingvalue, const compressor_t& compressor);
165 
166  void _writeWithRetry(
167  rawdata_t rawdata,
168  BrickStatus brickstatus,
169  std::int64_t fileoffset,
170  const std::array<std::int64_t,3>& brickpos,
171  std::int32_t lod);
172 
173  void _writeOneNormalBrick(
174  const std::shared_ptr<DataBuffer>& data,
175  std::int64_t fileoffset,
176  const std::array<std::int64_t,3>& brickpos,
177  std::int32_t lod,
178  const compressor_t& compressor);
179 
180  void _writeOneConstantBrick(
181  const std::shared_ptr<DataBuffer>& data,
182  const std::array<std::int64_t,3>& brickpos,
183  std::int32_t lod);
184 
185  bool _mustLeakOldBrick(
186  const std::shared_ptr<DataBuffer>& data,
187  const compressor_t& compressor,
188  BrickStatus brickstatus);
189 
190  void _writeOneBrick(
191  const std::shared_ptr<DataBuffer>& data,
192  const std::array<std::int64_t,3>& brickpos,
193  std::int32_t lod,
194  const compressor_t& compressor);
195 
196  void _writeAlignedRegion(
197  const std::shared_ptr<DataBuffer>& data,
198  const std::array<std::int64_t,3>& start,
199  std::int32_t lod,
200  const compressor_t& compressor);
201 };
202 
203 } // namespace
InternalZGY::ZgyInternalBulk
Definition: bulk.h:48
enum.h
enums and type aliases not visible to the public API.
InternalZGY::rawdata_t
std::pair< std::shared_ptr< const void >, std::int64_t > rawdata_t
Shared data plus size. No other information.
Definition: enum.h:71
InternalZGY::UpdateMode
UpdateMode
Definition: enum.h:212
InternalZGY::RawDataType
RawDataType
Definition: enum.h:95
InternalZGY::ZgyInternalBulk::ErrorsWillCorruptFile
Definition: bulk.cpp:189
InternalZGY::compressor_t
std::function< rawdata_t(const rawdata_t &, const index3_t &)> compressor_t
Function for compressing a brick.
Definition: enum.h:84
InternalZGY
Implementation not visible to clients.
InternalZGY::BrickStatus
BrickStatus
Definition: enum.h:167