This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sndlib2matplan.py
executable file
·169 lines (142 loc) · 5.64 KB
/
sndlib2matplan.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""sndlib to MatPlanWD XML file converter
Copyright (C) <2011> Robert Vežnaver <robert.veznaver@fer.hr>
Released under the GNU General Public License.
This utility converts a sndlib network model XML file
to a MatPlanWDM XML network model file.
Sndlib files are available from:
http://sndlib.zib.de/
MatPlanWDM is available from:
http://www.ait.upct.es/~ppavon/matplanwdm/
"""
import sys
import math
from xml.etree import ElementTree as ET
def calcGeoDistance(lat1=0, lon1=0, lat2=0, lon2=0):
"""Python implementation of the Haversine formula
Copyright (C) <2009> Bartek Górny <bartek@gorny.edu.pl>
Released under the GNU General Public License.
This module implements the Haversin formula for calculating
the distance between two points on a sphere.
http://en.wikipedia.org/wiki/Haversine_formula
"""
start_long = math.radians(lon1)
start_latt = math.radians(lat1)
end_long = math.radians(lon2)
end_latt = math.radians(lat2)
d_latt = end_latt - start_latt
d_long = end_long - start_long
a = math.sin(d_latt/2)**2 + \
math.cos(start_latt) * math.cos(end_latt) * math.sin(d_long/2)**2
c = 2 * math.asin(math.sqrt(a))
return 6371 * c
def getCityList(tree, namespace=""):
"""Extracts the city name, longitude & latitude
Needs an ElementTree instance and namespace.
Returns a list of dicts with the above info.
"""
nodeList = tree.findall\
("{0}networkStructure/{0}nodes/{0}node".format(namespace))
(cityList, x, y) = (list(), 0, 0)
for node in nodeList:
name = node.attrib["id"]
for coordinates in node:
for element in coordinates:
if element.tag == "{0}x".format(namespace):
x = element.text
if element.tag == "{0}y".format(namespace):
y = element.text
cityList.append({"name":name, "x":x, "y":y})
return cityList
def getLinkList(tree, namespace=""):
"""Extracts the link origin and destination
Needs an ElementTree instance and namespace.
Returns a list of dicts with the above info.
"""
linkListXML = tree.findall\
("{0}networkStructure/{0}links/{0}link".format(namespace))
(linkList, x, y) = (list(), 0, 0)
for link in linkListXML:
for element in link:
if element.tag == "{0}source".format(namespace):
source = element.text
if element.tag == "{0}target".format(namespace):
target = element.text
linkList.append({"source":source, "target":target})
return linkList
def main():
# read in XML from file
fp = open(sys.argv[1],"r")
tree = ET.parse(fp)
fp.close
# extract namespace from the root element
namespace = tree.getroot().tag
namespace = namespace[namespace.find("{"):namespace.find("}")+1]
# write out header
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
print "<!DOCTYPE network SYSTEM \"../dtd/vtDesign.dtd\">"
print "<network author=\"\" date=\"\" description=\"\" "\
+ "flowGeneratorFile=\"\" flowGeneratorParametersString=\"\" "\
+ "multihourPlanning=\"false\" title=\""\
+ str(sys.argv[1][:-4]) + "\" "\
+ "planningAlgorithmFile=\"\" "\
+ "planningAlgorithmParametersString=\"\" trafficMatrixFile=\"\">"
print "\t<layer id=\"physicalTopology\">"
# extract city list and write out
cityList = getCityList(tree,namespace)
for counter, city in enumerate(cityList):
print "\t\t<node id=\""\
+ str(counter+1)\
+ "\" nodeLevel=\"1\" nodeName=\""\
+ city["name"]\
+ "\" nodePopulation=\"#POP#\" nodeTimezone=\"1\" xCoord=\""\
+ city["x"]\
+ "\" yCoord=\""\
+ city["y"]\
+ "\">"
if counter <= ((len(cityList)-1)/2):
print "\t\t\t<eoTransmitter number=\"10\"/>"
print "\t\t\t<oeReceiver number=\"10\"/>"
else:
print "\t\t\t<eoTransmitter number=\"10000\"/>"
print "\t\t\t<oeReceiver number=\"10000\"/>"
print "\t\t\t<wc number=\"0\"/>"
print "\t\t</node>"
# extract link list and write out
linkList = getLinkList(tree,namespace)
for counter, link in enumerate(linkList):
# find source and target city index in list
try:
source = next(cnt for cnt, city in enumerate(cityList)\
if link["source"] in city["name"])
except StopIteration:
print >> sys.stderr, link["source"] + " not found in city list!"
try:
target = next(cnt for cnt, city in enumerate(cityList)\
if link["target"] in city["name"])
except StopIteration:
print >> sys.stderr, link["target"] + " not found in city list!"
# write out
print "\t\t<fibre id=\""\
+ str(counter+1)\
+ "\" origNodeId=\""\
+ str(source+1)\
+ "\" destNodeId=\""\
+ str(target+1)\
+ "\" linkLengthInKm=\""\
+ str(int(calcGeoDistance(\
float(cityList[source]["y"]),\
float(cityList[source]["x"]),\
float(cityList[target]["y"]),\
float(cityList[target]["x"]))))\
+ "\" numberWavelengths=\"40\" />"
# write out footer
print "\t\t<levelInformationMatrix>"
print "\t\t\t<factor idDest=\"1\" idOrig=\"1\" value=\"1\"/>"
print "\t\t</levelInformationMatrix>"
print "\t\t<lightpathCapacity value=\"40\"/>"
print "\t</layer>"
print "</network>"
if __name__ == "__main__":
main()