Exemple #1
0
def a_centripetal2(model=None, s=None, tau=None, r=3, centre='rho', z=None):
	'''
	Computes centripetal acceleration from rotation around column s
	(s is given by the "snake" module).

	TODO: from 3D to 2D
	'''
	if not model: model=_model
	if z != None: to2d=True
	else: to2d=False
	if tau == None: tau=1.
	if s == None:
		tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
		tau1=int(round(np.mean(tau1_level)))
		s=snake.snake_from_box(model.z.rho,radius=r,start=tau1)
	if z == 'tau': z=tau1
	X, Y, Z = meshgrid(model)
	v2 = model.z.v1**2+model.z.v2**2+model.z.v3**2
	aX = np.zeros_like(v2)
	aY = np.zeros_like(v2)
	pint = np.array(s, dtype=int)
	for p0 in pint:
		if to2d and p0[2] != z: continue
		if centre == 'rho':
			pX, pY, pZ = model.z.xc1[p0[0],0,0], model.z.xc2[0,p0[1],0], model.z.xc3[0,0,p0[2]]
		else:
			pX, pY, pZ = model.z.xc1[centre[0],0,0], model.z.xc2[0,centre[1],0], model.z.xc3[0,0,p0[2]]
		dX, dY = X[:,:,p0[2]]-pX, Y[:,:,p0[2]]-pY
		r2 = dX**2+dY**2
		if centre == 'rho': r2[p0[0],p0[1]] = 1.e-10
		else: r2[centre[0],centre[1]] = 1.e-10
		aX[:,:,p0[2]] = -v2[:,:,p0[2]]/r2*dX
		aY[:,:,p0[2]] = -v2[:,:,p0[2]]/r2*dY
	if to2d: return aX[:,:,z], aY[:,:,z]
	else: return aX, aY
Exemple #2
0
def advection2(vec, model=None, dq=None, tau=None, z=None):
	'''
	Computes advection of vec.
	'''
	if not model: model=_model
	if dq == None: dq=bool(_parfile)
	if dq:
		tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
		tau1=int(round(np.mean(tau1_level)))
	else: tau1=int(np.size(model.z.xc3)/2.)
	dim = len(vec)
	if dim == 3 and tau != None: to2d=True
	else: to2d=False
	if tau == None: tau=1.
	if to2d:
		v1,v2,v3 = model.z.v1[:,:,tau1],model.z.v2[:,:,tau1],model.z.v3[:,:,tau1]
	else:
		v1,v2,v3 = model.z.v1,model.z.v2,model.z.v3
	dvXdX,dvXdY,dvXdZ = gradient(vec[0], model, dq, tau, z)
	dvYdX,dvYdY,dvYdZ = gradient(vec[1], model, dq, tau, z)
	if dim == 3:
		dvZdX,dvZdY,dvZdZ = gradient(vec[2], model, dq, tau, z)
	else:
		dvZdX = 0.
		dvZdY = 0.
		dvZdZ = 0.
	advX = v1*dvXdX+v2*dvXdY+v3*dvXdZ
	advY = v1*dvYdX+v2*dvYdY+v3*dvYdZ
	advZ = v1*dvZdX+v2*dvZdY+v3*dvZdZ
	return (advX, advY, advZ)
