本文整理匯總了Python中netCDF4.Variable方法的典型用法代碼示例。如果您正苦於以下問題:Python netCDF4.Variable方法的具體用法?Python netCDF4.Variable怎麽用?Python netCDF4.Variable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類netCDF4
的用法示例。
在下文中一共展示了netCDF4.Variable方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_construction
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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
示例2: info
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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)
示例3: time
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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")
示例4: __getitem__
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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
示例5: location
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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
示例6: gen_celltree_mask_from_center_mask
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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
示例7: __init__
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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
示例8: __init__
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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)
示例9: isVariable
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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)
示例10: __init__
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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)
示例11: __init__
# 需要導入模塊: import netCDF4 [as 別名]
# 或者: from netCDF4 import Variable [as 別名]
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])