/src/gdal/frmts/zarr/zarr_v2_array.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL |
4 | | * Purpose: Zarr driver |
5 | | * Author: Even Rouault <even dot rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2021, Even Rouault <even dot rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "cpl_float.h" |
14 | | #include "cpl_vsi_virtual.h" |
15 | | #include "cpl_worker_thread_pool.h" |
16 | | #include "gdal_thread_pool.h" |
17 | | #include "zarr.h" |
18 | | #include "vsikerchunk.h" |
19 | | |
20 | | #include "netcdf_cf_constants.h" // for CF_UNITS, etc |
21 | | |
22 | | #include <algorithm> |
23 | | #include <cassert> |
24 | | #include <cstdlib> |
25 | | #include <limits> |
26 | | #include <map> |
27 | | #include <set> |
28 | | |
29 | | /************************************************************************/ |
30 | | /* ZarrV2Array::ZarrV2Array() */ |
31 | | /************************************************************************/ |
32 | | |
33 | | ZarrV2Array::ZarrV2Array( |
34 | | const std::shared_ptr<ZarrSharedResource> &poSharedResource, |
35 | | const std::shared_ptr<ZarrGroupBase> &poParent, const std::string &osName, |
36 | | const std::vector<std::shared_ptr<GDALDimension>> &aoDims, |
37 | | const GDALExtendedDataType &oType, const std::vector<DtypeElt> &aoDtypeElts, |
38 | | const std::vector<GUInt64> &anBlockSize, bool bFortranOrder) |
39 | 215 | : GDALAbstractMDArray(poParent->GetFullName(), osName), |
40 | 215 | ZarrArray(poSharedResource, poParent, osName, aoDims, oType, aoDtypeElts, |
41 | 215 | anBlockSize, anBlockSize), |
42 | 215 | m_bFortranOrder(bFortranOrder) |
43 | 215 | { |
44 | 215 | m_oCompressorJSon.Deinit(); |
45 | 215 | } Unexecuted instantiation: ZarrV2Array::ZarrV2Array(std::__1::shared_ptr<ZarrSharedResource> const&, std::__1::shared_ptr<ZarrGroupBase> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<GDALDimension>, std::__1::allocator<std::__1::shared_ptr<GDALDimension> > > const&, GDALExtendedDataType const&, std::__1::vector<DtypeElt, std::__1::allocator<DtypeElt> > const&, std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> > const&, bool) ZarrV2Array::ZarrV2Array(std::__1::shared_ptr<ZarrSharedResource> const&, std::__1::shared_ptr<ZarrGroupBase> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<GDALDimension>, std::__1::allocator<std::__1::shared_ptr<GDALDimension> > > const&, GDALExtendedDataType const&, std::__1::vector<DtypeElt, std::__1::allocator<DtypeElt> > const&, std::__1::vector<unsigned long long, std::__1::allocator<unsigned long long> > const&, bool) Line | Count | Source | 39 | 215 | : GDALAbstractMDArray(poParent->GetFullName(), osName), | 40 | 215 | ZarrArray(poSharedResource, poParent, osName, aoDims, oType, aoDtypeElts, | 41 | 215 | anBlockSize, anBlockSize), | 42 | 215 | m_bFortranOrder(bFortranOrder) | 43 | 215 | { | 44 | 215 | m_oCompressorJSon.Deinit(); | 45 | 215 | } |
|
46 | | |
47 | | /************************************************************************/ |
48 | | /* ZarrV2Array::Create() */ |
49 | | /************************************************************************/ |
50 | | |
51 | | std::shared_ptr<ZarrV2Array> ZarrV2Array::Create( |
52 | | const std::shared_ptr<ZarrSharedResource> &poSharedResource, |
53 | | const std::shared_ptr<ZarrGroupBase> &poParent, const std::string &osName, |
54 | | const std::vector<std::shared_ptr<GDALDimension>> &aoDims, |
55 | | const GDALExtendedDataType &oType, const std::vector<DtypeElt> &aoDtypeElts, |
56 | | const std::vector<GUInt64> &anBlockSize, bool bFortranOrder) |
57 | 215 | { |
58 | 215 | auto arr = std::shared_ptr<ZarrV2Array>( |
59 | 215 | new ZarrV2Array(poSharedResource, poParent, osName, aoDims, oType, |
60 | 215 | aoDtypeElts, anBlockSize, bFortranOrder)); |
61 | 215 | if (arr->m_nTotalInnerChunkCount == 0) |
62 | 0 | return nullptr; |
63 | 215 | arr->SetSelf(arr); |
64 | | |
65 | 215 | return arr; |
66 | 215 | } |
67 | | |
68 | | /************************************************************************/ |
69 | | /* ~ZarrV2Array() */ |
70 | | /************************************************************************/ |
71 | | |
72 | | ZarrV2Array::~ZarrV2Array() |
73 | 215 | { |
74 | 215 | ZarrV2Array::Flush(); |
75 | 215 | } |
76 | | |
77 | | /************************************************************************/ |
78 | | /* Flush() */ |
79 | | /************************************************************************/ |
80 | | |
81 | | bool ZarrV2Array::Flush() |
82 | 1.32k | { |
83 | 1.32k | if (!m_bValid) |
84 | 0 | return true; |
85 | | |
86 | 1.32k | bool ret = ZarrV2Array::FlushDirtyBlock(); |
87 | | |
88 | 1.32k | m_anCachedBlockIndices.clear(); |
89 | | |
90 | 1.32k | if (m_bDefinitionModified) |
91 | 215 | { |
92 | 215 | if (!Serialize()) |
93 | 0 | ret = false; |
94 | 215 | m_bDefinitionModified = false; |
95 | 215 | } |
96 | | |
97 | 1.32k | CPLJSONArray j_ARRAY_DIMENSIONS; |
98 | 1.32k | bool bDimensionsModified = false; |
99 | 1.32k | if (!m_aoDims.empty()) |
100 | 1.32k | { |
101 | 1.32k | for (const auto &poDim : m_aoDims) |
102 | 2.72k | { |
103 | 2.72k | const auto poZarrDim = |
104 | 2.72k | dynamic_cast<const ZarrDimension *>(poDim.get()); |
105 | 2.72k | if (poZarrDim && poZarrDim->IsXArrayDimension()) |
106 | 2.72k | { |
107 | 2.72k | if (poZarrDim->IsModified()) |
108 | 0 | bDimensionsModified = true; |
109 | 2.72k | j_ARRAY_DIMENSIONS.Add(poDim->GetName()); |
110 | 2.72k | } |
111 | 0 | else |
112 | 0 | { |
113 | 0 | j_ARRAY_DIMENSIONS = CPLJSONArray(); |
114 | 0 | break; |
115 | 0 | } |
116 | 2.72k | } |
117 | 1.32k | } |
118 | | |
119 | 1.32k | if (m_oAttrGroup.IsModified() || bDimensionsModified || |
120 | 1.15k | (m_bNew && j_ARRAY_DIMENSIONS.Size() != 0) || m_bUnitModified || |
121 | 1.02k | m_bOffsetModified || m_bScaleModified || m_bSRSModified) |
122 | 291 | { |
123 | 291 | m_bNew = false; |
124 | | |
125 | 291 | auto oAttrs = SerializeSpecialAttributes(); |
126 | | |
127 | 291 | if (j_ARRAY_DIMENSIONS.Size() != 0) |
128 | 291 | { |
129 | 291 | oAttrs.Delete("_ARRAY_DIMENSIONS"); |
130 | 291 | oAttrs.Add("_ARRAY_DIMENSIONS", j_ARRAY_DIMENSIONS); |
131 | 291 | } |
132 | | |
133 | 291 | CPLJSONDocument oDoc; |
134 | 291 | oDoc.SetRoot(oAttrs); |
135 | 291 | const std::string osAttrFilename = |
136 | 291 | CPLFormFilenameSafe(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(), |
137 | 291 | ".zattrs", nullptr); |
138 | 291 | if (!oDoc.Save(osAttrFilename)) |
139 | 0 | ret = false; |
140 | 291 | m_poSharedResource->SetZMetadataItem(osAttrFilename, oAttrs); |
141 | 291 | } |
142 | | |
143 | 1.32k | return ret; |
144 | 1.32k | } |
145 | | |
146 | | /************************************************************************/ |
147 | | /* StripUselessItemsFromCompressorConfiguration() */ |
148 | | /************************************************************************/ |
149 | | |
150 | | static void StripUselessItemsFromCompressorConfiguration(CPLJSONObject &o) |
151 | 0 | { |
152 | 0 | if (o.GetType() == CPLJSONObject::Type::Object) |
153 | 0 | { |
154 | 0 | o.Delete("num_threads"); // Blosc |
155 | 0 | o.Delete("typesize"); // Blosc |
156 | 0 | o.Delete("header"); // LZ4 |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | /************************************************************************/ |
161 | | /* ZarrV2Array::Serialize() */ |
162 | | /************************************************************************/ |
163 | | |
164 | | bool ZarrV2Array::Serialize() |
165 | 215 | { |
166 | 215 | CPLJSONDocument oDoc; |
167 | 215 | CPLJSONObject oRoot = oDoc.GetRoot(); |
168 | | |
169 | 215 | CPLJSONArray oChunks; |
170 | 215 | for (const auto nBlockSize : m_anOuterBlockSize) |
171 | 404 | { |
172 | 404 | oChunks.Add(static_cast<GInt64>(nBlockSize)); |
173 | 404 | } |
174 | 215 | oRoot.Add("chunks", oChunks); |
175 | | |
176 | 215 | if (m_oCompressorJSon.IsValid()) |
177 | 0 | { |
178 | 0 | oRoot.Add("compressor", m_oCompressorJSon); |
179 | 0 | CPLJSONObject compressor = oRoot["compressor"]; |
180 | 0 | StripUselessItemsFromCompressorConfiguration(compressor); |
181 | 0 | } |
182 | 215 | else |
183 | 215 | { |
184 | 215 | oRoot.AddNull("compressor"); |
185 | 215 | } |
186 | | |
187 | 215 | if (m_dtype.GetType() == CPLJSONObject::Type::Object) |
188 | 215 | oRoot.Add("dtype", m_dtype["dummy"]); |
189 | 0 | else |
190 | 0 | oRoot.Add("dtype", m_dtype); |
191 | | |
192 | 215 | if (m_pabyNoData == nullptr) |
193 | 214 | { |
194 | 214 | oRoot.AddNull("fill_value"); |
195 | 214 | } |
196 | 1 | else |
197 | 1 | { |
198 | 1 | switch (m_oType.GetClass()) |
199 | 1 | { |
200 | 1 | case GEDTC_NUMERIC: |
201 | 1 | { |
202 | 1 | SerializeNumericNoData(oRoot); |
203 | 1 | break; |
204 | 0 | } |
205 | | |
206 | 0 | case GEDTC_STRING: |
207 | 0 | { |
208 | 0 | char *pszStr; |
209 | 0 | char **ppszStr = reinterpret_cast<char **>(m_pabyNoData); |
210 | 0 | memcpy(&pszStr, ppszStr, sizeof(pszStr)); |
211 | 0 | if (pszStr) |
212 | 0 | { |
213 | 0 | const size_t nNativeSize = |
214 | 0 | m_aoDtypeElts.back().nativeOffset + |
215 | 0 | m_aoDtypeElts.back().nativeSize; |
216 | 0 | char *base64 = CPLBase64Encode( |
217 | 0 | static_cast<int>(std::min(nNativeSize, strlen(pszStr))), |
218 | 0 | reinterpret_cast<const GByte *>(pszStr)); |
219 | 0 | oRoot.Add("fill_value", base64); |
220 | 0 | CPLFree(base64); |
221 | 0 | } |
222 | 0 | else |
223 | 0 | { |
224 | 0 | oRoot.AddNull("fill_value"); |
225 | 0 | } |
226 | 0 | break; |
227 | 0 | } |
228 | | |
229 | 0 | case GEDTC_COMPOUND: |
230 | 0 | { |
231 | 0 | const size_t nNativeSize = m_aoDtypeElts.back().nativeOffset + |
232 | 0 | m_aoDtypeElts.back().nativeSize; |
233 | 0 | std::vector<GByte> nativeNoData(nNativeSize); |
234 | 0 | EncodeElt(m_aoDtypeElts, m_pabyNoData, &nativeNoData[0]); |
235 | 0 | char *base64 = CPLBase64Encode(static_cast<int>(nNativeSize), |
236 | 0 | nativeNoData.data()); |
237 | 0 | oRoot.Add("fill_value", base64); |
238 | 0 | CPLFree(base64); |
239 | 0 | } |
240 | 1 | } |
241 | 1 | } |
242 | | |
243 | 215 | if (m_oFiltersArray.Size() == 0) |
244 | 215 | oRoot.AddNull("filters"); |
245 | 0 | else |
246 | 0 | oRoot.Add("filters", m_oFiltersArray); |
247 | | |
248 | 215 | oRoot.Add("order", m_bFortranOrder ? "F" : "C"); |
249 | | |
250 | 215 | CPLJSONArray oShape; |
251 | 215 | for (const auto &poDim : m_aoDims) |
252 | 404 | { |
253 | 404 | oShape.Add(static_cast<GInt64>(poDim->GetSize())); |
254 | 404 | } |
255 | 215 | oRoot.Add("shape", oShape); |
256 | | |
257 | 215 | oRoot.Add("zarr_format", 2); |
258 | | |
259 | 215 | if (m_osDimSeparator != ".") |
260 | 0 | { |
261 | 0 | oRoot.Add("dimension_separator", m_osDimSeparator); |
262 | 0 | } |
263 | | |
264 | 215 | bool ret = oDoc.Save(m_osFilename); |
265 | | |
266 | 215 | m_poSharedResource->SetZMetadataItem(m_osFilename, oRoot); |
267 | | |
268 | 215 | return ret; |
269 | 215 | } |
270 | | |
271 | | /************************************************************************/ |
272 | | /* ZarrV2Array::NeedDecodedBuffer() */ |
273 | | /************************************************************************/ |
274 | | |
275 | | bool ZarrV2Array::NeedDecodedBuffer() const |
276 | 356 | { |
277 | 356 | const size_t nSourceSize = |
278 | 356 | m_aoDtypeElts.back().nativeOffset + m_aoDtypeElts.back().nativeSize; |
279 | 356 | if (m_oType.GetClass() == GEDTC_COMPOUND && |
280 | 0 | nSourceSize != m_oType.GetSize()) |
281 | 0 | { |
282 | 0 | return true; |
283 | 0 | } |
284 | 356 | else if (m_oType.GetClass() != GEDTC_STRING) |
285 | 356 | { |
286 | 356 | for (const auto &elt : m_aoDtypeElts) |
287 | 356 | { |
288 | 356 | if (elt.needByteSwapping || elt.gdalTypeIsApproxOfNative || |
289 | 356 | elt.nativeType == DtypeElt::NativeType::STRING_ASCII || |
290 | 356 | elt.nativeType == DtypeElt::NativeType::STRING_UNICODE) |
291 | 0 | { |
292 | 0 | return true; |
293 | 0 | } |
294 | 356 | } |
295 | 356 | } |
296 | 356 | return false; |
297 | 356 | } |
298 | | |
299 | | /************************************************************************/ |
300 | | /* ZarrV2Array::AllocateWorkingBuffers() */ |
301 | | /************************************************************************/ |
302 | | |
303 | | bool ZarrV2Array::AllocateWorkingBuffers() const |
304 | 9.31k | { |
305 | 9.31k | if (m_bAllocateWorkingBuffersDone) |
306 | 9.13k | return m_bWorkingBuffersOK; |
307 | | |
308 | 178 | m_bAllocateWorkingBuffersDone = true; |
309 | | |
310 | 178 | size_t nSizeNeeded = m_nInnerBlockSizeBytes; |
311 | 178 | if (m_bFortranOrder || m_oFiltersArray.Size() != 0) |
312 | 0 | { |
313 | 0 | if (nSizeNeeded > std::numeric_limits<size_t>::max() / 2) |
314 | 0 | { |
315 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too large chunk size"); |
316 | 0 | return false; |
317 | 0 | } |
318 | 0 | nSizeNeeded *= 2; |
319 | 0 | } |
320 | 178 | if (NeedDecodedBuffer()) |
321 | 0 | { |
322 | 0 | size_t nDecodedBufferSize = m_oType.GetSize(); |
323 | 0 | for (const auto &nBlockSize : m_anOuterBlockSize) |
324 | 0 | { |
325 | 0 | if (nDecodedBufferSize > std::numeric_limits<size_t>::max() / |
326 | 0 | static_cast<size_t>(nBlockSize)) |
327 | 0 | { |
328 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too large chunk size"); |
329 | 0 | return false; |
330 | 0 | } |
331 | 0 | nDecodedBufferSize *= static_cast<size_t>(nBlockSize); |
332 | 0 | } |
333 | 0 | if (nSizeNeeded > |
334 | 0 | std::numeric_limits<size_t>::max() - nDecodedBufferSize) |
335 | 0 | { |
336 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too large chunk size"); |
337 | 0 | return false; |
338 | 0 | } |
339 | 0 | nSizeNeeded += nDecodedBufferSize; |
340 | 0 | } |
341 | | |
342 | | // Reserve a buffer for tile content |
343 | 178 | if (nSizeNeeded > 1024 * 1024 * 1024 && |
344 | 0 | !CPLTestBool(CPLGetConfigOption("ZARR_ALLOW_BIG_TILE_SIZE", "NO"))) |
345 | 0 | { |
346 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
347 | 0 | "Zarr tile allocation would require " CPL_FRMT_GUIB " bytes. " |
348 | 0 | "By default the driver limits to 1 GB. To allow that memory " |
349 | 0 | "allocation, set the ZARR_ALLOW_BIG_TILE_SIZE configuration " |
350 | 0 | "option to YES.", |
351 | 0 | static_cast<GUIntBig>(nSizeNeeded)); |
352 | 0 | return false; |
353 | 0 | } |
354 | | |
355 | 178 | m_bWorkingBuffersOK = AllocateWorkingBuffers( |
356 | 178 | m_abyRawBlockData, m_abyTmpRawBlockData, m_abyDecodedBlockData); |
357 | 178 | return m_bWorkingBuffersOK; |
358 | 178 | } |
359 | | |
360 | | bool ZarrV2Array::AllocateWorkingBuffers( |
361 | | ZarrByteVectorQuickResize &abyRawBlockData, |
362 | | ZarrByteVectorQuickResize &abyTmpRawBlockData, |
363 | | ZarrByteVectorQuickResize &abyDecodedBlockData) const |
364 | 178 | { |
365 | | // This method should NOT modify any ZarrArray member, as it is going to |
366 | | // be called concurrently from several threads. |
367 | | |
368 | | // Set those #define to avoid accidental use of some global variables |
369 | 178 | #define m_abyTmpRawBlockData cannot_use_here |
370 | 178 | #define m_abyRawBlockData cannot_use_here |
371 | 178 | #define m_abyDecodedBlockData cannot_use_here |
372 | | |
373 | 178 | try |
374 | 178 | { |
375 | 178 | abyRawBlockData.resize(m_nInnerBlockSizeBytes); |
376 | 178 | if (m_bFortranOrder || m_oFiltersArray.Size() != 0) |
377 | 0 | abyTmpRawBlockData.resize(m_nInnerBlockSizeBytes); |
378 | 178 | } |
379 | 178 | catch (const std::bad_alloc &e) |
380 | 178 | { |
381 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what()); |
382 | 0 | return false; |
383 | 0 | } |
384 | | |
385 | 178 | if (NeedDecodedBuffer()) |
386 | 0 | { |
387 | 0 | size_t nDecodedBufferSize = m_oType.GetSize(); |
388 | 0 | for (const auto &nBlockSize : m_anOuterBlockSize) |
389 | 0 | { |
390 | 0 | nDecodedBufferSize *= static_cast<size_t>(nBlockSize); |
391 | 0 | } |
392 | 0 | try |
393 | 0 | { |
394 | 0 | abyDecodedBlockData.resize(nDecodedBufferSize); |
395 | 0 | } |
396 | 0 | catch (const std::bad_alloc &e) |
397 | 0 | { |
398 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what()); |
399 | 0 | return false; |
400 | 0 | } |
401 | 0 | } |
402 | | |
403 | 178 | return true; |
404 | 178 | #undef m_abyTmpRawBlockData |
405 | 178 | #undef m_abyRawBlockData |
406 | 178 | #undef m_abyDecodedBlockData |
407 | 178 | } |
408 | | |
409 | | /************************************************************************/ |
410 | | /* ZarrV2Array::BlockTranspose() */ |
411 | | /************************************************************************/ |
412 | | |
413 | | void ZarrV2Array::BlockTranspose(const ZarrByteVectorQuickResize &abySrc, |
414 | | ZarrByteVectorQuickResize &abyDst, |
415 | | bool bDecode) const |
416 | 0 | { |
417 | | // Perform transposition |
418 | 0 | const size_t nDims = m_anOuterBlockSize.size(); |
419 | 0 | const size_t nSourceSize = |
420 | 0 | m_aoDtypeElts.back().nativeOffset + m_aoDtypeElts.back().nativeSize; |
421 | |
|
422 | 0 | struct Stack |
423 | 0 | { |
424 | 0 | size_t nIters = 0; |
425 | 0 | const GByte *src_ptr = nullptr; |
426 | 0 | GByte *dst_ptr = nullptr; |
427 | 0 | size_t src_inc_offset = 0; |
428 | 0 | size_t dst_inc_offset = 0; |
429 | 0 | }; |
430 | |
|
431 | 0 | std::vector<Stack> stack(nDims); |
432 | 0 | #if defined(__GNUC__) |
433 | 0 | #pragma GCC diagnostic push |
434 | 0 | #pragma GCC diagnostic ignored "-Wnull-dereference" |
435 | 0 | #endif |
436 | 0 | stack.emplace_back( |
437 | 0 | Stack()); // to make gcc 9.3 -O2 -Wnull-dereference happy |
438 | 0 | #if defined(__GNUC__) |
439 | 0 | #pragma GCC diagnostic pop |
440 | 0 | #endif |
441 | |
|
442 | 0 | if (bDecode) |
443 | 0 | { |
444 | 0 | stack[0].src_inc_offset = nSourceSize; |
445 | 0 | for (size_t i = 1; i < nDims; ++i) |
446 | 0 | { |
447 | 0 | stack[i].src_inc_offset = |
448 | 0 | stack[i - 1].src_inc_offset * |
449 | 0 | static_cast<size_t>(m_anOuterBlockSize[i - 1]); |
450 | 0 | } |
451 | |
|
452 | 0 | stack[nDims - 1].dst_inc_offset = nSourceSize; |
453 | 0 | for (size_t i = nDims - 1; i > 0;) |
454 | 0 | { |
455 | 0 | --i; |
456 | 0 | stack[i].dst_inc_offset = |
457 | 0 | stack[i + 1].dst_inc_offset * |
458 | 0 | static_cast<size_t>(m_anOuterBlockSize[i + 1]); |
459 | 0 | } |
460 | 0 | } |
461 | 0 | else |
462 | 0 | { |
463 | 0 | stack[0].dst_inc_offset = nSourceSize; |
464 | 0 | for (size_t i = 1; i < nDims; ++i) |
465 | 0 | { |
466 | 0 | stack[i].dst_inc_offset = |
467 | 0 | stack[i - 1].dst_inc_offset * |
468 | 0 | static_cast<size_t>(m_anOuterBlockSize[i - 1]); |
469 | 0 | } |
470 | |
|
471 | 0 | stack[nDims - 1].src_inc_offset = nSourceSize; |
472 | 0 | for (size_t i = nDims - 1; i > 0;) |
473 | 0 | { |
474 | 0 | --i; |
475 | 0 | stack[i].src_inc_offset = |
476 | 0 | stack[i + 1].src_inc_offset * |
477 | 0 | static_cast<size_t>(m_anOuterBlockSize[i + 1]); |
478 | 0 | } |
479 | 0 | } |
480 | |
|
481 | 0 | stack[0].src_ptr = abySrc.data(); |
482 | 0 | stack[0].dst_ptr = &abyDst[0]; |
483 | |
|
484 | 0 | size_t dimIdx = 0; |
485 | 0 | lbl_next_depth: |
486 | 0 | if (dimIdx == nDims) |
487 | 0 | { |
488 | 0 | void *dst_ptr = stack[nDims].dst_ptr; |
489 | 0 | const void *src_ptr = stack[nDims].src_ptr; |
490 | 0 | if (nSourceSize == 1) |
491 | 0 | *stack[nDims].dst_ptr = *stack[nDims].src_ptr; |
492 | 0 | else if (nSourceSize == 2) |
493 | 0 | *static_cast<uint16_t *>(dst_ptr) = |
494 | 0 | *static_cast<const uint16_t *>(src_ptr); |
495 | 0 | else if (nSourceSize == 4) |
496 | 0 | *static_cast<uint32_t *>(dst_ptr) = |
497 | 0 | *static_cast<const uint32_t *>(src_ptr); |
498 | 0 | else if (nSourceSize == 8) |
499 | 0 | *static_cast<uint64_t *>(dst_ptr) = |
500 | 0 | *static_cast<const uint64_t *>(src_ptr); |
501 | 0 | else |
502 | 0 | memcpy(dst_ptr, src_ptr, nSourceSize); |
503 | 0 | } |
504 | 0 | else |
505 | 0 | { |
506 | 0 | stack[dimIdx].nIters = static_cast<size_t>(m_anOuterBlockSize[dimIdx]); |
507 | 0 | while (true) |
508 | 0 | { |
509 | 0 | dimIdx++; |
510 | 0 | stack[dimIdx].src_ptr = stack[dimIdx - 1].src_ptr; |
511 | 0 | stack[dimIdx].dst_ptr = stack[dimIdx - 1].dst_ptr; |
512 | 0 | goto lbl_next_depth; |
513 | 0 | lbl_return_to_caller: |
514 | 0 | dimIdx--; |
515 | 0 | if ((--stack[dimIdx].nIters) == 0) |
516 | 0 | break; |
517 | 0 | stack[dimIdx].src_ptr += stack[dimIdx].src_inc_offset; |
518 | 0 | stack[dimIdx].dst_ptr += stack[dimIdx].dst_inc_offset; |
519 | 0 | } |
520 | 0 | } |
521 | 0 | if (dimIdx > 0) |
522 | 0 | goto lbl_return_to_caller; |
523 | 0 | } |
524 | | |
525 | | /************************************************************************/ |
526 | | /* ZarrV2Array::LoadBlockData() */ |
527 | | /************************************************************************/ |
528 | | |
529 | | bool ZarrV2Array::LoadBlockData(const uint64_t *blockIndices, |
530 | | bool &bMissingBlockOut) const |
531 | 5.07k | { |
532 | 5.07k | return LoadBlockData(blockIndices, |
533 | 5.07k | false, // use mutex |
534 | 5.07k | m_psDecompressor, m_abyRawBlockData, |
535 | 5.07k | m_abyTmpRawBlockData, m_abyDecodedBlockData, |
536 | 5.07k | bMissingBlockOut); |
537 | 5.07k | } |
538 | | |
539 | | bool ZarrV2Array::LoadBlockData(const uint64_t *blockIndices, bool bUseMutex, |
540 | | const CPLCompressor *psDecompressor, |
541 | | ZarrByteVectorQuickResize &abyRawBlockData, |
542 | | ZarrByteVectorQuickResize &abyTmpRawBlockData, |
543 | | ZarrByteVectorQuickResize &abyDecodedBlockData, |
544 | | bool &bMissingBlockOut) const |
545 | 5.07k | { |
546 | | // This method should NOT modify any ZarrArray member, as it is going to |
547 | | // be called concurrently from several threads. |
548 | | |
549 | | // Set those #define to avoid accidental use of some global variables |
550 | 5.07k | #define m_abyTmpRawBlockData cannot_use_here |
551 | 5.07k | #define m_abyRawBlockData cannot_use_here |
552 | 5.07k | #define m_abyDecodedBlockData cannot_use_here |
553 | 5.07k | #define m_psDecompressor cannot_use_here |
554 | | |
555 | 5.07k | bMissingBlockOut = false; |
556 | | |
557 | 5.07k | std::string osFilename = BuildChunkFilename(blockIndices); |
558 | | |
559 | | // For network file systems, get the streaming version of the filename, |
560 | | // as we don't need arbitrary seeking in the file |
561 | 5.07k | osFilename = VSIFileManager::GetHandler(osFilename.c_str()) |
562 | 5.07k | ->GetStreamingFilename(osFilename); |
563 | | |
564 | | // First if we have a tile presence cache, check tile presence from it |
565 | 5.07k | bool bEarlyRet; |
566 | 5.07k | if (bUseMutex) |
567 | 0 | { |
568 | 0 | std::lock_guard<std::mutex> oLock(m_oMutex); |
569 | 0 | bEarlyRet = IsBlockMissingFromCacheInfo(osFilename, blockIndices); |
570 | 0 | } |
571 | 5.07k | else |
572 | 5.07k | { |
573 | 5.07k | bEarlyRet = IsBlockMissingFromCacheInfo(osFilename, blockIndices); |
574 | 5.07k | } |
575 | 5.07k | if (bEarlyRet) |
576 | 0 | { |
577 | 0 | bMissingBlockOut = true; |
578 | 0 | return true; |
579 | 0 | } |
580 | | |
581 | 5.07k | VSILFILE *fp = nullptr; |
582 | | // This is the number of files returned in a S3 directory listing operation |
583 | 5.07k | constexpr uint64_t MAX_TILES_ALLOWED_FOR_DIRECTORY_LISTING = 1000; |
584 | 5.07k | const char *const apszOpenOptions[] = {"IGNORE_FILENAME_RESTRICTIONS=YES", |
585 | 5.07k | nullptr}; |
586 | 5.07k | const auto nErrorBefore = CPLGetErrorCounter(); |
587 | 5.07k | if ((m_osDimSeparator == "/" && !m_anOuterBlockSize.empty() && |
588 | 0 | m_anOuterBlockSize.back() > MAX_TILES_ALLOWED_FOR_DIRECTORY_LISTING) || |
589 | 5.07k | (m_osDimSeparator != "/" && |
590 | 5.07k | m_nTotalInnerChunkCount > MAX_TILES_ALLOWED_FOR_DIRECTORY_LISTING)) |
591 | 3.34k | { |
592 | | // Avoid issuing ReadDir() when a lot of files are expected |
593 | 3.34k | CPLConfigOptionSetter optionSetter("GDAL_DISABLE_READDIR_ON_OPEN", |
594 | 3.34k | "YES", true); |
595 | 3.34k | fp = VSIFOpenEx2L(osFilename.c_str(), "rb", 0, apszOpenOptions); |
596 | 3.34k | } |
597 | 1.72k | else |
598 | 1.72k | { |
599 | 1.72k | fp = VSIFOpenEx2L(osFilename.c_str(), "rb", 0, apszOpenOptions); |
600 | 1.72k | } |
601 | 5.07k | if (fp == nullptr) |
602 | 5.07k | { |
603 | 5.07k | if (nErrorBefore != CPLGetErrorCounter()) |
604 | 0 | { |
605 | 0 | return false; |
606 | 0 | } |
607 | 5.07k | else |
608 | 5.07k | { |
609 | | // Missing files are OK and indicate nodata_value |
610 | 5.07k | CPLDebugOnly(ZARR_DEBUG_KEY, "Block %s missing (=nodata)", |
611 | 5.07k | osFilename.c_str()); |
612 | 5.07k | bMissingBlockOut = true; |
613 | 5.07k | return true; |
614 | 5.07k | } |
615 | 5.07k | } |
616 | | |
617 | 0 | bMissingBlockOut = false; |
618 | 0 | bool bRet = true; |
619 | 0 | size_t nRawDataSize = abyRawBlockData.size(); |
620 | 0 | if (psDecompressor == nullptr) |
621 | 0 | { |
622 | 0 | nRawDataSize = VSIFReadL(&abyRawBlockData[0], 1, nRawDataSize, fp); |
623 | 0 | } |
624 | 0 | else |
625 | 0 | { |
626 | 0 | VSIFSeekL(fp, 0, SEEK_END); |
627 | 0 | const auto nSize = VSIFTellL(fp); |
628 | 0 | VSIFSeekL(fp, 0, SEEK_SET); |
629 | 0 | if (nSize > static_cast<vsi_l_offset>(std::numeric_limits<int>::max())) |
630 | 0 | { |
631 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Too large tile %s", |
632 | 0 | osFilename.c_str()); |
633 | 0 | bRet = false; |
634 | 0 | } |
635 | 0 | else |
636 | 0 | { |
637 | 0 | ZarrByteVectorQuickResize abyCompressedData; |
638 | 0 | try |
639 | 0 | { |
640 | 0 | abyCompressedData.resize(static_cast<size_t>(nSize)); |
641 | 0 | } |
642 | 0 | catch (const std::exception &) |
643 | 0 | { |
644 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, |
645 | 0 | "Cannot allocate memory for tile %s", |
646 | 0 | osFilename.c_str()); |
647 | 0 | bRet = false; |
648 | 0 | } |
649 | |
|
650 | 0 | if (bRet && |
651 | 0 | (abyCompressedData.empty() || |
652 | 0 | VSIFReadL(&abyCompressedData[0], 1, abyCompressedData.size(), |
653 | 0 | fp) != abyCompressedData.size())) |
654 | 0 | { |
655 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
656 | 0 | "Could not read tile %s correctly", |
657 | 0 | osFilename.c_str()); |
658 | 0 | bRet = false; |
659 | 0 | } |
660 | 0 | else |
661 | 0 | { |
662 | 0 | void *out_buffer = &abyRawBlockData[0]; |
663 | 0 | if (!psDecompressor->pfnFunc( |
664 | 0 | abyCompressedData.data(), abyCompressedData.size(), |
665 | 0 | &out_buffer, &nRawDataSize, nullptr, |
666 | 0 | psDecompressor->user_data)) |
667 | 0 | { |
668 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
669 | 0 | "Decompression of tile %s failed", |
670 | 0 | osFilename.c_str()); |
671 | 0 | bRet = false; |
672 | 0 | } |
673 | 0 | } |
674 | 0 | } |
675 | 0 | } |
676 | 0 | VSIFCloseL(fp); |
677 | 0 | if (!bRet) |
678 | 0 | return false; |
679 | | |
680 | 0 | for (int i = m_oFiltersArray.Size(); i > 0;) |
681 | 0 | { |
682 | 0 | --i; |
683 | 0 | const CPLCompressor *psFilterDecompressor; |
684 | 0 | CPLStringList aosOptions; |
685 | 0 | std::string osFilterId; |
686 | 0 | { |
687 | | // Below CPLJSONObject/CPLJSONArray uses are not thread safe |
688 | 0 | std::lock_guard<std::mutex> oLock(m_oMutex); |
689 | 0 | const auto &oFilter = m_oFiltersArray[i]; |
690 | 0 | osFilterId = oFilter["id"].ToString(); |
691 | 0 | psFilterDecompressor = |
692 | 0 | EQUAL(osFilterId.c_str(), "shuffle") |
693 | 0 | ? ZarrGetShuffleDecompressor() |
694 | 0 | : EQUAL(osFilterId.c_str(), "quantize") |
695 | 0 | ? ZarrGetQuantizeDecompressor() |
696 | 0 | : EQUAL(osFilterId.c_str(), "fixedscaleoffset") |
697 | 0 | ? ZarrGetFixedScaleOffsetDecompressor() |
698 | 0 | : CPLGetDecompressor(osFilterId.c_str()); |
699 | 0 | CPLAssert(psFilterDecompressor); |
700 | |
|
701 | 0 | for (const auto &obj : oFilter.GetChildren()) |
702 | 0 | { |
703 | 0 | aosOptions.SetNameValue(obj.GetName().c_str(), |
704 | 0 | obj.ToString().c_str()); |
705 | 0 | } |
706 | 0 | } |
707 | |
|
708 | 0 | void *out_buffer = &abyTmpRawBlockData[0]; |
709 | 0 | size_t nOutSize = abyTmpRawBlockData.size(); |
710 | 0 | if (!psFilterDecompressor->pfnFunc( |
711 | 0 | abyRawBlockData.data(), nRawDataSize, &out_buffer, &nOutSize, |
712 | 0 | aosOptions.List(), psFilterDecompressor->user_data)) |
713 | 0 | { |
714 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
715 | 0 | "Filter %s for tile %s failed", osFilterId.c_str(), |
716 | 0 | osFilename.c_str()); |
717 | 0 | return false; |
718 | 0 | } |
719 | | |
720 | 0 | nRawDataSize = nOutSize; |
721 | 0 | std::swap(abyRawBlockData, abyTmpRawBlockData); |
722 | 0 | } |
723 | 0 | if (nRawDataSize != abyRawBlockData.size()) |
724 | 0 | { |
725 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
726 | 0 | "Decompressed tile %s has not expected size after filters", |
727 | 0 | osFilename.c_str()); |
728 | 0 | return false; |
729 | 0 | } |
730 | | |
731 | 0 | if (m_bFortranOrder && !m_aoDims.empty()) |
732 | 0 | { |
733 | 0 | BlockTranspose(abyRawBlockData, abyTmpRawBlockData, true); |
734 | 0 | std::swap(abyRawBlockData, abyTmpRawBlockData); |
735 | 0 | } |
736 | |
|
737 | 0 | if (!abyDecodedBlockData.empty()) |
738 | 0 | { |
739 | 0 | const size_t nSourceSize = |
740 | 0 | m_aoDtypeElts.back().nativeOffset + m_aoDtypeElts.back().nativeSize; |
741 | 0 | const auto nDTSize = m_oType.GetSize(); |
742 | 0 | const size_t nValues = abyDecodedBlockData.size() / nDTSize; |
743 | 0 | const GByte *pSrc = abyRawBlockData.data(); |
744 | 0 | GByte *pDst = &abyDecodedBlockData[0]; |
745 | 0 | for (size_t i = 0; i < nValues; |
746 | 0 | i++, pSrc += nSourceSize, pDst += nDTSize) |
747 | 0 | { |
748 | 0 | DecodeSourceElt(m_aoDtypeElts, pSrc, pDst); |
749 | 0 | } |
750 | 0 | } |
751 | |
|
752 | 0 | return true; |
753 | |
|
754 | 0 | #undef m_abyTmpRawBlockData |
755 | 0 | #undef m_abyRawBlockData |
756 | 0 | #undef m_abyDecodedBlockData |
757 | 0 | #undef m_psDecompressor |
758 | 0 | } |
759 | | |
760 | | /************************************************************************/ |
761 | | /* ZarrV2Array::IAdviseRead() */ |
762 | | /************************************************************************/ |
763 | | |
764 | | bool ZarrV2Array::IAdviseRead(const GUInt64 *arrayStartIdx, const size_t *count, |
765 | | CSLConstList papszOptions) const |
766 | 0 | { |
767 | 0 | std::vector<uint64_t> anIndicesCur; |
768 | 0 | int nThreadsMax = 0; |
769 | 0 | std::vector<uint64_t> anReqBlocksIndices; |
770 | 0 | size_t nReqBlocks = 0; |
771 | 0 | if (!IAdviseReadCommon(arrayStartIdx, count, papszOptions, anIndicesCur, |
772 | 0 | nThreadsMax, anReqBlocksIndices, nReqBlocks)) |
773 | 0 | { |
774 | 0 | return false; |
775 | 0 | } |
776 | 0 | if (nThreadsMax <= 1) |
777 | 0 | { |
778 | 0 | return true; |
779 | 0 | } |
780 | | |
781 | 0 | const int nThreads = static_cast<int>( |
782 | 0 | std::min(static_cast<size_t>(nThreadsMax), nReqBlocks)); |
783 | |
|
784 | 0 | CPLWorkerThreadPool *wtp = GDALGetGlobalThreadPool(nThreadsMax); |
785 | 0 | if (wtp == nullptr) |
786 | 0 | return false; |
787 | | |
788 | 0 | struct JobStruct |
789 | 0 | { |
790 | 0 | JobStruct() = default; |
791 | |
|
792 | 0 | JobStruct(const JobStruct &) = delete; |
793 | 0 | JobStruct &operator=(const JobStruct &) = delete; |
794 | |
|
795 | 0 | JobStruct(JobStruct &&) = default; |
796 | 0 | JobStruct &operator=(JobStruct &&) = default; |
797 | |
|
798 | 0 | const ZarrV2Array *poArray = nullptr; |
799 | 0 | bool *pbGlobalStatus = nullptr; |
800 | 0 | int *pnRemainingThreads = nullptr; |
801 | 0 | const std::vector<uint64_t> *panReqBlocksIndices = nullptr; |
802 | 0 | size_t nFirstIdx = 0; |
803 | 0 | size_t nLastIdxNotIncluded = 0; |
804 | 0 | }; |
805 | |
|
806 | 0 | std::vector<JobStruct> asJobStructs; |
807 | |
|
808 | 0 | bool bGlobalStatus = true; |
809 | 0 | int nRemainingThreads = nThreads; |
810 | | // Check for very highly overflow in below loop |
811 | 0 | assert(static_cast<size_t>(nThreads) < |
812 | 0 | std::numeric_limits<size_t>::max() / nReqBlocks); |
813 | | |
814 | | // Setup jobs |
815 | 0 | for (int i = 0; i < nThreads; i++) |
816 | 0 | { |
817 | 0 | JobStruct jobStruct; |
818 | 0 | jobStruct.poArray = this; |
819 | 0 | jobStruct.pbGlobalStatus = &bGlobalStatus; |
820 | 0 | jobStruct.pnRemainingThreads = &nRemainingThreads; |
821 | 0 | jobStruct.panReqBlocksIndices = &anReqBlocksIndices; |
822 | 0 | jobStruct.nFirstIdx = static_cast<size_t>(i * nReqBlocks / nThreads); |
823 | 0 | jobStruct.nLastIdxNotIncluded = std::min( |
824 | 0 | static_cast<size_t>((i + 1) * nReqBlocks / nThreads), nReqBlocks); |
825 | 0 | asJobStructs.emplace_back(std::move(jobStruct)); |
826 | 0 | } |
827 | |
|
828 | 0 | const auto JobFunc = [](void *pThreadData) |
829 | 0 | { |
830 | 0 | const JobStruct *jobStruct = |
831 | 0 | static_cast<const JobStruct *>(pThreadData); |
832 | |
|
833 | 0 | const auto poArray = jobStruct->poArray; |
834 | 0 | const size_t l_nDims = poArray->GetDimensionCount(); |
835 | 0 | ZarrByteVectorQuickResize abyRawBlockData; |
836 | 0 | ZarrByteVectorQuickResize abyDecodedBlockData; |
837 | 0 | ZarrByteVectorQuickResize abyTmpRawBlockData; |
838 | 0 | const CPLCompressor *psDecompressor = |
839 | 0 | CPLGetDecompressor(poArray->m_osDecompressorId.c_str()); |
840 | |
|
841 | 0 | for (size_t iReq = jobStruct->nFirstIdx; |
842 | 0 | iReq < jobStruct->nLastIdxNotIncluded; ++iReq) |
843 | 0 | { |
844 | | // Check if we must early exit |
845 | 0 | { |
846 | 0 | std::lock_guard<std::mutex> oLock(poArray->m_oMutex); |
847 | 0 | if (!(*jobStruct->pbGlobalStatus)) |
848 | 0 | return; |
849 | 0 | } |
850 | | |
851 | 0 | const uint64_t *blockIndices = |
852 | 0 | jobStruct->panReqBlocksIndices->data() + iReq * l_nDims; |
853 | |
|
854 | 0 | if (!poArray->AllocateWorkingBuffers( |
855 | 0 | abyRawBlockData, abyTmpRawBlockData, abyDecodedBlockData)) |
856 | 0 | { |
857 | 0 | std::lock_guard<std::mutex> oLock(poArray->m_oMutex); |
858 | 0 | *jobStruct->pbGlobalStatus = false; |
859 | 0 | break; |
860 | 0 | } |
861 | | |
862 | 0 | bool bIsEmpty = false; |
863 | 0 | bool success = poArray->LoadBlockData( |
864 | 0 | blockIndices, |
865 | 0 | true, // use mutex |
866 | 0 | psDecompressor, abyRawBlockData, abyTmpRawBlockData, |
867 | 0 | abyDecodedBlockData, bIsEmpty); |
868 | |
|
869 | 0 | std::lock_guard<std::mutex> oLock(poArray->m_oMutex); |
870 | 0 | if (!success) |
871 | 0 | { |
872 | 0 | *jobStruct->pbGlobalStatus = false; |
873 | 0 | break; |
874 | 0 | } |
875 | | |
876 | 0 | CachedBlock cachedBlock; |
877 | 0 | if (!bIsEmpty) |
878 | 0 | { |
879 | 0 | if (!abyDecodedBlockData.empty()) |
880 | 0 | std::swap(cachedBlock.abyDecoded, abyDecodedBlockData); |
881 | 0 | else |
882 | 0 | std::swap(cachedBlock.abyDecoded, abyRawBlockData); |
883 | 0 | } |
884 | 0 | const std::vector<uint64_t> cacheKey{blockIndices, |
885 | 0 | blockIndices + l_nDims}; |
886 | 0 | poArray->m_oChunkCache[cacheKey] = std::move(cachedBlock); |
887 | 0 | } |
888 | | |
889 | 0 | std::lock_guard<std::mutex> oLock(poArray->m_oMutex); |
890 | 0 | (*jobStruct->pnRemainingThreads)--; |
891 | 0 | }; |
892 | | |
893 | | // Start jobs |
894 | 0 | for (int i = 0; i < nThreads; i++) |
895 | 0 | { |
896 | 0 | if (!wtp->SubmitJob(JobFunc, &asJobStructs[i])) |
897 | 0 | { |
898 | 0 | std::lock_guard<std::mutex> oLock(m_oMutex); |
899 | 0 | bGlobalStatus = false; |
900 | 0 | nRemainingThreads = i; |
901 | 0 | break; |
902 | 0 | } |
903 | 0 | } |
904 | | |
905 | | // Wait for all jobs to be finished |
906 | 0 | while (true) |
907 | 0 | { |
908 | 0 | { |
909 | 0 | std::lock_guard<std::mutex> oLock(m_oMutex); |
910 | 0 | if (nRemainingThreads == 0) |
911 | 0 | break; |
912 | 0 | } |
913 | 0 | wtp->WaitEvent(); |
914 | 0 | } |
915 | |
|
916 | 0 | return bGlobalStatus; |
917 | 0 | } |
918 | | |
919 | | /************************************************************************/ |
920 | | /* ZarrV2Array::FlushDirtyBlock() */ |
921 | | /************************************************************************/ |
922 | | |
923 | | bool ZarrV2Array::FlushDirtyBlock() const |
924 | 25.0k | { |
925 | 25.0k | if (!m_bDirtyBlock) |
926 | 1.32k | return true; |
927 | 23.7k | m_bDirtyBlock = false; |
928 | | |
929 | 23.7k | std::string osFilename = BuildChunkFilename(m_anCachedBlockIndices.data()); |
930 | | |
931 | 23.7k | const size_t nSourceSize = |
932 | 23.7k | m_aoDtypeElts.back().nativeOffset + m_aoDtypeElts.back().nativeSize; |
933 | 23.7k | const auto &abyBlock = m_abyDecodedBlockData.empty() |
934 | 23.7k | ? m_abyRawBlockData |
935 | 23.7k | : m_abyDecodedBlockData; |
936 | | |
937 | 23.7k | if (IsEmptyBlock(abyBlock)) |
938 | 764 | { |
939 | 764 | m_bCachedBlockEmpty = true; |
940 | | |
941 | 764 | VSIStatBufL sStat; |
942 | 764 | if (VSIStatL(osFilename.c_str(), &sStat) == 0) |
943 | 0 | { |
944 | 0 | CPLDebugOnly(ZARR_DEBUG_KEY, |
945 | 0 | "Deleting tile %s that has now empty content", |
946 | 0 | osFilename.c_str()); |
947 | 0 | return VSIUnlink(osFilename.c_str()) == 0; |
948 | 0 | } |
949 | 764 | return true; |
950 | 764 | } |
951 | | |
952 | 22.9k | if (!m_abyDecodedBlockData.empty()) |
953 | 0 | { |
954 | 0 | const size_t nDTSize = m_oType.GetSize(); |
955 | 0 | const size_t nValues = m_abyDecodedBlockData.size() / nDTSize; |
956 | 0 | GByte *pDst = &m_abyRawBlockData[0]; |
957 | 0 | const GByte *pSrc = m_abyDecodedBlockData.data(); |
958 | 0 | for (size_t i = 0; i < nValues; |
959 | 0 | i++, pDst += nSourceSize, pSrc += nDTSize) |
960 | 0 | { |
961 | 0 | EncodeElt(m_aoDtypeElts, pSrc, pDst); |
962 | 0 | } |
963 | 0 | } |
964 | | |
965 | 22.9k | if (m_bFortranOrder && !m_aoDims.empty()) |
966 | 0 | { |
967 | 0 | BlockTranspose(m_abyRawBlockData, m_abyTmpRawBlockData, false); |
968 | 0 | std::swap(m_abyRawBlockData, m_abyTmpRawBlockData); |
969 | 0 | } |
970 | | |
971 | 22.9k | size_t nRawDataSize = m_abyRawBlockData.size(); |
972 | 22.9k | for (const auto &oFilter : m_oFiltersArray) |
973 | 0 | { |
974 | 0 | const auto osFilterId = oFilter["id"].ToString(); |
975 | 0 | if (osFilterId == "quantize" || osFilterId == "fixedscaleoffset") |
976 | 0 | { |
977 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
978 | 0 | "%s filter not supported for writing", osFilterId.c_str()); |
979 | 0 | return false; |
980 | 0 | } |
981 | 0 | const auto psFilterCompressor = |
982 | 0 | EQUAL(osFilterId.c_str(), "shuffle") |
983 | 0 | ? ZarrGetShuffleCompressor() |
984 | 0 | : CPLGetCompressor(osFilterId.c_str()); |
985 | 0 | CPLAssert(psFilterCompressor); |
986 | |
|
987 | 0 | CPLStringList aosOptions; |
988 | 0 | for (const auto &obj : oFilter.GetChildren()) |
989 | 0 | { |
990 | 0 | aosOptions.SetNameValue(obj.GetName().c_str(), |
991 | 0 | obj.ToString().c_str()); |
992 | 0 | } |
993 | 0 | void *out_buffer = &m_abyTmpRawBlockData[0]; |
994 | 0 | size_t nOutSize = m_abyTmpRawBlockData.size(); |
995 | 0 | if (!psFilterCompressor->pfnFunc( |
996 | 0 | m_abyRawBlockData.data(), nRawDataSize, &out_buffer, &nOutSize, |
997 | 0 | aosOptions.List(), psFilterCompressor->user_data)) |
998 | 0 | { |
999 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1000 | 0 | "Filter %s for tile %s failed", osFilterId.c_str(), |
1001 | 0 | osFilename.c_str()); |
1002 | 0 | return false; |
1003 | 0 | } |
1004 | | |
1005 | 0 | nRawDataSize = nOutSize; |
1006 | 0 | std::swap(m_abyRawBlockData, m_abyTmpRawBlockData); |
1007 | 0 | } |
1008 | | |
1009 | 22.9k | if (m_osDimSeparator == "/") |
1010 | 0 | { |
1011 | 0 | std::string osDir = CPLGetDirnameSafe(osFilename.c_str()); |
1012 | 0 | VSIStatBufL sStat; |
1013 | 0 | if (VSIStatL(osDir.c_str(), &sStat) != 0) |
1014 | 0 | { |
1015 | 0 | if (VSIMkdirRecursive(osDir.c_str(), 0755) != 0) |
1016 | 0 | { |
1017 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1018 | 0 | "Cannot create directory %s", osDir.c_str()); |
1019 | 0 | return false; |
1020 | 0 | } |
1021 | 0 | } |
1022 | 0 | } |
1023 | | |
1024 | 22.9k | if (m_psCompressor == nullptr && m_psDecompressor != nullptr) |
1025 | 0 | { |
1026 | | // Case of imagecodecs_tiff |
1027 | |
|
1028 | 0 | CPLError(CE_Failure, CPLE_NotSupported, |
1029 | 0 | "Only decompression supported for '%s' compression method", |
1030 | 0 | m_osDecompressorId.c_str()); |
1031 | 0 | return false; |
1032 | 0 | } |
1033 | | |
1034 | 22.9k | VSILFILE *fp = VSIFOpenL(osFilename.c_str(), "wb"); |
1035 | 22.9k | if (fp == nullptr) |
1036 | 0 | { |
1037 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot create tile %s", |
1038 | 0 | osFilename.c_str()); |
1039 | 0 | return false; |
1040 | 0 | } |
1041 | | |
1042 | 22.9k | bool bRet = true; |
1043 | 22.9k | if (m_psCompressor == nullptr) |
1044 | 22.9k | { |
1045 | 22.9k | if (VSIFWriteL(m_abyRawBlockData.data(), 1, nRawDataSize, fp) != |
1046 | 22.9k | nRawDataSize) |
1047 | 0 | { |
1048 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1049 | 0 | "Could not write tile %s correctly", osFilename.c_str()); |
1050 | 0 | bRet = false; |
1051 | 0 | } |
1052 | 22.9k | } |
1053 | 0 | else |
1054 | 0 | { |
1055 | 0 | std::vector<GByte> abyCompressedData; |
1056 | 0 | try |
1057 | 0 | { |
1058 | 0 | constexpr size_t MIN_BUF_SIZE = 64; // somewhat arbitrary |
1059 | 0 | abyCompressedData.resize(static_cast<size_t>( |
1060 | 0 | MIN_BUF_SIZE + nRawDataSize + nRawDataSize / 3)); |
1061 | 0 | } |
1062 | 0 | catch (const std::exception &) |
1063 | 0 | { |
1064 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, |
1065 | 0 | "Cannot allocate memory for tile %s", osFilename.c_str()); |
1066 | 0 | bRet = false; |
1067 | 0 | } |
1068 | |
|
1069 | 0 | if (bRet) |
1070 | 0 | { |
1071 | 0 | void *out_buffer = &abyCompressedData[0]; |
1072 | 0 | size_t out_size = abyCompressedData.size(); |
1073 | 0 | CPLStringList aosOptions; |
1074 | 0 | const auto &compressorConfig = m_oCompressorJSon; |
1075 | 0 | for (const auto &obj : compressorConfig.GetChildren()) |
1076 | 0 | { |
1077 | 0 | aosOptions.SetNameValue(obj.GetName().c_str(), |
1078 | 0 | obj.ToString().c_str()); |
1079 | 0 | } |
1080 | 0 | if (EQUAL(m_psCompressor->pszId, "blosc") && |
1081 | 0 | m_oType.GetClass() == GEDTC_NUMERIC) |
1082 | 0 | { |
1083 | 0 | aosOptions.SetNameValue( |
1084 | 0 | "TYPESIZE", |
1085 | 0 | CPLSPrintf("%d", GDALGetDataTypeSizeBytes( |
1086 | 0 | GDALGetNonComplexDataType( |
1087 | 0 | m_oType.GetNumericDataType())))); |
1088 | 0 | } |
1089 | |
|
1090 | 0 | if (!m_psCompressor->pfnFunc( |
1091 | 0 | m_abyRawBlockData.data(), nRawDataSize, &out_buffer, |
1092 | 0 | &out_size, aosOptions.List(), m_psCompressor->user_data)) |
1093 | 0 | { |
1094 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1095 | 0 | "Compression of tile %s failed", osFilename.c_str()); |
1096 | 0 | bRet = false; |
1097 | 0 | } |
1098 | 0 | abyCompressedData.resize(out_size); |
1099 | 0 | } |
1100 | |
|
1101 | 0 | if (bRet && |
1102 | 0 | VSIFWriteL(abyCompressedData.data(), 1, abyCompressedData.size(), |
1103 | 0 | fp) != abyCompressedData.size()) |
1104 | 0 | { |
1105 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1106 | 0 | "Could not write tile %s correctly", osFilename.c_str()); |
1107 | 0 | bRet = false; |
1108 | 0 | } |
1109 | 0 | } |
1110 | 22.9k | VSIFCloseL(fp); |
1111 | | |
1112 | 22.9k | return bRet; |
1113 | 22.9k | } |
1114 | | |
1115 | | /************************************************************************/ |
1116 | | /* BuildChunkFilename() */ |
1117 | | /************************************************************************/ |
1118 | | |
1119 | | std::string ZarrV2Array::BuildChunkFilename(const uint64_t *blockIndices) const |
1120 | 28.7k | { |
1121 | 28.7k | std::string osFilename; |
1122 | 28.7k | if (m_aoDims.empty()) |
1123 | 0 | { |
1124 | 0 | osFilename = "0"; |
1125 | 0 | } |
1126 | 28.7k | else |
1127 | 28.7k | { |
1128 | 114k | for (size_t i = 0; i < m_aoDims.size(); ++i) |
1129 | 86.1k | { |
1130 | 86.1k | if (!osFilename.empty()) |
1131 | 57.3k | osFilename += m_osDimSeparator; |
1132 | 86.1k | osFilename += std::to_string(blockIndices[i]); |
1133 | 86.1k | } |
1134 | 28.7k | } |
1135 | | |
1136 | 28.7k | return CPLFormFilenameSafe(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(), |
1137 | 28.7k | osFilename.c_str(), nullptr); |
1138 | 28.7k | } |
1139 | | |
1140 | | /************************************************************************/ |
1141 | | /* GetDataDirectory() */ |
1142 | | /************************************************************************/ |
1143 | | |
1144 | | std::string ZarrV2Array::GetDataDirectory() const |
1145 | 0 | { |
1146 | 0 | return CPLGetDirnameSafe(m_osFilename.c_str()); |
1147 | 0 | } |
1148 | | |
1149 | | /************************************************************************/ |
1150 | | /* GetChunkIndicesFromFilename() */ |
1151 | | /************************************************************************/ |
1152 | | |
1153 | | CPLStringList |
1154 | | ZarrV2Array::GetChunkIndicesFromFilename(const char *pszFilename) const |
1155 | 0 | { |
1156 | 0 | return CPLStringList( |
1157 | 0 | CSLTokenizeString2(pszFilename, m_osDimSeparator.c_str(), 0)); |
1158 | 0 | } |
1159 | | |
1160 | | /************************************************************************/ |
1161 | | /* ParseDtype() */ |
1162 | | /************************************************************************/ |
1163 | | |
1164 | | static size_t GetAlignment(const CPLJSONObject &obj) |
1165 | 0 | { |
1166 | 0 | if (obj.GetType() == CPLJSONObject::Type::String) |
1167 | 0 | { |
1168 | 0 | const auto str = obj.ToString(); |
1169 | 0 | if (str.size() < 3) |
1170 | 0 | return 1; |
1171 | 0 | const char chType = str[1]; |
1172 | 0 | const int nBytes = atoi(str.c_str() + 2); |
1173 | 0 | if (chType == 'S') |
1174 | 0 | return sizeof(char *); |
1175 | 0 | if (chType == 'c' && nBytes == 8) |
1176 | 0 | return sizeof(float); |
1177 | 0 | if (chType == 'c' && nBytes == 16) |
1178 | 0 | return sizeof(double); |
1179 | 0 | return nBytes; |
1180 | 0 | } |
1181 | 0 | else if (obj.GetType() == CPLJSONObject::Type::Array) |
1182 | 0 | { |
1183 | 0 | const auto oArray = obj.ToArray(); |
1184 | 0 | size_t nAlignment = 1; |
1185 | 0 | for (const auto &oElt : oArray) |
1186 | 0 | { |
1187 | 0 | const auto oEltArray = oElt.ToArray(); |
1188 | 0 | if (!oEltArray.IsValid() || oEltArray.Size() != 2 || |
1189 | 0 | oEltArray[0].GetType() != CPLJSONObject::Type::String) |
1190 | 0 | { |
1191 | 0 | return 1; |
1192 | 0 | } |
1193 | 0 | nAlignment = std::max(nAlignment, GetAlignment(oEltArray[1])); |
1194 | 0 | if (nAlignment == sizeof(void *)) |
1195 | 0 | break; |
1196 | 0 | } |
1197 | 0 | return nAlignment; |
1198 | 0 | } |
1199 | 0 | return 1; |
1200 | 0 | } |
1201 | | |
1202 | | static GDALExtendedDataType ParseDtype(const CPLJSONObject &obj, |
1203 | | std::vector<DtypeElt> &elts) |
1204 | 0 | { |
1205 | 0 | const auto AlignOffsetOn = [](size_t offset, size_t alignment) |
1206 | 0 | { return offset + (alignment - (offset % alignment)) % alignment; }; |
1207 | |
|
1208 | 0 | do |
1209 | 0 | { |
1210 | 0 | if (obj.GetType() == CPLJSONObject::Type::String) |
1211 | 0 | { |
1212 | 0 | const auto str = obj.ToString(); |
1213 | 0 | char chEndianness = 0; |
1214 | 0 | char chType; |
1215 | 0 | int nBytes; |
1216 | 0 | DtypeElt elt; |
1217 | 0 | if (str.size() < 3) |
1218 | 0 | break; |
1219 | 0 | chEndianness = str[0]; |
1220 | 0 | chType = str[1]; |
1221 | 0 | nBytes = atoi(str.c_str() + 2); |
1222 | 0 | if (nBytes <= 0 || nBytes >= 1000) |
1223 | 0 | break; |
1224 | | |
1225 | 0 | elt.needByteSwapping = false; |
1226 | 0 | if ((nBytes > 1 && chType != 'S') || chType == 'U') |
1227 | 0 | { |
1228 | 0 | if (chEndianness == '<') |
1229 | 0 | elt.needByteSwapping = (CPL_IS_LSB == 0); |
1230 | 0 | else if (chEndianness == '>') |
1231 | 0 | elt.needByteSwapping = (CPL_IS_LSB != 0); |
1232 | 0 | } |
1233 | |
|
1234 | 0 | GDALDataType eDT; |
1235 | 0 | if (!elts.empty()) |
1236 | 0 | { |
1237 | 0 | elt.nativeOffset = |
1238 | 0 | elts.back().nativeOffset + elts.back().nativeSize; |
1239 | 0 | } |
1240 | 0 | elt.nativeSize = nBytes; |
1241 | 0 | if (chType == 'b' && nBytes == 1) // boolean |
1242 | 0 | { |
1243 | 0 | elt.nativeType = DtypeElt::NativeType::BOOLEAN; |
1244 | 0 | eDT = GDT_UInt8; |
1245 | 0 | } |
1246 | 0 | else if (chType == 'u' && nBytes == 1) |
1247 | 0 | { |
1248 | 0 | elt.nativeType = DtypeElt::NativeType::UNSIGNED_INT; |
1249 | 0 | eDT = GDT_UInt8; |
1250 | 0 | } |
1251 | 0 | else if (chType == 'i' && nBytes == 1) |
1252 | 0 | { |
1253 | 0 | elt.nativeType = DtypeElt::NativeType::SIGNED_INT; |
1254 | 0 | eDT = GDT_Int8; |
1255 | 0 | } |
1256 | 0 | else if (chType == 'i' && nBytes == 2) |
1257 | 0 | { |
1258 | 0 | elt.nativeType = DtypeElt::NativeType::SIGNED_INT; |
1259 | 0 | eDT = GDT_Int16; |
1260 | 0 | } |
1261 | 0 | else if (chType == 'i' && nBytes == 4) |
1262 | 0 | { |
1263 | 0 | elt.nativeType = DtypeElt::NativeType::SIGNED_INT; |
1264 | 0 | eDT = GDT_Int32; |
1265 | 0 | } |
1266 | 0 | else if (chType == 'i' && nBytes == 8) |
1267 | 0 | { |
1268 | 0 | elt.nativeType = DtypeElt::NativeType::SIGNED_INT; |
1269 | 0 | eDT = GDT_Int64; |
1270 | 0 | } |
1271 | 0 | else if (chType == 'u' && nBytes == 2) |
1272 | 0 | { |
1273 | 0 | elt.nativeType = DtypeElt::NativeType::UNSIGNED_INT; |
1274 | 0 | eDT = GDT_UInt16; |
1275 | 0 | } |
1276 | 0 | else if (chType == 'u' && nBytes == 4) |
1277 | 0 | { |
1278 | 0 | elt.nativeType = DtypeElt::NativeType::UNSIGNED_INT; |
1279 | 0 | eDT = GDT_UInt32; |
1280 | 0 | } |
1281 | 0 | else if (chType == 'u' && nBytes == 8) |
1282 | 0 | { |
1283 | 0 | elt.nativeType = DtypeElt::NativeType::UNSIGNED_INT; |
1284 | 0 | eDT = GDT_UInt64; |
1285 | 0 | } |
1286 | 0 | else if (chType == 'f' && nBytes == 2) |
1287 | 0 | { |
1288 | 0 | elt.nativeType = DtypeElt::NativeType::IEEEFP; |
1289 | 0 | eDT = GDT_Float16; |
1290 | 0 | } |
1291 | 0 | else if (chType == 'f' && nBytes == 4) |
1292 | 0 | { |
1293 | 0 | elt.nativeType = DtypeElt::NativeType::IEEEFP; |
1294 | 0 | eDT = GDT_Float32; |
1295 | 0 | } |
1296 | 0 | else if (chType == 'f' && nBytes == 8) |
1297 | 0 | { |
1298 | 0 | elt.nativeType = DtypeElt::NativeType::IEEEFP; |
1299 | 0 | eDT = GDT_Float64; |
1300 | 0 | } |
1301 | 0 | else if (chType == 'c' && nBytes == 8) |
1302 | 0 | { |
1303 | 0 | elt.nativeType = DtypeElt::NativeType::COMPLEX_IEEEFP; |
1304 | 0 | eDT = GDT_CFloat32; |
1305 | 0 | } |
1306 | 0 | else if (chType == 'c' && nBytes == 16) |
1307 | 0 | { |
1308 | 0 | elt.nativeType = DtypeElt::NativeType::COMPLEX_IEEEFP; |
1309 | 0 | eDT = GDT_CFloat64; |
1310 | 0 | } |
1311 | 0 | else if (chType == 'S') |
1312 | 0 | { |
1313 | 0 | elt.nativeType = DtypeElt::NativeType::STRING_ASCII; |
1314 | 0 | elt.gdalType = GDALExtendedDataType::CreateString(nBytes); |
1315 | 0 | elt.gdalSize = elt.gdalType.GetSize(); |
1316 | 0 | elts.emplace_back(elt); |
1317 | 0 | return GDALExtendedDataType::CreateString(nBytes); |
1318 | 0 | } |
1319 | 0 | else if (chType == 'U') |
1320 | 0 | { |
1321 | 0 | elt.nativeType = DtypeElt::NativeType::STRING_UNICODE; |
1322 | | // the dtype declaration is number of UCS4 characters. Store it |
1323 | | // as bytes |
1324 | 0 | elt.nativeSize *= 4; |
1325 | | // We can really map UCS4 size to UTF-8 |
1326 | 0 | elt.gdalType = GDALExtendedDataType::CreateString(); |
1327 | 0 | elt.gdalSize = elt.gdalType.GetSize(); |
1328 | 0 | elts.emplace_back(elt); |
1329 | 0 | return GDALExtendedDataType::CreateString(); |
1330 | 0 | } |
1331 | 0 | else |
1332 | 0 | break; |
1333 | 0 | elt.gdalType = GDALExtendedDataType::Create(eDT); |
1334 | 0 | elt.gdalSize = elt.gdalType.GetSize(); |
1335 | 0 | elts.emplace_back(elt); |
1336 | 0 | return GDALExtendedDataType::Create(eDT); |
1337 | 0 | } |
1338 | 0 | else if (obj.GetType() == CPLJSONObject::Type::Array) |
1339 | 0 | { |
1340 | 0 | bool error = false; |
1341 | 0 | const auto oArray = obj.ToArray(); |
1342 | 0 | std::vector<std::unique_ptr<GDALEDTComponent>> comps; |
1343 | 0 | size_t offset = 0; |
1344 | 0 | size_t alignmentMax = 1; |
1345 | 0 | for (const auto &oElt : oArray) |
1346 | 0 | { |
1347 | 0 | const auto oEltArray = oElt.ToArray(); |
1348 | 0 | if (!oEltArray.IsValid() || oEltArray.Size() != 2 || |
1349 | 0 | oEltArray[0].GetType() != CPLJSONObject::Type::String) |
1350 | 0 | { |
1351 | 0 | error = true; |
1352 | 0 | break; |
1353 | 0 | } |
1354 | 0 | GDALExtendedDataType subDT = ParseDtype(oEltArray[1], elts); |
1355 | 0 | if (subDT.GetClass() == GEDTC_NUMERIC && |
1356 | 0 | subDT.GetNumericDataType() == GDT_Unknown) |
1357 | 0 | { |
1358 | 0 | error = true; |
1359 | 0 | break; |
1360 | 0 | } |
1361 | | |
1362 | 0 | const std::string osName = oEltArray[0].ToString(); |
1363 | | // Add padding for alignment |
1364 | 0 | const size_t alignmentSub = GetAlignment(oEltArray[1]); |
1365 | 0 | assert(alignmentSub); |
1366 | 0 | alignmentMax = std::max(alignmentMax, alignmentSub); |
1367 | 0 | offset = AlignOffsetOn(offset, alignmentSub); |
1368 | 0 | comps.emplace_back(std::unique_ptr<GDALEDTComponent>( |
1369 | 0 | new GDALEDTComponent(osName, offset, subDT))); |
1370 | 0 | offset += subDT.GetSize(); |
1371 | 0 | } |
1372 | 0 | if (error) |
1373 | 0 | break; |
1374 | 0 | size_t nTotalSize = offset; |
1375 | 0 | nTotalSize = AlignOffsetOn(nTotalSize, alignmentMax); |
1376 | 0 | return GDALExtendedDataType::Create(obj.ToString(), nTotalSize, |
1377 | 0 | std::move(comps)); |
1378 | 0 | } |
1379 | 0 | } while (false); |
1380 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1381 | 0 | "Invalid or unsupported format for dtype: %s", |
1382 | 0 | obj.ToString().c_str()); |
1383 | 0 | return GDALExtendedDataType::Create(GDT_Unknown); |
1384 | 0 | } |
1385 | | |
1386 | | static void SetGDALOffset(const GDALExtendedDataType &dt, |
1387 | | const size_t nBaseOffset, std::vector<DtypeElt> &elts, |
1388 | | size_t &iCurElt) |
1389 | 0 | { |
1390 | 0 | if (dt.GetClass() == GEDTC_COMPOUND) |
1391 | 0 | { |
1392 | 0 | const auto &comps = dt.GetComponents(); |
1393 | 0 | for (const auto &comp : comps) |
1394 | 0 | { |
1395 | 0 | const size_t nBaseOffsetSub = nBaseOffset + comp->GetOffset(); |
1396 | 0 | SetGDALOffset(comp->GetType(), nBaseOffsetSub, elts, iCurElt); |
1397 | 0 | } |
1398 | 0 | } |
1399 | 0 | else |
1400 | 0 | { |
1401 | 0 | elts[iCurElt].gdalOffset = nBaseOffset; |
1402 | 0 | iCurElt++; |
1403 | 0 | } |
1404 | 0 | } |
1405 | | |
1406 | | /************************************************************************/ |
1407 | | /* ZarrV2Group::LoadArray() */ |
1408 | | /************************************************************************/ |
1409 | | |
1410 | | std::shared_ptr<ZarrArray> |
1411 | | ZarrV2Group::LoadArray(const std::string &osArrayName, |
1412 | | const std::string &osZarrayFilename, |
1413 | | const CPLJSONObject &oRoot, bool bLoadedFromZMetadata, |
1414 | | const CPLJSONObject &oAttributesIn) const |
1415 | 2 | { |
1416 | | // Add osZarrayFilename to m_poSharedResource during the scope |
1417 | | // of this function call. |
1418 | 2 | ZarrSharedResource::SetFilenameAdder filenameAdder(m_poSharedResource, |
1419 | 2 | osZarrayFilename); |
1420 | 2 | if (!filenameAdder.ok()) |
1421 | 0 | return nullptr; |
1422 | | |
1423 | 2 | const auto osFormat = oRoot["zarr_format"].ToString(); |
1424 | 2 | if (osFormat != "2") |
1425 | 2 | { |
1426 | 2 | CPLError(CE_Failure, CPLE_NotSupported, |
1427 | 2 | "Invalid value for zarr_format: %s", osFormat.c_str()); |
1428 | 2 | return nullptr; |
1429 | 2 | } |
1430 | | |
1431 | 0 | bool bFortranOrder = false; |
1432 | 0 | const char *orderKey = "order"; |
1433 | 0 | const auto osOrder = oRoot[orderKey].ToString(); |
1434 | 0 | if (osOrder == "C") |
1435 | 0 | { |
1436 | | // ok |
1437 | 0 | } |
1438 | 0 | else if (osOrder == "F") |
1439 | 0 | { |
1440 | 0 | bFortranOrder = true; |
1441 | 0 | } |
1442 | 0 | else |
1443 | 0 | { |
1444 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "Invalid value for %s", |
1445 | 0 | orderKey); |
1446 | 0 | return nullptr; |
1447 | 0 | } |
1448 | | |
1449 | 0 | const auto oShape = oRoot["shape"].ToArray(); |
1450 | 0 | if (!oShape.IsValid()) |
1451 | 0 | { |
1452 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "shape missing or not an array"); |
1453 | 0 | return nullptr; |
1454 | 0 | } |
1455 | | |
1456 | 0 | const char *chunksKey = "chunks"; |
1457 | 0 | const auto oChunks = oRoot[chunksKey].ToArray(); |
1458 | 0 | if (!oChunks.IsValid()) |
1459 | 0 | { |
1460 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s missing or not an array", |
1461 | 0 | chunksKey); |
1462 | 0 | return nullptr; |
1463 | 0 | } |
1464 | | |
1465 | 0 | if (oShape.Size() != oChunks.Size()) |
1466 | 0 | { |
1467 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1468 | 0 | "shape and chunks arrays are of different size"); |
1469 | 0 | return nullptr; |
1470 | 0 | } |
1471 | | |
1472 | 0 | CPLJSONObject oAttributes(oAttributesIn); |
1473 | 0 | if (!bLoadedFromZMetadata) |
1474 | 0 | { |
1475 | 0 | CPLJSONDocument oDoc; |
1476 | 0 | const std::string osZattrsFilename(CPLFormFilenameSafe( |
1477 | 0 | CPLGetDirnameSafe(osZarrayFilename.c_str()).c_str(), ".zattrs", |
1478 | 0 | nullptr)); |
1479 | 0 | CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); |
1480 | 0 | if (oDoc.Load(osZattrsFilename)) |
1481 | 0 | { |
1482 | 0 | oAttributes = oDoc.GetRoot(); |
1483 | 0 | } |
1484 | 0 | } |
1485 | | |
1486 | | // Deep-clone of oAttributes |
1487 | 0 | { |
1488 | 0 | CPLJSONDocument oTmpDoc; |
1489 | 0 | oTmpDoc.SetRoot(oAttributes); |
1490 | 0 | CPL_IGNORE_RET_VAL(oTmpDoc.LoadMemory(oTmpDoc.SaveAsString())); |
1491 | 0 | oAttributes = oTmpDoc.GetRoot(); |
1492 | 0 | } |
1493 | |
|
1494 | 0 | std::vector<std::shared_ptr<GDALDimension>> aoDims; |
1495 | 0 | for (int i = 0; i < oShape.Size(); ++i) |
1496 | 0 | { |
1497 | 0 | const auto nSize = static_cast<GUInt64>(oShape[i].ToLong()); |
1498 | 0 | if (nSize == 0) |
1499 | 0 | { |
1500 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid content for shape"); |
1501 | 0 | return nullptr; |
1502 | 0 | } |
1503 | 0 | aoDims.emplace_back(std::make_shared<ZarrDimension>( |
1504 | 0 | m_poSharedResource, |
1505 | 0 | std::dynamic_pointer_cast<ZarrGroupBase>(m_pSelf.lock()), |
1506 | 0 | std::string(), CPLSPrintf("dim%d", i), std::string(), std::string(), |
1507 | 0 | nSize)); |
1508 | 0 | } |
1509 | | |
1510 | | // XArray extension |
1511 | 0 | const auto arrayDimensionsObj = oAttributes["_ARRAY_DIMENSIONS"]; |
1512 | |
|
1513 | 0 | const auto FindDimension = |
1514 | 0 | [this, &aoDims, bLoadedFromZMetadata, &osArrayName, |
1515 | 0 | &oAttributes](const std::string &osDimName, |
1516 | 0 | std::shared_ptr<GDALDimension> &poDim, int i) |
1517 | 0 | { |
1518 | 0 | auto oIter = m_oMapDimensions.find(osDimName); |
1519 | 0 | if (oIter != m_oMapDimensions.end()) |
1520 | 0 | { |
1521 | 0 | if (m_bDimSizeInUpdate || |
1522 | 0 | oIter->second->GetSize() == poDim->GetSize()) |
1523 | 0 | { |
1524 | 0 | poDim = oIter->second; |
1525 | 0 | return true; |
1526 | 0 | } |
1527 | 0 | else |
1528 | 0 | { |
1529 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1530 | 0 | "Size of _ARRAY_DIMENSIONS[%d] different " |
1531 | 0 | "from the one of shape", |
1532 | 0 | i); |
1533 | 0 | return false; |
1534 | 0 | } |
1535 | 0 | } |
1536 | | |
1537 | | // Try to load the indexing variable. |
1538 | | |
1539 | | // If loading from zmetadata, we should have normally |
1540 | | // already loaded the dimension variables, unless they |
1541 | | // are in a upper level. |
1542 | 0 | if (bLoadedFromZMetadata && osArrayName != osDimName && |
1543 | 0 | m_oMapMDArrays.find(osDimName) == m_oMapMDArrays.end()) |
1544 | 0 | { |
1545 | 0 | auto poParent = m_poParent.lock(); |
1546 | 0 | while (poParent != nullptr) |
1547 | 0 | { |
1548 | 0 | oIter = poParent->m_oMapDimensions.find(osDimName); |
1549 | 0 | if (oIter != poParent->m_oMapDimensions.end() && |
1550 | 0 | oIter->second->GetSize() == poDim->GetSize()) |
1551 | 0 | { |
1552 | 0 | poDim = oIter->second; |
1553 | 0 | return true; |
1554 | 0 | } |
1555 | 0 | poParent = poParent->m_poParent.lock(); |
1556 | 0 | } |
1557 | 0 | } |
1558 | | |
1559 | | // Not loading from zmetadata, and not in m_oMapMDArrays, |
1560 | | // then stat() the indexing variable. |
1561 | 0 | else if (!bLoadedFromZMetadata && osArrayName != osDimName && |
1562 | 0 | m_oMapMDArrays.find(osDimName) == m_oMapMDArrays.end()) |
1563 | 0 | { |
1564 | 0 | std::string osDirName = m_osDirectoryName; |
1565 | 0 | while (true) |
1566 | 0 | { |
1567 | 0 | if (CPLHasPathTraversal(osDimName.c_str())) |
1568 | 0 | { |
1569 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1570 | 0 | "Path traversal detected in %s", |
1571 | 0 | osDimName.c_str()); |
1572 | 0 | return false; |
1573 | 0 | } |
1574 | 0 | const std::string osArrayFilenameDim = CPLFormFilenameSafe( |
1575 | 0 | CPLFormFilenameSafe(osDirName.c_str(), osDimName.c_str(), |
1576 | 0 | nullptr) |
1577 | 0 | .c_str(), |
1578 | 0 | ".zarray", nullptr); |
1579 | 0 | VSIStatBufL sStat; |
1580 | 0 | if (VSIStatL(osArrayFilenameDim.c_str(), &sStat) == 0) |
1581 | 0 | { |
1582 | 0 | CPLJSONDocument oDoc; |
1583 | 0 | if (oDoc.Load(osArrayFilenameDim)) |
1584 | 0 | { |
1585 | 0 | LoadArray(osDimName, osArrayFilenameDim, oDoc.GetRoot(), |
1586 | 0 | false, CPLJSONObject()); |
1587 | 0 | } |
1588 | 0 | } |
1589 | 0 | else |
1590 | 0 | { |
1591 | 0 | if ((cpl::starts_with(osDirName, JSON_REF_FS_PREFIX) || |
1592 | 0 | cpl::starts_with(osDirName, PARQUET_REF_FS_PREFIX)) && |
1593 | 0 | osDirName.back() == '}') |
1594 | 0 | { |
1595 | 0 | break; |
1596 | 0 | } |
1597 | | |
1598 | | // Recurse to upper level for datasets such as |
1599 | | // /vsis3/hrrrzarr/sfc/20210809/20210809_00z_anl.zarr/0.1_sigma_level/HAIL_max_fcst/0.1_sigma_level/HAIL_max_fcst |
1600 | 0 | std::string osDirNameNew = |
1601 | 0 | CPLGetPathSafe(osDirName.c_str()); |
1602 | 0 | if (!osDirNameNew.empty() && osDirNameNew != osDirName) |
1603 | 0 | { |
1604 | 0 | osDirName = std::move(osDirNameNew); |
1605 | 0 | continue; |
1606 | 0 | } |
1607 | 0 | } |
1608 | 0 | break; |
1609 | 0 | } |
1610 | 0 | } |
1611 | | |
1612 | 0 | oIter = m_oMapDimensions.find(osDimName); |
1613 | 0 | if (oIter != m_oMapDimensions.end() && |
1614 | 0 | oIter->second->GetSize() == poDim->GetSize()) |
1615 | 0 | { |
1616 | 0 | poDim = oIter->second; |
1617 | 0 | return true; |
1618 | 0 | } |
1619 | | |
1620 | 0 | std::string osType; |
1621 | 0 | std::string osDirection; |
1622 | 0 | if (aoDims.size() == 1 && osArrayName == osDimName) |
1623 | 0 | { |
1624 | 0 | ZarrArray::GetDimensionTypeDirection(oAttributes, osType, |
1625 | 0 | osDirection); |
1626 | 0 | } |
1627 | |
|
1628 | 0 | auto poDimLocal = std::make_shared<ZarrDimension>( |
1629 | 0 | m_poSharedResource, |
1630 | 0 | std::dynamic_pointer_cast<ZarrGroupBase>(m_pSelf.lock()), |
1631 | 0 | GetFullName(), osDimName, osType, osDirection, poDim->GetSize()); |
1632 | 0 | poDimLocal->SetXArrayDimension(); |
1633 | 0 | m_oMapDimensions[osDimName] = poDimLocal; |
1634 | 0 | poDim = poDimLocal; |
1635 | 0 | return true; |
1636 | 0 | }; |
1637 | |
|
1638 | 0 | if (arrayDimensionsObj.GetType() == CPLJSONObject::Type::Array) |
1639 | 0 | { |
1640 | 0 | const auto arrayDims = arrayDimensionsObj.ToArray(); |
1641 | 0 | if (arrayDims.Size() == oShape.Size()) |
1642 | 0 | { |
1643 | 0 | bool ok = true; |
1644 | 0 | for (int i = 0; i < oShape.Size(); ++i) |
1645 | 0 | { |
1646 | 0 | if (arrayDims[i].GetType() == CPLJSONObject::Type::String) |
1647 | 0 | { |
1648 | 0 | const auto osDimName = arrayDims[i].ToString(); |
1649 | 0 | ok &= FindDimension(osDimName, aoDims[i], i); |
1650 | 0 | } |
1651 | 0 | } |
1652 | 0 | if (ok) |
1653 | 0 | { |
1654 | 0 | oAttributes.Delete("_ARRAY_DIMENSIONS"); |
1655 | 0 | } |
1656 | 0 | } |
1657 | 0 | else |
1658 | 0 | { |
1659 | 0 | CPLError( |
1660 | 0 | CE_Warning, CPLE_AppDefined, |
1661 | 0 | "Size of _ARRAY_DIMENSIONS different from the one of shape"); |
1662 | 0 | } |
1663 | 0 | } |
1664 | | |
1665 | | // _NCZARR_ARRAY extension |
1666 | 0 | const auto nczarrArrayDimrefs = oRoot["_NCZARR_ARRAY"]["dimrefs"].ToArray(); |
1667 | 0 | if (nczarrArrayDimrefs.IsValid()) |
1668 | 0 | { |
1669 | 0 | const auto arrayDims = nczarrArrayDimrefs.ToArray(); |
1670 | 0 | if (arrayDims.Size() == oShape.Size()) |
1671 | 0 | { |
1672 | 0 | auto poRG = |
1673 | 0 | std::dynamic_pointer_cast<ZarrGroupBase>(m_pSelf.lock()); |
1674 | 0 | CPLAssert(poRG != nullptr); |
1675 | 0 | while (true) |
1676 | 0 | { |
1677 | 0 | auto poNewRG = poRG->m_poParent.lock(); |
1678 | 0 | if (poNewRG == nullptr) |
1679 | 0 | break; |
1680 | 0 | poRG = std::move(poNewRG); |
1681 | 0 | } |
1682 | |
|
1683 | 0 | for (int i = 0; i < oShape.Size(); ++i) |
1684 | 0 | { |
1685 | 0 | if (arrayDims[i].GetType() == CPLJSONObject::Type::String) |
1686 | 0 | { |
1687 | 0 | const auto osDimFullpath = arrayDims[i].ToString(); |
1688 | 0 | const std::string osArrayFullname = |
1689 | 0 | (GetFullName() != "/" ? GetFullName() : std::string()) + |
1690 | 0 | '/' + osArrayName; |
1691 | 0 | if (aoDims.size() == 1 && |
1692 | 0 | (osDimFullpath == osArrayFullname || |
1693 | 0 | osDimFullpath == "/" + osArrayFullname)) |
1694 | 0 | { |
1695 | | // If this is an indexing variable, then fetch the |
1696 | | // dimension type and direction, and patch the dimension |
1697 | 0 | std::string osType; |
1698 | 0 | std::string osDirection; |
1699 | 0 | ZarrArray::GetDimensionTypeDirection( |
1700 | 0 | oAttributes, osType, osDirection); |
1701 | |
|
1702 | 0 | auto poDimLocal = std::make_shared<ZarrDimension>( |
1703 | 0 | m_poSharedResource, |
1704 | 0 | std::dynamic_pointer_cast<ZarrGroupBase>( |
1705 | 0 | m_pSelf.lock()), |
1706 | 0 | GetFullName(), osArrayName, osType, osDirection, |
1707 | 0 | aoDims[i]->GetSize()); |
1708 | 0 | aoDims[i] = poDimLocal; |
1709 | |
|
1710 | 0 | m_oMapDimensions[osArrayName] = std::move(poDimLocal); |
1711 | 0 | } |
1712 | 0 | else if (auto poDim = |
1713 | 0 | poRG->OpenDimensionFromFullname(osDimFullpath)) |
1714 | 0 | { |
1715 | 0 | if (poDim->GetSize() != aoDims[i]->GetSize()) |
1716 | 0 | { |
1717 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1718 | 0 | "Inconsistency in size between NCZarr " |
1719 | 0 | "dimension %s and regular dimension", |
1720 | 0 | osDimFullpath.c_str()); |
1721 | 0 | } |
1722 | 0 | else |
1723 | 0 | { |
1724 | 0 | aoDims[i] = std::move(poDim); |
1725 | 0 | } |
1726 | 0 | } |
1727 | 0 | else |
1728 | 0 | { |
1729 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1730 | 0 | "Cannot find NCZarr dimension %s", |
1731 | 0 | osDimFullpath.c_str()); |
1732 | 0 | } |
1733 | 0 | } |
1734 | 0 | } |
1735 | 0 | } |
1736 | 0 | else |
1737 | 0 | { |
1738 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1739 | 0 | "Size of _NCZARR_ARRAY.dimrefs different from the one of " |
1740 | 0 | "shape"); |
1741 | 0 | } |
1742 | 0 | } |
1743 | |
|
1744 | 0 | constexpr const char *dtypeKey = "dtype"; |
1745 | 0 | auto oDtype = oRoot[dtypeKey]; |
1746 | 0 | if (!oDtype.IsValid()) |
1747 | 0 | { |
1748 | 0 | CPLError(CE_Failure, CPLE_NotSupported, "%s missing", dtypeKey); |
1749 | 0 | return nullptr; |
1750 | 0 | } |
1751 | 0 | std::vector<DtypeElt> aoDtypeElts; |
1752 | 0 | const auto oType = ParseDtype(oDtype, aoDtypeElts); |
1753 | 0 | if (oType.GetClass() == GEDTC_NUMERIC && |
1754 | 0 | oType.GetNumericDataType() == GDT_Unknown) |
1755 | 0 | return nullptr; |
1756 | 0 | size_t iCurElt = 0; |
1757 | 0 | SetGDALOffset(oType, 0, aoDtypeElts, iCurElt); |
1758 | |
|
1759 | 0 | std::vector<GUInt64> anBlockSize; |
1760 | 0 | if (!ZarrArray::ParseChunkSize(oChunks, oType, anBlockSize)) |
1761 | 0 | return nullptr; |
1762 | | |
1763 | 0 | std::string osDimSeparator = oRoot["dimension_separator"].ToString(); |
1764 | 0 | if (osDimSeparator.empty()) |
1765 | 0 | osDimSeparator = "."; |
1766 | |
|
1767 | 0 | std::vector<GByte> abyNoData; |
1768 | |
|
1769 | 0 | struct NoDataFreer |
1770 | 0 | { |
1771 | 0 | std::vector<GByte> &m_abyNodata; |
1772 | 0 | const GDALExtendedDataType &m_oType; |
1773 | |
|
1774 | 0 | NoDataFreer(std::vector<GByte> &abyNoDataIn, |
1775 | 0 | const GDALExtendedDataType &oTypeIn) |
1776 | 0 | : m_abyNodata(abyNoDataIn), m_oType(oTypeIn) |
1777 | 0 | { |
1778 | 0 | } |
1779 | |
|
1780 | 0 | ~NoDataFreer() |
1781 | 0 | { |
1782 | 0 | if (!m_abyNodata.empty()) |
1783 | 0 | m_oType.FreeDynamicMemory(&m_abyNodata[0]); |
1784 | 0 | } |
1785 | 0 | }; |
1786 | |
|
1787 | 0 | NoDataFreer NoDataFreer(abyNoData, oType); |
1788 | |
|
1789 | 0 | auto oFillValue = oRoot["fill_value"]; |
1790 | 0 | auto eFillValueType = oFillValue.GetType(); |
1791 | | |
1792 | | // Normally arrays are not supported, but that's what NCZarr 4.8.0 outputs |
1793 | 0 | if (eFillValueType == CPLJSONObject::Type::Array && |
1794 | 0 | oFillValue.ToArray().Size() == 1) |
1795 | 0 | { |
1796 | 0 | oFillValue = oFillValue.ToArray()[0]; |
1797 | 0 | eFillValueType = oFillValue.GetType(); |
1798 | 0 | } |
1799 | |
|
1800 | 0 | if (!oFillValue.IsValid()) |
1801 | 0 | { |
1802 | | // fill_value is normally required but some implementations |
1803 | | // are lacking it: https://github.com/Unidata/netcdf-c/issues/2059 |
1804 | 0 | CPLError(CE_Warning, CPLE_AppDefined, "fill_value missing"); |
1805 | 0 | } |
1806 | 0 | else if (eFillValueType == CPLJSONObject::Type::Null) |
1807 | 0 | { |
1808 | | // Nothing to do |
1809 | 0 | } |
1810 | 0 | else if (eFillValueType == CPLJSONObject::Type::String) |
1811 | 0 | { |
1812 | 0 | const auto osFillValue = oFillValue.ToString(); |
1813 | 0 | if (oType.GetClass() == GEDTC_NUMERIC && |
1814 | 0 | CPLGetValueType(osFillValue.c_str()) != CPL_VALUE_STRING) |
1815 | 0 | { |
1816 | 0 | abyNoData.resize(oType.GetSize()); |
1817 | | // Be tolerant with numeric values serialized as strings. |
1818 | 0 | if (oType.GetNumericDataType() == GDT_Int64) |
1819 | 0 | { |
1820 | 0 | const int64_t nVal = static_cast<int64_t>( |
1821 | 0 | std::strtoll(osFillValue.c_str(), nullptr, 10)); |
1822 | 0 | GDALCopyWords(&nVal, GDT_Int64, 0, &abyNoData[0], |
1823 | 0 | oType.GetNumericDataType(), 0, 1); |
1824 | 0 | } |
1825 | 0 | else if (oType.GetNumericDataType() == GDT_UInt64) |
1826 | 0 | { |
1827 | 0 | const uint64_t nVal = static_cast<uint64_t>( |
1828 | 0 | std::strtoull(osFillValue.c_str(), nullptr, 10)); |
1829 | 0 | GDALCopyWords(&nVal, GDT_UInt64, 0, &abyNoData[0], |
1830 | 0 | oType.GetNumericDataType(), 0, 1); |
1831 | 0 | } |
1832 | 0 | else |
1833 | 0 | { |
1834 | 0 | const double dfNoDataValue = CPLAtof(osFillValue.c_str()); |
1835 | 0 | GDALCopyWords(&dfNoDataValue, GDT_Float64, 0, &abyNoData[0], |
1836 | 0 | oType.GetNumericDataType(), 0, 1); |
1837 | 0 | } |
1838 | 0 | } |
1839 | 0 | else if (oType.GetClass() == GEDTC_NUMERIC) |
1840 | 0 | { |
1841 | 0 | double dfNoDataValue; |
1842 | 0 | if (osFillValue == "NaN") |
1843 | 0 | { |
1844 | 0 | dfNoDataValue = std::numeric_limits<double>::quiet_NaN(); |
1845 | 0 | } |
1846 | 0 | else if (osFillValue == "Infinity") |
1847 | 0 | { |
1848 | 0 | dfNoDataValue = std::numeric_limits<double>::infinity(); |
1849 | 0 | } |
1850 | 0 | else if (osFillValue == "-Infinity") |
1851 | 0 | { |
1852 | 0 | dfNoDataValue = -std::numeric_limits<double>::infinity(); |
1853 | 0 | } |
1854 | 0 | else |
1855 | 0 | { |
1856 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid fill_value"); |
1857 | 0 | return nullptr; |
1858 | 0 | } |
1859 | 0 | if (oType.GetNumericDataType() == GDT_Float16) |
1860 | 0 | { |
1861 | 0 | const GFloat16 hfNoDataValue = |
1862 | 0 | static_cast<GFloat16>(dfNoDataValue); |
1863 | 0 | abyNoData.resize(sizeof(hfNoDataValue)); |
1864 | 0 | memcpy(&abyNoData[0], &hfNoDataValue, sizeof(hfNoDataValue)); |
1865 | 0 | } |
1866 | 0 | if (oType.GetNumericDataType() == GDT_Float32) |
1867 | 0 | { |
1868 | 0 | const float fNoDataValue = static_cast<float>(dfNoDataValue); |
1869 | 0 | abyNoData.resize(sizeof(fNoDataValue)); |
1870 | 0 | memcpy(&abyNoData[0], &fNoDataValue, sizeof(fNoDataValue)); |
1871 | 0 | } |
1872 | 0 | else if (oType.GetNumericDataType() == GDT_Float64) |
1873 | 0 | { |
1874 | 0 | abyNoData.resize(sizeof(dfNoDataValue)); |
1875 | 0 | memcpy(&abyNoData[0], &dfNoDataValue, sizeof(dfNoDataValue)); |
1876 | 0 | } |
1877 | 0 | else |
1878 | 0 | { |
1879 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid fill_value"); |
1880 | 0 | return nullptr; |
1881 | 0 | } |
1882 | 0 | } |
1883 | 0 | else if (oType.GetClass() == GEDTC_STRING) |
1884 | 0 | { |
1885 | | // zarr.open('unicode_be.zarr', mode = 'w', shape=(1,), dtype = |
1886 | | // '>U1', compressor = None) oddly generates "fill_value": "0" |
1887 | 0 | if (osFillValue != "0") |
1888 | 0 | { |
1889 | 0 | std::vector<GByte> abyNativeFillValue(osFillValue.size() + 1); |
1890 | 0 | memcpy(&abyNativeFillValue[0], osFillValue.data(), |
1891 | 0 | osFillValue.size()); |
1892 | 0 | int nBytes = CPLBase64DecodeInPlace(&abyNativeFillValue[0]); |
1893 | 0 | abyNativeFillValue.resize(nBytes + 1); |
1894 | 0 | abyNativeFillValue[nBytes] = 0; |
1895 | 0 | abyNoData.resize(oType.GetSize()); |
1896 | 0 | char *pDstStr = CPLStrdup( |
1897 | 0 | reinterpret_cast<const char *>(&abyNativeFillValue[0])); |
1898 | 0 | char **pDstPtr = reinterpret_cast<char **>(&abyNoData[0]); |
1899 | 0 | memcpy(pDstPtr, &pDstStr, sizeof(pDstStr)); |
1900 | 0 | } |
1901 | 0 | } |
1902 | 0 | else |
1903 | 0 | { |
1904 | 0 | std::vector<GByte> abyNativeFillValue(osFillValue.size() + 1); |
1905 | 0 | memcpy(&abyNativeFillValue[0], osFillValue.data(), |
1906 | 0 | osFillValue.size()); |
1907 | 0 | int nBytes = CPLBase64DecodeInPlace(&abyNativeFillValue[0]); |
1908 | 0 | abyNativeFillValue.resize(nBytes); |
1909 | 0 | if (abyNativeFillValue.size() != |
1910 | 0 | aoDtypeElts.back().nativeOffset + aoDtypeElts.back().nativeSize) |
1911 | 0 | { |
1912 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid fill_value"); |
1913 | 0 | return nullptr; |
1914 | 0 | } |
1915 | 0 | abyNoData.resize(oType.GetSize()); |
1916 | 0 | ZarrArray::DecodeSourceElt(aoDtypeElts, abyNativeFillValue.data(), |
1917 | 0 | &abyNoData[0]); |
1918 | 0 | } |
1919 | 0 | } |
1920 | 0 | else if (eFillValueType == CPLJSONObject::Type::Boolean || |
1921 | 0 | eFillValueType == CPLJSONObject::Type::Integer || |
1922 | 0 | eFillValueType == CPLJSONObject::Type::Long || |
1923 | 0 | eFillValueType == CPLJSONObject::Type::Double) |
1924 | 0 | { |
1925 | 0 | if (oType.GetClass() == GEDTC_NUMERIC) |
1926 | 0 | { |
1927 | 0 | const double dfNoDataValue = oFillValue.ToDouble(); |
1928 | 0 | if (oType.GetNumericDataType() == GDT_Int64) |
1929 | 0 | { |
1930 | 0 | const int64_t nNoDataValue = |
1931 | 0 | static_cast<int64_t>(oFillValue.ToLong()); |
1932 | 0 | abyNoData.resize(oType.GetSize()); |
1933 | 0 | GDALCopyWords(&nNoDataValue, GDT_Int64, 0, &abyNoData[0], |
1934 | 0 | oType.GetNumericDataType(), 0, 1); |
1935 | 0 | } |
1936 | 0 | else if (oType.GetNumericDataType() == GDT_UInt64 && |
1937 | | /* we can't really deal with nodata value between */ |
1938 | | /* int64::max and uint64::max due to json-c limitations */ |
1939 | 0 | dfNoDataValue >= 0) |
1940 | 0 | { |
1941 | 0 | const int64_t nNoDataValue = |
1942 | 0 | static_cast<int64_t>(oFillValue.ToLong()); |
1943 | 0 | abyNoData.resize(oType.GetSize()); |
1944 | 0 | GDALCopyWords(&nNoDataValue, GDT_Int64, 0, &abyNoData[0], |
1945 | 0 | oType.GetNumericDataType(), 0, 1); |
1946 | 0 | } |
1947 | 0 | else |
1948 | 0 | { |
1949 | 0 | abyNoData.resize(oType.GetSize()); |
1950 | 0 | GDALCopyWords(&dfNoDataValue, GDT_Float64, 0, &abyNoData[0], |
1951 | 0 | oType.GetNumericDataType(), 0, 1); |
1952 | 0 | } |
1953 | 0 | } |
1954 | 0 | else |
1955 | 0 | { |
1956 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid fill_value"); |
1957 | 0 | return nullptr; |
1958 | 0 | } |
1959 | 0 | } |
1960 | 0 | else |
1961 | 0 | { |
1962 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid fill_value"); |
1963 | 0 | return nullptr; |
1964 | 0 | } |
1965 | | |
1966 | 0 | const CPLCompressor *psCompressor = nullptr; |
1967 | 0 | const CPLCompressor *psDecompressor = nullptr; |
1968 | 0 | const auto oCompressor = oRoot["compressor"]; |
1969 | 0 | std::string osDecompressorId("NONE"); |
1970 | |
|
1971 | 0 | if (!oCompressor.IsValid()) |
1972 | 0 | { |
1973 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "compressor missing"); |
1974 | 0 | return nullptr; |
1975 | 0 | } |
1976 | 0 | if (oCompressor.GetType() == CPLJSONObject::Type::Null) |
1977 | 0 | { |
1978 | | // nothing to do |
1979 | 0 | } |
1980 | 0 | else if (oCompressor.GetType() == CPLJSONObject::Type::Object) |
1981 | 0 | { |
1982 | 0 | osDecompressorId = oCompressor["id"].ToString(); |
1983 | 0 | if (osDecompressorId.empty()) |
1984 | 0 | { |
1985 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing compressor id"); |
1986 | 0 | return nullptr; |
1987 | 0 | } |
1988 | 0 | if (osDecompressorId == "imagecodecs_tiff") |
1989 | 0 | { |
1990 | 0 | psDecompressor = ZarrGetTIFFDecompressor(); |
1991 | 0 | } |
1992 | 0 | else |
1993 | 0 | { |
1994 | 0 | psCompressor = CPLGetCompressor(osDecompressorId.c_str()); |
1995 | 0 | psDecompressor = CPLGetDecompressor(osDecompressorId.c_str()); |
1996 | 0 | if (psCompressor == nullptr || psDecompressor == nullptr) |
1997 | 0 | { |
1998 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1999 | 0 | "Decompressor %s not handled", |
2000 | 0 | osDecompressorId.c_str()); |
2001 | 0 | return nullptr; |
2002 | 0 | } |
2003 | 0 | } |
2004 | 0 | } |
2005 | 0 | else |
2006 | 0 | { |
2007 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid compressor"); |
2008 | 0 | return nullptr; |
2009 | 0 | } |
2010 | | |
2011 | 0 | CPLJSONArray oFiltersArray; |
2012 | 0 | const auto oFilters = oRoot["filters"]; |
2013 | 0 | if (!oFilters.IsValid()) |
2014 | 0 | { |
2015 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "filters missing"); |
2016 | 0 | return nullptr; |
2017 | 0 | } |
2018 | 0 | if (oFilters.GetType() == CPLJSONObject::Type::Null) |
2019 | 0 | { |
2020 | 0 | } |
2021 | 0 | else if (oFilters.GetType() == CPLJSONObject::Type::Array) |
2022 | 0 | { |
2023 | 0 | oFiltersArray = oFilters.ToArray(); |
2024 | 0 | for (const auto &oFilter : oFiltersArray) |
2025 | 0 | { |
2026 | 0 | const auto osFilterId = oFilter["id"].ToString(); |
2027 | 0 | if (osFilterId.empty()) |
2028 | 0 | { |
2029 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Missing filter id"); |
2030 | 0 | return nullptr; |
2031 | 0 | } |
2032 | 0 | if (!EQUAL(osFilterId.c_str(), "shuffle") && |
2033 | 0 | !EQUAL(osFilterId.c_str(), "quantize") && |
2034 | 0 | !EQUAL(osFilterId.c_str(), "fixedscaleoffset")) |
2035 | 0 | { |
2036 | 0 | const auto psFilterCompressor = |
2037 | 0 | CPLGetCompressor(osFilterId.c_str()); |
2038 | 0 | const auto psFilterDecompressor = |
2039 | 0 | CPLGetDecompressor(osFilterId.c_str()); |
2040 | 0 | if (psFilterCompressor == nullptr || |
2041 | 0 | psFilterDecompressor == nullptr) |
2042 | 0 | { |
2043 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2044 | 0 | "Filter %s not handled", osFilterId.c_str()); |
2045 | 0 | return nullptr; |
2046 | 0 | } |
2047 | 0 | } |
2048 | 0 | } |
2049 | 0 | } |
2050 | 0 | else |
2051 | 0 | { |
2052 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid filters"); |
2053 | 0 | return nullptr; |
2054 | 0 | } |
2055 | | |
2056 | 0 | auto poArray = |
2057 | 0 | ZarrV2Array::Create(m_poSharedResource, Self(), osArrayName, aoDims, |
2058 | 0 | oType, aoDtypeElts, anBlockSize, bFortranOrder); |
2059 | 0 | if (!poArray) |
2060 | 0 | return nullptr; |
2061 | 0 | poArray->SetCompressorJson(oCompressor); |
2062 | 0 | poArray->SetUpdatable(m_bUpdatable); // must be set before SetAttributes() |
2063 | 0 | poArray->SetFilename(osZarrayFilename); |
2064 | 0 | poArray->SetDimSeparator(osDimSeparator); |
2065 | 0 | poArray->SetCompressorDecompressor(osDecompressorId, psCompressor, |
2066 | 0 | psDecompressor); |
2067 | 0 | poArray->SetFilters(oFiltersArray); |
2068 | 0 | if (!abyNoData.empty()) |
2069 | 0 | { |
2070 | 0 | poArray->RegisterNoDataValue(abyNoData.data()); |
2071 | 0 | } |
2072 | |
|
2073 | 0 | const auto gridMapping = oAttributes["grid_mapping"]; |
2074 | 0 | if (gridMapping.GetType() == CPLJSONObject::Type::String) |
2075 | 0 | { |
2076 | 0 | const std::string gridMappingName = gridMapping.ToString(); |
2077 | 0 | if (m_oMapMDArrays.find(gridMappingName) == m_oMapMDArrays.end()) |
2078 | 0 | { |
2079 | 0 | if (CPLHasPathTraversal(gridMappingName.c_str())) |
2080 | 0 | { |
2081 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2082 | 0 | "Path traversal detected in %s", |
2083 | 0 | gridMappingName.c_str()); |
2084 | 0 | return nullptr; |
2085 | 0 | } |
2086 | 0 | const std::string osArrayFilenameDim = CPLFormFilenameSafe( |
2087 | 0 | CPLFormFilenameSafe(m_osDirectoryName.c_str(), |
2088 | 0 | gridMappingName.c_str(), nullptr) |
2089 | 0 | .c_str(), |
2090 | 0 | ".zarray", nullptr); |
2091 | 0 | VSIStatBufL sStat; |
2092 | 0 | if (VSIStatL(osArrayFilenameDim.c_str(), &sStat) == 0) |
2093 | 0 | { |
2094 | 0 | CPLJSONDocument oDoc; |
2095 | 0 | if (oDoc.Load(osArrayFilenameDim)) |
2096 | 0 | { |
2097 | 0 | LoadArray(gridMappingName, osArrayFilenameDim, |
2098 | 0 | oDoc.GetRoot(), false, CPLJSONObject()); |
2099 | 0 | } |
2100 | 0 | } |
2101 | 0 | } |
2102 | 0 | } |
2103 | | |
2104 | 0 | poArray->SetAttributes(Self(), oAttributes); |
2105 | 0 | poArray->SetDtype(oDtype); |
2106 | 0 | RegisterArray(poArray); |
2107 | | |
2108 | | // If this is an indexing variable, attach it to the dimension. |
2109 | 0 | if (aoDims.size() == 1 && aoDims[0]->GetName() == poArray->GetName()) |
2110 | 0 | { |
2111 | 0 | auto oIter = m_oMapDimensions.find(poArray->GetName()); |
2112 | 0 | if (oIter != m_oMapDimensions.end()) |
2113 | 0 | { |
2114 | 0 | oIter->second->SetIndexingVariable(poArray); |
2115 | 0 | } |
2116 | 0 | } |
2117 | |
|
2118 | 0 | if (CPLTestBool(m_poSharedResource->GetOpenOptions().FetchNameValueDef( |
2119 | 0 | "CACHE_TILE_PRESENCE", "NO"))) |
2120 | 0 | { |
2121 | 0 | poArray->BlockCachePresence(); |
2122 | 0 | } |
2123 | |
|
2124 | 0 | return poArray; |
2125 | 0 | } |
2126 | | |
2127 | | /************************************************************************/ |
2128 | | /* ZarrV2Array::SetCompressorJson() */ |
2129 | | /************************************************************************/ |
2130 | | |
2131 | | void ZarrV2Array::SetCompressorJson(const CPLJSONObject &oCompressor) |
2132 | 0 | { |
2133 | 0 | m_oCompressorJSon = oCompressor; |
2134 | 0 | if (oCompressor.GetType() != CPLJSONObject::Type::Null) |
2135 | 0 | m_aosStructuralInfo.SetNameValue("COMPRESSOR", |
2136 | 0 | oCompressor.ToString().c_str()); |
2137 | 0 | } |
2138 | | |
2139 | | /************************************************************************/ |
2140 | | /* ZarrV2Array::SetFilters() */ |
2141 | | /************************************************************************/ |
2142 | | |
2143 | | void ZarrV2Array::SetFilters(const CPLJSONArray &oFiltersArray) |
2144 | 215 | { |
2145 | 215 | m_oFiltersArray = oFiltersArray; |
2146 | 215 | if (oFiltersArray.Size() > 0) |
2147 | 0 | m_aosStructuralInfo.SetNameValue("FILTERS", |
2148 | 0 | oFiltersArray.ToString().c_str()); |
2149 | 215 | } |
2150 | | |
2151 | | /************************************************************************/ |
2152 | | /* ZarrV2Array::GetRawBlockInfoInfo() */ |
2153 | | /************************************************************************/ |
2154 | | |
2155 | | CPLStringList ZarrV2Array::GetRawBlockInfoInfo() const |
2156 | 0 | { |
2157 | 0 | CPLStringList aosInfo(m_aosStructuralInfo); |
2158 | 0 | if (!m_aoDtypeElts.empty() && m_aoDtypeElts[0].nativeSize > 1 && |
2159 | 0 | m_aoDtypeElts[0].nativeType != DtypeElt::NativeType::STRING_ASCII && |
2160 | 0 | m_aoDtypeElts[0].nativeType != DtypeElt::NativeType::STRING_UNICODE) |
2161 | 0 | { |
2162 | 0 | if (m_aoDtypeElts[0].needByteSwapping ^ CPL_IS_LSB) |
2163 | 0 | aosInfo.SetNameValue("ENDIANNESS", "LITTLE"); |
2164 | 0 | else |
2165 | 0 | aosInfo.SetNameValue("ENDIANNESS", "BIG"); |
2166 | 0 | } |
2167 | 0 | if (m_bFortranOrder) |
2168 | 0 | { |
2169 | 0 | const int nDims = static_cast<int>(m_aoDims.size()); |
2170 | 0 | if (nDims > 1) |
2171 | 0 | { |
2172 | 0 | std::string osOrder("["); |
2173 | 0 | for (int i = 0; i < nDims; ++i) |
2174 | 0 | { |
2175 | 0 | if (i > 0) |
2176 | 0 | osOrder += ','; |
2177 | 0 | osOrder += std::to_string(nDims - 1 - i); |
2178 | 0 | } |
2179 | 0 | osOrder += ']'; |
2180 | 0 | aosInfo.SetNameValue("TRANSPOSE_ORDER", osOrder.c_str()); |
2181 | 0 | } |
2182 | 0 | } |
2183 | 0 | return aosInfo; |
2184 | 0 | } |