-
Notifications
You must be signed in to change notification settings - Fork 0
/
correct_meta.py
69 lines (59 loc) · 2.52 KB
/
correct_meta.py
1
2
3
4
5
6
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import xml.etree.ElementTree as ET
import argparse
import logging
import subprocess
from correct_xml import add_element, correct_charset
_log = logging.getLogger(__name__)
def edit_add_attrs(adds):
for child in adds:
if child.attrib["name"] == "cdm_trajectory_variables":
adds.remove(child)
if child.attrib["name"] == "subsetVariables":
adds.remove(child)
add_element(adds, "cdm_data_type", "TimeSeries")
add_element(adds, "featureType", "TimeSeries")
add_element(adds, "cdm_timeseries_variables", "datasetID")
add_element(adds, "subsetVariables", "datasetID")
def update_doc(dataset_id):
"""
Edit the xml generated by GenerateDatasetsXml.sh for ctd datasets
"""
document_loc = f"/home/usrerddap/erddap/xml_edit/xml/{dataset_id}.xml"
dataset_id = f"meta_{dataset_id}"
tree = ET.parse(document_loc)
root = tree.getroot()
root.attrib["datasetID"] = dataset_id
correct_charset(root)
for child in root:
# fix for addAttributes
if child.tag == "addAttributes":
add_attrs = child
#edit_add_attrs(add_attrs)
if child.tag == "dataVariable":
profile_index = False
# Fix for the profile index
for grand_child in child:
if grand_child.tag == "sourceName" and grand_child.text == "deployment_start":
profile_index = True
if profile_index:
if grand_child.tag == "destinationName":
grand_child.text = "deployment_start"
profile_index = False
ET.indent(tree, ' ')
out = f"/home/usrerddap/erddap/content/parts/{dataset_id}.xml"
tree.write(out, encoding="utf-8", xml_declaration=True)
_log.info(f"Recombining datasets.xml")
subprocess.check_call(['/usr/bin/bash', "/home/usrerddap/erddap/xml_edit/make_datasets.sh"])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Correct xml for ctd dataset')
parser.add_argument('dataset_id', type=str, help='dataset id')
args = parser.parse_args()
logf = f'/data/log/ctd.log'
logging.basicConfig(filename=logf,
filemode='a',
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
_log.info(f"Start add dataset {args.dataset_id} to xml")
update_doc(args.dataset_id)
_log.info(f"Complete add dataset {args.dataset_id} to xml")