File size: 1,716 Bytes
5877840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def read_kvl_file(file):
    data = {}
    current_key = None

    for line in file:
        line = line.decode().strip()  # Convert bytes to string and strip whitespace
        if not line:  # Skip empty lines
            continue
        if '=' in line:
            key, value = line.split('=')
            key = key.strip()
            value = value.strip()
            if value:  # Non-empty value, treat as vector data
                if key not in data:
                    data[key] = []
                try:
                    data[key].append(float(value))
                except ValueError:
                    data[key].append(value)
            else:  # Empty value, treat as matrix data
                current_key = key
                if current_key not in data:
                    data[current_key] = []  # Initialize list to store matrices
        elif current_key:
            row_values = line.split()
            if row_values:
                data[current_key].append([float(value) for value in row_values])

    return data

# def write_data_to_file(file_path, data):
#     with open(file_path, 'w') as file:
#         for key, values in data.items():
#             file.write(key + ':\n')
#             if isinstance(values[0], list):  # Matrix data
#                 for row in values:
#                     file.write(' '.join(row) + '\n')
#             else:  # Vector data
#                 for value in values:
#                     file.write(value + '\n')

# # Example usage
# input_file_path = 'd:\Git\KVL_visualize\Turbidites_out.kvl'
# output_file_path = 'd:\Git\KVL_visualize\Test.txt'

# kvl_data = read_kvl_file(input_file_path)

# write_data_to_file(output_file_path, kvl_data)