Exemplo n.º 1
0
 def grid_detection_report(filename):
     from gnome.environment.grid import PyGrid
     topo = PyGrid._find_topology_var(filename)
     report = ['Grid report:']
     if topo is None:
         report.append(
             '    A standard grid topology was not found in the file')
         report.append('    topology breakdown future feature')
     else:
         report.append(
             '    A grid topology was found in the file: {0}'.format(topo))
     return report
Exemplo n.º 2
0
def grid_detection_report(filename):
    from gnome.environment.gridded_objects_base import PyGrid

    topo = PyGrid._find_topology_var(filename)
    report = ['Grid report:']

    if topo is None:
        report.append('    A standard grid topology was not found in the file')
        report.append('    topology breakdown future feature')
    else:
        report.append('    A grid topology was found in the file: {0}'
                      .format(topo))

    return report
Exemplo n.º 3
0
def env_from_netCDF(filename=None,
                    dataset=None,
                    grid_file=None,
                    data_file=None,
                    _cls_list=None,
                    **kwargs):
    '''
        Returns a list of instances of environment objects that can be produced
        from a file or dataset.  These instances will be created with a common
        underlying grid, and will interconnect when possible.
        For example, if an IceAwareWind can find an existing IceConcentration,
        it will use it instead of instantiating another. This function tries
        ALL gridded types by default. This means if a particular subclass
        of object is possible to be built, it is likely that all it's parents
        will be built and included as well.

        If you wish to limit the types of environment objects that will
        be used, pass a list of the types using "_cls_list" kwarg
    '''
    def attempt_from_netCDF(cls, **klskwargs):
        obj = None
        try:
            obj = c.from_netCDF(**klskwargs)
        except Exception as e:
            import logging
            logging.warn('''Class {0} could not be constituted from netCDF file
                                    Exception: {1}'''.format(c.__name__, e))
        return obj

    from gnome.environment.gridded_objects_base import Variable, VectorVariable
    from gridded.utilities import get_dataset
    from gnome.environment import PyGrid, Environment

    new_env = []

    if filename is not None:
        data_file = filename
        grid_file = filename

    ds = None
    dg = None
    if dataset is None:
        if grid_file == data_file:
            ds = dg = get_dataset(grid_file)
        else:
            ds = get_dataset(data_file)
            dg = get_dataset(grid_file)
    else:
        if grid_file is not None:
            dg = get_dataset(grid_file)
        else:
            dg = dataset
        ds = dataset
    dataset = ds

    grid = kwargs.pop('grid', None)
    if grid is None:
        grid = PyGrid.from_netCDF(filename=filename, dataset=dg, **kwargs)
        kwargs['grid'] = grid

    if _cls_list is None:
        scs = copy.copy(Environment._subclasses)
    else:
        scs = _cls_list

    for c in scs:
        if (issubclass(c, (Variable, VectorVariable))
                and not any([isinstance(o, c) for o in new_env])):
            clskwargs = copy.copy(kwargs)
            obj = None

            try:
                req_refs = c._req_refs
            except AttributeError:
                req_refs = None

            if req_refs is not None:
                for ref, klass in req_refs.items():
                    for o in new_env:
                        if isinstance(o, klass):
                            clskwargs[ref] = o

                    if ref in clskwargs.keys():
                        continue
                    else:
                        obj = attempt_from_netCDF(c,
                                                  filename=filename,
                                                  dataset=dataset,
                                                  grid_file=grid_file,
                                                  data_file=data_file,
                                                  **clskwargs)
                        clskwargs[ref] = obj

                        if obj is not None:
                            new_env.append(obj)

            obj = attempt_from_netCDF(c,
                                      filename=filename,
                                      dataset=dataset,
                                      grid_file=grid_file,
                                      data_file=data_file,
                                      **clskwargs)

            if obj is not None:
                new_env.append(obj)

    return new_env
Exemplo n.º 4
0
def env_from_netCDF(filename=None, dataset=None,
                    grid_file=None, data_file=None, _cls_list=None,
                    **kwargs):
    '''
        Returns a list of instances of environment objects that can be produced
        from a file or dataset.  These instances will be created with a common
        underlying grid, and will interconnect when possible.
        For example, if an IceAwareWind can find an existing IceConcentration,
        it will use it instead of instantiating another. This function tries
        ALL gridded types by default. This means if a particular subclass
        of object is possible to be built, it is likely that all it's parents
        will be built and included as well.

        If you wish to limit the types of environment objects that will
        be used, pass a list of the types using "_cls_list" kwarg
    '''
    def attempt_from_netCDF(cls, **klskwargs):
        obj = None
        try:
            obj = c.from_netCDF(**klskwargs)
        except Exception as e:
            import logging
            logging.warn('''Class {0} could not be constituted from netCDF file
                                    Exception: {1}'''.format(c.__name__, e))
        return obj

    from gnome.environment.gridded_objects_base import Variable, VectorVariable
    from gridded.utilities import get_dataset
    from gnome.environment import PyGrid, Environment

    new_env = []

    if filename is not None:
        data_file = filename
        grid_file = filename

    ds = None
    dg = None
    if dataset is None:
        if grid_file == data_file:
            ds = dg = get_dataset(grid_file)
        else:
            ds = get_dataset(data_file)
            dg = get_dataset(grid_file)
    else:
        if grid_file is not None:
            dg = get_dataset(grid_file)
        else:
            dg = dataset
        ds = dataset
    dataset = ds

    grid = kwargs.pop('grid', None)
    if grid is None:
        grid = PyGrid.from_netCDF(filename=filename, dataset=dg, **kwargs)
        kwargs['grid'] = grid

    if _cls_list is None:
        scs = copy.copy(Environment._subclasses)
    else:
        scs = _cls_list

    for c in scs:
        if (issubclass(c, (Variable, VectorVariable)) and
                not any([isinstance(o, c) for o in new_env])):
            clskwargs = copy.copy(kwargs)
            obj = None

            try:
                req_refs = c._req_refs
            except AttributeError:
                req_refs = None

            if req_refs is not None:
                for ref, klass in req_refs.items():
                    for o in new_env:
                        if isinstance(o, klass):
                            clskwargs[ref] = o

                    if ref in clskwargs.keys():
                        continue
                    else:
                        obj = attempt_from_netCDF(c,
                                                  filename=filename,
                                                  dataset=dataset,
                                                  grid_file=grid_file,
                                                  data_file=data_file,
                                                  **clskwargs)
                        clskwargs[ref] = obj

                        if obj is not None:
                            new_env.append(obj)

            obj = attempt_from_netCDF(c,
                                      filename=filename,
                                      dataset=dataset,
                                      grid_file=grid_file,
                                      data_file=data_file,
                                      **clskwargs)

            if obj is not None:
                new_env.append(obj)

    return new_env
Exemplo n.º 5
0
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 = PyGrid(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...
        from gnome.environment import GridCurrent
        from gnome.environment.grid_property import GriddedProp
        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)
    }