test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
asciidump.py
Go to the documentation of this file.
1 from xml.dom.minidom import *
2 import os
3 import string
4 import sys
5 import glob
6 import math
7 
8 # output directory
9 CONFIGFILE = "configuration.xml"
10 #CONFIGFILE = "config_test.xml"
11 outdir = "./data/"
12 #outdir = "./Test/"
13 soldir = ""
14 #soldir = "solids"
15 GENSOLID_ONCE = {}
16 LOGFILE = open("log.txt",'w')
17 LOGFILE.write("Logfile\n")
18 #delete previously generated tables
19 cmd = "rm -rf " + outdir
20 os.system(cmd)
21 os.mkdir(outdir)
22 #os.mkdir(outdir+soldir)
23 #for i in glob.glob(outdir+"*"):
24 # try:
25 # os.remove(i)
26 # except:
27 # print "ex"
28 
29 
30 ##################################################################################
31 # Solid actions
32 ##################################################################################
33 
34 # generic for atomic solids ######################################################
35 # input: table-name, dom-element, current-namespace, other #######################
36 # postfix = any other text to be written end of the row, must end with a ',
37 # prefix = any other text to be written beginning of a row, must end with a ','
38 # output: entry in file 'table-name' of all attributes of 'dom-element' ##########
39 def generic_solid(table,element,ns,postfix="",prefix="",docpost="[postfix]",docpre="[prefix],"):
40  if GENSOLID_ONCE.has_key(table) == 0:
41  GENSOLID_ONCE[table]=1
42  f = open(outdir+soldir+table+".cdl",'w')
43  s = "-- "
44  if len(prefix):
45  s = s + docpre
46  for att in element.attributes.values():
47  s = s + att.name + ','
48  if len(postfix):
49  s = s + docpost
50  f.write(s[:-1]+"\n")
51  f.close()
52  file = open(outdir+soldir+table,'a')
53  s=prefix
54  for att in element.attributes.values():
55  if att.name == "name":
56  s = s + qname(att.value,ns) + ','
57  else:
58  s = s + unitc(att.value).__str__() + ','
59  s = s + postfix
60  file.write(s[:-1]+"\n")
61 
62 # examines the sensfullness of the pcone/phedra z-sections or rz-points (currently z-section only)
63 def examine_section(r,ns,kind):
64  # currently only z-sections
65  if kind != "ZS,":
66  return
67  sections = r.getElementsByTagName("ZSection")
68  z = []
69  rmin = []
70  rmax = []
71  for s in sections:
72  z.append(unitc(s.getAttribute("z")))
73  rmin.append(unitc(s.getAttribute("rMin")))
74  rmax.append(unitc(s.getAttribute("rMax")))
75  #print z
76  # check, whether z-values come in order
77  xmin = z[0]
78  for x in z:
79  if x < xmin:
80  LOGFILE.write(r.tagName + " " + qname(r.getAttribute("name"),ns) + " z values not z1 <= z2\n")
81  xmin = x
82  for i in range(0,len(rmin)):
83  if rmin[i] == rmax[i]:
84  LOGFILE.write(r.tagName + " " + qname(r.getAttribute("name"),ns) + " rmin = rmax\n")
85  if (i != 0) and (i != (len(rmin)-1)):
86  LOGFILE.write(" the one above might be a problem!\n")
87  print r.tagName + " " + qname(r.getAttribute("name"),ns) + " rmin = rmax\n", i, range(0,len(rmin))
88 
89  for i in range(0,len(rmin)):
90  if rmin[i] > rmax[i]:
91  LOGFILE.write(r.tagName + " " + qname(r.getAttribute("name"),ns) + " rmin > rmax\n")
92  # check pairs of same z-values
93  zz = {}
94  for i in range(0,len(rmin)):
95  zz[z[i]] = 1
96  #print z[i], zz
97  if i < len(rmin)-1:
98  err = 0
99  if z[i] == z[i+1]:
100  if rmax[i+1] < rmin[i]:
101  err = 1
102  if rmin[i+1] > rmax[i]:
103  err = 2
104  if err>0:
105  LOGFILE.write(r.tagName + " " + qname(r.getAttribute("name"),ns) + " discontinuity at " + i.__str__() + "\n")
106  if len(zz) == 2:
107  #print zz, qname(r.getAttribute("name"),ns)
108  #print n
109  LOGFILE.write(r.tagName + " " + qname(r.getAttribute("name"),ns) + " simpler solid possible\n")
110 
111 def poly_section_action(r,ns,kind):
112  count = 0
113  name = qname(r.getAttribute("name"),ns) + ","
114  if r.tagName == "Polycone":
115  name = name + ","
116  else:
117  name = "," + name
118  #print kind
119  if kind=="ZS,":
120  for i in r.getElementsByTagName("ZSection"):
121  prefix = count.__str__() + ","
122  generic_solid("ZSECTIONS.dat",i,ns,name,prefix,"polycone_solid_name,polyhedra_solid_name,","sequence_no,")
123  count = count + 1
124  else:
125  for i in r.getElementsByTagName("RZPoint"):
126  prefix = count.__str__() + ","
127  generic_solid("RZPOINTS.dat",i,ns,name,prefix,"polycone_solid_name,polyhedra_solid_name,","sequence_no,")
128  count = count + 1
129  #if count<5:
130  # LOGFILE.write("ZSECTION:" +" " + r.tagName + " " + name + "\n")
131  examine_section(r,ns,kind)
132 
134  s = ""
135  for c in r.getElementsByTagName("rSolid"):
136  s = s + qname(c.getAttribute("name"),ns) + ","
137  t = r.getElementsByTagName("Translation")
138  if len(t) != 0:
139  s = s + unitc(t[0].getAttribute("x")).__str__() + ","
140  s = s + unitc(t[0].getAttribute("y")).__str__() + ","
141  s = s + unitc(t[0].getAttribute("z")).__str__() + ","
142  else:
143  s = s + "0,0,0,"
144  r = r.getElementsByTagName("rRotation")
145  if len(r) != 0:
146  s = s + qname(r[0].getAttribute("name"),ns) + ","
147  else:
148  s = s + "rotations:UNIT,"
149  return s
150 
151 # Solid-action ##########################################################################
152 # called during the SOLIDS-table generation
153 # each action creates a sub-table containing the data for the solids
154 # some actions create several sub-tables (e.g. polycones, rz-sectsion ...)
155 
156 def box_action(r,s,ns):
157  s = s + r.tagName +','
158  generic_solid("BOXES.dat",r,ns)
159  return s
160 
162  s = s + r.tagName +','
163  generic_solid("REFLECTIONSOLIDS.dat",r,ns)
164  return s
165 
167  s = s + r.tagName +','
168  generic_solid("SHAPELESSSOLIDS.dat",r,ns)
169  return s
170 
171 def tubs_action(r,s,ns):
172  s = s + r.tagName +','
173  generic_solid("TUBESECTIONS.dat",r,ns)
174  return s
175 
176 def tube_action(r,s,ns):
177  s = s + r.tagName +','
178  generic_solid("TUBES.dat",r,ns)
179  return s
180 
181 def trd1_action(r,s,ns):
182  s = s + r.tagName +','
183  generic_solid("TRD1S.dat",r,ns)
184  return s
185 
186 def polyhedra_action(r,s,ns):
187  kind = "RZ"
188  if len(r.getElementsByTagName("ZSection"))>0:
189  kind = "ZS,"
190  poly_section_action(r,ns,kind)
191  s = s + r.tagName +','
192  generic_solid("POLYHEDRAS.dat",r,ns,kind,"","RZ_or_ZS,")
193  return s
194 
195 def polycone_action(r,s,ns):
196  kind = "RZ"
197  if len(r.getElementsByTagName("ZSection"))>0:
198  kind = "ZS,"
199  poly_section_action(r,ns,kind)
200  generic_solid("POLYCONES.dat",r,ns,kind,"","RZ_or_ZS,")
201  s = s + r.tagName +','
202  return s
203 
204 def cone_action(r,s,ns):
205  s = s + r.tagName +','
206  generic_solid("CONES.dat",r,ns)
207  return s
208 
209 def pseudotrap_action(r,s,ns):
210  s = s + r.tagName +','
211  generic_solid("PSEUDOTRAPEZOIDS.dat",r,ns)
212  return s
213 
214 def trapezoid_action(r,s,ns):
215  s = s + r.tagName +','
216  generic_solid("TRAPEZOIDS.dat",r,ns)
217  return s
218 
220  s = s + r.tagName +','
221  comp = boolean_components(r,ns)
222  generic_solid("BOOLEANSOLIDS.dat",r,ns,"I,"+comp,"","operation,solidA,solidB,x,y,z,rot")
223  return s
224 
225 def unionsolid_action(r,s,ns):
226  s = s + r.tagName +','
227  comp = boolean_components(r,ns)
228  generic_solid("BOOLDEANSOLIDS.dat",r,ns,"U,"+comp,"","operation,solidA,solidB,x,y,z,rot")
229  return s
230 
232  s = s + r.tagName +','
233  comp = boolean_components(r,ns)
234  generic_solid("BOOLEANSOLIDS.dat",r,ns,"S,"+comp,"","operation,solidA,solidB,x,y,z,rot")
235  return s
236 
237 # global things
238 CATEGORIES = {}
239 SOLIDTYPES = {
240  'SubtractionSolid':subtractionsolid_action,
241  'UnionSolid':unionsolid_action,
242  'IntersectionSolid':intersectionsolid_action,
243  'Trapezoid':trapezoid_action,
244  'PseudoTrap':pseudotrap_action,
245  'Cone':cone_action,
246  'Polycone':polycone_action,
247  'Polyhedra':polyhedra_action,
248  'Trd1':trd1_action,
249  'Tube':tube_action,
250  'Tubs':tubs_action,
251  'ShapelessSolid':shapelesssolid_action,
252  'ReflectionSolid':reflectionsolid_action,
253  'Box':box_action,
254  }
255 
256 rad_to_deg = math.pi/180.
257 UNITS = { u'deg':rad_to_deg,
258  u'fm':1e-12,
259  u'nm':1e-9,
260  u'mum':1e-3,
261  u'mm':1.,
262  u'cm':10.,
263  u'm':1000.,
264  u'rad':1,
265  u'g/cm3':1,
266  u'mg/cm3':0.001,
267  u'g/mole':1.,
268  u'mg/mole':0.001
269  }
270 
271 PPCOUNT = [ 0 ]
272 
273 # unitconversion
274 def unitc(s):
275  try:
276  s = string.strip(s)
277  x = string.split(s,'*')
278  if len(x) == 1:
279  return s
280  if len(x) == 2:
281  return string.atof(x[0])*UNITS[x[1]]
282  except:
283  print "ERROR IN unitc: >" + s + "<"
284  print x
285  print UNITS[x[1]]
286 
287 def get_docs(config):
288  result = []
289  print CONFIGFILE
290  doc = xml.dom.minidom.parse(open(CONFIGFILE,'r'))
291  file_elements = doc.getElementsByTagName("File")
292  print `file_elements`
293  for file_element in file_elements:
294  file_name = file_element.getAttribute("name")
295  url_name = file_element.getAttribute("url")
296  print url_name + file_name
297  path = url_name+file_name
298  ns = string.split(path,'/')[-1][:-4]
299  result.append([url_name+file_name,ns])
300  return result
301 
302 ##################################################################################
303 # other actions
304 ##################################################################################
305 
306 def rot_action(r,s,ns):
307  #print r.tagName
308  if r.tagName=="ReflectionRotation":
309  s = s + "1,"
310  else:
311  s = s + "0,"
312  return s
313 
314 # "ABC:abc" -> "ABC:abc", "abc" -> "ns:abc"
315 def qname(s,ns):
316  if string.find(s,':') == -1:
317  s = ns +':' + s
318  return s
319 
320 def comp_action(e,s,ns):
321  file = open(outdir+"MATERIALFRACTIONS.dat",'a')
322  fracs = e.getElementsByTagName("MaterialFraction")
323  for frac in fracs:
324  fm = frac.getAttribute("fraction")
325  mat = qname(frac.getElementsByTagName("rMaterial")[0].getAttribute("name"),ns)
326  result = "" + qname(e.getAttribute("name"),ns) + ',' + mat + ',' + fm + "\n"
327  file.write(result)
328  return s
329 
330 def log_action(logp,s,ns):
331  # isolate the category, name_cat[0]=name, name_cat[1]=category
332  name_cat = string.split(s,',')
333  CATEGORIES[name_cat[1]] ='' # put category in global dictionary
334  #print name_cat[1]
335  nodelist = logp.childNodes
336  l = nodelist.length
337  mat =""
338  sol =""
339  for i in range(0,l-1):
340  if string.find(nodelist[i].nodeName,"Ma") != -1:
341  mat = qname(nodelist[i].getAttribute("name"),ns)
342  if string.find(nodelist[i].nodeName,"So") != -1:
343  sol = qname(nodelist[i].getAttribute("name"),ns)
344  result = name_cat[0] + ',' + sol + ',' + mat + ',' + name_cat[1] + ','
345  return result
346 
347 def pos_action(posp,s,ns):
348  PPCOUNT[0] = PPCOUNT[0] + 1
349  nodelist = posp.childNodes
350  l = nodelist.length
351  parent = ""
352  child = ""
353  tra = []
354  rot = ""
355  for i in range(0,l-1):
356  nn = nodelist[i].nodeName
357  if string.find(nn,'Pa') != -1:
358  parent = qname(nodelist[i].getAttribute("name"),ns)
359  if string.find(nn,'Child') != -1:
360  child = qname(nodelist[i].getAttribute("name"),ns)
361  if string.find(nn,'Tra') != -1:
362  tra.append(unitc(nodelist[i].getAttribute("x")))
363  tra.append(unitc(nodelist[i].getAttribute("y")))
364  tra.append(unitc(nodelist[i].getAttribute("z")))
365  if string.find(nn,'Rot') != -1:
366  rot = qname(nodelist[i].getAttribute("name"),ns)
367  result = PPCOUNT[0].__str__() + ',' + s
368  if len(tra) != 0:
369  result = result + tra[0].__str__() + ',' + tra[1].__str__() + ',' + tra[2].__str__() + ','
370  else:
371  result = result + ",,,"
372  result = result + rot + ',' + parent + ',' + child + ','
373  return result
374 
375 docs_ns = get_docs("configuration.xml")
376 print `docs_ns`
377 for doc in docs_ns:
378  document_path = doc[0]
379  namespace = doc[1]
380  print "Processing document: " + doc[0] + " ns=" + doc[1]
381  doc = xml.dom.minidom.parse(open(doc[0],'r'));
382 
383  # syntax: list( tablename, list(elements), list(attributes) )
384  # prefixing an attribute name with ":" = convert to a q-name
385  # prefixing an attribute name with ";" = do NOT perform unit-conversion
386  table = [
387  [ 'MATERIALS.dat', ['ElementaryMaterial', 'CompositeMaterial'] ,
388  [':name','density'] ,0 ],
389 
390  [ 'ELEMENTARYMATERIALS.dat', ['ElementaryMaterial'],
391  [':name', ';atomicNumber', 'atomicWeight', ';symbol' ], 0 ],
392 
393  [ 'COMPOSITEMATERIALS.dat', ['CompositeMaterial'],
394  [':name',';method' ], comp_action ],
395 
396  [ 'ROTATIONS.dat', ['Rotation', 'ReflectionRotation'],
397  [':name', 'thetaX', 'phiX', 'thetaY', 'phiY', 'thetaZ', 'phiZ'], rot_action ],
398 
399  [ 'LOGICALPARTS.dat', ['LogicalPart'],
400  [':name', ';category', 'itemid' ], log_action ],
401 
402  [ 'POSPARTS.dat', ['PosPart'],
403  ['copyNumber'], pos_action ],
404 
405  [ 'SOLIDS.dat', SOLIDTYPES.keys(),
406  [':name'], SOLIDTYPES.values() ]
407 
408  ]
409 
410  for i in table:
411  tablename = i[0]
412  filename=outdir+tablename
413  file = open(outdir+tablename,'a')
414  elements = i[1]
415  attributes = i[2]
416  action = i[3]
417  elCount = -1
418  for el in elements:
419  elCount = elCount + 1
420  els = doc.getElementsByTagName(el)
421  for e in els:
422  s = ""
423  for at in attributes:
424  flagQ = 0 # q-name
425  flagU = 0 # unit converson
426  if at[0]==':': # process a q-name
427  flagQ = 1
428  at = at[1:]
429  if at[0]==';': # don't do a unit-conversion
430  flagU = 1
431  at = at[1:]
432  attr = e.getAttribute(at)
433  if flagQ == 1: # process a q-name
434  attr = qname(attr,namespace)
435  else:
436  if flagU == 0:
437  attr = unitc(attr)
438  else:
439  if attr == " ": # fix symbol=" " in ElementaryMaterial (could be done in an action)
440  attr = "0"
441  s = s + attr.__str__() + ","
442  if action != 0: # trigger special action if defined in the processing-table
443  le=0
444  try:
445  le=len(action)
446  except TypeError:
447  s = action(e,s,namespace)
448  else:
449  s = action[elCount](e,s,namespace)
450  #print s[:len(s)-1]
451  file.write(s[:len(s)-1]+"\n")
452  file.close()
453 
454 file = open(outdir+"CATEGORIES.dat",'w')
455 for i in CATEGORIES.keys():
456  file.write(i+"\n")
457 file.close()
458 
459 LOGFILE.close()
def unionsolid_action
Definition: asciidump.py:225
def subtractionsolid_action
Definition: asciidump.py:231
def rot_action
other actions
Definition: asciidump.py:306
def comp_action
Definition: asciidump.py:320
def unitc
Definition: asciidump.py:274
def generic_solid
Solid actions.
Definition: asciidump.py:39
def pos_action
Definition: asciidump.py:347
def reflectionsolid_action
Definition: asciidump.py:161
def intersectionsolid_action
Definition: asciidump.py:219
def shapelesssolid_action
Definition: asciidump.py:166
def polycone_action
Definition: asciidump.py:195
def trd1_action
Definition: asciidump.py:181
list action
Definition: asciidump.py:416
def trapezoid_action
Definition: asciidump.py:214
def examine_section
Definition: asciidump.py:63
def pseudotrap_action
Definition: asciidump.py:209
def get_docs
Definition: asciidump.py:287
def tube_action
Definition: asciidump.py:176
def polyhedra_action
Definition: asciidump.py:186
def poly_section_action
Definition: asciidump.py:111
def box_action
Definition: asciidump.py:156
def qname
Definition: asciidump.py:315
def boolean_components
Definition: asciidump.py:133
def cone_action
Definition: asciidump.py:204
def tubs_action
Definition: asciidump.py:171
def log_action
Definition: asciidump.py:330