Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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_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')
Example #5
0
 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()
Example #6
0
    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_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!')
Example #8
0
    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 __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()
Example #10
0
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=",")
Example #11
0
    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)
Example #12
0
    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
Example #13
0
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)
Example #14
0
    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!')
Example #15
0
    def test_boundary_timeII_1_5(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)
        domain.set_flow_algorithm("1_5")
        new_starttime = 0.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.88601), msg

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #16
0
 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()
Example #17
0
 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()
Example #18
0
 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
    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)
Example #20
0
    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)
Example #21
0
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
Example #22
0
    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)
Example #23
0
    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()
Example #25
0
 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
Example #26
0
    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 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], (Q2[0] + Q1[1])/2, rtol=1.0e-5, atol=1.0e-5)
        assert num.allclose(A[1], (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], (Q0[0] + Q1[5] + Q2[4])/3, rtol=1.0e-5, atol=1.0e-5)

        #Center point
        assert num.allclose(A[4], (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)
Example #28
0
    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)
Example #29
0
 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
Example #30
0
    def test_sync(self):
        """test_sync - Test info stored at each timestep is as expected (incl initial condition)
        """

        import time, os

        self.domain.set_name('synctest')
        self.domain.format = 'sww'
        self.domain.smooth = False
        self.domain.store = True

        self.domain.tight_slope_limiters = True
        self.domain.use_centroid_velocities = True

        # In this case tight_slope_limiters as default
        # in conjunction with protection
        # against isolated degenerate timesteps works.
        #self.domain.tight_slope_limiters = 1
        #self.domain.protect_against_isolated_degenerate_timesteps = True

        #print 'tight_sl', self.domain.tight_slope_limiters

        #Evolution
        for t in self.domain.evolve(yieldstep=1.0, finaltime=4.0):

            #########self.domain.write_time(track_speeds=True)
            stage = self.domain.quantities['stage'].vertex_values

            #Get NetCDF
            fid = NetCDFFile(self.domain.writer.filename, netcdf_mode_r)
            stage_file = fid.variables['stage']

            if t == 0.0:
                assert num.allclose(stage,
                                    self.initial_stage,
                                    rtol=1.0e-5,
                                    atol=1.0e-5)
                assert num.allclose(stage_file[:],
                                    stage.flatten(),
                                    rtol=1.0e-5,
                                    atol=1.0e-5)
            else:
                assert not num.allclose(
                    stage, self.initial_stage, rtol=1.0e-5, atol=1.0e-5)
                assert not num.allclose(
                    stage_file[:], stage.flatten(), rtol=1.0e-5, atol=1.0e-5)

            fid.close()

        os.remove(self.domain.writer.filename)
Example #31
0
    def test_boundary_time(self):
        """
        test_boundary_time(self):
        test that the starttime of a boundary condition is carried thru
        to the output sww file.
        
        """

        boundary_starttime = 0
        boundary_filename = self.create_sww_boundary(boundary_starttime)
        filename = tempfile.mktemp(".sww")
        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.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(senario_name)
        domain.set_datadir(dir)

        # 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.0, finaltime=10.0):
            pass
            #print 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'][:]
        #print "times", times
        #print "fid.starttime", fid.starttime
        assert num.allclose(fid.starttime, boundary_starttime)
        fid.close()

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #32
0
    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 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
Example #34
0
    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)
Example #35
0
    def test_boundary_time(self):
        """
        test_boundary_time(self):
        test that the starttime of a boundary condition is carried thru
        to the output sww file.
        
        """

        boundary_starttime = 0
        boundary_filename = self.create_sww_boundary(boundary_starttime)
        filename = tempfile.mktemp(".sww")
        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.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]])
        mesh.generate_mesh(verbose=False)

        domain = pmesh_to_domain_instance(mesh, anuga.Domain)
        domain.set_name(senario_name)
        domain.set_datadir(dir)

        # 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.0, finaltime=10.0):
            pass
            # print 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"][:]
        # print "times", times
        # print "fid.starttime", fid.starttime
        assert num.allclose(fid.starttime, boundary_starttime)
        fid.close()

        # clean up
        os.remove(boundary_filename)
        os.remove(filename)
