本文整理汇总了Python中netCDF4.MFDataset方法的典型用法代码示例。如果您正苦于以下问题:Python netCDF4.MFDataset方法的具体用法?Python netCDF4.MFDataset怎么用?Python netCDF4.MFDataset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netCDF4
的用法示例。
在下文中一共展示了netCDF4.MFDataset方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_dataset
# 需要导入模块: import netCDF4 [as 别名]
# 或者: from netCDF4 import MFDataset [as 别名]
def get_dataset(ncfile, dataset=None):
"""
Utility to create a netCDF4 Dataset from a filename, list of filenames,
or just pass it through if it's already a netCDF4.Dataset
if dataset is not None, it should be a valid netCDF4 Dataset object,
and it will simply be returned
"""
if dataset is not None:
return dataset
if isinstance(ncfile, nc4.Dataset):
return ncfile
elif isinstance(ncfile, Iterable) and len(ncfile) == 1:
return nc4.Dataset(ncfile[0])
elif isstring(ncfile):
return nc4.Dataset(ncfile)
else:
return nc4.MFDataset(ncfile)
示例2: netcdf
# 需要导入模块: import netCDF4 [as 别名]
# 或者: from netCDF4 import MFDataset [as 别名]
def netcdf(file, aggdim=None):
"""
Wrapper around netCDF4 to open a file as either a Dataset or an
MFDataset.
Parameters
----------
file : string or list,
Filename(s) to open. If the string has wildcards or is a list,
this attempts to open an MFDataset
aggdim : string,
Name of dimension to concatenate along if loading a set of files.
A value of None (default) uses the unlimited dimension.
Returns
-------
netCDF4 Dataset or MFDataset
"""
import netCDF4
try:
nc = netCDF4.Dataset(file)
except (OSError, RuntimeError):
try:
nc = netCDF4.MFDataset(file, aggdim=aggdim)
except IndexError:
raise FileNotFoundError("{:s} cannot be found.".format(file))
return nc
示例3: get_index_lat
# 需要导入模块: import netCDF4 [as 别名]
# 或者: from netCDF4 import MFDataset [as 别名]
def get_index_lat(resource, variable=None):
"""
returns the dimension index of the latiude values
:param resource: list of path(s) to netCDF file(s) of one Dataset
:param variable: variable name
:return int: index
"""
if variable is None:
variable = get_variable(resource)
if type(resource) != list:
resource = [resource]
if len(resource) == 1:
ds = Dataset(resource[0])
else:
ds = MFDataset(resource)
var = ds.variables[variable]
dims = list(var.dimensions)
if 'rlat' in dims:
index = dims.index('rlat')
if 'lat' in dims:
index = dims.index('lat')
if 'latitude' in dims:
index = dims.index('latitude')
if 'y' in dims:
index = dims.index('y')
return index
示例4: Convert_nc_to_tiff
# 需要导入模块: import netCDF4 [as 别名]
# 或者: from netCDF4 import MFDataset [as 别名]
def Convert_nc_to_tiff(input_nc, output_folder):
"""
This function converts the nc file into tiff files
Keyword Arguments:
input_nc -- name, name of the adf file
output_folder -- Name of the output tiff file
"""
from datetime import date
import wa.General.raster_conversions as RC
#All_Data = RC.Open_nc_array(input_nc)
if type(input_nc) == str:
nc = netCDF4.Dataset(input_nc)
elif type(input_nc) == list:
nc = netCDF4.MFDataset(input_nc)
Var = nc.variables.keys()[-1]
All_Data = nc[Var]
geo_out, epsg, size_X, size_Y, size_Z, Time = RC.Open_nc_info(input_nc)
if epsg == 4326:
epsg = 'WGS84'
# Create output folder if needed
if not os.path.exists(output_folder):
os.mkdir(output_folder)
for i in range(0,size_Z):
if not Time == -9999:
time_one = Time[i]
d = date.fromordinal(time_one)
name = os.path.splitext(os.path.basename(input_nc))[0]
nameparts = name.split('_')[0:-2]
name_out = os.path.join(output_folder, '_'.join(nameparts) + '_%d.%02d.%02d.tif' %(d.year, d.month, d.day))
Data_one = All_Data[i,:,:]
else:
name=os.path.splitext(os.path.basename(input_nc))[0]
name_out = os.path.join(output_folder, name + '.tif')
Data_one = All_Data[:,:]
Save_as_tiff(name_out, Data_one, geo_out, epsg)
return()
示例5: __enter__
# 需要导入模块: import netCDF4 [as 别名]
# 或者: from netCDF4 import MFDataset [as 别名]
def __enter__(self):
if not self.meta_only:
# Don't decode times since we do it anyways.
decode_times = False
if self._nc_files:
try:
self.dataset = xarray.open_mfdataset(
self._nc_files,
decode_times=decode_times,
chunks=200,
)
except xarray.core.variable.MissingDimensionsError:
# xarray won't open FVCOM files due to dimension/coordinate/variable label
# duplication issue, so fall back to using netCDF4.Dataset()
self.dataset = netCDF4.MFDataset(self._nc_files)
else:
try:
# Handle list of URLs for staggered grid velocity field datasets
url = self.url if isinstance(self.url, list) else [self.url]
# This will raise a FutureWarning for xarray>=0.12.2.
# That warning should be resolvable by changing to:
# fields = xarray.open_mfdataset(self.url, combine="by_coords", decode_times=decode_times)
fields = xarray.open_mfdataset(url, decode_times=decode_times)
except xarray.core.variable.MissingDimensionsError:
# xarray won't open FVCOM files due to dimension/coordinate/variable label
# duplication issue, so fall back to using netCDF4.Dataset()
fields = netCDF4.Dataset(self.url)
if getattr(self._dataset_config, "geo_ref", {}):
drop_variables = self._dataset_config.geo_ref.get("drop_variables", [])
geo_refs = xarray.open_dataset(
self._dataset_config.geo_ref["url"], drop_variables=drop_variables,
)
fields = fields.merge(geo_refs)
self.dataset = fields
if self._grid_angle_file_url:
angle_file = xarray.open_dataset(
self._grid_angle_file_url,
drop_variables=[self._dataset_config.lat_var_key, self._dataset_config.lon_var_key]
)
self.dataset = self.dataset.merge(angle_file)
angle_file.close()
self._dataset_open = True
return self
示例6: get_timerange
# 需要导入模块: import netCDF4 [as 别名]
# 或者: from netCDF4 import MFDataset [as 别名]
def get_timerange(resource):
"""
returns from/to timestamp of given netcdf file(s).
:param resource: list of path(s) to netCDF file(s)
:returns netcdf.datetime.datetime: start, end
"""
start = end = None
if type(resource) != list:
resource = [resource]
LOGGER.debug('length of recources: %s files' % len(resource))
try:
resource.sort()
if len(resource) > 1:
# ds = MFDataset(resource)
LOGGER.error('functon expect single file, Mulitple files found {}'.format(len(resource)))
else:
ds = Dataset(resource[0])
LOGGER.debug('Dataset loaded for %s file in resource:' % len(resource))
time = ds.variables['time']
if (hasattr(time, 'units') and hasattr(time, 'calendar')) is True:
s = num2date(time[0], time.units, time.calendar)
e = num2date(time[-1], time.units, time.calendar)
elif hasattr(time, 'units'):
s = num2date(time[0], time.units)
e = num2date(time[-1], time.units)
else:
s = num2date(time[0])
e = num2date(time[-1])
# TODO: include frequency
start = '%s%s%s' % (s.year, str(s.month).zfill(2), str(s.day).zfill(2))
end = '%s%s%s' % (e.year, str(e.month).zfill(2), str(e.day).zfill(2))
ds.close()
except Exception:
msg = 'failed to get time range'
LOGGER.exception(msg)
ds.close()
raise Exception(msg)
return start, end