def test_construction(self, sg_data, sg_topology): filename = sg_data[0] dataset = sg_data[1] grid_topology = sg_topology sg = Grid_S.from_netCDF(filename, dataset, grid_topology=grid_topology) assert sg.filename == filename sg2 = Grid_S.from_netCDF(filename) assert sg2.filename == filename sg3 = PyGrid.from_netCDF(filename, dataset, grid_topology=grid_topology) print sg3.shape assert sg == sg3 sg4 = PyGrid.from_netCDF(filename) print sg4.shape assert sg2 == sg4
def test_serialize(self, sg, sg_data, sg_topology): filename = sg_data[0] dataset = sg_data[1] grid_topology = sg_topology sg2 = Grid_S.from_netCDF(filename, dataset, grid_topology=grid_topology) print sg.serialize()['filename'] print sg2.serialize()['filename'] assert sg.serialize()['filename'] == sg2.serialize()['filename']
node_lon = np.array(([1, 3, 5], [1, 3, 5], [1, 3, 5])) node_lat = np.array(([1, 1, 1], [3, 3, 3], [5, 5, 5])) edge2_lon = np.array(([0, 2, 4, 6], [0, 2, 4, 6], [0, 2, 4, 6])) edge2_lat = np.array(([1, 1, 1, 1], [3, 3, 3, 3], [5, 5, 5, 5])) edge1_lon = np.array(([1, 3, 5], [1, 3, 5], [1, 3, 5], [1, 3, 5])) edge1_lat = np.array(([0, 0, 0], [2, 2, 2], [4, 4, 4], [6, 6, 6])) center_lon = np.array(([0, 2, 4, 6], [0, 2, 4, 6], [0, 2, 4, 6], [0, 2, 4, 6])) center_lat = np.array(([0, 0, 0, 0], [2, 2, 2, 2], [4, 4, 4, 4], [6, 6, 6, 6])) center_mask = np.zeros_like(center_lat).astype(np.bool) center_mask[0, 0] = True g = Grid_S(node_lon=node_lon, node_lat=node_lat, edge1_lon=edge1_lon, edge1_lat=edge1_lat, edge2_lon=edge2_lon, edge2_lat=edge2_lat, center_lon=center_lon, center_lat=center_lat, center_mask=center_mask) c_var = np.array(([0, 0, 0, 0], [0, 1, 2, 0], [0, 2, 1, 0], [0, 0, 0, 0])) e2_var = np.array(([1, 0, 0, 1], [0, 1, 2, 0], [0, 0, 0, 0])) e1_var = np.array(([1, 1, 0], [0, 1, 0], [0, 2, 0], [1, 1, 0])) n_var = np.array(([0, 1, 0], [1, 0, 1], [0, 1, 0])) c_var.setflags(write=False) e2_var.setflags(write=False) e1_var.setflags(write=False) n_var.setflags(write=False) #TODO: Redo this test in a less answer-dependent way.
def gen_vortex_3D(filename=None): x, y = np.mgrid[-30:30:61j, -30:30:61j] y = np.ascontiguousarray(y.T) x = np.ascontiguousarray(x.T) x_size = 61 y_size = 61 g = Grid_S(node_lon=x, node_lat=y) g.build_celltree() lin_nodes = g._cell_trees['node'][1] lin_faces = np.array([ np.array([([lx, lx + x_size + 1, lx + 1 ], [lx, lx + x_size, lx + x_size + 1]) for lx in range(0, x_size - 1, 1)]) + ly * x_size for ly in range(0, y_size - 1) ]) lin_faces = lin_faces.reshape(-1, 3) # y += np.sin(x) / 1 # x += np.sin(x) / 5 t0 = datetime(2001, 1, 1, 0, 0) tarr = [t0 + timedelta(hours=i) for i in range(0, 11)] angs = -np.arctan2(y, x) mag = np.sqrt(x**2 + y**2) vx = np.cos(angs) * mag vy = np.sin(angs) * mag vx = vx[np.newaxis, :] * 20 vy = vy[np.newaxis, :] * 20 vw = -0.001 d_scale = [1, 0.5, 0, -0.5, -1] t_scale = np.linspace(0, 1, 11) tvx = np.array([vx * t for t in t_scale]).squeeze() tvy = np.array([vy * t for t in t_scale]).squeeze() dvx = np.array([vx * s for s in d_scale]).squeeze() dvy = np.array([vy * s for s in d_scale]).squeeze() tdvx = np.array([dvx * t for t in t_scale]).squeeze() tdvy = np.array([dvy * t for t in t_scale]).squeeze() lin_vx = vx.reshape(-1) lin_vy = vy.reshape(-1) lin_tvx = np.array([lin_vx * t for t in t_scale]) lin_tvy = np.array([lin_vy * t for t in t_scale]) lin_dvx = np.array([lin_vx * s for s in d_scale]) lin_dvy = np.array([lin_vy * s for s in d_scale]) lin_tdvx = np.array([lin_dvx * t for t in t_scale]) lin_tdvy = np.array([lin_dvy * t for t in t_scale]) ds = None if filename is not None: ds = nc4.Dataset(filename, 'w', diskless=True, persist=True) ds.createDimension('y', y.shape[0]) ds.createDimension('x', x.shape[1]) ds.createDimension('time', len(tarr)) ds.createDimension('depth', len(d_scale)) ds.createVariable('x', 'f8', dimensions=('x', 'y')) ds['x'][:] = x ds.createVariable('y', 'f8', dimensions=('x', 'y')) ds['y'][:] = y ds.createVariable('time', 'f8', dimensions=('time')) ds['time'][:] = nc4.date2num(tarr, 'hours since {0}'.format(t0)) ds['time'].setncattr('units', 'hours since {0}'.format(t0)) ds.createVariable('vx', 'f8', dimensions=('x', 'y')) ds.createVariable('vy', 'f8', dimensions=('x', 'y')) ds['vx'][:] = vx ds['vy'][:] = vy ds.createVariable('tvx', 'f8', dimensions=('time', 'x', 'y')) ds.createVariable('tvy', 'f8', dimensions=('time', 'x', 'y')) ds['tvx'][:] = tvx ds['tvy'][:] = tvy ds.createVariable('dvx', 'f8', dimensions=('depth', 'x', 'y')) ds.createVariable('dvy', 'f8', dimensions=('depth', 'x', 'y')) ds['dvx'][:] = dvx ds['dvy'][:] = dvy ds.createVariable('tdvx', 'f8', dimensions=('time', 'depth', 'x', 'y')) ds.createVariable('tdvy', 'f8', dimensions=('time', 'depth', 'x', 'y')) ds['tdvx'][:] = tdvx ds['tdvy'][:] = tdvy for v in ds.variables: if 'v' in v: ds[v].units = 'm/s' ds.createDimension('nv', lin_nodes.shape[0]) ds.createDimension('nele', lin_faces.shape[0]) ds.createDimension('two', 2) ds.createDimension('three', 3) ds.createVariable('nodes', 'f8', dimensions=('nv', 'two')) ds.createVariable('faces', 'f8', dimensions=('nele', 'three')) ds.createVariable('lin_vx', 'f8', dimensions=('nv')) ds.createVariable('lin_vy', 'f8', dimensions=('nv')) ds.createVariable('lin_tvx', 'f8', dimensions=('time', 'nv')) ds.createVariable('lin_tvy', 'f8', dimensions=('time', 'nv')) ds.createVariable('lin_dvx', 'f8', dimensions=('depth', 'nv')) ds.createVariable('lin_dvy', 'f8', dimensions=('depth', 'nv')) ds.createVariable('lin_tdvx', 'f8', dimensions=('time', 'depth', 'nv')) ds.createVariable('lin_tdvy', 'f8', dimensions=('time', 'depth', 'nv')) for k, v in { 'nodes': lin_nodes, 'faces': lin_faces, 'lin_vx': lin_vx, 'lin_vy': lin_vy, 'lin_tvx': lin_tvx, 'lin_tvy': lin_tvy, 'lin_dvx': lin_dvx, 'lin_dvy': lin_dvy, 'lin_tdvx': lin_tdvx, 'lin_tdvy': lin_tdvy }.items(): ds[k][:] = v if 'lin' in k: ds[k].units = 'm/s' PyGrid._get_grid_type(ds, grid_topology={ 'node_lon': 'x', 'node_lat': 'y' }) PyGrid._get_grid_type(ds) ds.setncattr('grid_type', 'sgrid') if ds is not None: # Need to test the dataset... sgt = {'node_lon': 'x', 'node_lat': 'y'} _sg = PyGrid.from_netCDF(dataset=ds, grid_topology=sgt, grid_type='sgrid') _sgc1 = GridCurrent.from_netCDF(dataset=ds, varnames=['vx', 'vy'], grid_topology=sgt) _sgc2 = GridCurrent.from_netCDF(dataset=ds, varnames=['tvx', 'tvy'], grid_topology=sgt) _sgc3 = GridCurrent.from_netCDF(dataset=ds, varnames=['dvx', 'dvy'], grid_topology=sgt) _sgc4 = GridCurrent.from_netCDF(dataset=ds, varnames=['tdvx', 'tdvy'], grid_topology=sgt) ugt = {'nodes': 'nodes', 'faces': 'faces'} # ug = PyGrid_U(nodes=ds['nodes'][:], faces=ds['faces'][:]) _ugc1 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_vx', 'lin_vy'], grid_topology=ugt) _ugc2 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_tvx', 'lin_tvy'], grid_topology=ugt) _ugc3 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_dvx', 'lin_dvy'], grid_topology=ugt) _ugc4 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_tdvx', 'lin_tdvy'], grid_topology=ugt) ds.close() return { 'sgrid': (x, y), 'sgrid_vel': (dvx, dvy), 'sgrid_depth_vel': (tdvx, tdvy), 'ugrid': (lin_nodes, lin_faces), 'ugrid_depth_vel': (lin_tdvx, lin_tdvy) }
def test_deserialize(self, sg): d_sg = Grid_S.deserialize(sg.serialize()) pp.pprint(sg.serialize()) pp.pprint(d_sg.serialize()) assert sg == d_sg
SimpleMover, PyCurrentMover) from gnome.outputters import Renderer from gnome.outputters import NetCDFOutput from gnome.tamoc import tamoc_spill # define base directory base_dir = os.path.dirname(__file__) x, y = np.mgrid[-30:30:61j, -30:30:61j] y = np.ascontiguousarray(y.T) x = np.ascontiguousarray(x.T) # y += np.sin(x) / 1 # x += np.sin(x) / 5 g = Grid_S(node_lon=x, node_lat=y) g.build_celltree() t = Time.constant_time() angs = -np.arctan2(y, x) mag = np.sqrt(x ** 2 + y ** 2) vx = np.cos(angs) * mag vy = np.sin(angs) * mag vx = vx[np.newaxis, :] * 5 vy = vy[np.newaxis, :] * 5 vels_x = Variable(name='v_x', units='m/s', time=t, grid=g, data=vx) vels_y = Variable(name='v_y', units='m/s', time=t, grid=g, data=vy) vg = GridCurrent(variables=[vels_y, vels_x], time=t, grid=g, units='m/s') def make_model(images_dir=os.path.join(base_dir, 'images')):
def gen_vortex_3D(filename=None): x, y = np.mgrid[-30:30:61j, -30:30:61j] y = np.ascontiguousarray(y.T) x = np.ascontiguousarray(x.T) x_size = 61 y_size = 61 g = Grid_S(node_lon=x, node_lat=y) g.build_celltree() lin_nodes = g._cell_trees['node'][1] lin_faces = np.array([np.array([([lx, lx + x_size + 1, lx + 1], [lx, lx + x_size, lx + x_size + 1]) for lx in range(0, x_size - 1, 1)]) + ly * x_size for ly in range(0, y_size - 1)]) lin_faces = lin_faces.reshape(-1, 3) # y += np.sin(x) / 1 # x += np.sin(x) / 5 t0 = datetime(2001, 1, 1, 0, 0) tarr = [t0 + timedelta(hours=i) for i in range(0, 11)] angs = -np.arctan2(y, x) mag = np.sqrt(x ** 2 + y ** 2) vx = np.cos(angs) * mag vy = np.sin(angs) * mag vx = vx[np.newaxis, :] * 20 vy = vy[np.newaxis, :] * 20 vw = -0.001 d_scale = [1, 0.5, 0, -0.5, -1] t_scale = np.linspace(0, 1, 11) tvx = np.array([vx * t for t in t_scale]).squeeze() tvy = np.array([vy * t for t in t_scale]).squeeze() dvx = np.array([vx * s for s in d_scale]).squeeze() dvy = np.array([vy * s for s in d_scale]).squeeze() tdvx = np.array([dvx * t for t in t_scale]).squeeze() tdvy = np.array([dvy * t for t in t_scale]).squeeze() lin_vx = vx.reshape(-1) lin_vy = vy.reshape(-1) lin_tvx = np.array([lin_vx * t for t in t_scale]) lin_tvy = np.array([lin_vy * t for t in t_scale]) lin_dvx = np.array([lin_vx * s for s in d_scale]) lin_dvy = np.array([lin_vy * s for s in d_scale]) lin_tdvx = np.array([lin_dvx * t for t in t_scale]) lin_tdvy = np.array([lin_dvy * t for t in t_scale]) ds = None if filename is not None: ds = nc4.Dataset(filename, 'w', diskless=True, persist=True) ds.createDimension('y', y.shape[0]) ds.createDimension('x', x.shape[1]) ds.createDimension('time', len(tarr)) ds.createDimension('depth', len(d_scale)) ds.createVariable('x', 'f8', dimensions=('x', 'y')) ds['x'][:] = x ds.createVariable('y', 'f8', dimensions=('x', 'y')) ds['y'][:] = y ds.createVariable('time', 'f8', dimensions=('time')) ds['time'][:] = nc4.date2num(tarr, 'hours since {0}'.format(t0)) ds['time'].setncattr('units', 'hours since {0}'.format(t0)) ds.createVariable('vx', 'f8', dimensions=('x', 'y')) ds.createVariable('vy', 'f8', dimensions=('x', 'y')) ds['vx'][:] = vx ds['vy'][:] = vy ds.createVariable('tvx', 'f8', dimensions=('time', 'x', 'y')) ds.createVariable('tvy', 'f8', dimensions=('time', 'x', 'y')) ds['tvx'][:] = tvx ds['tvy'][:] = tvy ds.createVariable('dvx', 'f8', dimensions=('depth', 'x', 'y')) ds.createVariable('dvy', 'f8', dimensions=('depth', 'x', 'y')) ds['dvx'][:] = dvx ds['dvy'][:] = dvy ds.createVariable('tdvx', 'f8', dimensions=('time', 'depth', 'x', 'y')) ds.createVariable('tdvy', 'f8', dimensions=('time', 'depth', 'x', 'y')) ds['tdvx'][:] = tdvx ds['tdvy'][:] = tdvy for v in ds.variables: if 'v' in v: ds[v].units = 'm/s' ds.createDimension('nv', lin_nodes.shape[0]) ds.createDimension('nele', lin_faces.shape[0]) ds.createDimension('two', 2) ds.createDimension('three', 3) ds.createVariable('nodes', 'f8', dimensions=('nv', 'two')) ds.createVariable('faces', 'f8', dimensions=('nele', 'three')) ds.createVariable('lin_vx', 'f8', dimensions=('nv')) ds.createVariable('lin_vy', 'f8', dimensions=('nv')) ds.createVariable('lin_tvx', 'f8', dimensions=('time', 'nv')) ds.createVariable('lin_tvy', 'f8', dimensions=('time', 'nv')) ds.createVariable('lin_dvx', 'f8', dimensions=('depth', 'nv')) ds.createVariable('lin_dvy', 'f8', dimensions=('depth', 'nv')) ds.createVariable('lin_tdvx', 'f8', dimensions=('time', 'depth', 'nv')) ds.createVariable('lin_tdvy', 'f8', dimensions=('time', 'depth', 'nv')) for k, v in {'nodes': lin_nodes, 'faces': lin_faces, 'lin_vx': lin_vx, 'lin_vy': lin_vy, 'lin_tvx': lin_tvx, 'lin_tvy': lin_tvy, 'lin_dvx': lin_dvx, 'lin_dvy': lin_dvy, 'lin_tdvx': lin_tdvx, 'lin_tdvy': lin_tdvy }.items(): ds[k][:] = v if 'lin' in k: ds[k].units = 'm/s' PyGrid._get_grid_type(ds, grid_topology={'node_lon': 'x', 'node_lat': 'y'}) PyGrid._get_grid_type(ds) ds.setncattr('grid_type', 'sgrid') if ds is not None: # Need to test the dataset... sgt = {'node_lon': 'x', 'node_lat': 'y'} _sg = PyGrid.from_netCDF(dataset=ds, grid_topology=sgt, grid_type='sgrid') _sgc1 = GridCurrent.from_netCDF(dataset=ds, varnames=['vx', 'vy'], grid_topology=sgt) _sgc2 = GridCurrent.from_netCDF(dataset=ds, varnames=['tvx', 'tvy'], grid_topology=sgt) _sgc3 = GridCurrent.from_netCDF(dataset=ds, varnames=['dvx', 'dvy'], grid_topology=sgt) _sgc4 = GridCurrent.from_netCDF(dataset=ds, varnames=['tdvx', 'tdvy'], grid_topology=sgt) ugt = {'nodes': 'nodes', 'faces': 'faces'} # ug = PyGrid_U(nodes=ds['nodes'][:], faces=ds['faces'][:]) _ugc1 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_vx', 'lin_vy'], grid_topology=ugt) _ugc2 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_tvx', 'lin_tvy'], grid_topology=ugt) _ugc3 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_dvx', 'lin_dvy'], grid_topology=ugt) _ugc4 = GridCurrent.from_netCDF(dataset=ds, varnames=['lin_tdvx', 'lin_tdvy'], grid_topology=ugt) ds.close() return {'sgrid': (x, y), 'sgrid_vel': (dvx, dvy), 'sgrid_depth_vel': (tdvx, tdvy), 'ugrid': (lin_nodes, lin_faces), 'ugrid_depth_vel': (lin_tdvx, lin_tdvy)}