idl.py 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. ''' python equivalents for some IDL functions
  3. '''
  4. import numpy as np
  5. def main():
  6. n=50
  7. print(dist_vals(n))
  8. d = dist(n, 2*n)
  9. print(d)
  10. import pylab as plt
  11. plt.imshow(d)
  12. plt.show()
  13. def dist(nx, ny=None, xlim=None, ylim=None):
  14. ''' like IDLs dist function: Calculates a 2d eucleedian distance array.
  15. '''
  16. if ny is None:
  17. ny = nx
  18. if xlim is None:
  19. xvals = dist_vals(nx)
  20. else:
  21. xvals = np.linspace(xlim[0], xlim[1],nx)
  22. if ylim is None:
  23. yvals = dist_vals(ny)
  24. else:
  25. yvals = np.linspace(ylim[0], ylim[1],ny)
  26. x,y = np.meshgrid(xvals, yvals)
  27. d = np.sqrt(x**2 + y**2)
  28. return d
  29. def dist_vals(n):
  30. maxval = n/2
  31. vals = np.linspace(0, maxval, maxval+1)
  32. if n % 2 == 0:
  33. return np.hstack([vals, vals[1:-1][::-1]])
  34. else:
  35. return np.hstack([vals, vals[1:][::-1]])
  36. if __name__=="__main__":
  37. main()