Ejemplo n.º 1
0
def _fake_complete(group):
    """
    'Complete' a group which is lacking some dimension lengths or data types.

    As the examples in simple_semantic_containers don't contain data, they
    don't have data types or lengths of unlimited dimensions (e.g. 'time').
    This operation sets missing lengths to 1, and fakes the data.
    This allows CDL to be generated (which needs the dtype), and variables to
    be compared (via "np.all(x.data == y.data)").

    This allows the examples builder functions to omit variables data, which is
    neater and saves space.

    """
    # Check all dims have definitions defined in the build code (for clarity).
    ncg.has_no_missing_dims(group, fail_if_not=True)

    # Fix up any top-level dims with no length (e.g. unlimited 'time').
    for dim in group.dimensions:
        if dim.length is None:
            dim.length = 1

    # Patch it all together.  Should work, now we have all the lengths.
    ncg.complete(group)

    for var in ncg.all_variables(group):
        if var.data is None:
            # NOTE: this might be better be done with numpy stride tricks ?
            var.data = _dummy_data(dim.length for dim in var.dimensions)
Ejemplo n.º 2
0
 def do_complete(self, group):
     # complete a group, and check that re-calling has no effect
     complete(group)
     self.assertTrue(has_no_missing_dims(group))
     firstpass_result = group.detached_copy()
     complete(group)
     self.assertEqual(group, firstpass_result)
Ejemplo n.º 3
0
def write(dataset, group):
    """
    Write a dataset to a netCDF file.

    Args:

    * dataset (:class:`netCDF4.Dataset` or string):
        An open writeable file, or a path string to create one.
        If a file was created, it is closed again afterwards.

    * group (:class:`ncobj.Group`):
        Data to write.  Note that this is passed to
        :func:`ncobj.grouping.complete`, which will usually modify it.

    .. note::

        Writing data into an existing file can obviously cause problems, but
        will equally obviously work fine in specific cases.

    """
    # Ready group for output first (any error should lead unchanged).
    ncg.complete(group)
    # Either save to the provided dataset, or open one and save to it.
    if isinstance(dataset, basestring):
        with netCDF4.Dataset(dataset, 'w') as ds:
            _save_group(ds, group)
    else:
        _save_group(dataset, group)
Ejemplo n.º 4
0
def group_flat_containers(group):
    result = group.detached_copy()
    ncg.complete(result)
    link_attribute_references(result)
    _inner_group_flat_containers(result)
    unlink_attribute_references(result)
    return result
Ejemplo n.º 5
0
def _make_complex_group():
    g = og(
        'temp',
        aa=[oa('a_root_attr_num', _long(1)),
            oa('c_root_attr_str', 'xyz'),
            oa('b_root_attr_vec', np.array([1.2, 3, 4]))],
        dd=[od('root_dim_x', 2)],
        vv=[ov('root_var_1',
               dd=[od('root_dim_x')],
               aa=[oa('root_var_attr_1', _long(11))],
               data=np.zeros((2))),
            ov('root_var_2_scalar',
               data=np.array(3.15, dtype=np.float32))],
        gg=[og('subgroup',
               aa=[oa('subgroup_attr', 'qq')],
               dd=[od('subgroup_dim_y', 3)],
               vv=[ov('subgroup_var',
                      dd=[od('root_dim_x'), od('subgroup_dim_y')],
                      aa=[oa('subgroup_var_attr', 57.5)],
                      data=np.zeros((2, 3)))],
               gg=[og('sub_sub_group',
                      aa=[oa('sub_sub_group_attr', 'this')],
                      vv=[ov('sub_sub_group_var',
                             dd=[od('subgroup_dim_y')],
                             data=np.zeros((3)))])]),
            og('sg_2_empty')])
    ncg.complete(g)
    return g
Ejemplo n.º 6
0
    def __getitem__(self, keys):
        """
        Return an extracted sub-section of the contained group.

        The specified extractor dimensions are indexed as required.
        The result is a completed group based on the original.

        """
        if not isinstance(keys, Iterable):
            keys = [keys]
        #print 'index by:', keys
        group = self._group.detached_copy()
        for index, dim in zip(keys, self._dims):
            #print 'slice {} by {}'.format(dim, index)
            dim_name = dim.name
            dim_reduces = not isinstance(index, slice)
            # Slice all variables that map this dimension
            for var in ncg.all_variables(group):
                #print 'var before:', var
                var_inds = [index if this_dim.name == dim_name else slice(None)
                            for this_dim in var.dimensions]
                #print 'var_inds = ', var_inds
                var.data = var.data[tuple(var_inds)]
                if dim_reduces:
                    var_dims = [this_dim for this_dim in var.dimensions
                                if this_dim.name != dim_name]
                    var.dimensions = var_dims
                    #print 'var_dims = ', var_dims
                #print 'var after:', var, 'shape = ', var.data.shape

            # Remove the dimension if we indexed it away.
            if dim_reduces:
                #print 'removed ', dim
                group.dimensions.remove(dim)
        ncg.complete(group)
        return group
Ejemplo n.º 7
0
        oa('root_attr_vec', np.array([1.2, 3, 4]))],
    dd=[od('root_dim_x', 2)],
    vv=[ov('root_var_1',
           dd=[od('root_dim_x')],
           aa=[oa('root_var_attr_1', 11)],
           data=np.zeros((2))),
        ov('root_var_2_scalar', data=np.array(3.15, dtype=np.float32))],
    gg=[og('subgroup',
           aa=[oa('subgroup_attr', 'qq')],
           dd=[od('subgroup_dim_y', 3)],
           vv=[ov('subgroup_var',
                  dd=[od('root_dim_x'), od('subgroup_dim_y')],
                  aa=[oa('subgroup_var_attr', 57.31)],
                  data=np.zeros((2, 3)))],
           gg=[og('sub_sub_group',
                  aa=[oa('sub_sub_group_attr', 'this')],
                  vv=[ov('sub_sub_group_var',
                         dd=[od('subgroup_dim_y')],
                         data=np.zeros((3)))])]),
        og('sg_2_empty')])


ncg.complete(g)
print g
print
file_path = './temp.nc'
with nc4.Dataset(file_path, 'w') as ds:
    ncf.write(ds, g)
print
os.system('ncdump -h temp.nc')