#! /usr/bin/python3 from collections import OrderedDict import textwrap from os import environ from pathlib import Path import re parameter_zero = {'_':-1} def make_keppar(path = None, file = None): """Create the KEPLER parameter file form the KEPLER source code.""" if file is None: if path is None: path = ( Path(environ.get('KEPLER_PATH', '~/kepler')).expanduser() / environ.get('KEPLER_PYTHON', 'source') ) file = path / 'kepdef.f90' with open(file) as f: s = f.read() r = re.compile(r'(?ms)^ *data *[(]nameparm[(]ii[)],ii=[0-9]*,(?:[0-9]*|nparm)[)] */([^/]*)/') pnames = ''.join(r.findall(s)) r = re.compile(r"(?ms)'([^']*)'") pnames = r.findall(pnames) r = re.compile(r'(?ms)^ *data *[(]iptype[(]ii[)],ii=[0-9]*,(?:[0-9]*|nparm)[)] /([^/]*)/') pvalues = ','.join(r.findall(s)) r = re.compile(r'(?ms)^ *([^\n]*)$') pvalues = ''.join(r.findall(pvalues)) pvalues = pvalues.replace(' ','') pvalues = pvalues.replace('&','') pvalues = [int(px) for px in re.split(',',pvalues)] print('nparm = '+str(len(pvalues))) p = OrderedDict(parameter_zero) p.update(zip(pnames, pvalues)) r = re.compile(r'(?ms)^ *data *[(]nameqprm[(]ii[)],ii=[0-9]*,(?:[0-9]*|nqparm)[)] /([^/]*)/') qnames = ''.join(r.findall(s)) r = re.compile(r"(?ms)'([^']*)'") qnames = r.findall(qnames) r = re.compile(r'(?ms)^ *data *[(]iqtype[(]ii[)],ii=[0-9]*,(?:[0-9]*|nqparm)[)] /([^/]*)/') qvalues = ','.join(r.findall(s)) r = re.compile(r'(?ms)^ *([^\n]*)$') qvalues = ''.join(r.findall(qvalues)) qvalues = qvalues.replace(' ','') qvalues = qvalues.replace('&','') qvalues = [int(qx) for qx in re.split(',',qvalues)] print('nqparm = '+str(len(qvalues))) q = OrderedDict(parameter_zero) q.update(zip(qnames, qvalues)) path = Path(__file__).parent file = 'keppar_data.py' file = path / file # textwrap.fill(s,72,subsequent_indent=' '*4,fix_sentence_endings=True, break_on_hyphens=False) with open(file, 'w', encoding = 'utf-8') as f: f.write('from collections import OrderedDict\n') s = 'p = '+str(p) s = textwrap.fill(s, 72, subsequent_indent=' '*4,fix_sentence_endings=True, break_on_hyphens=False) f.write(s + '\n') s = 'q = '+str(q) s = textwrap.fill(s, 72, subsequent_indent=' '*4,fix_sentence_endings=True, break_on_hyphens=False) f.write(s + '\n') if __name__ == "__main__": make_keppar()