Example #1
0
def __get_class(parent_class, name=None):
    """
    Identify the subclass of parent_class to a given name, if specified.
    If the name is not specified, the routine picks a default.

    Note, only the first filename of the list is use here.

    :param parent_class: A base class
    :param name: name of the class to find
    :return: a subclass of the parent_class
    """
    from cis.plugin import find_plugin_classes
    from cis.exceptions import ClassNotFoundError
    import logging

    all_classes = find_plugin_classes(parent_class, 'cis.collocation.col_implementations')
    for cls in all_classes:

        if name is None:
            # Use default
            pass
        else:
            # Method specified directly
            if name == cls.__name__:
                logging.debug("Selected class " + cls.__name__)
                return cls

    raise ClassNotFoundError("Specified "+parent_class.__name__+" subclass cannot be found")
Example #2
0
def __get_class(parent_class, name=None):
    """
    Identify the subclass of parent_class to a given name, if specified.
    If the name is not specified, the routine picks a default.

    Note, only the first filename of the list is use here.

    :param parent_class: A base class
    :param name: name of the class to find
    :return: a subclass of the parent_class
    """
    from cis.plugin import find_plugin_classes
    from cis.exceptions import ClassNotFoundError
    import logging

    all_classes = find_plugin_classes(parent_class,
                                      'cis.collocation.col_implementations')
    for cls in all_classes:

        if name is None:
            # Use default
            pass
        else:
            # Method specified directly
            if name == cls.__name__:
                logging.debug("Selected class " + cls.__name__)
                return cls

    raise ClassNotFoundError("Specified " + parent_class.__name__ +
                             " subclass cannot be found")
Example #3
0
def __get_class(filename, product=None):
    """
    Identify the subclass of :class:`.AProduct` to a given product name if specified.
    If the product name is not specified, the routine uses the signature (regex)
    given by :meth:`get_file_signature` to infer the product class from the filename.

    Note, only the first filename of the list is use here.

    :param filename: A single filename
    :param product: name of the product
    :return: a subclass of :class:`.AProduct`
    """
    import re
    import os
    import cis.plugin as plugin
    from cis.exceptions import ClassNotFoundError

    # Ensure the filename doesn't include the path
    basename = os.path.basename(filename)

    product_classes = plugin.find_plugin_classes(AProduct,
                                                 'cis.data_io.products')
    product_classes = sorted(product_classes,
                             key=lambda cls: cls.priority,
                             reverse=True)

    for cls in product_classes:

        if product is None:
            # search for a pattern that matches file signature
            class_instance = cls()
            patterns = class_instance.get_file_signature()
            for pattern in patterns:
                # Match the pattern - re.I allows for case insensitive matches.
                # Appending '$' to the pattern ensures we match the whole string
                if re.match(pattern + '$', basename, re.I) is not None:
                    logging.debug("Found product class " + cls.__name__ +
                                  " matching regex pattern " + pattern)
                    errors = class_instance.get_file_type_error(filename)
                    if errors is None:
                        return cls
                    else:
                        logging.info(
                            "Product class {} is not right because {}".format(
                                cls.__name__, errors))
        else:
            # product specified directly
            if product == cls.__name__:
                logging.debug("Selected product class " + cls.__name__)
                return cls
    error_message = "Product cannot be found for given file.\nSupported products and signatures are:\n"
    for cls in product_classes:
        error_message += cls().__class__.__name__ + ": " + str(
            cls().get_file_signature()) + "\n"
    raise ClassNotFoundError(error_message)
Example #4
0
File: parse.py Project: cpaulik/cis
def check_aggregate_kernel(arg, parser):
    import cis.plugin as plugin
    from cis.collocation.col_framework import Kernel
    from cis.aggregation.aggregation_kernels import aggregation_kernels

    aggregation_classes = plugin.find_plugin_classes(Kernel, 'cis.collocation.col_implementations')
    aggregation_names = [cls().__class__.__name__ for cls in aggregation_classes]
    if arg in aggregation_kernels.keys() or arg in aggregation_names:
        return arg
    else:
        parser.error(arg + " is not a valid aggregation kernel. Please use one of " + str(aggregation_names))
Example #5
0
File: parse.py Project: cpaulik/cis
def check_product(product, parser):
    from cis.data_io.products.AProduct import AProduct
    import cis.plugin as plugin

    if product:
        product_classes = plugin.find_plugin_classes(AProduct, 'cis.data_io.products.products')
        product_names = [cls().__class__.__name__ for cls in product_classes]
        if product not in product_names:
            parser.error(product + " is not a valid product. Please use one of " + str(product_names))
    else:
        product = None
    return product
