[2411] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | # this must come first |
---|
| 5 | from __future__ import print_function, unicode_literals, division |
---|
| 6 | |
---|
| 7 | # standard library imports |
---|
| 8 | from argparse import ArgumentParser |
---|
| 9 | import os |
---|
| 10 | import os.path |
---|
| 11 | import numpy as np |
---|
[2425] | 12 | # import matplotlib.pyplot as plt |
---|
| 13 | # from matplotlib.backends.backend_pdf import PdfPages |
---|
[2411] | 14 | |
---|
[2413] | 15 | # Application library imports |
---|
[2460] | 16 | from libconso import * |
---|
[2411] | 17 | |
---|
| 18 | |
---|
| 19 | ######################################## |
---|
| 20 | class DirVolume(object): |
---|
| 21 | #--------------------------------------- |
---|
| 22 | def __init__(self, date, login, dirname, size): |
---|
| 23 | self.date = date |
---|
| 24 | self.login = login |
---|
| 25 | self.dirname = dirname |
---|
| 26 | self.dirsize = size |
---|
| 27 | |
---|
| 28 | #--------------------------------------- |
---|
| 29 | def __repr__(self): |
---|
| 30 | return "{}={}".format(self.dirname, self.dirsize) |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | ######################################## |
---|
| 34 | class StoreDict(dict): |
---|
| 35 | #--------------------------------------- |
---|
| 36 | def __init__(self): |
---|
| 37 | self = {} |
---|
| 38 | |
---|
| 39 | #--------------------------------------- |
---|
| 40 | def fill_data(self, filein): |
---|
[2427] | 41 | """ |
---|
| 42 | """ |
---|
| 43 | try: |
---|
| 44 | data = np.genfromtxt( |
---|
| 45 | filein, |
---|
| 46 | skip_header=1, |
---|
[2462] | 47 | converters={ |
---|
| 48 | 0: string_to_date, |
---|
| 49 | 1: str, |
---|
| 50 | 2: string_to_size_unit, |
---|
| 51 | 3: str, |
---|
| 52 | }, |
---|
[2427] | 53 | missing_values="nan", |
---|
| 54 | ) |
---|
| 55 | except: |
---|
[2437] | 56 | print("Empty file {}, nothing to plot".format(filein)) |
---|
[2427] | 57 | exit(1) |
---|
[2411] | 58 | |
---|
| 59 | for date, login, dirsize, dirname in data: |
---|
| 60 | self.add_item(date, login, dirsize, dirname) |
---|
| 61 | |
---|
| 62 | #--------------------------------------- |
---|
| 63 | def add_item(self, date, login, dirsize, dirname): |
---|
| 64 | """ |
---|
| 65 | """ |
---|
| 66 | if login not in self: |
---|
| 67 | self[login] = Login(date, login) |
---|
| 68 | self[login].add_directory(date, login, dirsize, dirname) |
---|
| 69 | |
---|
| 70 | #--------------------------------------- |
---|
| 71 | def get_items(self): |
---|
| 72 | """ |
---|
| 73 | """ |
---|
| 74 | items = (subitem for item in self.itervalues() |
---|
| 75 | for subitem in item.listdir) |
---|
| 76 | items = sorted(items, key=lambda item: item.login) |
---|
| 77 | |
---|
| 78 | return items |
---|
| 79 | |
---|
| 80 | #--------------------------------------- |
---|
| 81 | def get_items_by_name(self, pattern): |
---|
| 82 | """ |
---|
| 83 | """ |
---|
| 84 | items = (subitem for item in self.itervalues() |
---|
| 85 | for subitem in item.listdir |
---|
| 86 | if pattern in subitem.dirname) |
---|
| 87 | items = sorted(items, key=lambda item: item.login) |
---|
| 88 | |
---|
| 89 | return items |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | ######################################## |
---|
| 93 | class Login(object): |
---|
| 94 | #--------------------------------------- |
---|
| 95 | def __init__(self, date, login): |
---|
| 96 | self.date = date |
---|
| 97 | self.login = login |
---|
| 98 | self.total = SizeUnit(0., "K") |
---|
| 99 | self.listdir = [] |
---|
| 100 | |
---|
| 101 | #--------------------------------------- |
---|
| 102 | def __repr__(self): |
---|
| 103 | return "{}/{:%F}: {}".format(self.login, self.date, self.listdir) |
---|
| 104 | |
---|
| 105 | #--------------------------------------- |
---|
| 106 | def add_to_total(self, dirsize): |
---|
| 107 | """ |
---|
| 108 | """ |
---|
| 109 | somme = self.total.convert_size("K").size + \ |
---|
| 110 | dirsize.convert_size("K").size |
---|
| 111 | self.total = SizeUnit(somme, "K") |
---|
| 112 | |
---|
| 113 | #--------------------------------------- |
---|
| 114 | def add_directory(self, date, login, dirsize, dirname): |
---|
| 115 | """ |
---|
| 116 | """ |
---|
| 117 | self.listdir.append(DirVolume(date, login, dirname, dirsize)) |
---|
| 118 | self.add_to_total(dirsize) |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | ######################################## |
---|
| 122 | def plot_init(): |
---|
| 123 | paper_size = np.array([29.7, 21.0]) |
---|
| 124 | fig, ax = plt.subplots(figsize=(paper_size/2.54)) |
---|
| 125 | |
---|
| 126 | return fig, ax |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | ######################################## |
---|
| 130 | def plot_data(ax, coords, ylabels, values): |
---|
| 131 | """ |
---|
| 132 | """ |
---|
| 133 | ax.barh(coords, values, align="center", color="linen", |
---|
| 134 | linewidth=0.2, label="volume sur STORE ($To$)") |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | ######################################## |
---|
| 138 | def plot_config(ax, coords, ylabels, dirnames, title, tot_volume): |
---|
| 139 | """ |
---|
| 140 | """ |
---|
| 141 | # ... Config axes ... |
---|
| 142 | # ------------------- |
---|
| 143 | # 1) Range |
---|
| 144 | ymin, ymax = coords[0]-1, coords[-1]+1 |
---|
| 145 | ax.set_ylim(ymin, ymax) |
---|
| 146 | |
---|
| 147 | # 2) Ticks labels |
---|
| 148 | ax.ticklabel_format(axis="x", style="sci", scilimits=(0, 0)) |
---|
| 149 | ax.set_yticks(coords, minor=False) |
---|
| 150 | ax.set_yticklabels(ylabels, size="x-small", fontweight="bold") |
---|
| 151 | ax.invert_yaxis() |
---|
| 152 | |
---|
| 153 | xmin, xmax = ax.get_xlim() |
---|
| 154 | xpos = xmin + (xmax-xmin)/50. |
---|
| 155 | for (ypos, text) in zip(coords, dirnames): |
---|
| 156 | ax.text(s=text, x=xpos, y=ypos, va="center", ha="left", |
---|
| 157 | size="xx-small", color="gray", style="italic") |
---|
| 158 | |
---|
| 159 | # 3) Define axes title |
---|
| 160 | ax.set_xlabel("$To$", fontweight="bold") |
---|
| 161 | |
---|
| 162 | # ... Main title and legend ... |
---|
| 163 | # ----------------------------- |
---|
| 164 | ax.set_title(title, fontweight="bold", size="large") |
---|
| 165 | ax.legend(loc="best", fontsize="x-small", frameon=False) |
---|
| 166 | |
---|
| 167 | tot_label = "volume total = {}".format(tot_volume) |
---|
| 168 | plt.figtext(x=0.95, y=0.93, s=tot_label, backgroundcolor="linen", |
---|
| 169 | ha="right", va="bottom", fontsize="small") |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | ######################################## |
---|
| 173 | def get_arguments(): |
---|
| 174 | parser = ArgumentParser() |
---|
| 175 | parser.add_argument("-v", "--verbose", action="store_true", |
---|
| 176 | help="Verbose mode") |
---|
| 177 | parser.add_argument("-f", "--full", action="store_true", |
---|
| 178 | help="plot all the directories in IGCM_OUT" + |
---|
| 179 | "(default: plot IPSLCM6 directories)") |
---|
| 180 | parser.add_argument("-p", "--pattern", action="store", |
---|
| 181 | default="IPSLCM6", |
---|
| 182 | help="plot the whole period") |
---|
[2425] | 183 | parser.add_argument("-s", "--show", action="store_true", |
---|
| 184 | help="interactive mode") |
---|
| 185 | parser.add_argument("-d", "--dods", action="store_true", |
---|
| 186 | help="copy output on dods") |
---|
[2411] | 187 | |
---|
| 188 | return parser.parse_args() |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | ######################################## |
---|
| 192 | if __name__ == '__main__': |
---|
| 193 | |
---|
| 194 | # .. Initialization .. |
---|
| 195 | # ==================== |
---|
| 196 | # ... Command line arguments ... |
---|
| 197 | # ------------------------------ |
---|
| 198 | args = get_arguments() |
---|
| 199 | if args.verbose: |
---|
| 200 | print(args) |
---|
| 201 | |
---|
[2425] | 202 | # ... Turn interactive mode off ... |
---|
| 203 | # --------------------------------- |
---|
| 204 | if not args.show: |
---|
| 205 | import matplotlib |
---|
| 206 | matplotlib.use('Agg') |
---|
| 207 | |
---|
| 208 | import matplotlib.pyplot as plt |
---|
[2427] | 209 | # from matplotlib.backends.backend_pdf import PdfPages |
---|
[2425] | 210 | |
---|
| 211 | if not args.show: |
---|
| 212 | plt.ioff() |
---|
| 213 | |
---|
[2411] | 214 | # ... Files and directories ... |
---|
| 215 | # ----------------------------- |
---|
[2460] | 216 | project_name, DIR, OUT = parse_config("bin/config.ini") |
---|
| 217 | |
---|
[2427] | 218 | (file_param, file_utheo, file_data) = \ |
---|
| 219 | get_input_files(DIR["SAVEDATA"], |
---|
| 220 | [OUT["PARAM"], OUT["UTHEO"], OUT["STORE"]]) |
---|
[2411] | 221 | |
---|
[2427] | 222 | img_name = "store" |
---|
| 223 | today = os.path.basename(file_param).strip(OUT["PARAM"]) |
---|
| 224 | |
---|
[2417] | 225 | if args.verbose: |
---|
| 226 | print(file_param) |
---|
| 227 | print(file_utheo) |
---|
[2427] | 228 | print(file_data) |
---|
[2417] | 229 | print(img_name) |
---|
[2427] | 230 | print(today) |
---|
[2417] | 231 | |
---|
[2411] | 232 | # .. Get project info .. |
---|
| 233 | # ====================== |
---|
[2460] | 234 | projet = Project() |
---|
| 235 | projet.fill_data(file_param) |
---|
| 236 | projet.get_date_init(file_utheo) |
---|
[2411] | 237 | |
---|
| 238 | # .. Fill in data dict .. |
---|
| 239 | # ======================= |
---|
| 240 | stores = StoreDict() |
---|
[2427] | 241 | stores.fill_data(file_data) |
---|
[2411] | 242 | |
---|
| 243 | # .. Extract data depending on C.L. arguments .. |
---|
| 244 | # ============================================== |
---|
| 245 | if args.full: |
---|
| 246 | selected_items = stores.get_items() |
---|
| 247 | else: |
---|
| 248 | selected_items = stores.get_items_by_name(args.pattern) |
---|
| 249 | |
---|
| 250 | if args.verbose: |
---|
| 251 | for item in selected_items: |
---|
| 252 | print( |
---|
| 253 | "{:8s} {:%F} {} {:>18s} {} ".format( |
---|
| 254 | item.login, |
---|
| 255 | item.date, |
---|
| 256 | item.dirsize, |
---|
| 257 | item.dirsize.convert_size("K"), |
---|
| 258 | item.dirname, |
---|
| 259 | ) |
---|
| 260 | ) |
---|
| 261 | |
---|
| 262 | # .. Compute data to be plotted .. |
---|
| 263 | # ================================ |
---|
| 264 | ylabels = [item.login for item in selected_items] |
---|
| 265 | values = np.array([item.dirsize.convert_size("T").size |
---|
| 266 | for item in selected_items], |
---|
| 267 | dtype=float) |
---|
| 268 | dirnames = [item.dirname for item in selected_items] |
---|
| 269 | date = selected_items[0].date |
---|
| 270 | |
---|
| 271 | nb_items = len(ylabels) |
---|
| 272 | coords = np.linspace(1, nb_items, num=nb_items) |
---|
| 273 | |
---|
| 274 | # .. Plot stuff .. |
---|
| 275 | # ================ |
---|
| 276 | # ... Initialize figure ... |
---|
| 277 | # ------------------------- |
---|
| 278 | (fig, ax) = plot_init() |
---|
| 279 | |
---|
| 280 | # ... Plot data ... |
---|
| 281 | # ----------------- |
---|
| 282 | plot_data(ax, coords, ylabels, values) |
---|
| 283 | |
---|
| 284 | # ... Tweak figure ... |
---|
| 285 | # -------------------- |
---|
| 286 | title = "Occupation {} de STORE par login\n{:%d/%m/%Y}".format( |
---|
[2460] | 287 | projet.project.upper(), |
---|
[2411] | 288 | date |
---|
| 289 | ) |
---|
| 290 | plot_config(ax, coords, ylabels, dirnames, title, |
---|
| 291 | SizeUnit(np.sum(values), "T")) |
---|
| 292 | |
---|
| 293 | # ... Save figure ... |
---|
| 294 | # ------------------- |
---|
[2428] | 295 | img_in = os.path.join(DIR["PLOT"], "{}.pdf".format(img_name)) |
---|
| 296 | img_out = os.path.join(DIR["SAVEPLOT"], |
---|
| 297 | "{}_{}.pdf".format(img_name, today)) |
---|
[2411] | 298 | |
---|
[2460] | 299 | plot_save(img_in, img_out, title, DIR) |
---|
[2428] | 300 | |
---|
[2425] | 301 | # ... Publish figure on dods ... |
---|
| 302 | # ------------------------------ |
---|
| 303 | if args.dods: |
---|
[2460] | 304 | dods_cp(img_in, DIR) |
---|
[2425] | 305 | |
---|
| 306 | if args.show: |
---|
| 307 | plt.show() |
---|
| 308 | |
---|
[2427] | 309 | exit(0) |
---|
[2433] | 310 | |
---|