Blame view

update_amda/makeEPNTAPGranules.php 14.6 KB
b2b3cdea   Benjamin Renard   Add script to bui...
1
2
3
4
<?php

//error_reporting(E_ERROR);

40067249   Benjamin Renard   Update EPN/TAP gr...
5
6
date_default_timezone_set("UTC");

b2b3cdea   Benjamin Renard   Add script to bui...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$localparams_tree = getenv('LocalData')."/LocalParams.xml";
$output_file      = getenv('EPNTAPData')."/amda-granules.csv";
$ddservice_wsdl   = getenv('DD_WSDL');
$amda_rest_api    = getenv('REST_API_URL');

$mapping = array(
	'granule_uid' => '@@dataset_id@@-@@granule_index@@',
        'granule_gid' => 'data',
        'obs_id' => '@@dataset_id@@',
        'dataproduct_type' => 'ts',
        'target_name' => '@@target_name@@',
        'target_class' => '@@target_class@@',
        'time_min' => '@@granule_start_time@@',
        'time_max' => '@@granule_stop_time@@',
        'time_sampling_step_min' => '@@min_sampling@@',
        'time_sampling_step_max' => '@@max_sampling@@',
        'time_exp_min' => '',
        'time_exp_max' => '',
        'spectral_range_min' => '',
        'spectral_range_max' => '',
        'spectral_sampling_step_min' => '',
        'spectral_sampling_step_max' => '',
        'spectral_resolution_min' => '',
        'spectral_resolution_max' => '',
        'c1min' => '',
        'c1max' => '',
        'c2min' => '',
        'c2max' => '',
        'c3min' => '',
        'c3max' => '',
        's_region' => '',
        'c1_resol_min' => '',
        'c1_resol_max' => '',
        'c2_resol_min' => '',
        'c2_resol_max' => '',
        'c3_resol_min' => '',
        'c3_resol_max' => '',
a63e6108   Myriam Bouchemit   Fix scripts for H...
44
        'spatial_frame_type' => 'none',
b2b3cdea   Benjamin Renard   Add script to bui...
45
46
47
48
49
50
51
52
53
54
55
56
57
        'incidence_min' => '',
        'incidence_max' => '',
        'emergence_min' => '',
        'emergence_max' => '',
        'phase_min' => '',
        'phase_max' => '',
        'instrument_host_name' => '@@mission_name@@',
        'instrument_name' => '@@instrument_name@@',
        'measurement_type' => '@@measurement_type@@',
        'processing_level' => '@@processing_level@@',
        'creation_date' => '@@creation_date@@',
        'modification_date' => '@@modification_date@@',
        'release_date' => '@@release_date@@',
a63e6108   Myriam Bouchemit   Fix scripts for H...
58
        'service_title' => 'amdadb',
40067249   Benjamin Renard   Update EPN/TAP gr...
59
        'access_url' => '@@amda_rest_api_entry_point@@getGranule.php?obs_id=@@dataset_id@@&start=@@granule_start_time_timestamp@@&stop=@@granule_stop_time_timestamp@@',
b2b3cdea   Benjamin Renard   Add script to bui...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        'access_format' => 'application/x-cdf-istp',
        'target_region' => '@@target_region@@',
        'publisher' => 'CDPP',
        'time_scale' => 'UTC',
        'spase_resource_id' => '@@spase_resource_id@@',
        'spase_region' => '@@spase_region@@',
        'spase_measurement_type' => '@@spase_measurement_type@@',
);

function parseSampling($sampling) {
	if (empty($sampling))
		return "";
	$unit = substr($sampling, -1);
	$value = floatval(substr($sampling, 0, -1));
	switch ($unit) {
		case "S":
			return strval($value);
		case "M":
			return strval(60.*$value);
		case "H":
			return strval(3600.*$value);
		case "D":
			return strval(86400.*$value);
		default:
			echo "[WARNING] Sampling - Unknown unit: ".$sampling.PHP_EOL;
			return $sampling;
	}
}

a63e6108   Myriam Bouchemit   Fix scripts for H...
89
90
91
92
93
94
95
96
function parseProcessingLevel($processing_level) {
	switch ($processing_level) {
		case 'Uncalibrated':
			return '2';
		case 'ValueAdded':
			return '5';
		default:
			return '3';
b2b3cdea   Benjamin Renard   Add script to bui...
97
	}
b2b3cdea   Benjamin Renard   Add script to bui...
98
99
}

