#!/usr/bin/env python # -*- coding: utf-8 -*- # this must come first from __future__ import print_function, unicode_literals, division # standard library imports from argparse import ArgumentParser import os import os.path import numpy as np # Application library imports from gencmip6 import * from gencmip6_path import * ######################################## class LoginDict(dict): #--------------------------------------- def __init__(self): self = {} #--------------------------------------- def fill_data(self, filein): """ """ try: data = np.genfromtxt( filein, skip_header=1, converters={0: string_to_date, 1: str}, missing_values="nan", ) except: print("Empty file {}".format(filein)) exit(1) for date, login, conso in data: self.add_item(date, login, conso) #--------------------------------------- def add_item(self, date, login, conso): """ """ self[login] = Login(date, login, conso) #--------------------------------------- def get_items(self): """ """ items = (item for item in self.itervalues()) items = sorted(items, key=lambda item: item.login) return items #--------------------------------------- def get_items_not_null(self): """ """ items = (item for item in self.itervalues() if item.conso > 0.) items = sorted(items, key=lambda item: item.login) return items class Login(object): #--------------------------------------- def __init__(self, date, login, conso): self.date = date self.login = login self.conso = conso #--------------------------------------- def __repr__(self): return "{} ({:.2}h)".format(self.login, self.conso) ######################################## def plot_init(): paper_size = np.array([29.7, 21.0]) fig, ax = plt.subplots(figsize=(paper_size/2.54)) return fig, ax ######################################## def plot_data(ax, ycoord, ylabels, consos): """ """ ax.barh(ycoord, consos, align="center", color="linen", linewidth=0.2, label="conso (heures)") ######################################## def plot_config(ax, ycoord, ylabels, title): """ """ # ... Config axes ... # ------------------- # 1) Range ymin, ymax = ycoord[0]-1, ycoord[-1]+1 ax.set_ylim(ymin, ymax) # 2) Ticks labels ax.ticklabel_format(axis="x", style="sci", scilimits=(0, 0)) ax.set_yticks(ycoord, minor=False) ax.set_yticklabels(ylabels, size="x-small", fontweight="bold") ax.invert_yaxis() # 3) Define axes title ax.set_xlabel("heures", fontweight="bold") # ... Main title and legend ... # ----------------------------- ax.set_title(title, fontweight="bold", size="large") ax.legend(loc="best", fontsize="x-small", frameon=False) # ######################################## # def plot_save(img_name): # """ # """ # dpi = 200. # img_in = os.path.join(DIR["PLOT"], "{}.pdf".format(img_name)) # with PdfPages(img_in) as pdf: # pdf.savefig(dpi=dpi) # # pdf file's metadata # d = pdf.infodict() # d["Title"] = "Conso GENCMIP6 par login" # d["Author"] = "plot_bilan.py" # # d["Subject"] = "Time spent over specific commands during create_ts \ # # jobs at IDRIS and four configurations at TGCC" # # d["Keywords"] = "bench create_ts TGCC IDRIS ncrcat" # # d["CreationDate"] = dt.datetime(2009, 11, 13) # # d["ModDate"] = dt.datetime.today() # if os.path.isdir(DIR["SAVEPLOT"]): # img_out = os.path.join(DIR["SAVEPLOT"], # "{}_{}.pdf".format(img_name, today)) # shutil.copy(img_in, img_out) ######################################## def get_arguments(): parser = ArgumentParser() parser.add_argument("-v", "--verbose", action="store_true", help="Verbose mode") parser.add_argument("-f", "--full", action="store_true", help="plot all the logins" + " (default: plot only non-zero)") parser.add_argument("-s", "--show", action="store_true", help="interactive mode") parser.add_argument("-d", "--dods", action="store_true", help="copy output on dods") return parser.parse_args() ######################################## if __name__ == '__main__': # .. Initialization .. # ==================== # ... Command line arguments ... # ------------------------------ args = get_arguments() if args.verbose: print(args) # ... Turn interactive mode off ... # --------------------------------- if not args.show: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt # from matplotlib.backends.backend_pdf import PdfPages if not args.show: plt.ioff() # ... Files and directories ... # ----------------------------- (file_param, file_utheo, file_data) = \ get_input_files(DIR["SAVEDATA"], [OUT["PARAM"], OUT["UTHEO"], OUT["LOGIN"]]) img_name = "login" today = os.path.basename(file_param).strip(OUT["PARAM"]) if args.verbose: print(file_param) print(file_utheo) print(file_data) print(img_name) print(today) # .. Get project info .. # ====================== gencmip6 = Project() gencmip6.fill_data(file_param) gencmip6.get_date_init(file_utheo) # .. Fill in data dict .. # ======================= # ... Initialization ... # ---------------------- logins = LoginDict() logins.fill_data(file_data) # .. Extract data depending on C.L. arguments .. # ============================================== if args.full: selected_items = logins.get_items() else: selected_items = logins.get_items_not_null() if args.verbose: for login in selected_items: print(login) # .. Compute data to be plotted .. # ================================ nb_items = len(selected_items) ycoord = np.linspace(1, nb_items, num=nb_items) ylabels = [item.login for item in selected_items] consos = np.array([item.conso for item in selected_items], dtype=float) date = selected_items[0].date # .. Plot stuff .. # ================ # ... Initialize figure ... # ------------------------- (fig, ax) = plot_init() # ... Plot data ... # ----------------- plot_data(ax, ycoord, ylabels, consos) # ... Tweak figure ... # -------------------- title = "Consommation {} par login\n{:%d/%m/%Y}".format( gencmip6.project.upper(), date ) plot_config(ax, ycoord, ylabels, title) # ... Save figure ... # ------------------- plot_save(img_name, today, "Conso GENCMIP6 par login") # ... Publish figure on dods ... # ------------------------------ if args.dods: dods_cp(os.path.join(DIR["PLOT"], img_name)) if args.show: plt.show() exit(0)