From f20bf97c5dd9d1b0f161eee5018cf0d4cc0ba49c Mon Sep 17 00:00:00 2001 From: FedorSarafanov Date: Fri, 29 Jul 2022 15:39:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D1=87=D1=82=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B1=D0=BB?= =?UTF-8?q?=D0=BE=D0=BA=D0=BE=D0=B2,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=20?= =?UTF-8?q?=D1=81=D1=8B=D1=80=D1=8B=D1=85=20=D0=B1=D0=B8=D0=BD=D0=B0=D1=80?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85,=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=20seek?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.py | 2 +- src/main.cpp | 22 +++++++++++++++++----- tests/test.bin | Bin 0 -> 6344 bytes tests/test.py | 15 +++++++++++++-- tests/test2.bin | Bin 0 -> 6344 bytes 5 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 tests/test.bin create mode 100644 tests/test2.bin diff --git a/setup.py b/setup.py index a45a27d..868245a 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ from pybind11 import get_cmake_dir from pybind11.setup_helpers import Pybind11Extension, build_ext from setuptools import setup -__version__ = "0.1.2" +__version__ = "0.1.3" ext_modules = [ Pybind11Extension( diff --git a/src/main.cpp b/src/main.cpp index 1c33a49..683fa43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,9 +3,14 @@ #include #include #include +#include #include #include #include "stormpci.h" +#include "string.h" + +namespace py = pybind11; + #define STRINGIFY(x) #x #define MACRO_STRINGIFY(x) STRINGIFY(x) @@ -43,10 +48,12 @@ double distance(double lat1, double lon1, double lat2, double lon2) class BlockReader { public: - BlockReader(const std::string &fn){ + BlockReader(const std::string &fn, long int seek_position){ f = std::ifstream(fn, std::fstream::binary); + pos = seek_position; } BlockBinary& read_block(){ + f.seekg(pos); f.read(reinterpret_cast(&_single_block), sizeof(BlockBinary)); if (not f){ throw; @@ -56,13 +63,16 @@ public: return _single_block; } std::vector& read_all_blocks(){ + f.seekg(pos); bool read_successfully = true; while (read_successfully){ f.read(reinterpret_cast(&_single_block), sizeof(BlockBinary)); - if (not f){ + if (f) { + _blocks_vector.push_back(_single_block); + pos += sizeof(BlockBinary); + } else { read_successfully = false; } - _blocks_vector.push_back(_single_block); } return _blocks_vector; } @@ -76,7 +86,6 @@ private: std::vector _blocks_vector; }; -namespace py = pybind11; PYBIND11_MODULE(stormpci_bin_reader, m) { m.doc() = R"pbdoc( @@ -92,7 +101,7 @@ PYBIND11_MODULE(stormpci_bin_reader, m) { py::class_(m, "BlockReader") - .def(py::init()) + .def(py::init(), py::arg("fn"), py::arg("seek")=int(0)) .def("read_block", &BlockReader::read_block) .def("read_all_blocks", &BlockReader::read_all_blocks); @@ -100,6 +109,9 @@ PYBIND11_MODULE(stormpci_bin_reader, m) { .def_readonly("strike", &BlockBinary::strike) .def_readonly("device_id", &BlockBinary::device_id) .def_readonly("experiment_id", &BlockBinary::experiment_id) + .def_property_readonly("raw_data", [](const BlockBinary bb) { + return py::bytes(reinterpret_cast(&bb), sizeof(bb)); + }) .def("__repr__", [](const BlockBinary &bb) { std::stringstream stream; diff --git a/tests/test.bin b/tests/test.bin new file mode 100644 index 0000000000000000000000000000000000000000..9afba222731c7e21b1c2e45ecc0bfe7b9fb55530 GIT binary patch literal 6344 zcmeH|yKa*~5QW!?A?rZlf(VHM2n0mWD?o)rWAP%~@BBcxgr_hSQl5cV0Nw)8(@`-e z`9JAsw7*S75y#T_?9QAyvzJ+Wy&Qf<0Y}yPEgr)Pp1@J<=~dX*3byNi-|{N-(eWOa z|F^%6&g;TJjbDXjR9yDP3?I45!|-+Clfm_Tjk8~KIB1~Bx?Iom89So&HgsjXJ!(vS z>AnnmF<{TbFMF2kX+Iaro|v_K9{2I%$ip6`vE~oMK#kq-3$uCNgGL_wtmkV%U-|dL zGRl|z)I}c+>PQ0}@*O=6&wcl-rnI$Q&XctZi4o^cK;0>KuKRbx4-)HwM;{LLrN_m` z&=A5=57L{C>Qn4pjF;K_;JAJ0=gN~`bGN;i%RZUm`xrhp@_9=Im4+>5*e?@e*wqa{7}Y3y3` zGw6DGOdjaEt=}=PmDi?N&SBo#KegZp#pBF}&^bDPS=S@?&OPIIo_yXT;(G5eMuR)% zJ>t(OUD+Z%#aXX->fKxq-CURY^dN|yw8=%&Q897gJ1VBR3|;EUM}PaB=pM2WSFXmc zK|Opzc;sb-;|RxV%h#I29g2Gn{Ret3>vX=xaNJLRM*7{~nmF*#U=C78C?7icz4;t1 zQ0wFjTWgjNE_FSJ{lf#v5t0WI)4XNA-c~&8peLk0NX+LNw+t>d{XB_LgEPyR-wN*r ze;2js_l(klk1lhe+mMcP7^CI-#Dw_Wm-YGmaD+qtvO3ZyM<_#2WAa%Cced!h{a&2M z{kkS&V%()L-#K$3eZc$V|BZUmL)($Hq0!23)ye%&eUSRhh4T4-c;--tJ#)5>oQorS zSj26FzJ#*q>e4neAXP@i1?(EI$M!gcA^GcY8 b%}P00w}UT7DU*6FqNCkX?u-vT|MLA8O&$xo literal 0 HcmV?d00001 diff --git a/tests/test.py b/tests/test.py index 09c81a5..12e8d3b 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,7 +1,18 @@ from stormpci_bin_reader import BlockReader -br = BlockReader("2022-04-23-00:00:37.bin") +br = BlockReader("2022-04-23-00:00:37.bin", seek=0) blocks = br.read_all_blocks() -for block in blocks: +# print(len(blocks)) + +for j, block in enumerate(blocks): + if j == 0: + # print(len(block.raw_data)) + with open("test.bin", "wb") as f: + f.write(block.raw_data) print(block) + +# for tests run in bash: +# head -c 6344 2022-04-23-00\:00\:37.bin > test2.bin +# and +# diff test2.bin test.bin diff --git a/tests/test2.bin b/tests/test2.bin new file mode 100644 index 0000000000000000000000000000000000000000..9afba222731c7e21b1c2e45ecc0bfe7b9fb55530 GIT binary patch literal 6344 zcmeH|yKa*~5QW!?A?rZlf(VHM2n0mWD?o)rWAP%~@BBcxgr_hSQl5cV0Nw)8(@`-e z`9JAsw7*S75y#T_?9QAyvzJ+Wy&Qf<0Y}yPEgr)Pp1@J<=~dX*3byNi-|{N-(eWOa z|F^%6&g;TJjbDXjR9yDP3?I45!|-+Clfm_Tjk8~KIB1~Bx?Iom89So&HgsjXJ!(vS z>AnnmF<{TbFMF2kX+Iaro|v_K9{2I%$ip6`vE~oMK#kq-3$uCNgGL_wtmkV%U-|dL zGRl|z)I}c+>PQ0}@*O=6&wcl-rnI$Q&XctZi4o^cK;0>KuKRbx4-)HwM;{LLrN_m` z&=A5=57L{C>Qn4pjF;K_;JAJ0=gN~`bGN;i%RZUm`xrhp@_9=Im4+>5*e?@e*wqa{7}Y3y3` zGw6DGOdjaEt=}=PmDi?N&SBo#KegZp#pBF}&^bDPS=S@?&OPIIo_yXT;(G5eMuR)% zJ>t(OUD+Z%#aXX->fKxq-CURY^dN|yw8=%&Q897gJ1VBR3|;EUM}PaB=pM2WSFXmc zK|Opzc;sb-;|RxV%h#I29g2Gn{Ret3>vX=xaNJLRM*7{~nmF*#U=C78C?7icz4;t1 zQ0wFjTWgjNE_FSJ{lf#v5t0WI)4XNA-c~&8peLk0NX+LNw+t>d{XB_LgEPyR-wN*r ze;2js_l(klk1lhe+mMcP7^CI-#Dw_Wm-YGmaD+qtvO3ZyM<_#2WAa%Cced!h{a&2M z{kkS&V%()L-#K$3eZc$V|BZUmL)($Hq0!23)ye%&eUSRhh4T4-c;--tJ#)5>oQorS zSj26FzJ#*q>e4neAXP@i1?(EI$M!gcA^GcY8 b%}P00w}UT7DU*6FqNCkX?u-vT|MLA8O&$xo literal 0 HcmV?d00001