info.php 3.34 KB
<?php

    function rglob($pattern, $flags = 0) {
        $files = glob($pattern, $flags); 
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
            $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
        }
        return $files;
    }

    $METADATA_DIR = "PARAM_DEF";

    $response = array();
    $response["HAPI"] = "2.0"; 
    header('Content-Type: application/json');

    if (!$_GET["id"]) { 
        $response["status"] = 404;
        $response["msg"] = "No ID !!!";

        exit(json_encode($response));
    }
   
    $id = $_GET["id"];

    if ($_GET["parameters"]) {
        $paramRequest = explode(",",$_GET["parameters"]);
    } 
    else {
        $paramRequest = null;
    }

    $pattern = "$id.xml";
    $files = rglob($pattern);

    $xmlName = $files[0];
    
    if (!file_exists($xmlName)) {;
            header("HTTP/1.1 404 NO SUCH ID");
            $response["status"] = array("code" => 1406, "message" => "NO SUCH ID"); 
        exit(json_encode($response));
    }

    $dom = new DomDocument("1.0");
    $dom->load($xmlName);  

    $dataset = $dom->getElementsByTagName("Spase")->item(0);

    $parameters = $dataset->getElementsByTagName('Parameter');
    if ( $parameters->length == 0 ) {
        header("HTTP/1.1 404 NO PARAMETERS");
        $response["status"] = array("code" => 1406, "message" => "NO  PARAMETERS");       
        exit(json_encode($response));
    }

    $paramDom = new DomDocument("1.0");  
 
    $parametersResponse = array(array("name" => "Time", "type" => "isotime", "length" => 20, "units" => "", "fill" =>""));
    foreach ( $parameters as $param ) {  
        $oneParam = array();
        $oneParam["type"] = "double";
        $paramId =  $param->getElementsByTagName('ParameterKey')->item(0)->nodeValue;
        if (file_exists("$METADATA_DIR/$paramId.xml")) {
            $paramDom->load("$METADATA_DIR/$paramId.xml");
            $process = $paramDom->getElementsByTagName("process")->item(0)->nodeValue;
            if (!$process) { 
                $baseParam = $paramDom->getElementsByTagName("baseParam")->item(0);                  
                $oneParam["name"] = $baseParam->getAttribute('name');
                $oneParam["description"] = $param->getElementsByTagName('Description')->item(0)->nodeValue;
                $oneParam["fill"] = $param->getElementsByTagName('FillValue')->item(0)->nodeValue;
                $oneParam["units"] = $param->getElementsByTagName('Units')->item(0)->nodeValue;
                $size = $param->getElementsByTagName('Size');
                if ($size->length > 0) {
                    $oneParam["size"] = array(intval($size->item(0)->nodeValue));
                }
                if ($paramRequest && !in_array($baseParam->getAttribute('name'), $paramRequest)) { 
                   //
                } else  {
                    $parametersResponse[] = $oneParam;
                }
            }
        }
    }

    $response["startDate"] = $dataset->getElementsByTagName('StartDate')->item(0)->nodeValue;
    $response["stopDate"] = $dataset->getElementsByTagName('StopDate')->item(0)->nodeValue;
    $response["cadence"] = $dataset->getElementsByTagName('Cadence')->item(0)->nodeValue;
    $response["parameters"] = $parametersResponse;
    $response["status"] = array("code" => 1200, "msg" => "OK");

    exit(json_encode($response));
?>