40067249   Benjamin Renard   Update EPN/TAP gr...
100
101
102
103
104
105
106
107
function getEpnTapMeasurementType($measurement_type, $UCDs) {
	$generic_ucd = '';
	switch ($measurement_type) {
		case 'ThermalPlasma':
			$generic_ucd = 'phys.flux';
			break;
		case 'IonComposition':
		case 'NeutralGas':
a63e6108   Myriam Bouchemit   Fix scripts for H...
108
			$generic_ucd = 'phys';
40067249   Benjamin Renard   Update EPN/TAP gr...
109
110
			break;
		case 'EnergeticParticles':
a63e6108   Myriam Bouchemit   Fix scripts for H...
111
			$generic_ucd = 'phys';
40067249   Benjamin Renard   Update EPN/TAP gr...
112
113
114
			break;
		case 'Waves':
		case 'Waves.Passive':
a63e6108   Myriam Bouchemit   Fix scripts for H...
115
			$generic_ucd = 'phys';
40067249   Benjamin Renard   Update EPN/TAP gr...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
			break;
		case 'Ephemeris':
			$generic_ucd = 'pos.ephem';
			break;
		case 'ElectricField':
			$generic_ucd = 'phys.electField';
			break;
		case 'MagneticField':
			$generic_ucd = 'phys.magField';
			break;
		case 'Spectrum':
			$generic_ucd = 'spect';
			break;
		case 'InstrumentStatus':
			$generic_ucd = 'instr';
			break;
		case 'Irradiance':
			$generic_ucd = 'phot.flux';
			break;
		case 'Radiance':
			$generic_ucd = 'phot.radiance';
			break;
		default:
			echo "[WARNING] UCD - Unknown measurement type: ".$measurement_type.PHP_EOL;
			break;
	}
a63e6108   Myriam Bouchemit   Fix scripts for H...
142
	if (!empty($generic_ucd) && !in_array($generic_ucd, $UCDs)) {
40067249   Benjamin Renard   Update EPN/TAP gr...
143
		array_unshift($UCDs, $generic_ucd);
a63e6108   Myriam Bouchemit   Fix scripts for H...
144
145
	}
	return implode('#', $UCDs);
b2b3cdea   Benjamin Renard   Add script to bui...
146
147
148
149
150
151
}

function timestampToJulianDay($timestamp) {
	return intval($timestamp) / 86400 + 2440587.5;
}

40067249   Benjamin Renard   Update EPN/TAP gr...
152
153
154
155
function timestampToISO($timestamp) {
	return date("Y-m-d\TH:i:s\Z", $timestamp);
}

b2b3cdea   Benjamin Renard   Add script to bui...
156
157
158
159
160
161
162
163
164
165
function getTargetClass($spaseRegion) {
	$parts = explode(".", $spaseRegion);
	if (count($parts) < 1) {
		echo "[WARNING] Error in ObservatoryRegion definition : ".$spaseRegion.PHP_EOL;
		return "planet";
	}
	switch ($parts[0]) {
		case "Asteroid":
			return "asteroid";
		case "Comet":
a63e6108   Myriam Bouchemit   Fix scripts for H...
166
		case "Rosetta@C-G":
b2b3cdea   Benjamin Renard   Add script to bui...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
			return "comet";
		case "Earth":
		case "Jupiter":
		case "Mars":
		case "Mercury":
		case "Neptune":
		case "Pluto":
		case "Saturn":
		case "Uranus":
		case "Venus":
			if ((count($parts) > 1) && !in_array($parts[1], array("Magnetosheath", "Magnetosphere", "NearSurface", "Surface"))) {
				return "satellite";
			}
			return "planet";
		case "Heliosphere":
		case "Interstellar":
			return "interplanetary_medium";
		case "Sun":
			return "star";
a63e6108   Myriam Bouchemit   Fix scripts for H...
186
187
188
189
190
191
		case "Bepi":
		case "Juno":
		case "SolO":
		case "PSP":
		case "Cassini":
			return "spacecraft";
b2b3cdea   Benjamin Renard   Add script to bui...
192
193
194
195
196
197
198
199
200
201
202
203
	}
	echo "[WARNING] Error in ObservatoryRegion definition : ".$spaseRegion.PHP_EOL;
	return "planet";
}