Exemple #3
0
def a_breaking_ratio(model=None, s=None, dq=None, odb=None, tau=1., z=None, r=3, full_advection=False, only_boundary=True, centre='rho', advect=False):
	'''
	Computes the "breaking ratio" of gradient p to centripetal
	acceleration: (a_g-a_c)/a_c.

	For an ideal cylindrically symetric field rotating around a
	given centre, this ratio is zero.

	If full_advection is True, replaces a_c by advection term.
	'''
	if not model: model=_model
	if dq == None: dq=bool(_parfile)
	if dq:
		tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
		tau1=int(round(np.mean(tau1_level)))
		tau1min=int(round(np.min(tau1_level)))
	else: tau1=int(np.size(model.z.xc3)/2.)
	if s == None:
		s=snake.snake_from_box(model.z.rho,radius=r,start=tau1)
	if z == None or z == 'tau': pass
	if z == 'mintau': tau1 = tau1min
	if z == 'halftau': tau1 = int(round(.5*(tau1min+tau1)))
	tau1_sindex = int(list(s[:,2]).index(tau1))
	xmean = int(s[tau1_sindex,0])
	ymean = int(s[tau1_sindex,1])
	if not odb: odb=snake.select_disk(model.z.rho[:,:,tau1],(xmean,ymean))
	o, d, b = odb
	a_g = a_gradient((xmean,ymean), model,tau1)
	if not advect:
		a_c = a_centripetal((xmean,ymean),model,tau1)
		#a_c = (a_c[0][:,:,tau1], a_c[1][:,:,tau1])
		a_c = np.sqrt(a_c[0]**2+a_c[1]**2)
		#a_g = (a_g[0][:,:,tau1], a_g[1][:,:,tau1])
	else:
		a_c1 = advection(model.z.v1,model,tau1)
		a_c2 = advection(model.z.v1,model,tau1)
		a_c3 = advection(model.z.v1,model,tau1)
		a_c = np.sqrt(a_c1**2+a_c2**2+a_c3**2)
		#a_g = (a_g[0][:,:,tau1], a_g[1][:,:,tau1], a_g[2][:,:,tau1])
	a_g = np.sqrt(a_g[0]**2+a_g[1]**2)
	if only_boundary:
		return np.sqrt(np.mean(((a_g[d]-a_c[d])/a_c[d])**2)),np.max(np.abs((a_g[d]-a_c[d])/a_c[d]))
	else:
		o_star = np.zeros_like(a_c,dtype=bool)
		o_star[o] = True
		o_star[xmean,ymean] = False
		o_star=np.where(o_star)
		return np.sqrt(np.mean(((a_g[o_star]-a_c[o_star])/np.mean(a_c[o_star]))**2)),np.max(np.abs((a_g[o_star]-a_c[o_star])/np.mean(a_c[o_star])))
Exemple #4
0
def wilson_depression(model=None,s=None,odb=None,tau=1.,r=3):
	'''
	Computes the Wilson depression.
	'''
	if not model: model=_model
	tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
	tau1=int(round(np.mean(tau1_level)))
	if s is None: s=snake.snake_from_box(model.z.rho,radius=r,start=tau1)
	tau1_sindex = int(list(s[:,2]).index(tau1))
	xmean = int(s[tau1_sindex,0])
	ymean = int(s[tau1_sindex,1])
	if not odb: o,d,b=snake.select_disk(model.z.rho[:,:,tau1],(xmean,ymean))
	else: o,d,b=odb
	min_tau1=int(np.min(tau1_level[o]))
	extended_b = np.ones_like(model.z.rho[:,:,0],dtype=bool)
	extended_b[o] = False
	extended_b = np.where(extended_b)
	mean_tau1=int(round(np.mean(tau1_level[extended_b])))
	return model.z.xc3[0,0,mean_tau1]-model.z.xc3[0,0,min_tau1]
Exemple #5
0
def contrast(arr,model=None,s=None,odb=None,tau=1.,r=3):
	'''
	Computes local and global contrast of arr; (arr_i-<arr>)/<arr>.
	<arr> is taken in the local neighbourhood and on the whole z=cst
        slice, at <tau>=tau (=1 by default).
	'''
	if not model: model=_model
	tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
	tau1=int(round(np.mean(tau1_level)))
	if s is None: s=snake.snake_from_box(model.z.rho,radius=r,start=tau1)
	tau1_sindex = int(list(s[:,2]).index(tau1))
	xmean = int(s[tau1_sindex,0])
	ymean = int(s[tau1_sindex,1])
	if not odb: o,d,b=snake.select_disk(model.z.rho[:,:,tau1],(xmean,ymean))
	else: o,d,b=odb
	arr_mean=np.mean(arr[:,:,tau1])
	arr_bmean=np.mean(arr[:,:,tau1][b])
	contrast_global = (arr[xmean,ymean,tau1]-arr_mean)/arr_mean
	contrast_local = (arr[xmean,ymean,tau1]-arr_bmean)/arr_bmean
	return contrast_global, contrast_local
