def _infer_time_period(filename, results_folder): filenames = sorted( glob(os.path.join(results_folder, _variable_name(filename) + '*.nc'))) start = start_date(managed_dataset(filenames[0], 'r')) end = end_date(managed_dataset(filenames[-1], 'r')) freq = dataset_frequency(managed_dataset(filenames[0], 'r')) return ObjectDict(type=freq, start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), representation='YYYY-MM-DD')
def __init__(self,filename,variable,preload=False): #import netCDF4 as nc self.fh = managed_dataset(filename,'r') self.extent = self.fh.get_extent() self.cs = self.extent.to_coords() if preload: self.v = self.fh.variables[variable][...] else: self.v = self.fh.variables[variable]
def __init__(self, ncfile, mode='r'): if mode == 'r': self.f = h5py.File(ncfile, 'r') self.available = [ m for m in list(self.f) if isinstance(self.f[m], h5py.Group) ] self._fastget = True else: self.f = managed_dataset(ncfile, mode) self.available = sorted(self.f.groups) self._fastget = False
def plot_cell_timeseries(file_map, slice, ax=None, figsize=(20, 10)): if not ax: ax = plt.figure(figsize=figsize).gca() for s in file_map: md = dm.managed_dataset(file_map[s]) d = md.awra_var[slice] df = pd.DataFrame(d) df.plot(ax=ax) return ax
def load_georef_nc(fn, load_mask=True): from awrams.utils.io.data_mapping import managed_dataset ds = managed_dataset(fn) if load_mask: e = ds.get_extent() georef = e.parent_ref mask = e.mask if hasattr(mask, 'shape'): if (mask == True).all(): mask = False else: georef = latlon_to_georef(ds.variables['latitude'][...], ds.variables['longitude'][...]) mask = False return georef, mask
def __init__(self, source, filename, path): self.filename = filename self.path = path pattern = os.path.join(os.path.abspath(path), filename + '*.nc') files = sorted(glob(pattern)) ref_ds = managed_dataset(files[0]) name = ref_ds.awra_var.name if name.startswith('/'): name = name[1:] units = ref_ds.awra_var.units ref_ds.close() VariableInstance.__init__(self, source, name, units) self._pattern = pattern self._reader = None self._dataset = ref_ds self.agg_method = 'mean' ### temporal aggregate method
def display_ncslice(fn, idx, extent=None, mask_value=None, cmap=None, norm=None, fig=None, ax=None, **kwds): import awrams.utils.io.data_mapping as dm import os.path md = dm.managed_dataset(fn) v = md.get_mapping_var() shape = md.variables[v.name].shape md.close() p, f = os.path.split(fn) sfm = dm.SplitFileManager.open_existing(p, f, v) def get_data(idx, extent, sfm): if not extent: a = sfm[np.s_[idx, :]] return a, 'slice', None else: try: s = [idx] s.extend(x for x in extent) a = sfm[np.s_[s]] return a, 'slice', None except: #raise gb = from_boundary_coords(*extent, compute_areas=False) print(gb.x_size, gb.y_size) a = sfm[np.s_[idx, slice(gb.x_min, gb.x_max), slice(gb.y_min, gb.y_max)]] extent = (gb.lon_min, gb.lon_max, gb.lat_min, gb.lat_max ) #(111.975,154.025,-44.025,-9.975) return a, 'geobounds', extent if type(idx) == int: if idx < 0: idx += shape[0] a, extent_type, extent = get_data(idx, extent, sfm) else: # idx is dti _idx = sfm.ref_ds.idx_for_period(idx) a, extent_type, extent = get_data(int(_idx[0]), extent, sfm) a = np.ma.masked_invalid(a) if mask_value is not None: a = np.ma.masked_equal(a, mask_value) print(a.min(), a.max(), a.mean(), a.shape) if fig is None: fig = plt.figure(figsize=(16, 20)) if ax is None: ax = fig.add_subplot(1, 1, 1) if cmap: kwds['cmap'] = cmap if norm: kwds['norm'] = norm if extent_type == 'geobounds': cax = ax.imshow(a, interpolation='none', extent=extent, **kwds) else: cax = ax.imshow(a, interpolation='none', **kwds) sfm.close_all() return fig, ax, cax
def __init__(self, ncfile, mode='r'): self.f = managed_dataset(ncfile, mode) self.available = sorted(self.f.groups)