hspf 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hspf/hspfModel.py ADDED
@@ -0,0 +1,203 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Oct 13 09:26:05 2022
4
+
5
+ @author: mfratki
6
+ """
7
+ from pathlib import Path
8
+ import os.path
9
+ import subprocess
10
+
11
+ from pyhspf.uci import UCI
12
+ from pyhspf import hbn
13
+ from pyhspf.reports import Reports
14
+ from pyhspf.wdm import wdmInterface
15
+ from pyhspf import wdmReader
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ # Only for accessing information regarding a specific uci_file
24
+ # Trying to segregate manipulating the uci file and information about the uci file
25
+
26
+
27
+ class hspfModel():
28
+ winHSPF = str(Path(__file__).resolve().parent) + '\\bin\\WinHSPFLt\\WinHspfLt.exe'
29
+
30
+
31
+ # Imposed structures of an hspf model:
32
+ # 1. all model files are located in the same directory as the uci file.
33
+ def __init__(self,uci_file:str):
34
+ #wdm_files:list = None,
35
+ #hbn_files:str = None):
36
+ # Inputs
37
+ self.uci = UCI(uci_file)
38
+ self.hbn_paths= []
39
+ self.wdm_paths = []
40
+ self.uci_file = Path(uci_file).resolve()
41
+ # Validate and load binary data
42
+ self.validate_uci()
43
+
44
+
45
+ self.hbns = hbn.hbnInterface(self.hbn_paths)
46
+ try:
47
+ self.wdms = wdmInterface(self.wdm_paths)
48
+ except:
49
+ self.wdms = None
50
+
51
+ # Compositions
52
+ self.reports = Reports(self.uci,self.hbns,self.wdms)
53
+
54
+
55
+ def validate_uci(self):
56
+ # Ensure wdm files exist and the folders for the other file types exist relative
57
+ # to the uci path
58
+
59
+ for index, row in self.uci.table('FILES',drop_comments = False).iterrows():
60
+ file_path = self.uci_file.parent.joinpath(Path(row['FILENAME']))
61
+ if file_path.suffix.lower() == '.wdm':
62
+ assert file_path.exists(),'File Specified in the UCI does not exist:' + file_path.as_posix()
63
+ self.wdm_paths.append(file_path)
64
+ elif file_path.suffix.lower() == '.hbn':
65
+ assert file_path.parent.exists(),'File folder Specified in the UCI does not exist: ' + file_path.as_posix()
66
+ #self.hbns[file_path.name.split('.')[0]] = None
67
+ if file_path.exists():
68
+ #self.hbns[file_path.name.split('.')[0]] = hbn.hbnClass(file_path)
69
+ self.hbn_paths.append(file_path)
70
+ else:
71
+ self.run_model()
72
+ else:
73
+ assert file_path.parent.exists(),'File folder Specified in the UCI does not exist: ' + file_path.as_posix()
74
+
75
+ def run_model(self,new_uci_file = None):
76
+
77
+ if new_uci_file is None:
78
+ new_uci_file = self.uci_file
79
+
80
+ # new_uci_file = self.model_path.joinpath(uci_name)
81
+ # self.uci.write(new_uci_file)
82
+ subprocess.run([self.winHSPF,self.uci_file.as_posix()]) #, stdout=subprocess.PIPE, creationflags=0x08000000)
83
+ self.load_uci(new_uci_file)
84
+
85
+ def load_hbn(self,hbn_name):
86
+ self.hbns[hbn_name] = hbn.hbnClass(self.uci_file.parent.joinpath(hbn_name).as_posix())
87
+
88
+ def load_uci(self,uci_file):
89
+ self.uci = UCI(uci_file)
90
+ self.validate_uci()
91
+
92
+ def convert_wdms(self):
93
+ for wdm_file in self.wdm_paths:
94
+ wdmReader.readWDM(wdm_file,
95
+ wdm_file.parent.joinpath(wdm_file.name.replace('.wdm','.hdf5').replace('.WDM','hdf5')))
96
+ self._load_wdms()
97
+
98
+ def load_wdm(self,wdm_file):
99
+ raise NotImplementedError()
100
+
101
+ def _load_wdms(self):
102
+ self.wdms = wdmInterface(self.wdm_paths)
103
+
104
+
105
+ # Model checks
106
+ def check_filename_exist(self,file_extension: str):
107
+ table = self.uci.table('FILES',drop_comments = False)
108
+ uci_path = Path(self.uci_file).parent
109
+ check = []
110
+ for index, row in table.iterrows():
111
+ file_path = Path(row['FILENAME'])
112
+ if file_path.suffix == file_extension:
113
+ relative_path = (uci_path / file_path).resolve()
114
+ check.append(relative_path.exists())
115
+ return all(check)
116
+
117
+
118
+
119
+
120
+ def check_filename_match(self,file_names):
121
+ table = self.uci.table('FILES',drop_comments = False)
122
+ #uci_path = Path(mod.uci.filepath).parent
123
+ for index, row in table.iterrows():
124
+ file_path = Path(row['FILENAME'])
125
+ if file_path.suffix == '.wdm':
126
+ assert(file_path.name in [file_name.name for file_name in file_names])
127
+
128
+ def get_filename_paths(self,file_extension):
129
+ table = self.uci.table('FILES',drop_comments = False)
130
+ wdm_files = []
131
+ for index, row in table.iterrows():
132
+ file_path = Path(row['FILENAME'])
133
+ if file_path.suffix == file_extension:
134
+ wdm_files.append(self.uci_file.parent.joinpath(Path(file_path)))
135
+ return wdm_files
136
+
137
+ def update_filename_paths(self,file_names):
138
+ table = self.uci.table('FILES',drop_comments = False)
139
+ for index, row in table.iterrows():
140
+ file_path = Path(row['FILENAME'])
141
+ for file_name in file_names:
142
+ if file_name.name == file_path.name:
143
+ #print(Path(os.path.relpath(wdm_file, start = uci_path)).as_posix())
144
+ table.loc[index,'FILENAME'] = Path(os.path.relpath(file_name, start = self.uci_file.parent)).as_posix()
145
+ self.uci.replace_table(table,'FILES')
146
+
147
+ def check_filename_folder(self,file_extension):
148
+ table = self.uci.table('FILES',drop_comments = False)
149
+ for index, row in table.iterrows():
150
+ file_path = Path(row['FILENAME'])
151
+ if file_path.suffix == file_extension:
152
+ if self.model_path.joinpath(file_path.parent).exists():
153
+ continue
154
+ else:
155
+ table.loc[index,'FILENAME'] = Path(os.path.relpath(file_path.name, start = self.uci_file.parent)).as_posix()
156
+ self.uci.replace_table(table,'FILES')
157
+
158
+
159
+
160
+
161
+
162
+
163
+ # class runManager():
164
+ # def __init__()
165
+
166
+ # self.requests = {'original': 0,'copy0':0,'copy1':0,'copy2':0}
167
+ # self.childs = {'original':None,
168
+ # 'copy0':None,
169
+ # 'copy1':None,
170
+ # 'copy2':None}
171
+
172
+ # def original_run(self):
173
+ # table = self.table('FILES',drop_comments = False)
174
+ # # Assumes duplicate uci are in uci/copy/
175
+ # wdm_files = [ (index,name.split('/')[-1]) for index,name in enumerate(table['FILENAME'])
176
+ # if name.split('.')[-1] in ['ech','out','wdm']]
177
+ # for file in wdm_files:
178
+ # table.iloc[file[0], table.columns.get_loc('FILENAME')] = '../wdms/' + file[1]
179
+
180
+ # hbn_files = [ (index,name.split('/')[-1]) for index,name in enumerate(table['FILENAME'])
181
+ # if name.split('.')[-1] in ['hbn']]
182
+ # for file in hbn_files:
183
+ # table.iloc[file[0], table.columns.get_loc('FILENAME')] = '../hbns/' + file[1]
184
+
185
+ # self.uci['FILES']['na']['table'][0] = table
186
+ # self.update_lines('FILES')
187
+
188
+ # def duplicate_run(self,copy): #copy1,copy2,copy3 ... copy7 only options
189
+ # table = self.table('FILES',drop_comments = False)
190
+
191
+ # # Assumes duplicate uci are in uci/copy/
192
+ # wdm_files = [ (index,name.split('/')[-1]) for index,name in enumerate(table['FILENAME'])
193
+ # if name.split('.')[-1] in ['ech','out','wdm']]
194
+ # for file in wdm_files:
195
+ # table.iloc[file[0], table.columns.get_loc('FILENAME')] = '../../wdms/' + copy + '/' + file[1]
196
+
197
+ # hbn_files = [ (index,name.split('/')[-1]) for index,name in enumerate(table['FILENAME'])
198
+ # if name.split('.')[-1] in ['hbn']]
199
+ # for file in hbn_files:
200
+ # table.iloc[file[0], table.columns.get_loc('FILENAME')] = '../../hbns/' + file[1]
201
+
202
+ # self.uci['FILES']['na']['table'][0] = table
203
+ # self.update_lines('FILES')
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Thu Dec 9 10:42:05 2021
4
+
5
+ @author: mfratki
6
+ """