Exemple #6
0
def report(ireport=None, model=None, s=None, dq=None, odb=None, tau=1., z=None, r=3, savefig=False, show=True):
	if not model: model=_model
	if dq == None: dq=bool(_parfile)
	if dq:
		tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
		tau1=int(round(np.mean(tau1_level)))
		tau1min=int(round(np.min(tau1_level)))
	else: tau1=int(np.size(model.z.xc3)/2.)
	if s == None: s=snake.snake_from_box(model.z.rho,radius=r,start=tau1)
	if z == None or z == 'tau': pass
	if z == 'mintau': tau1 = tau1min
	if z == 'halftau': tau1 = int(round(.5*(tau1min+tau1)))
	tau1_sindex = int(list(s[:,2]).index(tau1))
	xmean = int(s[tau1_sindex,0])
	ymean = int(s[tau1_sindex,1])
	print(str(xmean)+', '+str(ymean))
	print(str(np.unravel_index(np.argmin(model.z.v1[46:56,39:50,tau1]**2+model.z.v2[46:56,39:50,tau1]**2),(10,11))))
	if not odb: odb=snake.select_disk(model.z.rho[:,:,tau1],(xmean,ymean),threshold=.5)
	rot_index = np.argmin(model.z.v1[:,:,tau1][odb[0]]**2)
	xrot,yrot = odb[0][0][rot_index], odb[0][1][rot_index]
	print(str(xrot)+', '+str(yrot))
	#
	#if not odb: odb=snake.select_disk(tau1_level,(xmean,ymean),mask_flag=False,threshold=.8)
	plt.imshow(model.z.rho[:,:,tau1],origin='bottom')
	oring=np.zeros_like(model.z.rho[:,:,tau1],dtype=bool)
	oring[odb[0]]=True
	plt.imshow(oring,origin='bottom',alpha=.7,interpolation='none')
	plt.quiver(model.z.v2[:,:,tau1],model.z.v1[:,:,tau1])
	#odb2=snake.select_disk(model.z.rho[:,:,tau1],(xmean,ymean),threshold=.8)
	#plt.figure()
	#plt.imshow(model.z.rho[:,:,tau1],origin='bottom')
	#oring=np.zeros_like(model.z.rho[:,:,tau1],dtype=bool)
	#oring[odb2[0]]=True
	#plt.imshow(oring,origin='bottom',alpha=.7,interpolation='none')
	#plt.quiver(model.z.v2[:,:,tau1],model.z.v1[:,:,tau1])
	#plt.figure()
	#plt.imshow(model.z.v2[:,:,tau1]**2+model.z.v1[:,:,tau1]**2,origin='bottom',interpolation='none',cmap=cm.gray)
	#
	fmt='{0: <20}'
	if _modelfile: print(fmt.format('File:')+_modelfile)
	else: print(fmt.format('File:')+'Unknown')
	wd=wilson_depression(model,s,odb,tau,r)
	print(fmt.format('Wilson depression:')+str(wd))
	#c_rho=contrast(model.z.rho,model,s,odb,tau,r)
	c_rho=contrastf(model.z.rho[:,:,tau1],odb,np.min)
	print(fmt.format('Density contrast:')+str(c_rho))
	#c_p=contrast(model.dq.P,model,s,odb,tau,r)
	c_p=contrastf(model.dq.P[:,:,tau1],odb,np.min)
	print(fmt.format('Pressure contrast:')+str(c_p))
	a_ratio=a_breaking_ratio(model,s,dq,odb,tau,z,r,centre='rho')
	print(fmt.format('Acceleration ratio:')+str(a_ratio))
	adv_ratio=a_breaking_ratio(model,s,dq,odb,tau,z,r,centre='rho',advect=True)
	print(fmt.format('Advection ratio::')+str(adv_ratio))
	if _modelfile:
		title = _modelfile.split('.')
		title = '.'.join(title[:max(1,len(title)-1)])
	if ireport and _modelfile:
		id_name = title.split('nMBP')[-1]
		with open(ireport) as f:
			lines = f.readlines()
		names = [l.split('\t')[0] for l in lines]
		lines = [[f.strip() for f in l.split('\t')] for l in lines[1:]]
		idx = int(names.index(id_name))+1
		diam = float(lines[idx][1])
		c_I = (float(lines[idx][2]),float(lines[idx][3]))
		time = float(lines[idx][4])
		print(fmt.format('Diametre:')+str(diam))
		print(fmt.format('Intensity contrast:')+str(c_I))
		print(fmt.format('Lifetime:')+str(time))
	boxtext = 'Wilson depression: '+str(int(round(wd/1.e5)))+' [km]\n'
	boxtext+= 'Density contrast: '+str(int(round(100*c_rho[0])))+'%, '
	boxtext+= str(int(round(100*c_rho[1])))+'%\n'
	boxtext+= 'Pressure contrast: '+str(int(round(100*c_p[0])))+'%, '
	boxtext+= str(int(round(100*c_p[1])))+'%'
	#boxtext+= 'Acceleration ratio: '+str(int(round(100*a_ratio[0])))+'%, '
	#boxtext+= str(int(round(100*a_ratio[1])))+'%'
	if ireport and _modelfile:
		delta = model.z.xc1[1,:,:]-model.z.xc1[0,:,:]
		boxtext+= '\nDiametre: '+str(int(round(diam*delta/1.e5)))+' [km]'
		boxtext+= '\nIntensity contrast: '+str(int(round(100*c_I[0])))+'%, '
		boxtext+= str(int(round(100*c_I[1])))+'%'
		boxtext+= '\nLifetime: '+str(int(round(time)))+' [s]'
	if savefig:
		sY,sX=plotv_slice(model.z.rho,model,s,dq,tau,r,boxtext,show=False)
		if savefig == True: plt.savefig(title+'.png')
		else: plt.savefig(savefig)
		if show:
			plt.show()
		plt.close()
	else:
		sY,sX=plotv_slice(model.z.rho,model,s,dq,tau,r,boxtext,show=show)
		plt.close()
