view climate_stripes.py @ 1:c6f2435d680b draft

"planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/climate-stripes commit 78edbd356193b8a8701ff7a28b7979c7047c0c5b"
author climate
date Tue, 14 Apr 2020 15:08:10 -0400
parents fec3147b0206
children abdc27e01dca
line wrap: on
line source

#!/usr/bin/env python3
#
#
# usage: climate_stripes.py [-h] [--cmap CMAP]
#                           [--output OUTPUT]
#                           [--xname XNAME]
#                           [--format_plot FORMAT_PLOT]
#                           [--format_date FORMAT_DATE]
#                           [--title TITLE]
#                           [--nxsplit NXSPLIT]
#                          input varname
#
# positional arguments:
#   input                 input filename with timeseries (tabular format)
#   varname               column name to use for plotting (case sensitive)
#
# optional arguments:
#   -h, --help            show this help message and exit
#   --cmap CMAP           Specify which colormap to use for plotting
#   --output OUTPUT       output filename to store resulting image (png format)
#   --xname XNAME         column name to use for x-axis
#   --format_plot FORMAT_PLOT
#                         format for plotting dates on the x-axis
#   --format_date FORMAT_DATE
#                         format for input date/time column
#   --title TITLE         plot title
#   --nxsplit NXSPLIT     number of ticks on the x-axis
#

import argparse
import warnings

import matplotlib as mpl
mpl.use('Agg')

import matplotlib.pyplot as plt   # noqa: I202,E402

import numpy as np  # noqa: I202,E402

import pandas as pd  # noqa: I202,E402


class Stripes ():
    def __init__(self, input, valname, cmap, output, xname="",
                 date_format='%Y%m',
                 plot_format='%Y',
                 nxsplit=10,
                 title=""):
        self.input = input
        self.valname = valname
        self.xname = xname
        if not nxsplit:
            self.nxsplit = 10
        else:
            self.nxsplit = nxsplit
        if not cmap:
            self.cmap = 'RdBu_r'
        else:
            self.cmap = cmap
        if not output:
            self.output = "stripes.png"
        else:
            self.output = output
        self.title = title
        if not date_format:
            self.format = '%Y%m'
        else:
            self.format = date_format.replace('X', '%')
        if not plot_format:
            self.plot_format = self.format
        else:
            self.plot_format = plot_format.replace('X', '%')

    def read_data(self):
        self.data = pd.read_csv(self.input, sep='\t')

    def create_stripes(self):
        data = np.zeros((2, self.data[self.valname].shape[0]), dtype='float')
        data[:] = np.NaN
        data[0, :] = self.data[self.valname]
        data[1, :] = self.data[self.valname]
        fig = plt.figure(figsize=(10, 2))
        ax = plt.subplot(111)
        plt.pcolor(data, cmap=self.cmap,
                   vmin=self.data[self.valname].quantile(q=0.01),
                   vmax=self.data[self.valname].quantile(q=0.99))
        if self.title:
            plt.title(self.title)
        if self.xname:
            nrange = self.data.index.values
            n = int(np.floor((nrange.max() - nrange.min())/int(self.nxsplit)))
            date_list = self.data[self.xname].loc[::n].apply(
                             lambda x: pd.to_datetime(str(x),
                                                      format=self.format))
            date_list = [i.strftime(self.plot_format) for i in date_list]
            nval = int(self.data[self.xname].loc[::n].shape[0])
            ax.xaxis.set_major_locator(plt.MaxNLocator(nval))
            ax.xaxis.set_ticklabels(date_list)
            ax.xaxis.set_tick_params(rotation=45)
        else:
            ax.set_xticks([])
        ax.set_yticks([])
        fig.tight_layout()
        fig.savefig(self.output)


if __name__ == '__main__':
    warnings.filterwarnings("ignore")
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'input',
        help='input filename with geographical coordinates (netCDF format)'
    )
    parser.add_argument(
        'varname',
        help='column name to use for plotting (case sensitive)'
    )
    parser.add_argument(
        '--cmap',
        help='Specify which colormap to use for plotting'
    )
    parser.add_argument(
        '--output',
        help='output filename to store resulting image (png format)'
    )
    parser.add_argument(
        '--xname',
        help='column name to use for x-axis'
    )
    parser.add_argument(
        '--format_plot',
        help='format for plotting dates on the x-axis'
    )
    parser.add_argument(
        '--format_date',
        help='format for input date/time column (default is Month d, yyyy)'
    )
    parser.add_argument(
        '--title',
        help='plot title'
    )
    parser.add_argument(
        '--nxsplit',
        help='number of ticks on the x-axis'
    )
    args = parser.parse_args()
    stripes = Stripes(args.input, args.varname, args.cmap, args.output,
                      xname=args.xname, date_format=args.format_date,
                      plot_format=args.format_plot, title=args.title,
                      nxsplit=args.nxsplit)
    stripes.read_data()
    stripes.create_stripes()