Creating your own Side-View plot

Sometimes the classes provided by MSS are not enough. This page will show you how our plot classes are structured and how to build your own one. This is an example of a Side-View plot class

"""
    This file is part of MSS.

    :copyright: Copyright 2021-2023 by the MSS team, see AUTHORS.
    :license: APACHE-2.0, see LICENSE for details.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

import numpy as np
import matplotlib as mpl
import mslib.mswms.mpl_vsec_styles


class VS_Template(mslib.mswms.mpl_vsec_styles.AbstractVerticalSectionStyle):
    name = "VSTemplate"  # Pick a proper name starting with "VS"
    title = "Air Temperature"
    abstract = "Air Temperature (degC)"

    required_datafields = [
        # level type, CF standard name, unit
        ("pl", "air_pressure", "Pa"),  # air_pressure must be given for VS plots
        ("pl", "air_temperature", "degC"),
    ]

    def _plot_style(self):
        fill_range = np.arange(-93, 28, 2)
        fill_entity = "air_temperature"
        contour_entity = "air_temperature"

        # main plot
        cmap = mpl.cm.plasma

        cf = self.ax.contourf(
            self.horizontal_coordinate, self.data["air_pressure"], self.data[fill_entity],
            fill_range, cmap=cmap, extend="both")
        self.add_colorbar(cf, fill_entity)

        # contour
        temps_c = self.ax.contour(
            self.horizontal_coordinate, self.data["air_pressure"], self.data[contour_entity], colors="w")
        self.ax.clabel(temps_c, fmt="%i")

        # finalise the plot
        self._latlon_logp_setup()

It produces the following plot, filled with a temperature colourmap and contour lines

gallery/plots/Side_VSTemplate-.png

By cutting the code into segments it will be easier to understand what it does and how to change it to your liking.



class VS_Template(mslib.mswms.mpl_vsec_styles.AbstractVerticalSectionStyle):
    name = "VSTemplate"  # Pick a proper name starting with "VS"
    title = "Air Temperature"
    abstract = "Air Temperature (degC)"

We begin our plots with various identifiers and information which should be self-explanatory.



    required_datafields = [
        # level type, CF standard name, unit
        ("pl", "air_pressure", "Pa"),  # air_pressure must be given for VS plots
        ("pl", "air_temperature", "degC"),
    ]

Within the required_datafields you list all quantities your plot initially needs as a list of 3-tuples containing

  1. The type of vertical level (ml, pl, al, pv, tl, sfc)

  2. The CF standard name of the entity required

  3. The desired unit of the entity

To create a VS plot, you require air_pressure to be present. Do not remove it.



    def _plot_style(self):
        fill_range = np.arange(-93, 28, 2)
        fill_entity = "air_temperature"
        contour_entity = "air_temperature"

First inside the plotting function the desired range of the temperature is set. This will be the range of your colourmap. In this case the colourmap ranges between -93 and 28 °C in steps of 2. Adjust it to your liking. Second it is decided which entity will fill out the plot and which will just be a contour above it. Of course you don’t need both, any one will suffice. In this case, temperature is both the plot filler and the contour.



        # main plot
        cmap = mpl.cm.plasma

        cf = self.ax.contourf(
            self.horizontal_coordinate, self.data["air_pressure"], self.data[fill_entity],
            fill_range, cmap=cmap, extend="both")
        self.add_colorbar(cf, fill_entity)

Now the colourmap is decided, in this case “plasma”. It is best to pick one which best describes your data. Here is a list of all available ones. The rest of the code you normally don’t have to touch, but feel free to if you like. Afterwards the plot is filled with the fill_entity and a corresponding colour bar is created. Of course if you only want a contour plot, you can delete this part of the code.



        # contour
        temps_c = self.ax.contour(
            self.horizontal_coordinate, self.data["air_pressure"], self.data[contour_entity], colors="w")
        self.ax.clabel(temps_c, fmt="%i")

        # finalise the plot
        self._latlon_logp_setup()

Lastly the contour_entity is drawn on top of the plot, in white. Feel free to use any other colour. Of course if you don’t want a contour, you can delete this part of the code. The final part you don’t usually need to touch.


That’s about it. Feel free to download this template and play around with it however you like.

If you wish to include this into your WMS server

  1. Put the file into your mswms_settings.py directory, e.g. ~/mss

  2. Assuming you didn’t change the file or class name, append the following lines into your mswms_settings.py

from VS_template import VS_Template
register_vertical_layers = [] if not register_vertical_layers else register_vertical_layers
register_vertical_layers.append((VS_Template, [next(iter(data))]))