pydfnWorks
python wrapper for dfnWorks
paths.py
Go to the documentation of this file.
1 from shutil import move
2 import os
3 import sys
4 import subprocess
5 import json
6 
7 DFNPARAMS = '~/.dfnworksrc'
8 DFNPARAMS = os.path.expanduser(DFNPARAMS)
9 
10 
11 def valid(name, path, path_type):
12  """" Check that path is valid for a executable
13  Parameters
14  ----------
15  name : string
16  Path to file or executable
17  path_type : string
18  Path type can either be an executable or a directory
19 
20  Returns
21  -------
22  0 if paths are valid
23 
24  Notes
25  -----
26  If file is not found, file is not an executable, or directory does not exists then program exits
27 """
28  if path_type == "executable":
29  if not os.path.isfile(path):
30  error = f"Error checking {name}\n{path} is not a valid path to a file name.\nPlease check the path in either pydfnworks/general/paths.py or .dfnworksrc.\nExiting\n"
31  sys.stderr.write(error)
32  sys.exit(1)
33  else:
34  if not os.access(path, os.X_OK):
35  error = f"Error checking {name}\n{path} is not an executable.\nPlease check file permissions.\nExiting\n"
36  sys.stderr.write(error)
37  sys.exit(1)
38 
39  if path_type == "directory":
40  if not os.path.isdir(path):
41  error = f"Error checking {name}\n{path} is not a directory.\nPlease check the path in either pydfnworks/general/paths.py or .dfnworksrc.\nExiting\n"
42  sys.stderr.write(error)
43  sys.exit(1)
44 
45  return 0
46 
47 
48 def compile_dfn_exe(path):
49  """Compile executables used in the DFN workflow including: DFNGen, DFNTrans, correct_uge, correct_stor, mesh_checking. The executables LaGriT, PFLOTRAN, and FEHM are not compiled in this function
50  Parameters
51  ----------
52  directory : string
53  Path to dfnWorks executable
54  Returns
55  -------
56  None
57 
58  Notes
59  -----
60  This function is only called if an executable is not found.
61 """
62 
63  print(f"Compiling {path}")
64  cwd = os.getcwd()
65  os.chdir(path)
66  subprocess.call("make", shell=True)
67  os.chdir(cwd)
68  print("Complete")
69 
70 
72  """Defines environmental variables for use in dfnWorks. The user must change these to match their workspace.
73  Parameters
74  ----------
75  None
76  Returns
77  -------
78  None
79  Notes
80  -----
81  Environmental variables are set to executables
82 """
83 
84  # ================================================
85  # THESE PATHS MUST BE SET BY THE USER.
86  # ================================================
87 
88  # Either write paths to ~/.dfnworksrc in a JSON format...
89  if os.path.isfile(DFNPARAMS):
90  with open(DFNPARAMS, 'r') as f:
91  env_paths = json.load(f)
92  # Or, change the paths here
93  else:
94  env_paths = {
95  'dfnworks_PATH': '',
96  'PETSC_DIR': '',
97  'PETSC_ARCH': '',
98  'PFLOTRAN_EXE': '',
99  'PYTHON_EXE': '',
100  'LAGRIT_EXE': '',
101  'FEHM_EXE': ''
102  }
103 
104  # the dfnworks-main repository
105  os.environ['dfnworks_PATH'] = env_paths['dfnworks_PATH']
106  valid("dfnworks_PATH", os.environ['dfnworks_PATH'], "directory")
107 
108  # PETSC paths
109  os.environ['PETSC_DIR'] = env_paths['PETSC_DIR']
110  os.environ['PETSC_ARCH'] = env_paths['PETSC_ARCH']
111  valid('PETSC_DIR', os.environ['PETSC_DIR'], "directory")
112  valid('PETSC_ARCH',
113  os.environ['PETSC_DIR'] + os.sep + os.environ['PETSC_ARCH'],
114  "directory")
115 
116  # PFLOTRAN path
117  os.environ['PFLOTRAN_EXE'] = env_paths['PFLOTRAN_EXE']
118  valid('PFLOTRAN_EXE', os.environ['PFLOTRAN_EXE'], "executable")
119 
120  # Python executable
121  os.environ['PYTHON_EXE'] = env_paths['PYTHON_EXE']
122  valid('PYTHON_EXE', os.environ['PYTHON_EXE'], "executable")
123 
124  # LaGriT executable
125  os.environ['LAGRIT_EXE'] = env_paths['LAGRIT_EXE']
126  valid('LAGRIT_EXE', os.environ['LAGRIT_EXE'], "executable")
127 
128  os.environ['FEHM_EXE'] = env_paths['FEHM_EXE']
129  valid('FEHM_EXE', os.environ['FEHM_EXE'], "executable")
130 
131  # ===================================================
132  # THESE PATHS ARE AUTOMATICALLY SET. DO NOT CHANGE.
133  # ====================================================
134 
135  # Directories
136  os.environ['DFNGEN_EXE'] = os.environ['dfnworks_PATH'] + 'DFNGen/DFNGen'
137  if not os.path.isfile(os.environ['DFNGEN_EXE']):
138  compile_dfn_exe(os.environ['dfnworks_PATH'] + 'DFNGen/')
139  valid('DFNGen',os.environ['DFNGEN_EXE'],"executable")
140 
141 
142  os.environ[
143  'DFNTRANS_EXE'] = os.environ['dfnworks_PATH'] + 'DFNTrans/DFNTrans'
144  if not os.path.isfile(os.environ['DFNTRANS_EXE']):
145  compile_dfn_exe(os.environ['dfnworks_PATH'] + 'DFNTrans/')
146  valid('DFNTrans',os.environ['DFNTRANS_EXE'],"executable")
147 
148  os.environ['CORRECT_UGE_EXE'] = os.environ[
149  'dfnworks_PATH'] + 'C_uge_correct/correct_uge'
150  if not os.path.isfile(os.environ['CORRECT_UGE_EXE']):
151  compile_dfn_exe(os.environ['dfnworks_PATH'] + 'C_uge_correct/')
152  valid('CORRECT_UGE_EXE',os.environ['CORRECT_UGE_EXE'],"executable")
153 
154 
155  os.environ['CORRECT_STOR_EXE'] = os.environ[
156  'dfnworks_PATH'] + 'C_stor_correct/correct_stor'
157  if not os.path.isfile(os.environ['CORRECT_STOR_EXE']):
158  compile_dfn_exe(os.environ['dfnworks_PATH'] + 'C_stor_correct/')
159  valid('CORRECT_STOR_EXE',os.environ['CORRECT_STOR_EXE'],"executable")
160 
161  os.environ['CONNECT_TEST_EXE'] = os.environ[
162  'dfnworks_PATH'] + 'DFN_Mesh_Connectivity_Test/ConnectivityTest'
163  if not os.path.isfile(os.environ['CONNECT_TEST_EXE']):
164  compile_dfn_exe(os.environ['dfnworks_PATH'] +
165  'DFN_Mesh_Connectivity_Test/')
166  valid('CONNECT_TEST_EXE',os.environ['CONNECT_TEST_EXE'],"executable")
def valid(name, path, path_type)
Definition: paths.py:11
def compile_dfn_exe(path)
Definition: paths.py:48