itom
Loading...
Searching...
No Matches
pythonDateTime.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2021, Institut für Technische Optik (ITO),
5 Universität Stuttgart, Germany
6
7 This file is part of itom.
8
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13
14 itom is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17 General Public Licence for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with itom. If not, see <http://www.gnu.org/licenses/>.
21*********************************************************************** */
22
23#pragma once
24
25/* includes */
26#ifndef Q_MOC_RUN
27 // see numpy help ::array api :: Miscellaneous :: Importing the api (this line
28 // must before include global.h)
29 #define PY_ARRAY_UNIQUE_SYMBOL itom_ARRAY_API
30 #define NO_IMPORT_ARRAY
31
32 #include "python/pythonWrapper.h"
33#endif
34
35#include "DataObject/dataobj.h"
36#include "common/helperDatetime.h"
37#include "common/typeDefs.h"
38
39namespace ito {
40
42{
43public:
44 // checks for Python datetime and corresponding numpy types
45 static bool PyDateTime_CheckExt(PyObject* obj);
46
47 // checks for Python time delta and corresponding numpy types
48 static bool PyTimeDelta_CheckExt(PyObject* obj);
49
50 static DateTime GetDateTime(PyObject* obj, bool& ok);
51 static TimeDelta GetTimeDelta(PyObject* obj, bool& ok);
52
53 // new ref, sets a PyException if an error occurs
54 static PyObject* GetPyDateTime(const DateTime& datetime);
55
56 // new ref, sets a PyException if an error occurs
57 static PyObject* GetPyTimeDelta(const TimeDelta& delta);
58
59 static bool ItoDatetime2npyDatetime(
60 const ito::DateTime& src, npy_datetime& dest, const PyArray_DatetimeMetaData& meta);
61
62 static bool NpyDatetime2itoDatetime(
63 const npy_datetime& dt, const PyArray_DatetimeMetaData &md, ito::DateTime& out);
64
65 static bool ItoTimedelta2npyTimedleta(
66 const ito::TimeDelta& src, npy_timedelta& dest, const PyArray_DatetimeMetaData& meta);
67
68 static bool NpyTimedelta2itoTimedelta(
69 const npy_timedelta& dt, const PyArray_DatetimeMetaData &md, ito::TimeDelta& out);
70
71 template <typename _Tp, size_t timeMemberOffset>
72 static void GuessDateTimeMetaFromDataObjectValues(
73 const ito::DataObject* dobj, PyArray_DatetimeMetaData& descr_meta)
74 {
75 ito::int64 combined = 0LL;
76 int dims = dobj->getDims();
77 const cv::Mat* plane;
78 const _Tp* rowPtr;
79 const uchar* item;
80
81 if (dims > 0)
82 {
83 for (int p = 0; p < dobj->getNumPlanes(); ++p)
84 {
85 plane = dobj->getCvPlaneMat(p);
86
87 for (int r = 0; r < plane->rows; ++r)
88 {
89 rowPtr = plane->ptr<_Tp>(r);
90
91 for (int c = 0; c < plane->cols; ++c)
92 {
93 item = reinterpret_cast<const uchar*>(&(rowPtr[c]));
94
95 combined |= *reinterpret_cast<const ito::int64*>(item + timeMemberOffset);
96 }
97 }
98 }
99 }
100
101 // set defaults
102 descr_meta.num = 1;
103 descr_meta.base = NPY_FR_us;
104
105 if (combined % 1000LL == 0)
106 {
107 // no microseconds --> milli
108 if (combined % 1000000LL == 0)
109 {
110 // no milliseconds
111 if (combined % 1000000000LL == 0)
112 {
113 // no seconds
114 if (combined % (60LL * 1000000000LL) == 0)
115 {
116 // no minutes
117 if (combined % (3600LL * 1000000000LL) == 0)
118 {
119 // no hours
120 descr_meta.base = NPY_FR_D;
121 }
122 else
123 {
124 descr_meta.base = NPY_FR_h;
125 }
126 }
127 else
128 {
129 descr_meta.base = NPY_FR_m;
130 }
131 }
132 else
133 {
134 descr_meta.base = NPY_FR_s;
135 }
136 }
137 else
138 {
139 descr_meta.base = NPY_FR_ms;
140 }
141 }
142 else
143 {
144 descr_meta.base = NPY_FR_us;
145 }
146 }
147};
148
149}; // end namespace ito
dataObject contains a n-dimensional matrix
Definition dataobj.h:591
int getDims(void) const
Definition dataobj.h:882
int getNumPlanes(void) const
calculates numbers of single opencv matrices which are part of the ROI which has previously been set.
Definition dataobj.h:908
cv::Mat * getCvPlaneMat(const int planeIndex)
returns pointer to cv::Mat plane with given index considering a possible roi.
Definition dataobj.cpp:1317
Definition typeDefs.h:161
Definition pythonDateTime.h:42
Definition typeDefs.h:218
Definition apiFunctionsGraph.cpp:40