Python netCDF4.Variable() Examples

The following are 13 code examples of netCDF4.Variable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module netCDF4 , or try the search function .
Example #1
Source File: test_grid.py    From gridded with The Unlicense 6 votes vote down vote up
def test_construction(self, ug_data, ug_topology):
        filename = ug_data[0]
        dataset = ug_data[1]
        grid_topology = ug_topology
        ug = Grid_U.from_netCDF(filename, dataset, grid_topology=grid_topology)
#         assert ug.filename == filename
#         assert isinstance(ug.node_lon, nc.Variable)
#         assert ug.node_lon.name == 'lonc'

        ug2 = Grid_U.from_netCDF(filename)
        assert ug2.filename == filename
#         assert isinstance(ug2.node_lon, nc.Variable)
#         assert ug2.node_lon.name == 'lon'

        ug3 = Grid.from_netCDF(filename, dataset, grid_topology=grid_topology)
        ug4 = Grid.from_netCDF(filename)
        print(ug3.shape)
        print(ug4.shape)
        assert ug == ug3
        assert ug2 == ug4 
Example #2
Source File: variable.py    From gridded with The Unlicense 6 votes vote down vote up
def info(self):
        """
        Information about the variable object
        This could be filled out more
        """
        try:
            std_name = self.attributes['standard_name']
        except KeyError:
            std_name = None
        msg = """
              Variable:
                filename: {0.filename}
                varname: {0.varname}
                standard name: {1}
                units: {0.units}
                grid: {0.grid}
                data shape: {0.data.shape}
              """.format(self, std_name)
        return dedent(msg) 
Example #3
Source File: variable.py    From gridded with The Unlicense 6 votes vote down vote up
def time(self, t):
        Time = self.__class__._default_component_types['time']
        if self.variables is not None:
            for v in self.variables:
                try:
                    v.time = t
                except ValueError as e:
                    raise ValueError('''Time was not compatible with variables.
                    Set variables attribute to None to allow changing other attributes
                    Original error: {0}'''.format(str(e)))
        if isinstance(t, Time):
            self._time = t
        elif isinstance(t, collections.Iterable) or isinstance(t, nc4.Variable):
            self._time = Time(t)
        else:
            raise ValueError("Time must be set with an iterable container or netCDF variable") 
Example #4
Source File: netcdf_utils.py    From satpy with GNU General Public License v3.0 6 votes vote down vote up
def __getitem__(self, key):
        """Get item for given key."""
        val = self.file_content[key]
        if isinstance(val, netCDF4.Variable):
            if key in self.cached_file_content:
                return self.cached_file_content[key]
            # these datasets are closed and inaccessible when the file is
            # closed, need to reopen
            # TODO: Handle HDF4 versus NetCDF3 versus NetCDF4
            parts = key.rsplit('/', 1)
            if len(parts) == 2:
                group, key = parts
            else:
                group = None
            if self.file_handle is not None:
                val = self._get_var_from_filehandle(group, key)
            else:
                val = self._get_var_from_xr(group, key)
        elif isinstance(val, netCDF4.Group):
            # Full groups are conveniently read with xr even if file_handle is available
            with xr.open_dataset(self.filename, group=key,
                                 **self._xarray_kwargs) as nc:
                val = nc
        return val 
Example #5
Source File: variable.py    From gridded with The Unlicense 5 votes vote down vote up
def location(self, location):
        # Fixme: perhaps we need Variable subclasses,
        #        to distingish between variable types.
        if location not in VALID_LOCATIONS:
            raise ValueError("Invalid location: {}, must be one of: {}".format(location, VALID_LOCATIONS))
        self._location = location 
Example #6
Source File: variable.py    From gridded with The Unlicense 5 votes vote down vote up
def time(self, t):
        Time = self.__class__._default_component_types['time']
        if t is None:
            self._time = None
            return
        if self.data is not None and len(t) != self.data.shape[0] and len(t) > 1:
            raise ValueError("Data/time interval mismatch")
        if isinstance(t, Time):
            self._time = t
        elif isinstance(t, collections.Iterable) or isinstance(t, nc4.Variable):
            self._time = Time(t)
        else:
            raise ValueError("Time must be set with an iterable container or netCDF variable") 
Example #7
Source File: utilities.py    From gridded with The Unlicense 5 votes vote down vote up
def gen_celltree_mask_from_center_mask(center_mask, sl):
    """
    Generates celltree face mask from the center mask
    """

    input_mask = center_mask[sl]
    ret_mask = np.ones(input_mask.shape, dtype=bool)
    type1 = (isinstance(center_mask, nc4.Variable)
             and hasattr(center_mask, 'flag_values')
             and hasattr(center_mask, 'flag_meanings'))
    type2 = (isinstance(center_mask, nc4.Variable)
             and hasattr(center_mask, 'option_0'))
    if type1:
        fm = center_mask.flag_meanings
        try:
            fm = fm.split()
        except AttributeError:
            pass  # must not be a string -- we assume it's a sequence already
        meaning_mask = [False if ('water' in s or 'lake' in s) else True for s in fm]
        tfmap = dict(zip(center_mask.flag_values, meaning_mask))
        for k, v in tfmap.items():
            ret_mask[input_mask == k] = v
    elif type2:  # special case where option_0 == land,
                 # option_1 == water, etc
                 # TODO: generalize this properly
        meaning_mask = [True, False]
        tfmap = dict(zip([0, 1], meaning_mask))
        for k, v in tfmap.items():
            ret_mask[input_mask == k] = v
    else:
        ret_mask[:] = input_mask

    return ret_mask 
