Beispiel #1
0
	def plot_depth_slice(self, component, depth, valmin, valmax, iteration=0, verbose=True, stations=True, res="i"):
		"""
		plot_depth_slice(self, component, depth, valmin, valmax, iteration=0, verbose=True, stations=True, res="i")

		Plot depth slices of field component at depth "depth" with colourbar ranging between "valmin" and "valmax".
		The resolution of the coastline is "res" (c, l, i, h, f).

		The currently available "components" are:
			Material parameters: A, B, C, mu, lambda, rhoinv, vp, vsh, vsv, rho
			Velocity field snapshots: vx, vy, vz
			Sensitivity kernels: Q_mu, Q_kappa, alpha_mu, alpha_kappa
		"""


		#- Some initialisations. ------------------------------------------------------------------
		n_procs = self.setup["procs"]["px"] * self.setup["procs"]["py"] * self.setup["procs"]["pz"]
		radius = 1000.0 * (6371.0 - depth)

		vmax = float("-inf")
		vmin = float("inf")

		lat_min = 90.0 - self.setup["domain"]["theta_max"]*180.0/np.pi
		lat_max = 90.0 - self.setup["domain"]["theta_min"]*180.0/np.pi
		lon_min = self.setup["domain"]["phi_min"]*180.0/np.pi
		lon_max = self.setup["domain"]["phi_max"]*180.0/np.pi

		lat_centre = (lat_max+lat_min)/2.0
		lon_centre = (lon_max+lon_min)/2.0
		lat_centre,lon_centre = rot.rotate_coordinates(self.n,-self.rotangle,90.0-lat_centre,lon_centre)
		lat_centre = 90.0-lat_centre

		d_lon = np.round((lon_max-lon_min)/10.0)
		d_lat = np.round((lat_max-lat_min)/10.0)

		#- Set up the map. ------------------------------------------------------------------------
		if (lat_max-lat_min) > 30.0:
			m = Basemap(projection='ortho', lon_0=lon_centre, lat_0=lat_centre, resolution=res)
			m.drawparallels(np.arange(-80.0,80.0,10.0),labels=[1,0,0,1])
			m.drawmeridians(np.arange(-170.0,170.0,10.0),labels=[1,0,0,1])	
		else:
			m=Basemap(projection='merc',llcrnrlat=lat_min,urcrnrlat=lat_max,llcrnrlon=lon_min,urcrnrlon=lon_max,lat_ts=20,resolution=res)
			m.drawparallels(np.arange(np.round(lat_min),np.round(lat_max),d_lat),labels=[1,0,0,1])
			m.drawmeridians(np.arange(np.round(lon_min),np.round(lon_max),d_lon),labels=[1,0,0,1])

		m.drawcoastlines()
		m.fillcontinents("0.9", zorder=0)
		m.drawmapboundary(fill_color="white")
		m.drawcountries()

		#- Loop over processor boxes and check if depth falls within the volume. ------------------
		for p in range(n_procs):

			if (radius >= self.z[p,:].min()) & (radius <= self.z[p,:].max()):

				#- Read this field and make lats & lons. ------------------------------------------
				field = self.read_single_box(component,p,iteration)
				lats = 90.0 - self.theta[p,:] * 180.0 / np.pi
				lons = self.phi[p,:] * 180.0 / np.pi
				lon, lat = np.meshgrid(lons, lats)

				#- Find the depth index and plot for this one box. --------------------------------
				idz=min(np.where(min(np.abs(self.z[p,:]-radius))==np.abs(self.z[p,:]-radius))[0])

				r_effective = int(self.z[p,idz]/1000.0)

				#- Find min and max values. -------------------------------------------------------

				vmax = max(vmax, field[:,:,idz].max())
				vmin = min(vmin, field[:,:,idz].min())

				#- Make lats and lons. ------------------------------------------------------------
				lats = 90.0 - self.theta[p,:] * 180.0 / np.pi
				lons = self.phi[p,:] * 180.0 / np.pi
				lon, lat = np.meshgrid(lons, lats)

				#- Rotate if necessary. -----------------------------------------------------------
				if self.rotangle != 0.0:

					lat_rot = np.zeros(np.shape(lon),dtype=float)
					lon_rot = np.zeros(np.shape(lat),dtype=float)

					for idlon in np.arange(len(lons)):
						for idlat in np.arange(len(lats)):

							lat_rot[idlat,idlon],lon_rot[idlat,idlon] = rot.rotate_coordinates(self.n,-self.rotangle,90.0-lat[idlat,idlon],lon[idlat,idlon])
							lat_rot[idlat,idlon] = 90.0-lat_rot[idlat,idlon]

					lon = lon_rot
					lat = lat_rot

				#- Make a nice colourmap. ---------------------------------------------------------
				my_colormap=cm.make_colormap({0.0:[0.1,0.0,0.0], 0.2:[0.8,0.0,0.0], 0.3:[1.0,0.7,0.0],0.48:[0.92,0.92,0.92], 0.5:[0.92,0.92,0.92], 0.52:[0.92,0.92,0.92], 0.7:[0.0,0.6,0.7], 0.8:[0.0,0.0,0.8], 1.0:[0.0,0.0,0.1]})

				x, y = m(lon, lat)
				im = m.pcolormesh(x, y, field[:,:,idz], cmap=my_colormap, vmin=valmin,vmax=valmax)

		#- Add colobar and title. ------------------------------------------------------------------
		cb = m.colorbar(im, "right", size="3%", pad='2%')
		if component in UNIT_DICT:
			cb.set_label(UNIT_DICT[component], fontsize="x-large", rotation=0)
	
		plt.suptitle("Depth slice of %s at %i km" % (component, r_effective), size="large")

		#- Plot stations if available. ------------------------------------------------------------
		if (self.stations == True) & (stations==True):

			x,y = m(self.stlons,self.stlats)

			for n in range(self.n_stations):
				plt.text(x[n],y[n],self.stnames[n][:4])
				plt.plot(x[n],y[n],'ro')


		plt.show()

		if verbose == True:
			print "minimum value: "+str(vmin)+", maximum value: "+str(vmax)
Beispiel #2
0
  def convert_to_vtk(self,directory,filename,verbose=False):
    """ convert ses3d model to vtk format for plotting with Paraview

    convert_to_vtk(self,directory,filename,verbose=False):
    """

    #- preparatory steps

    nx=np.zeros(self.nsubvol,dtype=int)
    ny=np.zeros(self.nsubvol,dtype=int)
    nz=np.zeros(self.nsubvol,dtype=int)
    N=0

    for n in np.arange(self.nsubvol):
      nx[n]=len(self.m[n].lat)
      ny[n]=len(self.m[n].lon)
      nz[n]=len(self.m[n].r)
      N=N+nx[n]*ny[n]*nz[n]

    #- open file and write header

    fid=open(directory+filename,'w')

    if verbose==True:
      print 'write to file '+directory+filename

    fid.write('# vtk DataFile Version 3.0\n')
    fid.write('vtk output\n')
    fid.write('ASCII\n')
    fid.write('DATASET UNSTRUCTURED_GRID\n')

    #- write grid points

    fid.write('POINTS '+str(N)+' float\n')

    for n in np.arange(self.nsubvol):

      if verbose==True:
	print 'writing grid points for subvolume '+str(n)

      for i in np.arange(nx[n]):
	for j in np.arange(ny[n]):
	  for k in np.arange(nz[n]):

	    theta=90.0-self.m[n].lat[i]
	    phi=self.m[n].lon[j]

	    #- rotate coordinate system

	    if self.phi!=0.0:
	      theta,phi=rot.rotate_coordinates(self.n,-self.phi,theta,phi)

	    #- transform to cartesian coordinates and write to file

	    theta=theta*np.pi/180.0
	    phi=phi*np.pi/180.0

	    r=self.m[n].r[k]
	    x=r*np.sin(theta)*np.cos(phi);
            y=r*np.sin(theta)*np.sin(phi);
            z=r*np.cos(theta);

	    fid.write(str(x)+' '+str(y)+' '+str(z)+'\n')

    #- write connectivity

    n_cells=0

    for n in np.arange(self.nsubvol):
      n_cells=n_cells+(nx[n]-1)*(ny[n]-1)*(nz[n]-1)

    fid.write('\n')
    fid.write('CELLS '+str(n_cells)+' '+str(9*n_cells)+'\n')

    count=0

    for n in np.arange(self.nsubvol):

      if verbose==True:
	print 'writing conectivity for subvolume '+str(n)

      for i in np.arange(1,nx[n]):
	for j in np.arange(1,ny[n]):
	  for k in np.arange(1,nz[n]):
								# i j k
	    a=count+k+(j-1)*nz[n]+(i-1)*ny[n]*nz[n]-1     	# 0 0 0
	    b=count+k+(j-1)*nz[n]+(i-1)*ny[n]*nz[n]       	# 0 0 1
	    c=count+k+(j)*nz[n]+(i-1)*ny[n]*nz[n]-1       	# 0 1 0
	    d=count+k+(j)*nz[n]+(i-1)*ny[n]*nz[n]         	# 0 1 1
	    e=count+k+(j-1)*nz[n]+(i)*ny[n]*nz[n]-1       	# 1 0 0
	    f=count+k+(j-1)*nz[n]+(i)*ny[n]*nz[n]         	# 1 0 1
	    g=count+k+(j)*nz[n]+(i)*ny[n]*nz[n]-1         	# 1 1 0
	    h=count+k+(j)*nz[n]+(i)*ny[n]*nz[n]           	# 1 1 1

	    fid.write('8 '+str(a)+' '+str(b)+' '+str(c)+' '+str(d)+' '+str(e)+' '+str(f)+' '+str(g)+' '+str(h)+'\n')

      count=count+nx[n]*ny[n]*nz[n]

    #- write cell types

    fid.write('\n')
    fid.write('CELL_TYPES '+str(n_cells)+'\n')

    for n in np.arange(self.nsubvol):

      if verbose==True:
	print 'writing cell types for subvolume '+str(n)

      for i in np.arange(nx[n]-1):
	for j in np.arange(ny[n]-1):
	  for k in np.arange(nz[n]-1):

	    fid.write('11\n')

    #- write data

    fid.write('\n')
    fid.write('POINT_DATA '+str(N)+'\n')
    fid.write('SCALARS scalars float\n')
    fid.write('LOOKUP_TABLE mytable\n')

    for n in np.arange(self.nsubvol):

      if verbose==True:
	print 'writing data for subvolume '+str(n)

      idx=np.arange(nx[n])
      idx[nx[n]-1]=nx[n]-2

      idy=np.arange(ny[n])
      idy[ny[n]-1]=ny[n]-2

      idz=np.arange(nz[n])
      idz[nz[n]-1]=nz[n]-2

      for i in idx:
	for j in idy:
	  for k in idz:

	    fid.write(str(self.m[n].v[i,j,k])+'\n')

    #- clean up

    fid.close()
Beispiel #3
0
  def plot_threshold(self,val,min_val_plot,max_val_plot,colormap='tomo',verbose=False):
    """ plot depth to a certain threshold value 'val' in an ses3d model

    plot_threshold(val,min_val_plot,max_val_plot,colormap='tomo',verbose=False):
    val=threshold value
    min_val_plot, max_val_plot=minimum and maximum values of the colour scale
    colormap='tomo','mono'
    """

    #- set up a map and colourmap

    if self.global_regional=='regional':
      m=Basemap(projection='merc',llcrnrlat=self.lat_min,urcrnrlat=self.lat_max,llcrnrlon=self.lon_min,urcrnrlon=self.lon_max,lat_ts=20,resolution=res)
      m.drawparallels(np.arange(self.lat_min,self.lat_max,self.d_lon),labels=[1,0,0,1])
      m.drawmeridians(np.arange(self.lon_min,self.lon_max,self.d_lat),labels=[1,0,0,1])
    elif self.global_regional=='global':
      m=Basemap(projection='ortho',lon_0=self.lon_centre,lat_0=self.lat_centre,resolution=res)
      m.drawparallels(np.arange(-80.0,80.0,10.0),labels=[1,0,0,1])
      m.drawmeridians(np.arange(-170.0,170.0,10.0),labels=[1,0,0,1])

    m.drawcoastlines()
    m.drawcountries()

    m.drawmapboundary(fill_color=[1.0,1.0,1.0])

    if colormap=='tomo':
      my_colormap=cm.make_colormap({0.0:[0.1,0.0,0.0], 0.2:[0.8,0.0,0.0], 0.3:[1.0,0.7,0.0],0.48:[0.92,0.92,0.92], 0.5:[0.92,0.92,0.92], 0.52:[0.92,0.92,0.92], 0.7:[0.0,0.6,0.7], 0.8:[0.0,0.0,0.8], 1.0:[0.0,0.0,0.1]})
    elif colormap=='mono':
      my_colormap=cm.make_colormap({0.0:[1.0,1.0,1.0], 0.15:[1.0,1.0,1.0], 0.85:[0.0,0.0,0.0], 1.0:[0.0,0.0,0.0]})

    #- loop over subvolumes

    for k in np.arange(self.nsubvol):

      depth=np.zeros(np.shape(self.m[k].v[:,:,0]))

      nx=len(self.m[k].lat)
      ny=len(self.m[k].lon)

      #- find depth

      r=self.m[k].r
      r=0.5*(r[0:len(r)-1]+r[1:len(r)])

      for idx in np.arange(nx-1):
	for idy in np.arange(ny-1):

	  n=self.m[k].v[idx,idy,:]>=val
	  depth[idx,idy]=6371.0-np.max(r[n])

      #- rotate coordinate system if necessary

      lon,lat=np.meshgrid(self.m[k].lon[0:ny],self.m[k].lat[0:nx])

      if self.phi!=0.0:

	lat_rot=np.zeros(np.shape(lon),dtype=float)
	lon_rot=np.zeros(np.shape(lat),dtype=float)

	for idx in np.arange(nx):
	  for idy in np.arange(ny):

	    colat=90.0-lat[idx,idy]

	    lat_rot[idx,idy],lon_rot[idx,idy]=rot.rotate_coordinates(self.n,-self.phi,colat,lon[idx,idy])
	    lat_rot[idx,idy]=90.0-lat_rot[idx,idy]

	lon=lon_rot
	lat=lat_rot

	#- convert to map coordinates and plot

      x,y=m(lon,lat)
      im=m.pcolor(x,y,depth,cmap=my_colormap,vmin=min_val_plot,vmax=max_val_plot)

    m.colorbar(im,"right", size="3%", pad='2%')
    plt.title('depth to '+str(val)+' km/s [km]')
    plt.show()
Beispiel #4
0
  def read(self,directory,filename,verbose=False):
    """ read an ses3d model from a file

    read(self,directory,filename,verbose=False):
    """

    #- read block files ====================================================

    fid_x=open(directory+'block_x','r')
    fid_y=open(directory+'block_y','r')
    fid_z=open(directory+'block_z','r')

    if verbose==True:
      print 'read block files:'
      print '\t '+directory+'block_x'
      print '\t '+directory+'block_y'
      print '\t '+directory+'block_z'

    dx=np.array(fid_x.read().strip().split('\n'),dtype=float)
    dy=np.array(fid_y.read().strip().split('\n'),dtype=float)
    dz=np.array(fid_z.read().strip().split('\n'),dtype=float)

    fid_x.close()
    fid_y.close()
    fid_z.close()

    #- read coordinate lines ===============================================

    self.nsubvol=int(dx[0])

    if verbose==True:
      print 'number of subvolumes: '+str(self.nsubvol)

    idx=np.zeros(self.nsubvol,dtype=int)+1
    idy=np.zeros(self.nsubvol,dtype=int)+1
    idz=np.zeros(self.nsubvol,dtype=int)+1

    for k in np.arange(1,self.nsubvol,dtype=int):
      idx[k]=int(dx[idx[k-1]])+idx[k-1]+1
      idy[k]=int(dy[idy[k-1]])+idy[k-1]+1
      idz[k]=int(dz[idz[k-1]])+idz[k-1]+1

    for k in np.arange(self.nsubvol,dtype=int):
      subvol=ses3d_submodel()
      subvol.lat=90.0-dx[(idx[k]+1):(idx[k]+1+dx[idx[k]])]
      subvol.lon=dy[(idy[k]+1):(idy[k]+1+dy[idy[k]])]
      subvol.r  =dz[(idz[k]+1):(idz[k]+1+dz[idz[k]])]
      self.m.append(subvol)

    #- compute rotated version of the coordinate lines ====================

    if self.phi!=0.0:

      for k in np.arange(self.nsubvol,dtype=int):

        nx=len(self.m[k].lat)
        ny=len(self.m[k].lon)

        self.m[k].lat_rot=np.zeros([nx,ny])
        self.m[k].lon_rot=np.zeros([nx,ny])

        for idx in np.arange(nx):
          for idy in np.arange(ny):

            self.m[k].lat_rot[idx,idy],self.m[k].lon_rot[idx,idy]=rot.rotate_coordinates(self.n,-self.phi,90.0-self.m[k].lat[idx],self.m[k].lon[idy])
            self.m[k].lat_rot[idx,idy]=90.0-self.m[k].lat_rot[idx,idy]

    else:

      self.m[k].lat_rot,self.m[k].lon_rot=np.meshgrid(self.m[k].lat,self.m[k].lon)

    #- read model volume ==================================================

    fid_m=open(directory+filename,'r')

    if verbose==True:
      print 'read model file: '+directory+filename

    v=np.array(fid_m.read().strip().split('\n'),dtype=float)

    fid_m.close()

    #- assign values ======================================================

    idx=1
    for k in np.arange(self.nsubvol):

      n=int(v[idx])
      nx=len(self.m[k].lat)-1
      ny=len(self.m[k].lon)-1
      nz=len(self.m[k].r)-1

      self.m[k].v=v[(idx+1):(idx+1+n)].reshape(nx,ny,nz)

      idx=idx+n+1

    #- decide on global or regional model==================================

    self.lat_min=90.0
    self.lat_max=-90.0
    self.lon_min=180.0
    self.lon_max=-180.0

    for k in np.arange(self.nsubvol):
      if np.min(self.m[k].lat_rot) < self.lat_min: self.lat_min = np.min(self.m[k].lat_rot)
      if np.max(self.m[k].lat_rot) > self.lat_max: self.lat_max = np.max(self.m[k].lat_rot)
      if np.min(self.m[k].lon_rot) < self.lon_min: self.lon_min = np.min(self.m[k].lon_rot)
      if np.max(self.m[k].lon_rot) > self.lon_max: self.lon_max = np.max(self.m[k].lon_rot)

    if ((self.lat_max-self.lat_min) > 90.0 or (self.lon_max-self.lon_min) > 90.0):
      self.global_regional = "global"

      self.lat_centre = (self.lat_max+self.lat_min)/2.0
      self.lon_centre = (self.lon_max+self.lon_min)/2.0

    else:
      self.global_regional = "regional"

      self.d_lat=5.0
      self.d_lon=5.0
Beispiel #5
0
    def plot_threshold(self,
                       val,
                       min_val_plot,
                       max_val_plot,
                       colormap='tomo',
                       verbose=False):
        """ plot depth to a certain threshold value 'val' in an ses3d model

    plot_threshold(val,min_val_plot,max_val_plot,colormap='tomo',verbose=False):
    val=threshold value
    min_val_plot, max_val_plot=minimum and maximum values of the colour scale
    colormap='tomo','mono'
    """

        #- set up a map and colourmap

        if self.global_regional == 'regional':
            m = Basemap(projection='merc',
                        llcrnrlat=self.lat_min,
                        urcrnrlat=self.lat_max,
                        llcrnrlon=self.lon_min,
                        urcrnrlon=self.lon_max,
                        lat_ts=20,
                        resolution=res)
            m.drawparallels(np.arange(self.lat_min, self.lat_max, self.d_lon),
                            labels=[1, 0, 0, 1])
            m.drawmeridians(np.arange(self.lon_min, self.lon_max, self.d_lat),
                            labels=[1, 0, 0, 1])
        elif self.global_regional == 'global':
            m = Basemap(projection='ortho',
                        lon_0=self.lon_centre,
                        lat_0=self.lat_centre,
                        resolution=res)
            m.drawparallels(np.arange(-80.0, 80.0, 10.0), labels=[1, 0, 0, 1])
            m.drawmeridians(np.arange(-170.0, 170.0, 10.0),
                            labels=[1, 0, 0, 1])

        m.drawcoastlines()
        m.drawcountries()

        m.drawmapboundary(fill_color=[1.0, 1.0, 1.0])

        if colormap == 'tomo':
            my_colormap = cm.make_colormap({
                0.0: [0.1, 0.0, 0.0],
                0.2: [0.8, 0.0, 0.0],
                0.3: [1.0, 0.7, 0.0],
                0.48: [0.92, 0.92, 0.92],
                0.5: [0.92, 0.92, 0.92],
                0.52: [0.92, 0.92, 0.92],
                0.7: [0.0, 0.6, 0.7],
                0.8: [0.0, 0.0, 0.8],
                1.0: [0.0, 0.0, 0.1]
            })
        elif colormap == 'mono':
            my_colormap = cm.make_colormap({
                0.0: [1.0, 1.0, 1.0],
                0.15: [1.0, 1.0, 1.0],
                0.85: [0.0, 0.0, 0.0],
                1.0: [0.0, 0.0, 0.0]
            })

        #- loop over subvolumes

        for k in np.arange(self.nsubvol):

            depth = np.zeros(np.shape(self.m[k].v[:, :, 0]))

            nx = len(self.m[k].lat)
            ny = len(self.m[k].lon)

            #- find depth

            r = self.m[k].r
            r = 0.5 * (r[0:len(r) - 1] + r[1:len(r)])

            for idx in np.arange(nx - 1):
                for idy in np.arange(ny - 1):

                    n = self.m[k].v[idx, idy, :] >= val
                    depth[idx, idy] = 6371.0 - np.max(r[n])

            #- rotate coordinate system if necessary

            lon, lat = np.meshgrid(self.m[k].lon[0:ny], self.m[k].lat[0:nx])

            if self.phi != 0.0:

                lat_rot = np.zeros(np.shape(lon), dtype=float)
                lon_rot = np.zeros(np.shape(lat), dtype=float)

                for idx in np.arange(nx):
                    for idy in np.arange(ny):

                        colat = 90.0 - lat[idx, idy]

                        lat_rot[idx,
                                idy], lon_rot[idx,
                                              idy] = rot.rotate_coordinates(
                                                  self.n, -self.phi, colat,
                                                  lon[idx, idy])
                        lat_rot[idx, idy] = 90.0 - lat_rot[idx, idy]

                lon = lon_rot
                lat = lat_rot

                #- convert to map coordinates and plot

            x, y = m(lon, lat)
            im = m.pcolor(x,
                          y,
                          depth,
                          cmap=my_colormap,
                          vmin=min_val_plot,
                          vmax=max_val_plot)

        m.colorbar(im, "right", size="3%", pad='2%')
        plt.title('depth to ' + str(val) + ' km/s [km]')
        plt.show()
Beispiel #6
0
    def convert_to_vtk(self, directory, filename, verbose=False):
        """ convert ses3d model to vtk format for plotting with Paraview

    convert_to_vtk(self,directory,filename,verbose=False):
    """

        #- preparatory steps

        nx = np.zeros(self.nsubvol, dtype=int)
        ny = np.zeros(self.nsubvol, dtype=int)
        nz = np.zeros(self.nsubvol, dtype=int)
        N = 0

        for n in np.arange(self.nsubvol):
            nx[n] = len(self.m[n].lat)
            ny[n] = len(self.m[n].lon)
            nz[n] = len(self.m[n].r)
            N = N + nx[n] * ny[n] * nz[n]

        #- open file and write header

        fid = open(directory + filename, 'w')

        if verbose == True:
            print 'write to file ' + directory + filename

        fid.write('# vtk DataFile Version 3.0\n')
        fid.write('vtk output\n')
        fid.write('ASCII\n')
        fid.write('DATASET UNSTRUCTURED_GRID\n')

        #- write grid points

        fid.write('POINTS ' + str(N) + ' float\n')

        for n in np.arange(self.nsubvol):

            if verbose == True:
                print 'writing grid points for subvolume ' + str(n)

            for i in np.arange(nx[n]):
                for j in np.arange(ny[n]):
                    for k in np.arange(nz[n]):

                        theta = 90.0 - self.m[n].lat[i]
                        phi = self.m[n].lon[j]

                        #- rotate coordinate system

                        if self.phi != 0.0:
                            theta, phi = rot.rotate_coordinates(
                                self.n, -self.phi, theta, phi)

                        #- transform to cartesian coordinates and write to file

                        theta = theta * np.pi / 180.0
                        phi = phi * np.pi / 180.0

                        r = self.m[n].r[k]
                        x = r * np.sin(theta) * np.cos(phi)
                        y = r * np.sin(theta) * np.sin(phi)
                        z = r * np.cos(theta)

                        fid.write(str(x) + ' ' + str(y) + ' ' + str(z) + '\n')

        #- write connectivity

        n_cells = 0

        for n in np.arange(self.nsubvol):
            n_cells = n_cells + (nx[n] - 1) * (ny[n] - 1) * (nz[n] - 1)

        fid.write('\n')
        fid.write('CELLS ' + str(n_cells) + ' ' + str(9 * n_cells) + '\n')

        count = 0

        for n in np.arange(self.nsubvol):

            if verbose == True:
                print 'writing conectivity for subvolume ' + str(n)

            for i in np.arange(1, nx[n]):
                for j in np.arange(1, ny[n]):
                    for k in np.arange(1, nz[n]):
                        # i j k
                        a = count + k + (j - 1) * nz[n] + (
                            i - 1) * ny[n] * nz[n] - 1  # 0 0 0
                        b = count + k + (j - 1) * nz[n] + (
                            i - 1) * ny[n] * nz[n]  # 0 0 1
                        c = count + k + (j) * nz[n] + (
                            i - 1) * ny[n] * nz[n] - 1  # 0 1 0
                        d = count + k + (j) * nz[n] + (
                            i - 1) * ny[n] * nz[n]  # 0 1 1
                        e = count + k + (j - 1) * nz[n] + (
                            i) * ny[n] * nz[n] - 1  # 1 0 0
                        f = count + k + (j - 1) * nz[n] + (
                            i) * ny[n] * nz[n]  # 1 0 1
                        g = count + k + (j) * nz[n] + (
                            i) * ny[n] * nz[n] - 1  # 1 1 0
                        h = count + k + (j) * nz[n] + (
                            i) * ny[n] * nz[n]  # 1 1 1

                        fid.write('8 ' + str(a) + ' ' + str(b) + ' ' + str(c) +
                                  ' ' + str(d) + ' ' + str(e) + ' ' + str(f) +
                                  ' ' + str(g) + ' ' + str(h) + '\n')

            count = count + nx[n] * ny[n] * nz[n]

        #- write cell types

        fid.write('\n')
        fid.write('CELL_TYPES ' + str(n_cells) + '\n')

        for n in np.arange(self.nsubvol):

            if verbose == True:
                print 'writing cell types for subvolume ' + str(n)

            for i in np.arange(nx[n] - 1):
                for j in np.arange(ny[n] - 1):
                    for k in np.arange(nz[n] - 1):

                        fid.write('11\n')

        #- write data

        fid.write('\n')
        fid.write('POINT_DATA ' + str(N) + '\n')
        fid.write('SCALARS scalars float\n')
        fid.write('LOOKUP_TABLE mytable\n')

        for n in np.arange(self.nsubvol):

            if verbose == True:
                print 'writing data for subvolume ' + str(n)

            idx = np.arange(nx[n])
            idx[nx[n] - 1] = nx[n] - 2

            idy = np.arange(ny[n])
            idy[ny[n] - 1] = ny[n] - 2

            idz = np.arange(nz[n])
            idz[nz[n] - 1] = nz[n] - 2

            for i in idx:
                for j in idy:
                    for k in idz:

                        fid.write(str(self.m[n].v[i, j, k]) + '\n')

        #- clean up

        fid.close()
Beispiel #7
0
    def read(self, directory, filename, verbose=False):
        """ read an ses3d model from a file

    read(self,directory,filename,verbose=False):
    """

        #- read block files ====================================================

        fid_x = open(directory + 'block_x', 'r')
        fid_y = open(directory + 'block_y', 'r')
        fid_z = open(directory + 'block_z', 'r')

        if verbose == True:
            print 'read block files:'
            print '\t ' + directory + 'block_x'
            print '\t ' + directory + 'block_y'
            print '\t ' + directory + 'block_z'

        dx = np.array(fid_x.read().strip().split('\n'), dtype=float)
        dy = np.array(fid_y.read().strip().split('\n'), dtype=float)
        dz = np.array(fid_z.read().strip().split('\n'), dtype=float)

        fid_x.close()
        fid_y.close()
        fid_z.close()

        #- read coordinate lines ===============================================

        self.nsubvol = int(dx[0])

        if verbose == True:
            print 'number of subvolumes: ' + str(self.nsubvol)

        idx = np.zeros(self.nsubvol, dtype=int) + 1
        idy = np.zeros(self.nsubvol, dtype=int) + 1
        idz = np.zeros(self.nsubvol, dtype=int) + 1

        for k in np.arange(1, self.nsubvol, dtype=int):
            idx[k] = int(dx[idx[k - 1]]) + idx[k - 1] + 1
            idy[k] = int(dy[idy[k - 1]]) + idy[k - 1] + 1
            idz[k] = int(dz[idz[k - 1]]) + idz[k - 1] + 1

        for k in np.arange(self.nsubvol, dtype=int):
            subvol = ses3d_submodel()
            subvol.lat = 90.0 - dx[(idx[k] + 1):(idx[k] + 1 + dx[idx[k]])]
            subvol.lon = dy[(idy[k] + 1):(idy[k] + 1 + dy[idy[k]])]
            subvol.r = dz[(idz[k] + 1):(idz[k] + 1 + dz[idz[k]])]
            self.m.append(subvol)

        #- compute rotated version of the coordinate lines ====================

        if self.phi != 0.0:

            for k in np.arange(self.nsubvol, dtype=int):

                nx = len(self.m[k].lat)
                ny = len(self.m[k].lon)

                self.m[k].lat_rot = np.zeros([nx, ny])
                self.m[k].lon_rot = np.zeros([nx, ny])

                for idx in np.arange(nx):
                    for idy in np.arange(ny):

                        self.m[k].lat_rot[idx, idy], self.m[k].lon_rot[
                            idx, idy] = rot.rotate_coordinates(
                                self.n, -self.phi, 90.0 - self.m[k].lat[idx],
                                self.m[k].lon[idy])
                        self.m[k].lat_rot[idx,
                                          idy] = 90.0 - self.m[k].lat_rot[idx,
                                                                          idy]

        else:

            self.m[k].lat_rot, self.m[k].lon_rot = np.meshgrid(
                self.m[k].lat, self.m[k].lon)

        #- read model volume ==================================================

        fid_m = open(directory + filename, 'r')

        if verbose == True:
            print 'read model file: ' + directory + filename

        v = np.array(fid_m.read().strip().split('\n'), dtype=float)

        fid_m.close()

        #- assign values ======================================================

        idx = 1
        for k in np.arange(self.nsubvol):

            n = int(v[idx])
            nx = len(self.m[k].lat) - 1
            ny = len(self.m[k].lon) - 1
            nz = len(self.m[k].r) - 1

            self.m[k].v = v[(idx + 1):(idx + 1 + n)].reshape(nx, ny, nz)

            idx = idx + n + 1

        #- decide on global or regional model==================================

        self.lat_min = 90.0
        self.lat_max = -90.0
        self.lon_min = 180.0
        self.lon_max = -180.0

        for k in np.arange(self.nsubvol):
            if np.min(self.m[k].lat_rot) < self.lat_min:
                self.lat_min = np.min(self.m[k].lat_rot)
            if np.max(self.m[k].lat_rot) > self.lat_max:
                self.lat_max = np.max(self.m[k].lat_rot)
            if np.min(self.m[k].lon_rot) < self.lon_min:
                self.lon_min = np.min(self.m[k].lon_rot)
            if np.max(self.m[k].lon_rot) > self.lon_max:
                self.lon_max = np.max(self.m[k].lon_rot)

        if ((self.lat_max - self.lat_min) > 90.0
                or (self.lon_max - self.lon_min) > 90.0):
            self.global_regional = "global"

            self.lat_centre = (self.lat_max + self.lat_min) / 2.0
            self.lon_centre = (self.lon_max + self.lon_min) / 2.0

        else:
            self.global_regional = "regional"

            self.d_lat = 5.0
            self.d_lon = 5.0
