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