File: util.py

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (95 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download
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
''' Helper module to support McStas trace output processing '''

import os, sys
from math import pi, cos, sin
from numpy import dot, array
import subprocess


def parse_multiline(line):
    ''' Parse a multiline with size as first elements and n points as rest '''
    elems = [float(x) for x in line.split(',')]
    count = int(elems.pop(0))
    points = []
    while count > 0:
        points.append(elems[0:3])
        elems = elems[3:]
        count -= 1
    return points


def rotate(point, (origin, rotm)):
    ''' Rotate and move v according to origin and rotation matrix '''
    return dot(point, rotm) + origin


POINTS_IN_CIRCLE = 128
def draw_circle(plane, pos, radius, comp):
    ''' Draw a circle in plane, at pos and with r radius, rotated by comp '''
    first = None
    p0=[]
    points=[]
    for i in xrange(0, POINTS_IN_CIRCLE):
        walk = 2 * pi * i / POINTS_IN_CIRCLE
        xyz = array(pos)
        xyz[plane[0]] += cos(walk) * radius
        xyz[plane[1]] += sin(walk) * radius
        # rotate
        xyz = rotate(xyz, comp)
        p = (xyz[0], xyz[1],xyz[2])
        points.append(p)
        # Store first point
        if i == 0:
            p0 = p
    # Tie the knot
    points.append(p0)
    return points
    


def get_line(fp):
    ''' Read a line from file-like object '''
    line = fp.readline()
    if line.startswith('Set value'):
        print(line)

    if line != '':
        return line.strip()


def debug(obj):
    ''' Write a Python object to stderr '''
    sys.stderr.write(repr(obj) + '\n')



def startfile(filepath):
    # Use built-in startfile if present (currently Windows-only)
    if hasattr(os, 'startfile'):
        return os.startfile(filepath)

    # Use 'open' on MAC and 'xdg-open' on Linux/*BSD
    # See: http://bugs.python.org/issue3177
    if sys.platform == 'darwin':
        command = ['open']
    else:
        command = ['xdg-open']

    # Run command and capture errors and return code
    ret = 0
    err = None
    try:
        ret = subprocess.call(command + [filepath])
    except OSError, e:
        err = e

    # Print error if something went wrong (couldn't find command or failed to run)
    if ret != 0 or err is not None:
        sys.stderr.write(
            'Error: Could not open file "%s" using "%s"\n' % (filepath, command[0]))
        if err:
            sys.stderr.write('Reason: %s\n' % err)

    # Only return 0 when all went well
    if err: return -1
    else: return ret