<?php

/**
 * This test runs RTGM calculations on all the provided Fox Plaza hazard curves
 * (see foxplaza.csv). This test requires visual inspection and comparison
 * against current Matlab results. Output is printed in (hopefully) a readily
 * comparible format.
 *
 */
chdir(dirname(__FILE__));
include_once '../../src/conf/config.inc.php';
$contents = file('foxplaza.csv');
$saline = rtrim(array_shift($contents));
$xs = explode(',', $saline);
array_shift($xs);
// Remove leading "SA" identifier
foreach ($contents as $line) {
    $ys = explode(',', rtrim($line));
    $period = array_shift($ys);
    $rtgm = new RTGM($xs, $ys);
    $rtgm->calculate();
    $result = $rtgm->getStructure();
    printf("%s\n%f\n%f\n%f\n\n", $period, $result['uhgm'], $result['riskCoefficient'], $result['rtgm']);
}
// Read parameters
$xparam = explode(',', urldecode($_GET['x']));
$yparam = explode(',', urldecode($_GET['y']));
// Parse parameters to floats (very basic input cleansing)
foreach ($xparam as $x) {
    $xs[] = floatval($x);
}
foreach ($yparam as $y) {
    $ys[] = floatval($y);
}
// Basic input validation
if (sizeof($xs) != sizeof($ys)) {
    jsonResponse(array('error' => 'Spectral Acceleration and Annual Frequency of Exceedance ' + 'values must be the same size.', 'status' => 400), $callback);
    exit;
}
// TODO :: (Future) Add Period or Beta values to this? For now we assume
//         input data is already max-direction and use default 0.6 beta value.
$rtgm = new RTGM($xs, $ys);
$rtgm->calculate();
// JSON output
jsonResponse(array('status' => 200, 'rtgm' => $rtgm->getStructure()), $callback);
// CSV-style output better for pasting into spreadsheets
// $s = $rtgm->getStructure();
// print 'sa,' . implode(',', $s['upsampledHazardCurve']->xs) . "\n";
// print 'afe,' . implode(',', $s['upsampledHazardCurve']->ys) . "\n";
// foreach ($s['iterations'] as $i) {
// 	print 'pdf,' . implode(',', $i['pdf']) . "\n";
// 	print 'cdf,' . implode(',', $i['cdf']) . "\n";
// 	print 'integrand,' . implode(',', $i['integrand']) . "\n";
// 	print 'integral,' . implode(',', $i['integral']) . "\n";
// }