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

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

copy plots on dods

  • Property svn:executable set to *
File size: 4.0 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########################################
101class Project(object):
102
103  #---------------------------------------
104  def __init__(self):
105    self.project   = ""
106    self.date_init = ""
107    self.deadline  = ""
108    self.alloc     = 0
109
110  #---------------------------------------
111  def fill_data(self, filein):
112    import json
113    dico = json.load(open(filein, "r"))
114    self.project = dico["project"]
115    self.deadline = string_to_date(dico["deadline"]) + \
116                    dt.timedelta(days=-1)
117    self.alloc = dico["alloc"]
118
119  #---------------------------------------
120  def get_date_init(self, filein):
121    data = np.genfromtxt(
122      filein,
123      skip_header=1,
124      converters={0: string_to_date,
125                  1: string_to_percent},
126      missing_values="nan",
127    )
128    # print(data)
129    # print(type(data))
130    # print(data.shape)
131    # print(data.size)
132    dates, utheos = zip(*data)
133
134    (x1, x2) = (np.nanargmin(utheos), np.nanargmax(utheos))
135
136    m = np.array([[x1, 1.], [x2, 1.]])
137    n = np.array([utheos[x1], utheos[x2]])
138
139    try:
140      (a, b) = np.linalg.solve(m, n)
141    except np.linalg.linalg.LinAlgError:
142      (a, b) = (None, None)
143
144    if a and b:
145      delta = int(round((-b/a)-x1 + 1))
146
147      d1 = dates[x1]
148      self.date_init = d1 + dt.timedelta(days=delta)
149    else:
150      self.date_init = dt.datetime(self.deadline.year, 1, 1)
151
152
153########################################
154class SizeUnit(object):
155  #---------------------------------------
156  def __init__(self, size, unit):
157    self.size = size
158    self.unit = unit
159
160  #---------------------------------------
161  def __repr__(self):
162    return "{:6.2f}{}o".format(self.size, self.unit)
163
164  #---------------------------------------
165  def convert_size(self, unit_out):
166    """
167    """
168    prefixes = ["K", "M", "G", "T", "P", "H"]
169
170    if not self.size or \
171       self.unit == unit_out:
172      size_out = self.size
173    else:
174      idx_deb = prefixes.index(self.unit)
175      idx_fin = prefixes.index(unit_out)
176      size_out = self.size
177      for i in xrange(abs(idx_fin-idx_deb)):
178        if idx_fin > idx_deb:
179          size_out = size_out / 1024
180        else:
181          size_out = size_out * 1024
182
183    return SizeUnit(size_out, unit_out)
184
185
186########################################
187if __name__ == '__main__':
188  pass
Note: See TracBrowser for help on using the repository browser.