-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.php
608 lines (556 loc) · 12.7 KB
/
analysis.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
require("public.php");
//config
//percentage of elements
$pertemp = 0.3;//temperature
$perstat = 0.5;//status
$perhum = 0.1;//humidity
$perwd = 0.05;//wind direction
$perws = 0.05;//wind speed
$db;
if (!dbInit())//init db
{
die();
}
$qqresult = getLatestQQResult();
foreach($qqresult as $citystatus)
{
getWeatherResult($citystatus);
getNmcResult($citystatus);
calcScore($citystatus);
}
foreach($qqresult as $result)
{
print $result->name.": score: $result->score isValid: $result->isValid";
if (!$result->isValid)
print " $result->error";
print "\n";
$qq = $result->current['qq'];
$weather = $result->current['weather'];
$nmc = $result->current['nmc'];
print "\t\tqq: ($qq->updateTime) $qq->state $qq->temp $qq->hu $qq->wd $qq->ws\n";
print "\t\tweather:($weather->updateTime) $weather->state $weather->temp $weather->hu $weather->wd $weather->ws\n";
print "\t\tnmc:($nmc->updateTime) $nmc->state $nmc->temp $nmc->hu $nmc->wd $nmc->ws\n";
print "\n";
}
function getLatestQQResult()
{
global $db;
$result = $db->query("select * from (select * from current where source=3 order by updateTime desc) tmp group by city");
if (!$result)
{
print "query error $db->error\n";
return false;
}
while($row = $result->fetch_row())
{
$ct = new CityStatus;
$ct->name = $row[0];
$ct->current['qq'] = json_decode($row[3]);
$qqresult[] = $ct;
}
return $qqresult;
}
function getWeatherResult($citystatus)
{
global $db;
$result = $db->query("select * from current where source=1 and city='$citystatus->name' and updateTime <= addtime('{$citystatus->current['qq']->updateTime}','00:30:00') order by updateTime desc limit 0,1");
while($row = $result->fetch_row())
{
$citystatus->current['weather'] = json_decode($row[3]);
}
$result = $db->query("select * from forecast where source=1 and city='$citystatus->name' and updateTime <= '{$citystatus->current['weather']->updateTime}' order by updateTime desc limit 0,1");
if (!$result)
{
print "query error $db->error\n";
return false;
}
while($row = $result->fetch_row())
{
$citystatus->updateTime['weather'] = $row[1];
$citystatus->forecast['weather'] = json_decode($row[3]);
}
getState($citystatus->current['weather'], $citystatus->updateTime['weather'], $citystatus->forecast['weather']);
}
function getNmcResult($citystatus)
{
global $db;
$result = $db->query("select * from current where source=2 and city='$citystatus->name' and updateTime <= addtime('{$citystatus->current['qq']->updateTime}', '00:30:00') order by updateTime desc limit 0,1");
while($row = $result->fetch_row())
{
$citystatus->current['nmc'] = json_decode($row[3]);
}
$result = $db->query("select * from forecast where source=2 and city='$citystatus->name' and updateTime <= '{$citystatus->current['weather']->updateTime}' order by updateTime desc limit 0,1");
if (!$result)
{
print "query error $db->error\n";
return false;
}
while($row = $result->fetch_row())
{
$citystatus->updateTime['nmc'] = $row[1];
$citystatus->forecast['nmc'] = json_decode($row[3]);
}
getState($citystatus->current['nmc'], $citystatus->updateTime['nmc'], $citystatus->forecast['nmc']);
}
function calcScore($citystatus)
{
global $pertemp;
global $perhum;
global $perwd;
global $perws;
global $perstat;
$citystatus->isValid = true;
$citystatus->score = 0;
$citystatus->error = '';
$qq = $citystatus->current['qq'];
$weather = $citystatus->current['weather'];
$nmc = $citystatus->current['nmc'];
$useweather = true;
$usenmc = true;
if (abs(strtotime($qq->updateTime) - strtotime($weather->updateTime)) >= 3*3600)//二者数据误差超过三个小时,没有参考价值
{
$useweather = false;
}
if (abs(strtotime($qq->updateTime) - strtotime($nmc->updateTime)) >= 3*3600)//二者数据误差超过三个小时,没有参考价值
{
$usenmc = false;
}
if (!$useweather && !$usenmc)
return;
//temperature
$tempscore = 0;
if ($useweather)
{
preg_match('/\d+/', $weather->temp, $tempa);
$temp = (int)$tempa[0];
$qqtemp = (int)$qq->temp;
$res = calcTempScore($temp, $qqtemp);
if ($res === false)
{
$citystatus->isValid = false;
$citystatus->error = "温度温差过大: weather.com.cn:$temp, qq:$qqtemp";
}else{
$tempscore += $res;
}
}
if ($usenmc)
{
$temp = (int)$nmc->temp;
$res = calcTempScore((int)$nmc->temp, (int)$qq->temp);
if ($res === false)
{
$citystatus->isValid = false;
$citystatus->error .= " 温度温差过大: weather.com.cn:$temp, qq:$qqtemp";
}else{
$tempscore += $res;
}
}
if (!$citystatus->isValid)
{
return;
}
if ($useweather && $usenmc)
{
$citystatus->score += (double)$pertemp*$tempscore/2;
}else{
$citystatus->score += $pertemp*$tempscore;
}
//temperature done
//humidity
$tempscore = 0;
if ($useweather)
{
preg_match('/\d+/', $weather->hu, $hua);
$hu = (double)$hua[0];
$res = calcHumidScore($hu, (double)$qq->hu);
$tempscore += $res;
}
if ($usenmc)
{
preg_match('/\d+/', $nmc->hu, $hua);
$hu = (double)$hua[0];
$res = calcHumidScore($hu, (double)$qq->hu);
$tempscore += $res;
}
if ($useweather && $usenmc)
{
$citystatus->score += (double)$perhum*$tempscore/2;
}else{
$citystatus->score += $perhum*$tempscore;
}
//humidity done
//wind direction
$tempscore = 0;
if ($useweather)
{
$res = calcWindDirec($weather->wd, $qq->wd);
if ($res === false)
{
//未知风向
}else{
$tempscore += $res;
}
}
if ($usenmc)
{
$res = calcWindDirec($nmc->wd, $qq->wd);
if ($res === false)
{
//未知风向
}else{
$tempscore += $res;
}
}
if ($useweather && $usenmc)
{
$citystatus->score += (double)$perwd*$tempscore/2;
}else{
$citystatus->score += $perwd*$tempscore;
}
//humidity done
//wind speed
$tempscore = 0;
if ($useweather)
{
$res = calcWindSpd($weather->ws, $qq->ws);
$tempscore += $res;
}
if ($usenmc)
{
$res = calcWindSpd($nmc->ws, $qq->ws);
$tempscore += $res;
}
if ($useweather && $usenmc)
{
$citystatus->score += (double)$perws*$tempscore/2;
}else{
$citystatus->score += $perws*$tempscore;
}
//wind spped done
//state
$tempscore = 0;
if ($useweather)
{
$res = calcStateScore($weather->state, $qq->state);
if ($res === false)
{
$citystatus->isValid = false;
$citystatus->error = "预报状态有误: weather.com.cn:$weather->state, qq: $qq->state";
}else{
$tempscore += $res;
}
}
if ($usenmc)
{
$res = calcStateScore($nmc->state, $qq->state);
if ($res === false)
{
$citystatus->isValid = false;
$citystatus->error .= " 预报状态有误: nmc: $nmc->state, qq: $qq->state";
}else{
$tempscore += $res;
}
}
if (!$citystatus->isValid)
{
return;
}
if ($useweather && $usenmc)
{
$citystatus->score += (double)$perstat*$tempscore/2;
}else{
$citystatus->score += $perstat*$tempscore;
}
//state done
}
//input: (int)tempa (int)tempb
//false代表数据有问题
function calcTempScore($tempa, $tempb)
{
$diff = abs($tempa - $tempb);
if ($diff == 0)
return 0;
if ($diff == 1)
return 1;
if ($diff <= 3)
return 2;
if ($diff <= 5)
return 3;
return false;
}
//input: (double)hua (double)hub
function calcHumidScore($hua, $hub)
{
$diff = abs($hua - $hub);
if ($diff <= 1)
return 0;
if ($diff <= 3)
return 1;
if ($diff <= 5)
return 2;
return 3;
}
//input: (string)wd*
function calcWindDirec($wda, $wdb)
{
global $winddirec;
if (array_key_exists($wda, $winddirec))
$wdaa = $winddirec[$wda];
else
{
print "风向不存在:$wda\n";
return false;
}
if (array_key_exists($wdb, $winddirec))
$wdbb = $winddirec[$wdb];
else{
print "风向不存在: $wdb\n";
return false;
}
$diff = $wdaa - $wdbb;
if ($diff < -4)
$diff += 8;
if ($diff > 4)
$diff = 8 - $diff;
if ($diff < 3)
return $diff;
return 3;
}
function calcWindSpd($spda, $spdb)
{
$spdaa = translateWs($spda);
$spdbb = translateWs($spdb);
$diff = abs($spdaa - $spdbb);
if ($diff < 3)
return $diff;
return 3;
}
function translateWs($spd)
{
if ($spd == '微风')
return 0;
preg_match_all('/\d+/', $spd, $spa);
$sp = (int)$spa[0][0];
if ($sp < 3)
return 0;
return $sp-2;
}
//温度以及降水量都为参考量
function calcStateScore($sta, $stb)
{
global $weathercatalog;
if (array_key_exists($sta, $weathercatalog))
{
$staa = $weathercatalog[$sta];
}else{
print "天气现象不存在: $sta\n";
return false;
}
if (array_key_exists($stb, $weathercatalog))
{
$stbb = $weathercatalog[$stb];
}else{
print "天气现象不存在: $stb\n";
return false;
}
//统一化
if ($stbb[0] < $staa[0] ||
($stbb[0] == $staa[0] && count($staa) == 2 && $stbb[1] < $staa[1])
)
{
$tmp = $staa;
$staa = $stbb;
$stbb = $tmp;
}
//两者天气情况一样
if ($staa[0] == $stbb[0])
{
if (count($staa) == 1 || $staa[1] == $stbb[1])
return 0;
}
//如果一个是晴天,多云
//另一个是雨,雪,冰雹,则上报
if (
($staa[0] == 0 || $staa[0] == 1) &&
($stbb[0] == 3 || $stbb[0] == 4 || $stbb[0] == 6)
){
return false;
}
//如果一个是阴天,另一个下冰雹了
if ($staa[0] == 2 && $stbb[0] == 6)
{
return false;
}
//一个下雪天一个雷阵雨
if ($staa[0] == 3 && $staa[1] == 1 && $stbb[0] == 4)
return false;
//雨天、雪起沙尘暴
if (($staa[0] == 3||$stbb[0] == 4) && $stbb[0] == 7)
return false;
//雾天有沙尘暴
if ($staa[0] == 5 && $stbb[0] == 7)
return false;
//冰雹天有沙尘暴
if ($staa[0] == 6 && $stbb[0] == 7)
return false;
switch($staa[0])
{
case 0://晴天
if ($stbb[0] == 1)//多云
return 1;
if ($stbb[0] == 2 || $stbb[0] == 5)//阴,雾
return 2;
if ($stbb[0] == 7)//沙尘暴
return 3;
case 1://多云
if ($stbb[0] == 2 || $stbb[0] == 5)//阴
return 2;
if ($stbb[0] == 7)
return 3;
case 2://阴
if ($stbb[0] == 3)//雨天的情况
{
if ($stbb[1] <= 2)//如果是阵雨或者小雨
return 1;
if ($stbb[1] <= 4)//中雨,大雨
return 2;
return 3;//大雨以上
}
if ($stbb[0] == 4)//雪天
{
if ($stbb[1] <= 3)//小雪或者阵雪或者雨夹雪
return 1;
if ($stbb[1] <= 4)
return 2;
return 3;
}
if ($stbb[0] == 5)//雾
return 2;
if ($stbb[0] == 7)
return 3;
case 3://雨天
if ($stbb[0] == 3)//都为雨天的情况
{
$diff = $stbb[1] - $staa[1];
if ($staa[1] < 2)//阵雨情况
return 1;
if ($diff < 3)
return $diff;
return 3;
}
if ($stbb[0] == 4)//雪天
{
if ($staa[1] == 0 && $stbb[1] < 4)//一边阵雨一边小雪一下是有可能的
return 1;
if ($staa[1] == 0)//一边阵雨一边中雪以上就不正常了
return 3;
if ($staa[1] == 2 && $stbb[1] == 2)//小雪和小雨也是可能的
return 1;
//一边中雨一边中雪就不大可能了
return 3;
}
if ($stbb[0] == 5)//雾
{
//下雨天同时又是雾天,认为有问题
return 3;
}
if ($stbb[0] == 6)//冰雹
return 3;
case 4://雪天
if ($stbb[0] == 4)//都为雪天
{
$diff = $stbb[1] - $staa[1];
if ($staa[1] == 0 && $diff <=3)//雨夹雪与小雪之类差别不大)
return 1;
if ($staa[1] == 0)//与剩下的来说就不大可能了
return 3;
if ($staa[1] == 1 && $diff <= 2)//冻雨小雪差别也不大
return 1;
if ($staa[1] == 1)//与剩下来说也不大可能
return 3;
if ($staa[1] == 2 && $diff <= 2)//阵雪与中雪相比还是有可能的
return 1;
if ($staa[1] == 2)//与剩下差别较大
return 2;
return $diff;//剩下就是小雪,中雪,大雪,暴雪之间的区别了
}
//雪天与雾天
if ($stbb[0] == 5)
{
return 2;
}
//雪天下冰雹
if ($stbb[0] == 6)
{
return 2;
}
case 5://雾
if ($stbb[0] == 6)//冰雹
return 3;
case 7://沙尘暴
$diff = $stbb[1] - $staa[1];
return $diff;
}
echo "error: $sta $stb\n";
return 3;
}
//补全状态
function getState($current, $foreTime, $forecast)
{
$ct = $current->updateTime;
$rt = str2sec(date('H:i', strtotime($ct)));
if ((int)date('z', strtotime($ct)) > (int)date('z', strtotime($foreTime)))
{
//超过一天了
$rt += 3600*24;
}
$daypass = false;
foreach($forecast as $f)
{
$t = $f->updateTime;
preg_match_all('/\d{2}:\d{2}/', $t, $tr);
$ta[0] = str2sec($tr[0][0]);
$ta[1] = str2sec($tr[0][1]);
if ($daypass)
{
$ta[0] += 3600*24;
$ta[1] += 3600*24;
}
if ($ta[1] < $ta[0])//跨过一天了
{
$daypass = true;
$ta[1] += 3600*24;
}
if ($rt <= $ta[1]) //如果比最小的都小,则用这个
{
$current->state = $f->state;
break;
}
}
}
function str2sec($str)
{
$ta = explode(':', $str);
if (count($ta) == 2)
return 60*60*(int)$ta[0]+60*(int)$ta[1];
else
return false;
}
function dbInit()
{
global $db;
global $dbaddr;
global $dbusr;
global $dbpwd;
global $dbschema;
global $charset;
$db = new mysqli($dbaddr, $dbusr, $dbpwd, $dbschema);
if ($db)
{
$db->set_charset($charset);
return true;
}
return false;
}
?>