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