@@ -33,9 +33,9 @@ void yyyymmdd2ymd(uint64_t yyyymmdd, long& y, long& m, long& d) {
33
33
}
34
34
35
35
void hhmmss2hms (uint64_t hhmmss, long & h, long & m, long & s) {
36
- h = static_cast <long >(hhmmss % 100 );
36
+ s = static_cast <long >(hhmmss % 100 );
37
37
m = static_cast <long >((hhmmss % 10000 ) / 100 );
38
- s = static_cast <long >((hhmmss % 1000000 ) / 10000 );
38
+ h = static_cast <long >((hhmmss % 1000000 ) / 10000 );
39
39
if (s < 0 || s > 59 ) {
40
40
throw eckit::SeriousBug (" invalid seconds range" , Here ());
41
41
}
@@ -57,7 +57,38 @@ eckit::DateTime yyyymmdd_hhmmss2DateTime(uint64_t yyyymmdd, uint64_t hhmmss) {
57
57
} // namespace
58
58
59
59
60
- OperationWindow::OperationWindow (std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsConfiguration& cfg) :
60
+ OperationWindow make_window ( const std::unique_ptr<PeriodUpdater>& periodUpdater, const StatisticsConfiguration& cfg) {
61
+ eckit::DateTime epochPoint{cfg.epoch ()};
62
+ eckit::DateTime startPoint{periodUpdater->computeWinStartTime (cfg.winStart ())};
63
+ eckit::DateTime creationPoint{periodUpdater->computeWinCreationTime (cfg.winStart ())};
64
+ eckit::DateTime endPoint{periodUpdater->computeWinEndTime (startPoint)};
65
+ long windowType = 0 ;
66
+ if ( cfg.options ().windowType () == " forward-offset" ){
67
+ windowType = 0 ;
68
+ }
69
+ else if ( cfg.options ().windowType () == " backward-offset" ) {
70
+ windowType = 1 ;
71
+ }
72
+ else {
73
+ std::ostringstream os;
74
+ os << " Unknown window type: " << cfg.options ().windowType () << std::endl;
75
+ throw eckit::SeriousBug (os.str (), Here ());
76
+ };
77
+ return OperationWindow{epochPoint, startPoint, creationPoint, endPoint, cfg.timeStep (), windowType};
78
+ };
79
+
80
+ OperationWindow load_window ( std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsOptions& opt ) {
81
+ IOmanager->pushDir ( " operationWindow" );
82
+ // std::ostringstream logos;
83
+ // logos << " - Loading operationWindow from: " << IOmanager->getCurrentDir() << std::endl;
84
+ // LOG_DEBUG_LIB(LibMultio) << logos.str() << std::endl;
85
+ OperationWindow opwin{IOmanager, opt};
86
+ IOmanager->popDir ();
87
+ return opwin;
88
+ };
89
+
90
+
91
+ OperationWindow::OperationWindow (std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsOptions& opt) :
61
92
epochPoint_{eckit::Date{0 }, eckit::Time{0 }},
62
93
startPoint_{eckit::Date{0 }, eckit::Time{0 }},
63
94
creationPoint_{eckit::Date{0 }, eckit::Time{0 }},
@@ -66,14 +97,15 @@ OperationWindow::OperationWindow(std::shared_ptr<StatisticsIO>& IOmanager, const
66
97
endPoint_{eckit::Date{0 }, eckit::Time{0 }},
67
98
lastFlush_{eckit::Date{0 }, eckit::Time{0 }},
68
99
timeStepInSeconds_{0 },
69
- count_{0 } {
70
- load (IOmanager, cfg);
100
+ count_{0 },
101
+ type_{0 } {
102
+ load (IOmanager, opt);
71
103
return ;
72
104
}
73
105
74
106
OperationWindow::OperationWindow (const eckit::DateTime& epochPoint, const eckit::DateTime& startPoint,
75
107
const eckit::DateTime& creationPoint, const eckit::DateTime& endPoint,
76
- long timeStepInSeconds) :
108
+ long timeStepInSeconds, long windowType ) :
77
109
epochPoint_{epochPoint},
78
110
startPoint_{startPoint},
79
111
creationPoint_{creationPoint},
@@ -82,26 +114,28 @@ OperationWindow::OperationWindow(const eckit::DateTime& epochPoint, const eckit:
82
114
endPoint_{endPoint},
83
115
lastFlush_{epochPoint},
84
116
timeStepInSeconds_{timeStepInSeconds},
85
- count_{0 } {}
117
+ count_{0 },
118
+ type_{windowType} {}
119
+
86
120
87
121
long OperationWindow::count () const {
88
122
return count_;
89
123
}
90
124
91
- void OperationWindow::load (std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsConfiguration& cfg) {
125
+ void OperationWindow::dump (std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsOptions& opt) const {
92
126
IOBuffer restartState{IOmanager->getBuffer (restartSize ())};
93
- IOmanager->read (" window" , restartSize ());
94
- deserialize (restartState);
95
127
restartState.zero ();
128
+ serialize (restartState, IOmanager->getCurrentDir () + " /operationWindow_dump.txt" , opt );
129
+ IOmanager->write (" operationWindow" , static_cast <size_t >(16 ), restartSize () );
130
+ IOmanager->flush ();
96
131
return ;
97
132
}
98
133
99
- void OperationWindow::dump (std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsConfiguration& cfg) const {
134
+ void OperationWindow::load (std::shared_ptr<StatisticsIO>& IOmanager, const StatisticsOptions& opt) {
100
135
IOBuffer restartState{IOmanager->getBuffer (restartSize ())};
136
+ IOmanager->read ( " operationWindow" , restartSize () );
137
+ deserialize (restartState, IOmanager->getCurrentDir () + " /operationWindow_load.txt" , opt);
101
138
restartState.zero ();
102
- serialize (restartState);
103
- IOmanager->write (" window" , restartSize ());
104
- IOmanager->flush ();
105
139
return ;
106
140
}
107
141
@@ -126,9 +160,32 @@ void OperationWindow::updateWindow(const eckit::DateTime& startPoint, const ecki
126
160
return ;
127
161
}
128
162
163
+ std::string OperationWindow::windowType () const {
164
+ if (type_ == 0 ) {
165
+ return std::string{" forward-offset" };
166
+ } else if (type_ == 1 ) {
167
+ return std::string{" backward-offset" };
168
+ } else {
169
+ std::ostringstream os;
170
+ os << *this << " Unknown window type " << std::endl;
171
+ throw eckit::SeriousBug (os.str (), Here ());
172
+ }
173
+ }
174
+
129
175
130
176
bool OperationWindow::isWithin (const eckit::DateTime& dt) const {
131
- bool ret = gtLowerBound (dt, true ) && leUpperBound (dt, false );
177
+ bool ret;
178
+ if ( type_ == 0 ) {
179
+ ret = gtLowerBound (dt, false ) && leUpperBound (dt, false );
180
+ }
181
+ else if ( type_ == 1 ) {
182
+ ret = geLowerBound (dt, false ) && ltUpperBound (dt, false );
183
+ }
184
+ else {
185
+ std::ostringstream os;
186
+ os << *this << " Unknown window type " << std::endl;
187
+ throw eckit::SeriousBug (os.str (), Here ());
188
+ }
132
189
LOG_DEBUG_LIB (LibMultio) << " ------ Is " << dt << " within " << *this << " ? -- " << (ret ? " yes" : " no" )
133
190
<< std::endl;
134
191
return ret;
@@ -143,6 +200,15 @@ bool OperationWindow::gtLowerBound(const eckit::DateTime& dt, bool throw_error)
143
200
return dt > creationPoint_;
144
201
};
145
202
203
+ bool OperationWindow::geLowerBound (const eckit::DateTime& dt, bool throw_error) const {
204
+ if (throw_error && creationPoint_ > dt) {
205
+ std::ostringstream os;
206
+ os << *this << " : " << dt << " is outside of current period : lower Bound violation" << std::endl;
207
+ throw eckit::SeriousBug (os.str (), Here ());
208
+ }
209
+ return dt >= creationPoint_;
210
+ };
211
+
146
212
bool OperationWindow::leUpperBound (const eckit::DateTime& dt, bool throw_error) const {
147
213
// TODO: test without 1 second added. Now it should work
148
214
if (throw_error && dt > endPoint ()) {
@@ -153,6 +219,16 @@ bool OperationWindow::leUpperBound(const eckit::DateTime& dt, bool throw_error)
153
219
return dt <= endPoint ();
154
220
};
155
221
222
+ bool OperationWindow::ltUpperBound (const eckit::DateTime& dt, bool throw_error) const {
223
+ // TODO: test without 1 second added. Now it should work
224
+ if (throw_error && dt >= endPoint ()) {
225
+ std::ostringstream os;
226
+ os << *this << " : " << dt << " is outside of current period : upper Bound violation" << std::endl;
227
+ throw eckit::SeriousBug (os.str (), Here ());
228
+ }
229
+ return dt < endPoint ();
230
+ };
231
+
156
232
long OperationWindow::timeSpanInHours () const {
157
233
return long (endPoint_ - creationPoint_) / 3600 ;
158
234
}
@@ -356,8 +432,22 @@ long OperationWindow::lastFlushInSteps() const {
356
432
return (lastFlush_ - epochPoint_) / timeStepInSeconds_;
357
433
}
358
434
359
- void OperationWindow::serialize (IOBuffer& currState) const {
360
-
435
+ void OperationWindow::serialize (IOBuffer& currState, const std::string& fname, const StatisticsOptions& opt) const {
436
+
437
+ if ( opt.debugRestart () ) {
438
+ std::ofstream outFile (fname);
439
+ outFile << " epochPoint_ :: " << epochPoint_ << std::endl;
440
+ outFile << " startPoint_ :: " << startPoint_ << std::endl;
441
+ outFile << " endPoint_ :: " << endPoint_ << std::endl;
442
+ outFile << " creationPoint_ :: " << creationPoint_ << std::endl;
443
+ outFile << " prevPoint_ :: " << prevPoint_ << std::endl;
444
+ outFile << " currPoint_ :: " << currPoint_ << std::endl;
445
+ outFile << " lastFlush_ :: " << lastFlush_ << std::endl;
446
+ outFile << " timeStepInSeconds_ :: " << timeStepInSeconds_ << std::endl;
447
+ outFile << " count_ :: " << count_ << std::endl;
448
+ outFile << " type_ :: " << type_ << std::endl;
449
+ outFile.close ();
450
+ }
361
451
362
452
currState[0 ] = static_cast <std::uint64_t >(epochPoint_.date ().yyyymmdd ());
363
453
currState[1 ] = static_cast <std::uint64_t >(epochPoint_.time ().hhmmss ());
@@ -377,18 +467,19 @@ void OperationWindow::serialize(IOBuffer& currState) const {
377
467
currState[10 ] = static_cast <std::uint64_t >(currPoint_.date ().yyyymmdd ());
378
468
currState[11 ] = static_cast <std::uint64_t >(currPoint_.time ().hhmmss ());
379
469
380
- currState[12 ] = static_cast <std::uint64_t >(currPoint_ .date ().yyyymmdd ());
381
- currState[13 ] = static_cast <std::uint64_t >(currPoint_ .time ().hhmmss ());
470
+ currState[12 ] = static_cast <std::uint64_t >(lastFlush_ .date ().yyyymmdd ());
471
+ currState[13 ] = static_cast <std::uint64_t >(lastFlush_ .time ().hhmmss ());
382
472
383
473
currState[14 ] = static_cast <std::uint64_t >(timeStepInSeconds_);
384
474
currState[15 ] = static_cast <std::uint64_t >(count_);
475
+ currState[16 ] = static_cast <std::uint64_t >(type_);
385
476
386
477
currState.computeChecksum ();
387
478
388
479
return ;
389
480
}
390
481
391
- void OperationWindow::deserialize (const IOBuffer& currState) {
482
+ void OperationWindow::deserialize (const IOBuffer& currState, const std::string& fname, const StatisticsOptions& opt ) {
392
483
393
484
currState.checkChecksum ();
394
485
epochPoint_ = yyyymmdd_hhmmss2DateTime (static_cast <long >(currState[0 ]), static_cast <long >(currState[1 ]));
@@ -400,16 +491,32 @@ void OperationWindow::deserialize(const IOBuffer& currState) {
400
491
lastFlush_ = yyyymmdd_hhmmss2DateTime (static_cast <long >(currState[12 ]), static_cast <long >(currState[13 ]));
401
492
timeStepInSeconds_ = static_cast <long >(currState[14 ]);
402
493
count_ = static_cast <long >(currState[15 ]);
494
+ type_ = static_cast <long >(currState[16 ]);
495
+
496
+ if ( opt.debugRestart () ) {
497
+ std::ofstream outFile (fname);
498
+ outFile << " epochPoint_ :: " << epochPoint_ << std::endl;
499
+ outFile << " startPoint_ :: " << startPoint_ << std::endl;
500
+ outFile << " endPoint_ :: " << endPoint_ << std::endl;
501
+ outFile << " creationPoint_ :: " << creationPoint_ << std::endl;
502
+ outFile << " prevPoint_ :: " << prevPoint_ << std::endl;
503
+ outFile << " currPoint_ :: " << currPoint_ << std::endl;
504
+ outFile << " lastFlush_ :: " << lastFlush_ << std::endl;
505
+ outFile << " timeStepInSeconds_ :: " << timeStepInSeconds_ << std::endl;
506
+ outFile << " count_ :: " << count_ << std::endl;
507
+ outFile << " type_ :: " << type_ << std::endl;
508
+ outFile.close ();
509
+ }
403
510
404
511
return ;
405
512
}
406
513
407
514
size_t OperationWindow::restartSize () const {
408
- return static_cast <size_t >(17 );
515
+ return static_cast <size_t >(18 );
409
516
}
410
517
411
518
void OperationWindow::print (std::ostream& os) const {
412
- os << " MovingWindow (" << startPoint_ << " to " << endPoint () << " )" ;
519
+ os << " OperationWindow (" << startPoint_ << " to " << endPoint () << " )" ;
413
520
}
414
521
415
522
std::ostream& operator <<(std::ostream& os, const OperationWindow& a) {
0 commit comments