def et_log_polar_coords(self, release: pd.Timestamp, coords_to_keep=None, rounding_vals=None, keep_list=None ): if keep_list is None: keep_list = [co.RL] if coords_to_keep is None: coords_to_keep = [co.WE, co.SN] if rounding_vals is None: rounding_vals = [co.ROUND_R_LOG, co.ROUND_TH_RAD] keep_vars = [co.CONC] keep_coords = coords_to_keep sum_ds = self.flexout_hour_ds sum_ds = sum_ds[keep_vars].sel(**{co.RL: release}) complement_coords = fa.get_dims_complement(sum_ds, keep_coords) log.ger.debug('completemt coords: %s', complement_coords) sum_ds = sum_ds.sum(dim=complement_coords) # log.ger.debug(sum_ds) val = rounding_vals log_pol_ds = xr.Dataset() for v in keep_vars: log_pol_ds[v] = fa.data_array_to_logpolar(da=sum_ds[v], dim2keep=keep_list, r_round_log=val[0], th_round_rad=val[1]) return log_pol_ds
def plot_cluster_map( self, i, log_plot_dic=None, map_dic=None, par_to_plot=co.COL, ): if map_dic is None: map_dic = {} if log_plot_dic is None: log_plot_dic = {} clus = self.cluster_flags[i] boo = self.merged_ds[co.ClusFlag] == clus ax = fa.get_ax_bolivia(**map_dic) # fig,ax = plt.subplots() warnings.simplefilter('ignore') ar = self.merged_ds.where(boo)[par_to_plot] com = fa.get_dims_complement(ar, [co.R_CENTER, co.TH_CENTER]) ar = ar.sum(dim=com) if par_to_plot is co.CPer: ar = ar / self.merged_ds[par_to_plot].sum() * 100 cmap = fa.get_custom_cmap(self.colors[i]) def_dic = dict(ax=ax, name=par_to_plot, perM=.95, quantile=True, colorbar=False, patch_args={'cmap': cmap}) com_dic = {**def_dic, **log_plot_dic} fa.logpolar_plot(ar, **com_dic)
def get_log_polar_coords_topo(head_ds, coords_to_keep=None, rounding_vals=None, keep_list=None, keep_vars=None): if keep_vars is None: keep_vars = [co.TOPO] if keep_list is None: keep_list = [] if rounding_vals is None: rounding_vals = [co.ROUND_R_LOG, co.ROUND_TH_RAD] if coords_to_keep is None: coords_to_keep = [co.WE, co.SN] keep_coords = coords_to_keep head_ds = fa.add_lat_lot(head_ds) head_ds = head_ds[keep_vars] complement_coords = fa.get_dims_complement(head_ds, keep_coords) head_ds = head_ds.mean(dim=complement_coords) val = rounding_vals lp_ds = xr.Dataset() for v in keep_vars: lp_ds[v] = fa.data_array_to_logpolar(head_ds[v], *val, dim2keep=keep_list, fun='mean') return lp_ds
def plot_cluster_influence_i( self, colors=None, i=0, ax=None, ylab=False, par_to_plot=co.CPer, ): if colors is None: colors = self.colors if ax is None: fig, ax = plt.subplots() color = colors[i] clus = self.cluster_flags[i] boo = self.merged_ds[co.ClusFlag] == clus # fig,ax = plt.subplots() c_per_ = self.merged_ds.where(boo)[par_to_plot] comp_dims = fa.get_dims_complement(c_per_, co.RL) c_per_.sum(dim=comp_dims).plot(color=color, ax=ax) ax.set_xlabel('arrival time (utc)') if ylab is not False: ax.set_ylabel(ylab) else: ax.set_ylabel(co.PLOT_LABS[par_to_plot]) ax.set_title('cluster {}'.format(clus)) ax.grid(True, 'both') return ax
def plot_hout_influence(self, i, log='False', pM=None): # %% lt = 'Local Time' ds = self.merged_ds.copy() ds[lt] = np.mod(ds[co.RL].dt.hour - 3.5, 24) ds = ds.assign_coords(**{lt: ds[lt]}) dsLP = ds.where(self.merged_ds[co.FLAGS] == i) com = fa.get_dims_complement(dsLP, co.RL) conLP = dsLP[co.CONC].sum(com) conTot = ds[co.CONC].sum(com) conLP = conLP / conTot * 100 cl = 'clog' conLP = conLP.where(conLP > 0) conLP = np.log(conLP) conLP.name = cl df = conLP.to_dataframe() gp = df.groupby(lt) desc = gp.describe()[cl] # %% labs = ['25%', '50%', '75%', 'mean'] desc = desc[labs] desc = np.e**desc if pM is None: yM = None else: yM = desc.quantile(pM).max() # %% fig, ax = plt.subplots() c = self.colors[i] ax.plot(desc.index, desc[labs[1]], color=c, label=labs[1]) ax.plot(desc.index, desc[labs[3]], color='k', label=labs[3]) ax.fill_between(desc.index, desc[labs[0]], desc[labs[2]], color=c, alpha=.2, label=labs[0] + '-' + labs[2]) ax.legend(loc='upper left') ax.set_xlabel(lt) ax.set_ylabel(co.PLOT_LABS[co.CPer]) ax.grid(True) tickrange = np.arange(0, 25, 3) ax.set_xticks(tickrange) ax.set_title('cluster {}'.format(i)) if log: ax.set_yscale('log') ax.set_ylim(None, yM) return ax
def plot_lab_time_series(da_lab, ax_ts: plt.Axes = None): if ax_ts is None: _, ax_ts = plt.subplots() dim_complement = fa.get_dims_complement(da_lab, co.RL) ds_sum: xr.DataArray = da_lab.sum(dim_complement, keep_attrs=True) ds_sum_rolling_mean = ds_sum.rolling(**{co.RL: 24 * 30}, min_periods=24 * 15, center=True).mean() ds_sum_rolling_mean.plot(ax=ax_ts, label='monthly running mean') ds_sum.plot(ax=ax_ts, label='hourly values') ax_ts.set(xlabel='arrival time') ax_ts.legend()
def add_time_per_row(ds, conc_lab, new_lab, long_name="source receptor relationships (SRR)", ): da = ds[conc_lab] da_tot = da.sum(fa.get_dims_complement(da, co.RL)) da_per = da / da_tot * 100 # return da_tot da_per.name = new_lab attr_dict = { 'long_name': long_name, # 'total' : da_tot.item() , 'units': '% over time step' } da_per = da_per.assign_attrs(attr_dict) ds[new_lab] = da_per
def add_conc_vars(self): """ adds extra CON variables derived from original co.CONC :return: """ CPer = co.CPer dim_complement = fa.get_dims_complement(self.merged_ds, co.RL) tot = self.merged_ds[co.CONC].sum(dim=dim_complement) self.merged_ds[CPer] = self.merged_ds[co.CONC] / tot * 100 CC = co.CC CCPer = co.CCPer if co.VOL in list(self.merged_ds.coords): self.merged_ds[CC] = self.merged_ds[co.CONC] / self.merged_ds[ co.VOL] pprint('using vol for conc') else: pprint('using area for conc') self.merged_ds[CC] = self.merged_ds[co.CONC] / self.merged_ds[ co.GA] tot = self.merged_ds[CC].sum(dim=dim_complement) self.merged_ds[CCPer] = self.merged_ds[CC] / tot * 100
def plot_clust_height(self, ax: plt.Axes, i, perM, drop_zero=True, par_to_plot=co.COL): clus = self.cluster_flags[i] boo = self.merged_ds[co.ClusFlag] == clus ar = self.merged_ds[par_to_plot].where(boo) com = fa.get_dims_complement(ar, [co.R_CENTER, co.ZM]) ar = ar.sum(dim=com) if par_to_plot is co.CPer: ar = ar / self.merged_ds[par_to_plot].sum() * 100 if drop_zero: ar = ar.where(ar > 0) q = ar.quantile(perM) lab = "km from CHC" ar[lab] = ar[co.R_CENTER] * 100 ar = ar.swap_dims({co.R_CENTER: lab}) try: ar.name = co.PLOT_LABS[par_to_plot] except AssertionError as error: log.ger.error(error) pass ar.plot( cmap=fa.get_custom_cmap(self.colors[i]), vmin=0, vmax=q, ax=ax, x=lab, ) ax.set_yscale('log') ax.set_ylim(100, 20000) ax.set_xscale('log') ax.set_xlim(.05 * 100, 30 * 100) ax.grid(True, 'major', axis='y') ax.grid(True, 'both', axis='x') ax.set_ylabel(co.PLOT_LABS[co.ZM])
path = '/Volumes/mbProD/Downloads/flx_log_coor/run_2019-06-05_18-42-11_' path = '/Volumes/mbProD/Downloads/flx_log_coor/run_2019-06-10_11-10-03_' # flp = FLP.FlexLogPol(path,concat=True) # self = FLP.FlexLogPol(path,concat=False) self = FLP.FlexLogPol( path, # concat=True, concat=False, get_clusters=False, open_merged=True, clusters_avail=False) # %% # %% com = fa.get_dims_complement(self.merged_ds, [co.R_CENTER, co.TH_CENTER]) sds = self.merged_ds.sum(com) ax = fa.get_ax_bolivia() fa.logpolar_plot(sds[co.CONC], name=co.CONC, ax=ax, drop_zeros=False) # %% ax = fa.get_ax_bolivia() fa.logpolar_plot(self.merged_ds[co.TOPO], name=co.TOPO, ax=ax, drop_zeros=False) # %% self.reset_z_levels() # %%
alpha=.5, label=f'global median = {np.round(q50)}') ax.axhline(_q50, color='r', linestyle='--', alpha=.5, label=f'global = {np.round(_q50)}') ax.axhline(_q50, color='r', linestyle='-', alpha=.5, label=f'tot = {np.round(_tot)}') ax.legend() # %% _da.sum(fa.get_dims_complement(_da, [co.TIME])).plot() # %% nds[co.CONC].sum(fa.get_dims_complement(nds[co.CONC], [co.TIME])).plot() # %% _n = nds[{co.RL: -1}] _n1 = _n.coarsen(**{ co.TIME: 11 }, boundary='trim').sum().sum(co.ZM)[co.CONC].load() _n1.plot.pcolormesh(col=co.TIME, col_wrap=5) # %% args = {
fls_dic['january night'], agl=agl) f, ax = plt.subplots() ax: plt.Axes new_ds.plot(hue='case', ax=ax, alpha=.5) jn.plot(ax=ax, color='k') if agl: ax.set_ylabel('Z AGL') else: ax.set_ylabel('Z ASL') plt.show() return res_list def get_mid_sums(case, ds_conc, flags): complement = fa.get_dims_complement(ds_conc, co.ZM) res = ds_conc.where(flags).sum(complement) res.name = case return res def get_mid_sums_zagl(case, ds_conc, flags): complement = fa.get_dims_complement(ds_conc, co.ZM) d_fl = ds_conc.where(flags) zagl = d_fl[co.ZM] - d_fl[co.TOPO] res = d_fl.sum(complement) res.name = case return res
def plot_absolute_height(self, i, ax=None, perM=.95, drop_zero=True, par_to_plit=co.COL): mer = self.merged_ds.copy() # i = 6 clus = self.cluster_flags[i] H = co.H fla = co.FLAGS HC = 'H*CONC' ver_area = 'VER_AREA' log_center = np.log(mer[co.R_CENTER]) dis = log_center - \ log_center.shift({co.R_CENTER: 1}) dis = dis.median() l1 = log_center - dis / 2 l2 = log_center + dis / 2 l1 = np.e**l1 l2 = np.e**l2 r_dis = (l2 - l1) * 100000 zlen = 500 ar = np.arange(zlen / 2, 20000, zlen) mer = mer[[co.CONC, fla, co.TOPO]] # return merged_ds mer = mer.where(mer[fla] == clus, 0) mer['c/v'] = mer[co.CONC] / mer[co.VOL] mer = mer.interp(**{co.ZM: ar}) mer[co.VOL] = mer[co.GA] * zlen mer[co.CONC] = mer['c/v'] * mer[co.VOL] mer[H] = mer[co.TOPO] + mer[co.ZM] mer[HC] = mer[H] * mer[co.CONC] var = [co.CONC, HC] com = fa.get_dims_complement(mer, [co.R_CENTER, co.ZM]) ms = mer[var].sum(com) ms[ver_area] = ms[co.ZLM] * r_dis # ms:xr.DataArray = ms/ms[ver_area] ms[co.CONC] = ms[co.CONC].where(ms[co.CONC] > 0, 0) # ms = ms*zlen*r_dis ms[H] = ms[HC] / ms[co.CONC] def find_nearest(value): # ar = self.get_z_lin() array = np.asarray(ar) idx = (np.abs(array - value)).argmin() return array[idx] ms[H] = xr.apply_ufunc(find_nearest, ms[H], vectorize=True) hs = ms.to_dataframe().groupby([H, co.R_CENTER ]).sum()[co.CONC].to_xarray() lab = "km from CHC" hs[lab] = hs[co.R_CENTER] * 100 hs = hs.swap_dims({co.R_CENTER: lab}) hs1 = hs.interp(**{H: ar}) hs1 = hs1.combine_first(hs) if drop_zero: hs1 = hs1.where(hs1 > 0) if ax is None: fig, ax = plt.subplots(figsize=(10, 5)) hs1 = hs1 / self.merged_ds[co.CONC].sum() * 100 hs1.name = co.CPer q = hs1.quantile(perM) hs1.name = co.PLOT_LABS[co.CPer] hs1.plot( cmap=fa.get_custom_cmap(self.colors[i]), vmin=0, vmax=q, ax=ax, x=lab, ) ax.set_ylim(100, 20000) ax.set_xscale('log') ax.set_xlim(.05 * 100, 30 * 100) ax.grid(True, 'major', axis='y') ax.grid(True, 'both', axis='x') ax.set_ylabel(co.PLOT_LABS[co.H])
def plot_conc_over_time(self): comp = fa.get_dims_complement(self.merged_ds, co.RL) res = self.merged_ds[co.CONC].sum(dim=comp) res.plot()
path, # concat=True, concat=False, get_clusters=False, open_merged=True, clusters_avail=False ) # %% ds = self.merged_ds # %% ds # %% _c = fa.get_dims_complement(ds,[co.RL]) ds_sum = ds.sum(_c) _ds = ds_sum[co.CONC] _ds = _ds.resample(**{co.RL:'1H'}).mean() _n = _ds.isnull().to_dataframe()[co.CONC] _vc = _n.value_counts()/_n.count() * 100 import pprint _p = pprint.pformat(_vc,compact=True) ax = _vc.plot.bar() ax.set_title('[%] missing dates\n' + _p)