# (C) Copyright 1996-2019 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 xarray as xr import numpy as np from Magics import macro as magics ref = "xarray2" ds = xr.open_dataset('netcdf3_t_z.nc') png = magics.output(output_name_first_page_number = "off", output_name = ref) data = magics.mxarray( xarray_dataset = ds, xarray_variable_name = "z", xarray_dimension_settings = { "time": np.datetime64('2017-10-13T00:00:00.000000000'), "level": 925}), contour = magics.mcont(contour_automatic_setting = "ecmwf") magics.plot(png, data, contour, magics.mcoast())
def geoplot(data): contour = macro.mcont(contour_automatic_setting="ecmwf", legend=True) legend = macro.mlegend(legend_display_type="continuous") return macro.plot(data, contour, macro.mcoast(), macro.mtext(), legend)
def plot_to_file(field, file, size=10., projection=None, contour=None, grid=True, title=True, title_text='Title', width=400, ratio=1.0, area=None, metadata=None, text_format='(automatic)', position=0, format=None, preproc=None, legend=False, boxes=[]): plottable = as_plottable(field, position, metadata, preproc) if isinstance(plottable, list): _, in_file_area, metadata, what = plottable[0] grib = [g[0] for g in plottable] else: grib, in_file_area, metadata, what = plottable # print("XXXX PLOT", what, "metadata =>", metadata, # "contour => ", contour, # "area =>", area) if projection is None: if area is None: area = in_file_area if area: n, w, s, e = area projection = macro.mmap(subpage_upper_right_longitude=float(e), subpage_upper_right_latitude=float(n), subpage_lower_left_latitude=float(s), subpage_lower_left_longitude=float(w), subpage_map_projection='cylindrical') else: projection = macro.mmap(subpage_map_projection='cylindrical') if isinstance(projection, str): projection = PROJECTIONS[projection] contour = make_contour(contour, legend) if isinstance(grib, list) and not isinstance(contour, list): contour = [contour] * len(grib) base, ext = os.path.splitext(file) if format is None: format = ext[1:] output = macro.output(output_formats=[format], output_name_first_page_number='off', page_x_length=float(size), page_y_length=float(size) * ratio, super_page_x_length=float(size), super_page_y_length=float(size) * ratio, subpage_x_length=float(size), subpage_y_length=float(size) * ratio, subpage_x_position=0., subpage_y_position=0., output_width=width, page_frame='on', page_id_line='off', output_name=base) foreground = macro.mcoast(map_grid=ONOFF[grid], map_label='off') background = macro.mcoast(map_grid=ONOFF[grid], map_grid_colour='tan', map_label='off', map_coastline_land_shade='on', map_coastline_land_shade_colour='cream', map_coastline_colour='tan') data = [] data.append(background) if isinstance(grib, list): for g, c in zip(grib, contour): data.append(g) data.append(c) else: data.append(grib) data.append(contour) data.append(foreground) bb = float(len(boxes) + 1) for i, b in enumerate(boxes): inc = 0.1 n, w, s, e = b a = np.ones(((e - w + inc) / inc - 2, ((n - s + inc) / inc) - 2)) a = np.pad(a, 1, 'constant', constant_values=0) b = macro.minput(input_field=a, input_field_initial_latitude=float(n), input_field_latitude_step=-float(inc), input_field_initial_longitude=float(w), input_field_longitude_step=float(inc), input_metadata={}) data.append(b) # print a.shape d = "rgba(1,0,0,%g)" % (i / bb) c = macro.mcont( contour="off", contour_shade="on", contour_shade_method="area_fill", contour_shade_max_level_colour=d, contour_shade_min_level_colour=d, ) data.append(c) if title: data.append(macro.mtext()) if legend: width = 1000 height = 200 width_cm = float(width) / 40. height_cm = float(height) / 40. output = macro.output( output_formats=[format], output_name_first_page_number='off', # output_cairo_transparent_background='on', output_width=width, super_page_x_length=width_cm, super_page_y_length=height_cm, output_name=base, subpage_frame=False, page_frame=False, page_id_line=False) leg = macro.mlegend(legend_title="on", legend_title_font_size=1.1, legend_display_type="continuous", legend_title_text=title_text, legend_text_colour="charcoal", legend_box_mode="positional", legend_text_format=text_format, legend_box_x_position=0.00, legend_box_y_position=0.00, legend_box_x_length=width_cm, legend_box_y_length=height_cm, legend_box_blanking="on", legend_text_font_size="15%", legend_border=False, legend_border_colour="rgb(0.18,0.18,0.62)") with LOCK: # print(output, data[1], data[2], leg) macro.plot(output, data[1], data[2], leg) return data.append(macro.page()) # print(output, projection, data) with LOCK: macro.plot(output, projection, data)
data = magics.minput( input_field=values, input_field_initial_latitude=firstlat, input_field_latitude_step=steplat, input_field_initial_longitude=firstlon, input_field_longitude_step=steplon, input_mars_metadata=json.dumps(metadata), ) #Setting the field contour contour = magics.mcont(contour_shade="on", legend="on", legend_display_type="continuous", contour_highlight="off", contour_shade_method="area_fill", contour_shade_colour_direction="clockwise", contour_shade_colour_method="calculate", contour_shade_max_level_colour="red", contour_shade_min_level_colour=" blue") eccharts = magics.mcont(contour_automatic_setting="ecchart") #Setting the title title = magics.mtext(text_lines=["Using Grib API and arrays..."], text_colour="charcoal", text_font_size='0.8', text_justification='left') #Plot the map magics.plot(output, europe, data, eccharts, magics.mcoast(), title)
def get_mcoast(name='PROVINCE', **kwargs): """ Project coastlines and latitude/longitude grid lines onto Args: name (str, optional): [description]. Defaults to 'PROVINCE'. Return: map coastlines object. """ if name.upper() == 'COAST': kwargs = check_kwargs(kwargs, 'map_coastline_colour', "grey") kwargs = check_kwargs(kwargs, 'map_label', "on") kwargs = check_kwargs(kwargs, 'map_label_height', 0.6) kwargs = check_kwargs(kwargs, 'map_label_right', 'off') kwargs = check_kwargs(kwargs, 'map_label_top', 'off') kwargs = check_kwargs(kwargs, 'map_grid', "on") kwargs = check_kwargs(kwargs, 'map_grid_thickness', 0.2) kwargs = check_kwargs(kwargs, 'map_grid_line_style', "dot") kwargs = check_kwargs(kwargs, 'map_grid_latitude_increment', 10) kwargs = check_kwargs(kwargs, 'map_grid_longitude_increment', 10) coast = magics.mcoast(**kwargs) elif name.upper() == 'COAST_FILL': kwargs = check_kwargs(kwargs, 'map_coastline_colour', "grey") kwargs = check_kwargs(kwargs, 'map_coastline_land_shade', "on") kwargs = check_kwargs(kwargs, 'map_coastline_sea_shade', "on") kwargs = check_kwargs(kwargs, 'map_coastline_land_shade_colour', "cream") kwargs = check_kwargs(kwargs, 'map_coastline_sea_shade_colour', "white") kwargs = check_kwargs(kwargs, 'map_label', "on") kwargs = check_kwargs(kwargs, 'map_label_height', 0.6) kwargs = check_kwargs(kwargs, 'map_label_right', 'off') kwargs = check_kwargs(kwargs, 'map_label_top', 'off') kwargs = check_kwargs(kwargs, 'map_grid', "on") kwargs = check_kwargs(kwargs, 'map_grid_thickness', 0.2) kwargs = check_kwargs(kwargs, 'map_grid_line_style', "dot") kwargs = check_kwargs(kwargs, 'map_grid_latitude_increment', 10) kwargs = check_kwargs(kwargs, 'map_grid_longitude_increment', 10) coast = magics.mcoast(**kwargs) elif name.upper() == 'PROVINCE': shpfile = pkg_resources.resource_filename('nmc_met_graphics', 'resources/maps/bou2_4l') kwargs = check_kwargs(kwargs, 'map_user_layer_colour', "navy") kwargs = check_kwargs(kwargs, 'map_user_layer_thickness', 1) kwargs = check_kwargs(kwargs, 'map_label', "off") kwargs = check_kwargs(kwargs, 'map_grid', "off") coast = magics.mcoast( map_user_layer="on", map_user_layer_name = shpfile, **kwargs) elif name.upper() == 'RIVER': shpfile = pkg_resources.resource_filename('nmc_met_graphics', 'resources/maps/hyd1_4l') kwargs = check_kwargs(kwargs, 'map_user_layer_colour', "blue") kwargs = check_kwargs(kwargs, 'map_user_layer_thickness', 1) kwargs = check_kwargs(kwargs, 'map_label', "off") kwargs = check_kwargs(kwargs, 'map_grid', "off") coast = magics.mcoast( map_user_layer="on", map_user_layer_name = shpfile, **kwargs) else: coast = None return coast
with open(args.json_fields) as json_data: # this should/could be set as "json.load" for pyhton3 mm_fields = json_load_byteified(json_data) with open(args.json_coasts) as json_data: # this should/could be set as "json.load" for pyhton3 mm_coasts = json_load_byteified(json_data) print("filtering grib: " + args.grib) gb_met = group_grib_metadata_by_fstep(args.grib, args.product, args.level) os.environ["MAGPLUS_QUIET"] = "true" area = mm.mmap(**mm_coasts["mmap"]) bg = mm.mcoast(**mm_coasts["background"]) fg = mm.mcoast(**mm_coasts["foreground"]) mm_coasts["mlegend"]["legend_title_text"] = units legend = mm.mlegend(**mm_coasts["mlegend"]) for k, v in gb_met.iteritems(): print("processing: " + args.product + " +" + str(k).zfill(2)) input_data = mm.mgrib( grib_input_file_name = args.grib, grib_field_position = v, grib_automatic_scaling = 'off', grib_scaling_factor = scaling_factor, grib_scaling_offset = scaling_offset, grib_automatic_derived_scaling = 'off',
from Magics import macro as magics #name = 'magics' #Setting of the output file name output = magics.output(output_formats=['png'], output_name_first_page_number="off", output_name="mymagicsplot") #Import the data data = magics.mnetcdf(netcdf_filename="test.nc", netcdf_value_variable="aps") #Apply styling legend = magics.mlegend() contour = magics.mcont(contour_shade="on", contour_shade_method="area_fill", legend='on') coast = magics.mcoast() # Make the plot magics.plot(output, data, contour, coast, legend)
import Magics.macro as magics # Setting the projection projection = magics.mmap(subpage_map_projection="lambert", subpage_lower_left_latitude=-0.00, subpage_lower_left_longitude=-80.00, subpage_upper_right_latitude=70.00, subpage_upper_right_longitude=160.00, subpage_map_area_definition="corners", page_id_line="off") # Defining the coastlines coast = magics.mcoast(map_coastline_resolution="automatic", map_coastline_colour="tan", map_coastline_land_shade="on", map_coastline_land_shade_colour="cream", map_grid="on", map_grid_line_style="dot", map_grid_colour="tan") png = magics.output(output_formats=['png'], output_name_first_page_number="off", output_name="my_png") magics.plot(png, projection, coast)