Commit f17b62eeeadf335df12804b2c2ca79d64eca7dc9
1 parent
a63e6108
Exists in
SpeasyGet
For now
Showing
2 changed files
with
163 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,40 @@ |
1 | +<?php | |
2 | +/** | |
3 | + * @class SPEASY_CDAWEB | |
4 | + * @brief | |
5 | + */ | |
6 | + | |
7 | +class SPEASY_CDAWEB extends RemoteDataCenterClientClass | |
8 | +{ | |
9 | + function __construct() | |
10 | + { | |
11 | + $this->baseID = get_class($this); | |
12 | + $this->baseDom = new DomDocument("1.0"); | |
13 | + $this->baseDomName = RemoteData.$this->baseID."/base.xml"; | |
14 | + | |
15 | + if (!@$this->baseDom->load($this->baseDomName)) | |
16 | + echo 'Cannot Load base.xml for '.$this->baseID.PHP_EOL; | |
17 | + | |
18 | + date_default_timezone_set('UTC'); | |
19 | + } | |
20 | + | |
21 | + protected function makeInternalParamXml($param) | |
22 | + { | |
23 | + return true; | |
24 | + } | |
25 | + | |
26 | + public function makeAllParams() | |
27 | + { | |
28 | + | |
29 | + } | |
30 | + | |
31 | + // make components description from base.xml | |
32 | + public function makeCenterNode($xmlDom) | |
33 | + { | |
34 | + $nodeBase = $this->baseDom->getElementsByTagName('dataCenter')->item(0); | |
35 | + $node = $xmlDom->importNode($nodeBase, true); | |
36 | + | |
37 | + return $node; | |
38 | + } | |
39 | +} | |
40 | +?> | ... | ... |
... | ... | @@ -0,0 +1,123 @@ |
1 | +import json | |
2 | +import xml.etree.ElementTree as ET | |
3 | +from xml.dom import minidom | |
4 | + | |
5 | +def create_element_with_id(tag, name, uid, parent_uid=None): | |
6 | + """Create an XML element with a specific id and name.""" | |
7 | + if parent_uid: | |
8 | + uid = f"{parent_uid}:{uid}" | |
9 | + elem = ET.Element(tag) | |
10 | + elem.set('xml:id', f"SPEASY_CDAWEB:{uid}") | |
11 | + elem.set('name', name) | |
12 | + return elem | |
13 | + | |
14 | +def parse_dimensions(shape): | |
15 | + dim1 = dim2 = None | |
16 | + if shape.isdigit(): | |
17 | + dim1 = int(shape) | |
18 | + elif shape.startswith("(") and shape.endswith(",)"): | |
19 | + dim1 = int(shape[1:-2]) | |
20 | + elif shape.startswith("(") and shape.endswith(")"): | |
21 | + numbers = shape[1:-1].split(",") | |
22 | + dim1 = int(numbers[0].strip()) | |
23 | + dim2 = int(numbers[1].strip()) | |
24 | + | |
25 | + return dim1, dim2 | |
26 | + | |
27 | +def getParamInfo(parameter, shape): | |
28 | + """Get dims and type info from the json to the XML element.""" | |
29 | + dim1, dim2 = parse_dimensions(shape) | |
30 | + if not dim1 == None: | |
31 | + parameter.set('dim1', str(dim1)) | |
32 | + if dim2 == None: | |
33 | + parameter.set('dim2', "1") | |
34 | + else: | |
35 | + parameter.set('dim2', str(dim2)) | |
36 | + parameter.set('type', 'double') | |
37 | + parameter.set('minSampling', '4') | |
38 | + | |
39 | +def count_levels(data): | |
40 | + """Recursively count the levels of nested dictionaries.""" | |
41 | + if isinstance(data, dict): | |
42 | + return 1 + max((count_levels(v) for v in data.values()), default=0) | |
43 | + return 0 | |
44 | + | |
45 | +def get_mission_levels(json_obj): | |
46 | + """Create a dictionary with mission names as keys and nested levels as values.""" | |
47 | + levels_dict = {} | |
48 | + for mission_key, mission_data in json_obj.items(): | |
49 | + if isinstance(mission_data, dict): | |
50 | + levels_dict[mission_key] = count_levels(mission_data) | |
51 | + return levels_dict | |
52 | + | |
53 | +def add_instrument(observatory, observatory_data, mission_observatory_key): | |
54 | + """Add the instrument, dataset and parameter to an observatory or a mission.""" | |
55 | + for instrument_key, instrument_data in observatory_data.items(): | |
56 | + if isinstance(instrument_data, dict) and '__spz_name__' in instrument_data: | |
57 | + instrument = create_element_with_id('instrument', instrument_data.get('__spz_name__', ''), instrument_key, mission_observatory_key) | |
58 | + observatory.append(instrument) | |
59 | + | |
60 | + for dataset_key, dataset_data in instrument_data.items(): | |
61 | + if isinstance(dataset_data, dict) and '__spz_name__' in dataset_data: | |
62 | + dataset = create_element_with_id('dataset', dataset_data.get('__spz_name__', ''), dataset_key, f"{mission_observatory_key}:{instrument_key}") | |
63 | + instrument.append(dataset) | |
64 | + | |
65 | + for param_key, param_data in dataset_data.items(): | |
66 | + if isinstance(param_data, dict) and param_data.get('__spz_type__') == 'ParameterIndex': | |
67 | + parameter = create_element_with_id('parameter', param_data.get('__spz_name__', ''), param_key, f"{mission_observatory_key}:{instrument_key}:{dataset_key}") | |
68 | + getParamInfo(parameter, param_data.get('spz_shape', '')) | |
69 | + dataset.append(parameter) | |
70 | + | |
71 | + | |
72 | +def json_to_custom_xml(json_obj, levels): | |
73 | + """Convert JSON object to custom XML format.""" | |
74 | + dataRoot = ET.Element('dataRoot') | |
75 | + dataRoot.set('xml:id', "myRemoteData-treeRootNode") | |
76 | + dataCenter = ET.Element('dataCenter') | |
77 | + dataCenter.set('xml:id', "SPEASY_CDAWEB") | |
78 | + dataCenter.set('name', "SPEASY_CDAWEB") | |
79 | + dataRoot.append(dataCenter) | |
80 | + | |
81 | + for mission_key, mission_data in json_obj.items(): | |
82 | + if isinstance(mission_data, dict): | |
83 | + mission = create_element_with_id('mission', mission_data.get('__spz_name__', ''), mission_key) | |
84 | + dataCenter.append(mission) | |
85 | + | |
86 | + | |
87 | + if(levels[mission_key] == 4): | |
88 | + observatory_data = mission_data | |
89 | + observatory = mission | |
90 | + mission_observatory_key = mission_key | |
91 | + add_instrument(observatory, observatory_data, mission_observatory_key) | |
92 | + else: | |
93 | + | |
94 | + for observatory_key, data in mission_data.items(): | |
95 | + if isinstance(data, dict): | |
96 | + observatory = create_element_with_id('observatory', data.get('__spz_name__', ''), observatory_key, mission_key) | |
97 | + observatory_data = data | |
98 | + mission_observatory_key = f"{mission_key}:{observatory_key}" | |
99 | + mission.append(observatory) | |
100 | + | |
101 | + add_instrument(observatory, observatory_data, mission_observatory_key) | |
102 | + break | |
103 | + | |
104 | + return ET.tostring(dataRoot, encoding='unicode') | |
105 | + | |
106 | +def pretty_print_xml(xml_str): | |
107 | + """Pretty print the XML string.""" | |
108 | + parsed_xml = minidom.parseString(xml_str) | |
109 | + return parsed_xml.toprettyxml(indent=" ") | |
110 | + | |
111 | +def convert_json_file_to_xml(json_file_path, xml_file_path): | |
112 | + """Reads a JSON file and writes its content as pretty-printed XML to another file.""" | |
113 | + with open(json_file_path, 'r') as json_file: | |
114 | + json_data = json.load(json_file) | |
115 | + | |
116 | + levels_dict = get_mission_levels(json_data) | |
117 | + xml_data = json_to_custom_xml(json_data, levels_dict) | |
118 | + pretty_xml = pretty_print_xml(xml_data) | |
119 | + | |
120 | + with open(xml_file_path, 'w') as xml_file: | |
121 | + xml_file.write(pretty_xml) | |
122 | + | |
123 | +convert_json_file_to_xml('data.json', 'base.xml') | |
0 | 124 | \ No newline at end of file | ... | ... |