Example #36
0
    def test_small_nxn(self):
        most2nc(input_file=FN,output_file='test.nc'\
                        ,inverted_bathymetry = False,verbose = False)

        fid = NetCDFFile('test.nc')
        elevation = fid.variables['ELEVATION'][:]
        fid.close()

        z=[[-13., -14., -15., -16.]\
           ,[-9., -10., -11., -12.]\
           ,[-5.,  -6.,  -7.,  -8.]\
           ,[-1.,  -2.,  -3.,  -4.]]
        z = num.asarray(z)

        assert num.allclose(z, elevation)
        import os
        os.remove('test.nc')
Example #37
0
    def helper_write_msh_file(self, filename, l):
        # open the NetCDF file
        fd = NetCDFFile(filename, netcdf_mode_w)
        fd.description = 'Test file - string arrays'

        # convert list of strings to num.array
        al = num.array(string_to_char(l), num.character)

        # write the list
        fd.createDimension('num_of_strings', al.shape[0])
        fd.createDimension('size_of_strings', al.shape[1])

        var = fd.createVariable('strings', netcdf_char,
                                ('num_of_strings', 'size_of_strings'))
        var[:] = al

        fd.close()
Example #38
0
    def test_small_nxn(self):
        most2nc(input_file=FN,output_file='test.nc'\
                        ,inverted_bathymetry = False,verbose = False)

        fid = NetCDFFile('test.nc')
        elevation = fid.variables['ELEVATION'][:]
        fid.close()

        z=[[-13., -14., -15., -16.]\
           ,[-9., -10., -11., -12.]\
           ,[-5.,  -6.,  -7.,  -8.]\
           ,[-1.,  -2.,  -3.,  -4.]]
        z = num.asarray(z)

        assert num.allclose(z,elevation)
        import os
        os.remove('test.nc')
