#!/usr/bin/env python
#coding: utf-8
#
# ratio 構成比を求める（Open usp Tukubai版）
# 
# designed by Nobuaki Tounaka
# written by Yoshio Katayama
#
# The MIT License
#
# Copyright (C) 2011 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "ratio key=<key> <file>"
_option = "ref=<ref> -- 参照キーの指定"
_option1 = "-<s> ------- 小数点以下の精度"
_option2 = "+<n>h ------ 先頭<n>行をヘッダーとみなす"
_version = "Fri Oct 21 11:26:06 JST 2011"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4/UTF-8)"
_keypat = r'(\d+|NF(-\d+)?)(([/@](\d+|NF(-\d+)?))*)$'
		# 1: 1st field
		# 3: 2nd and later fields with separator

import re
import os
import sys
import stat
import tempfile
from decimal import *

def round(x, n):
	return x.quantize(Decimal('.1') ** n)

def error(msg, *arg):
	print >> sys.stderr, 'Error[ratio] :', msg % arg
	sys.exit(1)

def usage():
	print >> sys.stderr, "Usage   :", _usage
	print >> sys.stderr, "Option  :", _option
	print >> sys.stderr, "        :", _option1
	print >> sys.stderr, "        :", _option2
	print >> sys.stderr, "Version :", _version
	print >> sys.stderr, "         ", _code
	sys.exit(1)

class FieldLine:
	def __init__(self, line, allow_z = False):
		self.__allow_zero = allow_z
		line = line.rstrip('\n')
		self.__fields = [ line ]
		self.__fields += [ x for x in line.split(' ') if x ]

	def size(self):
		return len(self.__fields) - 1

	def getFieldNum(self, key):
		if type(key) == type(0):
			return key
		if re.match(r'\d+$', key):
			key = int(key)
		elif key == 'NF':
			key = self.size()
		else:
			key = self.size() - int(key[3:])
			if key <= 0:
				error("NF-x の x が大きすぎます。")
		if key < 0:
			error("フィールド番号が負です。")
		if key == 0 and not self.__allow_zero:
			error("フィールド番号が０です。")
		if key > self.size():
			error("フィールド番号が大きすぎます。")
		return key

	def getField(self, s, e = None):
		s = self.getFieldNum(s)
		if e == None:
			e = s
		else:
			e = self.getFieldNum(e)
		if s <= e:
			return ' '.join(self.__fields[s : e + 1])
		else:
			t = self.__fields[e : s + 1]
			t.reverse()
			return ' '.join(t)

#
# 入力ファイルオープン
#
def open_file(n, mode = 'r'):
	if n >= len(sys.argv):
		file = sys.stdin
	elif sys.argv[n] == '-':
		file = sys.stdin
		del sys.argv[n]
	else:
		try:
			file = open(sys.argv[n], mode)
			del sys.argv[n]
		except:
			file = sys.stdin
	return file

#
# 通常ファイルの判定
#
def isfile(fd):
	try:
		return stat.S_ISREG(os.fstat(fd).st_mode)
	except:
		return False

#
# フィールド値の取得
#
def getval(line, n):
	if re.match(r'(\+|-)?(\d+\.?|\d*\.\d+)$', line.getField(n)):
		return Decimal(line.getField(n))
	else:
		error("数値変換できません。")

#
# 百分率の出力
#
def pr_percent(x, y, prec):
	if x != 0:
		x = round(100 * x / y, prec)
	print '%.*f' % (prec, x),

#
# key= / ref= の解析
#
def getkey(line, type, arg):
	k = re.match(type + '=' + _keypat, arg)
	if k == None:
		error("% 引数が正しくありません。", type);
	key = [ line.getFieldNum(k.group(1)) ]
	while k.group(3) != '':
		sep = k.group(3)[0]
		str = k.group(3)[1:]
		k = re.match(_keypat, str)
		n = line.getFieldNum(k.group(1))
		if sep == '@':
			key += [ n ]
		elif key[-1] <= n:
			key += range(key[-1] + 1, n + 1)
		else:
			key += range(key[-1] - 1, n - 1, -1)
	return key

