obsolete (use mergeDumps.sh)

import gzip, os, sys

srcPath = "/var/svn/space_for_gavo/dumps"

class Error(Exception):
	pass


class Dumpfile(object):
	def __init__(self, inName):
		self.input = gzip.open(os.path.join(srcPath, inName))
		self._getLine()
	
	def _getLine(self):
		self.buffer = self.input.readline()
		if self.buffer:
			self.id = int(self.buffer[:23])
		else:
			self.id = None
	
	def read(self):
		val = self.buffer
		self._getLine()
		return val


class Merger(object):
	def __init__(self):
		self.usno = Dumpfile("obsposusnob.txt.gz")
		self.mass = Dumpfile("obspos2mass.txt.gz")
		self.ppmx = Dumpfile("obsposppmx.txt.gz")
		self.drop2MASS, self.dropPPMX = 0, 0
	
	def __iter__(self):
		count = 0
		while True:
			curId = self.usno.id
			if not curId:
				break
			while curId==self.usno.id:
				yield self.usno.read()
			while self.mass.id and self.mass.id<curId:
				self.drop2MASS += 1
				self.mass.read()
			while curId==self.mass.id:
				yield self.mass.read()
			while self.ppmx.id and self.ppmx.id<curId:
				self.dropPPMX += 1
				self.ppmx.read()
			while curId==self.ppmx.id:
				yield self.ppmx.read()
			count += 1
		self.total = count
	
	def getReport(self):
		return "%d total, %d dropped from 2MASS, %s dropped from PPMX"%(
			self.total, self.drop2MASS, self.dropPPMX)


if __name__=="__main__":
	m = Merger()
	out = gzip.open(os.path.join(srcPath, "rawjoin.txt.gz"), "w")
	for l in m:
		out.write(l)
	sys.stderr.write(m.getReport()+"\n")
