-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_diag.py
91 lines (83 loc) · 3.02 KB
/
convert_diag.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
import json
import re
## Open the input (diag_table) and output (diag_json) files
diag_table=open("diag_table",'r')
diag_json=open("diag_json",'w')
#####################################################################
## Set up the lists
files = []
variables = []
topinfo = []
## Loop through the lines in diag_table
for lines in diag_table:
# print lines
## Clean up the lines and remove quotes, white space on the ends, and trailing commas
templine = lines.strip(',\n')
templine = templine.replace('"',"")
templine = templine.strip()
diagline = templine.split(",")
# print diagline, len(diagline)
## Seperate out comments in the diag table
if re.search('^#.*',diagline[0]):
continue
## Entries with 8 mebers are diagnostic variables
# elif len(diagline)>= 8:
# variables.append(diagline)
# print "This is a variable"
## Entries without commas are the experiment and base date
elif len(diagline) == 1:
topinfo.append(diagline)
## Entriles with a string then a number are files.
# elif len(diagline) == 6:
elif re.search('\s*\w+',diagline[0]) and re.search('^-*\d+',diagline[1].strip()):
print diagline
files.append(diagline)
# print "This is a file"
## All other entries are variables
else:
variables.append(diagline)
# print lines
# print "This is an error or is not yet supported."
# quit()
## Close the input file
diag_table.close()
#####################################################################
## Sort out the experimnet and base date
exp = topinfo[0][0]
dt = topinfo[1][0]
## Start the json dictionary
fullJson = {
"experiment": exp,
"date": dt
}
fnames=[]
filesJson={}
## Loop through the files and crate their entries in the dctionary
for i in range(len(files)):
if files[i][0] in fnames:
print files[i][0]+" Is already listed as a filename. skipping duplicate"
else:
fnames.append(files[i][0])
filesJson[files[i][0]] = [files[i][1].strip(),files[i][2].strip(),files[i][3].strip(),files[i][4].strip(),files[i][5].strip()]
## Write the files dictionary to the fill dictionary
fullJson['files']=filesJson
#####################################################################
modnames=[]
varsJson={}
for i in range(len(variables)):
if variables[i][1] in modnames:
if not variables[i][0].strip() in varsJson[variables[i][1].strip()]:
varsJson[variables[i][1].strip()][variables[i][0].strip()]=[]
# varsJson[variables[i][1].strip()][variables[i][0].strip()].append(map(str.strip,variables[i][2:7]))
else:
modnames.append(variables[i][1])
varsJson[variables[i][1].strip()] = {}
varsJson[variables[i][1].strip()][variables[i][0].strip()]=[]
# varsJson[variables[i][1].strip()][variables[i][0].strip()].append(map(str.strip,variables[i][2:7]))
varsJson[variables[i][1].strip()][variables[i][0].strip()].append(map(str.strip,variables[i][2:7]))
fullJson.update(varsJson)
#####################################################################
## Write the json to the output file
diag_json.write(json.dumps(fullJson,sort_keys=True, indent=2))
#diag_json.write(json.dumps(fullJson))
diag_json.close()