#Load time series in small chunks (to allow for real time analysis).
for ti in xrange(nsamp_down):
	t.display() #Just to calculate the remaining computation time.
	t.increase()

	lapse=ti*1./sampr #Lapse of time between samples (in sec).
	gulpi=int(1./fsamp) #Number of samples (of the full data) to extract.
	starti=gulpi*ti #Number of the sample to start.

	hctmat,vctmat=move(hc0mat,vc0mat,lapse)

	#Load data from each beam.
	for beami in xrange(numbeams):
		datafile=namedir+'BEAM_%.3i/' %int(beami+1) + utc + '.fil' #Name of the data file.	
		data=FilReader(datafile) #This is defined in class Filterbank, in file Filterbank.py.
		ts=data.makeTS(start=starti,gulp=gulpi,nsamps=gulpi) #Time series. 
		value=np.mean(ts) #Mean value of the data acquired with this beam in this lapse of time.
		beam_left=hcb_ini+beami*hcb_width #Left border of this beam.
		beam_right=hcb_ini+(beami+1)*hcb_width #Right border of this beam.
		beam_upper=vcb_ini #Upper border of this beam.
		beam_lower=vcb_ini-vcb_height #Lower border of this beam.

		#Find the indices of the initial sky matrix such that after this lapse of time would fall in the current beam.
		#indices=np.where((beam_left<=hctmat) & (hctmat<=beam_right) & (beam_lower<=vctmat) & (vctmat<=beam_upper))

		#image[indices]+=value		

	#print image

print
def main(sampr=samprdef,inputdir=inputdirdef,utc=utcdef,numbeams=numbeamsdef,maxnsamp=maxnsampdef):
	'''Main operation within the script.'''
	#Some parameters about the beam.
	hcres=4.4 #Angular resolution of the horizontal coordinate of the fan beam.
	vcres=2. #Angular resolution of the vertical coordinate of the fan beam.
	hclen=10. #Angular size (horizontal coordinate) of the sky matrix in degrees.
	vclen=10. #Angular size (vertical coordinate) of the sky matrix in degrees.
	hpix=100 #Number of pixels on the horizontal axis. WHAT WOULD BE THE CORRECT ONE?
	vpix=100 #Number of pixels on the vertical axis.

	#Load information about this observation (for example from the first beam).
	datafile=inputdir+'/'+utc+'/'+'BEAM_001/'+utc+'.fil'
	data=FilReader(datafile)
	ra=data.header['ra_rad'] #Right ascension in rad.
	dec=data.header['dec_rad'] #Declination in rad.
	tobs=data.header['tobs'] #Observing time in sec.
	nsamp_full=data.header['nsamples'] #Number of samples of original time series.

	#Derive some quantities that will be needed.
	nsamp_down=int(tobs*sampr) #Number of samples after downsampling.
	fsamp=nsamp_down*1./nsamp_full #Fraction of samples before and after downsampling.
	print 'Number of samples: %i ' %nsamp_down
	print 'Observing time: %.4e h' %(tobs*1./3600.)
	print
	#raw_input('Enter to proceed')

	#Define a matrix of initial sky position of objects in the sky, centered at the tracked object.
	hct=ra #The horizontal coordinate of the tracked object will be the RA.
	vct=dec #The vertical coordinate of the tracked object will be the DEC.
	hcmin=hct-hclen*0.5 #Left border of the sky matrix.
	hcmax=hct+hclen*0.5 #Right border of the sky matrix.
	vcmin=vct-vclen*0.5 #Bottom border of the sky matrix.
	vcmax=vct+vclen*0.5 #Top border of the sky matrix.
	hc0mat=np.tile(np.linspace(hcmin,hcmax,hpix),(vpix,1)) #Vector of borders of the horizontal pixels.
	vc0mat=np.tile(np.linspace(vcmin,vcmax,vpix)[::-1],(hpix,1)).T #Vector of borders of the vertical pixels. I want it to start from up to bottom.
	#If hc0vec and vc0vec are not of the same size, I could create a matrix with them, so that the 'where' condition below works.

	#Define a matrix that will contain the final image (in principle it can be of the size of the sky matrix).
	image=np.zeros((hpix,vpix))

	hcb_ini=hct-hcres*0.5 #Left border of the primary beam.
	vcb_ini=vct+vcres*0.5 #Upper border of the primary beam.
	hcb_width=hcres*1./numbeams #Width of each of the beams.
	vcb_height=vcres #Height of each of the beams.

	t=time_estimate(min(nsamp_down,maxnsamp)) #A class that prints estimated computation time.

	#Load time series in small chunks (to allow for real time analysis).
	for ti in xrange(min(nsamp_down,maxnsamp)):
		t.display() #Just to calculate the remaining computation time.
		t.increase()

		lapse=ti*1./sampr #Lapse of time between samples (in sec).
		gulpi=int(1./fsamp) #Number of samples (of the full data) to extract.
		starti=gulpi*ti #Number of the sample to start.

		hctmat,vctmat=move_oned(hc0mat,vc0mat,lapse)

		#Load data from each beam.
		for beami in xrange(numbeams):
			datafile=inputdir+'/'+utc+'/'+'BEAM_%.3i/' %int(beami+1) + utc + '.fil' #Name of the data file.	
			data=FilReader(datafile) #This is defined in class Filterbank, in file Filterbank.py.
			ts=data.makeTS(start=starti,gulp=gulpi,nsamps=gulpi) #Time series. 
			value=np.mean(ts) #Mean value of the data acquired with this beam in this lapse of time.
			beam_left=hcb_ini+beami*hcb_width #Left border of this beam.
			beam_right=hcb_ini+(beami+1)*hcb_width #Right border of this beam.
			beam_upper=vcb_ini #Upper border of this beam.
			beam_lower=vcb_ini-vcb_height #Lower border of this beam.

			#Find the indices of the initial sky matrix such that after this lapse of time would fall in the current beam.
			indices=np.where((beam_left<=hctmat) & (hctmat<=beam_right) & (beam_lower<=vctmat) & (vctmat<=beam_upper))

			image[indices]+=value

		#print image

	print

	py.ion()
	py.matshow(image,cmap='bone',extent=[hcb_ini,hcb_ini+hclen,vcb_ini-vclen,vcb_ini])
		#py.imshow(image,interpolation='nearest',origin='lower',aspect='auto',cmap='bone',extent=[hcb_ini,hcb_ini+hclen,vcb_ini-vclen,vcb_ini])
	py.xlabel('Horizontal coordinate')
	py.ylabel('Vertical coordinate')
	#py.imshow(image.T,origin='lower')
	raw_input('Next time sample')