Example #6
0
def files_which_match_some_of_regexp_but_not_the_end_shouldnt_match():
    """
    Because we're using re.match the regular expression has to match the start - but not necassarily the end. This
     test ensures that we have taken care of that as often the extension is the most important part.
    """
    import cis.plugin as plugin
    from cis.data_io.products.AProduct import AProduct
    # We have to patch all of the plugin classes because get_file_type_error gets called and this file doesn't exist
    #  we are only testing the wildcard matching logic.
    product_classes = plugin.find_plugin_classes(AProduct,
                                                 'cis.data_io.products')
    for p in product_classes:
        p.get_file_type_error = lambda self, f: None
    _ = __get_class(example_caliop_l2_filename + ".ext")
Example #7
0
def __get_class(filename, product=None):
    """
    Identify the subclass of :class:`.AProduct` to a given product name if specified.
    If the product name is not specified, the routine uses the signature (regex)
    given by :meth:`get_file_signature` to infer the product class from the filename.

    Note, only the first filename of the list is use here.

    :param filename: A single filename
    :param product: name of the product
    :return: a subclass of :class:`.AProduct`
    """
    import re
    import os
    import cis.plugin as plugin
    from cis.exceptions import ClassNotFoundError

    # Ensure the filename doesn't include the path
    basename = os.path.basename(filename)

    product_classes = plugin.find_plugin_classes(AProduct, 'cis.data_io.products')
    product_classes = sorted(product_classes, key=lambda cls: cls.priority, reverse=True)

    for cls in product_classes:

        if product is None:
            # search for a pattern that matches file signature
            class_instance = cls()
            patterns = class_instance.get_file_signature()
            for pattern in patterns:
                # Match the pattern - re.I allows for case insensitive matches.
                # Appending '$' to the pattern ensures we match the whole string
                if re.match(pattern+'$', basename, re.I) is not None:
                    logging.debug("Found product class " + cls.__name__ + " matching regex pattern " + pattern)
                    errors = class_instance.get_file_type_error(filename)
                    if errors is None:
                        return cls
                    else:
                        logging.info("Product class {} is not right because {}".format(cls.__name__, errors))
        else:
            # product specified directly
            if product == cls.__name__:
                logging.debug("Selected product class " + cls.__name__)
                return cls
    error_message = "Product cannot be found for given file.\nSupported products and signatures are:\n"
    for cls in product_classes:
        error_message += cls().__class__.__name__ + ": " + str(cls().get_file_signature()) + "\n"
    raise ClassNotFoundError(error_message)
