getData.php
2.85 KB
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
<?php
if (php_sapi_name() !='cli') die("This script can be only executed from command line");
require_once '../php/config.php';
$shortopts = "";
$longopts = array(
"id:",
"parameters:",
"start:",
"stop:",
);
$options = getopt($shortopts, $longopts);
if (!isset($options['id']) || !isset($options['parameters']) || !isset($options['start']) || !isset($options['stop'])) {
exit(1);
}
$query = array(
"startTime" => str_replace("000000Z", "Z", $options['start']),
"stopTime" => str_replace("000000Z", "Z", $options['stop']),
"outputFormat" => "ASCII",
"timeFormat" => "ISO8601",
);
$getDataset = FALSE;
$paramers = trim($options['parameters']);
$parameterId = array();
if (!empty($paramers)) {
$parametersId = explode(',', $paramers);
for ($i = 0; $i < count($parametersId); ++$i) {
$parametersId[$i] = trim($parametersId[$i]);
}
if ((count($parametersId) == 1) && ($parametersId[0] == 'Time')) {
$getDataset = TRUE;
}
else {
if (($key = array_search('Time', $parametersId)) !== FALSE) {
unset($parametersId[$key]);
}
}
}
else {
$getDataset = TRUE;
}
//Get token
$url = webAlias . "/php/rest/auth.php";
$token = file_get_contents($url);
if (empty($token)) {
exit(1);
}
$query["token"] = $token;
//Get data
$url = webAlias . "/php/rest/";
if ($getDataset) {
$query["datasetID"] = $options['id'];
$url .= "getDataset.php";
}
else {
$query["parameterID"] = implode(',', $parametersId);
$url .= "getParameter.php";
}
$url .= "?" . http_build_query($query);
$result = file_get_contents($url);
//Check result
if (empty($result)) {
exit(1);
}
$result = json_decode($result,TRUE);
if (!$result || empty($result['success']) || empty($result['dataFileURLs'])) {
exit(1);
}
$data_path = $result['dataFileURLs'];
ob_start();
$columns = array();
$data_handle = fopen($data_path, 'r');
while(1)
{
$data_line = fgets($data_handle);
if (!$data_line) {
break;
}
if (strpos($data_line, '#') === 0) {
if (strpos($data_line, '# DATA_COLUMNS : ') === 0) {
$data_line = str_replace('# DATA_COLUMNS : ', '', $data_line);
$columns = explode(',', $data_line);
}
continue;
}
if (!empty($columns)) {
$exploded_data = explode(' ', $data_line);
$data = array();
foreach ($exploded_data as $d) {
if ($d != "") {
$data[] = $d;
}
}
foreach ($columns as $col_key => $col_name) {
if ($col_key != 0)
echo ",";
$val = trim($data[$col_key]);
if ($val == "NaN") {
$val = "-1e31";
}
echo $val;
if ($col_key == 0)
echo "Z";
}
echo PHP_EOL;
}
ob_flush();
flush();
}
ob_end_flush();
?>