def test_at(self): u = Variable(grid=g, data=e1_var) v = Variable(grid=g, data=e2_var) gw = GridWind(name='test', grid=g, variables=[u, v]) pts_arr = ( [1, 1], # 1 [1, 1, 3], # 2 [[2, 2], [4, 4]], # 3 [[2, 4], [2, 4]], # 4 [[1.5, 1.5], [2, 2], [3, 3], [3.5, 3.5]], # 5 [ [1.5, 2, 3, 3.5], # 6 [1.5, 2, 3, 3.5] ], ( (1.5, 2, 3, 3.5), # 7 (1.5, 2, 3, 3.5), (1, 0, 0, 2))) ans_arr = (np.array([[0.5, 0.5, 0]]), np.array([[0, 0, 0]]), np.array([[0.5, 0.5, 0], [1, 1, 0]]), np.array([[1, 0.5, 0], [1, 0.5, 0]]), np.array([[0.4375, 0.375, 0], [0.5, 0.5, 0], [1.5, 1.5, 0], [1.3125, 1.3125, 0]]), np.array([[0.4375, 0.5, 1.5, 1.3125], [0.375, 0.5, 1.5, 1.3125], [0, 0, 0, 0]]), np.array([[0, 0.5, 1.5, 0], [0, 0.5, 1.5, 0], [0, 0, 0, 0]])) for pts, ans in zip(pts_arr, ans_arr): result = gw.at(pts, datetime.now()) assert np.allclose(result, ans)
def test_at_format(self, coord_sys): u = Variable(grid=g, data=e1_var) v = Variable(grid=g, data=e2_var) gw = GridWind(name='test', grid=g, variables=[u, v]) pts_arr = ( [1, 1], # 1 [1, 1, 3], # 2 [[2, 2], [4, 4]], # 3 [[2, 4], [2, 4]], # 4 [[1.5, 1.5], [2, 2], [3, 3], [3.5, 3.5]], # 5 [ [1.5, 2, 3, 3.5], # 6 [1.5, 2, 3, 3.5] ], ( (1.5, 2, 3, 3.5), # 7 (1.5, 2, 3, 3.5), (1, 0, 0, 2))) ans_arr = (np.array([ [0.5, 0.5, 0], ]), np.array([ [0, 0, 0], ]), np.array([[0.5, 0.5, 0], [1, 1, 0]]), np.array([[1, 0.5, 0], [1, 0.5, 0]]), np.array([[0.4375, 0.375, 0], [0.5, 0.5, 0], [1.5, 1.5, 0], [1.3125, 1.3125, 0]]), np.array([[0.4375, 0.5, 1.5, 1.3125], [0.375, 0.5, 1.5, 1.3125], [0, 0, 0, 0]]).T, np.array([[0, 0.5, 1.5, 0], [0, 0.5, 1.5, 0], [0, 0, 0, 0]]).T) for pts, ans in zip(pts_arr, ans_arr): raw_result = gw.at(pts, datetime.now(), coord_sys=coord_sys, _auto_align=False) ans_mag = np.sqrt(ans[:, 0]**2 + ans[:, 1]**2) print 'ans_mag', ans_mag print ans_dir = np.arctan2(ans[:, 1], ans[:, 0]) * 180. / np.pi if coord_sys in ('r-theta', 'r', 'theta'): if coord_sys == 'r': assert np.allclose(raw_result, ans_mag) elif coord_sys == 'theta': assert np.allclose(raw_result, ans_dir) else: assert np.allclose(raw_result, np.column_stack((ans_mag, ans_dir))) else: if coord_sys == 'u': assert np.allclose(raw_result, ans[:, 0]) else: assert np.allclose(raw_result, ans[:, 1])
def test_init(self): u = Variable(grid=g, data=e1_var) v = Variable(grid=g, data=e2_var) gw = GridWind(name='test', grid=g, variables=[u, v]) assert gw is not None assert gw.u is u assert gw.variables[0] is u assert gw.variables[1] is v assert np.all(gw.grid.node_lon == node_lon)
def test_construction(self): curr_file = os.path.join(s_data, 'staggered_sine_channel.nc') u = Variable.from_netCDF(filename=curr_file, varname='u_rho') v = Variable.from_netCDF(filename=curr_file, varname='v_rho') gvp = VectorVariable(name='velocity', units='m/s', time=u.time, variables=[u, v]) assert gvp.name == 'velocity' assert gvp.units == 'm/s' assert gvp.varnames[0] == 'u_rho'
def test_at(self): curr_file = os.path.join(s_data, 'staggered_sine_channel.nc') u = Variable.from_netCDF(filename=curr_file, varname='u_rho') v = Variable.from_netCDF(filename=curr_file, varname='v_rho') points = np.array(([0, 0, 0], [np.pi, 1, 0], [2 * np.pi, 0, 0])) time = dt.datetime.now() assert all(u.at(points, time) == [1, 1, 1]) print np.cos(points[:, 0] / 2) / 2 assert all(np.isclose(v.at(points, time), np.cos(points[:, 0] / 2) / 2))
def test_construction(self): data = sinusoid['u'][:] grid = PyGrid.from_netCDF(dataset=sinusoid) time = None u = Variable(name='u', units='m/s', data=data, grid=grid, time=time, data_file='staggered_sine_channel.nc', grid_file='staggered_sine_channel.nc') curr_file = os.path.join(s_data, 'staggered_sine_channel.nc') k = Variable.from_netCDF(filename=curr_file, varname='u', name='u') assert k.name == u.name assert k.units == 'm/s' # fixme: this was failing # assert k.time == u.time assert k.data[0, 0] == u.data[0, 0]
def __init__(self, angle=None, **kwargs): """ :param angle: scalar field of cell rotation angles (for rotated/distorted grids) """ if 'variables' in kwargs: variables = kwargs['variables'] if len(variables) == 2: variables.append( TimeseriesData(name='constant w', data=[0.0], time=Time.constant_time(), units='m/s')) kwargs['variables'] = variables if angle is None: df = None if kwargs.get('dataset', None) is not None: df = kwargs['dataset'] elif kwargs.get('grid_file', None) is not None: df = gridded.utilities.get_dataset(kwargs['grid_file']) if df is not None and 'angle' in df.variables.keys(): # Unrotated ROMS Grid! self.angle = Variable(name='angle', units='radians', time=Time.constant_time(), grid=kwargs['grid'], data=df['angle']) else: self.angle = None else: self.angle = angle super(VelocityGrid, self).__init__(**kwargs)
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')): print 'initializing the model' # set up the modeling environment start_time = datetime(2004, 12, 31, 13, 0) model = Model(start_time=start_time, duration=timedelta(days=3), time_step=30 * 60, uncertain=False) print 'adding the map'