source: TOOLS/ConsoGENCMIP6/bin/gencmip6.py @ 2428

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