#!/usr/bin/env python

import ortho_poly
import sys
import die
import avio


"""Do a batch reconstruction of orthogonal polynomial fits,
based on input which is contains coefficients
in avio format, with names specified by PATTERN.
Output just one value.
"""


try:
	enumerate([])
except NameError:
	from g_enumerate import enumerate

PATTERN = 'i%02d'
N = 100
OPNAME = 'Legendre'


if __name__ == '__main__':
	arglist = sys.argv[1:]
	k = None
	while arglist and arglist[0].startswith('-'):
		arg = arglist.pop(0)
		if arg == '-name':
			OPNAME = arglist.pop(0)
		elif arg == '-n':
			N = int(arglist.pop(0))
		elif arg == '-k':
			k = int(arglist.pop(0))
		elif arg == '--':
			break
		else:
			die.die('Bad arg: %s' % arg)
	if k is None:
		k = (N-1)//2

	out = []
	op = ortho_poly.F(OPNAME, n=N)
	for x in sys.stdin:
		if x.startswith('#'):
			continue
		x = x.strip()
		if x == '':
			continue
		a = avio.parse(x)
		i = 0
		c = []
		while PATTERN%i in a:
			c.append( float(a[PATTERN%i]) )
			i += 1
		print '%g' % ( op.expand(c)[k] )
