File: test_compoundatt.py

package info (click to toggle)
netcdf4-python 1.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,604 kB
  • sloc: python: 6,057; ansic: 854; makefile: 15; sh: 2
file content (73 lines) | stat: -rw-r--r-- 2,644 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import unittest
import os
import tempfile
from netCDF4 import Dataset, CompoundType
import numpy as np
from numpy.testing import assert_array_equal, assert_array_almost_equal

# test compound attributes.

FILE_NAME = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
DIM_NAME = 'time'
VAR_NAME = 'wind'
VAR_NAME2 = 'forecast_wind'
GROUP_NAME = 'forecasts'
dtype=np.dtype([('speed', 'f4'), ('direction', 'f4')])
TYPE_NAME = 'wind_vector_type'
TYPE_NAMEC = 'wind_vectorunits_type'
dtypec=np.dtype([('speed', 'S8'), ('direction', 'S8')])
missvals = np.empty(1,dtype)
missvals['direction']=1.e20
missvals['speed']=-999.
windunits = np.empty(1,dtypec)
windunits['speed'] = 'm/s'
windunits['direction'] = 'degrees'

class VariablesTestCase(unittest.TestCase):

    def setUp(self):
        self.file = FILE_NAME
        f  = Dataset(self.file, 'w')
        d = f.createDimension(DIM_NAME,None)
        g = f.createGroup(GROUP_NAME)
        wind_vector_type = f.createCompoundType(dtype, TYPE_NAME)
        wind_vectorunits_type = f.createCompoundType(dtypec, TYPE_NAMEC)
        v = f.createVariable(VAR_NAME,wind_vector_type, DIM_NAME)
        vv = g.createVariable(VAR_NAME2,wind_vector_type,DIM_NAME)
        v.missing_values = missvals
        v.units = windunits
        vv.missing_values = missvals
        vv.units = windunits
        f.close()

    def tearDown(self):
        # Remove the temporary files
        os.remove(self.file)

    def runTest(self):
        """testing compound attributes"""
        f = Dataset(self.file, 'r')
        v = f.variables[VAR_NAME]
        g = f.groups[GROUP_NAME]
        vv = g.variables[VAR_NAME2]
        assert_array_almost_equal(v.missing_values['speed'], missvals['speed'])
        assert_array_almost_equal(v.missing_values['direction'],\
                missvals['direction'])
        assert_array_almost_equal(vv.missing_values['speed'], missvals['speed'])
        assert_array_almost_equal(vv.missing_values['direction'],\
                missvals['direction'])
        assert_array_equal(v.units['speed'], windunits['speed'].squeeze())
        assert_array_equal(v.units['direction'],\
                windunits['direction'].squeeze())
        assert_array_equal(vv.units['speed'], windunits['speed'].squeeze())
        assert_array_equal(vv.units['direction'],\
                windunits['direction'].squeeze())
        assert v.units['speed'] == b'm/s' 
        assert v.units['direction'] == b'degrees' 
        assert vv.units['speed'] == b'm/s' 
        assert vv.units['direction'] == b'degrees' 
        f.close()

if __name__ == '__main__':
    unittest.main()