WSL/SLF GitLab Repository

Skip to content
Snippets Groups Projects
Commit 9d9c6717 authored by duerig's avatar duerig
Browse files

works now, wnly when both jsons are deep (2 dict_depth), then the info, name,...

works now, wnly when both jsons are deep (2 dict_depth), then the info, name, id are still confusing
parent 7bc3099c
No related branches found
No related tags found
2 merge requests!2works now, wnly when both jsons are deep (2 dict_depth), then the info, name,...,!1created LAR project
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -103,7 +103,7 @@ while True:
# print(file_data["profiles"][0].keys())
###
### load JSON files
file_data = json.load(file1)
file_append = json.load(file2)
......@@ -114,9 +114,7 @@ while True:
return max(dict_depth(dic[key], level + 1) for key in dic) if isinstance(dic, dict) else max(
dict_depth(dic[key], level + 1) for key in range(len(dic)))
dict_depth(file_data)
# returns depth of features
# returns depth of features (necessary for correct appending)
def feature_depth(dic, level=1):
for i in range(dict_depth(dic)):
if isinstance(dic, dict) and not any(key in features for key in dic.keys()):
......@@ -133,49 +131,65 @@ while True:
return level
return level
data_depth = feature_depth(file_data)
append_depth = feature_depth(file_append)
file_data_depth = feature_depth(file_data)
file_append_depth = feature_depth(file_append)
if file_append_depth > file_data_depth: # define deeper profile as "base"
file_data_tmp = file_data
file_data = file_append
file_append = file_data_tmp
file_data_depth = feature_depth(file_data)
file_append_depth = feature_depth(file_append)
profiles_dict = {}
# Typical structure of a exported SimpleProfile.json from NiViz
def file_appender(file_data, file_append, data_depth, append_depth):
if data_depth == 1:
if type(file_append) is dict:
def file_appender(file_data, file_append, file_data_depth, file_append_depth):
if file_data_depth == 1: # the case if only one element and not a profile
if type(file_data) is dict:
for i in file_data.keys():
profiles_dict[i] = file_data[i],
elif data_depth == 2:
# create profiles_dict with keys from source data file
profiles_dict[i] = file_data[i]
elif file_data_depth == 2: # usually the case if a profile (either exported from NiViz or created with "Excel_Converter"
if type(file_data) is dict: # usually first layer of a profile is "profiles"
for i in file_data.keys():
if type(file_data[i]) is list:
if type(file_data[i]) is list: # usually "elements" & "layers" contain lists
for j in range(len(file_data[i])):
if type(file_data[i][j]) is dict:
if type(file_data[i][j]) is dict: # Within the list are the features
for k in file_data[i][j].keys():
profiles_dict[k] = file_data[i][j][k]
else:
print("yet unknown data strcuture")
if append_depth == 1:
if file_append_depth == 1: # usually the case if file_append contains only an "element" like SMP measurements
if type(file_append) is dict:
for l in file_append.keys():
if l in profiles_dict:
print("hello")
profiles_dict[l]["elements"].append(file_append[l]["elements"][0])
else:
for l in file_append.keys(): # l = key of dict
if l in profiles_dict: # If "element" (key) already exists in file_data, append "layers" in "elements"
for m in range(len(file_append[l]["elements"])):
print(m)
profiles_dict[l]["elements"].append(file_append[l]["elements"][m])
else: # If "element" (key) is new, create new element in profiles_dict
profiles_dict[l] = file_append[l]
elif append_depth == 2:
elif file_append_depth == 2:
if type(file_append) is dict:
for l in file_append.keys():
if l in profiles_dict:
print(l)
if l == "profiles" and type(file_append[l]) is list:
for m in range(len(file_append[l])):
profiles_dict[l].append(file_append[l][m])
if type(file_append[l][m]) is dict:
for n in file_append[l][m]:
if n in profiles_dict:
print(n)
for o in range(len(file_append[l][m][n]["elements"])):
profiles_dict[n]["elements"].append(file_append[l][m][n]["elements"][o])
else:
for m in range(len(file_append[l])):
profiles_dict[l] = file_append[l][m]
print(n)
profiles_dict[l] = file_append[l][m][n]
#
# if type(file_append[l]) is list:
# for m in range(len(file_append[l])):
# if type(file_append[l][m]) is dict:
# for n in file_append[l][m].keys():
# if n in profiles_dict and data_depth == 2:
# if n in profiles_dict and file_data_depth == 2:
# profiles_dict[l].append(file_append[l][m][n])
# else:
# profiles_dict[l] = file_append[l]
......@@ -183,13 +197,7 @@ while True:
else:
print("Format of appended file not correct")
file_appender(file_data, file_append, data_depth, append_depth)
file_appender(file_data, file_append, file_data_depth, file_append_depth)
# if type(file_append) is dict:
# for l in file_append.keys():
......@@ -209,16 +217,15 @@ while True:
### Remove empty values (only applicable for temp atm --> density is already done above because it is nested and there is always at least one density value)
profiles_list = [profiles_dict]
try:
final_dict = {"profiles": profiles_list, "name": file_data["name"], "id": file_data["id"],
"position": file_data["position"]}
print("tried")
except:
final_dict = {"profiles": profiles_list, "name": file_append["name"], "id": file_append["id"],
"position": file_append["position"]}
# except:
# final_dict = {"profiles": profiles_list, "name": file_append["name"], "id": file_append["id"],
# "position": file_append["position"]}
with open(d_out, 'w') as outfile:
json.dump(final_dict, outfile, indent=2, default=myconverter)
print("saved to: " + d_out)
write_json(fullpath2, fullpath1, d_out)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment