[2411] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | # this must come first |
---|
| 5 | from __future__ import print_function, unicode_literals, division |
---|
| 6 | |
---|
| 7 | # standard library imports |
---|
[2413] | 8 | import socket |
---|
[2411] | 9 | import os |
---|
| 10 | import os.path |
---|
| 11 | import glob |
---|
[2421] | 12 | import shutil |
---|
[2411] | 13 | import datetime as dt |
---|
| 14 | import numpy as np |
---|
| 15 | |
---|
[2421] | 16 | # Application library imports |
---|
| 17 | from gencmip6_path import * |
---|
[2411] | 18 | |
---|
[2421] | 19 | |
---|
[2411] | 20 | ######################################## |
---|
[2421] | 21 | def 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 | ######################################## |
---|
[2411] | 35 | def string_to_percent(x): |
---|
| 36 | """ |
---|
| 37 | """ |
---|
| 38 | return float(x.strip("%"))/100. |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | ######################################## |
---|
| 42 | def string_to_size_unit(x): |
---|
| 43 | """ |
---|
| 44 | """ |
---|
| 45 | (size, unit) = (float(x[:-1]), x[-1]) |
---|
| 46 | return SizeUnit(size, unit) |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | ######################################## |
---|
| 50 | def string_to_float(x): |
---|
| 51 | """ |
---|
| 52 | """ |
---|
| 53 | return float(x.strip("h")) |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | ######################################## |
---|
| 57 | def 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 | ######################################## |
---|
[2413] | 71 | def 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 | ######################################## |
---|
[2411] | 85 | def get_last_file(dir_data, pattern): |
---|
| 86 | """ |
---|
| 87 | """ |
---|
| 88 | current_dir = os.getcwd() |
---|
| 89 | os.chdir(dir_data) |
---|
| 90 | filename = pattern + "*" |
---|
[2413] | 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 |
---|
[2411] | 96 | os.chdir(current_dir) |
---|
[2413] | 97 | return res |
---|
[2411] | 98 | |
---|
| 99 | |
---|
| 100 | ######################################## |
---|
| 101 | class 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 | ) |
---|
[2420] | 128 | # print(data) |
---|
| 129 | # print(type(data)) |
---|
| 130 | # print(data.shape) |
---|
| 131 | # print(data.size) |
---|
[2411] | 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 | |
---|
[2413] | 139 | try: |
---|
| 140 | (a, b) = np.linalg.solve(m, n) |
---|
| 141 | except np.linalg.linalg.LinAlgError: |
---|
| 142 | (a, b) = (None, None) |
---|
[2411] | 143 | |
---|
[2413] | 144 | if a and b: |
---|
| 145 | delta = int(round((-b/a)-x1 + 1)) |
---|
[2411] | 146 | |
---|
[2413] | 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) |
---|
[2411] | 151 | |
---|
| 152 | |
---|
| 153 | ######################################## |
---|
| 154 | class 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 | ######################################## |
---|
| 187 | if __name__ == '__main__': |
---|
| 188 | pass |
---|