-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathretrieve_mappings.py
137 lines (99 loc) · 3.96 KB
/
retrieve_mappings.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import pandas as pd
from entsog import EntsogRawClient, EntsogPandasClient
from entsog.parsers import parse_general
def get_operator_mappings():
client = EntsogPandasClient()
operators = client.query_operators(country_code=None)
points = client.query_operator_point_directions()
countries = operators.drop_duplicates(subset=['operator_country_key'])
c = []
for i, item in countries.iterrows():
country = item['operator_country_key']
operators_country = operators[operators['operator_country_key'] == country]
d = []
e = []
for j, jtem in operators_country.iterrows():
print(jtem)
operator = jtem['operator_key']
points_operator = points[points['operator_key'] == country]
for m, mtem in points_operator.iterrows():
e.append(
{
'point': mtem['point_key'],
'point_label': mtem['point_label']
}
)
d.append({**jtem.to_dict(), 'points': e})
c.append(
{
'country': country,
'operators': d
}
)
print(c)
#with open('mapping/countries.json', 'w') as fp:
# json.dump(c, fp)
def get_point_mappings():
client = EntsogPandasClient()
points = client.query_operator_point_directions()
print(points.columns)
print(points.head())
print(points['id'])
points = points[['point_key','point_label']].drop_duplicates()
for idx, item in points.iterrows():
print(f"""{item['point_label']} = "{item['point_key']}" """)
def get_area():
client = EntsogRawClient()
data = json.loads(client.query_operator_point_directions(limit=-1))
df = pd.json_normalize(data['operatorpointdirections'])
df_drop = df.drop_duplicates(subset=['tSOCountry'])
c = {}
for idx, item in df_drop.iterrows():
country = item['tSOCountry']
filtered = df[df['tSOCountry'] == country]
operatorKey = filtered.loc[:, 'operatorKey'].drop_duplicates()
# print(operatorKey)
operatorLabel = filtered.loc[:, 'operatorLabel'].drop_duplicates()
if country is None:
country = 'misc'
print(f"{country} ={country}, {tuple(operatorKey)}, {tuple(operatorLabel)} ,")
def check_new_operators():
client = EntsogRawClient()
json_data, url = client.query_operators(country_code=None)
data = parse_general(json_data)
result = {}
for index, item in data.iterrows():
country = item['operator_country_key']
# Only TSO's
if 'TSO' not in item['operator_key']:
continue
if country in result.keys():
result[country]['operators'].append(item['operator_key'])
result[country]['operator_labels'].append(item['operator_label'])
else:
result[country] = {
'operators': [item['operator_key']],
'operator_labels': [item['operator_label']],
'label': item['operator_country_label']
}
# Print result
# Countries
for key, value in result.items():
print(f"""{key} = "{key}", "{value['label']}" """)
# Enum area
for key, value in result.items():
parsed_operators = ','.join([f'"{o}"' for o in value['operators']])
parsed_operator_labels = ','.join([f'"{o}"' for o in value['operator_labels']])
print(f"""{key} = ({parsed_operators},), ({parsed_operator_labels},)""")
regions = pd.read_csv(
"https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv")
# Regions
for key, value in result.items():
try:
region = str(regions[regions['alpha-2'] == key]['sub-region'].iloc[0])
print(f"""{key} : "{region}" """)
except Exception as e:
print(f"""{key} : "REGION" """)
continue
get_point_mappings()