function getTargetName($targetClass, $spaseRegion, $missionName, $dataset_id) {
	$parts = explode(".", $spaseRegion);
	if (count($parts) < 1) {
		return "";
	}
	switch ($targetClass) {
		case "planet":
40067249   Benjamin Renard   Update EPN/TAP gr...
204
		case "satellite":
b2b3cdea   Benjamin Renard   Add script to bui...
205
206
207
208
209
210
211
212
213
214
			if ((count($parts) > 1) && !in_array($parts[1], array("Magnetosheath", "Magnetosphere", "NearSurface", "Surface"))) {
				return $parts[1];
			}
			return $parts[0];
		case "star":
		case "interplanetary_medium":
			return "Sun";
		case "comet":
			switch ($missionName) {
				case "Rosetta":
a63e6108   Myriam Bouchemit   Fix scripts for H...
215
				case "Rosetta@C-G":
b2b3cdea   Benjamin Renard   Add script to bui...
216
217
218
219
220
221
222
223
224
225
					return "67P";
				case "Giotto":
					return "1P";
				case "Astronomical Objects Ephemerides":
					if ($dataset_id == "p67-orb-all")
						return "67P";
					break;
				case "ICE":
					if ($dataset_id == "ice-mag-p21")
						return "21P";
a63e6108   Myriam Bouchemit   Fix scripts for H...
226
227
228
				case "Solar Wind Propagation Models":
					if (in_array($dataset_id, array("bls-ros-omni", "tao-ros-sw")))
						return "67P";
b2b3cdea   Benjamin Renard   Add script to bui...
229
			}
a63e6108   Myriam Bouchemit   Fix scripts for H...
230
			echo "[WARNING] Comet Id not defined for mission : ".$missionName." - ".$dataset_id.PHP_EOL;
b2b3cdea   Benjamin Renard   Add script to bui...
231
			return "";
a63e6108   Myriam Bouchemit   Fix scripts for H...
232
233
234
235
236
237
238
239
240
		case "spacecraft":
			switch ($spaseRegion) {
				case "SolO":
					return "SolarOrbiter";
				case "Bepi":
					return "BepiColombo";
				default:
					return $spaseRegion;
			}
b2b3cdea   Benjamin Renard   Add script to bui...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	}
	echo "[WARNING] Cannot retrieve target name from target class : ".$targetClass.PHP_EOL;
	return "";
}

function getTargetRegion($targetClass, $spaseRegion) {
	$parts = explode(".", $spaseRegion);
	if (count($parts) < 1) {
		return "";
	}
	switch ($targetClass) {
		case "planet":
			if (count($parts) > 1) {
				switch ($parts[1]) {
					case "Magnetosheath":
					case "Magnetosphere":
						return "planetary-magnetosphere";
					case "NearSurface":
						if (count($parts) > 2) {
							switch ($parts[2]) {
								case "Atmosphere":
									return "planetary-atmospheres";
								case "Ionosphere":
									return "planetary-ionospheres";
							}
						}
						return "planetary-atmospheres";
					case "Surface":
						return "planetary-surfaces";
				}
			}
			break;
		case "star":
		case "interplanetary_medium":
			return "solar-wind";
	}
	return "";
}

ini_set("soap.wsdl_cache_enabled", 0);
try {
	$client = new SoapClient($ddservice_wsdl, array('trace' => 1));
}
catch  (SoapFault $exception) {
	echo "[ERROR] Cannot retrieve DDService - ".$ddservice_wsdl.PHP_EOL;
	return;
}

$amda_place_holders = array(
	'amda_rest_api_entry_point' => $amda_rest_api,
);

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

$xpath = new DOMXpath($doc);

$output_file_handle = fopen($output_file, 'w');
fputcsv($output_file_handle, array_keys($mapping));

