Guest User

egsplot-dose1d.py

a guest
Apr 17th, 2013
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys, re, os.path
  4.  
  5. # usage
  6. if (len(sys.argv) < 2):
  7.     print '''\nusage:
  8.    
  9.    %s  x|y|z  a,b  filenames
  10.    
  11.    x|y|z       axis of the dose profile
  12.    a,b         coordinates along the other axes
  13.    filenames   space separated list of files to plot
  14.    
  15.    examples:   egsplot-dose1d.py z 0,0 *.3ddose
  16.                (extract the central z-axis dose profile for all .3ddose files)
  17.    
  18.                egsplot-dose1d.py z 0,0 *.3ddose | xmgrace -settype xydy -pipe
  19.                (pipe the output to xmgrace plotting tool)
  20.    '''%(os.path.basename(sys.argv[0]))
  21.     sys.exit(1)
  22.  
  23. # simplistic command-line parsing
  24. var   = sys.argv[1]                                     # arg1 is the independent variable: x, y, or z
  25. axis  = ['x', 'y', 'z'].index(var)                      # axis number: x=0, y=1, z=3
  26. where = map(float,sys.argv[2].split(','))               # position of the axis in x,y form
  27. files = sys.argv[3:]                                    # array of all files to process
  28.  
  29. # print xmgrace header
  30. print '''\
  31. @   title "dose distribution"
  32. @   xaxis label "%s (cm)"
  33. @   yaxis label "dose (Gy)"
  34. @   type xydy''' % var
  35.  
  36. # loop over all files
  37. count = 0
  38. for file in files:
  39.  
  40.     # open 3ddose file
  41.     dosefile = open(file, 'r')
  42.  
  43.     # get voxel counts on first line
  44.     nx, ny, nz = map(int,dosefile.readline().split())    # number of voxels along x, y, z
  45.     Ng = (nx+1) + (ny+1) + (nz+1)                        # total number of voxel grid values (voxels+1)
  46.     Nd = nx*ny*nz                                        # total number of data points
  47.  
  48.     # get voxel grid, dose and relative errors
  49.     data  = map(float,dosefile.read().split())           # read the rest of the file
  50.     xgrid = data[:nx+1]                                  # voxel boundaries in x (nx+1 values, 0 to nx)
  51.     ygrid = data[nx+1:nx+1+ny+1]                         # voxel boundaries in y (ny+1 values, nx+1 to nx+1+ny)
  52.     zgrid = data[nx+1+ny+1:Ng]                           # voxel boundaries in z (nz+1 values, rest up to Ng-1)
  53.     dose  = data[Ng:Nd+Ng]                               # flat array of Nd = nx*ny*nz dose values
  54.     errs  = data[Nd+Ng:]                                 # flat array of Nd = nx*ny*nz relative error values
  55.     del data                                             # make sure we don't refer to data by mistake from now on
  56.  
  57.     # close 3ddose file
  58.     dosefile.close()
  59.  
  60.     # setup variables for current axis
  61.     grid   = [xgrid, ygrid, zgrid]                       # voxel boundaries in x, y, z
  62.     step   = [1, nx, nx*ny]                              # step between values along x, y, z
  63.     jump   = [nx, nx*ny, nx*ny*nz]                       # distance between start and end voxels
  64.     mygrid = grid[axis]                                  # grid for plot axis
  65.     mystep = step[axis]                                  # step for plot axis
  66.     del grid[axis]                                       # remove plot axis from grid
  67.     del step[axis]                                       # remove plot axis from step
  68.  
  69.     # get voxel indices for location (along two remaining axes)
  70.     # (say you are plotting along z, then index will hold the indices for the requested x,y positions
  71.     index = []
  72.     for g,w in zip(grid,where):
  73.         if (w<g[0] or w>g[-1]):
  74.             print "ERROR: location", where, "outside of data grid!\n"
  75.             sys.exit(1)
  76.         i = len(g)-1
  77.         while (w < g[i]): i -= 1
  78.         index.append(i)
  79.  
  80.     # get the actual dose profiles
  81.     start  = index[0]*step[0] + index[1]*step[1]         # starting voxel index
  82.     end    = start + jump[axis]                          # "end" voxel index
  83.     mydose = dose[start:end:mystep]                      # dose slice
  84.     myerrs = errs[start:end:mystep]                      # relative error slice
  85.  
  86.     # print xmgrace format commands for this set
  87.     print '''\
  88. @   s%d errorbar length 0
  89. @   s%d legend "%s" ''' % (count, count, file)
  90.  
  91.     # print dose profile with absolute errors
  92.     print "#\n# %10s %12s %12s" % (var, "dose", "err")
  93.     for i in range(len(mydose)):
  94.         t = (mygrid[i]+mygrid[i+1])/2.0
  95.         print "%12.6f %12g %12g" % (t, mydose[i], myerrs[i]*mydose[i])
  96.     print
  97.  
  98.     count += 1
Advertisement
Add Comment
Please, Sign In to add comment