Example #39
0
    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_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], (Q2[0] + Q1[1])/2, rtol=1.0e-5, atol=1.0e-5)
        
        assert num.allclose(A[1], (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], (Q0[0] + Q1[5] + Q2[4])/3, rtol=1.0e-5, atol=1.0e-5)

        #Center point
        assert num.allclose(A[4], (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 getQuantityPoints(self, quantityName, dynamic=False):
     try:
         if dynamic:
             q = self.quantityCache[quantityName][self.vis_frame]
         else:
             q = self.quantityCache[quantityName]
     except KeyError:
         fin = NetCDFFile(self.vis_source, 'r')
         if dynamic:
             if not self.quantityCache.has_key(quantityName):
                 self.quantityCache[quantityName] = {}
             q = array(fin.variables[quantityName][self.vis_frame], Float)
             self.quantityCache[quantityName][self.vis_frame] = q
         else:
             q = array(fin.variables[quantityName], Float)
             self.quantityCache[quantityName] = q
         fin.close()
     return q
Example #42
0
 def getQuantityPoints(self, quantityName, dynamic=False):
     try:
         if dynamic:
             q = self.quantityCache[quantityName][self.vis_frame]
         else:
             q = self.quantityCache[quantityName]
     except KeyError:
         fin = NetCDFFile(self.vis_source, 'r')
         if dynamic:
             if quantityName not in self.quantityCache:
                 self.quantityCache[quantityName] = {}
             q = array(fin.variables[quantityName][self.vis_frame], Float)
             self.quantityCache[quantityName][self.vis_frame] = q
         else:
             q = array(fin.variables[quantityName], Float)
             self.quantityCache[quantityName] = q
         fin.close()
     return q
    def test_urs_ungridded2sww_mint_maxtII(self):

        #Zone:   50
        #Easting:  240992.578  Northing: 7620442.472
        #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
        lat_long = [[-21.5, 114.5], [-21, 114.5], [-21, 115]]
        time_step_count = 6
        time_step = 100
        tide = 9000000
        base_name, files = self.write_mux(lat_long, time_step_count, time_step)
        urs_ungridded2sww(base_name,
                          mean_stage=tide,
                          origin=(50, 23432, 4343),
                          mint=0,
                          maxt=100000)

        # 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)

        # Make x and y absolute
        geo_reference = Geo_reference(NetCDFObject=fid)
        points = geo_reference.get_absolute(
            list(zip(fid.variables['x'][:], fid.variables['y'][:])))
        points = ensure_numeric(points)
        x = points[:, 0]

        #Check the time vector
        times = fid.variables['time'][:]

        times_actual = [0, 100, 200, 300, 400, 500]
        assert num.allclose(ensure_numeric(times),
                            ensure_numeric(times_actual))

        #Check first value
        stage = fid.variables['stage'][:]
        assert num.allclose(stage[0], x + tide)

        fid.close()
        self.delete_mux(files)
        os.remove(sww_file)
    def test_sync(self):
        """test_sync - Test info stored at each timestep is as expected (incl initial condition)
        """

        import time, os

        self.domain.set_name('synctest')
        self.domain.format = 'sww'
        self.domain.smooth = False
        self.domain.store = True

        self.domain.tight_slope_limiters = True
        self.domain.use_centroid_velocities = True        
        
        # In this case tight_slope_limiters as default
        # in conjunction with protection
        # against isolated degenerate timesteps works.
        #self.domain.tight_slope_limiters = 1
        #self.domain.protect_against_isolated_degenerate_timesteps = True

        #print 'tight_sl', self.domain.tight_slope_limiters
        

        #Evolution
        for t in self.domain.evolve(yieldstep = 1.0, finaltime = 4.0):
            
            #########self.domain.write_time(track_speeds=True)
            stage = self.domain.quantities['stage'].vertex_values

            #Get NetCDF
            fid = NetCDFFile(self.domain.writer.filename, netcdf_mode_r)
            stage_file = fid.variables['stage']
            
            if t == 0.0:
                assert num.allclose(stage, self.initial_stage, rtol=1.0e-5, atol=1.0e-5)
                assert num.allclose(stage_file[:], stage.flatten(),rtol=1.0e-5, atol=1.0e-5)
            else:
                assert not num.allclose(stage, self.initial_stage, rtol=1.0e-5, atol=1.0e-5)
                assert not num.allclose(stage_file[:], stage.flatten(), rtol=1.0e-5, atol=1.0e-5)

            fid.close()

        os.remove(self.domain.writer.filename)
 def test_urs_ungridded2sww_mint_maxtII (self):
     
     #Zone:   50    
     #Easting:  240992.578  Northing: 7620442.472 
     #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 '' 
     lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
     time_step_count = 6
     time_step = 100
     tide = 9000000
     base_name, files = self.write_mux(lat_long,
                                       time_step_count, time_step)
     urs_ungridded2sww(base_name, mean_stage=tide, origin =(50,23432,4343),
                       mint=0, maxt=100000)
     
     # 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)
     
     # Make x and y absolute
     geo_reference = Geo_reference(NetCDFObject=fid)
     points = geo_reference.get_absolute(map(None, fid.variables['x'][:],
                                             fid.variables['y'][:]))
     points = ensure_numeric(points)
     x = points[:,0]
     
     #Check the time vector
     times = fid.variables['time'][:]
     
     times_actual = [0,100,200,300,400,500]
     assert num.allclose(ensure_numeric(times),
                         ensure_numeric(times_actual))
     
     #Check first value
     stage = fid.variables['stage'][:]
     assert num.allclose(stage[0], x +tide)
     
     fid.close()
     self.delete_mux(files)
     os.remove(sww_file)
Example #46
0
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)
Example #47
0
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 helper_write_msh_file(self, filename, l):
        # open the NetCDF file
        fd = NetCDFFile(filename, netcdf_mode_w)
        fd.description = 'Test file - string arrays'

        # convert list of strings to num.array
        al = num.array(string_to_char(l), num.character)

        # write the list
        fd.createDimension('num_of_strings', al.shape[0])
        fd.createDimension('size_of_strings', al.shape[1])

        var = fd.createVariable('strings', netcdf_char,
                                ('num_of_strings', 'size_of_strings'))
        var[:] = al

        fd.close()
    def test_triangulation_points_georeference(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
        points_georeference = Geo_reference(56, 1, 554354)
        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 == points_georeference
        fid.close()

        assert num.allclose(num.array(map(None, x,y)), points_utm)
        os.remove(filename)
    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], test_value/100)

        fid.close()

        #Cleanup
        import os
        os.remove(self.test_MOST_file + '.sww')
    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')
Example #52
0
def csv2sts(infile, outfile, latitude = None, longitude = None,
                    verbose = False):
    """
        Take a csv file and convert it to an sts file.
        
        May be used for timeseries, or any other data.
    """
        
    timeseries_data, col_names = load_csv_as_dict(infile, delimiter=' ')
    
    if not col_names:
        raise IOError('csv2sts: file %s is empty or unreadable.' % infile)
    
    if verbose:
        log.critical('csv2sts input data:')
        for col in col_names:
            log.critical('column ' + col + ':')
            log.critical(timeseries_data[col])        

    data_len = len(timeseries_data.values()[0])
    if verbose:
        log.critical('   data length = %d.' % data_len)
    
    fid = NetCDFFile(outfile, netcdf_mode_w)

    fid.createDimension('number_of_timesteps', data_len)

    if latitude:
        fid.latitude = latitude
        
    if longitude:
        fid.longitude = longitude
    
    for col in col_names:
        fid.createVariable(col, netcdf_float, ('number_of_timesteps',))
        
        fid.variables[col][:] = timeseries_data[col]

    fid.close()
Example #53
0
    def test_sww_constant_smooth(self):
        """Test that constant sww information can be written correctly
        (non smooth)
        """
        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True

        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
        X = fid.variables['x'][:]
        Y = fid.variables['y'][:]
        Z = fid.variables['elevation'][:]
        V = fid.variables['volumes']

        assert num.allclose([X[0], Y[0]], num.array([0.0, 0.0]))
        assert num.allclose([X[1], Y[1]], num.array([0.0, 0.5]))
        assert num.allclose([X[2], Y[2]], num.array([0.0, 1.0]))
        assert num.allclose([X[4], Y[4]], num.array([0.5, 0.5]))
        assert num.allclose([X[7], Y[7]], num.array([1.0, 0.5]))

        assert Z[4] == -0.5

        assert V[2, 0] == 4
        assert V[2, 1] == 5
        assert V[2, 2] == 1
        assert V[4, 0] == 6
        assert V[4, 1] == 7
        assert V[4, 2] == 3

        fid.close()
        os.remove(sww.filename)
    def test_sww_constant_smooth(self):
        """Test that constant sww information can be written correctly
        (non smooth)
        """
        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True

        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
        X = fid.variables['x'][:]
        Y = fid.variables['y'][:]
        Z = fid.variables['elevation'][:]
        V = fid.variables['volumes']

        assert num.allclose([X[0], Y[0]], num.array([0.0, 0.0]))
        assert num.allclose([X[1], Y[1]], num.array([0.0, 0.5]))
        assert num.allclose([X[2], Y[2]], num.array([0.0, 1.0]))
        assert num.allclose([X[4], Y[4]], num.array([0.5, 0.5]))
        assert num.allclose([X[7], Y[7]], num.array([1.0, 0.5]))

        assert Z[4] == -0.5

        assert V[2,0] == 4
        assert V[2,1] == 5
        assert V[2,2] == 1
        assert V[4,0] == 6
        assert V[4,1] == 7
        assert V[4,2] == 3

        fid.close()
        os.remove(sww.filename)
    def test_georef_types(self):
        '''Ensure that attributes of a georeference are of correct type.
        
        zone            int
        false_easting   int
        false_northing  int
        xllcorner       float
        yllcorner       float
        '''

        from anuga.file.netcdf import NetCDFFile

        # ensure that basic instance attributes are correct
        g = Geo_reference(56, 1.8, 1.8)
        self.assertTrue(isinstance(g.zone, int),
                        "geo_ref .zone should be 'int' type, "
                        "was '%s' type" % type(g.zone))  
        self.assertTrue(isinstance(g.false_easting, int),
                        "geo_ref .false_easting should be int type, "
                        "was '%s' type" % type(g.false_easting))  
        self.assertTrue(isinstance(g.false_northing, int),
                        "geo_ref .false_northing should be int type, "
                        "was '%s' type" % type(g.false_northing))
        self.assertTrue(isinstance(g.xllcorner, float),
                        "geo_ref .xllcorner should be float type, "
                        "was '%s' type" % type(g.xllcorner))
        self.assertTrue(isinstance(g.yllcorner, float),
                        "geo_ref .yllcorner should be float type, "
                        "was '%s' type" % type(g.yllcorner))

        # now write fikle, read back and check types again
        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(isinstance(new_g.zone, int),
                        "geo_ref .zone should be 'int' type, "
                        "was '%s' type" % type(new_g.zone))  
        self.assertTrue(isinstance(new_g.false_easting, int),
                        "geo_ref .false_easting should be int type, "
                        "was '%s' type" % type(new_g.false_easting))  
        self.assertTrue(isinstance(new_g.false_northing, int),
                        "geo_ref .false_northing should be int type, "
                        "was '%s' type" % type(new_g.false_northing))
        self.assertTrue(isinstance(new_g.xllcorner, float),
                        "geo_ref .xllcorner should be float type, "
                        "was '%s' type" % type(new_g.xllcorner))
        self.assertTrue(isinstance(new_g.yllcorner, float),
                        "geo_ref .yllcorner should be float type, "
                        "was '%s' type" % type(new_g.yllcorner))