Commit 7e8a899eddada6f4244f0a1e9b4a607439333872

Authored by Benjamin Renard
2 parents 3994bd1f b2b3cdea

Merge branch 'epn-tap' into amdadev

php/WebServices/WebServer.php
... ... @@ -623,6 +623,33 @@ class WebServer
623 623  
624 624 $this->throwError("serverError", $res['message']);
625 625 }
  626 +
  627 +/*
  628 +* get EPN-TAP granule
  629 +*/
  630 + public function getGranule($data)
  631 + {
  632 + if (!array_key_exists('obs_id', $data) || !array_key_exists('start', $data) || !array_key_exists('stop', $data)) {
  633 + $this->throwError("serverError", "Missing obs_id, start or stop definition");
  634 + }
  635 + $data['datasetID'] = $data['obs_id'];
  636 + $data['startTime'] = $data['start'];
  637 + $data['stopTime'] = $data['stop'];
  638 + $data['outputFormat'] = "CDF_ISTP";
  639 + if (array_key_exists('format', $data)) {
  640 + $data['outputFormat'] = $data["format"];
  641 + }
  642 + $res = $this->getDataset($data);
  643 + if (!$res['success']) {
  644 + return $res;
  645 + }
  646 + if (array_key_exists('dataFileURLs', $res)) {
  647 + $data = file_get_contents($res['dataFileURLs']);
  648 + header("Location: ".$res['dataFileURLs']);
  649 + die();
  650 + }
  651 + $this->throwError("serverError", "Unknown error");
  652 + }
626 653  
627 654 /*
628 655 * get status for jobs in batch
... ...
php/config.php
... ... @@ -120,6 +120,9 @@ define('XMLPATH', IHM_SRC_DIR.'/php/XML/');
120 120 define('HAPIData', DATAPATH.'/HAPI/');
121 121 define('HAPISERVER_PATH', '/opt/tools/hapi-server/');
122 122  
  123 +// EPNTAP dirs
  124 +define('EPNTAPData', DATAPATH.'/EPN-TAP/');
  125 +
123 126 require_once(INTEGRATION_BASE_PATH.'/src/amdaintegration_autoload.php');
124 127  
125 128  
... ...
php/getEnv.php
... ... @@ -26,6 +26,8 @@ $variables = array(
26 26 'RemoteData',
27 27 'HAPIData',
28 28 'HAPISERVER_PATH',
  29 + 'EPNTAPData',
  30 + 'REST_API_URL',
29 31 );
30 32  
31 33 foreach ($variables as $variable) {
... ...
php/rest/getGranule.php 0 โ†’ 100644
... ... @@ -0,0 +1,44 @@
  1 +<?php
  2 +
  3 +/**
  4 + * @api {get} getDataset.php getDataset
  5 + * @apiDescription Provides data corresponding to a dataset chosen by the user among those available in AMDA
  6 + * @apiName getDataset
  7 + * @apiGroup webservices
  8 + *
  9 + * @apiParam {String} token The API token.
  10 + * @apiParam {String} startTime Beginning of the time interval (ISO 8601 or UNIXTIME format).
  11 + * @apiParam {String} stopTime End of the time interval (ISO 8601 or UNIXTIME format).
  12 + * @apiParam {String} datasetID Identifier of the dataset, as defined in the file returned by the *getObsDataTree* web-services.
  13 + * @apiParam {String} [sampling] Sampling of data (*in seconds*).
  14 + * @apiParam {String} [userID] Identifier of the user in AMDA (*mandatory for user owned data*)
  15 + * @apiParam {String} [password] Password of the user in AMDA (*mandatory for user owned data*)
  16 + * @apiParam {String} [outputFormat] Format of the returned file. Two options: `VOTable` and `ASCII`.
  17 + * @apiParam {String} [timeFormat] Format of time in the data files. Two options: `ISO8601` and `UNIXTIME`.
  18 + * @apiParam {Boolean} [gzip] `1` if the file must be compressed before delivery.
  19 + *
  20 + * @apiSuccess {String} success `true`
  21 + * @apiSuccess {String} dataFileURLs URL of the files matching the criteria. If the file is empty, there is no data
  22 + * matching these criteria.
  23 + * @apiSuccess {String} status status of the job ( done | in_progress )
  24 + *
  25 + * @apiSuccessExample Success-Response:
  26 + * HTTP/1.1 200 OK
  27 + * [success] => 1
  28 + * http://amda.irap.omp.eu/AMDA/data/WSRESULT/getdataset_ace-imf-all_20130923T090000_20130924T130000.txt
  29 + * [status] => done
  30 + *
  31 + * @apiErrorExample Error-Response:
  32 + * {"error":"Cannot find info file for dataset ace-imf-any"}
  33 +
  34 + */
  35 +
  36 +//ini_set("allow_url_fopen", true);
  37 + require_once '../config.php';
  38 +
  39 + $amda_ws = new WebServer();
  40 +
  41 + $result = $amda_ws->getGranule($_GET);
  42 +
  43 + echo json_encode($result);
  44 +?>
