def init_globals():
    global dx, dy, rcyl, phant, globals_inited_p

    if not globals_inited_p:
        phant = CylinderPhantom()
        # FIXME
        phant.read('cylinder_skin_10MV_140deg.egsphant')
        dx = phant.dx
        dy = phant.dy
        rcyl = phant.radius

        globals_inited_p = True
    def __init__(self, dosefilename):
        global dx, dy, rcyl, phant, globals_inited_p

        debug_print("dosefilename: %s" % dosefilename)

        self.dosefilename = dosefilename
        self.phantfilename = self.dosefilename.split('.')[0] + '.egsphant'

        # square of interest has sides of length 2 * d2
        self.d2 = 0.1  # cm

        debug_print("ALOHA")
        self.dose_dist = egsnrc.MCDose.MCDose(self.dosefilename)
        debug_print("FOOBAR")
        self.phant = CylinderPhantom()
        debug_print("GADZOOKS")
        self.phant.read(self.phantfilename)
        debug_print("CUBAAN")

        # globals
        dx = self.phant.dx
        dy = self.phant.dy
        rcyl = self.phant.radius
def main(dosefilename, angle):
    # extracts the dose from a cylinder phantom, from a dose file
    # 'dosefilename' at an angle 'angle'
    global dx, dy, rcyl, phant, globals_inited_p

    #init_globals()

    phantfilename = dosefilename.split('.')[0] + '.egsphant'

    # square of interest has sides of length 2 * d2
    d2 = 0.1

    # convert to radians
    angle = angle * math.pi / 180.

    dose_dist = egsnrc.MCDose.MCDose(dosefilename)
    phant = CylinderPhantom()
    phant.read(phantfilename)
    dx = phant.dx
    dy = phant.dy
    rcyl = phant.radius

    debug_print('main: rcyl = %f\n' % rcyl)

    # compute the x-y coordinates of the voxels (some extent in z)
    r0 = rcyl - d2  # radial distance to wanted voxel
    x0 = r0 * math.cos(angle)
    y0 = -r0 * math.sin(angle)

    # compute the x-y index of the voxel
    (i0, j0) = indices(x0, y0)

    debug_print('angle = %f rad = %f deg' % (angle, angle / math.pi * 180.))
    debug_print('r0 = %f' % r0)
    debug_print('x0 = %f' % x0)
    debug_print('y0 = %f' % y0)
    debug_print('i0 = %d' % i0)
    debug_print('j0 = %d' % j0)

    # q and r are as in the notes
    q = math.fabs(d2 * (math.cos(angle) + math.sin(angle)))
    r = math.fabs(d2 * (math.cos(angle) - math.sin(angle)))

    # the four corners of the square
    x1 = x0 + q
    y1 = y0 + r

    x2 = x0 - r
    y2 = y0 + q

    x3 = x0 - q
    y3 = y0 - r

    x4 = x0 + r
    y4 = y0 - q

    (i1, j1) = indices(x1, y1)
    (i2, j2) = indices(x2, y2)
    (i3, j3) = indices(x3, y3)
    (i4, j4) = indices(x4, y4)

    # centers of the 4 edges
    (xA, yA, xB, yB, xC, yC, xD, yD) = square_edge_centers(x0, y0, d2, angle)

    debug_print(
        "In main(): (xA, yA), (xB, yB), (xC, yC), (xD, yD) = (%f, %f), (%f, %f), (%f, %f), (%f, %f)"
        % (xA, yA, xB, yB, xC, yC, xD, yD))

    # find the z-indices
    kstart = int((phant.phantom.zedges[-1] - phant.phantom.zedges[0]) /
                 (2 * phant.dz)) - 2

    debug_print('len(zedge) = %d' % len(phant.phantom.zedges))
    debug_print('zedge[0] = %f' % phant.phantom.zedges[0])
    debug_print('zedge[-1] = %f' % phant.phantom.zedges[-1])
    debug_print('kstart = %d' % kstart)

    voxel_count = 0
    avg_dose = 0.
    rms_error = 0.
    for k in range(kstart, kstart + 4):
        for j in range(len(dose_dist.yedges) - 1):
            for i in range(len(dose_dist.xedges) - 1):
                #debug_print("(k,j,i) = (%d,%d,%d)" % (k,j,i))
                if voxel_in_phantom_and_square_p(i, j, k, phant, d2, xA, yA,
                                                 xB, yB, xC, yC, xD, yD):
                    debug_print("aha! dose = %.6e; error = %2.2f%%" %
                                (dose_dist.dose[k, j, i],
                                 dose_dist.error[k, j, i] * 100.))
                    voxel_count += 1
                    avg_dose += dose_dist.dose[k, j, i]
                    rms_error += dose_dist.error[k, j,
                                                 i] * dose_dist.error[k, j, i]

    if voxel_count == 0:
        print >> sys.stderr, "Error: No matching voxels!"
        sys.exit(3)

    avg_dose = avg_dose / voxel_count
    rms_error = math.sqrt(rms_error / voxel_count)

    debug_print("voxel_count = %d" % voxel_count)
    print "avg_dose = %.6e; rms error = %.2f%%" % (avg_dose, rms_error * 100.)
#!/usr/bin/env python2.4

import sys
import math

from CylinderPhantom import *

if __name__ == '__main__':
    phant = CylinderPhantom(sys.argv[1])
    phant.build()