-
Notifications
You must be signed in to change notification settings - Fork 1
/
functionForWth.php
168 lines (125 loc) · 3.54 KB
/
functionForWth.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
<?php
// Judge the content type of the line
function judgeContentType($line, $flg) {
if (strpos($line, "*") === 0) {
if (strpos(strtolower($line), "weather") === 1) {
$flg[0] = "weather";
} else {
$flg[0] = "site";
}
$flg[1] = "";
$flg[2] = "data";
} else if (strpos($line, "@") === 0) {
$flg[0] = "site";
$flg[1] = strtolower(trim(substr($line, 1)));
$flg[2] = "";
} else if (strpos($line, "!") === 0) {
$flg[2] = "comment";
} else if (trim($line) !== "") {
$flg[2] = "data";
} else if ($flg[2] !== "blank") {
$flg[1] = "";
$flg[2] = "blank";
} else {
$flg[0] = "";
$flg[1] = "";
$flg[2] = "blank";
}
return $flg;
}
// split one line content into array
function getSpliterResult($flg, $line, $ret) {
//Read Weather Info
if ($flg[0] == "weather" && $flg[2] == "data") {
$line = str_ireplace("*soils:", "", $line);
$ret["address"] = str_ireplace("*weather data : ", "", $line);
// read Site Info
} else if ($flg[0] == "site") {
if (strpos($flg[1], "insi ") === 0 && $flg[2] == "data") {
sscanf($line, " %2s%2s %8f %8f %5f %5f %5f %5f %5f", $ret["inste"], $ret["sitee"], $ret["xlat"], $ret["xlong"], $ret["elev"], $ret["tav"], $ret["tamp"], $ret["refht"], $ret["wndht"]);
} else if (strpos($flg[1], "date ") === 0 && $flg[2] == "data") {
$tmp = createWthSubArray(); // For the situation that some files don't contain all the field
sscanf($line, "%5d %5f %5f %5f %5f %5f %5f %5f", $tmp["yrdoyw"], $tmp["srad"], $tmp["tmax"], $tmp["tmin"], $tmp["rain"], $tmp["tdew"], $tmp["windsp"], $tmp["par"]);
$ret["daily"] = addArray($ret["daily"], $tmp, "");
} else {
}
} else {
}
return $ret;
}
// Add the input value into the array where the position is located by id
function addArray($arr, $value, $id) {
// if id is not pointed out, then add into the end of array
if ($id == "") {
if (isset($arr)) {
$arr[count($arr)+1] = $value;
} else {
$arr[1] = $value;
}
// if id is pointed out, then add into the located position
} else {
$arr[$id] = $value;
}
return $arr;
}
// create exp data array
function createWthArray() {
$ret["address"] = "";
$ret["inste"] = "";
$ret["sitee"] = "";
$ret["xlat"] = "";
$ret["xlong"] = "";
$ret["elev"] = "";
$ret["tav"] = "";
$ret["tamp"] = "";
$ret["refht"] = "";
$ret["wndht"] = "";
$ret["daily"] = array();
return $ret;
}
// create exp data array
function createWthSubArray() {
$ret["yrdoyw"] = "";
$ret["srad"] = "";
$ret["tmax"] = "";
$ret["tmin"] = "";
$ret["rain"] = "";
$ret["tdew"] = "";
$ret["windsp"] = "";
$ret["par"] = "";
return $ret;
}
function checkInvalidValue($value) {
if ($value == "" || $value == "-99" || $value == -99) {
return true;
} else {
return false;
}
}
function printWthArray($arr) {
$keys = array_keys($arr);
foreach ($keys as $key) {
if (gettype($arr[$key]) == "array") {
// print title
$subArr = $arr[$key][1];
$subKeys = array_keys($subArr);
echo "<table><tr>";
foreach ($subKeys as $subKey) {
echo "<td>" . $subKey . "</td>";
}
echo "</tr>";
// print daily data
for ($i = 1; $i<= count($arr[$key]); $i++) {
echo "<tr>";
foreach ($subKeys as $subKey) {
echo "<td>" . $subArr[$subKey] . "</td>";
}
echo "</tr>";
}
echo "</table>";
} else {
echo "[". $key . "]......[" . $arr[$key] ."]<br />";
}
}
}
?>