• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# coding=utf-8
3"""
4 * Copyright (c) 2022 Huawei Device Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16"""
17import re
18import os
19import logging
20import pickle
21import png
22
23
24def read_data(filename):
25    data_res = {}
26    loc_pattern = re.compile("\(.*\)")
27    tz_pattern = re.compile("\[.*\]")
28    raw_lines = []
29    with open(filename, "r") as f:
30        lines = f.readlines()
31        for line in lines:
32            raw_lines.append(line)
33    for line in raw_lines:
34        line = line.strip()
35        loc = re.search(loc_pattern, line).group()
36        tz = re.search(tz_pattern, line).group()
37        location = eval(loc)
38        timezones = tz[1:-1].split(",")
39        data_res[location] = timezones
40    with os.fdopen(os.open("data/loc2tz.pickle", os.O_RDWR | os.O_CREAT, 0o640), "wb") as f:
41        pickle.dump(data_res, f)
42    return data_res
43
44
45def load_data(pickle_name):
46    with open(pickle_name, "rb") as f:
47        loaded_data = pickle.load(f)
48    return loaded_data
49
50
51def classify_timezones(data_input):
52    group_wn = set()
53    group_en = set()
54    group_ws = set()
55    group_es = set()
56    for loc, tzs in data_input.items():
57        if loc[0] < 0 and loc[1] >= 0:
58            for tz in tzs:
59                group_wn.add(tz)
60        elif loc[0] >= 0 and loc[1] >= 0:
61            for tz in tzs:
62                group_en.add(tz)
63        elif loc[0] < 0 and loc[1] < 0:
64            for tz in tzs:
65                group_ws.add(tz)
66        else:
67            for tz in tzs:
68                group_es.add(tz)
69    return [group_wn, group_en, group_ws, group_es]
70
71
72def numerized_dictionary(classes):
73    numerized_classes = []
74    for group in classes:
75        temp_group = {}
76        sgroup = list(group)
77        sgroup.sort()
78        for num, name in enumerate(sgroup):
79            temp_group[name] = num
80        numerized_classes.append(temp_group)
81    for i, c in enumerate(numerized_classes):
82        logging.info(i)
83        logging.info(c)
84    return numerized_classes
85
86
87def export_numerized_dicts(numerized_classes):
88    with os.fdopen(os.open("data/name2num.txt", os.O_RDWR | os.O_CREAT, 0o640), "w", encoding="utf-8") as f:
89        f.write(str(numerized_classes))
90    num2name_dict_list = []
91    for d in numerized_classes:
92        num2name = dict(zip(d.values(), d.keys()))
93        num2name_dict_list.append(num2name)
94    with os.fdopen(os.open("data/num2name.txt", os.O_RDWR | os.O_CREAT, 0o640), "w", encoding="utf-8") as f:
95        f.write(str(num2name_dict_list))
96
97
98def generate_array_from_data(data_input, numerized_classes):
99    total_nums_list = []
100    for key, val in data_input.items():
101        original_keys = key
102        if original_keys[0] < 0 and original_keys[1] >= 0:
103            quater = 0
104        elif original_keys[0] >= 0 and original_keys[1] >= 0:
105            quater = 1
106        elif original_keys[0] < 0 and original_keys[1] < 0:
107            quater = 2
108        else:
109            quater = 3
110        names_l = []
111        for v in val:
112            names_l.append(numerized_classes[quater][v])
113        nums_l = [255, 255, 255]
114        for i in enumerate(names_l):
115            nums_l[i[0]] = names_l[i[0]]
116        total_nums_list += nums_l
117    return total_nums_list
118
119
120def group_in_total(total_list):
121    i = 0
122    map_of_array = []
123    col_of_array = []
124    for item in total_list:
125        col_of_array.append(item)
126        i += 1
127        if i == 1800 * 3:
128            map_of_array.append(col_of_array)
129            col_of_array = []
130            i = 0
131    return map_of_array
132
133
134def pack2png(map_of_array):
135    png.from_array(map_of_array, "RGB").save("data/loc2tz.dat")
136
137
138def save_png_test():
139    array_one_point = [255, 0, 0]
140    array_one_row = [array_one_point] * 3600
141    array_map = [array_one_row] * 1800
142    png.from_array(array_map, "RGB").save("03.png")
143
144
145if __name__ == '__main__':
146    read_data("data/location2tz.txt")
147    data = load_data("data/loc2tz.pickle")
148    name_classes = classify_timezones(data)
149    nmrzd_classes = numerized_dictionary(name_classes)
150    export_numerized_dicts(nmrzd_classes)
151    t_list = generate_array_from_data(data, nmrzd_classes)
152    map_array = group_in_total(t_list)
153    pack2png(map_array)