-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEMXtoOMX.py
84 lines (70 loc) · 2.81 KB
/
EMXtoOMX.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
#Read and write OMX matrices from/to the databank
#Ben Stabler, ben.stabler@rsginc.com, 05/06/16
#Can export mfs, mos, and mds, but only one type at a time
#Arguments: emme_project scenario omx_file_name -i|e mat1 mat2 matN
#SET EMMEPY="C:\Program Files\INRO\Emme\Emme 4\Emme-4.2.5\Python27\python.exe"
#Example export: %EMMEPY% EMXtoOMX.py myproj.emp 9999 mats.omx -e mf1 mf2 mf3
#Example import: %EMMEPY% EMXtoOMX.py myproj.emp 9999 mats.omx -i mf1 mf2 mf3
######################################################################
#load libraries
import inro.modeller as m
import inro.emme.desktop.app as d
import sys, os.path, os
import openmatrix as omx
#run command line version
if __name__ == "__main__":
#start EMME desktop and attach a modeller session
empFile = sys.argv[1]
scenarioNum = sys.argv[2]
omxFile = sys.argv[3]
ioMode = sys.argv[4]
desktop = d.start_dedicated(False, "bts", empFile)
m = m.Modeller(desktop)
#determine if import or export mode
if ioMode == "-e":
export = True
else:
export = False
#get location of bank
bankDir = os.path.dirname(m.emmebank.path)
omx_file = os.path.join(bankDir, "emmemat", omxFile)
#get matrix names from command line argument
mats = []
for i in range(5,len(sys.argv)):
mats.append(sys.argv[i])
#export matrices
if export:
export_to_omx = m.tool("inro.emme.data.matrix.export_to_omx")
export_to_omx(mats, omx_file)
#remove appended names so mat names are just numbers
exportByNumber = True
if exportByNumber:
omxFile = omx.open_file(omx_file,"a")
matNames = omxFile.listMatrices()
matsLookup = dict(zip(map(lambda x: x.split("_")[0], matNames), matNames))
for i in range(len(matsLookup.keys())):
omxFile[matsLookup.keys()[i]] = omxFile[matsLookup.values()[i]]
del omxFile[matsLookup.values()[i]]
omxFile.close()
print(",".join(mats) + " -> " + omx_file)
#else import
else:
#map matrix names in file to matrix numbers in bank if needed
matNames = omx.open_file(omx_file).listMatrices()
if "_" in matNames[0]:
matsLookup = dict(map(lambda x: x.split("_"), matNames))
matsDict = {}
for aMat in mats:
if aMat in matsLookup.keys():
matsDict[aMat + "_" + matsLookup[aMat]] = aMat
else:
matsDictAll = dict(zip(matNames, matNames))
matsDict = {}
for aMat in mats:
if aMat in matsDictAll.keys():
matsDict[aMat] = aMat
#import matrices
import_from_omx = m.tool("inro.emme.data.matrix.import_from_omx")
scen = m.emmebank.scenario(scenarioNum)
import_from_omx(omx_file, matsDict, 'zone_number', scen)
print(omx_file + " -> " + ",".join(mats))