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