Exemple #7
0
def plotv_slice(arr,model=None,s=None,dq=None,tau=1.,r=3,boxtext=None,show=True,rf=10,tight=True):
	'''
	Plots vertical slices of arr at y=cst and x=cst respectively.
	Depending on optional parameters, also plots tau=tau (=1 by
	default) isosurface. Marks in red the "eye" of the nMBP.
	'''
	if not model: model=_model
	if dq == None: dq=bool(_parfile)
	if dq:
		tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
		tau1=int(round(np.mean(tau1_level)))
	else: tau1=int(np.size(model.z.xc3)/2.)
	if s is None: s=snake.snake_from_box(model.z.rho,radius=r,start=tau1)
	sx, sy, sz = np.shape(arr)
	x, y, z = np.array(s, dtype=int).T
	xc, yc, zc = model.z.xc1[:,0,0]/1.e5, model.z.xc2[0,:,0]/1.e5, model.z.xc3[0,0,:]/1.e5
	if dq:
		tau1_sindex = int(list(s[:,2]).index(tau1))
		xmean = int(s[tau1_sindex,0])
		ymean = int(s[tau1_sindex,1])
	else:
		xmean = int(round(np.mean(x)))
		ymean = int(round(np.mean(y)))
		tau1 = 0
	sharey = False
	if tight: z0 = z
	else:
		if dq:
			xp,yp,z1p,z2p = xc, yc, zc[tau1_level[:,ymean]]-zc[tau1],zc[tau1_level[xmean,:]]-zc[tau1]
			z0min = z[0]
			z0max = z[-1]+1
			z0min = min(z0min,np.min(tau1_level[:,ymean]))
			z0max = max(z0max,1+np.max(tau1_level[:,ymean]))
			z0min = min(z0min,np.min(tau1_level[xmean,:]))
			z0max = max(z0max,1+np.max(tau1_level[ymean,:]))
			delta = z0max - z0min
			z0min = max(0, int(z0min-.1*delta))
			z0max = min(sz, int(z0max+.1*delta))
			z0 = np.arange(z0min, z0max)
			sharey=True
		else:
			z0 = range(sz)
	sliceY=arr[np.ix_(range(sx),[ymean],z0)][:,0,:].T
	sliceX=arr[np.ix_([xmean],range(sy),z0)][0,:,:].T
	vY=model.z.v2[np.ix_(range(sx),[ymean],z0)][:,0,:].T
	vX=model.z.v1[np.ix_([xmean],range(sy),z0)][0,:,:].T
	if rf > 1:	# Refine factor
		vY = refine(vY,8)
		vX = refine(vX,8)
	vY=(vY>=0.)
	vX=(vX>=0.)
	extY=np.array([xc[0],xc[sx-1],zc[z0[0]]-zc[tau1],zc[z0[-1]]-zc[tau1]])
	extX=np.array([yc[0],yc[sy-1],zc[z0[0]]-zc[tau1],zc[z0[-1]]-zc[tau1]])
	f, (ax1, ax2) = plt.subplots(1, 2, figsize=(16,5), sharey=sharey)
	ax1.set_aspect('equal')
	ax2.set_aspect('equal')
	plt.tight_layout(pad=4)
	if _modelfile:
		title = _modelfile.split('.')
		title = '.'.join(title[:max(1,len(title)-1)])
		plt.suptitle(title,fontsize=16)
	vmin = min(np.min(sliceY), np.min(sliceX))
	vmax = max(np.max(sliceY), np.max(sliceX))
	im1 = ax1.imshow(sliceY, origin='bottom',alpha=1.,cmap=None, extent=extY, vmin=vmin, vmax=vmax)
	im2 = ax2.imshow(sliceX, origin='bottom',alpha=1.,cmap=None, extent=extX, vmin=vmin, vmax=vmax)
	f.subplots_adjust(right=0.8)
	cbar_ax = f.add_axes([0.83, 0.15, 0.02, 0.69])
	cbar = f.colorbar(im2, cax=cbar_ax)
	cbar.set_label('Density [g$\,$cm$^{-3}$]', rotation=270, labelpad=20)
	ax1.imshow(-vY, origin='bottom',alpha=.4,cmap=cm.gray, extent=extY)
	ax2.imshow(vX, origin='bottom',alpha=.4,cmap=cm.gray, extent=extX)
	xp,yp,zp = xc[x],yc[y],zc[z]-zc[tau1]
	if rf > 1:
		xp,yp,zp = refine(xp,rf), refine(yp,rf), refine(zp,rf)
		xp = savitzky_golay(xp, rf**2+1, 3)
		yp = savitzky_golay(yp, rf**2+1, 3)
		zp = savitzky_golay(zp, rf**2+1, 3)
	ax1.plot(xp,zp,'r')
	ax2.plot(yp,zp,'r')
	#ax1.plot(xc[x],zc[z]-zc[tau1],'r')
	#ax2.plot(yc[y],zc[z]-zc[tau1],'r')
	if dq:
		xp,yp,z1p,z2p = xc, yc, zc[tau1_level[:,ymean]]-zc[tau1],zc[tau1_level[xmean,:]]-zc[tau1]
		if rf > 1:
			xp,yp,z1p,z2p = refine(xp,rf), refine(yp,rf), refine(z1p,rf), refine(z2p,rf)
			xp = savitzky_golay(xp, rf*(rf-1)+1, 3)
			yp = savitzky_golay(yp, rf*(rf-1)+1, 3)
			z1p = savitzky_golay(z1p, rf*(rf-1)+1, 3)
			z2p = savitzky_golay(z2p, rf*(rf-1)+1, 3)
		ax1.plot(xp,z1p,'b')
		ax2.plot(yp,z2p,'b')
	#	ax1.plot(xc,zc[tau1_level[:,ymean]]-zc[tau1],'b')
	#	ax2.plot(yc,zc[tau1_level[xmean,:]]-zc[tau1],'b')
	ax1.set_xlim((extY[0],extY[1]))
	ax1.set_ylim((extY[2],extY[3]))
	ax2.set_xlim((extX[0],extX[1]))
	ax2.set_ylim((extX[2],extX[3]))
	ax1.set_xlabel("Spatial horizontal position (X axis) [km]")
	ax2.set_xlabel("Spatial horizontal position (Y axis) [km]")
	ax1.set_ylabel("Height [km]")
	if boxtext:
		props = dict(boxstyle='round, pad=.7, rounding_size=.2', facecolor='white', edgecolor='black', alpha=.8)
		ax1.text(0.05, 0.9, boxtext, size='smaller', ha='left', va='top', transform=ax1.transAxes, bbox=props)
	if show: plt.show()
	return sliceY, sliceX
