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

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

put all path definitions in a seperate file

  • Property svn:executable set to *
File size: 3.8 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    print(data)
114    print(type(data))
115    print(data.shape)
116    print(data.size)
117    dates, utheos = zip(*data)
118
119    (x1, x2) = (np.nanargmin(utheos), np.nanargmax(utheos))
120
121    m = np.array([[x1, 1.], [x2, 1.]])
122    n = np.array([utheos[x1], utheos[x2]])
123
124    try:
125      (a, b) = np.linalg.solve(m, n)
126    except np.linalg.linalg.LinAlgError:
127      (a, b) = (None, None)
128
129    if a and b:
130      delta = int(round((-b/a)-x1 + 1))
131
132      d1 = dates[x1]
133      self.date_init = d1 + dt.timedelta(days=delta)
134    else:
135      self.date_init = dt.datetime(self.deadline.year, 1, 1)
136
137
138########################################
139class SizeUnit(object):
140  #---------------------------------------
141  def __init__(self, size, unit):
142    self.size = size
143    self.unit = unit
144
145  #---------------------------------------
146  def __repr__(self):
147    return "{:6.2f}{}o".format(self.size, self.unit)
148
149  #---------------------------------------
150  def convert_size(self, unit_out):
151    """
152    """
153    prefixes = ["K", "M", "G", "T", "P", "H"]
154
155    if not self.size or \
156       self.unit == unit_out:
157      size_out = self.size
158    else:
159      idx_deb = prefixes.index(self.unit)
160      idx_fin = prefixes.index(unit_out)
161      size_out = self.size
162      for i in xrange(abs(idx_fin-idx_deb)):
163        if idx_fin > idx_deb:
164          size_out = size_out / 1024
165        else:
166          size_out = size_out * 1024
167
168    return SizeUnit(size_out, unit_out)
169
170
171########################################
172if __name__ == '__main__':
173  pass
Note: See TracBrowser for help on using the repository browser.