-
Notifications
You must be signed in to change notification settings - Fork 12
/
install_radex.py
306 lines (257 loc) · 11.1 KB
/
install_radex.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#raise "This was left in a terrible debug state may 25, 2021"
from __future__ import print_function
import tarfile
import sys
import re
import os
import shutil
import glob
import warnings
from numpy import f2py
from subprocess import CompletedProcess
try:
from astropy.utils.data import download_file
except ImportError:
download_file = None
def install_radex(download=True, extract=True, patch=True, compile=True):
if download:
filename = download_radex()
if extract:
extract_radex(filename)
if patch:
patch_radex()
if compile:
compile_radex()
def download_radex(redownload=True,
url='https://personal.sron.nl/~vdtak/radex/radex_public.tar.gz'):
filename = 'radex_public.tar.gz'
if os.path.isfile(filename) and not redownload:
return filename
print("Downloading RADEX")
try:
filename = download_file(url, cache=True)
assert os.path.exists(filename)
except:
import requests
r = requests.get(url,
#data={'filename':filename},
stream=True,
verify=False)
with open(filename,'wb') as f:
for chunk in r.iter_content(1024):
f.write(chunk)
print("Download succeeded, or at least didn't obviously fail.")
return filename
def extract_radex(filename='radex_public.tar.gz'):
print("Extracting RADEX source from file {0}".format(filename))
with tarfile.open(filename,mode='r:gz') as tar:
# this will fail if readonly files are overwritten: tf.extractall()
for file_ in tar:
try:
tar.extract(file_)
except IOError as e:
os.remove(file_.name)
tar.extract(file_)
finally:
os.chmod(file_.name, file_.mode)
def patch_radex():
# PATCHES
radlines = []
vers = re.compile("parameter\(version = '([a-z0-9]*)'\)")
with open('Radex/src/radex.inc') as f:
for line in f.readlines():
if ('parameter(radat' in line or
'parameter(version' in line or
'parameter(logfile' in line or
'parameter (method' in line):
line = 'c'+line
if vers.search(line):
radlines.append("c version = '%s'\n" % vers.search(line).groups()[0])
if 'parameter(debug' in line:
line = 'c'+line
radlines.append(' common/dbg/debug\n')
radlines.append(line)
if 'parameter (method = 3)' in line:
radlines.append(' common/setup/radat,method,version,logfile\n')
with open('Radex/src/radex.inc','w') as f:
for line in radlines:
f.write(line)
with open('Radex/src/background.f') as f:
lines = f.readlines()
with open('Radex/src/background.f','w') as f:
for line in lines:
if 'parameter(huge' in line:
f.write(' parameter(huge=1.0e38)\n')
f.write('c ! highest allowed by f90 (fdvt 28apr06)\n')
else:
f.write(line)
with open('Radex/src/readdata.f') as f:
lines = f.readlines()
with open('Radex/src/readdata.f','w') as f:
# comment out the block dealing with ortho/para ratio: let python
# handle that
for ii,line in enumerate(lines):
if ii <= 225 or ii > 235:
f.write(line)
else:
f.write("c"+line[1:])
#if 'density(3) = density(1)/(1.d0+1.d0/opr)' in line:
# f.write(line)
# f.write('c For conservation of total density, set n(H2) = 0\n')
# f.write(' density(1) = 0.0\n')
#else:
# f.write(line)
"""
Works for hpc:
PATH=/Users/adam/repos/hpc/bin/:/usr/bin:~/virtual-python/bin/:/bin FFLAGS='-m64 -fPIC' CFLAGS='-fno-strict-aliasing -fno-automatic -fno-common -dynamic -m64 -g -O2' LDFLAGS='-m64 -undefined dynamic_lookup -bundle' python -c "import install_radex; install_radex.compile_radex(f77exec='/Users/adam/repos/hpc/bin/gfortran')"
Works for 4.2.3:
FFLAGS='-arch i686 -arch x86_64 -fPIC' CFLAGS='-fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -O2' LDFLAGS='-arch i686 -arch x86_64 -undefined dynamic_lookup -bundle' python setup.py install_radex
"""
def compile_radex(fcompiler='gfortran',f77exec=None):
#r1 = os.system('f2py -h pyradex/radex/radex.pyf Radex/src/*.f --overwrite-signature > radex_build.log')
pwd = os.getcwd()
os.chdir('Radex/src/')
files = glob.glob('*.f')
include_path = '--include-paths {0}'.format(os.getcwd())
f2py.run_main(' -h radex.pyf --overwrite-signature'.split()+include_path.split()+files)
if f77exec is None:
f77exec=''
else:
f77exec = '--f77exec=%s' % f77exec
#cmd = '-m radex -c %s --fcompiler=%s %s' % (" ".join(files), fcompiler, f77exec)
#f2py.run_main(['-m','radex','-c','--fcompiler={0}'.format(fcompiler), f77exec,] + files)
source_list = []
for fn in files:
with open(fn, 'r') as f:
source_list.append(f.read())
source = "\n".join(source_list)
#with open("merged_source.f", 'wb') as fh:
# fh.write(source)
include_path = '-I{0}'.format(os.getcwd())
import platform
mac_ver = platform.mac_ver()
# not sure if this check is robust enough
if mac_ver[0] and int(mac_ver[0].split('.')[0]) >= 12:
linkdir = '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib'
if os.path.exists(linkdir):
linker_path = f'-L{linkdir}'
else:
other_linkpaths = glob.glob('/Library/Developer/CommandLineTools/SDKs/MacOSX*.sdk/usr/lib')
if len(other_linkpaths) >= 1:
linker_path = f'-L{other_linkpaths[0]}'
print(f"Set linkpath to {linker_path}")
else:
warnings.warn("NO LINK PATH WAS FOUND! Check that Mac OS X software development kit (SDK) is installed, and check where it's installed; it should be in /Library/Developer/CommandLineTools/SDKs/")
else:
linker_path = ''
extra_args = '--f77flags="-fno-automatic" --fcompiler={0} {1} {2} {3}'.format(fcompiler,
f77exec,
include_path,
linker_path)
print(f"Running f2py with fcompiler={fcompiler}, f77exec={f77exec}, include_path={include_path}, linker_path={linker_path}")
print(f"extra args = {extra_args}")
print(f"Current directory = {os.getcwd()}")
#f2py_path = os.path.join(sys.exec_prefix, 'bin', 'f2py')
# Hack doesn't work.
command = f'f2py -c -m radex {extra_args} *.f'
# print(command)
# import subprocess
# r2 = subprocess.run(command.split(), stdout=subprocess.PIPE,
# stderr=subprocess.PIPE, env=os.environ)
try:
r2 = f2py.compile(source="merged_source.f", modulename='radex',
verbose=True, full_output=False,
extra_args=extra_args,)
except TypeError:
# Regression fix for https://github.com/keflavich/pyradex/issues/37
print("You're using numpy version <1.20, which may cause additional failures. See Issue 37")
r2 = f2py.compile(source="merged_source.f", modulename='radex',
verbose=True,
extra_args=extra_args,)
print(f"Done running f2py in {os.getcwd()}. r2={r2}")
# 0 on success, or a subprocess.CompletedProcess if full_output=True
if not isinstance(r2, CompletedProcess) and r2 != 0:
r3 = f2py.compile(source="merged_source.f", modulename='radex',
verbose=True, full_output=True,
extra_args=extra_args,)
raise SystemError(f"f2py failed with error {r2}\n"
"Try running the command: "
f"\n\ncd Radex/src/\n{command}\ncd -\nmv Radex/src/*so pyradex/radex/\n\n"
"See also Github issues 39 and 40\n"
)
os.chdir(pwd)
outfile = glob.glob("Radex/src/radex.*so")
if len(outfile) != 1:
print("outfile = {0}".format(outfile))
raise OSError("Did not find the correct .so file(s)! Compilation has failed.\n"
"Try running the command: "
f"\n\ncd Radex/src/\n{command}\ncd -\nmv Radex/src/*so pyradex/radex/\n\n"
"See also Github issues 39 and 40")
sofile = outfile[0]
r3 = shutil.move(sofile, 'pyradex/radex/radex.so')
def build_radex_executable(datapath='./'):
filename = download_radex(redownload=False)
# need to re-extract the RADEX source to get an un-patched version
extract_radex(filename)
compile_radex_source(datapath=datapath)
def compile_radex_source(datapath='./'):
"""
Compile the source file in the Radex/ directory
May be good to download & untar first
"""
cwd = os.getcwd()
# Set datapath to the 'examples' directory if not specified (since there is
# at least co.dat there)
if datapath is None:
datapath = os.path.join(os.getcwd(), 'examples')
print("Datapath was not defined. Set to ",datapath)
# make sure ~ gets expanded
datapath = os.path.expanduser(datapath)
try:
os.link('Radex/data/hco+.dat',os.path.join(datapath,'hco+.dat'))
except OSError:
pass
# I think fortran requires a trailing slash... can't hurt
if datapath[-1] != '/':
datapath = datapath+'/'
os.chdir('Radex/src/')
with open('radex.inc','r') as f:
lines = [L.replace('/Users/floris/Radex/moldat/',datapath)
if 'radat' in L else L
for L in f.readlines()]
with open('radex.inc','w') as of:
of.writelines(lines)
method_types = {1:'sphere',2:'lvg',3:'slab'}
for method in method_types:
radex_inc_method('./',method=method)
r1 = os.system('make')
if r1 != 0:
raise SystemError("radex make failed with error %i" % r1)
shutil.move('../bin/radex','../bin/radex_%s' % method_types[method])
os.chdir(cwd)
def radex_inc_method(datapath, method=1):
"""
Convert the radex.inc file to a method
Parameters
----------
datapath: path
a directory path containing the target radex.inc
method: 1,2,3
1: sphere
2: lvg
3: slab
"""
fn = os.path.join(datapath, 'radex.inc')
with open(fn,'r') as f:
lines = [L.replace('/Users/floris/Radex/moldat/',datapath)
if 'radat' in L else L
for L in f.readlines()]
radlines = []
for line in lines:
if ('parameter (method' in line):
line = 'c'+line
if 'method = 3' in line:
radlines.append(' parameter (method = %i)\n' % method)
radlines.append(line)
with open(fn,'w') as f:
f.writelines(radlines)