Example #8
0
File: parse.py Project: cpaulik/cis
def add_plot_parser_arguments(parser):
    from cis.data_io.products.AProduct import AProduct
    import cis.plugin as plugin

    product_classes = plugin.find_plugin_classes(AProduct, 'cis.data_io.products', verbose=False)

    parser.add_argument("datagroups", metavar="Input datagroups", nargs="+",
                        help="The datagroups to be plotted, in the format 'variable:filenames[:options]', where "
                             "options are entered in a comma separated list of the form \'keyword=value\'. Available "
                             "options are color, edgecolor, itemstylem, label and product. Colour is any valid html "
                             "colour and product is one of the options listed below. For example 'cis plot "
                             "var1:file:product=NetCDF_CF_Gridded,colour=red'. Products: " +
                             str([cls().__class__.__name__ for cls in product_classes]))
    parser.add_argument("-o", "--output", metavar="Output filename", nargs="?",
                        help="The filename of the output file for the plot image")
    parser.add_argument("--type", metavar="Chart type", nargs="?",
                        help="The chart type, one of: " + str(Plotter.plot_types.keys()))

    parser.add_argument("--xlabel", metavar="X axis label", nargs="?", help="The label for the x axis")
    parser.add_argument("--ylabel", metavar="Y axis label", nargs="?", help="The label for the y axis")
    parser.add_argument("--cbarlabel", metavar="Colour bar label", nargs="?", help="The label for the colour bar")

    parser.add_argument("--xtickangle", metavar="X tick angle", nargs="?",
                        help="The angle (in degrees) of the ticks on the x axis")
    parser.add_argument("--ytickangle", metavar="Y tick angle", nargs="?",
                        help="The angle (in degrees) of the ticks on the y axis")

    parser.add_argument("--title", metavar="Chart title", nargs="?", help="The title for the chart")
    parser.add_argument("--itemwidth", metavar="Item width", nargs="?",
                        help="The width of an item. Unit are points in the case of a line, and point^2 in the case of a"
                             " scatter point.")
    parser.add_argument("--fontsize", metavar="Font size", nargs="?", help="The size of the font in points")
    parser.add_argument("--cmap", metavar="Colour map", nargs="?", help="The colour map used, e.g. RdBu")
    parser.add_argument("--height", metavar="Plot height", nargs="?", help="The height of the plot in inches")
    parser.add_argument("--width", metavar="Plot width", nargs="?", help="The width of the plot in inches")

    parser.add_argument("--xmin", metavar="Minimum x", nargs="?", help="The minimum x value to plot")
    parser.add_argument("--xmax", metavar="Maximum x", nargs="?", help="The maximum x value to plot")
    parser.add_argument("--xstep", metavar="X step", nargs="?", help="The step of the x axis")

    parser.add_argument("--ymin", metavar="Minimum y", nargs="?", help="The minimum y value to plot")
    parser.add_argument("--ymax", metavar="Maximum y", nargs="?", help="The maximum y value to plot")
    parser.add_argument("--ystep", metavar="Y step", nargs="?", help="The step of the y axis")

    parser.add_argument("--vmin", metavar="Minimum value", nargs="?", help="The minimum value to plot")
    parser.add_argument("--vmax", metavar="Maximum value", nargs="?", help="The maximum value to plot")
    parser.add_argument("--vstep", metavar="X value", nargs="?", help="The step of the colour bar")

    parser.add_argument("--xbinwidth", metavar="Histogram x axis bin width", nargs="?",
                        help="The width of the bins on the x axis of a histogram")
    parser.add_argument("--ybinwidth", metavar="Histogram y axis bin width", nargs="?",
                        help="The width of the bins on the y axis of a histogram")

    parser.add_argument("--cbarorient", metavar="Colour bar orientation", default="vertical", nargs="?",
                        help="The orientation of the colour bar, either horizontal or vertical")
    parser.add_argument("--nocolourbar", metavar="Hides the colour bar", default="False", nargs="?",
                        help="Does not show the colour bar")

    parser.add_argument("--logx", metavar="Log (base 10) scale on X axis", default="False", nargs="?",
                        help="Uses a log scale (base 10) on the x axis")
    parser.add_argument("--logy", metavar="Log (base 10) scale on Y axis", default="False", nargs="?",
                        help="Uses a log scale (base 10) on the y axis")
    parser.add_argument("--logv", metavar="Log (base 10) scale for values", default="False", nargs="?",
                        help="Uses a log scale (base 10) on the colour bar")

    parser.add_argument("--grid", metavar="Show grid", default="False", nargs="?", help="Shows grid lines on the plot")
    parser.add_argument("--xaxis", metavar="Variable on x axis", nargs="?",
                        help="Name of variable to use on the x axis")
    parser.add_argument("--yaxis", metavar="Variable on y axis", nargs="?",
                        help="Name of variable to use on the y axis")

    parser.add_argument("--coastlinescolour", metavar="Coastlines Colour", nargs="?",
                        help="The colour of the coastlines on a map. Any valid html colour (e.g. red)")
    parser.add_argument("--nasabluemarble", metavar="NASA Blue Marble background", default=False, nargs="?",
                        help="Add the NASA 'Blue Marble' image as the background to a map, instead of coastlines")

    parser.add_argument("--plotwidth", metavar="Width of the plot in inches", default=8, nargs="?",
                        help="Set the width of the plot when outputting to file")
    parser.add_argument("--plotheight", metavar="Height of the plot in inches", default=6, nargs="?",
                        help="Set the height of the plot when outputting to file")
    parser.add_argument("--cbarscale", metavar="A scaling for the color bar", default=None, nargs="?",
                        help="Scale the color bar, use when color bar does not match plot size")
    return parser
