Blame view

update_amda/makeHAPIMetadata.php 6.37 KB
fa544567   Benjamin Renard   First implementat...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

//error_reporting(E_ERROR);

$localparams_tree = getenv('LocalData')."/LocalParams.xml";
$infofiles_path   = getenv('PARAMS_LOCALINFO_DIR')."/";
$output_file      = getenv('HAPIData')."/metadata/amda-catalog.json";

$doc = new DOMDocument();
@$doc->load($localparams_tree);


$xpath = new DOMXpath($doc);

ff8c764d   Benjamin Renard   Add datasetGroup ...
15
$dataset_nodes = $xpath->query("/dataRoot/dataCenter/mission/instrument/dataset | /dataRoot/dataCenter/mission/observatory/instrument/dataset | /dataRoot/dataCenter/mission/instrument/datasetGroup/dataset");
fa544567   Benjamin Renard   First implementat...
16
17
$datasets = array();
foreach ($dataset_nodes as $dataset_node) {
337f0c93   Benjamin Renard   Use group restric...
18
	//Dataset id
1577b1e7   Benjamin Renard   Fix for HAPI serv...
19
20
21
22
23
24
	$datasetId = $dataset_node->getAttribute("xml:id");
	if (empty($datasetId)) {
		echo "[WARNING] Cannot retrieve dataset id".PHP_EOL;
		continue;
	}

337f0c93   Benjamin Renard   Use group restric...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
	//Exclude dataset with group
	$to_exclude = FALSE;
	$crt_node = $dataset_node;
	while (($crt_node != NULL) && !$to_exclude) {
		if ($crt_node->nodeType == XML_ELEMENT_NODE) {
			$group = $crt_node->getAttribute("group");
			if (!empty($group)) {
				$to_exclude = TRUE;
			}
		}
		$crt_node = $crt_node->parentNode;
	}
	if ($to_exclude) {
		echo "[WARNING] Exclude dataset ".$datasetId." (group restriction)".PHP_EOL;
		continue;
	}

1577b1e7   Benjamin Renard   Fix for HAPI serv...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	//Dataset start date
	$startDate = $dataset_node->getAttribute("dataStart");
	if (empty($startDate)) {
		echo "[WARNING] Cannot retrieve start date for ".$datasetId.PHP_EOL;
		continue;
	}
	if ($startDate == 'MissionDependent') {
		echo "[WARNING] Dataset ".$datasetId." is mission dependant => Skip this dataset".PHP_EOL;
		continue;
	}
	if ($startDate[strlen($startDate)-1] != 'Z') {
		$startDate .= 'Z';
	}

	//Dataset stop date
	$stopDate = $dataset_node->getAttribute("dataStop");
	if (empty($stopDate)) {
		echo "[WARNING] Cannot retrieve stop date for ".$datasetId.PHP_EOL;
		continue;
	}
	if ($stopDate[strlen($stopDate)-1] != 'Z') {
		$stopDate .= 'Z';
	}

fa544567   Benjamin Renard   First implementat...
66
	$dataset = array(
1577b1e7   Benjamin Renard   Fix for HAPI serv...
67
		"id" => $datasetId,
fa544567   Benjamin Renard   First implementat...
68
69
		"title" => $dataset_node->getAttribute("name"),
		"info" => (Object)array(
1577b1e7   Benjamin Renard   Fix for HAPI serv...
70
71
			"startDate" => $startDate,
			"stopDate" => $stopDate,
fa544567   Benjamin Renard   First implementat...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		),
	);

	//cadence
	$sampling = $dataset_node->getAttribute("sampling");
	if (!empty($sampling)) {
		$dataset["info"]->{"cadence"} = "PT".$sampling;
	}

	//description
	$desc = $dataset_node->getAttribute("desc");
	if (!empty($desc)) {
		$dataset["info"]->{"description"} = $desc;
	}

	//resourceID
	$spaseId = $dataset_node->getAttribute("spaseId");
	if (!empty($spaseId)) {
		$dataset["info"]->{"resourceID"} = $spaseId;
	}

	//modificationDate
	$lastUpdate = $dataset_node->getAttribute("lastUpdate");
	if (!empty($lastUpdate)) {
		//$dataset["info"]->{"modificationDate"} = $lastUpdate."Z";
	}

	//parameters
	$dataset["info"]->{"parameters"} = array();
	$dataset["info"]->{"parameters"}[] = (Object)array(
		"name" => "Time",
		"type" => "isotime",
		"length" => 24,
		"units" => "UTC",
		"fill" => NULL,
	);
	
	$parameter_nodes = $dataset_node->getElementsByTagName("parameter");
	foreach($parameter_nodes as $parameter_node) {
		$parameter = (Object)array();

		$param_id = $parameter_node->getAttribute("xml:id");

		//name
		$parameter->{"name"} = $param_id;

		//type
		$parameter->{"type"} = "double";

17e47c9e   Benjamin Renard   Add bins definiti...
121
122
123
124
125
126
127
128
129
130
		//Load info_file if exists
		$infofile_path = $infofiles_path . "/info_".$parameter_node->getAttribute("xml:id").".xml";
		$info_doc = NULL;
		if (file_exists($infofile_path)) {
			$info_doc = new DOMDocument();
			if (!@$info_doc->load($infofile_path)) {
				$info_doc = NULL;
			}
		}

fa544567   Benjamin Renard   First implementat...
131
132
133
134
		//size
		$size = $parameter_node->getAttribute("size");
		if (empty($size)) {
			if ($parameter_node->getAttribute("display_type") == "spectrogram") {
17e47c9e   Benjamin Renard   Add bins definiti...
135
136
				//Retrieve dimensions in info file
				if (!isset($info_doc)) {
fa544567   Benjamin Renard   First implementat...
137
138
139
					echo "[WARNING] Parameter ".$param_id." defines as a spectrogram but cannot retrieve info file => Skip this parameter".PHP_EOL;
					continue;
				}
fa544567   Benjamin Renard   First implementat...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
				$dimensions_node = $info_doc->getElementsByTagName("dimensions");
				if ($dimensions_node->length == 0) {
					echo "[WARNING] Cannot retrieve size for ".$param_id.PHP_EOL;
					continue;
				}
				$dimensions_node = $dimensions_node->item(0);
				$dim1 = $dimensions_node->getAttribute("dim_1");
				$dim1 = empty($dim1) ? 1 : intval($dim1);
				$dim2 = empty($dim2) ? 1 : intval($dim2);
				if (($dim1 == 1) && ($dim2 == 1)) {
					echo "[WARNING] Parameter ".$param_id." seems to be a scalar but defined as a spectrogram".PHP_EOL;
					$size = NULL;
				}
				else if ($dim2 == 1) {
					$size = array(intval($dim1));
				}
				else {
					$size = array(intval($dim1), intval($dim2));
				}
			}
			else {
				$size = NULL;
			}
		}
		else {
			$size = array(intval($size));
		}

		if (!empty($size)) {
			$parameter->{"size"} = $size;
		}

17e47c9e   Benjamin Renard   Add bins definiti...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
		//bins
		$bins = NULL;
		if (!empty($size) && isset($info_doc)) {
			$tables_node = $info_doc->getElementsByTagName("tables");
			if ($tables_node->length > 0) {
				$bins = array();
				$tables_node = $tables_node->item(0);
				$table_nodes = $tables_node->getElementsByTagName("table");
				if ($table_nodes->length > 0) {
					foreach ($table_nodes as $table_node) {
						$relatedDim = $table_node->getAttribute("relatedDim");
						$bin_index = ($relatedDim == "dim_1") ? 0 : 1;
						$nameBin = $table_node->getAttribute("name");
						$unitsBin = $table_node->getAttribute("units");
						$channel_nodes = $table_node->getElementsByTagName("channel");
						$ranges = array();
						foreach ($channel_nodes as $channel_node) {
							$min_range = $channel_node->getAttribute("min");
							$max_range = $channel_node->getAttribute("max");
							$ranges[] = array(floatval($min_range), floatval($max_range));
						}
						if (!empty($ranges)) {
							$bin_info = (Object)array();
							$bin_info->{"name"} = $nameBin;
ff8c764d   Benjamin Renard   Add datasetGroup ...
196
							$bin_info->{"units"} = html_entity_decode($unitsBin);
17e47c9e   Benjamin Renard   Add bins definiti...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
							$bin_info->{"ranges"} = $ranges;
							if ($bin_index == 0) {
								array_unshift($bins, $bin_info);
							}
							else {
								array_push($bins, $bin_info);
							}
						}
					}
				}
			}
		}
		if (!empty($bins)) {
			$parameter->{"bins"} = $bins;
		}

fa544567   Benjamin Renard   First implementat...
213
214
215
216
		//units
		$units = $parameter_node->getAttribute("units");
		if (empty($units))
			$units = NULL;
ff8c764d   Benjamin Renard   Add datasetGroup ...
217
		$parameter->{"units"} = html_entity_decode($units);
fa544567   Benjamin Renard   First implementat...
218
219

		//fill
80b3946d   Benjamin Renard   Fix NaN for HAPI ...
220
		$parameter->{"fill"} = "-1e31";
fa544567   Benjamin Renard   First implementat...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

		//description
		$description = $parameter_node->getAttribute("description");
		if (!empty($description)) {
			$parameter->{"description"} = $description;
		}

		$dataset["info"]->{"parameters"}[] = $parameter;
	}

	$datasets[] = $dataset;
}	

file_put_contents($output_file, json_encode($datasets));

?>