Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #!/bin/env python
  2. """
  3. Smear 1D data of equidistance intervals with Gaussians.
  4. """
  5.  
  6. import os,sys,math
  7. import optparse
  8.  
  9. usage= '%prog [options] datafile'
  10.  
  11. parser= optparse.OptionParser(usage=usage)
  12. parser.add_option("-s","--sigma",dest="sigma",type="float",
  13. default=2.0,
  14. help="width of Gaussian (sigma) in the unit of dx.")
  15. parser.add_option("-x",dest="xp",type="int",
  16. default=1,
  17. help="position of x data (start from 1).")
  18. parser.add_option("-y",dest="yp",type="int",
  19. default=2,
  20. help="position of y data (start from 1).")
  21. (options,args)= parser.parse_args()
  22.  
  23. xp= options.xp -1
  24. yp= options.yp -1
  25. sigma= options.sigma
  26. infname= args[0]
  27.  
  28. infile= open(infname,'r')
  29. nline= 0
  30. for line in infile.readlines():
  31. if line[0] == "#":
  32. continue
  33. nline += 1
  34. print ' num of data points in the file=',nline
  35. infile.seek(0)
  36. data= [ [0.0,0.0] for i in range(nline) ]
  37. il= 0
  38. for line in infile.readlines():
  39. if line[0] == "#":
  40. continue
  41. sline= line.split()
  42. data[il][0]= float(sline[xp])
  43. data[il][1]= float(sline[yp])
  44. il += 1
  45. infile.close()
  46.  
  47. dx= data[1][0]-data[0][0]
  48. sgm= sigma*dx
  49. isgm= int(sigma)+1
  50. pref= 1.0/(math.sqrt(2.0*math.pi)*sgm)
  51.  
  52. gdat= [ [0.0,0.0] for i in range(nline) ]
  53.  
  54. for ix in range(nline):
  55. gdat[ix][0]= data[ix][0]
  56. for jx in range(-nline,nline):
  57. if ix+jx < 0 or ix+jx >= nline:
  58. continue
  59. gdat[ix][1] += data[ix+jx][1]*pref*math.exp(-(dx*(jx))**2/sgm**2)*dx
  60.  
  61. outfile= open(infname+'.smeared','w')
  62. for il in range(nline):
  63. outfile.write(' {0:15.2f} {1:15.7f}\n'.format(gdat[il][0],gdat[il][1]))
  64. outfile.close()
  65.  
  66. print ' write '+infname+'.smeared'
  67. print ' GAUSSIAN_SMEAR done '
  68.  
  69. # dtotal= 0.0
  70. # gtotal= 0.0
  71. # for ix in range(nline):
  72. # if ix == 0 or ix == nline-1:
  73. # dtotal += data[ix][1]*dx/2
  74. # gtotal += data[ix][1]*dx/2
  75. # else:
  76. # dtotal += data[ix][1]*dx
  77. # gtotal += data[ix][1]*dx
  78. # print ' dtotal=',dtotal
  79. # print ' gtotal=',gtotal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement