def test_spherical_surface_elements(): xg, yg = np.meshgrid([0., 1., 2., 3.], [4., 5., 6., 3.]) grid = np.array([np.ravel(xg), np.ravel(yg)]) surfel = get_spherical_surface_elements(grid[0], grid[1]) assert (len(surfel) == len(grid[0])) assert (type(surfel) == np.ndarray) assert (pytest.approx(surfel[0]) == 12361699247.23782)
def __init__(self,model,w='r'): # Model is an hdf5 file which contains the basis and weights of the source model! try: self.model = h5py.File(model,w) self.src_loc = self.model['coordinates'] self.freq = self.model['frequencies'] # Presumably, these arrays are small and will be used very often --> good to have in memory. self.distr_basis = self.model['distr_basis'][:] self.spect_basis = self.model['spect_basis'][:] self.distr_weights = self.model['distr_weights'][:] # The surface area of each grid element...new since June 18 try: self.surf_area = self.model['surf_areas'][:] except KeyError: # approximate as spherical surface elements... self.surf_area = get_spherical_surface_elements( self.src_loc[0],self.src_loc[1]) np.save('surface_areas_grid.npy',self.surf_area) self.spatial_source_model = self.expand_distr() except IOError: msg = 'Unable to open model file '+model raise IOError(msg)
# the weights matrix is adapted by updates ######################## weights = np.eye(basis_spec.shape[0], num_bases) ######################## # approximate surface # areas of all elements # set up in this way. ######################## if config["voronoi_surface_area"]: from noisi.util.voronoi_surface_area import get_voronoi_surface_area grd, surf_areas = get_voronoi_surface_area(grd) else: surf_areas = get_spherical_surface_elements(grd[0], grd[1]) ######################## # Save to an hdf5 file ######################## with h5py.File(os.path.join(sourcepath, 'step_0', 'starting_model.h5'), 'w') as fh: fh.create_dataset('coordinates', data=grd.astype(np.float64)) fh.create_dataset('frequencies', data=freq.astype(np.float64)) fh.create_dataset('distr_basis', data=basis_geo.astype(np.float64)) # for now: Geographic model can vary freely. fh.create_dataset('distr_weights', data=weights) fh.create_dataset('spect_basis', data=basis_spec.astype(np.float64)) fh.create_dataset('surf_areas', data=surf_areas.astype(np.float64))
######################## weights = np.eye(basis_spec.shape[0],num_bases) ######################## # approximate surface # areas of all elements # set up in this way. ######################## if config["voronoi_surface_area"]: from noisi.util.voronoi_surface_area import get_voronoi_surface_area grd, surf_areas = get_voronoi_surface_area(grd) else: surf_areas = get_spherical_surface_elements(grd[0],grd[1]) ######################## # Save to an hdf5 file ######################## with h5py.File(os.path.join(sourcepath,'step_0','starting_model.h5'),'w') as fh: fh.create_dataset('coordinates',data=grd.astype(np.float64)) fh.create_dataset('frequencies',data=freq.astype(np.float64)) fh.create_dataset('distr_basis',data=basis_geo.astype(np.float64)) # for now: Geographic model can vary freely. fh.create_dataset('distr_weights',data=weights) fh.create_dataset('spect_basis',data=basis_spec.astype(np.float64)) fh.create_dataset('surf_areas',data=surf_areas.astype(np.float64))
def setup_source_startingmodel(self, args): # plotting: colors = ['purple', 'g', 'b', 'orange'] colors_cmaps = [ plt.cm.Purples, plt.cm.Greens, plt.cm.Blues, plt.cm.Oranges ] print("Setting up source starting model.", end="\n") with io.open(os.path.join(args.source_model, 'source_config.yml'), 'r') as fh: source_conf = yaml.safe_load(fh) with io.open(os.path.join(source_conf['project_path'], 'config.yml'), 'r') as fh: conf = yaml.safe_load(fh) with io.open(source_conf['source_setup_file'], 'r') as fh: parameter_sets = yaml.safe_load(fh) if conf['verbose']: print("The following input parameters are used:", end="\n") pp = pprint.PrettyPrinter() pp.pprint(parameter_sets) # load the source locations of the grid grd = np.load(os.path.join(conf['project_path'], 'sourcegrid.npy')) # add the approximate spherical surface elements if grd.shape[-1] < 50000: surf_el = get_spherical_surface_elements(grd[0], grd[1]) else: warn('Large grid; surface element computation slow. Using \ approximate surface elements.') surf_el = np.ones(grd.shape[-1]) * conf['grid_dx_in_m']**2 # get the relevant array sizes wfs = glob(os.path.join(conf['project_path'], 'greens', '*.h5')) if wfs != []: if conf['verbose']: print('Found wavefield stats.') else: pass else: raise FileNotFoundError('No wavefield database found. Run \ precompute_wavefield first.') with WaveField(wfs[0]) as wf: df = wf.stats['Fs'] n = wf.stats['npad'] freq = np.fft.rfftfreq(n, d=1. / df) n_distr = len(parameter_sets) coeffs = np.zeros((grd.shape[-1], n_distr)) spectra = np.zeros((n_distr, len(freq))) # fill in the distributions and the spectra for i in range(n_distr): coeffs[:, i] = self.distribution_from_parameters( grd, parameter_sets[i], conf['verbose']) # plot outfile = os.path.join(args.source_model, 'source_starting_model_distr%g.png' % i) if create_plot: plot_grid(grd[0], grd[1], coeffs[:, i], outfile=outfile, cmap=colors_cmaps[i % len(colors_cmaps)], sequential=True, normalize=False, quant_unit='Spatial weight (-)', axislabelpad=-0.1, size=10) spectra[i, :] = self.spectrum_from_parameters( freq, parameter_sets[i]) # plotting the spectra # plotting is not necessarily done to make sure code runs on clusters if create_plot: fig1 = plt.figure() ax = fig1.add_subplot('111') for i in range(n_distr): ax.plot(freq, spectra[i, :] / spectra.max(), color=colors[i % len(colors_cmaps)]) ax.set_xlabel('Frequency / Nyquist Frequency') plt.xticks([ 0, freq.max() * 0.25, freq.max() * 0.5, freq.max() * 0.75, freq.max() ], ['0', '0.25', '0.5', '0.75', '1']) ax.set_ylabel('Rel. PSD norm. to strongest spectrum (-)') fig1.savefig( os.path.join(args.source_model, 'source_starting_model_spectra.png')) # Save to an hdf5 file with h5py.File( os.path.join(args.source_model, 'iteration_0', 'starting_model.h5'), 'w') as fh: fh.create_dataset('coordinates', data=grd) fh.create_dataset('frequencies', data=freq) fh.create_dataset('model', data=coeffs.astype(np.float)) fh.create_dataset('spectral_basis', data=spectra.astype(np.float)) fh.create_dataset('surface_areas', data=surf_el.astype(np.float)) # Save to an hdf5 file with h5py.File(os.path.join(args.source_model, 'spectral_model.h5'), 'w') as fh: uniform_spatial = np.ones(coeffs.shape) * 1.0 fh.create_dataset('coordinates', data=grd) fh.create_dataset('frequencies', data=freq) fh.create_dataset('model', data=uniform_spatial.astype(np.float)) fh.create_dataset('spectral_basis', data=spectra.astype(np.float)) fh.create_dataset('surface_areas', data=surf_el.astype(np.float))