#! /usr/bin/python from __future__ import division from __future__ import print_function from __future__ import with_statement import os.path def make_keppar(path = os.path.join('~','kepler','source')): """Create the KEPLER parameter file form the KEPLER source code.""" import os.path import re path = os.path.expanduser(path) path = os.path.expandvars(path) file = 'kepdat.f' file = os.path.join(path,file) with open(file) as f: s = f.read() r = re.compile(r'^ *data *[(]nameparm[(]ii[)],ii=[0-9]*,(?:[0-9]*|nparm)[)] /([^/]*)/',re.MULTILINE + re.DOTALL) pnames = ''.join(r.findall(s)) r = re.compile(r"'([^']*)'",re.MULTILINE + re.DOTALL) pnames = r.findall(pnames) r = re.compile(r'^ *data *[(]iptype[(]ii[)],ii=[0-9]*,(?:[0-9]*|nparm)[)] /([^/]*)/',re.MULTILINE + re.DOTALL) pvalues = ','.join(r.findall(s)) r = re.compile(r'^.{7}([^\n]*)$',re.MULTILINE + re.DOTALL) pvalues = ''.join(r.findall(pvalues)) pvalues = pvalues.replace(' ','') pvalues = [int(px) for px in re.split(',',pvalues)] print('nparm = '+str(len(pvalues))) from collections import OrderedDict p = OrderedDict({'':-1}) p.update(zip(pnames,pvalues)) r = re.compile(r'^ *data *[(]nameqprm[(]ii[)],ii=[0-9]*,(?:[0-9]*|nqparm)[)] /([^/]*)/',re.MULTILINE + re.DOTALL) qnames = ''.join(r.findall(s)) r = re.compile(r"'([^']*)'",re.MULTILINE + re.DOTALL) qnames = r.findall(qnames) r = re.compile(r'^ *data *[(]iqtype[(]ii[)],ii=[0-9]*,(?:[0-9]*|nqparm)[)] /([^/]*)/',re.MULTILINE + re.DOTALL) qvalues = ','.join(r.findall(s)) r = re.compile(r'^.{7}([^\n]*)$',re.MULTILINE + re.DOTALL) qvalues = ''.join(r.findall(qvalues)) qvalues = qvalues.replace(' ','') qvalues = [int(qx) for qx in re.split(',',qvalues)] print('nqparm = '+str(len(qvalues))) q = OrderedDict({'':-1}) q.update(zip(qnames,qvalues)) path = os.path.join('~','python','source') path = os.path.expanduser(path) path = os.path.expandvars(path) file = 'keppar_data.py' file = os.path.join(path,file) with open(file,'wb') as f: f.write('from collections import OrderedDict\n') f.write('p = '+str(p)+'\n') f.write('q = '+str(q)+'\n') if __name__ == "__main__": make_keppar()