Exemple #1
0
 def test_attrs(self):
     result = cdl(ov('x', dd=[od('p'), od('q')],
                     aa=[oa('n1', _long(3)), oa('r1', 1.24)],
                     data=np.array(1.0)))
     self.assertEqual(result,
                      'double x(p, q) ;\n'
                      '    x:n1 = 3L ;\n'
                      '    x:r1 = 1.24 ;')
Exemple #2
0
 def test_inner_groups(self):
     g = og('group_name', gg=[og('sub_group')])
     result = cdl(g)
     self.assertEqual(result,
                      'netcdf group_name {\n'
                      '\n'
                      'group: sub_group {\n'
                      '} // group sub_group\n'
                      '}')
Exemple #3
0
 def test_attr(self):
     g = og('group_name', aa=[oa('x', _long(2))])
     result = cdl(g)
     self.assertEqual(result,
                      'netcdf group_name {\n'
                      '\n'
                      '// global attributes:\n'
                      '    :x = 2L ;\n'
                      '}')
Exemple #4
0
 def test_dim(self):
     g = og('group_name', dd=[od('x', 2)])
     result = cdl(g)
     self.assertEqual(result,
                      'netcdf group_name {\n'
                      '\n'
                      'dimensions:\n'
                      '    x = 2 ;\n'
                      '}')
Exemple #5
0
 def test_var(self):
     g = og('group_name', vv=[ov('x', data=np.array(1.0))])
     result = cdl(g)
     self.assertEqual(result,
                      'netcdf group_name {\n'
                      '\n'
                      'variables:\n'
                      '    double x ;\n'
                      '}')
Exemple #6
0
        def test_attrs(self):
            def _write_testdata(ds):
                # Create a simple x2 dimension for array testing.
                ds.createDimension('pair', 2)

                # Create variables + attributes of all netcdf basic types.
                for np_type, type_name in \
                        ncdl._DTYPES_TYPE_NAMES.iteritems():
                    if type_name == 'char':
                        scalar = 'A'
                        array = 'String_data'
                        # N.B. Wrapping this with np.array() has no effect.
                    else:
                        scalar = np.array(1.25, dtype=np_type)
                        array = np.array([1.25, 13.25], dtype=np_type)

                    attr_name = 'scalar_attr_' + type_name
                    ds.setncattr(attr_name, scalar)

                    attr_name = 'vector_attr_' + type_name
                    ds.setncattr(attr_name, array)

                    var_name = 'scalar_var_' + type_name
                    var = ds.createVariable(varname=var_name,
                                            datatype=np_type)
                    var[:] = scalar

                    var_name = 'array_var_' + type_name
                    var = ds.createVariable(varname=var_name,
                                            datatype=np_type,
                                            dimensions=('pair',))
                    var[:] = array

            testfile_name = 'alltypes.nc'
            temp_dirpath = tempfile.mkdtemp()
            try:
                file_path = os.path.join(temp_dirpath, testfile_name)
                with nc4.Dataset(file_path, 'w') as ds:
                    _write_testdata(ds)

                ncdump_output = subprocess.check_output(
                    'ncdump -h ' + file_path, shell=True)

                with nc4.Dataset(file_path, 'r') as ds:
                    read_data = ncf.read(ds)
                    read_data.rename('alltypes')
                    cdl_results = cdl(read_data)

                expected = sort_lines(strip_lines(ncdump_output))
                found = sort_lines(strip_lines(cdl_results))
                self.assertEqual(found, expected)

            finally:
                shutil.rmtree(temp_dirpath)
Exemple #7
0
 def test_inner_group_attr(self):
     g = og('group_name',
            gg=[og('sub_group',
                   aa=[oa('x', _long(2))])])
     result = cdl(g)
     self.assertEqual(result,
                      'netcdf group_name {\n'
                      '\n'
                      'group: sub_group {\n'
                      '\n'
                      '// group attributes:\n'
                      '    :x = 2L ;\n'
                      '} // group sub_group\n'
                      '}')
Exemple #8
0
def check_file_cdl(file_path):
    # Load from the given file
    with netCDF4.Dataset(file_path) as ds:
        g = ncds.read(ds)
        # Re-save to a temporary file, to get an ncdump output in known order.
        ncds.write('temp.nc', g)
        # Rename so name matches temporary file name as seen by ncdump.
        g.rename('temp')
        # Generate cdl.
        g_cdl = cdl(g)
    try:
        # Run ncdump on the test file
        f_cdl = subprocess.check_output('ncdump -h temp.nc', shell=True)
    finally:
        os.remove('temp.nc')
    # Check that the two CDL strings are "essentially" the same.
    g_cdl_std = ncdl.comparable_cdl(g_cdl)
    f_cdl_std = ncdl.comparable_cdl(f_cdl)
    if g_cdl_std == f_cdl_std:
        print 'OK    (ncdump and ncobj output EQUAL)'
    else:
        print 'FAIL: ncdump and ncobj output differ..'
    print
Exemple #9
0
 def test_ushort(self):
     result = cdl(ov('x', data=np.array(1, dtype=np.dtype('uint16'))))
     self.assertEqual(result, 'ushort x ;')
Exemple #10
0
 def test_ulong_type(self):
     result = cdl(oa('x', np.array(5, dtype=np.dtype('uint64'))))
     self.assertEqual(result, 'x = 5UL')
Exemple #11
0
 def test_float_type(self):
     result = cdl(oa('x', np.array(1.23, dtype=np.dtype('float32'))))
     self.assertEqual(result, 'x = 1.23f')
Exemple #12
0
 def test_empty(self):
     g = og('group_name')
     result = cdl(g)
     self.assertEqual(result, 'netcdf group_name {\n}')
Exemple #13
0
 def test__group(self):
     g = _make_complex_group()
     result_cdl = cdl(g)
     expect_cdl = _complex_cdl[:]
     self.assertEqual(result_cdl, expect_cdl)
Exemple #14
0
 def test_chars(self):
     result = cdl(ov('x', dd=[od('STRLEN_4', 4)],
                     data=np.array(['t', 'h', 'i', 's'])))
     self.assertEqual(result, 'char x(STRLEN_4) ;')
Exemple #15
0
 def test_unsigned_type(self):
     result = cdl(oa('x', np.array(5, dtype=np.dtype('uint32'))))
     self.assertEqual(result, 'x = 5U')
Exemple #16
0
 def test_double_type(self):
     result = cdl(oa('x', np.array(1.23, dtype=np.dtype('float64'))))
     self.assertEqual(result, 'x = 1.23')
Exemple #17
0
def ncfile_cdl(file_path):
    with netCDF4.Dataset(file_path) as ds:
        group = ncds.read(ds)
        result = cdl(group)
    return result
Exemple #18
0
 def test_basic(self):
     result = cdl(od('x', 3))
     self.assertEqual(result, 'x = 3 ;')
Exemple #19
0
 def test_ubyte(self):
     result = cdl(ov('x', data=np.array(1, dtype=np.dtype('uint8'))))
     self.assertEqual(result, 'ubyte x ;')
Exemple #20
0
 def test_unlimited(self):
     result = cdl(od('x', 3, u=True))
     self.assertEqual(result, 'x = UNLIMITED ;')
Exemple #21
0
 def test_nd(self):
     result = cdl(ov('x', dd=[od('p'), od('q')], data=np.array(1.0)))
     self.assertEqual(result, 'double x(p, q) ;')
Exemple #22
0
 def test_scalar(self):
     result = cdl(ov('x', data=np.array(3.21, dtype=np.float32)))
     self.assertEqual(result, 'float x ;')
Exemple #23
0
 def test_int(self):
     result = cdl(ov('x', data=np.array(1, dtype=np.dtype('int32'))))
     self.assertEqual(result, 'int x ;')
Exemple #24
0
 def test_ulong(self):
     result = cdl(ov('x', data=np.array(1, dtype=np.dtype('uint64'))))
     self.assertEqual(result, 'uint64 x ;')
Exemple #25
0
def show_results(extracted):
    #print extracted
    print cdl(extracted)
    print 'vars data: \n' + '\n'.join("{}:{}".format(var.name, var.data)
                                      for var in ncg.all_variables(extracted))
Exemple #26
0
 def test_float(self):
     result = cdl(ov('x', data=np.array(1.0,
                                        dtype=np.dtype('float32'))))
     self.assertEqual(result, 'float x ;')
"""
Example code demonstrating 'flatten' operation for enhanced semantic containers
definition.

"""
from ncobj.cdl import cdl

import ncobj.examples.semantic_containers as egs


g = egs.eg_simple_flat()
g_cdl = cdl(g)

print
print '----------------'
print 'Original flat version:'
print '----------------'
print g_cdl
print '----------------'

g_grouped = egs.group_flat_containers(g)
g_grouped_cdl = cdl(g_grouped)
print
print '----------------'
print 'Result, grouped from flat form:'
print '----------------'
print g_grouped_cdl
print '----------------'

g_grp_eg = egs.eg_simple_grouped()
Exemple #28
0
 def test_double(self):
     result = cdl(ov('x', data=np.array(1.0)))
     self.assertEqual(result, 'double x ;')
"""
Example code demonstrating 'flatten' operation for enhanced semantic containers
definition.

"""
from ncobj.cdl import cdl

import ncobj.examples.semantic_containers as egs


g = egs.eg_simple_grouped()
g_cdl = cdl(g)

print
print '----------------'
print 'Original grouped version:'
print '----------------'
print g_cdl
print '----------------'

g_flattened = egs.flatten_grouped_containers(g)
g_flattened_cdl = cdl(g_flattened)
print
print '----------------'
print 'Result, flattened from grouped form:'
print '----------------'
print g_flattened_cdl
print '----------------'

g_flat_eg = egs.eg_simple_flat()
Exemple #30
0
 def test_single_type(self):
     result = cdl(oa('x', np.array(5, dtype=np.dtype('int32'))))
     self.assertEqual(result, 'x = 5')