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

Last change on this file since 2427 was 2427, checked in by labetoulle, 8 years ago

Move common functions to common module

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