... ...
update_amda/makeEPNTAPGranules.php 0 โ†’ 100644
... ... @@ -0,0 +1,368 @@
  1 +<?php
  2 +
  3 +//error_reporting(E_ERROR);
  4 +
  5 +$localparams_tree = getenv('LocalData')."/LocalParams.xml";
  6 +$output_file = getenv('EPNTAPData')."/amda-granules.csv";
  7 +$ddservice_wsdl = getenv('DD_WSDL');
  8 +$amda_rest_api = getenv('REST_API_URL');
  9 +
  10 +$mapping = array(
  11 + 'granule_uid' => '@@dataset_id@@-@@granule_index@@',
  12 + 'granule_gid' => 'data',
  13 + 'obs_id' => '@@dataset_id@@',
  14 + 'dataproduct_type' => 'ts',
  15 + 'target_name' => '@@target_name@@',
  16 + 'target_class' => '@@target_class@@',
  17 + 'time_min' => '@@granule_start_time@@',
  18 + 'time_max' => '@@granule_stop_time@@',
  19 + 'time_sampling_step_min' => '@@min_sampling@@',
  20 + 'time_sampling_step_max' => '@@max_sampling@@',
  21 + 'time_exp_min' => '',
  22 + 'time_exp_max' => '',
  23 + 'spectral_range_min' => '',
  24 + 'spectral_range_max' => '',
  25 + 'spectral_sampling_step_min' => '',
  26 + 'spectral_sampling_step_max' => '',
  27 + 'spectral_resolution_min' => '',
  28 + 'spectral_resolution_max' => '',
  29 + 'c1min' => '',
  30 + 'c1max' => '',
  31 + 'c2min' => '',
  32 + 'c2max' => '',
  33 + 'c3min' => '',
  34 + 'c3max' => '',
  35 + 's_region' => '',
  36 + 'c1_resol_min' => '',
  37 + 'c1_resol_max' => '',
  38 + 'c2_resol_min' => '',
  39 + 'c2_resol_max' => '',
  40 + 'c3_resol_min' => '',
  41 + 'c3_resol_max' => '',
  42 + 'spatial_frame_type' => '',
  43 + 'incidence_min' => '',
  44 + 'incidence_max' => '',
  45 + 'emergence_min' => '',
  46 + 'emergence_max' => '',
  47 + 'phase_min' => '',
  48 + 'phase_max' => '',
  49 + 'instrument_host_name' => '@@mission_name@@',
  50 + 'instrument_name' => '@@instrument_name@@',
  51 + 'measurement_type' => '@@measurement_type@@',
  52 + 'processing_level' => '@@processing_level@@',
  53 + 'creation_date' => '@@creation_date@@',
  54 + 'modification_date' => '@@modification_date@@',
  55 + 'release_date' => '@@release_date@@',
  56 + 'service_title' => 'AMDADB',
  57 + 'access_url' => '@@amda_rest_api_entry_point@@getGranule.php?obs_id=@@dataset_id@@&start=@@granule_start_time_timestamp@@&stop=@@granule_stop_time_timestamp@@&format=CDF',
  58 + 'access_format' => 'application/x-cdf-istp',
  59 + 'target_region' => '@@target_region@@',
  60 + 'publisher' => 'CDPP',
  61 + 'time_scale' => 'UTC',
  62 + 'spase_resource_id' => '@@spase_resource_id@@',
  63 + 'spase_region' => '@@spase_region@@',
  64 + 'spase_measurement_type' => '@@spase_measurement_type@@',
  65 +);
  66 +
  67 +function parseSampling($sampling) {
  68 + if (empty($sampling))
  69 + return "";
  70 + $unit = substr($sampling, -1);
  71 + $value = floatval(substr($sampling, 0, -1));
  72 + switch ($unit) {
  73 + case "S":
  74 + return strval($value);
  75 + case "M":
  76 + return strval(60.*$value);
  77 + case "H":
  78 + return strval(3600.*$value);
  79 + case "D":
  80 + return strval(86400.*$value);
  81 + default:
  82 + echo "[WARNING] Sampling - Unknown unit: ".$sampling.PHP_EOL;
  83 + return $sampling;
  84 + }
  85 +}
  86 +
  87 +function parseProcessingLevel($info) {
  88 + if (strpos($info, 'L1') !== FALSE) {
  89 + return '2';
  90 + }
  91 + if (strpos($info, 'L3') !== FALSE) {
  92 + return '5';
  93 + }
  94 + return '3';
  95 +}
  96 +
  97 +function spaseToEpnTapMeasurementType($measurement_type) {
  98 + return $measurement_type;
  99 +}
  100 +
  101 +function timestampToJulianDay($timestamp) {
  102 + return intval($timestamp) / 86400 + 2440587.5;
  103 +}
  104 +
  105 +function getTargetClass($spaseRegion) {
  106 + $parts = explode(".", $spaseRegion);
  107 + if (count($parts) < 1) {
  108 + echo "[WARNING] Error in ObservatoryRegion definition : ".$spaseRegion.PHP_EOL;
  109 + return "planet";
  110 + }
  111 + switch ($parts[0]) {
  112 + case "Asteroid":
  113 + return "asteroid";
  114 + case "Comet":
  115 + return "comet";
  116 + case "Earth":
  117 + case "Jupiter":
  118 + case "Mars":
  119 + case "Mercury":
  120 + case "Neptune":
  121 + case "Pluto":
  122 + case "Saturn":
  123 + case "Uranus":
  124 + case "Venus":
  125 + if ((count($parts) > 1) && !in_array($parts[1], array("Magnetosheath", "Magnetosphere", "NearSurface", "Surface"))) {
  126 + return "satellite";
  127 + }
  128 + return "planet";
  129 + case "Heliosphere":
  130 + case "Interstellar":
  131 + return "interplanetary_medium";
  132 + case "Sun":
  133 + return "star";
  134 + }
  135 + echo "[WARNING] Error in ObservatoryRegion definition : ".$spaseRegion.PHP_EOL;
  136 + return "planet";
  137 +}
  138 +
  139 +function getTargetName($targetClass, $spaseRegion, $missionName, $dataset_id) {
  140 + $parts = explode(".", $spaseRegion);
  141 + if (count($parts) < 1) {
  142 + return "";
  143 + }
  144 + switch ($targetClass) {
  145 + case "planet":
  146 + if ((count($parts) > 1) && !in_array($parts[1], array("Magnetosheath", "Magnetosphere", "NearSurface", "Surface"))) {
  147 + return $parts[1];
  148 + }
  149 + return $parts[0];
  150 + case "star":
  151 + case "interplanetary_medium":
  152 + return "Sun";
  153 + case "comet":
  154 + switch ($missionName) {
  155 + case "Rosetta":
  156 + return "67P";
  157 + case "Giotto":
  158 + return "1P";
  159 + case "Astronomical Objects Ephemerides":
  160 + if ($dataset_id == "p67-orb-all")
  161 + return "67P";
  162 + break;
  163 + case "ICE":
  164 + if ($dataset_id == "ice-mag-p21")
  165 + return "21P";
  166 + }
  167 + echo "[WARNING] Comet Id not defined for mission : ".$missionName.PHP_EOL;
  168 + return "";
  169 + }
  170 + echo "[WARNING] Cannot retrieve target name from target class : ".$targetClass.PHP_EOL;
  171 + return "";
  172 +}
  173 +
  174 +function getTargetRegion($targetClass, $spaseRegion) {
  175 + $parts = explode(".", $spaseRegion);
  176 + if (count($parts) < 1) {
  177 + return "";
  178 + }
  179 + switch ($targetClass) {
  180 + case "planet":
  181 + if (count($parts) > 1) {
  182 + switch ($parts[1]) {
  183 + case "Magnetosheath":
  184 + case "Magnetosphere":
  185 + return "planetary-magnetosphere";
  186 + case "NearSurface":
  187 + if (count($parts) > 2) {
  188 + switch ($parts[2]) {
  189 + case "Atmosphere":
  190 + return "planetary-atmospheres";
  191 + case "Ionosphere":
  192 + return "planetary-ionospheres";
  193 + }
  194 + }
  195 + return "planetary-atmospheres";
  196 + case "Surface":
  197 + return "planetary-surfaces";
  198 + }
  199 + }
  200 + break;
  201 + case "star":
  202 + case "interplanetary_medium":
  203 + return "solar-wind";
  204 + }
  205 + return "";
  206 +}
  207 +
  208 +ini_set("soap.wsdl_cache_enabled", 0);
  209 +try {
  210 + $client = new SoapClient($ddservice_wsdl, array('trace' => 1));
  211 +}
  212 +catch (SoapFault $exception) {
  213 + echo "[ERROR] Cannot retrieve DDService - ".$ddservice_wsdl.PHP_EOL;
  214 + return;
  215 +}
  216 +
  217 +$amda_place_holders = array(
  218 + 'amda_rest_api_entry_point' => $amda_rest_api,
  219 +);
  220 +
  221 +$doc = new DOMDocument();
  222 +@$doc->load($localparams_tree);
  223 +
  224 +$xpath = new DOMXpath($doc);
  225 +
  226 +$output_file_handle = fopen($output_file, 'w');
  227 +fputcsv($output_file_handle, array_keys($mapping));
  228 +
  229 +$dataset_nodes = $xpath->query("/dataRoot/dataCenter/mission/instrument/dataset | /dataRoot/dataCenter/mission/observatory/instrument/dataset | /dataRoot/dataCenter/mission/instrument/datasetGroup/dataset");
  230 +$datasets = array();
  231 +foreach ($dataset_nodes as $dataset_node) {
  232 + $dataset_place_holders = array();
  233 + //Dataset id
  234 + $datasetId = $dataset_node->getAttribute("xml:id");
  235 + if (empty($datasetId)) {
  236 + echo "[WARNING] Cannot retrieve dataset id".PHP_EOL;
  237 + continue;
  238 + }
  239 + $dataset_place_holders['dataset_id'] = $datasetId;
  240 +
  241 + //Exclude dataset with group
  242 + $to_exclude = FALSE;
  243 + $crt_node = $dataset_node;
  244 + while (($crt_node != NULL) && !$to_exclude) {
  245 + if ($crt_node->nodeType == XML_ELEMENT_NODE) {
  246 + $group = $crt_node->getAttribute("group");
  247 + if (!empty($group)) {
  248 + $to_exclude = TRUE;
  249 + }
  250 + }
  251 + $crt_node = $crt_node->parentNode;
  252 + }
  253 + if ($to_exclude) {
  254 + echo "[WARNING] Exclude dataset ".$datasetId." (group restriction)".PHP_EOL;
  255 + continue;
  256 + }
  257 +
  258 + $dataset_place_holders['min_sampling'] = parseSampling($dataset_node->getAttribute("sampling"));
  259 + $dataset_place_holders['max_sampling'] = parseSampling($dataset_node->getAttribute("maxSampling"));
  260 + $dataset_place_holders['spase_resource_id'] = $dataset_node->getAttribute("spaseId");
  261 + $dataset_place_holders['spase_measurement_type'] = $dataset_node->getAttribute("measurement_type");
  262 + $dataset_place_holders['measurement_type'] = spaseToEpnTapMeasurementType($dataset_place_holders['spase_measurement_type']);
  263 + $dataset_place_holders['processing_level'] = parseProcessingLevel($dataset_node->getAttribute("xml:id"));
  264 + $target = $dataset_node->getAttribute("target");
  265 +
  266 +
  267 + $instrument_node = NULL;
  268 + $mission_node = NULL;
  269 + $observatory_node = NULL;
  270 + $dataset_group_node = NULL;
  271 +
  272 + $crt_node = $dataset_node;
  273 + while ($crt_node = $crt_node->parentNode) {
  274 + switch ($crt_node->nodeName) {
  275 + case 'instrument':
  276 + $instrument_node = $crt_node;
  277 + break;
  278 + case 'mission':
  279 + $mission_node = $crt_node;
  280 + break;
  281 + case 'observatory':
  282 + $observatory_node = $crt_node;
  283 + break;
  284 + case 'datasetGroup':
  285 + $dataset_group_node = $crt_node;
  286 + break;
  287 + }
  288 + }
  289 +
  290 + if (empty($instrument_node) || empty($mission_node)) {
  291 + echo "[ERROR] Cannot retrieve Mission or Instrument info for dataset ".$datasetId.PHP_EOL;
  292 + continue;
  293 + }
  294 +
  295 + $mission_name = $mission_node->getAttribute('name');
  296 + $instrument_name = $instrument_node->getAttribute('name');
  297 + if (empty($target)) {
  298 + $target = $instrument_node->getAttribute('target');
  299 + if (empty($target)) {
  300 + if (!empty($observatory_node)) {
  301 + $target = $observatory_node->getAttribute('target');
  302 + }
  303 + if (empty($target)) {
  304 + $target = $mission_node->getAttribute('target');
  305 + }
  306 + }
  307 + }
  308 + if (empty($target)) {
  309 + echo "[WARNING] No target defined for ".$datasetId.PHP_EOL;
  310 + }
  311 + $dataset_place_holders['spase_region'] = $target;
  312 + $dataset_place_holders['target_class'] = getTargetClass($target);
  313 + $dataset_place_holders['target_name'] = getTargetName($dataset_place_holders['target_class'], $target, $mission_name, $datasetId);
  314 + $dataset_place_holders['target_region'] = getTargetRegion($dataset_place_holders['target_class'], $target);
  315 + if (!empty($observatory_node)) {
  316 + $mission_name = $observatory_node->getAttribute('name');
  317 + }
  318 + $dataset_place_holders['mission_name'] = $mission_name;
  319 + $dataset_place_holders['instrument_name'] = $instrument_name;
  320 + if (!empty($dataset_group_node)) {
  321 + if ($dataset_place_holders['processing_level'] == 'L2') {
  322 + $dataset_place_holders['processing_level'] = parseProcessingLevel($dataset_group_node->getAttribute("xml:id"));
  323 + }
  324 + }
  325 +
  326 + try {
  327 + $vi = str_replace("-","_",$datasetId);
  328 + $res = $client->getGranules($vi);
  329 + }
  330 + catch (SoapFault $exception) {
  331 + echo "[ERROR] Cannot retrieve granules for ".$datasetId." - ".$exception->faultstring.PHP_EOL;
  332 + continue;
  333 + }
  334 +
  335 + if (empty($res)) {
  336 + echo "[ERROR] No granules for ".$datasetId.PHP_EOL;
  337 + continue;
  338 + }
  339 +
  340 + $granules = explode(PHP_EOL, $res);
  341 + foreach ($granules as $granule) {
  342 + $granule_info = explode(" ", $granule);
  343 + if (empty($granule_info) || (count($granule_info) != 4)) {
  344 + echo "[ERROR] Bad granule info in ".$datasetId.PHP_EOL;
  345 + continue;
  346 + }
  347 + $granule_place_holders['granule_index'] = str_replace(".nc", "", $granule_info[2]);
  348 + $granule_place_holders['granule_start_time'] = timestampToJulianDay($granule_info[0]);
  349 + $granule_place_holders['granule_stop_time'] = timestampToJulianDay($granule_info[1]);
  350 + $granule_place_holders['modification_date'] = timestampToJulianDay($granule_info[3]);
  351 + $granule_place_holders['granule_start_time_timestamp'] = $granule_info[0];
  352 + $granule_place_holders['granule_stop_time_timestamp'] = $granule_info[1];
  353 + $granule_place_holders['creation_date'] = $granule_place_holders['modification_date'];
  354 + $granule_place_holders['release_date'] = $granule_place_holders['modification_date'];
  355 +
  356 + $fields = array_values($mapping);
  357 + $all_place_holders = $amda_place_holders + $dataset_place_holders + $granule_place_holders;
  358 + foreach ($all_place_holders as $place_holder => $value) {
  359 + foreach ($fields as &$field) {
  360 + $field = str_replace('@@'.$place_holder.'@@', $value, $field);
  361 + }
  362 + }
  363 + fputcsv($output_file_handle, $fields);
  364 + }
  365 +}
  366 +
  367 +fclose($output_file_handle);
  368 +?>
... ...