#
# キー文字列の抽出
#
def keystr(line, key):
	return ' '.join(map(line.getField, key))

#
#メイン関数
#
if __name__ == '__main__':

        if len(sys.argv) < 2 \
         or sys.argv[1] == '--help' \
         or sys.argv[1] == '--version':
		usage()

	#
	# 丸めモードの設定
	#
	getcontext().rounding = ROUND_HALF_UP

	#
	# -<s>
	#
	if re.match(r'-\d+$', sys.argv[1]):
		prec = int(sys.argv[1][1:])
		del sys.argv[1]
		if len(sys.argv) < 2:
			usage()
	else:
		prec = 1

	#
	# 入力ファイルのオープン
	#
	file = open_file(len(sys.argv) - 1)
	if len(sys.argv) < 2:
		usage()
	if isfile(file.fileno()):
		tmp = None
	else:
		tmp = tempfile.mkstemp(prefix='ratio-')
		os.unlink(tmp[1])
		tmp = os.fdopen(tmp[0], 'w+')

	#
	# +<n>h
	#
	if re.match('\+\d*h$', sys.argv[1]):
		if sys.argv[1] == '+h':
			head = 1
		else:
			head = int(sys.argv[1][1:-1])
		del sys.argv[1]
		if len(sys.argv) < 2:
			usage()
		for i in range(head):
			line = file.readline()
			if tmp:
				tmp.write(line)
	else:
		head = 0

	#
	# １行入力
	#
	line = file.readline()
	if not line:
		sys.exit(0)
	if tmp:
		tmp.write(line)
	line = FieldLine(line)

	#
	# ref=
	#
	if sys.argv[1][0:4] == 'ref=':
		ref = getkey(line, 'ref', sys.argv[1])
		del sys.argv[1]
		if len(sys.argv) < 2:
			usage()
	else:
		ref = None

	#
	# key=
	#
	if sys.argv[1][0:4] == 'key=':
		key = getkey(line, 'key', sys.argv[1])
	else:
		usage()
	for x in zip(key, key[1:]):
		if x[0] >= x[1]:
			error("key 引数が正しくありません。");
	kmin = min(key)
	kmax = max(key)
	if line.size() < kmax:
		error("フィールドが足りません。")

	#
	# 初期化
	#
	sum = [ [ 1, [ 0 for i in range(kmax + 1) ] ] ]
	for i in range(kmin, kmax + 1):
		if i in key:
			sum[0][1][i] += getval(line, i)
	if ref:
		last_key = keystr(line, ref)

	#
	# 1st pass
	#
	for line in file:
		if tmp:
			tmp.write(line)
		line = FieldLine(line)
		if line.size() < kmax:
			error("フィールドが足りません。")
		if ref and last_key != keystr(line, ref):
			sum += [ [ 0, [ 0 for i in range(kmax + 1) ] ] ]
		sum[-1][0] += 1
		for i in range(kmin, kmax + 1):
			if i in key:
				sum[-1][1][i] += getval(line, i)
		if ref:
			last_key = keystr(line, ref)
	if tmp:
		file.close()
		file = tmp
	file.seek(0)

	#
	# 2nd pass
	#
	for i in range(head):
		line = FieldLine(file.readline())
		for j in range(1, line.size() + 1):
			print line.getField(j),
			if j in key:
				print "@",
		print

	for s in sum:
		for i in range(s[0]):
			line = FieldLine(file.readline())
			if line == '':
				error("予想外の EOF です。")
			for j in range(1, line.size() + 1):
				print line.getField(j),
				if j in key:
					pr_percent(getval(line, j), s[1][j], prec),
			print

	sys.exit(0)
