#!/usr/bin/env python

from glob import glob
from string import find, split, strip, upper, lower
import sys
from os import popen, system

fn = '10200001.FITS'
mon = 'apr03'

def getfiles(fs):
	list = popen('ls -1s /home/parallax2/big/incoming').readlines()
	F = []
	S = 0.0
	for l in list[1:]:
		s = split(strip(l))
		v = float(s[0])/1024.
		for i in fs:
			if i == s[1][0:4]:
				S = S + v;
				F.append(s[1])
	k = len(F)
	print 'Total data volume : ', S, 'MB'
	if S < 500.0:
		N = 1
	else:
		if S < 900:
			N = 2
		else:
			N = 3
	print 'it looks like N =', N
	d = k/N
	if k != d*N:
		print '%d files and %d correlations.' % (k, N)
		sys.exit(0)
	out = []
	q = 0
	for i in range(N):
		out.append([])
		for e in range(d):
			out[i].append(F[q])
			q = q + 1
	return out

def md(base, dr, psr, lstday, epoch):
	cmd = 'mkdir '+dr
	print 'Executing ', cmd
	system(cmd)
	cmd = 'chmod a+w '+dr
	print 'Executing ', cmd
	system(cmd)
	cmd = 'cp '+base[:-5]+'*.key '+dr
	print 'Executing ', cmd
	system(cmd)
	cmd = 'cp /home/parallax/vlba/big/'+psr+'/'+psr+'.x ' +  dr
	print 'Executing ', cmd
	system(cmd)
	xfile = dr+'/'+psr+'.x'
	f = open(xfile, 'a')
	f.write('\n')
	f.write(lstday+'\n')
	f.close()
	cmd = 'mv ' + xfile + ' ' + xfile + '.' + epoch
	print 'Executing ', cmd
	system(cmd)
	
	

def mv(f, d):
	cmd = 'mv '+f+' '+d
	print 'Executing ', cmd
	system(cmd)
		
def getepoch(exp):
	cmd = 'wget -q -O - http://www.astro.cornell.edu/~shami/psrvlb/observe/ | grep ' + lower(exp) + '.key'
	print 'Executing ', cmd
	out = popen(cmd).readlines()
	if len(out) < 1:
		print 'problem with wgetting'
		sys.exit(0)
	line = out[-1]
	p = find(line, 'epoch')
	if p < 1:
		print 'problem with getepoch'
		sys.exit(0)
	return line[p+5]
		

if len(sys.argv) != 3:
	print 'Usage : %s <filename> <month>' % sys.argv[0]
	sys.exit(0)

fn = sys.argv[1]
mon = sys.argv[2]

A = glob('/home/aspen6/astronomy/' + mon + '/bc120*/jobs/'+fn[0:5]+'*')

if len(A) == 1:
	j = find(A[0], 'jobs')
	filebase = A[0][:j]+'jobs/'
	print filebase
	p = find(A[0], 'bc12')
	exp = A[0][p:]
	p = find(exp, '/')
	exp = upper(exp[:p])
	print exp
	fx = glob(filebase+'*.fx')
	j = j + 8
	print len(fx), 'jobs total'
	fs = []
	for f in fx:
		fs.append(f[j:(j+4)])
	print fs

	epo = getepoch(exp)
	
	print 'Looks like epoch ', epo
	
	files = getfiles(fs)

	G = -1
	
	for h in range(len(files)):
		for w in files[h]:
			ko = filebase+'job'+w[:4]+'.fx'
			gr = popen('grep Gate '+ko).readlines()
			if len(gr) > 0:
				print ko ,'is gated'
				G = h

	print 'Gated series', G

	if G == -1:
		print 'Cant find gated series!'
		sys.exit(0)

	N = len(files)
	
	lstday = strip(popen('grep day '+filebase[:-5]+'*.key').readlines()[0])
	q = find(lstday, '!')
	lstday = 'lst'+lstday[:q]
	print lstday

	psr = strip(popen('grep calcode '+filebase[:-5]+'*.key').readlines()[0])
	q = find(psr, "'")
	psr = psr[(q+1):]
	q = find(psr, "'")
	psr = psr[:q]
	print psr
	
	md(filebase, '/home/parallax2/big/'+exp+'psr', psr, lstday, epo)
	if N > 1:
		md(filebase, '/home/parallax2/big/'+exp+'ib1', psr, lstday, epo)
	if N > 2:
		md(filebase, '/home/parallax2/big/'+exp+'ib2', psr, lstday, epo)
	
	z = 0
	for h in range(len(files)):
		if h == G:
			outdir = '/home/parallax2/big/'+exp+'psr'
		else:
			z = z+1
			outdir = '/home/parallax2/big/'+exp+('ib%d'%z)
		for w in files[h]:
			mv('/home/parallax2/big/incoming/'+w, outdir)

	print 'Paste these where appropriate:'
	if N > 1:
		print 'setenv '+exp+'IB1 '+'/home/parallax2/big/'+exp+'ib1'
	if N > 2:
		print 'setenv '+exp+'IB2 '+'/home/parallax2/big/'+exp+'ib2'
	print 'setenv '+exp+'PSR '+'/home/parallax2/big/'+exp+'psr'

	print ''
	if N > 1:
		print 'autoaips '+exp+'IB1 ib1 8'
	if N > 2:
		print 'autoaips '+exp+'IB2 ib2 8'
	print 'autoaips '+exp+'PSR psr 8'


else:
	print fn, 'not found in month', mon