Example #8
Source File: ncdf.py    From argos with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, ncVar, nodeName, fileName=''):
        """ Constructor.
            The name of the field must be given to the nodeName parameter.
        """
        super(NcdfFieldRti, self).__init__(nodeName, fileName=fileName)
        check_class(ncVar, Variable)

        self._ncVar = ncVar 
Example #9
Source File: ncdf.py    From argos with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, ncVar, nodeName, fileName=''):
        """ Constructor
        """
        super(NcdfVariableRti, self).__init__(nodeName, fileName=fileName)
        check_class(ncVar, Variable)
        self._ncVar = ncVar

        try:
            self._isStructured = bool(self._ncVar.dtype.names)
        except (AttributeError, KeyError):
            # If dtype is a string instead of an numpy dtype, netCDF4 raises a KeyError
            # or AttributeError, depending on its version.
            self._isStructured = False 
Example #10
Source File: netcdf_utils.py    From satpy with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, filename, filename_info, filetype_info,
                 auto_maskandscale=False, xarray_kwargs=None,
                 cache_var_size=0, cache_handle=False):
        """Initialize object."""
        super(NetCDF4FileHandler, self).__init__(
            filename, filename_info, filetype_info)
        self.file_content = {}
        self.cached_file_content = {}
        try:
            file_handle = netCDF4.Dataset(self.filename, 'r')
        except IOError:
            LOG.exception(
                'Failed reading file %s. Possibly corrupted file', self.filename)
            raise

        self.auto_maskandscale = auto_maskandscale
        if hasattr(file_handle, "set_auto_maskandscale"):
            file_handle.set_auto_maskandscale(auto_maskandscale)

        self.collect_metadata("", file_handle)
        self.collect_dimensions("", file_handle)
        if cache_var_size > 0:
            self.collect_cache_vars(
                    [varname for (varname, var)
                        in self.file_content.items()
                        if isinstance(var, netCDF4.Variable)
                        and isinstance(var.dtype, np.dtype)  # vlen may be str
                        and var.size * var.dtype.itemsize < cache_var_size],
                    file_handle)
        if cache_handle:
            self.file_handle = file_handle
        else:
            file_handle.close()
        self._xarray_kwargs = xarray_kwargs or {}
        self._xarray_kwargs.setdefault('chunks', CHUNK_SIZE)
        self._xarray_kwargs.setdefault('mask_and_scale', self.auto_maskandscale) 
Example #11
Source File: sile.py    From sisl with GNU Lesser General Public License v3.0 5 votes vote down vote up
def isVariable(cls, obj):
        """ Return true if ``obj`` is an instance of the NetCDF4 ``Variable`` type

        This is just a wrapper for ``isinstance(obj, netCDF4.Variable)``.
        """
        return isinstance(obj, _netCDF4.Variable) 
Example #12
Source File: time.py    From gridded with The Unlicense 4 votes vote down vote up
def __init__(self,
                 data=(datetime.now(),),
                 filename=None,
                 varname=None,
                 tz_offset=None,
                 origin=None,
                 displacement=timedelta(seconds=0),
                 *args,
                 **kwargs):
        '''
        Representation of a time axis. Provides interpolation alphas and indexing.

        :param time: Ascending list of times to use
        :param tz_offset: offset to compensate for time zone shifts
        :param origin: shifts the time interval to begin at the time specified
        :param displacement: displacement to apply to the time data. Allows shifting entire time interval into future or past
        :type time: netCDF4.Variable or [] of datetime.datetime
        :type tz_offset: datetime.timedelta
        :type origin: datetime.timedelta
        :type displacement: datetime.timedelta
        '''

        if isinstance(data, (nc4.Variable, nc4._netCDF4._Variable)):
            self.data = nc4.num2date(data[:], units=data.units)
        elif data is None:
            self.data = np.array([datetime.now()])
        else:
            self.data = np.asarray(data)

        if origin is not None:
            diff = self.data[0] - origin
            self.data -= diff

        self.data += displacement

        self.filename = filename
        self.varname = varname

#         if self.filename is None:
#             self.filename = self.id + '_time.txt'

        if tz_offset is not None:
            self.data += tz_offset

        if not self._timeseries_is_ascending(self.data):
            raise ValueError("Time sequence is not ascending")
        if self._has_duplicates(self.data):
            raise ValueError("Time sequence has duplicate entries")

        super(Time, self).__init__(*args, **kwargs) 
Example #13
Source File: variable.py    From gridded with The Unlicense 4 votes vote down vote up
def __init__(self,
                 name=None,
                 units=None,
                 time=None,
                 variables=None,
                 grid=None,
                 depth=None,
                 grid_file=None,
                 data_file=None,
                 dataset=None,
                 varnames=None,
                 **kwargs):

        super(VectorVariable, self).__init__()
        self.name = self._units = self._time = self._variables = None

        self.name = name

        if all([isinstance(v, Variable) for v in variables]):
            if time is not None and not isinstance(time, Time):
                time = Time(time)
            units = variables[0].units if units is None else units
            time = variables[0].time if time is None else time
        if units is None:
            units = variables[0].units
        self._units = units
        if variables is None or len(variables) < 2:
            raise ValueError('Variables must be an array-like of 2 or more Variable objects')
        self.variables = variables
        self._time = time
        unused_args = kwargs.keys() if kwargs is not None else None
        if len(unused_args) > 0:
            kwargs = {}
        if isinstance(self.variables[0], Variable):
            self.grid = self.variables[0].grid if grid is None else grid
            self.depth = self.variables[0].depth if depth is None else depth
            self.grid_file = self.variables[0].grid_file if grid_file is None else grid_file
            self.data_file = self.variables[0].data_file if data_file is None else data_file

        self._result_memo = collections.OrderedDict()
        for i, comp in enumerate(self.__class__.comp_order):
            setattr(self, comp, self.variables[i])