Commit 728e6dc6 authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Allow updating files also in the Python wrapper for OpenZGY/C++. Currently...

Allow updating files also in the Python wrapper for OpenZGY/C++. Currently with more restrictions than in native C++. Work in progress.
parent 39961e00
......@@ -1083,6 +1083,7 @@ ZgyWriter_create(ZgyClass* self, PyObject* args, PyObject* keywds)
const_cast<char*>("lodcompressor"), // O
const_cast<char*>("zfp_compressor"), // f
const_cast<char*>("zfp_lodcompressor"), // f
const_cast<char*>("update"), // p
NULL
};
......@@ -1104,8 +1105,9 @@ ZgyWriter_create(ZgyClass* self, PyObject* args, PyObject* keywds)
PyObject* compressor_obj{nullptr};
PyObject* lodcompressor_obj{nullptr};
float zfp_snr = 0, zfp_lodsnr = 0;
int update{0};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s(iii)|$(iii)O(ff)OOssddff(ff)(ff)((dd)(dd)(dd)(dd))OOOff", kwlist,
if (!PyArg_ParseTupleAndKeywords(args, keywds, "s(iii)|$(iii)O(ff)OOssddff(ff)(ff)((dd)(dd)(dd)(dd))OOOffp", kwlist,
&filename,
&size[0], &size[1], &size[2], // (iii)
&bricksize[0], &bricksize[1], &bricksize[2], // (iii)
......@@ -1128,6 +1130,7 @@ ZgyWriter_create(ZgyClass* self, PyObject* args, PyObject* keywds)
&iocontext_obj,
&compressor_obj, &lodcompressor_obj,
&zfp_snr, &zfp_lodsnr,
&update,
NULL))
return NULL;
......@@ -1190,7 +1193,9 @@ ZgyWriter_create(ZgyClass* self, PyObject* args, PyObject* keywds)
self->pimpl_->logger_(1, "Create \"" + std::string(filename) + "\"\n");
//writer_args.dump(std::cout);
self->pimpl_->writer_ = OpenZGY::IZgyWriter::open(writer_args);
self->pimpl_->writer_ = update ?
OpenZGY::IZgyWriter::reopen(writer_args) :
OpenZGY::IZgyWriter::open(writer_args);
self->pimpl_->meta_ = self->pimpl_->writer_;
self->pimpl_->filename = std::string(filename);
self->pimpl_->numthreads = 1;
......
......@@ -2365,6 +2365,83 @@ def testFancy14():
checkStatistics(fn.name, newzgy.ZgyReader, 0, 0, True, maxdelta=5000)
#FAILS checkHistogram(fn.name, newzgy.ZgyReader, 0, 0, True)
def testUpdateExisting(filename, zgyWriterFactory = newzgy.ZgyWriter, zgyReaderFactory = newzgy.ZgyReader):
"""
See that we can open an existing file for write in some circumstances.
This feature is currently only supported in OpenZGY/C++ and its Python
wrapper, not in the pure OpenZGY/Python.
"""
firstargs = {
"filename": filename,
"iocontext": SDCredentials(),
"size": (33, 28, 92),
"bricksize": (64, 64, 64),
"datatype": SampleDataType.int16,
"datarange": (-32768,+32767),
}
secondargs = {
"filename": filename,
"iocontext": SDCredentials(),
# Work in progress; should not need to re-specify these.
# But the wrapper has "size" as a mandatory argument.
# Also, the wrapper will touch every member of the
# ZgyWriterArgs instance even if we didn't specify them.
"size": (33, 28, 92),
"bricksize": (64, 64, 64),
"datatype": SampleDataType.int16,
"datarange": (-32768,+32767),
# End work in progress.
"annotstart": (1, 500),
"annotinc": (1, 2),
"zstart": 100,
"zinc": 4,
"zunitdim": UnitDimension.time,
"zunitname": "ms",
"zunitfactor": 0.001, # Should that have been 1000?
"hunitdim": UnitDimension.length,
"hunitname": "ft",
"hunitfactor": 0.3048,
"corners": (( 5, 7),
( 5, 107),
(205, 7),
(105, 107)),
}
with zgyWriterFactory(update = False, **firstargs) as writer:
pass
# Try to re-open it. Should work since there are no data blocks.
with zgyWriterFactory(update = True, **secondargs) as writer:
pass
# Re-open again, re-specifying the basic info (no error unless it changes)
# and not re-specifying the mutable part (should then be retained).
# Work in progress. Fails because all ZgyWriterArgs members are touched.
#with zgyWriterFactory(update = True, **firstargs) as writer:
# pass
with zgyReaderFactory(filename, iocontext = SDCredentials()) as reader:
assert reader.size == (33, 28, 92)
assert reader.bricksize == (64, 64, 64)
assert reader.datatype == SampleDataType.int16
assert reader.datarange == (-32768,+32767)
assert reader.annotstart == (1, 500)
assert reader.annotinc == (1, 2)
assert reader.zstart == 100
assert reader.zinc == 4
assert reader.zunitdim == UnitDimension.time
assert reader.zunitname == "ms"
assert reader.zunitfactor == 0.001 # Should that have been 1000?
assert reader.hunitdim == UnitDimension.length
assert reader.hunitname == "ft"
assert reader.hunitfactor == 0.3048
assert np.allclose(reader.corners, (( 5, 7),
( 5, 107),
(205, 7),
(205, 107)))
def testCloudAutoDelete():
with CloudFileAutoDelete("xyzzy", None) as fn:
assert fn.name[:5] == "sd://"
......@@ -2950,6 +3027,9 @@ def Main():
testFancy12()
with TimeMe("TestFancy13"):
testFancy13()
with TimeMe("UpdateExisting"):
with LocalFileAutoDelete("update-existing.zgy") as fn:
testUpdateExisting(fn.name)
# ZFP COMPRESSION
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment