예제 #1
0
    def __init__(self, variable, index=slice(None)):
        """
        Initializer

        Parameters:
            variable (VariableDesc): A variable descriptor object
            index (tuple, slice, int, dict): A tuple of slices or ints, or a slice or int,
                specifying the range of data to read from the file (in file-local indices)
        """

        # Check variable descriptor type and existence in the file
        if not isinstance(variable, VariableDesc):
            raise TypeError('Unrecognized variable descriptor of type '
                            '{!r}: {!r}'.format(type(variable), variable.name))

        # Check for associated file
        if len(variable.files) == 0:
            raise ValueError(
                'Variable descriptor {} has no associated files'.format(
                    variable.name))
        self._filepath = None
        for fdesc in variable.files.itervalues():
            if fdesc.exists():
                self._filepath = fdesc.name
                break
        if self._filepath is None:
            raise OSError(
                'File path not found for input variable: {!r}'.format(
                    variable.name))

        # Check that the variable exists in the file
        with Dataset(self._filepath, 'r') as ncfile:
            if variable.name not in ncfile.variables:
                raise OSError(
                    'Variable {!r} not found in NetCDF file: {!r}'.format(
                        variable.name, self._filepath))
        self._variable = variable.name

        # Check if the index means "all"
        is_all = False
        if isinstance(index, slice) and index == slice(None):
            is_all = True
        elif isinstance(index, (list, tuple)) and all(i == slice(None)
                                                      for i in index):
            is_all = True
        elif isinstance(index, dict) and all(v == slice(None)
                                             for v in index.itervalues()):
            is_all = True

        # Store the reading index
        self._index = index

        # Call the base class initializer
        if is_all:
            label = variable.name
        else:
            label = '{}[{}]'.format(variable.name, index_str(index))
        super(ReadNode, self).__init__(label)
예제 #2
0
 def test_index_str_dict(self):
     indata = {'x': 4, 'y': slice(3, 1, -4)}
     testname = 'index_str({!r})'.format(indata)
     actual = index_str(indata)
     expected = "'x': 4, 'y': 3:1:-4"
     print_test_message(testname,
                        indata=indata,
                        actual=actual,
                        expected=expected)
     self.assertEqual(actual, expected, '{} failed'.format(testname))
예제 #3
0
 def test_index_str_tuple(self):
     indata = (4, slice(3, 1, -4))
     testname = 'index_str({!r})'.format(indata)
     actual = index_str(indata)
     expected = '4, 3:1:-4'
     print_test_message(testname,
                        indata=indata,
                        actual=actual,
                        expected=expected)
     self.assertEqual(actual, expected, '{} failed'.format(testname))
예제 #4
0
 def test_index_str_slice(self):
     indata = slice(1, 2, 3)
     testname = 'index_str({!r})'.format(indata)
     actual = index_str(indata)
     expected = '1:2:3'
     print_test_message(testname,
                        indata=indata,
                        actual=actual,
                        expected=expected)
     self.assertEqual(actual, expected, '{} failed'.format(testname))
예제 #5
0
 def test_index_str_int(self):
     indata = 3
     testname = 'index_str({!r})'.format(indata)
     actual = index_str(indata)
     expected = str(indata)
     print_test_message(testname,
                        indata=indata,
                        actual=actual,
                        expected=expected)
     self.assertEqual(actual, expected, '{} failed'.format(testname))