a63e6108   Myriam Bouchemit   Fix scripts for H...
301
$dataset_nodes = $xpath->query("/dataRoot/dataCenter/mission/instrument/dataset | /dataRoot/dataCenter/mission/observatory/instrument/dataset | /dataRoot/dataCenter/mission/instrument/datasetGroup/dataset | /dataRoot/dataCenter/mission/observatory/instrument/datasetGroup/dataset");
b2b3cdea   Benjamin Renard   Add script to bui...
302
303
304
305
306
$datasets = array();
foreach ($dataset_nodes as $dataset_node) {
	$dataset_place_holders = array();
	//Dataset id
	$datasetId = $dataset_node->getAttribute("xml:id");
a63e6108   Myriam Bouchemit   Fix scripts for H...
307
	//if ($datasetId != 'so-pas-mom')
40067249   Benjamin Renard   Update EPN/TAP gr...
308
	//	continue;
b2b3cdea   Benjamin Renard   Add script to bui...
309
310
311
312
313
314
315
316
317
	if (empty($datasetId)) {
		echo "[WARNING] Cannot retrieve dataset id".PHP_EOL;
		continue;
	}
	$dataset_place_holders['dataset_id'] = $datasetId;

	//Exclude dataset with group
	$to_exclude = FALSE;
	$crt_node = $dataset_node;
a63e6108   Myriam Bouchemit   Fix scripts for H...
318
	$time_restriction = 0;
b2b3cdea   Benjamin Renard   Add script to bui...
319
320
321
322
	while (($crt_node != NULL) && !$to_exclude) {
		if ($crt_node->nodeType == XML_ELEMENT_NODE) {
			$group = $crt_node->getAttribute("group");
			if (!empty($group)) {
a63e6108   Myriam Bouchemit   Fix scripts for H...
323
324
325
326
327
328
				$restriction = $crt_node->getAttribute("restriction");
				if (empty($restriction))
					$to_exclude = TRUE;
				$time_restriction = strtotime($restriction);
				if ($time_restriction === FALSE)
					$to_exclude = TRUE;
b2b3cdea   Benjamin Renard   Add script to bui...
329
330
331
332
333
334
335
336
337
			}
		}
		$crt_node = $crt_node->parentNode;
	}
	if ($to_exclude) {
		echo "[WARNING] Exclude dataset ".$datasetId." (group restriction)".PHP_EOL;
		continue;
	}

40067249   Benjamin Renard   Update EPN/TAP gr...
338
339
340
341
342
343
	$UCDs = array();
	$parameter_nodes = $dataset_node->getElementsByTagName("parameter");
	foreach ($parameter_nodes as $parameter_node) {
		$ucd = trim($parameter_node->getAttribute('ucd'));
		if (empty($ucd))
			continue;
a63e6108   Myriam Bouchemit   Fix scripts for H...
344
345
346
347
		if (!in_array($ucd, $UCDs)) {
			$UCDs[] = $ucd;
		}
		/*$ucd_list = explode(';', $ucd);
40067249   Benjamin Renard   Update EPN/TAP gr...
348
349
350
351
352
353
		foreach ($ucd_list as $u) {
			$u = trim($u);
			if (empty($u))
				continue;
			if (!in_array($u, $UCDs))
				$UCDs[] = $u;
a63e6108   Myriam Bouchemit   Fix scripts for H...
354
		}*/
40067249   Benjamin Renard   Update EPN/TAP gr...
355
356
	}

b2b3cdea   Benjamin Renard   Add script to bui...
357
358
	$dataset_place_holders['min_sampling'] = parseSampling($dataset_node->getAttribute("sampling"));
	$dataset_place_holders['max_sampling'] = parseSampling($dataset_node->getAttribute("maxSampling"));
a63e6108   Myriam Bouchemit   Fix scripts for H...
359
360
361
	if (empty($dataset_place_holders['max_sampling'])) {
		$dataset_place_holders['max_sampling'] = $dataset_place_holders['min_sampling'];
	}
b2b3cdea   Benjamin Renard   Add script to bui...
362
363
	$dataset_place_holders['spase_resource_id'] = $dataset_node->getAttribute("spaseId");
	$dataset_place_holders['spase_measurement_type'] = $dataset_node->getAttribute("measurement_type");
40067249   Benjamin Renard   Update EPN/TAP gr...
364
	$dataset_place_holders['measurement_type'] = getEpnTapMeasurementType($dataset_place_holders['spase_measurement_type'], $UCDs);
a63e6108   Myriam Bouchemit   Fix scripts for H...
365
	$dataset_place_holders['processing_level'] = parseProcessingLevel($dataset_node->getAttribute("processing_level"));
b2b3cdea   Benjamin Renard   Add script to bui...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
	$target = $dataset_node->getAttribute("target");
	

	$instrument_node = NULL;
	$mission_node = NULL;
	$observatory_node = NULL;
	$dataset_group_node = NULL;

	$crt_node = $dataset_node;
	while ($crt_node = $crt_node->parentNode) {
		switch ($crt_node->nodeName) {
			case 'instrument':
				$instrument_node = $crt_node;
				break;
			case 'mission':
				$mission_node = $crt_node;
				break;
			case 'observatory':
				$observatory_node = $crt_node;
				break;
			case 'datasetGroup':
				$dataset_group_node = $crt_node;
				break;
		}
	}

	if (empty($instrument_node) || empty($mission_node)) {
		echo "[ERROR] Cannot retrieve Mission or Instrument info for dataset ".$datasetId.PHP_EOL;
		continue;
	}

	$mission_name = $mission_node->getAttribute('name');
	$instrument_name = $instrument_node->getAttribute('name');
	if (empty($target)) {
		$target = $instrument_node->getAttribute('target');
		if (empty($target)) {
			if (!empty($observatory_node)) {
				$target = $observatory_node->getAttribute('target');
			}
			if (empty($target)) {
				$target = $mission_node->getAttribute('target');
			}
		}
	}
	if (empty($target)) {
		echo "[WARNING] No target defined for ".$datasetId.PHP_EOL;
	}
	$dataset_place_holders['spase_region'] = $target;
	$dataset_place_holders['target_class'] = getTargetClass($target);
	$dataset_place_holders['target_name'] = getTargetName($dataset_place_holders['target_class'], $target, $mission_name, $datasetId);
	$dataset_place_holders['target_region'] = getTargetRegion($dataset_place_holders['target_class'], $target);
	if (!empty($observatory_node)) {
		$mission_name = $observatory_node->getAttribute('name');
	}
40067249   Benjamin Renard   Update EPN/TAP gr...
420
	$dataset_place_holders['mission_name'] = utf8_encode($mission_name);
a63e6108   Myriam Bouchemit   Fix scripts for H...
421
422
423
	if ($dataset_node->getAttribute('simulation') == '1') {
		$dataset_place_holders['mission_name'] = 'Simulation';
	}
40067249   Benjamin Renard   Update EPN/TAP gr...
424
425
	$dataset_place_holders['instrument_name'] = preg_replace('/[^:\/\(\) \w-]/', ' ', $instrument_name);
	if ($dataset_place_holders['instrument_name'] != $instrument_name) {
a63e6108   Myriam Bouchemit   Fix scripts for H...
426
427
428
429
430
		if ($dataset_place_holders['instrument_name'] == "IS   IS EPI-Lo") {
			$dataset_place_holders['instrument_name'] = "ISOIS EPI-Lo";
		}
		else if ($dataset_place_holders['instrument_name'] == "IS   IS EPI-Hi") {
			$dataset_place_holders['instrument_name'] = "ISOIS EPI-Hi";
b2b3cdea   Benjamin Renard   Add script to bui...
431
432
433
434
		}
	}

	try {
40067249   Benjamin Renard   Update EPN/TAP gr...
435
436
437
438
439
440
441
		$prior_id = trim($dataset_node->getAttribute('PriorID'));
		if (!empty($prior_id)) {
			$vi = str_replace("-","_",$prior_id);
		}
		else {
			$vi = str_replace("-","_",$datasetId);
		}
b2b3cdea   Benjamin Renard   Add script to bui...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
		$res = $client->getGranules($vi);
	}
	catch  (SoapFault $exception) {
		echo "[ERROR] Cannot retrieve granules for ".$datasetId." - ".$exception->faultstring.PHP_EOL;
		continue;
	}

	if (empty($res)) {
		echo "[ERROR] No granules for  ".$datasetId.PHP_EOL;
		continue;
	}
	
	$granules = explode(PHP_EOL, $res);
	foreach ($granules as $granule) {
		$granule_info = explode(" ", $granule);
		if (empty($granule_info) || (count($granule_info) != 4)) {
			echo "[ERROR] Bad granule info in   ".$datasetId.PHP_EOL;
			continue;
		}
		$granule_place_holders['granule_index'] = str_replace(".nc", "", $granule_info[2]);
		$granule_place_holders['granule_start_time'] = timestampToJulianDay($granule_info[0]);
		$granule_place_holders['granule_stop_time']  = timestampToJulianDay($granule_info[1]);
a63e6108   Myriam Bouchemit   Fix scripts for H...
464
465
466
467
468
469
470
471
472
473
		if ($granule_place_holders['granule_start_time'] > $granule_place_holders['granule_stop_time']) {
			echo "[ERROR] Bad start/stop definition for granule ".$granule_place_holders['granule_index']." of ".$datasetId.PHP_EOL;
			continue;
		}
		if (!empty($time_restriction)) {
			// Exclude restricted granules
			if (intval($granule_info[1]) > $time_restriction) {
				continue;
			}
		}
40067249   Benjamin Renard   Update EPN/TAP gr...
474
		$granule_place_holders['modification_date']  = timestampToISO($granule_info[3]);
b2b3cdea   Benjamin Renard   Add script to bui...
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
		$granule_place_holders['granule_start_time_timestamp'] = $granule_info[0];
		$granule_place_holders['granule_stop_time_timestamp']  = $granule_info[1];
		$granule_place_holders['creation_date']      = $granule_place_holders['modification_date'];
		$granule_place_holders['release_date']       = $granule_place_holders['modification_date'];

		$fields = array_values($mapping);
		$all_place_holders = $amda_place_holders + $dataset_place_holders + $granule_place_holders;
		foreach ($all_place_holders as $place_holder => $value) {
			foreach ($fields as &$field) {
				$field = str_replace('@@'.$place_holder.'@@', $value, $field);
			}
		}
		fputcsv($output_file_handle, $fields);
	}
}	

fclose($output_file_handle);
?>