def winddir_scatter(metdat, catinfo, category, vertloc=80, basecolor='red', exclude_angles=[(46, 228)]): ########################################### """ make scatter plot from pandas.Series wind direction and some other value of the same size. Includes blocked off angles from IEC standards. Plot monthly average profiles against one another. Parameters: metdat: Pandas dataframe containing met mast data catinfo: dict containing categorization info for the metmast data. Fore each category, catinfo holds column names, labels, units, and save names category: string specifying category of information to plot (e.g. 'speed', 'stability', etc.) vertloc: int or float describing the exact or approximate height of interest along the tower basecolor: string with the color code info to get from utils. exclude_angles: tuple or list of tuples of start and stop angles to shade out regions according to IEC standards """ # set up data dircol, _, _ = utils.get_vertical_locations( catinfo['columns']['direction'], location=vertloc) varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) colors = utils.get_nrelcolors() fig = plt.figure(figsize=(8, 2.5)) ax = fig.add_subplot(111) ax.scatter(metdat[dircol], metdat[varcol], marker='o', facecolor='w', color='k', lw=0.5, alpha=0.7) ax.set_xlim([0, 360]) for ii in range(len(exclude_angles)): ax.axvspan(exclude_angles[ii][0], exclude_angles[ii][1], alpha=0.1, color=colors[basecolor][0]) # ax.set_title(r'$z={}$ m'.format(vertloc)) ax.set_xlabel(r'Wind Direction [$^\circ$]') ax.set_ylabel(catinfo['labels'][category]) return fig, ax #, leg
def monthly_hist(metdat, catinfo, category, vertloc=80, basecolor='blue'): ########################################### """ Histogram of a given field without any sorting. Parameters: metdat: Pandas dataframe containing met mast data catinfo: dict containing categorization info for the metmast data. Fore each category, catinfo holds column names, labels, units, and save names category: string specifying category of information to plot (e.g. 'speed', 'stability', etc.) vertloc: int or float describing the exact or approximate height of interest along the tower basecolor: string with the color code info to get from utils. """ colors = utils.get_nrelcolors() color = colors[basecolor][0] months = utils.monthnames() # set up data varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) temp = metdat.groupby(metdat.index.month) temp = temp[varcol] binwidth = (metdat[varcol].dropna().max() - metdat[varcol].dropna().min()) / 35 bins = np.arange(metdat[varcol].dropna().min(), metdat[varcol].dropna().max(), binwidth) fig, ax = plt.subplots(4, 3, figsize=(9, 9), sharex=True, sharey=True) for im, month in enumerate(months): data = temp.get_group(im + 1).dropna() ax.flatten()[im].hist(data, bins=bins, color=color, edgecolor='k', weights=np.ones(len(data)) / len(data) * 100) ax.flatten()[im].set_title(month, fontsize=12) fig.tight_layout() fig.text(0, 0.5, 'Frequency [%]', rotation='vertical', ha='center', va='center') fig.text(0.5, 0, catinfo['labels'][category], ha='center', va='center') return fig, ax
def monthly_hist(metdat, catinfo, category, vertloc=80, basecolor='blue'): """**Get Monthly Histogram Figure**. Plot the monthly histogram of a given variable (or category of variables) grouped by a given condition (or set of conditions). Parameters: 1. metdat (Pandas DataFrame): The desired input data (Met Mast). 2. catinfo (dictionary): Categorization information for the desired input data. Holds column names, labels, units, and save names. 3. category (string): Specifies the category of information that is desired for plotting. 4. vertloc (integer, float) [default: 80]: Describes the desired vertical location alond the tower for analysis. 5. basecolor (string) [default: 'blue']: Provides the color code information to get from "utils.py". Returns: 1. fig (Matplotlib Figure): The figure object for the desired input data and categories. 2. ax (Matplotlib Axes): The axes object for the desired input data and categories. """ colors = utils.get_nrelcolors() color = colors[basecolor][0] months = utils.monthnames() # set up data varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) temp = metdat.groupby(metdat.index.month) temp = temp[varcol] binwidth = (metdat[varcol].dropna().max() - metdat[varcol].dropna().min()) / 35 bins = np.arange(metdat[varcol].dropna().min(), metdat[varcol].dropna().max(), binwidth) fig, ax = plt.subplots(4, 3, figsize=(9, 9), sharex=True, sharey=True) for im, month in enumerate(months): data = temp.get_group(im + 1).dropna() ax.flatten()[im].hist(data, bins=bins, color=color, edgecolor='k', weights=np.ones(len(data)) / len(data) * 100) ax.flatten()[im].set_title(month, fontsize=12) fig.tight_layout() fig.text(0, 0.5, 'Frequency [%]', rotation='vertical', ha='center', va='center') fig.text(0.5, 0, catinfo['labels'][category], ha='center', va='center') return fig, ax
def winddir_scatter(metdat, catinfo, category, vertloc=80, basecolor='red', exclude_angles=[(46, 228)]): """**Get Wind Direction Scatter Figure**. Plot the wind direction scatter of a given variable (or category of variables) grouped by a given condition (or set of conditions). Parameters: 1. metdat (Pandas DataFrame): The desired input data (Met Mast). 2. catinfo (dictionary): Categorization information for the desired input data. Holds column names, labels, units, and save names. 3. category (string): Specifies the category of information that is desired for plotting. 4. vertloc (integer, float): Describes the desired vertical location alond the tower for analysis. 5. basecolor (string): Provides the color code information to get from "utils.py". 6. exclude_angles (tuple, list): Defines the start and stop angles to shade out regions according to International Electrotechnical Commission (IEC) standards. Returns: 1. fig (Matplotlib Figure): The figure object for the desired input data and categories. 2. ax (Matplotlib Axes): The axes object for the desired input data and categories. """ # set up data dircol, _, _ = utils.get_vertical_locations( catinfo['columns']['direction'], location=vertloc) varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) colors = utils.get_nrelcolors() fig = plt.figure(figsize=(8, 2.5)) ax = fig.add_subplot(111) ax.scatter(metdat[dircol], metdat[varcol], marker='o', facecolor='w', color='k', lw=0.5, alpha=0.7) ax.set_xlim([0, 360]) for ii in range(len(exclude_angles)): ax.axvspan(exclude_angles[ii][0], exclude_angles[ii][1], alpha=0.1, color=colors[basecolor][0]) ax.set_title(r'$z={}$ m'.format(vertloc)) ax.set_xlabel(r'Wind Direction [$^\circ$]') ax.set_ylabel(catinfo['labels'][category]) return fig, ax #, leg
def scatter_by_height(metdat, catinfo, category1, category2, vertloc=80, basecolor='red'): ########################################### """ make scatter plot from pandas.Series wind direction and some other value of the same size. Includes blocked off angles from IEC standards. Plot monthly average profiles against one another. Parameters: metdat: Pandas dataframe containing met mast data catinfo: dict containing categorization info for the metmast data. Fore each category, catinfo holds column names, labels, units, and save names category1: string specifying category of information to plot (e.g. 'speed', 'stability', etc.) category2: string specifying category of information to plot (e.g. 'speed', 'stability', etc.) vertloc: int or float describing the exact or approximate height of interest along the tower basecolor: string with the color code info to get from utils. exclude_angles: tuple or list of tuples of start and stop angles to shade out regions according to IEC standards """ # set up data varcolx, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category1], location=vertloc) varcoly, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category2], location=vertloc) colors = utils.get_nrelcolors() fig = plt.figure(figsize=(7, 5)) ax = fig.add_subplot(111) ax.scatter(metdat[varcolx], metdat[varcoly], marker='.', facecolor=colors[basecolor][1], color='k', lw=0.5, alpha=0.9) # ax.set_title(r'$z={}$ m'.format(vertloc)) ax.set_xlabel(catinfo['labels'][category1]) ax.set_ylabel(catinfo['labels'][category2]) return fig, ax #, leg
def fit_weibull(data, ax, labels=True, basecolor='red', xy=(0.95, 0.9)): ########################################### """ Fit a weibull distribution to wind speed data paramters: data - input wind speed data to fit ax - axis to plot onto bins - x locations of bins from histogram """ colors = utils.get_nrelcolors() if basecolor == 'red': pcolor = colors['red'][1] elif basecolor is 'blue': pcolor = colors['blue'][0] else: pcolor = 'k' # get limiting value (~1) fixpt = 1.0 - np.finfo(float).eps # get x values along axis xmin, xmax = data.min(), data.max() xdata = np.linspace(xmin, xmax, len(data)) # fit a weibull distribution # floc=0 keeps the location fixed at zero, # f0=1 keeps the first shape parameter of the exponential weibull fixed at 1 fitparams = stats.exponweib.fit(data, floc=0, f0=fixpt) fitdat = stats.exponweib.pdf( xdata, *fitparams) # now get theoretical values in our interval # add fit to plot and annotate ax.plot(xdata, fitdat, label="Weib", color=pcolor) if labels is True: shape = fitparams[1].round(2) scale = fitparams[3].round(2) ax.annotate(s='shape = {}\nscale = {}\nN = {}'.format( shape, scale, len(data)), xy=xy, xycoords='axes fraction', ha='right', va='top') ax.set_xlim(left=-1, right=xmax)
def hist(metdat, catinfo, category, vertloc=80, basecolor='blue'): """**Get Histogram Figure**. Plot the histogram of a given variable (or category of variables) grouped by a given condition (or set of conditions). Parameters: 1. metdat (Pandas DataFrame): The desired input data (Met Mast). 2. catinfo (dictionary): Categorization information for the desired input data. Holds column names, labels, units, and save names. 3. category (string): Specifies the category of information that is desired for plotting. 4. vertloc (integer, float) [default: 80]: Describes the desired vertical location alond the tower for analysis. 5. basecolor (string) [default: 'blue']: Provides the color code information to get from "utils.py". Returns: 1. fig (Matplotlib Figure): The figure object for the desired input data and categories. 2. ax (Matplotlib Axes): The axes object for the desired input data and categories. """ colors = utils.get_nrelcolors() color = colors[basecolor][0] # set up data varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) data = metdat[varcol].dropna(how='any') fig, ax = plt.subplots(figsize=(5, 3)) ax.hist(data, bins=35, facecolor=color, edgecolor='k', weights=np.ones(len(data)) / len(data), density=False) ax.set_title(r'$z={}$ m'.format(vertloc)) fig.text(0, 0.5, 'Frequency [%]', rotation='vertical', ha='center', va='center') fig.text(0.5, 0, catinfo['labels'][category], ha='center', va='center') fig.tight_layout() return fig, ax
def hist(metdat, catinfo, category, vertloc=80, basecolor='blue'): ########################################### """ Histogram of a given field without any sorting. Parameters: metdat: Pandas dataframe containing met mast data catinfo: dict containing categorization info for the metmast data. Fore each category, catinfo holds column names, labels, units, and save names category: string specifying category of information to plot (e.g. 'speed', 'stability', etc.) vertloc: int or float describing the exact or approximate height of interest along the tower basecolor: string with the color code info to get from utils. """ colors = utils.get_nrelcolors() color = colors[basecolor][0] # set up data varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) data = metdat[varcol].dropna(how='any') fig, ax = plt.subplots(figsize=(5, 3)) ax.hist(data, bins=35, facecolor=color, edgecolor='k', weights=np.ones(len(data)) / len(data), density=False) ax.set_title(r'$z={}$ m'.format(vertloc)) fig.text(0, 0.5, 'Frequency [%]', rotation='vertical', ha='center', va='center') fig.text(0.5, 0, catinfo['labels'][category], ha='center', va='center') fig.tight_layout() return fig, ax
def stability_winddir_scatter(metdat, catinfo, category, vertloc=80, basecolor='red', exclude_angles=[(46, 228)]): """**Get Wind Direction Stability Scatter Figure**. Plot the wind direction stability scatter of a given variable (or category of variables) grouped by a given condition (or set of conditions). Parameters: 1. metdat (Pandas DataFrame): The desired input data (Met Mast). 2. catinfo (dictionary): Categorization information for the desired input data. Holds column names, labels, units, and save names. 3. category (string): Specifies the category of information that is desired for plotting. 4. vertloc (integer, float) [default: 80]: Describes the desired vertical location alond the tower for analysis. 5. basecolor (string) [default: 'red']: Provides the color code information to get from "utils.py". 6. exclude_angles (tuple, list) [default: [(46, 228)]]: Defines the start and stop angles to shade out regions according to International Electrotechnical Commission (IEC) standards. Returns: 1. fig (Matplotlib Figure): The figure object for the desired input data and categories. 2. ax (Matplotlib Axes): The axes object for the desired input data and categories. """ stabconds = utils.get_stabconds() colors = utils.get_colors(len(stabconds), basecolor='span') nrelcolors = utils.get_nrelcolors() # Set up data dircol, _, _ = utils.get_vertical_locations( catinfo['columns']['direction'], location=vertloc) varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) stabcol, _, _ = utils.get_vertical_locations( catinfo['columns']['stability flag'], location=vertloc) # dirind = utils.get_nearest_direction(metdat[category]) fig, ax = plt.subplots(len(stabconds), 1, sharex=True, sharey=True, figsize=(6, 8)) plotdat = metdat.groupby(stabcol) for ind, stabcond in enumerate(stabconds): ax.flatten()[ind].scatter(plotdat[dircol].get_group(stabcond), plotdat[varcol].get_group(stabcond), marker='o', facecolor=colors[ind], color='k', lw=0.5, alpha=0.7) ax.flatten()[ind].set_xlim([0, 360]) # ax.flatten()[ind].set_ylim([0,120]) ax.flatten()[ind].legend([stabcond], fontsize=12, loc=1, frameon=False) for ii in range(len(exclude_angles)): ax.flatten()[ind].axvspan(exclude_angles[ii][0], exclude_angles[ii][1], alpha=0.1, color=nrelcolors[basecolor][0]) if ind == 0: ax.flatten()[ind].set_title(r'$z={}$ m'.format(vertloc)) fig.tight_layout() fig.text(0.5, 0, r'Wind Direction [$^\circ$]', ha='center', va='center') fig.text(0, 0.5, catinfo['labels'][category], ha='center', va='center', rotation='vertical') return fig, ax #, leg
def fit_gaussian(data, bins, ax, labels=True, basecolor='red', xy=(0, 0.9), gamma=-0.5): ########################################### """ Fit a Gaussian distribution to wind speed data paramters: data - input wind speed data to fit ax - axis to plot onto bins - x locations of bins from histogram """ colors = utils.get_nrelcolors() if basecolor == 'red': pcolor = colors['red'][1] elif basecolor is 'blue': pcolor = colors['blue'][0] else: pcolor = 'k' # get x and y data yvals, xvals = np.histogram(data, bins=bins) # center x values xvals = np.array([(xvals[i] + xvals[i + 1]) / 2 for i in range(len(xvals) - 1)]) model = lmfmodels.GaussianModel() # set initial parameter values params = model.make_params(amplitude=10, center=data.mean(), sigma=data.std(), gamma=gamma) # adjust parameters to best fit data. result = model.fit(yvals, params, x=xvals) fitdat = result.best_fit / len(data) ax.plot(xvals, result.best_fit * 1.0 / float(len(data)), color=pcolor, linewidth=2.5) if labels is True: # gamma = np.round(result.params['gamma'].value,2) sigma = np.round(result.params['sigma'].value, 2) center = np.round(result.params['center'].value, 2) amp = np.round(result.params['amplitude'].value, 2) if gamma > 0: xcoord = 0.95 else: xcoord = 0.05 if xy[0] > 1: xcoord = 0 xy = (xcoord + xy[0], xy[1]) ax.annotate( s='$A = {}$\n$\mu = {}$\n$\gamma = {}$\n$\sigma = {}$'.format( amp, center, gamma, sigma), xy=xy, xycoords='axes fraction', ha='right', va='top')
def stability_winddir_scatter(metdat, catinfo, category, vertloc=80, basecolor='red', exclude_angles=[(46, 228)]): ########################################### """ make scatter plot from pandas.Series wind direction and some other value of the same size. Includes blocked off angles from IEC standards. Subplots correspond to stability conditions from Obukhov length Parameters: metdat: Pandas dataframe containing met mast data catinfo: dict containing categorization info for the metmast data. Fore each category, catinfo holds column names, labels, units, and save names category: string specifying category of information to plot (e.g. 'speed', 'stability', etc.) vertloc: int or float describing the exact or approximate height of interest along the tower basecolor: string with the color code info to get from utils. exclude_angles: tuple or list of tuples of start and stop angles to shade out regions according to IEC standards """ stabconds = utils.get_stabconds() colors = utils.get_colors(5, basecolor='span') nrelcolors = utils.get_nrelcolors() # set up data dircol, _, _ = utils.get_vertical_locations( catinfo['columns']['direction'], location=vertloc) varcol, vertloc, _ = utils.get_vertical_locations( catinfo['columns'][category], location=vertloc) stabcol, _, _ = utils.get_vertical_locations( catinfo['columns']['stability flag'], location=vertloc) # dirind = utils.get_nearest_direction(metdat[category]) fig, ax = plt.subplots(5, 1, sharex=True, sharey=True, figsize=(6, 8)) plotdat = metdat.groupby(stabcol) for ind, stabcond in enumerate(stabconds): ax.flatten()[ind].scatter(plotdat[dircol].get_group(stabcond), plotdat[varcol].get_group(stabcond), marker='o', facecolor=colors[ind], color='k', lw=0.5, alpha=0.7) ax.flatten()[ind].set_xlim([0, 360]) # ax.flatten()[ind].set_ylim([0,120]) ax.flatten()[ind].legend([stabcond], fontsize=12, loc=1, frameon=False) for ii in range(len(exclude_angles)): ax.flatten()[ind].axvspan(exclude_angles[ii][0], exclude_angles[ii][1], alpha=0.1, color=nrelcolors[basecolor][0]) # if ind == 0: # ax.flatten()[ind].set_title(r'$z={}$ m'.format(vertloc)) fig.tight_layout() fig.text(0.5, 0, r'Wind Direction [$^\circ$]', ha='center', va='center') fig.text(0, 0.5, catinfo['labels'][category], ha='center', va='center', rotation='vertical') return fig, ax #, leg