forked from respec/HSPsquared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone.py
100 lines (84 loc) · 3 KB
/
clone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
''' Copyright (c) 2020 by RESPEC, INC.
Author: Robert Heaphy, Ph.D.
License: LGPL2
'''
from pandas import concat, HDFStore
def clone(hdfname, operation, fromID, toID):
'''
Add new segment ID to all HSP2 HDF5 tables for operation with values fromID
NOTE: Does not add new segment to CONTROL/OP_SEQUENCE. User must update
timeseries in CONTROL/EXT_SOURCES and values in CONTROL/LINKS & CONTROL/MASS_LINKS
Parameters
----------
hdfname : str
Name of HSP2 HDF5 file
operation : str
One of PERLND, IMPLND or RCHRES
fromID : str
Segment name to copy values from
toID : str
New segment name
Returns
-------
None.
'''
with HDFStore(hdfname) as store:
paths = [key for key in store.keys() if key.startswith(f'/{operation}')]
for path in paths:
df = store[path]
if fromID in df.index:
df.loc[toID, :] = df.loc[fromID,:]
df.to_hdf(store, path, format='table', data_columns=True)
path = 'CONTROL/EXT_SOURCES'
df = store[path]
indx = df[(df.TVOL == operation) & (df.TVOLNO == fromID)].index
newdf = df.loc[indx,:]
newdf['TVOLNO'] = toID
dff = concat([df, newdf], ignore_index=True)
dff.to_hdf(store, path, format='table', data_columns=True)
path = 'CONTROL/LINKS'
df = store[path]
indx = df[(df.SVOL == operation) & (df.SVOLNO == fromID)].index
newdf = df.loc[indx,:]
newdf['SVOLNO'] = toID
dff = concat([df, newdf], ignore_index=True)
dff.to_hdf(store, path, format='table', data_columns=True)
return
def removeClone(hdfname, operation, ID):
'''
Removes segment ID from all HSP2 HDF5 tables for specified operation
Parameters
----------
hdfname : str
Name of HSP2 HDF5 file
operation : str
One of PERLND, IMPLND or RCHRES
ID : str
Segment name to remove everywhere in HSP2 HDF5 file
Returns
-------
None.
'''
with HDFStore(hdfname) as store:
paths = [key for key in store.keys() if key.startswith(f'/{operation}')]
for path in paths:
df = store[path]
if ID in df.index:
df = df.drop(index=ID)
df.to_hdf(store, path, format='table', data_columns=True)
path = 'CONTROL/OP_SEQUENCE'
df = store[path]
indx = df[(df.OPERATION==operation) & (df.SEGMENT==ID)].index
df = df.drop(index=indx)
df.to_hdf(store, path, format='table', data_columns=True)
path = 'CONTROL/EXT_SOURCES'
df = store[path]
indx = df[(df.TVOL == operation) & (df.TVOLNO == ID)].index
df = df.drop(index=indx)
df.to_hdf(store, path, format='table', data_columns=True)
path = 'CONTROL/LINKS'
df = store[path]
indx = df[(df.SVOL == operation) & (df.SVOLNO == ID)].index
df = df.drop(index=indx)
df.to_hdf(store, path, format='table', data_columns=True)
return