forked from baklanovp/pystella
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpystella.py
More file actions
executable file
·412 lines (360 loc) · 12.7 KB
/
pystella.py
File metadata and controls
executable file
·412 lines (360 loc) · 12.7 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
# #!/usr/bin/python3
import os
import readline
import subprocess
import multiprocessing as mp
# import shlex
from cmd import Cmd
from os.path import dirname
# import argparse
import numpy as np
ROOT_DIRECTORY = dirname(os.path.abspath(__file__))
# hist_file = os.path.join(ROOT_DIRECTORY, '.pystella_history')
hist_file = os.path.expanduser('~/.pystella_history')
hist_file_size = 1000
class HistConsole(Cmd):
@property
def hfile(self):
local = '.pystella_history'
if os.path.exists(local):
fname = local
else:
fname = hist_file
return fname
def preloop(self):
if readline and os.path.exists(self.hfile):
readline.read_history_file(self.hfile)
def postloop(self):
if readline:
readline.set_history_length(hist_file_size)
readline.write_history_file(self.hfile)
@staticmethod
def do_h(args):
"""Show history
:type args: number of commands
"""
try:
lim = int(args)
except:
lim = 10
hlen = readline.get_current_history_length()
for i in np.arange(max(0, hlen-lim), hlen):
print("{0}: {1}".format(i, readline.get_history_item(i)))
class MyPrompt(HistConsole):
options = {}
@staticmethod
def insert_options(line):
res = line
if len(MyPrompt.options) > 0:
for k, v in MyPrompt.options.items():
s = '$'+k
if s in res:
res = res.replace(s, v)
return res
@staticmethod
def is_set_proc():
"""
Return True if set 'proc'
To detach the command process
"""
# if len(MyPrompt.options) > 0:
# for k, v in MyPrompt.options.items():
# if 'proc' in k:
# return True
return 'proc' in MyPrompt.options
def bad_insert_options(line):
words = line.split()
keys = [w[1:] for w in words if w.startswith('$')]
res = line
if len(keys) > 0:
for k in keys:
v = MyPrompt.options.get(k, False)
if v:
res = res.replace('$'+k, v)
else:
print(f'You should set up the var: {k}')
return res
@staticmethod
def do_set(args, sep='=', sep_group=';'):
"""Set global options via name = value.
To insert option to command use prefix: $.
For example:
pystella>set ebv=0.07
pystella>set mdl= cR500M20Ni06_3eps
pystella>ubv -i $mdl -e $ebv
>>/home/bakl/Sn/Release/python/pystella/ubv.py -i cR500M20Ni06_3eps -e 0.07
Run 'set' without arguments to see the current options: pystella>set
You may set many options separating them via semicolon ;
pystella>set ebv=0.07; mdl= cR500M20Ni06_3eps
Set 'proc' to run commands in seperated proccesses. Use Cntr+D to go next cmd.
To remove name: pystella>set name= None
"""
if not args:
print(MyPrompt.options)
return
if sep not in args:
print(f'Format: key {sep} value')
return
if sep_group not in args:
args += sep_group # add 1 sep_group
# print('Before: ', MyPrompt.options)
for group in args.split(sep_group):
group = group.strip()
if sep not in group:
print(f'No {sep} in "{group}"')
continue
k, v = group.split(sep)
k, v = k.strip(), v.strip()
print(k, v)
if v == 'None':
MyPrompt.options.pop(k, None) # remove key
else:
MyPrompt.options[k] = v.strip()
print(MyPrompt.options)
@staticmethod
def call_cmd(script, args, is_proc=False, is_shell=True):
import multiprocessing
def run_script():
print('process: id {} parent {}'.format(os.getpid(), os.getppid()))
subprocess.call(command, shell=is_shell)
# with subprocess.Popen([command], bufsize=1, shell=True) as p:
# stdout, stderr = p.communicate()
# for line in stdout:
# print(line, end='')
# print("")
command = "{0} {1}".format(script, args).strip()
if '$' in command:
# print(command)
command = MyPrompt.insert_options(command)
print(f">>> {command}")
# print(args, is_proc)
is_proc = is_proc or MyPrompt.is_set_proc()
if is_proc:
# print(f'Detach thread')
# print('process id:', os.getpid())
# print(f'Detach thread. script= {command}')
# print(f' args.strip()= {args.strip()}')
p = mp.Process(target=run_script)
p.start()
# with subprocess.Popen([script, args], stdout=subprocess.PIPE, bufsize=1,
# shell=True,
# universal_newlines=True) as p:
# stdout, stderr = p.communicate()
# for line in stdout:
# print(line, end='')
else:
subprocess.call(command, shell=is_shell)
@staticmethod
def do_snespace(args):
"""Plot SN using json-file from https://sne. For detailed help type 'snespace -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'sne_space.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_h5(args):
"""Work with Stella models via h5-file. For detailed help type 'h5 -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'h5.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_snec(args):
"""Convert SNEC models to Stella models. For detailed help type 'snec -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'snec.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_mesa(args):
"""Convert MESA models to Stella models. For detailed help type 'mesa -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'mesa.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_ls(args):
"""Show Stella models. For detailed help type 'ls -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'ls.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_ubv(args):
"""Plot Stella Light Curves. For detailed help type 'ubv -h'.
"""
if len(args) == 0:
name = 'Please provide a model-file.'
else:
name = args
print(f"ubv {name}")
script = os.path.join(ROOT_DIRECTORY, 'ubv.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_fit(args):
"""Fit with Stella Obs Light Curves. For detailed help type 'fit -h'.
"""
if len(args) == 0:
name = 'Please provide a model-file AND/OR some options'
else:
name = args
print("fit %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'fit.py')
MyPrompt.call_cmd(script, args)
# @staticmethod
# def do_run(args):
# """Run Stella. For detailed help type 'fit -h'.
# """
# if len(args) == 0:
# name = 'Please provide a model-file AND/OR some options'
# else:
# name = args
# print("run %s" % name)
# script = os.path.join(ROOT_DIRECTORY, 'run_stella.py')
# MyPrompt.call_cmd(script, args)
@staticmethod
def do_obs(args):
"""Plot Observed Light Curves. For detailed help type 'obs -h'.
"""
if len(args) == 0:
name = 'Please provide a obs. data. You may use "lcobs:fname:marker:dt:dm" '
else:
name = args
print("obs %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'obs.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_spec(args):
"""Plot Stella Light Curves. For detailed help type 'spec -h'.
"""
if len(args) == 0:
name = 'Please provide a ph-file.'
else:
name = args
print("spec %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'spec.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_tau(args):
"""Plot the optical depth of the model. For detailed help type 'tau -h'.
"""
if len(args) == 0:
name = 'Please provide a tau-file.'
else:
name = args
print("tau %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'tau.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_eve(args):
"""Plot Stella Chemical structure. For detailed help type 'reve -h'.
"""
if len(args) == 0:
name = 'No data. Please provide a rho-file.'
else:
name = args
print("eve %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'eve.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_reve(args):
"""Plot Supremna Light Curves. For detailed help type 'reve -h'.
"""
if len(args) == 0:
name = 'No data. Please provide a rho-file.'
else:
name = args
print("reve %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'rsn_eve.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_swd(args):
"""Plot Stella Shock Wave Details. For detailed help type 'swd -h'.
"""
if len(args) == 0:
name = 'No data. Please provide a swd-file.'
else:
name = args
print("swd %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'swd.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_rswd(args):
"""Plot Supremna Shock Wave Details. For detailed help type 'rswd -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'rsn_swd.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_reng(args):
"""Plot Supremna Energy Details. For detailed help type 'reng -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'rsn_eng.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_bands(args):
"""Plot passbands. For detailed help type 'bands -h'.
"""
if len(args) == 0:
name = 'All the passbands.'
else:
name = args
print("bands %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'bands.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_run(args):
"""Run the Stella simulations. For detailed help type 'run -h'.
"""
script = os.path.join(ROOT_DIRECTORY, 'run_stella.py')
MyPrompt.call_cmd(script, args)
@staticmethod
def do_zeta(args):
"""Plot Tcolor-Zeta diagrams. For detailed help type 'zeta -h'.
"""
if len(args) == 0:
name = 'Please provide some options'
else:
name = args
print("zeta %s" % name)
script = os.path.join(ROOT_DIRECTORY, 'zeta.py')
MyPrompt.call_cmd(script, args)
def do_quit(self, args):
"""Quits the program."""
self.postloop()
print("Do Svidanya!")
raise SystemExit
def do_q(self, args):
"""Quits the program."""
self.do_quit(args)
@staticmethod
def do_ipython(args):
"""Run ipython shell
Usage:
It was already done: import pystella as ps
so you may:
> s = ps.Stella('model3xni')
> curves = s.curves(bands=('U','B','R','V'))
> ax = ps.light_curve_plot.curves_plot(curves)
> plt.show()
OR
> ax.get_figure().savefig('zz.pdf')
> ...
"""
import math
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
#
import pystella as ps
# from pystella import callback as cb
# from pystella import velocity as vel
# from pystella import band
# from pystella import Stella
# from pystella import light_curve_func as lcf
# from pystella import light_curve_plot as lcp
from IPython import embed
embed(banner1="Hit Ctrl-D to exit interpreter and continue pystella",
exit_msg="Back in pystella, moving along...")
if __name__ == '__main__':
# mp.set_start_method('spawn')
prompt = MyPrompt()
prompt.prompt = '\x01\u001b[35m\x02' + 'pystella>' + '\x01\u001b[0m\x02'
# prompt.prompt = '\x1b[1;35;47m' + 'pystella>' + '\x1b[0m'
# prompt.prompt = "pystella>"
prompt.cmdloop('Let''s study a supernova ... \n type "help" for help ')