def test_ferret2sww_2(self): """Test that georeferencing etc works when converting from ferret format (lat/lon) to sww format (UTM) """ #The test file has # LON = 150.66667, 150.83334, 151, 151.16667 # LAT = -34.5, -34.33333, -34.16667, -34 ; # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ; # # First value (index=0) in small_ha.nc is 0.3400644 cm, # Fourth value (index==3) is -6.50198 cm from anuga.coordinate_transforms.redfearn import redfearn #fid = NetCDFFile('small_ha.nc') fid = NetCDFFile(self.test_MOST_file + '_ha.nc') #Pick a coordinate and a value time_index = 1 lat_index = 0 lon_index = 2 test_value = fid.variables['HA'][:][time_index, lat_index, lon_index] test_time = fid.variables['TIME'][:][time_index] test_lat = fid.variables['LAT'][:][lat_index] test_lon = fid.variables['LON'][:][lon_index] linear_point_index = lat_index * 4 + lon_index fid.close() #Call conversion (with zero origin) ferret2sww(self.test_MOST_file, verbose=self.verbose, origin=(56, 0, 0)) #Work out the UTM coordinates for test point zone, e, n = redfearn(test_lat, test_lon) #Read output file 'small.sww' fid = NetCDFFile(self.test_MOST_file + '.sww') x = fid.variables['x'][:] y = fid.variables['y'][:] #Check that test coordinate is correctly represented assert num.allclose(x[linear_point_index], e) assert num.allclose(y[linear_point_index], n) #Check test value stage = fid.variables['stage'][:] assert num.allclose(stage[time_index, linear_point_index], old_div(test_value, 100)) fid.close() #Cleanup import os os.remove(self.test_MOST_file + '.sww')
def test_read_write_NetCDF(self): from anuga.file.netcdf import NetCDFFile g = Geo_reference(56, 1.9, 1.9) file_name = tempfile.mktemp(".geo_referenceTest") out_file = NetCDFFile(file_name, netcdf_mode_w) g.write_NetCDF(out_file) out_file.close() in_file = NetCDFFile(file_name, netcdf_mode_r) new_g = Geo_reference(NetCDFObject=in_file) in_file.close() os.remove(file_name) self.assertTrue(g == new_g, 'test_read_write_NetCDF failed')
def test_urs_ungridded_holeII(self): # Check that if using a hole that returns no triangles, # urs_ungridded2sww removes the hole label. lat_long = [[-20.5, 114.5], [-20.6, 114.6], [-20.5, 115.5], [-20.6, 115.4], [-21.5, 114.5], [-21.4, 114.6], [-21.5, 115.5], [-21.4, 115.4] ] time_step_count = 6 time_step = 100 tide = 9000000 base_name, files = self.write_mux(lat_long, time_step_count, time_step) #Easting: 292110.784 Northing: 7676551.710 #Latitude: -21 0 ' 0.00000 '' Longitude: 115 0 ' 0.00000 '' urs_ungridded2sww(base_name, mean_stage=-240992.0, hole_points_UTM=[ 292110.784, 7676551.710 ]) # now I want to check the sww file ... sww_file = base_name + '.sww' fid = NetCDFFile(sww_file) volumes = fid.variables['volumes'][:] #print "number_of_volumes",len(volumes) fid.close() os.remove(sww_file) urs_ungridded2sww(base_name, mean_stage=-240992.0) # now I want to check the sww file ... sww_file = base_name + '.sww' fid = NetCDFFile(sww_file) volumes_again = fid.variables['volumes'][:] #print "number_of_volumes",len(volumes_again) assert num.allclose(len(volumes_again), len(volumes)) fid.close() os.remove(sww_file) self.delete_mux(files)
def read_result(fn, save_pl, matrix_A, ele, quantity_names, case_num, time_thinning=1): fid = NetCDFFile(fn, netcdf_mode_r) time = fid.variables['time'][:] upper_time_index = len(time) quantities = {} for i, name in enumerate(quantity_names): quantities[name] = fid.variables[name][:] quantities[name] = np.array(quantities[name][::time_thinning, :]) fid.close() for i, t in enumerate(time): for name in quantity_names: Q = quantities[name][i, :] # Quantities at timestep i result = matrix_A * Q result = np.asarray(result) result = result.reshape((100, 100)) if name == 'stage': result = result - ele save_name = save_pl + case_num + '_' + 'depth' + '_{0}.csv'.format( i) else: save_name = save_pl + case_num + '_' + name + '_{0}.csv'.format( i) np.savetxt(save_name, result, delimiter=",")
def __init__(self, *args, **kwargs): Visualiser.__init__(self, *args, **kwargs) fin = NetCDFFile(self.vis_source, 'r') self.xPoints = array(fin.variables['x'], Float) self.yPoints = array(fin.variables['y'], Float) self.quantityCache = {} fin.close()
def __init__(self, source, frameDelay=100, frameStep=1): """The source parameter is assumed to be a NetCDF sww file. The frameDelay parameter is the number of milliseconds waited between frames. """ Visualiser.__init__(self, source) self.frameNumber = 0 fin = NetCDFFile(self.source, 'r') self.maxFrameNumber = fin.variables['time'].shape[0] - 1 fin.close() #self.frameNumberTkVariable = StringVar() #self.frameNumberTkVariable.set('Frame - %05g'%self.framNumber) self.frameDelay = frameDelay self.xmin = None self.xmax = None self.ymin = None self.ymax = None self.zmin = None self.zmax = None self.frameStep = frameStep self.vtk_heightQuantityCache = [] for i in range(self.maxFrameNumber + 1): # maxFrameNumber is zero indexed. self.vtk_heightQuantityCache.append({}) self.paused = False self.movie = False
def test_sww_header(self): """Test that constant sww information can be written correctly (non smooth) """ self.domain.set_name('datatest' + str(id(self))) self.domain.format = 'sww' #Remove?? self.domain.smooth = False sww = SWW_file(self.domain) sww.store_connectivity() # Check contents # Get NetCDF fid = NetCDFFile(sww.filename, netcdf_mode_r) # Open existing file for append # Get the variables sww_revision = fid.revision_number try: revision_number = get_revision_number() except: revision_number = None assert str(revision_number) == sww_revision fid.close() #print "sww.filename", sww.filename os.remove(sww.filename)
def test_sww_constant(self): """Test that constant sww information can be written correctly (non smooth) """ self.domain.set_name('datatest' + str(id(self))) self.domain.format = 'sww' #Remove?? self.domain.smooth = False sww = SWW_file(self.domain) sww.store_connectivity() fid = NetCDFFile(sww.filename, netcdf_mode_r) # Open existing file for append # Get the variables x = fid.variables['x'] y = fid.variables['y'] z = fid.variables['elevation'] V = fid.variables['volumes'] assert num.allclose (x[:], self.X.flatten()) assert num.allclose (y[:], self.Y.flatten()) assert num.allclose (z[:], self.F.flatten()) P = len(self.domain) for k in range(P): assert V[k, 0] == 3*k assert V[k, 1] == 3*k+1 assert V[k, 2] == 3*k+2 fid.close() os.remove(sww.filename)
def sww_merge_parallel(domain_global_name, np, verbose=False, delete_old=False): output = domain_global_name + ".sww" swwfiles = [ domain_global_name + "_P" + str(np) + "_" + str(v) + ".sww" for v in range(np) ] fid = NetCDFFile(swwfiles[0], netcdf_mode_r) try: # works with netcdf4 number_of_volumes = len(fid.dimensions['number_of_volumes']) number_of_points = len(fid.dimensions['number_of_points']) except: # works with scientific.io.netcdf number_of_volumes = int(fid.dimensions['number_of_volumes']) number_of_points = int(fid.dimensions['number_of_points']) fid.close() if 3 * number_of_volumes == number_of_points: _sww_merge_parallel_non_smooth(swwfiles, output, verbose, delete_old) else: _sww_merge_parallel_smooth(swwfiles, output, verbose, delete_old)
def test_ferret2sww_nz_origin(self): from anuga.coordinate_transforms.redfearn import redfearn #Call conversion (with nonzero origin) ferret2sww(self.test_MOST_file, verbose=self.verbose, origin=(56, 100000, 200000)) #Work out the UTM coordinates for first point zone, e, n = redfearn(-34.5, 150.66667) #Read output file 'small.sww' #fid = NetCDFFile('small.sww', netcdf_mode_r) fid = NetCDFFile(self.test_MOST_file + '.sww') x = fid.variables['x'][:] y = fid.variables['y'][:] #Check that first coordinate is correctly represented assert num.allclose(x[0], e - 100000) assert num.allclose(y[0], n - 200000) fid.close() #Cleanup os.remove(self.test_MOST_file + '.sww')
def test_boundary_timeII(self): """ test_boundary_timeII(self): Test that starttime can be set in the middle of a boundary condition """ boundary_starttime = 0 boundary_filename = self.create_sww_boundary(boundary_starttime) #print "boundary_filename",boundary_filename filename = tempfile.mktemp(".sww") #print "filename",filename dir, base = os.path.split(filename) senario_name = base[:-4] mesh = Mesh() mesh.add_region_from_polygon([[10, 10], [90, 10], [90, 90], [10, 90]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(senario_name) domain.set_datadir(dir) new_starttime = 0. domain.set_starttime(new_starttime) # Setup initial conditions domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 0.0) Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False) # Setup boundary conditions domain.set_boundary({'exterior': Bf}) for t in domain.evolve(yieldstep=5, finaltime=9.0): pass #print domain.boundary_statistics() #domain.write_time() #print "domain.time", domain.time # do an assertion on the time of the produced sww file fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read times = fid.variables['time'][:] stage = fid.variables['stage'][:] #print stage #print "times", times #print "fid.starttime", fid.starttime assert num.allclose(fid.starttime, new_starttime) fid.close() #print "stage[2,0]", stage[2,0] msg = "This test is a bit hand crafted, based on the output file. " msg += "Not logic. " msg += "It's testing that starttime is working" assert num.allclose(stage[2, 0], 4.85825), msg # clean up os.remove(boundary_filename) os.remove(filename)
def test_sww_variable2(self): """Test that sww information can be written correctly multiple timesteps. Use average as reduction operator """ import time, os self.domain.set_name('datatest' + str(id(self))) self.domain.format = 'sww' self.domain.smooth = True self.domain.reduction = mean sww = SWW_file(self.domain) sww.store_connectivity() sww.store_timestep() #self.domain.tight_slope_limiters = 1 self.domain.evolve_to_end(finaltime=0.01) sww.store_timestep() # Check contents # Get NetCDF fid = NetCDFFile(sww.filename, netcdf_mode_r) # Get the variables x = fid.variables['x'] y = fid.variables['y'] z = fid.variables['elevation'] time = fid.variables['time'] stage = fid.variables['stage'] #Check values Q = self.domain.quantities['stage'] Q0 = Q.vertex_values[:, 0] Q1 = Q.vertex_values[:, 1] Q2 = Q.vertex_values[:, 2] A = stage[1, :] assert num.allclose(A[0], old_div((Q2[0] + Q1[1]), 2), rtol=1.0e-5, atol=1.0e-5) assert num.allclose(A[1], old_div((Q0[1] + Q1[3] + Q2[2]), 3), rtol=1.0e-5, atol=1.0e-5) assert num.allclose(A[2], Q0[3], rtol=1.0e-5, atol=1.0e-5) assert num.allclose(A[3], old_div((Q0[0] + Q1[5] + Q2[4]), 3), rtol=1.0e-5, atol=1.0e-5) #Center point assert num.allclose(A[4], old_div((Q1[0] + Q2[1] + Q0[2] +\ Q0[5] + Q2[6] + Q1[7]),6), rtol=1.0e-5, atol=1.0e-5) fid.close() #Cleanup os.remove(sww.filename)
def test_ferret2sww_lat_longII(self): # Test that min lat long works #The test file has # LON = 150.66667, 150.83334, 151, 151.16667 # LAT = -34.5, -34.33333, -34.16667, -34 ; #Read from anuga.coordinate_transforms.redfearn import redfearn fid = NetCDFFile(self.test_MOST_file + '_ha.nc') first_value = fid.variables['HA'][:][0, 0, 0] fourth_value = fid.variables['HA'][:][0, 0, 3] fid.close() #Call conversion (with zero origin) #ferret2sww('small', verbose=self.verbose, # origin = (56, 0, 0)) try: ferret2sww(self.test_MOST_file, verbose=self.verbose, origin=(56, 0, 0), minlat=-34.5, maxlat=-35) except AssertionError: pass else: self.assertTrue(0 == 1, 'Bad input did not throw exception error!')
def test_triangulation_2_geo_refs(self): # # filename = tempfile.mktemp("_data_manager.sww") outfile = NetCDFFile(filename, netcdf_mode_w) points_utm = num.array([[0., 0.], [1., 1.], [0., 1.]]) volumes = [[0, 1, 2]] elevation = [0, 1, 2] new_origin = Geo_reference(56, 1, 1) points_georeference = Geo_reference(56, 0, 0) points_utm = points_georeference.change_points_geo_ref(points_utm) times = [0, 10] number_of_volumes = len(volumes) number_of_points = len(points_utm) sww = Write_sww(['elevation'], ['stage', 'xmomentum', 'ymomentum']) sww.store_header(outfile, times, number_of_volumes, number_of_points, description='fully sick testing', verbose=self.verbose, sww_precision=netcdf_float) sww.store_triangulation(outfile, points_utm, volumes, elevation, new_origin=new_origin, points_georeference=points_georeference, verbose=self.verbose) outfile.close() fid = NetCDFFile(filename) x = fid.variables['x'][:] y = fid.variables['y'][:] results_georef = Geo_reference() results_georef.read_NetCDF(fid) assert results_georef == new_origin fid.close() absolute = Geo_reference(56, 0, 0) assert num.allclose( num.array( absolute.change_points_geo_ref(map(None, x, y), new_origin)), points_utm) os.remove(filename)
def __init__(self, swwfile='domain.sww', plot_dir='_plot', min_depth=0.01, minimum_allowed_depth=1.0e-03): self.plot_dir = plot_dir self.make_plot_dir() self.min_depth = min_depth import matplotlib.tri as tri import numpy as np import os self.name = os.path.splitext(swwfile)[0] from anuga.file.netcdf import NetCDFFile p = NetCDFFile(swwfile) self.x = np.array(p.variables['x']) self.y = np.array(p.variables['y']) self.triangles = np.array(p.variables['volumes']) vols0 = self.triangles[:, 0] vols1 = self.triangles[:, 1] vols2 = self.triangles[:, 2] self.triang = tri.Triangulation(self.x, self.y, self.triangles) self.xc = (self.x[vols0] + self.x[vols1] + self.x[vols2]) / 3.0 self.yc = (self.y[vols0] + self.y[vols1] + self.y[vols2]) / 3.0 self.xllcorner = p.xllcorner self.yllcorner = p.yllcorner self.zone = p.zone self.elev = np.array(p.variables['elevation_c']) self.stage = np.array(p.variables['stage_c']) self.xmom = np.array(p.variables['xmomentum_c']) self.ymom = np.array(p.variables['ymomentum_c']) self.depth = np.zeros_like(self.stage) if (len(self.elev.shape) == 2): self.depth = self.stage - self.elev else: for i in range(self.depth.shape[0]): self.depth[i, :] = self.stage[i, :] - self.elev with np.errstate(invalid='ignore'): self.xvel = np.where(self.depth > minimum_allowed_depth, self.xmom / self.depth, 0.0) self.yvel = np.where(self.depth > minimum_allowed_depth, self.ymom / self.depth, 0.0) self.speed = np.sqrt(self.xvel**2 + self.yvel**2) self.time = np.array(p.variables['time'])
def test_read_NetCDFI(self): # test if read_NetCDF from anuga.file.netcdf import NetCDFFile g = Geo_reference(56, 1.9, 1.9) file_name = tempfile.mktemp(".geo_referenceTest") outfile = NetCDFFile(file_name, netcdf_mode_w) outfile.xllcorner = g.get_xllcorner() outfile.yllcorner = g.get_yllcorner() outfile.zone = g.get_zone() outfile.close() in_file = NetCDFFile(file_name, netcdf_mode_r) new_g = Geo_reference(NetCDFObject=in_file) in_file.close() os.remove(file_name) self.assertTrue(g == new_g, ' failed')
def setup_grid(self): fin = NetCDFFile(self.source, 'r') self.vtk_cells = vtkCellArray() N_tri = fin.variables['volumes'].shape[0] for v in range(N_tri): self.vtk_cells.InsertNextCell(3) for i in range(3): self.vtk_cells.InsertCellPoint(fin.variables['volumes'][v][i]) fin.close()
def test_triangulationII(self): # # filename = tempfile.mktemp("_data_manager.sww") outfile = NetCDFFile(filename, netcdf_mode_w) points_utm = num.array([[0., 0.], [1., 1.], [0., 1.]]) volumes = [[0, 1, 2]] elevation = [0, 1, 2] new_origin = None #new_origin = Geo_reference(56, 0, 0) times = [0, 10] number_of_volumes = len(volumes) number_of_points = len(points_utm) sww = Write_sww(['elevation'], ['stage', 'xmomentum', 'ymomentum']) sww.store_header(outfile, times, number_of_volumes, number_of_points, description='fully sick testing', verbose=self.verbose, sww_precision=netcdf_float) sww.store_triangulation(outfile, points_utm, volumes, new_origin=new_origin, verbose=self.verbose) sww.store_static_quantities(outfile, elevation=elevation) outfile.close() fid = NetCDFFile(filename) x = fid.variables['x'][:] y = fid.variables['y'][:] results_georef = Geo_reference() results_georef.read_NetCDF(fid) assert results_georef == Geo_reference(zone=None, xllcorner=0, yllcorner=0) fid.close() assert num.allclose(num.array(list(zip(x, y))), points_utm) os.remove(filename)
def helper_read_msh_file(self, filename): fid = NetCDFFile(filename, netcdf_mode_r) mesh = {} # Get the 'strings' variable strings = fid.variables['strings'][:] fid.close() return char_to_string(strings)
def get_matrix_A(fn, gauge_name): # points to read information from point_reader = reader(file(gauge_name)) points = [] point_name = [] for i, row in enumerate(point_reader): if i == 0: for j, value in enumerate(row): if value.strip() == 'easting': easting = j if value.strip() == 'northing': northing = j if value.strip() == 'name': name = j if value.strip() == 'elevation': elevation = j else: #points.append([float(row[easting]),float(row[northing])]) points.append([float(row[easting]), float(row[northing])]) point_name.append(row[name]) points_array = np.array(points, np.float) dim_gauge = int(np.sqrt(points_array.shape[0])) interpolation_points = ensure_absolute(points_array) # read the sww file to extract something fid = NetCDFFile(fn, netcdf_mode_r) xllcorner = fid.xllcorner yllcorner = fid.yllcorner zone = fid.zone x = fid.variables['x'][:] y = fid.variables['y'][:] triangles = fid.variables['volumes'][:] x = np.reshape(x, (len(x), 1)) y = np.reshape(y, (len(y), 1)) vertex_coordinates = np.concatenate((x, y), axis=1) ele = fid.variables['elevation'][:] fid.close() vertex_coordinates = ensure_absolute(vertex_coordinates) triangles = ensure_numeric(triangles) interpolation_points = ensure_numeric(interpolation_points) interpolation_points[:, 0] -= xllcorner interpolation_points[:, 1] -= yllcorner mesh = Mesh(vertex_coordinates, triangles) mesh_boundary_polygon = mesh.get_boundary_polygon() indices = outside_polygon(interpolation_points, mesh_boundary_polygon) interp = Interpolate(vertex_coordinates, triangles) matrix_A, inside_poly_indices, outside_poly_indices, centroids = interp._build_interpolation_matrix_A( interpolation_points, output_centroids=False, verbose=False) ele = matrix_A * ele ele = np.asarray(ele) ele = ele.reshape((100, 100)) return ele, matrix_A
def sww2obj(filename, size): """ Convert netcdf based data output to obj Convert SWW data to OBJ data. basefilename Stem of filename, needs size and extension added. size The number of lines to write. """ if filename[-4:] != '.sww': raise IOError('Output file %s should be of type .sww.' % sww_file) basefilename = filename[:-4] # Get NetCDF nc_fname = create_filename('.', basefilename, 'sww', size) log.critical('Reading from %s' % nc_fname) fid = NetCDFFile(nc_fname, netcdf_mode_r) #Open existing file for read # Get the variables x = fid.variables['x'] y = fid.variables['y'] z = fid.variables['elevation'] time = fid.variables['time'] stage = fid.variables['stage'] M = size #Number of lines xx = num.zeros((M, 3), num.float) yy = num.zeros((M, 3), num.float) zz = num.zeros((M, 3), num.float) for i in range(M): for j in range(3): xx[i, j] = x[i + j * M] yy[i, j] = y[i + j * M] zz[i, j] = z[i + j * M] # Write obj for bathymetry FN = create_filename('.', basefilename, 'obj', size) write_obj(FN, xx, yy, zz) # Now read all the data with variable information, combine with # x,y info and store as obj for k in range(len(time)): t = time[k] log.critical('Processing timestep %f' % t) for i in range(M): for j in range(3): zz[i, j] = stage[k, i + j * M] #Write obj for variable data #FN = create_filename(basefilename, 'obj', size, time=t) FN = create_filename('.', basefilename[:5], 'obj', size, time=t) write_obj(FN, xx, yy, zz)
def test_sww_variable(self): """Test that sww information can be written correctly """ self.domain.set_name('datatest' + str(id(self))) self.domain.format = 'sww' self.domain.smooth = True self.domain.reduction = mean sww = SWW_file(self.domain) sww.store_connectivity() sww.store_timestep() # Check contents # Get NetCDF fid = NetCDFFile(sww.filename, netcdf_mode_r) # Open existing file for append # Get the variables time = fid.variables['time'] stage = fid.variables['stage'] Q = self.domain.quantities['stage'] Q0 = Q.vertex_values[:, 0] Q1 = Q.vertex_values[:, 1] Q2 = Q.vertex_values[:, 2] A = stage[0, :] # print stage[0,:] # print A[0], (Q2[0] + Q1[1])/2 # print A[1], (Q0[1] + Q1[3] + Q2[2])/3 # print A[2], Q0[3] # print A[3], (Q0[0] + Q1[5] + Q2[4])/3 assert num.allclose(A[0], old_div((Q2[0] + Q1[1]), 2), rtol=1.0e-5, atol=1.0e-5) assert num.allclose(A[1], old_div((Q0[1] + Q1[3] + Q2[2]), 3), rtol=1.0e-5, atol=1.0e-5) assert num.allclose(A[2], Q0[3]) assert num.allclose(A[3], old_div((Q0[0] + Q1[5] + Q2[4]), 3), rtol=1.0e-5, atol=1.0e-5) #Center point assert num.allclose(A[4], old_div((Q1[0] + Q2[1] + Q0[2] +\ Q0[5] + Q2[6] + Q1[7]),6), rtol=1.0e-5, atol=1.0e-5) fid.close() os.remove(sww.filename)
def test_ferret2sww_lat_longII(self): # Test that min lat long works #The test file has # LON = 150.66667, 150.83334, 151, 151.16667 # LAT = -34.5, -34.33333, -34.16667, -34 ; #Read from anuga.coordinate_transforms.redfearn import redfearn fid = NetCDFFile(self.test_MOST_file + '_ha.nc') first_value = fid.variables['HA'][:][0, 0, 0] fourth_value = fid.variables['HA'][:][0, 0, 3] fid.close() #Call conversion (with zero origin) #ferret2sww('small', verbose=False, # origin = (56, 0, 0)) ferret2sww(self.test_MOST_file, verbose=False, origin=(56, 0, 0), minlat=-34.4, maxlat=-34.2) #Work out the UTM coordinates for first point zone, e, n = redfearn(-34.5, 150.66667) #print zone, e, n #Read output file 'small.sww' #fid = NetCDFFile('small.sww') fid = NetCDFFile(self.test_MOST_file + '.sww') x = fid.variables['x'][:] y = fid.variables['y'][:] #Check that first coordinate is correctly represented assert 12 == len(x) fid.close() #Cleanup import os os.remove(self.test_MOST_file + '.sww')
def test_sww_range(self): """Test that constant sww information can be written correctly Use non-smooth to be able to compare to quantity values. """ self.domain.set_name('datatest' + str(id(self))) self.domain.format = 'sww' self.domain.set_store_vertices_uniquely(True) sww = SWW_file(self.domain) dqs = self.domain.get_quantity('stage') dqx = self.domain.get_quantity('xmomentum') dqy = self.domain.get_quantity('ymomentum') xmom_min = ymom_min = stage_min = sys.maxint xmom_max = ymom_max = stage_max = -stage_min for t in self.domain.evolve(yieldstep = 1, finaltime = 1): wmax = max(dqs.get_values().flatten()) if wmax > stage_max: stage_max = wmax wmin = min(dqs.get_values().flatten()) if wmin < stage_min: stage_min = wmin uhmax = max(dqx.get_values().flatten()) if uhmax > xmom_max: xmom_max = uhmax uhmin = min(dqx.get_values().flatten()) if uhmin < xmom_min: xmom_min = uhmin vhmax = max(dqy.get_values().flatten()) if vhmax > ymom_max: ymom_max = vhmax vhmin = min(dqy.get_values().flatten()) if vhmin < ymom_min: ymom_min = vhmin # Get NetCDF fid = NetCDFFile(sww.filename, netcdf_mode_r) # Open existing file for append # Get the variables range = fid.variables['stage_range'][:] assert num.allclose(range,[stage_min, stage_max]) range = fid.variables['xmomentum_range'][:] #print range assert num.allclose(range, [xmom_min, xmom_max]) range = fid.variables['ymomentum_range'][:] #print range assert num.allclose(range, [ymom_min, ymom_max]) fid.close() os.remove(sww.filename)
def setupGrid(self): fin = NetCDFFile(self.vis_source, 'r') nTri = fin.variables['volumes'].shape[0] insertNextCell = vtkCellArray.InsertNextCell insertCellPoint = vtkCellArray.InsertCellPoint for v in range(nTri): insertNextCell(self.vtk_cells, 3) for i in range(3): insertCellPoint(self.vtk_cells, fin.variables['volumes'][v][i]) fin.close()
def getQuantityDict(self): quantities = {} fin = NetCDFFile(self.vis_source, 'r') names = [ k for k in fin.variables.keys() if k != 'x' and k != 'y' and k != 'z' and k != 'time' and k != 'volumes' ] argss = [(name, len(fin.variables[name].shape) != 1) for name in names] fin.close() for args in argss: quantities[name] = self.getQuantityPoints(*args) return quantities
def test_sww_minimum_storable_height(self): """Test that sww information can be written correctly multiple timesteps using a different reduction operator (min) """ import time, os self.domain.set_name('datatest' + str(id(self))) self.domain.format = 'sww' self.domain.smooth = True self.domain.reduction = min self.domain.minimum_storable_height = 100 sww = SWW_file(self.domain) sww.store_connectivity() sww.store_timestep() #self.domain.tight_slope_limiters = 1 self.domain.evolve_to_end(finaltime = 0.01) sww.store_timestep() #Check contents #Get NetCDF fid = NetCDFFile(sww.filename, netcdf_mode_r) # Get the variables x = fid.variables['x'] y = fid.variables['y'] z = fid.variables['elevation'] time = fid.variables['time'] stage = fid.variables['stage'] xmomentum = fid.variables['xmomentum'] ymomentum = fid.variables['ymomentum'] #Check values Q = self.domain.quantities['stage'] Q0 = Q.vertex_values[:,0] Q1 = Q.vertex_values[:,1] Q2 = Q.vertex_values[:,2] A = stage[1,:] assert num.allclose(stage[1,:], z[:]) assert num.allclose(xmomentum, 0.0) assert num.allclose(ymomentum, 0.0) fid.close() #Cleanup os.remove(sww.filename)
def test_urs_ungridded_hole (self): #Zone: 50 #Easting: 240992.578 Northing: 7620442.472 #Latitude: -21 30 ' 0.00000 '' Longitude: 114 30 ' 0.00000 '' lat_long = [[-20.5, 114.5], [-20.6, 114.6], [-20.5, 115.], [-20.6, 115.], [-20.5, 115.5], [-20.6, 115.4], [-21., 114.5], [-21., 114.6], [-21., 115.5], [-21., 115.4], [-21.5, 114.5], [-21.4, 114.6], [-21.5, 115.], [-21.4, 115.], [-21.5, 115.5], [-21.4, 115.4] ] time_step_count = 6 time_step = 100 tide = 9000000 base_name, files = self.write_mux(lat_long, time_step_count, time_step) #Easting: 292110.784 Northing: 7676551.710 #Latitude: -21 0 ' 0.00000 '' Longitude: 115 0 ' 0.00000 '' urs_ungridded2sww(base_name, mean_stage=-240992.0, hole_points_UTM=[ 292110.784, 7676551.710 ]) # now I want to check the sww file ... sww_file = base_name + '.sww' #Let's interigate the sww file # Note, the sww info is not gridded. It is point data. fid = NetCDFFile(sww_file) number_of_volumes = fid.variables['volumes'] #print "number_of_volumes",len(number_of_volumes) assert num.allclose(16, len(number_of_volumes)) fid.close() self.delete_mux(files) #print "sww_file", sww_file os.remove(sww_file)
def prepare_timeboundary(filename, verbose=False): """Convert benchmark 2 time series to NetCDF tms file. This is a 'throw-away' code taylor made for files like 'Benchmark_2_input.txt' from the LWRU2 benchmark """ from anuga.file.netcdf import NetCDFFile if verbose: print 'Creating', filename # Read the ascii (.txt) version of this file fid = open(filename[:-4] + '.txt') # Skip first line line = fid.readline() # Read remaining lines lines = fid.readlines() fid.close() N = len(lines) T = num.zeros(N, num.float) #Time Q = num.zeros(N, num.float) #Values for i, line in enumerate(lines): fields = line.split() T[i] = float(fields[0]) Q[i] = float(fields[1]) # Create tms NetCDF file fid = NetCDFFile(filename, 'w') fid.institution = 'Geoscience Australia' fid.description = 'Input wave for Benchmark 2' fid.starttime = 0.0 fid.createDimension('number_of_timesteps', len(T)) fid.createVariable('time', netcdf_float, ('number_of_timesteps', )) fid.variables['time'][:] = T fid.createVariable('stage', netcdf_float, ('number_of_timesteps', )) fid.variables['stage'][:] = Q[:] fid.createVariable('xmomentum', netcdf_float, ('number_of_timesteps', )) fid.variables['xmomentum'][:] = 0.0 fid.createVariable('ymomentum', netcdf_float, ('number_of_timesteps', )) fid.variables['ymomentum'][:] = 0.0 fid.close()
def build_quantity_dict(self): quantities = {} fin = NetCDFFile(self.source, 'r') for q in filter( lambda n: n != 'x' and n != 'y' and n != 'z' and n != 'time' and n != 'volumes', fin.variables.keys()): if len(fin.variables[q].shape) == 1: # Not a time-varying quantity quantities[q] = num.ravel( num.array(fin.variables[q], num.float)) else: # Time-varying, get the current timestep data quantities[q] = num.array(fin.variables[q][self.frameNumber], num.float) fin.close() return quantities