asciiURL_to_votableURL.php 6.3 KB
<?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";

?>