def test_declarative_station_plot_fontsize(): """Test adjusting the font size for station plots in PlotObs.""" data = parse_metar_file(get_test_data('metar_20190701_1200.txt', as_file_obj=False), year=2019, month=7) obs = PlotObs() obs.data = data obs.time = datetime(2019, 7, 1, 12) obs.time_window = timedelta(minutes=15) obs.level = None obs.fields = ['cloud_coverage', 'air_temperature', 'dew_point_temperature', 'air_pressure_at_sea_level', 'current_wx1_symbol'] obs.plot_units = [None, 'degF', 'degF', None, None] obs.locations = ['C', 'NW', 'SW', 'NE', 'W'] obs.formats = ['sky_cover', None, None, lambda v: format(v * 10, '.0f')[-3:], 'current_weather'] obs.reduce_points = 3 obs.vector_field = ['eastward_wind', 'northward_wind'] obs.fontsize = 8 panel = MapPanel() panel.area = 'centus' panel.projection = 'lcc' panel.layers = ['coastline', 'borders', 'usstates'] panel.plots = [obs] pc = PanelContainer() pc.size = (8, 8) pc.panels = [panel] pc.draw() return pc.figure
def test_declarative_multiple_sfc_obs_change_units(ccrs): """Test making a surface observation plot.""" data = parse_metar_file(get_test_data('metar_20190701_1200.txt', as_file_obj=False), year=2019, month=7) obs = PlotObs() obs.data = data obs.time = datetime(2019, 7, 1, 12) obs.time_window = timedelta(minutes=15) obs.level = None obs.fields = ['air_temperature', 'dew_point_temperature', 'air_pressure_at_sea_level'] obs.locations = ['NW', 'W', 'NE'] obs.colors = ['red', 'green', 'black'] obs.reduce_points = 0.75 obs.plot_units = ['degF', 'degF', None] # Panel for plot with Map features panel = MapPanel() panel.layout = (1, 1, 1) panel.projection = ccrs.PlateCarree() panel.area = 'in' panel.layers = ['states'] panel.plots = [obs] # Bringing it all together pc = PanelContainer() pc.size = (12, 12) pc.panels = [panel] pc.draw() return pc.figure
pc.size = (15, 15) pc.panels = [panel] # Show the figure pc.show() ######################################################################### # Plot Surface Obs # ---------------- # # We can also plot surface (or upper-air) observations at point locations using the simplified # syntax. Whether it is surface or upper-air data, the ``PlotObs()`` class is what you would # want to use. Then you would add those observations to a map panel and then collect the panels # to plot the figure; similar to what you would do for a gridded plot. df = metar.parse_metar_file(get_test_data('metar_20190701_1200.txt', False), year=2019, month=7) # Let's take a look at the variables that we could plot coming from our METAR observations. print(df.keys()) # Set the observation time obs_time = datetime(2019, 7, 1, 12) ######################################################################### # Setting of our attributes for plotting observations is pretty straignforward and just needs # to be lists for the variables, and a comparable number of items for plot characteristics that # are specific to the individual fields. For example, the locations around a station plot, the # plot units, and any plotting formats would all meed to have the same number of items as the # fields attribute. #
import matplotlib.pyplot as plt from metpy.calc import reduce_point_density from metpy.cbook import get_test_data from metpy.io import metar from metpy.plots import add_metpy_logo, current_weather, sky_cover, StationPlot ########################################### # The setup # --------- # # First read in the data. We use the metar reader because it simplifies a lot of tasks, # like dealing with separating text and assembling a pandas dataframe # https://thredds-test.unidata.ucar.edu/thredds/catalog/noaaport/text/metar/catalog.html data = metar.parse_metar_file(get_test_data('metar_20190701_1200.txt', as_file_obj=False)) # Drop rows with missing winds data = data.dropna(how='any', subset=['wind_direction', 'wind_speed']) ########################################### # This sample data has *way* too many stations to plot all of them. The number # of stations plotted will be reduced using `reduce_point_density`. # Set up the map projection proj = ccrs.LambertConformal(central_longitude=-95, central_latitude=35, standard_parallels=[35]) # Use the Cartopy map projection to transform station locations to the map and # then refine the number of stations plotted by setting a 300km radius point_locs = proj.transform_points(ccrs.PlateCarree(), data['longitude'].values,
from io import StringIO from urllib.request import urlopen from metpy.io import metar from metpy.plots.declarative import * from metpy.units import units # Set Date date = datetime(2020, 12, 20, 12) # Get Data data = StringIO( urlopen('http://bergeron.valpo.edu/current_surface_data/' f'{date:%Y%m%d%H}_sao.wmo').read().decode('utf-8', 'backslashreplace')) df = metar.parse_metar_file(data, year=date.year, month=date.month) # Plot Data obs = PlotObs() obs.data = df obs.time = date obs.time_window = timedelta(minutes=15) obs.level = None obs.fields = ['air_temperature'] # Panel for plot with Map features panel = MapPanel() panel.layout = (1, 1, 1) panel.projection = 'lcc' panel.area = 'in' panel.layers = ['states']