Beispiel #8
0
    def plot_depth_slice(self,
                         component,
                         depth,
                         valmin,
                         valmax,
                         iteration=0,
                         verbose=True,
                         stations=True,
                         res="i"):
        """
		plot_depth_slice(self, component, depth, valmin, valmax, iteration=0, verbose=True, stations=True, res="i")

		Plot depth slices of field component at depth "depth" with colourbar ranging between "valmin" and "valmax".
		The resolution of the coastline is "res" (c, l, i, h, f).

		The currently available "components" are:
			Material parameters: A, B, C, mu, lambda, rhoinv, vp, vsh, vsv, rho
			Velocity field snapshots: vx, vy, vz
			Sensitivity kernels: Q_mu, Q_kappa, alpha_mu, alpha_kappa
		"""

        #- Some initialisations. ------------------------------------------------------------------
        n_procs = self.setup["procs"]["px"] * self.setup["procs"][
            "py"] * self.setup["procs"]["pz"]
        radius = 1000.0 * (6371.0 - depth)

        vmax = float("-inf")
        vmin = float("inf")

        lat_min = 90.0 - self.setup["domain"]["theta_max"] * 180.0 / np.pi
        lat_max = 90.0 - self.setup["domain"]["theta_min"] * 180.0 / np.pi
        lon_min = self.setup["domain"]["phi_min"] * 180.0 / np.pi
        lon_max = self.setup["domain"]["phi_max"] * 180.0 / np.pi

        lat_centre = (lat_max + lat_min) / 2.0
        lon_centre = (lon_max + lon_min) / 2.0
        lat_centre, lon_centre = rot.rotate_coordinates(
            self.n, -self.rotangle, 90.0 - lat_centre, lon_centre)
        lat_centre = 90.0 - lat_centre

        d_lon = np.round((lon_max - lon_min) / 10.0)
        d_lat = np.round((lat_max - lat_min) / 10.0)

        #- Set up the map. ------------------------------------------------------------------------
        if (lat_max - lat_min) > 30.0:
            m = Basemap(projection='ortho',
                        lon_0=lon_centre,
                        lat_0=lat_centre,
                        resolution=res)
            m.drawparallels(np.arange(-80.0, 80.0, 10.0), labels=[1, 0, 0, 1])
            m.drawmeridians(np.arange(-170.0, 170.0, 10.0),
                            labels=[1, 0, 0, 1])
        else:
            m = Basemap(projection='merc',
                        llcrnrlat=lat_min,
                        urcrnrlat=lat_max,
                        llcrnrlon=lon_min,
                        urcrnrlon=lon_max,
                        lat_ts=20,
                        resolution=res)
            m.drawparallels(np.arange(np.round(lat_min), np.round(lat_max),
                                      d_lat),
                            labels=[1, 0, 0, 1])
            m.drawmeridians(np.arange(np.round(lon_min), np.round(lon_max),
                                      d_lon),
                            labels=[1, 0, 0, 1])

        m.drawcoastlines()
        m.fillcontinents("0.9", zorder=0)
        m.drawmapboundary(fill_color="white")
        m.drawcountries()

        #- Loop over processor boxes and check if depth falls within the volume. ------------------
        for p in range(n_procs):

            if (radius >= self.z[p, :].min()) & (radius <= self.z[p, :].max()):

                #- Read this field and make lats & lons. ------------------------------------------
                field = self.read_single_box(component, p, iteration)
                lats = 90.0 - self.theta[p, :] * 180.0 / np.pi
                lons = self.phi[p, :] * 180.0 / np.pi
                lon, lat = np.meshgrid(lons, lats)

                #- Find the depth index and plot for this one box. --------------------------------
                idz = min(
                    np.where(
                        min(np.abs(self.z[p, :] -
                                   radius)) == np.abs(self.z[p, :] -
                                                      radius))[0])

                r_effective = int(self.z[p, idz] / 1000.0)

                #- Find min and max values. -------------------------------------------------------

                vmax = max(vmax, field[:, :, idz].max())
                vmin = min(vmin, field[:, :, idz].min())

                #- Make lats and lons. ------------------------------------------------------------
                lats = 90.0 - self.theta[p, :] * 180.0 / np.pi
                lons = self.phi[p, :] * 180.0 / np.pi
                lon, lat = np.meshgrid(lons, lats)

                #- Rotate if necessary. -----------------------------------------------------------
                if self.rotangle != 0.0:

                    lat_rot = np.zeros(np.shape(lon), dtype=float)
                    lon_rot = np.zeros(np.shape(lat), dtype=float)

                    for idlon in np.arange(len(lons)):
                        for idlat in np.arange(len(lats)):

                            lat_rot[idlat, idlon], lon_rot[
                                idlat, idlon] = rot.rotate_coordinates(
                                    self.n, -self.rotangle,
                                    90.0 - lat[idlat, idlon], lon[idlat,
                                                                  idlon])
                            lat_rot[idlat,
                                    idlon] = 90.0 - lat_rot[idlat, idlon]

                    lon = lon_rot
                    lat = lat_rot

                #- Make a nice colourmap. ---------------------------------------------------------
                my_colormap = cm.make_colormap({
                    0.0: [0.1, 0.0, 0.0],
                    0.2: [0.8, 0.0, 0.0],
                    0.3: [1.0, 0.7, 0.0],
                    0.48: [0.92, 0.92, 0.92],
                    0.5: [0.92, 0.92, 0.92],
                    0.52: [0.92, 0.92, 0.92],
                    0.7: [0.0, 0.6, 0.7],
                    0.8: [0.0, 0.0, 0.8],
                    1.0: [0.0, 0.0, 0.1]
                })

                x, y = m(lon, lat)
                im = m.pcolormesh(x,
                                  y,
                                  field[:, :, idz],
                                  cmap=my_colormap,
                                  vmin=valmin,
                                  vmax=valmax)

        #- Add colobar and title. ------------------------------------------------------------------
        cb = m.colorbar(im, "right", size="3%", pad='2%')
        if component in UNIT_DICT:
            cb.set_label(UNIT_DICT[component], fontsize="x-large", rotation=0)

        plt.suptitle("Depth slice of %s at %i km" % (component, r_effective),
                     size="large")

        #- Plot stations if available. ------------------------------------------------------------
        if (self.stations == True) & (stations == True):

            x, y = m(self.stlons, self.stlats)

            for n in range(self.n_stations):
                plt.text(x[n], y[n], self.stnames[n][:4])
                plt.plot(x[n], y[n], 'ro')

        plt.show()

        if verbose == True:
            print "minimum value: " + str(vmin) + ", maximum value: " + str(
                vmax)