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

Last change on this file since 2413 was 2413, checked in by labetoulle, 9 years ago

Move all scripts to the same dir to use common configuration files

  • Property svn:executable set to *
File size: 3.7 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
8# from argparse import ArgumentParser
9import socket
10import os
11import os.path
12import glob
13import datetime as dt
14import numpy as np
15# import matplotlib.pyplot as plt
16# from matplotlib.backends.backend_pdf import PdfPages
17
18
19########################################
20def string_to_percent(x):
21  """
22  """
23  return float(x.strip("%"))/100.
24
25
26########################################
27def string_to_size_unit(x):
28  """
29  """
30  (size, unit) = (float(x[:-1]), x[-1])
31  return SizeUnit(size, unit)
32
33
34########################################
35def string_to_float(x):
36  """
37  """
38  return float(x.strip("h"))
39
40
41########################################
42def string_to_date(ssaammjj, fmt="%Y-%m-%d"):
43  """
44  """
45  return dt.datetime.strptime(ssaammjj, fmt)
46
47
48# ########################################
49# def date_to_string(dtdate, fmt="%Y-%m-%d"):
50#   """
51#   """
52#   return dt.datetime.strftime(dtdate, fmt)
53
54
55########################################
56def where_we_run():
57
58  res = ""
59  if "curie" in socket.getfqdn():
60    res = "curie"
61  elif "ipsl" in socket.getfqdn():
62    res = "ipsl"
63  else:
64    res = "default"
65
66  return res
67
68
69########################################
70def get_last_file(dir_data, pattern):
71  """
72  """
73  current_dir = os.getcwd()
74  os.chdir(dir_data)
75  filename = pattern + "*"
76  file_list = glob.glob(os.path.join(dir_data, filename))
77  if file_list:
78    res = sorted(file_list)[-1]
79  else:
80    res = None
81  os.chdir(current_dir)
82  return res
83
84
85########################################
86class Project(object):
87
88  #---------------------------------------
89  def __init__(self):
90    self.project   = ""
91    self.date_init = ""
92    self.deadline  = ""
93    self.alloc     = 0
94
95  #---------------------------------------
96  def fill_data(self, filein):
97    import json
98    dico = json.load(open(filein, "r"))
99    self.project = dico["project"]
100    self.deadline = string_to_date(dico["deadline"]) + \
101                    dt.timedelta(days=-1)
102    self.alloc = dico["alloc"]
103
104  #---------------------------------------
105  def get_date_init(self, filein):
106    data = np.genfromtxt(
107      filein,
108      skip_header=1,
109      converters={0: string_to_date,
110                  1: string_to_percent},
111      missing_values="nan",
112    )
113    dates, utheos = zip(*data)
114
115    (x1, x2) = (np.nanargmin(utheos), np.nanargmax(utheos))
116
117    m = np.array([[x1, 1.], [x2, 1.]])
118    n = np.array([utheos[x1], utheos[x2]])
119
120    try:
121      (a, b) = np.linalg.solve(m, n)
122    except np.linalg.linalg.LinAlgError:
123      (a, b) = (None, None)
124
125    if a and b:
126      delta = int(round((-b/a)-x1 + 1))
127
128      d1 = dates[x1]
129      self.date_init = d1 + dt.timedelta(days=delta)
130    else:
131      self.date_init = dt.datetime(self.deadline.year, 1, 1)
132
133
134########################################
135class SizeUnit(object):
136  #---------------------------------------
137  def __init__(self, size, unit):
138    self.size = size
139    self.unit = unit
140
141  #---------------------------------------
142  def __repr__(self):
143    return "{:6.2f}{}o".format(self.size, self.unit)
144
145  #---------------------------------------
146  def convert_size(self, unit_out):
147    """
148    """
149    prefixes = ["K", "M", "G", "T", "P", "H"]
150
151    if not self.size or \
152       self.unit == unit_out:
153      size_out = self.size
154    else:
155      idx_deb = prefixes.index(self.unit)
156      idx_fin = prefixes.index(unit_out)
157      size_out = self.size
158      for i in xrange(abs(idx_fin-idx_deb)):
159        if idx_fin > idx_deb:
160          size_out = size_out / 1024
161        else:
162          size_out = size_out * 1024
163
164    return SizeUnit(size_out, unit_out)
165
166
167########################################
168if __name__ == '__main__':
169  pass
Note: See TracBrowser for help on using the repository browser.