popt.misc_tools.basic_tools
1import numpy as np 2import sys 3 4""" 5Collection of simple, yet useful Python tools 6""" 7 8 9def index2d(list2d, value): 10 """ 11 Search in a 2D list for pattern or value and return is (i, j) index. If the 12 pattern/value is not found, (None, None) is returned 13 14 Example: 15 16 >>> l = [['string1', 1], ['string2', 2]] 17 >>> print index2d(l, 'string1') 18 (0, 0) 19 20 Parameters 21 ---------- 22 list2d : list of lists 23 2D list. 24 25 value : object 26 Pattern or value to search for. 27 28 Returns 29 ------- 30 ind : tuple 31 Indices (i, j) of the value. 32 """ 33 return next(((i, j) for i, lst in enumerate(list2d) for j, x in enumerate(lst) if x == value), None) 34 35 36def read_file(val_type, filename): 37 """ 38 Read an eclipse file with specified keyword. 39 Example: 40 read_file('PERMX','filename.permx') 41 42 Input: 43 - val_type: keyword or property 44 - filename: the file that is read 45 46 Output: 47 - values: a vector with values for each cell 48 """ 49 50 file = open(filename, 'r') 51 lines = file.readlines() 52 key = '' 53 line_idx = 0 54 while key != val_type: 55 line = lines[line_idx] 56 if not line: 57 print('Error: Keyword not found') 58 sys.exit(1) 59 60 line_idx += 1 61 if len(line): 62 key = line.split() 63 if key: 64 key = key[0] 65 data = [] 66 finished = False 67 while line_idx < len(lines) and not finished: 68 line = lines[line_idx] 69 line_idx += 1 70 if line == '\n' or line[:2] == '--': 71 continue 72 if line == '': 73 break 74 if line.strip() == '/': 75 finished = True 76 sub_str = line.split() 77 for s in sub_str: 78 if '*' in s: 79 num_val = s.split('*') 80 v = float(num_val[1]) * np.ones(int(num_val[0])) 81 data.append(v) 82 elif '/' in s: 83 finished = True 84 break 85 else: 86 data.append(float(s)) 87 88 values = np.hstack(data) 89 return values 90 91 92def write_file(filename, val_type, data): 93 """ 94 Write an eclipse file with specified keyword. 95 Example: 96 write_file('filename.permx','PERMX',data_vec) 97 98 Input: 99 - filename: the file that is read 100 - val_type: keyword or property 101 - data : data written to file 102 """ 103 104 file = open(filename, 'w') 105 file.writelines(val_type + '\n') 106 if data.dtype == 'int64': 107 np.savetxt(file, data, fmt='%i') 108 else: 109 np.savetxt(file, data) 110 file.writelines('/' + '\n') 111 file.close()
def
index2d(list2d, value):
10def index2d(list2d, value): 11 """ 12 Search in a 2D list for pattern or value and return is (i, j) index. If the 13 pattern/value is not found, (None, None) is returned 14 15 Example: 16 17 >>> l = [['string1', 1], ['string2', 2]] 18 >>> print index2d(l, 'string1') 19 (0, 0) 20 21 Parameters 22 ---------- 23 list2d : list of lists 24 2D list. 25 26 value : object 27 Pattern or value to search for. 28 29 Returns 30 ------- 31 ind : tuple 32 Indices (i, j) of the value. 33 """ 34 return next(((i, j) for i, lst in enumerate(list2d) for j, x in enumerate(lst) if x == value), None)
Search in a 2D list for pattern or value and return is (i, j) index. If the pattern/value is not found, (None, None) is returned
Example:
>>> l = [['string1', 1], ['string2', 2]]
>>> print index2d(l, 'string1')
(0, 0)
Parameters
- list2d (list of lists): 2D list.
- value (object): Pattern or value to search for.
Returns
- ind (tuple): Indices (i, j) of the value.
def
read_file(val_type, filename):
37def read_file(val_type, filename): 38 """ 39 Read an eclipse file with specified keyword. 40 Example: 41 read_file('PERMX','filename.permx') 42 43 Input: 44 - val_type: keyword or property 45 - filename: the file that is read 46 47 Output: 48 - values: a vector with values for each cell 49 """ 50 51 file = open(filename, 'r') 52 lines = file.readlines() 53 key = '' 54 line_idx = 0 55 while key != val_type: 56 line = lines[line_idx] 57 if not line: 58 print('Error: Keyword not found') 59 sys.exit(1) 60 61 line_idx += 1 62 if len(line): 63 key = line.split() 64 if key: 65 key = key[0] 66 data = [] 67 finished = False 68 while line_idx < len(lines) and not finished: 69 line = lines[line_idx] 70 line_idx += 1 71 if line == '\n' or line[:2] == '--': 72 continue 73 if line == '': 74 break 75 if line.strip() == '/': 76 finished = True 77 sub_str = line.split() 78 for s in sub_str: 79 if '*' in s: 80 num_val = s.split('*') 81 v = float(num_val[1]) * np.ones(int(num_val[0])) 82 data.append(v) 83 elif '/' in s: 84 finished = True 85 break 86 else: 87 data.append(float(s)) 88 89 values = np.hstack(data) 90 return values
Read an eclipse file with specified keyword. Example: read_file('PERMX','filename.permx')
Input: - val_type: keyword or property - filename: the file that is read
Output: - values: a vector with values for each cell
def
write_file(filename, val_type, data):
93def write_file(filename, val_type, data): 94 """ 95 Write an eclipse file with specified keyword. 96 Example: 97 write_file('filename.permx','PERMX',data_vec) 98 99 Input: 100 - filename: the file that is read 101 - val_type: keyword or property 102 - data : data written to file 103 """ 104 105 file = open(filename, 'w') 106 file.writelines(val_type + '\n') 107 if data.dtype == 'int64': 108 np.savetxt(file, data, fmt='%i') 109 else: 110 np.savetxt(file, data) 111 file.writelines('/' + '\n') 112 file.close()
Write an eclipse file with specified keyword. Example: write_file('filename.permx','PERMX',data_vec)
Input: - filename: the file that is read - val_type: keyword or property - data : data written to file