Example #9
0
File: parse.py Project: cedadev/cis
def add_plot_parser_arguments(parser):
    from cis.data_io.products.AProduct import AProduct
    from cis.parse_datetime import parse_as_number_or_datetime_delta, parse_as_number_or_datetime
    import cis.plugin as plugin
    from matplotlib.colors import cnames

    product_classes = plugin.find_plugin_classes(AProduct, 'cis.data_io.products', verbose=False)

    parser.add_argument("datagroups", metavar="Input datagroups", nargs="+",
                        help="The datagroups to be plotted, in the format 'variable:filenames[:options]', where "
                             "options are entered in a comma separated list of the form \'keyword=value\'. Available "
                             "options are color, edgecolor, itemstylem, label and product. Colour is any valid html "
                             "colour and product is one of the options listed below. For example 'cis plot "
                             "var1:file:product=NetCDF_CF_Gridded,colour=red'. Products: " +
                             str([cls().__class__.__name__ for cls in product_classes]))
    parser.add_argument("-o", "--output", metavar="Output filename", nargs="?", default=None,
                        help="The filename of the output file for the plot image")
    parser.add_argument("--type", metavar="Chart type", nargs="?",
                        help="The chart type, one of: " + str(plot_types.keys()),
                        choices=plot_types.keys())

    parser.add_argument("--xlabel", metavar="X axis label", nargs="?", help="The label for the x axis")
    parser.add_argument("--ylabel", metavar="Y axis label", nargs="?", help="The label for the y axis")
    parser.add_argument("--cbarlabel", metavar="Colour bar label", nargs="?", help="The label for the colour bar")

    parser.add_argument("--title", metavar="Chart title", nargs="?", help="The title for the chart")
    parser.add_argument("--fontsize", metavar="Font size", nargs="?", help="The size of the font in points", type=float)
    parser.add_argument("--cmap", metavar="Colour map", nargs="?", help="The colour map used, e.g. RdBu")
    parser.add_argument("--height", metavar="Plot height", nargs="?", help="The height of the plot in inches",
                        type=float)
    parser.add_argument("--width", metavar="Plot width", nargs="?", help="The width of the plot in inches", type=float)

    parser.add_argument("--xmin", metavar="Minimum x", nargs="?", help="The minimum x value to plot",
                        type=parse_as_number_or_datetime)
    parser.add_argument("--xmax", metavar="Maximum x", nargs="?", help="The maximum x value to plot",
                        type=parse_as_number_or_datetime)
    parser.add_argument("--xstep", metavar="X step", nargs="?", help="The step of the x axis",
                        type=parse_as_number_or_datetime_delta)

    parser.add_argument("--ymin", metavar="Minimum y", nargs="?", help="The minimum y value to plot",
                        type=parse_as_number_or_datetime)
    parser.add_argument("--ymax", metavar="Maximum y", nargs="?", help="The maximum y value to plot",
                        type=parse_as_number_or_datetime)
    parser.add_argument("--ystep", metavar="Y step", nargs="?", help="The step of the y axis",
                        type=parse_as_number_or_datetime_delta)

    parser.add_argument("--vmin", metavar="Minimum value", nargs="?", help="The minimum value to plot",
                        type=parse_as_number_or_datetime)
    parser.add_argument("--vmax", metavar="Maximum value", nargs="?", help="The maximum value to plot",
                        type=parse_as_number_or_datetime)
    parser.add_argument("--vstep", metavar="X value", nargs="?", help="The step of the colour bar",
                        type=parse_as_number_or_datetime_delta)

    parser.add_argument("--xbins", metavar="Number of histogram x axis bins", nargs="?",
                        help="The number of bins on the x axis of a histogram", type=int)
    parser.add_argument("--ybins", metavar="Number of histogram x axis bins", nargs="?",
                        help="The number of bins on the y axis of a histogram", type=int)

    parser.add_argument("--cbarorient", metavar="Colour bar orientation", nargs="?",
                        help="The orientation of the colour bar, either horizontal or vertical",
                        choices=['vertical', 'horizontal'])
    parser.add_argument("--nocolourbar", dest='colourbar',
                        help="Does not show the colour bar", action='store_false')

    parser.add_argument("--logx",
                        help="Uses a log scale (base 10) on the x axis", action='store_true')
    parser.add_argument("--logy",
                        help="Uses a log scale (base 10) on the y axis", action='store_true')
    parser.add_argument("--logv",
                        help="Uses a log scale (base 10) on the colour bar", action='store_true')

    parser.add_argument("--grid", help="Shows grid lines on the plot",
                        action='store_true')

    parser.add_argument("--xaxis", metavar="Variable on x axis", nargs="?",
                        help="Name of variable to use on the x axis")
    parser.add_argument("--yaxis", metavar="Variable on y axis", nargs="?",
                        help="Name of variable to use on the y axis")

    parser.add_argument("--coastlinescolour", metavar="Coastlines Colour", nargs="?",
                        help="The colour of the coastlines on a map. Any valid html colour (e.g. red)",
                        choices=(list(cnames.keys()) + ['grey']))
    parser.add_argument("--nasabluemarble",
                        help="Add the NASA 'Blue Marble' image as the background to a map, instead of coastlines",
                        action='store_true')

    parser.add_argument("--cbarscale", metavar="A scaling for the color bar", nargs="?",
                        help="Scale the color bar, use when color bar does not match plot size", type=float)

    parser.add_argument("--projection", choices=projections.keys())

    # Taylor diagram specific options
    parser.add_argument('--solid', action='store_true', help='Use solid markers')
    parser.add_argument('--extend', type=float, help='Extend plot for negative correlation')
    parser.add_argument('--fold', action='store_true', help='Fold plot for negative correlation or large variance')
    parser.add_argument('--gammamax', type=float, help='Fix maximum extent of radial axis')
    parser.add_argument('--stdbiasmax', type=float, help='Fix maximum standardised bias')
    parser.add_argument('--bias', metavar='METHOD', choices=['color', 'colour', 'size', 'flag'],
                        help='Indicate bias using the specified method (colo[u]r, size, flag)')

    return parser