Exemple #8
0
def gradient2(arr, model=None, dq=None, tau=None, z=None):
	'''
	Computes gradient of arr from a nMBP.
	'''
	if not model: model=_model
	dim = len(np.shape(arr))
	if dim == 3 and tau != None: to2d=True
	else: to2d=False
	if tau == None: tau=1.
	if dq == None: dq=bool(_parfile)
	if dq:
		tau1_level=np.array(pybold.level(model.dq.tau,tau),dtype=int)
		tau1=int(round(np.mean(tau1_level)))
	else: tau1=int(np.size(model.z.xc3)/2.)
	if z != None: tau1=z
	if to2d:
		gradX = np.empty_like(arr[:,:,tau1])
		gradY = np.empty_like(arr[:,:,tau1])
		if dim == 3:
			gradZ = np.empty_like(arr[:,:,tau1])
	else:
		gradX = np.empty_like(arr)
		gradY = np.empty_like(arr)
		if dim == 3:
			gradZ = np.empty_like(arr)
	[[m1,m2,m3],[n1,n2,n3]] = model.z.dimension
	X, Y, Z = meshgrid(model)
	#if dim == 3: X, Y, Z = meshgrid(model)
	#else: X, Y = meshgrid2D(model)
	if to2d:
		gradX[1:-1,:] = (arr[2:,:,tau1]-arr[:-2,:,tau1])/(X[2:,:,tau1]-X[:-2,:,tau1])
		gradY[:,1:-1] = (arr[:,2:,tau1]-arr[:,:-2,tau1])/(Y[:,2:,tau1]-Y[:,:-2,tau1])
		if tau1>0 and tau1<n3-m3:
			gradZ[:,:] = (arr[:,:,tau1+1]-arr[:,:,tau1-1])/(Z[:,:,tau1+1]-Z[:,:,tau1-1])
		elif tau1==0:
			gradZ[:,:] = arr[:,:,1]+(arr[:,:,1]-arr[:,:,2])/(Z[:,:,1]-Z[:,:,2])*(Z[:,:,0]-Z[:,:,1])
		elif tau1==n3-m3:
			gradZ[:,:] = arr[:,:,n3-m3-1]+(arr[:,:,n3-m3-1]-arr[:,:,n3-m3-2])/(Z[:,:,n3-m3-1]-Z[:,:,n3-m3-2])*(Z[:,:,n3-m3]-Z[:,:,n3-m3-1])
		gradX[0,:] = arr[1,:,tau1]+(arr[1,:,tau1]-arr[2,:,tau1])/(X[1,:,tau1]-X[2,:,tau1])*(X[0,:,tau1]-X[1,:,tau1])
		gradX[-1,:] = arr[-2,:,tau1]+(arr[-2,:,tau1]-arr[-3,:,tau1])/(X[-2,:,tau1]-X[-3,:,tau1])*(X[-1,:,tau1]-X[-2,:,tau1])
		gradY[:,0] = arr[:,1,tau1]+(arr[:,1,tau1]-arr[:,2,tau1])/(Y[:,1,tau1]-Y[:,2,tau1])*(Y[:,0,tau1]-Y[:,1,tau1])
		gradY[:,-1] = arr[:,-2,tau1]+(arr[:,-2,tau1]-arr[:,-3,tau1])/(Y[:,-2,tau1]-Y[:,-3,tau1])*(Y[:,-1,tau1]-Y[:,-2,tau1])
		return (gradX, gradY, gradZ)
	elif dim == 3:
		gradX[1:-1,:,:] = (arr[2:,:,:]-arr[:-2,:,:])/(X[2:,:,:]-X[:-2,:,:])
		gradY[:,1:-1,:] = (arr[:,2:,:]-arr[:,:-2,:])/(Y[:,2:,:]-Y[:,:-2,:])
		gradZ[:,:,1:-1] = (arr[:,:,2:]-arr[:,:,:-2])/(Z[:,:,2:]-Z[:,:,:-2])
		gradX[0,:,:] = gradX[1,:,:]+(gradX[1,:,:]-gradX[2,:,:])/(X[1,:,:]-X[2,:,:])*(X[0,:,:]-X[1,:,:])
		gradX[-1,:,:] = gradX[-2,:,:]+(gradX[-2,:,:]-gradX[-3,:,:])/(X[-2,:,:]-X[-3,:,:])*(X[-1,:,:]-X[-2,:,:])
		gradY[:,0,:] = gradY[:,1,:]+(gradY[:,1,:]-gradY[:,2,:])/(Y[:,1,:]-Y[:,2,:])*(Y[:,0,:]-Y[:,1,:])
		gradY[:,-1,:] = gradY[:,-2,:]+(gradY[:,-2,:]-gradY[:,-3,:])/(Y[:,-2,:]-Y[:,-3,:])*(Y[:,-1,:]-Y[:,-2,:])
		gradZ[:,:,0] = gradZ[:,:,1]+(gradZ[:,:,1]-gradZ[:,:,2])/(Z[:,:,1]-Z[:,:,2])*(Z[:,:,0]-Z[:,:,1])
		gradZ[:,:,-1] = gradZ[:,:,-2]+(gradZ[:,:,-2]-gradZ[:,:,-3])/(Z[:,:,-2]-Z[:,:,-3])*(Z[:,:,-1]-Z[:,:,-2])
		return (gradX, gradY, gradZ)
	else:
		gradX[1:-1,:] = (arr[2:,:]-arr[:-2,:])/(X[2:,:]-X[:-2,:])
		gradY[:,1:-1] = (arr[:,2:]-arr[:,:-2])/(Y[:,2:]-Y[:,:-2])
		gradX[0,:] = gradX[1,:]+(gradX[1,:]-gradX[2,:])/(X[1,:]-X[2,:])*(X[0,:]-X[1,:])
		gradX[-1,:] = gradX[-2,:]+(gradX[-2,:]-gradX[-3,:])/(X[-2,:]-X[-3,:])*(X[-1,:]-X[-2,:])
		gradY[:,0] = gradY[:,1]+(gradY[:,1]-gradY[:,2])/(Y[:,1]-Y[:,2])*(Y[:,0]-Y[:,1])
		gradY[:,-1] = gradY[:,-2]+(gradY[:,-2]-gradY[:,-3])/(Y[:,-2]-Y[:,-3])*(Y[:,-1]-Y[:,-2])
		return (gradX, gradY)
Exemple #9
0
def report():
    '''
	Prints a report of the current nMBP together with the corresponding
	plots.
	'''
    global _model, _parameters, _rad
    model = _model
    tau = 1.
    r = 3
    tau1_level = np.array(pybold.level(model.dq.tau, tau), dtype=int)
    tau1 = int(round(np.mean(tau1_level)))
    tau1min = int(round(np.min(tau1_level)))
    seed = [
        _parameters[0][0] - model.z.dimension[0][0],
        _parameters[0][1] - model.z.dimension[0][1], tau1
    ]
    s = snake.snake_from_box(model.z.rho,
                             radius=r,
                             start=tau1,
                             periodic=False,
                             seed=seed)
    tau1_sindex = int(list(s[:, 2]).index(tau1))
    xmean = int(s[tau1_sindex, 0])
    ymean = int(s[tau1_sindex, 1])
    odb = snake.select_disk(model.z.rho[:, :, tau1], (xmean, ymean),
                            threshold=.5)
    rot_index = np.argmin(model.z.v1[:, :, tau1][odb[0]]**2)
    xrot, yrot = odb[0][0][rot_index], odb[0][1][rot_index]
    xc1, xc2 = model.z.xc1.flatten() / 1.e5, model.z.xc2.flatten() / 1.e5
    xb1, xb2 = model.z.xb1.flatten() / 1.e5, model.z.xb2.flatten() / 1.e5
    ext = [xc1[0], xc1[-1], xc2[0], xc2[-1]]
    #Ttau1 = pybold.varAtLevel(model.dq.T,model.dq.tau,1.)
    rhotau1 = pybold.varAtLevel(model.z.rho, model.dq.tau, 1.)
    #plt.imshow(model.z.rho[:,:,tau1].T,origin='bottom',cmap=plt.cm.gray,extent=ext)
    plt.imshow(rhotau1.T, origin='bottom', extent=ext)
    plt.xlim(xc1[0], xc1[-1])
    plt.ylim(xc2[0], xc2[-1])
    plt.xlabel('Spatial horizontal position (X axis) [km]')
    plt.ylabel('Spatial horizontal position (Y axis) [km]')
    oring = np.zeros_like(model.z.rho[:, :, tau1], dtype=bool)
    oring[odb[0]] = True
    plt.imshow(oring.T,
               origin='bottom',
               alpha=.15,
               interpolation='none',
               extent=ext,
               cmap=plt.cm.gray_r)
    X, Y = np.meshgrid(xc1, xc2)
    arrfreq = 5
    v1 = pybold.varAtLevel(model.z.v1, model.dq.tau, 1.)
    v2 = pybold.varAtLevel(model.z.v2, model.dq.tau, 1.)
    #plt.quiver(X[::arrfreq,::arrfreq], Y[::arrfreq,::arrfreq], model.z.v1[::arrfreq,::arrfreq,tau1].T,model.z.v2[::arrfreq,::arrfreq,tau1].T)
    plt.quiver(X[::arrfreq, ::arrfreq], Y[::arrfreq, ::arrfreq],
               v1[::arrfreq, ::arrfreq].T, v2[::arrfreq, ::arrfreq].T)
    fmt = '{0: <20}'
    if _modelfile: print(fmt.format('File:') + _modelfile)
    else: print(fmt.format('File:') + 'Unknown')
    wd = wilson_depression(model, s, odb, tau, r)
    print(fmt.format('Wilson depression:') + str(wd))
    c_rho = contrast(model.z.rho, model, s, odb, tau, r)
    print(fmt.format('Density contrast:') + str(c_rho))
    c_p = contrast(model.dq.P, model, s, odb, tau, r)
    print(fmt.format('Pressure contrast:') + str(c_p))
    if _modelfile:
        title = _modelfile.split('.')
        title = '.'.join(title[:max(1, len(title) - 1)])
    boxtext = 'Wilson depression: ' + str(int(round(wd / 1.e5))) + ' [km]\n'
    boxtext += 'Density contrast: ' + str(int(round(100 * c_rho[1]))) + '%, '
    boxtext += str(int(round(100 * c_rho[0]))) + '%\n'
    boxtext += 'Pressure contrast: ' + str(int(round(100 * c_p[1]))) + '%, '
    boxtext += str(int(round(100 * c_p[0]))) + '%'
    diam = _parameters[3]
    delta = model.z.xc1[1, :, :] - model.z.xc1[0, :, :]
    boxtext += '\nDiametre: ' + str(int(round(diam * delta / 1.e5))) + ' [km]'
    c_I = (_parameters[1], _parameters[2])
    boxtext += '\nIntensity contrast: ' + str(int(round(100 * c_I[0]))) + '%, '
    boxtext += str(int(round(100 * c_I[1]))) + '%'
    boxtext += '\nLifetime: ' + str(int(round(120.))) + ' [s]'
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    deltaX = xb1[1] - xb1[0]
    deltaY = xb2[1] - xb2[0]
    xlim_min = xc1[0] - model.z.dimension[0][0] * deltaX
    ylim_min = xc2[0] - model.z.dimension[0][1] * deltaY
    szX, szY = np.shape(_rad.T)
    ext = [
        xlim_min, xlim_min + szX * deltaX, ylim_min, ylim_min + szY * deltaY
    ]
    ax.imshow(_rad.T, origin='bottom', cmap=plt.cm.gray, extent=ext)
    ax.set_xlim(xlim_min, xlim_min + szX * deltaX)
    ax.set_ylim(ylim_min, ylim_min + szY * deltaY)
    ax.set_xlabel('Spatial horizontal position (X axis) [km]')
    ax.set_ylabel('Spatial horizontal position (Y axis) [km]')
    c1_idx = _parameters[0][0] - model.z.dimension[0][0]
    c2_idx = _parameters[0][1] - model.z.dimension[0][1]
    centre = [xc1[c1_idx], xc2[c2_idx]]
    c_r = 50. * np.sqrt(deltaX**2 + deltaY**2) / np.sqrt(2.)
    circ = plt.Circle(centre, radius=c_r, color='r', fill=False)
    ax.add_patch(circ)
    sY, sX = plotv_slice(model.z.rho,
                         model,
                         s,
                         dq=True,
                         tau=tau,
                         r=r,
                         boxtext=boxtext,
                         show=True,
                         tight=False)
    plt.close()