Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. def dispersion():
  2.     MAP_CENTER = [8192/2, 8192/2]
  3.     param = [1,1]
  4.     frame = 50
  5.     axis='y'  
  6.     sim=sims[0]
  7.     # Choose a random point (py,px) in a 8192x8192 map
  8.     py=np.random.randint(8192)
  9.     px=np.random.randint(8192)
  10.     at = access_thing(sim,res,frame,param[0],param[1], machine=machine)
  11.  
  12.     # load stokes maps and calculate \phi in radians
  13.     Q = at.get_fits_array('Q%s_n0-%04d_p-%d'%(axis,param[0],param[1]), axis)
  14.     U = at.get_fits_array('U%s_n0-%04d_p-%d'%(axis,param[0],param[1]), axis)
  15.     _phi = 0.5*np.arctan2(U,Q)    
  16.    
  17.     SIZE = int(sys.argv[1])                 # size of cutout around (py,px)
  18.     c = (SIZE/2,SIZE/2)                     # center of cutout
  19.     max_l = SIZE/2
  20.     l_map=np.zeros([SIZE,SIZE]).astype(int)
  21.     l_list = range(1,max_l)
  22.     l_masks = {}                            # dict {l:l_mask}
  23.     nl = {}                                 # dict {l:number of pixels at a distance l from the center}
  24.     cos_delta_phi_l = {}                    # dict {l: Sum of all cos(\Delta\phi) at a given l}        
  25.     dispersion_function = []                # 1 - <cos(\Delta\phi>
  26.    
  27.     # Make a map of the distance l from the center (py,px) to each pint (i,j) in the map
  28.     # along with masks for each l that is True for all points (i,j) at a distance l from the center
  29.     # and False for all points that are not a distance l away.
  30.     # here is what l_map looks like when size = 15
  31.     #[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  32.     # [0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0],
  33.     # [0, 0, 0, 6, 6, 5, 5, 5, 5, 5, 6, 6, 0, 0, 0],
  34.     # [0, 0, 6, 6, 5, 4, 4, 4, 4, 4, 5, 6, 6, 0, 0],
  35.     # [0, 0, 6, 5, 4, 4, 3, 3, 3, 4, 4, 5, 6, 0, 0],
  36.     # [0, 6, 5, 4, 4, 3, 2, 2, 2, 3, 4, 4, 5, 6, 0],
  37.     # [0, 6, 5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5, 6, 0],
  38.     # [0, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 0],
  39.     # [0, 6, 5, 4, 3, 2, 1, 1, 1, 2, 3, 4, 5, 6, 0],
  40.     # [0, 6, 5, 4, 4, 3, 2, 2, 2, 3, 4, 4, 5, 6, 0],
  41.     # [0, 0, 6, 5, 4, 4, 3, 3, 3, 4, 4, 5, 6, 0, 0],
  42.     # [0, 0, 6, 6, 5, 4, 4, 4, 4, 4, 5, 6, 6, 0, 0],
  43.     # [0, 0, 0, 6, 6, 5, 5, 5, 5, 5, 6, 6, 0, 0, 0],
  44.     # [0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0],
  45.     # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]  
  46.     y,x = np.mgrid[0:SIZE,0:SIZE]
  47.     for l in l_list:
  48.         l_mask = np.logical_and(((x-c[1])**2 + (y-c[0])**2 >= (l - 0.5)**2),((x-c[1])**2 + (y-c[0])**2 < (l + 0.5)**2))  
  49.         l_masks[l] = l_mask
  50.         l_map[l_mask] = l
  51.         nl[l] = len(l_map[l_map==l])
  52.         cos_delta_phi_l[l] = 0
  53.    
  54.    
  55.     print "calculating dispersion..."
  56.     for i in range(SIZE):
  57.         print "i =", i
  58.         for j in range(SIZE):
  59.             '''
  60.            ## ROLL ARRAY
  61.            # move point to center of box
  62.            phi = np.roll(_phi, MAP_CENTER[0] - (py-i), axis=0)
  63.            phi = np.roll( phi, MAP_CENTER[1] - (px-j), axis=1)
  64.            
  65.            # cutout a box in the stokes maps centered at (py-i,px-j)
  66.            phi = phi[MAP_CENTER[0]-SIZE/2 : MAP_CENTER[0]+SIZE/2, MAP_CENTER[1]-SIZE/2 : MAP_CENTER[1]+SIZE/2]
  67.            '''
  68.             ## CUTOUT WITHOUT ROLLING ARRAY
  69.             phi = _phi[(py-i)-SIZE/2 : (py-i)+SIZE/2, (px-j)-SIZE/2 : (px-j)+SIZE/2]
  70.             phi_c = phi[c]     # phi_c = phi at the center of the cutout
  71.  
  72.             # calculate cos(\Delta\phi) for each pixel at each distance l
  73.             # sum all the cos(\Delta\phi) in a given l bin
  74.             for l in l_list:
  75.                 phi_l = phi[phi * l_masks[l] != 0]
  76.                 cos_delta_phi_l[l] += np.sum(np.cos(phi_c*np.ones(phi_l.shape) - phi_l))
  77.      
  78.     # calclate 1 - <cos(\Delta\phi>
  79.     # there are nl*SIZE*SIZE pixels in each l bin
  80.     for l in l_list:
  81.         dispersion_function.append(1 - 1./(nl[l]*SIZE*SIZE)*cos_delta_phi_l[l])
  82.    
  83.     plt.scatter(l_list, dispersion_function)
  84.     plt.xlabel("l")
  85.     plt.ylabel(r'1 - $<cos(\Delta\phi)>$')
  86.     plt.title("Dispersion Function")
  87.     plt.savefig("dispersion_%s_lmax-%d_point-%d-%d"%(axis, max_l, py, px))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement