source: TOOLS/ConsoGENCMIP6/plot/gencmip6.py @ 2411

Last change on this file since 2411 was 2411, checked in by labetoulle, 10 years ago

Add ConsoGENCMIP6 tools

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