asciiURL_to_votableURL.php
6.3 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
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
// ------------------------------------------------------------------------------
// A php script which reads an ascii data file and converts it into VOTable file
//
// Usage from command line:
// php asciiURL_to_votableURL.php asciiURL
// asciiURL url of the ascii file
//
// Usage from browser:
// http://impex-fp7.fmi.fi/ws/asciiURL_to_votableURL.php?asciiURL
// asciiURL url of the ascii file
//
// The script returns the url of the created VOTable file.
//
// This script is written for handling ascii data files (from Cluster) provided
// by Vincent Genot. It makes assumptions about the format of data in the ascii
// file so it is probably not suitable for handling other ascii files.
// ------------------------------------------------------------------------------
// -----------------------------------
// Get the URL of the ascii data file
// -----------------------------------
if (isset($_GET['url'])) { // For html calls
$url_ascii = $_GET['url'];
} else { // For command line
if (!isset($argv[1])) die("No url given" . "\n");
$url_ascii = $argv[1];
}
// --------------------------------
// Read the ascii file into memory
// --------------------------------
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: text/html;\r\nAccept-language: en\r\n",
'user_agent' => 'FMI web service'
)
);
$context = stream_context_create($opts);
$ascii_file = file_get_contents($url_ascii, false, $context);
if ($ascii_file === false) die("Could not read url " . $url_ascii . "\n");
// -------------------------------------------------
// A simple test that this is indeed our data file:
// Must start with '# ' string.
// -------------------------------------------------
if (substr($ascii_file,0,2) !== "# ") die("Ascii file not in proper format " . "\n");
// ------------------------------------------------------
// Define input parameter array for getVOTableURL method
// ------------------------------------------------------
$params = array(
'Table_name' => $url_ascii,
'Description' => "",
'Fields' => array()
);
// -------------------------------------------------------------------
// Go through the file line by line and extract field names and data
// -------------------------------------------------------------------
$ascii_table = preg_split("/[\n]+/", $ascii_file); // Split the ascii file into an array of lines
$First = true; // Flag for detecting the first non-comment line (= line with field names)
foreach($ascii_table as $line) {
// Store comment lines into VOT table description
// Comment lines start with character '#'
if (substr($line,0,1) == "#") {
$params['Description'] .= $line . "\n";
continue;
}
// If first non-comment line read field names and units. The line content is something like:
// it year mo dy hr mn sc msc X Y Z rho ux uy uz Bx(nT) By(nT) Bz(nT) p jx jy jz
if ($First) {
$Field_list = preg_split("/[\s,]+/", trim($line)); // Create an array of field names
foreach($Field_list as $field) {
$params['Fields'][] = array(); // Create a new associative array for this field
$index = count($params['Fields']) - 1;
// Get name and possible unit (included in parenthesis)
preg_match("/([^(]+)[(]*([^)]*)[)]*$/", $field, $matches);
$params['Fields'][$index]['name'] = $matches[1];
$params['Fields'][$index]['unit'] = $matches[2]; // unit string is "" if no unit defined
// Set the datatype attribute. Otherwise Topcat won't recognize the variable
if (in_array($matches[1], array('it','year','mo','dy','hr','mn','sc','msc')))
$params['Fields'][$index]['datatype'] = 'int';
else
$params['Fields'][$index]['datatype'] = 'float';
$params['Fields'][$index]['data'] = array(); // Define the array for data values
}
$First = false;
continue;
}
// Read data line
$i = 0;
$Value_list = preg_split("/[\s,]+/", trim($line)); // Make an array of data values in this line
foreach($Value_list as $value) {
$params['Fields'][$i++]['data'][] = $value; // Add the data value into field's data array
}
}
// ---------------------------------------
// Add the time column in ISO8601 format.
// ---------------------------------------
// First try to locate the data arrays for year, mo, dy, hr, mn, sc and msc columns
foreach($params['Fields'] as $field) {
if ($field['name'] == 'year') $year_data = $field['data'];
if ($field['name'] == 'mo') $mo_data = $field['data'];
if ($field['name'] == 'dy') $dy_data = $field['data'];
if ($field['name'] == 'hr') $hr_data = $field['data'];
if ($field['name'] == 'mn') $mn_data = $field['data'];
if ($field['name'] == 'sc') $sc_data = $field['data'];
if ($field['name'] == 'msc') $msc_data = $field['data'];
}
// If all date arrays are found then create the date string in ISO8601 format
if (isset($year_data) and isset($mo_data) and isset($dy_data) and isset($hr_data) and isset($mn_data) and isset($sc_data) and isset($msc_data)) {
$params['Fields'][] = array(); // Add new field to $params array
$index = count($params['Fields']) - 1; // Index of the new field
$params['Fields'][$index]['name'] = 'Time'; // Set the name to 'Time'
$params['Fields'][$index]['data'] = array(); // Create array for data values
for ($i = 0; $i < count($year_data); $i++) { // Fill the data array
$time_stamp = gmmktime($hr_data[$i], $mn_data[$i], $sc_data[$i], $mo_data[$i], $dy_data[$i], $year_data[$i]);
$time_str = gmdate("Y-m-d\TH:i:s", $time_stamp) . "." . substr("000" . $msc_data[$i],-3);
$params['Fields'][$index]['data'][] = $time_str; // Add the value to the end of the data table
}
}
// -------------------------------------------------------------------------
// More computable quantities (e.g. Btot, Utot) may be added here similarly
// -------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------
// Finally call the getVOTableURL method to create a VOTtable file and store it into FMI's server
// -----------------------------------------------------------------------------------------------
$methods_file = "http://impex-fp7.fmi.fi/ws/Methods_FMI.wsdl";
$client = new SoapClient($methods_file);
try {
$data_url = $client->getVOTableURL($params);
}
catch (Exception $e) {
echo "Error : <br />";
echo $e->getMessage();
exit();
}
echo $data_url . "\n";
?>