// 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";
// }
<?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']);
}
 // Define curves for global test
 $xs = array(0.0025, 0.00375, 0.00563, 0.00844, 0.0127, 0.019, 0.0285, 0.0427, 0.0641, 0.0961, 0.144, 0.216, 0.324, 0.487, 0.73, 1.09, 1.64, 2.46, 3.69, 5.54);
 $ys = array(0.4782, 0.3901, 0.3055, 0.2322, 0.1716, 0.1241, 0.08620999999999999, 0.05687, 0.03492, 0.01985, 0.01045, 0.005095, 0.002302, 0.0009371, 0.0003308, 9.488E-5, 1.952E-5, 2.174E-6, 8.553E-8, 1.315E-10);
 // What we expect back
 $riskCoeff = 0.95595068110365;
 $rtgmIters = array(0.67538571727694, 0.60742259454374, 0.64563543643857);
 $riskIters = array(0.89937139475318, 1.1544650238328, 1.0007739239289);
 // Values for testing methods.
 $afe4uhgm = -log(1 - 0.02) / 50;
 $min_sa = 0.001;
 $rtgmTmp = 24.000128833913;
 $riskTmp = 0.009838107787837501;
 $target_risk = -log(1 - 0.01) / 50;
 $uhgm = 0.6753857172769;
 $usampling_factor = 1.05;
 $rtgm = new RTGM($xs, $ys, Frequency::SA_1P00, 0.8);
 // RGTM_Util method tests
 $a = array(1.0, 3.0, 5.0, 7.0, 8.0, 9.0, 10.0);
 notify('Binary search found', 2, RTGM_Util::binary_search($a, 5.0));
 notify('Binary search not found', -4, RTGM_Util::binary_search($a, 6.0));
 notify('Build Sequence', 178, count(RTGM_Util::buildSequence(log($min_sa), log($xs[count($xs) - 1]), log($usampling_factor), true)));
 notify('Build Sequence Inf/NaN', 0, count(RTGM_Util::buildSequence(INF, NAN, 1, true)));
 $a2 = array(1, 2, 3);
 notify('Exp', array(2.718281828459, 7.3890560989307, 20.085536923188), RTGM_Util::exp($a2));
 notify('Find Log Log X', $uhgm, RTGM_Util::findLogLogX($xs, $ys, $afe4uhgm));
 notify('Find Log Log Y', $afe4uhgm, RTGM_Util::findLogLogY($xs, $ys, $uhgm));
 notify('Log Normal Cumulative Prob', 0.012893887361471, RTGM_Util::logNormalCumProb(1.3, 1.6, 0.6));
 notify('Log Normal Cumulative Prob (0)', 0.0, RTGM_Util::logNormalCumProb(0.0, 1.6, 0.6));
 notify('Log Normal Density', 0.042613954514149, RTGM_Util::logNormalDensity(1.3, 1.6, 0.6));
 notify('Log Normal Density (0)', 0.0, RTGM_Util::logNormalDensity(0.0, 1.6, 0.6));
 $a3 = array(1, 2, 3);