[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 |
---|
[2413] | 8 | import socket |
---|
[2411] | 9 | import os |
---|
| 10 | import os.path |
---|
| 11 | import glob |
---|
[2421] | 12 | import shutil |
---|
[2437] | 13 | import subprocess |
---|
[2411] | 14 | import datetime as dt |
---|
| 15 | import numpy as np |
---|
| 16 | |
---|
[2421] | 17 | # Application library imports |
---|
| 18 | from gencmip6_path import * |
---|
[2411] | 19 | |
---|
[2421] | 20 | |
---|
[2411] | 21 | ######################################## |
---|
[2421] | 22 | def dods_cp(filein): |
---|
| 23 | """ |
---|
| 24 | """ |
---|
| 25 | if not DIR["DODS"]: |
---|
| 26 | print("DODS directory not defined") |
---|
| 27 | return |
---|
| 28 | |
---|
[2437] | 29 | basefile = os.path.basename(filein) |
---|
| 30 | |
---|
| 31 | fileout = os.path.join(DIR["DODS"], basefile) |
---|
| 32 | filepng = os.path.join(DIR["DODS"], "img", basefile.split(".")[0] + ".png") |
---|
| 33 | |
---|
| 34 | #Â Copy file |
---|
[2421] | 35 | shutil.copy(filein, fileout) |
---|
| 36 | |
---|
[2437] | 37 | # Convert it to png for web page |
---|
| 38 | command = ["convert", "-density", "200", fileout, filepng] |
---|
| 39 | |
---|
| 40 | try : |
---|
| 41 | subprocess.call(command) |
---|
| 42 | except Exception as rc : |
---|
| 43 | print("Error in convert for {}:\n{}".format(fileout, rc)) |
---|
| 44 | |
---|
[2421] | 45 | return |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | ######################################## |
---|
[2411] | 49 | def string_to_percent(x): |
---|
| 50 | """ |
---|
| 51 | """ |
---|
| 52 | return float(x.strip("%"))/100. |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | ######################################## |
---|
| 56 | def string_to_size_unit(x): |
---|
| 57 | """ |
---|
| 58 | """ |
---|
[2428] | 59 | if unicode(x).isdecimal(): |
---|
| 60 | x = x + "o" |
---|
| 61 | |
---|
[2411] | 62 | (size, unit) = (float(x[:-1]), x[-1]) |
---|
[2428] | 63 | |
---|
[2411] | 64 | return SizeUnit(size, unit) |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | ######################################## |
---|
| 68 | def string_to_float(x): |
---|
| 69 | """ |
---|
| 70 | """ |
---|
| 71 | return float(x.strip("h")) |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | ######################################## |
---|
| 75 | def string_to_date(ssaammjj, fmt="%Y-%m-%d"): |
---|
| 76 | """ |
---|
| 77 | """ |
---|
| 78 | return dt.datetime.strptime(ssaammjj, fmt) |
---|
| 79 | |
---|
| 80 | |
---|
[2431] | 81 | ######################################## |
---|
| 82 | def string_to_datetime(string, fmt="%Y-%m-%d-%H:%M"): |
---|
| 83 | """ |
---|
| 84 | """ |
---|
| 85 | return dt.datetime.strptime(string, fmt) |
---|
| 86 | |
---|
| 87 | |
---|
[2411] | 88 | # ######################################## |
---|
| 89 | # def date_to_string(dtdate, fmt="%Y-%m-%d"): |
---|
| 90 | # """ |
---|
| 91 | # """ |
---|
| 92 | # return dt.datetime.strftime(dtdate, fmt) |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | ######################################## |
---|
[2413] | 96 | def where_we_run(): |
---|
| 97 | |
---|
| 98 | res = "" |
---|
| 99 | if "curie" in socket.getfqdn(): |
---|
| 100 | res = "curie" |
---|
| 101 | elif "ipsl" in socket.getfqdn(): |
---|
| 102 | res = "ipsl" |
---|
| 103 | else: |
---|
| 104 | res = "default" |
---|
| 105 | |
---|
| 106 | return res |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | ######################################## |
---|
[2411] | 110 | def get_last_file(dir_data, pattern): |
---|
| 111 | """ |
---|
| 112 | """ |
---|
| 113 | current_dir = os.getcwd() |
---|
| 114 | os.chdir(dir_data) |
---|
| 115 | filename = pattern + "*" |
---|
[2413] | 116 | file_list = glob.glob(os.path.join(dir_data, filename)) |
---|
| 117 | if file_list: |
---|
| 118 | res = sorted(file_list)[-1] |
---|
| 119 | else: |
---|
| 120 | res = None |
---|
[2411] | 121 | os.chdir(current_dir) |
---|
[2413] | 122 | return res |
---|
[2411] | 123 | |
---|
| 124 | |
---|
| 125 | ######################################## |
---|
[2427] | 126 | def get_input_files(dir_data, file_list): |
---|
| 127 | """ |
---|
| 128 | """ |
---|
| 129 | res = [] |
---|
| 130 | |
---|
| 131 | for filename in file_list: |
---|
| 132 | res.append(get_last_file(dir_data, filename)) |
---|
| 133 | |
---|
| 134 | if None in res: |
---|
| 135 | print("\nMissing one or more input files, we stop.") |
---|
| 136 | for f_in, f_out in zip(file_list, res): |
---|
| 137 | print("=> {}: {}".format(f_in, f_out)) |
---|
| 138 | exit(1) |
---|
| 139 | |
---|
| 140 | return res |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | ######################################## |
---|
| 144 | def plot_save(img_in, img_out, title): |
---|
| 145 | """ |
---|
| 146 | """ |
---|
| 147 | from matplotlib.backends.backend_pdf import PdfPages |
---|
| 148 | |
---|
| 149 | dpi = 200. |
---|
| 150 | |
---|
| 151 | # img_in = os.path.join(DIR["PLOT"], "{}.pdf".format(img_name)) |
---|
| 152 | |
---|
| 153 | with PdfPages(img_in) as pdf: |
---|
| 154 | pdf.savefig(dpi=dpi) |
---|
| 155 | |
---|
| 156 | # pdf file's metadata |
---|
| 157 | d = pdf.infodict() |
---|
| 158 | d["Title"] = title |
---|
| 159 | d["Author"] = os.path.basename(__file__) |
---|
| 160 | # d["Subject"] = "Time spent over specific commands during create_ts \ |
---|
| 161 | # jobs at IDRIS and four configurations at TGCC" |
---|
| 162 | # d["Keywords"] = "bench create_ts TGCC IDRIS ncrcat" |
---|
| 163 | # d["CreationDate"] = dt.datetime(2009, 11, 13) |
---|
| 164 | # d["ModDate"] = dt.datetime.today() |
---|
| 165 | |
---|
| 166 | if os.path.isdir(DIR["SAVEPLOT"]): |
---|
| 167 | # img_out = os.path.join(DIR["SAVEPLOT"], |
---|
| 168 | # "{}_{}.pdf".format(img_name, suffix)) |
---|
| 169 | shutil.copy(img_in, img_out) |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | ######################################## |
---|
[2411] | 173 | class Project(object): |
---|
| 174 | |
---|
| 175 | #--------------------------------------- |
---|
| 176 | def __init__(self): |
---|
| 177 | self.project = "" |
---|
| 178 | self.date_init = "" |
---|
| 179 | self.deadline = "" |
---|
| 180 | self.alloc = 0 |
---|
| 181 | |
---|
| 182 | #--------------------------------------- |
---|
| 183 | def fill_data(self, filein): |
---|
| 184 | import json |
---|
| 185 | dico = json.load(open(filein, "r")) |
---|
| 186 | self.project = dico["project"] |
---|
| 187 | self.deadline = string_to_date(dico["deadline"]) + \ |
---|
| 188 | dt.timedelta(days=-1) |
---|
| 189 | self.alloc = dico["alloc"] |
---|
| 190 | |
---|
| 191 | #--------------------------------------- |
---|
| 192 | def get_date_init(self, filein): |
---|
| 193 | data = np.genfromtxt( |
---|
| 194 | filein, |
---|
| 195 | skip_header=1, |
---|
| 196 | converters={0: string_to_date, |
---|
| 197 | 1: string_to_percent}, |
---|
| 198 | missing_values="nan", |
---|
| 199 | ) |
---|
| 200 | dates, utheos = zip(*data) |
---|
| 201 | |
---|
| 202 | (x1, x2) = (np.nanargmin(utheos), np.nanargmax(utheos)) |
---|
| 203 | |
---|
| 204 | m = np.array([[x1, 1.], [x2, 1.]]) |
---|
| 205 | n = np.array([utheos[x1], utheos[x2]]) |
---|
| 206 | |
---|
[2413] | 207 | try: |
---|
| 208 | (a, b) = np.linalg.solve(m, n) |
---|
| 209 | except np.linalg.linalg.LinAlgError: |
---|
| 210 | (a, b) = (None, None) |
---|
[2411] | 211 | |
---|
[2413] | 212 | if a and b: |
---|
| 213 | delta = int(round((-b/a)-x1 + 1)) |
---|
[2411] | 214 | |
---|
[2413] | 215 | d1 = dates[x1] |
---|
| 216 | self.date_init = d1 + dt.timedelta(days=delta) |
---|
| 217 | else: |
---|
| 218 | self.date_init = dt.datetime(self.deadline.year, 1, 1) |
---|
[2411] | 219 | |
---|
[2433] | 220 | delta = self.deadline - self.date_init |
---|
| 221 | self.days = delta.days + 1 |
---|
[2411] | 222 | |
---|
[2433] | 223 | |
---|
[2411] | 224 | ######################################## |
---|
| 225 | class SizeUnit(object): |
---|
| 226 | #--------------------------------------- |
---|
| 227 | def __init__(self, size, unit): |
---|
| 228 | self.size = size |
---|
| 229 | self.unit = unit |
---|
| 230 | |
---|
| 231 | #--------------------------------------- |
---|
| 232 | def __repr__(self): |
---|
| 233 | return "{:6.2f}{}o".format(self.size, self.unit) |
---|
| 234 | |
---|
| 235 | #--------------------------------------- |
---|
| 236 | def convert_size(self, unit_out): |
---|
| 237 | """ |
---|
| 238 | """ |
---|
[2428] | 239 | prefixes = ["o", "K", "M", "G", "T", "P", "H"] |
---|
[2411] | 240 | |
---|
| 241 | if not self.size or \ |
---|
| 242 | self.unit == unit_out: |
---|
| 243 | size_out = self.size |
---|
| 244 | else: |
---|
| 245 | idx_deb = prefixes.index(self.unit) |
---|
| 246 | idx_fin = prefixes.index(unit_out) |
---|
| 247 | size_out = self.size |
---|
| 248 | for i in xrange(abs(idx_fin-idx_deb)): |
---|
| 249 | if idx_fin > idx_deb: |
---|
| 250 | size_out = size_out / 1024 |
---|
| 251 | else: |
---|
| 252 | size_out = size_out * 1024 |
---|
| 253 | |
---|
| 254 | return SizeUnit(size_out, unit_out) |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | ######################################## |
---|
| 258 | if __name__ == '__main__': |
---|
| 259 | pass |
---|
[2433] | 260 | |
---|