-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_cf_attributes.py
More file actions
110 lines (94 loc) · 4.18 KB
/
process_cf_attributes.py
File metadata and controls
110 lines (94 loc) · 4.18 KB
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
70
71
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
import csv
from pathlib import Path
class CF_Attributes:
def __init__(self):
script_dir = Path(__file__).parent
self.file_path = script_dir / 'cf_attributes.csv'
self._to_dict()
self._global_attributes()
self._coordinate_variable_attributes()
self._data_variable_attributes()
self._boundary_variable_attributes()
self._geometry_container_variable_attributes()
self._quantization_container_variable_attributes()
self._group_attributes
self._variable_attributes()
def _to_dict(self):
# Dictionary to store the result
self.all_attributes = {}
# Read the CSV file and process
with open(self.file_path, "r") as file:
reader = csv.DictReader(file, delimiter="|")
for row in reader:
# Clean the Attribute column
attribute = row["Attribute"].strip("**`")
# Add to dictionary, removing unwanted characters
self.all_attributes[attribute] = {
"Type": row["Type"].strip(),
"Use": row["Use"].strip().split(', '),
"Description": row["Description"].strip()
}
self.all_attributes = {
key: self.all_attributes[key]
for key in sorted(self.all_attributes)
}
def _coordinate_variable_attributes(self):
self.coordinate_variable_attributes = {}
for key, val in self.all_attributes.items():
if 'C' in self.all_attributes[key]['Use']:
self.coordinate_variable_attributes[key] = val
def _data_variable_attributes(self):
self.data_variable_attributes = {}
for key, val in self.all_attributes.items():
if 'D' in self.all_attributes[key]['Use']:
self.data_variable_attributes[key] = val
def _global_attributes(self):
self.global_attributes = {}
for key, val in self.all_attributes.items():
if 'G' in self.all_attributes[key]['Use']:
self.global_attributes[key] = val
def _group_attributes(self):
self.group_attributes = {}
for key, val in self.all_attributes.items():
if 'Gr' in self.all_attributes[key]['Use']:
self.group_attributes[key] = val
def _geometry_container_variable_attributes(self):
self.geometry_container_variable_attributes = {}
for key, val in self.all_attributes.items():
if 'M' in self.all_attributes[key]['Use']:
self.geometry_container_variable_attributes[key] = val
def _boundary_variable_attributes(self):
self.boundary_variable_attributes = {}
for key, val in self.all_attributes.items():
if 'BI' in self.all_attributes[key]['Use'] or 'BI' in self.all_attributes[key]['Use']:
self.boundary_variable_attributes[key] = val
def _quantization_container_variable_attributes(self):
self.quantization_container_variable_attributes = {}
for key, val in self.all_attributes.items():
if 'Q' in self.all_attributes[key]['Use']:
self.quantization_container_variable_attributes[key] = val
def _variable_attributes(self):
# Combine all variable-related dictionaries
self.variable_attributes = {}
variable_dictionaries = [
self.coordinate_variable_attributes,
self.data_variable_attributes,
self.boundary_variable_attributes,
self.geometry_container_variable_attributes,
self.quantization_container_variable_attributes
]
for var_dict in variable_dictionaries:
for key, value in var_dict.items():
if key not in self.variable_attributes:
self.variable_attributes[key] = value
self.variable_attributes = {
key: self.variable_attributes[key]
for key in sorted(self.variable_attributes)
}
def main():
# Create an instance of CF_Attributes
cf_attributes = CF_Attributes()
# Call the display method
cf_attributes.display_global_attributes()
if __name__ == "__main__":
main()