Python netCDF4.MFDataset() Examples
The following are 6
code examples of netCDF4.MFDataset().
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: utilities.py From gridded with The Unlicense | 6 votes |
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)
Example #2
Source File: lib.py From seapy with MIT License | 5 votes |
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
Example #3
Source File: nc_utils.py From flyingpigeon with Apache License 2.0 | 5 votes |
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
Example #4
Source File: data_conversions.py From wa with Apache License 2.0 | 4 votes |
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()
Example #5
Source File: netcdf_data.py From Ocean-Data-Map-Project with GNU General Public License v3.0 | 4 votes |
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
Example #6
Source File: nc_utils.py From flyingpigeon with Apache License 2.0 | 4 votes |
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