def ice_field(filename=None): gridset = None dataset = None dataset = nc4.Dataset(filename) time = Time(dataset['time']) w_u = pysgrid.variables.SGridVariable(data=dataset['water_u']) w_v = pysgrid.variables.SGridVariable(data=dataset['water_v']) i_u = pysgrid.variables.SGridVariable(data=dataset['ice_u']) i_v = pysgrid.variables.SGridVariable(data=dataset['ice_v']) a_u = pysgrid.variables.SGridVariable(data=dataset['air_u']) a_v = pysgrid.variables.SGridVariable(data=dataset['air_v']) i_thickness = pysgrid.variables.SGridVariable( data=dataset['ice_thickness']) i_coverage = pysgrid.variables.SGridVariable(data=dataset['ice_fraction']) grid = pysgrid.SGrid(node_lon=dataset['lon'], node_lat=dataset['lat']) ice_vars = {'u': i_u, 'v': i_v, 'thickness': i_thickness, 'coverage': i_coverage} water_vars = {'u': w_u, 'v': w_v, } air_vars = {'u': a_u, 'v': a_v} dims = grid.node_lon.shape icefield = SField(grid, time=time, variables=ice_vars, dimensions=dims) waterfield = SField(grid, time=time, variables=water_vars, dimensions=dims) airfield = SField(grid, time=time, variables=air_vars, dimensions=dims) return (icefield, waterfield, airfield)
def curv_field(filename=None, dataset=None): if dataset is None: dataset = nc4.Dataset(filename) node_lon = dataset['lonc'] node_lat = dataset['latc'] u = dataset['water_u'] v = dataset['water_v'] dims = node_lon.dimensions[0] + ' ' + node_lon.dimensions[1] grid = pysgrid.SGrid(node_lon=node_lon, node_lat=node_lat, node_dimensions=dims) grid.u = pysgrid.variables.SGridVariable(data=u) grid.v = pysgrid.variables.SGridVariable(data=v) time = Time(dataset['time']) variables = {'u': grid.u, 'v': grid.v, 'time': time} return SField(grid, time=time, variables=variables)
import pysgrid import numpy as np 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])) sgrid = pysgrid.SGrid(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) 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])) ptsx, ptsy = np.mgrid[0:6:600j, 0:6:600j] pts = np.stack((ptsx, ptsy), axis=-1) interp_c = sgrid.interpolate_var_to_points(pts, c_var).reshape(600, 600) interp_e1 = sgrid.interpolate_var_to_points(pts, e1_var).reshape(600, 600).T interp_e2 = sgrid.interpolate_var_to_points(pts, e2_var).reshape(600, 600).T
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' # set up the modeling environment start_time = datetime(2016, 9, 23, 0, 0) model = Model(start_time=start_time, duration=timedelta(days=2), time_step=30 * 60, uncertain=False) print 'adding the map' model.map = GnomeMap() # this is a "water world -- no land anywhere" # renderere is only top-down view on 2d -- but it's something renderer = Renderer( output_dir=images_dir, size=(1024, 768), output_timestep=timedelta(hours=1), ) renderer.viewport = ((196.14, 71.89), (196.18, 71.93)) print 'adding outputters' model.outputters += renderer # Also going to write the results out to a netcdf file netcdf_file = os.path.join(base_dir, 'script_arctic_plume.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput( netcdf_file, which_data='most', # output most of the data associated with the elements output_timestep=timedelta(hours=2)) print "adding Horizontal and Vertical diffusion" # Horizontal Diffusion model.movers += RandomMover(diffusion_coef=500) # vertical diffusion (different above and below the mixed layer) model.movers += RandomVerticalMover(vertical_diffusion_coef_above_ml=5, vertical_diffusion_coef_below_ml=.11, mixed_layer_depth=10) print 'adding Rise Velocity' # droplets rise as a function of their density and radius model.movers += TamocRiseVelocityMover() print 'adding a circular current and eastward current' fn = 'hycom_glb_regp17_2016092300_subset.nc' fn_ice = 'hycom-cice_ARCu0.08_046_2016092300_subset.nc' import pysgrid import netCDF4 as nc df = nc.Dataset(fn) lon = df['lon'][:] lat = df['lat'][:] grd = pysgrid.SGrid(node_lon=np.repeat(lon.reshape(1, -1), len(lat), axis=0), node_lat=np.repeat(lat.reshape(-1, 1), len(lon), axis=1)) print(grd.node_lon.shape) print(grd.node_lat.shape) gc = GridCurrent.from_netCDF(fn, units='m/s', grid=grd) model.movers += IceMover(fn_ice) model.movers += GridCurrentMover(fn) model.movers += SimpleMover(velocity=(0., 0., 0.)) model.movers += constant_wind_mover(20, 315, units='knots') # Now to add in the TAMOC "spill" print "Adding TAMOC spill" model.spills += tamoc_spill.TamocSpill( release_time=start_time, start_position=(196.16, 71.91, 40.0), num_elements=1000, end_release_time=start_time + timedelta(days=1), name='TAMOC plume', TAMOC_interval=None, # how often to re-run TAMOC ) model.spills[0].data_sources['currents'] = gc return model
def init_grid(filename, grid_topology=None, dataset=None,): gt = grid_topology gf = dataset if gf is None: gf = _get_dataset(filename) grid = None if gt is None: try: grid = pyugrid.UGrid.from_nc_dataset(gf) except (ValueError, NameError): pass try: grid = pysgrid.SGrid.load_grid(gf) except (ValueError, NameError): gt = _gen_topology(filename) if grid is None: nodes = node_lon = node_lat = None if 'nodes' not in gt: if 'node_lon' not in gt and 'node_lat' not in gt: raise ValueError('Nodes must be specified with either the "nodes" or "node_lon" and "node_lat" keys') node_lon = gf[gt['node_lon']] node_lat = gf[gt['node_lat']] else: nodes = gf[gt['nodes']] if 'faces' in gt and gf[gt['faces']]: # UGrid faces = gf[gt['faces']] if faces.shape[0] == 3: faces = np.ascontiguousarray(np.array(faces).T - 1) if nodes is None: nodes = np.column_stack((node_lon, node_lat)) grid = pyugrid.UGrid(nodes=nodes, faces=faces) else: # SGrid center_lon = center_lat = edge1_lon = edge1_lat = edge2_lon = edge2_lat = None if node_lon is None: node_lon = nodes[:, 0] if node_lat is None: node_lat = nodes[:, 1] if 'center_lon' in gt: center_lon = gf[gt['center_lon']] if 'center_lat' in gt: center_lat = gf[gt['center_lat']] if 'edge1_lon' in gt: edge1_lon = gf[gt['edge1_lon']] if 'edge1_lat' in gt: edge1_lat = gf[gt['edge1_lat']] if 'edge2_lon' in gt: edge2_lon = gf[gt['edge2_lon']] if 'edge2_lat' in gt: edge2_lat = gf[gt['edge2_lat']] grid = pysgrid.SGrid(node_lon=node_lon, node_lat=node_lat, center_lon=center_lon, center_lat=center_lat, edge1_lon=edge1_lon, edge1_lat=edge1_lat, edge2_lon=edge2_lon, edge2_lat=edge2_lat) return grid
base_dir = os.path.dirname(__file__) ''' Need to hook this up to existing test data infrastructure ''' s_data = os.path.join(base_dir, 'sample_data') curr_dir = os.path.join(s_data, 'currents') curr_file = get_datafile(os.path.join(curr_dir, 'tbofs_example.nc')) dataset = nc.Dataset(curr_file) node_lon = dataset['lonc'] node_lat = dataset['latc'] grid_u = dataset['water_u'] grid_v = dataset['water_v'] grid_time = dataset['time'] test_grid = pysgrid.SGrid(node_lon=node_lon, node_lat=node_lat) dates = np.array([ dt.datetime(2000, 1, 1, 0), dt.datetime(2000, 1, 1, 2), dt.datetime(2000, 1, 1, 4) ]) dates2 = np.array([ dt.datetime(2000, 1, 1, 0), dt.datetime(2000, 1, 1, 2), dt.datetime(2000, 1, 1, 4), dt.datetime(2000, 1, 1, 6), dt.datetime(2000, 1, 1, 8), ]) uv_units = 'm/s' u_data = np.array([2., 4., 6., 8., 10.])
nc = nc4.Dataset(url) timeobj = sgrid = None if ('ocean_time' in nc.variables.keys()): timeobj = Time(nc['ocean_time']) else: timeobj = Time(nc['time']) if 'grid' in nc.variables.keys(): sgrid = pysgrid.load_grid(nc) else: sgrid = pysgrid.SGrid(node_lon=nc['lon_psi'], node_lat=nc['lat_psi'], edge1_lon=nc['lon_u'], edge1_lat=nc['lat_u'], edge2_lon=nc['lon_v'], edge2_lat=nc['lat_v'], ) sgrid.u = pysgrid.variables.SGridVariable(data=nc['u']) sgrid.v = pysgrid.variables.SGridVariable(data=nc['v']) sgrid.angles = pysgrid.variables.SGridVariable(data=nc['angle']) points = np.stack((lons, lats), axis=-1).reshape(-1, 2) ind = sgrid.locate_faces(points) ang_ind = ind + [1, 1] angles = sgrid.angles[:][ang_ind[:, 0], ang_ind[:, 1]]