def plot_geo(geo, coastline, symbol, legend, title, code): with NamedTemporaryFile(delete=False, suffix=f"__WT_{code}.pdf") as pdf: pdf_obj = mv.pdf_output(output_name=pdf.name.replace(".pdf", "")) mv.setoutput(pdf_obj) mv.plot(coastline, symbol, legend, title, geo) os.chmod(pdf.name, 0o444) return {"pdf": pdf.name}
def plot_obs_freq(predictor_matrix, code): coastline = mv.mcoast(map_coastline_thickness=2, map_boundaries="on", map_coastline_colour="chestnut") symbol = mv.msymb( legend="on", symbol_type="marker", symbol_table_mode="on", symbol_outline="on", symbol_min_table=[1, 2, 5, 10, 15, 20, 25, 30], symbol_max_table=[2, 5, 10, 15, 20, 25, 30, 100000], symbol_colour_table=[ "RGB(0.7020,0.7020,0.7020)", "RGB(0.4039,0.4039,0.4039)", "blue", "RGB(0.4980,1.0000,0.0000)", "RGB(1.0000,0.8549,0.0000)", "orange", "red", "magenta", ], symbol_marker_table=15, symbol_height_table=0.3, ) legend = mv.mlegend( legend_text_font="arial", legend_text_font_size=0.35, legend_entry_plot_direction="row", legend_box_blanking="on", legend_entry_text_width=50, ) title = mv.mtext( text_line_count=4, text_line_1= "OBS Frequency", # To sostitute with "FE" values when relevant. text_line_2=f"WT Code = {code}", text_line_4=" ", text_font="arial", text_font_size=0.4, ) df = predictor_matrix[["LonOBS", "LatOBS", "OBS"]] grouped_df = df.groupby(["LatOBS", "LonOBS"], as_index=False).count() geo = mv.create_geo(len(grouped_df), "xyv") geo = mv.set_latitudes(geo, grouped_df["LatOBS"].to_numpy(dtype=np.float)) geo = mv.set_longitudes(geo, grouped_df["LonOBS"].to_numpy(dtype=np.float)) geo = mv.set_values(geo, grouped_df["OBS"].to_numpy(dtype=np.float)) with NamedTemporaryFile(delete=False, suffix=".pdf") as pdf: pdf_obj = mv.pdf_output(output_name=pdf.name.replace(".pdf", "")) mv.setoutput(pdf_obj) mv.plot(coastline, symbol, legend, title, geo) return pdf.name
def plot_std(predictor_matrix, code): coastline = mv.mcoast(map_coastline_thickness=2, map_boundaries="on", map_coastline_colour="chestnut") symbol = mv.msymb( legend="on", symbol_type="marker", symbol_table_mode="on", symbol_outline="on", symbol_min_table=[0, 0.0001, 0.5, 1, 2, 5], symbol_max_table=[0.0001, 0.5, 1, 2, 5, 1000], symbol_colour_table=[ "RGB(0.7020,0.7020,0.7020)", "RGB(0.2973,0.2973,0.9498)", "RGB(0.1521,0.6558,0.5970)", "RGB(1.0000,0.6902,0.0000)", "red", "RGB(1.0000,0.0000,1.0000)", ], symbol_marker_table=15, symbol_height_table=0.3, ) legend = mv.mlegend( legend_text_font="arial", legend_text_font_size=0.35, legend_entry_plot_direction="row", legend_box_blanking="on", legend_entry_text_width=50, ) error = "FER" if "FER" in predictor_matrix.columns else "FE" title = mv.mtext( text_line_count=4, text_line_1=f"{error} Standard Deviation", text_line_2=f"WT Code = {code}", text_line_4=" ", text_font="arial", text_font_size=0.4, ) df = predictor_matrix[["LonOBS", "LatOBS", error]] grouped_df = df.groupby(["LatOBS", "LonOBS"])[error].mean().reset_index() geo = mv.create_geo(len(grouped_df), "xyv") geo = mv.set_latitudes(geo, grouped_df["LatOBS"].to_numpy(dtype=np.float)) geo = mv.set_longitudes(geo, grouped_df["LonOBS"].to_numpy(dtype=np.float)) geo = mv.set_values(geo, grouped_df[error].to_numpy(dtype=np.float)) with NamedTemporaryFile(delete=False, suffix=".pdf") as pdf: pdf_obj = mv.pdf_output(output_name=pdf.name.replace(".pdf", "")) mv.setoutput(pdf_obj) mv.plot(coastline, symbol, legend, title, geo) return pdf.name
def test_plot_2_pages(): output_name = file_in_testdir('test_plot_2_pages') png = mv.png_output(output_name=output_name) degraded_field = mv.read(data=TEST_FIELDSET, grid=[4, 4]) page1 = mv.plot_page(top=2.2, bottom=52.2, right=100) page2 = mv.plot_page(top=50) dw = mv.plot_superpage(pages=[page1, page2]) mv.setoutput(png) mv.plot(dw[0], degraded_field, dw[1], degraded_field + 50) output_name_from_magics = output_name + '.1.png' assert (os.path.isfile(output_name_from_magics)) os.remove(output_name_from_magics)
def plot_geo(geo, coastline, symbol, legend, title): with NamedTemporaryFile(suffix=".png") as png, NamedTemporaryFile( delete=False, suffix=".pdf") as pdf: png_obj = mv.png_output(output_name=png.name.replace(".png", "")) mv.setoutput(png_obj) mv.plot(coastline, symbol, legend, title, geo) pdf_obj = mv.pdf_output(output_name=pdf.name.replace(".pdf", "")) mv.setoutput(pdf_obj) mv.plot(coastline, symbol, legend, title, geo) os.chmod(pdf.name, 0o444) with open(png.name.replace(".png", ".1.png"), "rb") as img: return {"preview": b64encode(img.read()).decode(), "pdf": pdf.name}
def build(self): if not is_ipython_active(): return img_size = 120 img, names = self._build(img_size) # reset jupyter output settings mv.setoutput("jupyter", **mv.plot.jupyter_args) if len(img) > 0: from IPython.display import HTML return HTML( self.build_gallery(names, img, row_height=f"{img_size}px"))
def test_plot_1(): output_name = file_in_testdir('test_plot_1') mv.setoutput(mv.png_output(output_name=output_name)) grid_shade = { 'legend': True, 'contour': False, 'contour_highlight': True, 'contour_shade': True, 'contour_shade_technique': 'grid_shading', 'contour_shade_max_level_colour': 'red', 'contour_shade_min_level_colour': 'blue', 'contour_shade_colour_direction': 'clockwise', } degraded_field = mv.read(data=TEST_FIELDSET, grid=[4, 4]) mv.plot(degraded_field, mv.mcont(grid_shade)) output_name_from_magics = output_name + '.1.png' assert (os.path.isfile(output_name_from_magics)) os.remove(output_name_from_magics)
def _build(self, img_size): img = [] names = [] # img_size = 120 tmp_dir = os.path.join(os.getenv("METVIEW_TMPDIR", ""), "_maparea_") Path(tmp_dir).mkdir(exist_ok=True) for name in map_area_names(): f_name = os.path.join(tmp_dir, name + ".png") if not os.path.exists(f_name): view = mv.make_geoview(area=name, style="grey_1") mv.setoutput( mv.png_output( output_name=f_name[:-4], output_width=300, output_name_first_page_number="off", )) mv.plot(view) if os.path.exists(f_name): names.append(name) img.append(self.to_base64(f_name)) return (img, names)
def _build(self, img_size): img = [] names = [] # img_size = 120 tmp_dir = os.path.join(os.getenv("METVIEW_TMPDIR", ""), "_mapstyle_") Path(tmp_dir).mkdir(exist_ok=True) for name, d in map_styles().items(): f_name = os.path.join(tmp_dir, name + ".png") if not os.path.exists(f_name): view = mv.make_geoview(area=[30, -30, 75, 45], style=name) view.update({"MAP_COASTLINE_RESOLUTION": "low"}, sub="coastlines") mv.setoutput( mv.png_output( output_name=f_name[:-4], output_width=300, output_name_first_page_number="off", )) mv.plot(view) if os.path.exists(f_name): names.append(name) img.append(self.to_base64(f_name)) return (img, names)
map_coastline_sea_shade_colour="RGB(0.85,0.93,1)", ) area_view = mv.geoview( map_area_definition="corners", area=[45.83, -13.87, 62.03, 8.92], coastlines=land_shade, ) # Simplest plot: # NOTE that when plotting a 'raw' BUFR file, Magics will plot synop symbols as shown in # https://software.ecmwf.int/wiki/display/METV/Data+Part+1 "Plotting BUFR Data" obs = mv.read("../tests/obs_3day.bufr") mv.setoutput(mv.png_output(output_width=1200, output_name="./obsplot1")) mv.plot(area_view, obs) # ALTERNATIVELY, add an Observations Plotting visual definition obs_plotting = mv.mobs( obs_temperature=False, obs_cloud=False, obs_low_cloud=False, obs_dewpoint_colour="purple", ) mv.setoutput(mv.png_output(output_width=1200, output_name="./obsplot2")) mv.plot(area_view, obs, obs_plotting) # ALTERNATIVELY, if we don't want to plot the whole observations, but instead want to
# read geopotential z = mv.read("./z_for_UC-04.grib") t_shade_c = mv.mcont( legend=True, contour_highlight=False, contour_level_selection_type="interval", contour_interval=10, contour_shade=True, contour_shade_max_level=60, contour_shade_min_level=-60, contour_shade_method="area_fill", contour_shade_max_level_colour="red", contour_shade_min_level_colour="blue", contour_shade_colour_direction="clockwise", ) z_isolines = mv.mcont( legend=True, contour_line_thickness=2, contour_line_colour="black", contour_highlight_colour="black", contour_highlight_thickness=4, contour_level_selection_type="interval", contour_interval=5, contour_legend_text="Geopotential", ) mv.setoutput(mv.png_output(output_width=1000, output_name="./gribplot")) mv.plot(t2, t_shade_c, z, z_isolines)
t2m_grib = mv.read("./t2m_grib.grib") obs_3day = mv.read("./obs_3day.bufr") t2m_gpt = mv.obsfilter(parameter="012004", output="geopoints", data=obs_3day) diff = t2m_grib - t2m_gpt diff_symb = mv.msymb( legend=True, symbol_type="marker", symbol_table_mode="advanced", ) mv.setoutput(mv.png_output(output_width=1000, output_name="./obsdiff1")) mv.plot(area_view, diff, diff_symb) # Extract geopoints that are hotter by 1 deg or more # hotter = mv.filter(diff, diff >= 1) hotter = diff.filter(diff >= 1) # Extract geopoints that are colder by 1 deg or more # colder = mv.filter(diff, diff <= -1) colder = diff.filter(diff <= -1) # Get geopoints that are within +/-1 # exact = mv.filter(diff, (diff > -1) * (diff < 1)) exact = diff.filter((diff > -1) * (diff < 1)) # Symbol visdefs for each classification
UC-05. The Analyst retrieves certain parameters, computes a derived parameter and plots its values for a specific time. -------------------------------------------------------------------------------- 1. Analyst filters, the chosen parameters of a file from a given source (i.e. MARS, files, observation databases) -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- 2. Analyst computes the desired derived parameter (i.e. wind velocity from zonal and meridional components) -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- 3. Analyst plots the derived parameter values -------------------------------------------------------------------------------- """ import metview as mv uv = mv.read('./uv.grib') u = mv.read(data=uv, param='u') v = mv.read(data=uv, param='v') wind_speed = mv.sqrt(u * u + v * v) mv.setoutput(mv.png_output(output_width=1000, output_name='./uvplot')) mv.plot(wind_speed)
t2m_grib = mv.read('./t2m_grib.grib') obs_3day = mv.read('./obs_3day.bufr') t2m_gpt = mv.obsfilter(parameter='012004', output='geopoints', data=obs_3day) diff = t2m_grib - t2m_gpt diff_symb = mv.msymb( legend=True, symbol_type='marker', symbol_table_mode='advanced', ) mv.setoutput(mv.png_output(output_width=1000, output_name='./obsdiff1')) mv.plot(area_view, diff, diff_symb) # Extract geopoints that are hotter by 1 deg or more #hotter = mv.filter(diff, diff >= 1) hotter = diff.filter(diff >= 1) # Extract geopoints that are colder by 1 deg or more #colder = mv.filter(diff, diff <= -1) colder = diff.filter(diff <= -1) # Get geopoints that are within +/-1 #exact = mv.filter(diff, (diff > -1) * (diff < 1)) exact = diff.filter((diff > -1) * (diff < 1)) # Symbol visdefs for each classification
# (C) Copyright 2021 ECMWF. # # This software is licensed under the terms of the Apache Licence Version 2.0 # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. # In applying this licence, ECMWF does not waive the privileges and immunities # granted to it by virtue of its status as an intergovernmental organisation # nor does it submit to any jurisdiction. # import metview as mv from climetlab.core.ipython import ipython_active if ipython_active: try: import ipywidgets # noqa mv.setoutput("jupyter", plot_widget=True) except ImportError: mv.setoutput("jupyter", plot_widget=False) def mv_read(*args, **kwargs): return mv.read(*args, **kwargs) def mv_read_table(*args, **kwargs): return mv.read_table(*args, **kwargs)
land_shade = mv.mcoast(map_coastline_land_shade=True, map_coastline_land_shade_colour="RGB(0.98,0.95,0.82)", map_coastline_sea_shade=False, map_coastline_sea_shade_colour="RGB(0.85,0.93,1)") area_view = mv.geoview(map_area_definition='corners', area=[45.83, -13.87, 62.03, 8.92], coastlines=land_shade) # Simplest plot: # NOTE that when plotting a 'raw' BUFR file, Magics will plot synop symbols as shown in # https://software.ecmwf.int/wiki/display/METV/Data+Part+1 "Plotting BUFR Data" obs = mv.read('../tests/obs_3day.bufr') mv.setoutput(mv.png_output(output_width=1200, output_name='./obsplot1')) mv.plot(area_view, obs) # ALTERNATIVELY, add an Observations Plotting visual definition obs_plotting = mv.mobs(obs_temperature=False, obs_cloud=False, obs_low_cloud=False, obs_dewpoint_colour='purple') mv.setoutput(mv.png_output(output_width=1200, output_name='./obsplot2')) mv.plot(area_view, obs, obs_plotting) # ALTERNATIVELY, if we don't want to plot the whole observations, but instead want to # extract a specific parameter from the BUFR messages, then we use the Observation Filter # as shown here: