@@ -44,14 +44,16 @@ public function getParameter(): string
44
44
* Parse the textual representation of DWD Data, can be filtered by specifying before and after.
45
45
* This means if you specify after - you will get timestamps after the specified team
46
46
* If you also specify before you can pinpoint values.
47
+ * @deprecated
47
48
* @param String $content - Textual representation of a DWD Hourly/Recent pressure file.
48
- * @param DateTime |null $start - returns all values after the specific time
49
- * @param DateTime |null $end - returns all values after $after AND after if set.
49
+ * @param Carbon |null $startDate - returns all values after the specific time
50
+ * @param Carbon |null $endDate - returns all values after $after AND after if set.
50
51
* @return array of parameters
51
52
* @throws ParseError
52
53
*/
53
- public function parseHourlyData (String $ content , DWDStation $ nearestStation , Coordinate $ coordinate , DateTime $ start = null , DateTime $ end = null ): array
54
+ public function parseHourlyDataOld (String $ content , DWDStation $ nearestStation , Coordinate $ coordinate , Carbon $ startDate = null , Carbon $ endDate = null ): array
54
55
{
56
+ $ time = microtime (true );
55
57
$ lines = explode ('eor ' , $ content );
56
58
$ data = array ();
57
59
@@ -61,7 +63,35 @@ public function parseHourlyData(String $content, DWDStation $nearestStation, Coo
61
63
* 2. from this day, calculate the hour difference between requested and $start+$end
62
64
* 3. jump to the specific lines
63
65
* 4. parse
66
+ * DOES NOT PROVIDE CORRECT DATA - the dwd files do skip missing data instead of providing "-999" as value, as such the optimization will not work.
64
67
*/
68
+
69
+ // $newestDate = null;
70
+ // //retrieve the latest line that contains a valid date
71
+ // for ($i = count($lines) - 1; !isset($newestDate); $i++) {
72
+ // $newestData = str_replace(' ', '', $lines[count($lines) - $i]);
73
+ // $cols = explode(';', $newestData);
74
+ // if (sizeof($cols) > 3){
75
+ // $newestDate = Carbon::createFromFormat($this->getTimeFormat(), $cols[1], 'utc');
76
+ // break;
77
+ // }
78
+ // }
79
+ // /* @var $newestDate Carbon */
80
+ // $start = min($newestDate->diffInHours($startDate), $newestDate->diffInHours($endDate));
81
+ // $end = max($newestDate->diffInHours($startDate), $newestDate->diffInHours($endDate));
82
+ // DWDUtil::log("MAXMIN", "start=" .$start."; end=".$end. "available Lines=".count($lines));
83
+ // //Retrieve the rest of the data that is found between start and end.
84
+ // for ($i = $start; $i<count($lines) && $i < $end; $i++) {
85
+ // $lines[$i] = str_replace(' ', '', $lines[$i]);
86
+ // $cols = explode(';', $lines[$i]);
87
+ // $date = Carbon::createFromFormat($this->getTimeFormat(), $cols[1], 'utc');
88
+ // if (isset($date)) {
89
+ // $data[] = $this->createParameter($cols, $date, $nearestStation, $coordinate);
90
+ // } else
91
+ // throw new ParseError(self::class . " - Error while parsing date: col=" . $cols[1] . " | date=" . $date);
92
+ // }
93
+
94
+ DWDUtil::log ("PARSER " , "DATE=[ " . $ endDate ->toIso8601String () . ", " . $ startDate ->toIso8601String () . "] " );
65
95
for ($ i = sizeof ($ lines ) - 1 ; $ i > 0 ; $ i --) {
66
96
$ lines [$ i ] = str_replace (' ' , '' , $ lines [$ i ]);
67
97
@@ -75,7 +105,7 @@ public function parseHourlyData(String $content, DWDStation $nearestStation, Coo
75
105
switch (func_num_args ()) {
76
106
//$start is set
77
107
case 4 : {
78
- if ($ date >= $ start ) {
108
+ if ($ date >= $ startDate ) {
79
109
$ temp = $ this ->createParameter ($ cols , $ date , $ nearestStation , $ coordinate );
80
110
81
111
$ data [] = $ temp ;
@@ -87,12 +117,12 @@ public function parseHourlyData(String $content, DWDStation $nearestStation, Coo
87
117
}
88
118
//$start & $end are set
89
119
case 5 : {
90
- if ($ date <= $ end && $ date >= $ start ) {
120
+ if ($ date <= $ endDate && $ date >= $ startDate ) {
91
121
$ temp = $ this ->createParameter ($ cols , $ date , $ nearestStation , $ coordinate );
92
122
93
123
$ data [] = $ temp ;
94
124
} else
95
- if ($ date <= $ start ) {
125
+ if ($ date <= $ startDate ) {
96
126
//break from loop and switch
97
127
break 2 ;
98
128
}
@@ -109,10 +139,64 @@ public function parseHourlyData(String $content, DWDStation $nearestStation, Coo
109
139
} else
110
140
throw new ParseError (self ::class . " - Error while parsing date: col= " . $ cols [1 ] . " | date= " . $ date );
111
141
}
142
+ DWDUtil::log ("PARSER " , "RetCount= " . count ($ data ));
143
+
144
+ DWDUtil::log ("TIMER " , "Duration= " . (microtime (true ) - $ time ));
145
+
146
+ return $ data ;
147
+ }
148
+
149
+
150
+ public function parseHourlyData (String $ content , DWDStation $ nearestStation , Coordinate $ coordinate , Carbon $ startDate , Carbon $ endDate ): array
151
+ {
152
+ $ start = $ startDate ->format ($ this ->getTimeFormat ());
153
+ $ end = $ endDate ->format ($ this ->getTimeFormat ());
154
+
155
+ $ content = str_replace ([" " , PHP_EOL ], "" , $ content );
156
+ $ lines = explode (";eor " , $ content );
157
+ $ data = [];
158
+ $ startIndex = $ this ->binarySearch ($ start , $ lines );
159
+ print "go station= $ nearestStation<br> " ;
160
+ for ($ i = (int )$ startIndex ; $ i < count ($ lines ); $ i ++) {
161
+ $ cols = explode ('; ' , $ lines [$ i ]);
162
+ $ date = Carbon::createFromFormat ($ this ->getTimeFormat (), $ cols [1 ], 'utc ' );
112
163
164
+ print $ i . ": " . $ date ->toIso8601String () . ">>> DIFF= " . $ endDate ->diff ($ date )->h . "<br> " ;
165
+
166
+ if ($ date <= $ endDate && $ date >= $ startDate ) {
167
+ print "1.= " .(int )($ date <= $ endDate )." 2.= " .(int )($ date >= $ startDate )."<br> " ;
168
+ $ temp = $ this ->createParameter ($ cols , $ date , $ nearestStation , $ coordinate );
169
+ $ data [] = $ temp ;
170
+ } else
171
+ break ; //break if we exceed our end point
172
+
173
+ }
113
174
return $ data ;
114
175
}
115
176
177
+ /** Finds the position of $item in $array
178
+ * @param $item
179
+ * @param $array
180
+ * @return int
181
+ */
182
+ private function binarySearch ($ item , $ array )
183
+ {
184
+ $ low = 0 ;
185
+ $ high = count ($ array );
186
+ while ($ high - $ low > 1 ) {
187
+ $ center = ($ high + $ low ) / 2 ;
188
+ // print("high=$high, low=$low, center=$center -- val=".(int)explode(';', $array[$center])[1]."<br>");
189
+ if ((int )explode ('; ' , $ array [$ center ])[1 ] < (int )$ item ) {
190
+ $ low = $ center ;
191
+ } else
192
+ $ high = $ center ;
193
+ }
194
+ if ($ high == count ($ array ))
195
+ throw new ParseError (self ::class . " - Error while searching for position of item= " . $ item . "high= " . $ high . "; val= " . $ array [$ high ]);
196
+ else
197
+ return $ high + 1 ;
198
+ }
199
+
116
200
/**
117
201
* Get the dateformat. default is in dwdHourly->parserSettings->dateFormat. Override as needed (ex. solar).
118
202
* @return string
0 commit comments