def main(sampr=samprdef, inputdir=inputdirdef, utc=utcdef, numbeams=numbeamsdef, maxnsamp=maxnsampdef, savenpy=savenpydef, loadnpy=loadnpydef, saveonly=saveonlydef, liveplot=liveplotdef):
	'''Main operation within the script.'''

	#Decide if .npy input data should be loaded/saved, and create output dir for .npy data (if desired) and plots.
	savenpy, samprdir, outputdir=FN.load_and_save_check(inputdir, utc, sampr, numbeams, savenpy, saveonly, maxnsamp)

	#Obtain some parameters about the observation ("infodata", either as .npy or .fil file) and save it.
	infodata=FN.load_and_save_infodata(inputdir, utc, sampr, savenpy, loadnpy)
	ra, dec, tobs, nsamp_full, tsamp, maptype=infodata['ra_rad'], infodata['dec_rad'], infodata['tobs'], infodata['nsamples'], infodata['tsamp'], infodata['maptype'] #A bit redundant, since nsamp_full=tobs/tsamp.+++
	old_sampr=1./tsamp #Old number of samples per second.
	ra=ra*180./np.pi
	dec=dec*180./np.pi

	lst_hms=commands.getstatusoutput('mopsr_getlst '+utc)[1].split(' ')[1].split(':') #Local sidereal time in HH:MM:SS.SSS.
	lst=(int(lst_hms[0])*3600.+int(lst_hms[1])*60.+float(lst_hms[2]))*360./sidday
	if maptype=='drift':
		#In the drift scan mode, the RA from the heather is always zero, so I calculate it from the LST.
		ra=lst

	#Derive or define some quantities that will be needed.
	global hpix #This line is needed because to avoid an UnboundLocalError.
	if hpix==0:
		hpix=tobs
	hsize=hres*1./numbeams #Horizontal angular size of each beam, in degrees.
	vsize=vres #Vertical angular size of each beam, in degrees.
	frac_sampr=int(round(old_sampr*1./sampr)) #Number of old samples per new sample.
	nsamp_down=int(round(sampr*tobs))

	#tvec=np.arange(0., tobs, 1./sampr) #Vector of time (in seconds).
	#tvec=np.linspace(0.,tobs, nsamp_down)
	tvec=np.linspace(0.,tobs, nsamp_down-1)#THE -1 IS BECAUSE DATA WILL LOSE ONE SAMPLE!!+++USE THIS FOR DRIFT
	#tvec=np.linspace(0.,tobs, nsamp_down-2)#THE -2 IS BECAUSE DATA WILL LOSE TWO SAMPLES!! WHY?+++ USE THIS FOR TRACK

	if maxnsamp!=True:
		tvec=tvec[0:maxnsamp] #Vector of time (in seconds).

	#Define the size of the final map.
	ra_max=ra+hres*0.5 #Maximum RAS observed (initial RAS plus half of the fan beam size).
	if maptype=='drift':
		ra_min=ra-hres*0.5-(tobs*360./sidday) #Minimum RAS observed (left border of the initial fan beam minus the hour angle described after the observation). +++IS THIS CONVERSION CORRECT?
	elif maptype=='track':
		ra_min=ra-hres*0.5
	map_ra=np.linspace(ra_min, ra_max, hpix) #Vector of RAS.
	dec_max=dec+vres*0.5 #I impose the same maximum size as the horizontal coordinate.
	dec_min=dec-vres*0.5
	ra_vec=np.linspace(ra_min, ra_max, hpix) #Vector of RAS.
	dec_vec=np.linspace(dec_min, dec_max, vpix) #Vector of DEC.
	mapi=np.zeros((vpix, hpix)) #Matrix of values of the map.
	f_t_map=np.zeros((numbeams, len(tvec))) #Matrix of fan beam versus time (raw data from the beams).
	ras_cor=(ra_vec%360.) #Restrict RAS to the interval [0,360). This will be used for plotting and saving.

	ras_mat=np.tile(ras_cor, (len(tvec), 1)).T
	dec_mat=np.tile(dec_vec, (len(tvec), 1)).T
	
	#Load data beam by beam.
	if not loadnpy:
		print 'Reading filterbank files (it will take longer than reading numpy files)...'
		print
	else:
		print 'Reading numpy files if already existing...'
		print
	if savenpy:
		print 'The input data will be saved as numpy files.'
		print

	t=time_estimate(numbeams) #A class that prints estimated computation time.
	for beami in xrange(numbeams):
		t.display() #Shows the remaining computation time.
		t.increase() #Needed to calculate the remaining computation time.

		#Load/save data.
		npydatafile=samprdir+'BEAM_%.3i' %int(beami+1) #Name of the .npy file (to be either loaded or saved).
		if loadnpy and os.path.isfile(npydatafile+'.npy'): #Load .npy data.
			if saveonly: #If the .npy file already exists, move on to the next file.
				continue
			data=np.load(npydatafile+'.npy')
		else: #Load .fil data.
			datafile=inputdir+utc+'/'+'BEAM_%.3i/' %int(beami+1) + utc + '.fil' #Name of the data file.
			data_raw=FilReader(datafile)
			data=(data_raw.collapse()/data_raw.header.nchans).downsample(frac_sampr)*1./frac_sampr
		if savenpy: #Save input data as numpy files, if necessary and if desired.
			np.save(npydatafile,data)
		if saveonly: #Simply load .fil data and save .npy data.
			continue

		if maxnsamp!=True:
			data=data[0:maxnsamp]

		f_t_map[beami,:]=data 	#Stores raw fan beam versus time data.

		ras_ini=ra-hres*0.5+hsize*beami #Initial RAS of this particular beam. It will be the RAS of the left-most border of the fan beam.

	'''
	print np.shape(f_t_map)
	mapi=f_t_map.copy()
	lefti=np.array([])
	righti=np.zeros(len(tvec))
	newmapi=np.zeros((numbeams, len(tvec)+len(righti)))
	shifti=1
	for fi in xrange(numbeams):
		lefti=np.zeros(fi)
		righti=np.zeros()
		newmapi[fi]=np.hstack((lefti,mapi[fi],righti))
		lefti=np.concatentae
	'''
	return tvec, f_t_map
	

	'''
def main(maptype=maptypedef, sampr=samprdef, inputdir=inputdirdef, utc=utcdef, numbeams=numbeamsdef, maxnsamp=maxnsampdef, savenpy=savenpydef, loadnpy=loadnpydef, saveonly=saveonlydef, liveplot=liveplotdef):
	'''Main operation within the script.'''

	#Decide if .npy input data should be loaded/saved, and create output dir for .npy data (if desired) and plots.
	savenpy, samprdir, outputdir=FN.load_and_save_check(inputdir, utc, sampr, numbeams, savenpy, saveonly, maxnsamp)

	#Obtain some parameters about the observation ("infodata", either as .npy or .fil file) and save it.
	infodata=FN.load_and_save_infodata(inputdir, utc, sampr, savenpy, loadnpy)
	ra, dec, tobs, nsamp_full, tsamp=infodata['ra_rad'], infodata['dec_rad'], infodata['tobs'], infodata['nsamples'], infodata['tsamp'] #A bit redundant, since nsamp_full=tobs/tsamp.+++
	old_sampr=1./tsamp #Old number of samples per second.
	ra=ra*180./np.pi
	dec=dec*180./np.pi
	lst_hms=commands.getstatusoutput('mopsr_getlst '+utc)[1].split(' ')[1].split(':') #Local sidereal time in HH:MM:SS.SSS.
	lst=(int(lst_hms[0])*3600.+int(lst_hms[1])*60.+float(lst_hms[2]))*360./sidday #+++TRIVIAL QUESTION, IS THIS CONVERSION CORRECT?
	if maptype=='drift':
		#In the drift scan mode, the RA from the heather is always zero, so I calculate it from the LST.
		ra=lst

	#Derive or define some quantities that will be needed.
	global hpix #This line is needed because to avoid an UnboundLocalError.
	if hpix==0:
		hpix=tobs
	hsize=hres*1./numbeams #Horizontal angular size of each beam, in degrees.
	vsize=vres #Vertical angular size of each beam, in degrees.
	frac_sampr=int(round(old_sampr*1./sampr)) #Number of old samples per new sample.
	nsamp_down=int(round(sampr*tobs))

	#tvec=np.arange(0., tobs, 1./sampr) #Vector of time (in seconds).
	#tvec=np.linspace(0.,tobs, nsamp_down)
	tvec=np.linspace(0.,tobs, nsamp_down-1)#THE -1 IS BECAUSE DATA WILL LOSE ONE SAMPLE!!+++USE THIS FOR DRIFT
	#tvec=np.linspace(0.,tobs, nsamp_down-2)#THE -2 IS BECAUSE DATA WILL LOSE TWO SAMPLES!! WHY?+++ USE THIS FOR TRACK

	#NOT YET IMPLEMENTED.-----------
	if maxnsamp!=True:
		tvec=tvec[0:maxnsamp] #Vector of time (in seconds).
	#NOT YET IMPLEMENTED.-----------

	#Define the size of the final map.
	ra_max=ra+hres*0.5 #Maximum RAS observed (initial RAS plus half of the fan beam size).
	if maptype=='drift':
		ra_min=ra-hres*0.5-(tobs*360./sidday) #Minimum RAS observed (left border of the initial fan beam minus the hour angle described after the observation). +++IS THIS CONVERSION CORRECT?
	elif maptype=='track':
		ra_min=ra-hres*0.5
	map_ra=np.linspace(ra_min, ra_max, hpix) #Vector of RAS.
	dec_max=dec+vres*0.5 #I impose the same maximum size as the horizontal coordinate.
	dec_min=dec-vres*0.5
	ra_vec=np.linspace(ra_min, ra_max, hpix) #Vector of RAS.
	dec_vec=np.linspace(dec_min, dec_max, vpix) #Vector of DEC.
	mapi=np.zeros((vpix, hpix)) #Matrix of values of the map.
	f_t_map=np.zeros((numbeams, len(tvec))) #Matrix of fan beam versus time (raw data from the beams).
	ras_cor=(ra_vec%360.) #Restrict RAS to the interval [0,360). This will be used for plotting and saving.

	ras_mat=np.tile(ras_cor, (len(tvec), 1)).T
	dec_mat=np.tile(dec_vec, (len(tvec), 1)).T
	
	#Load data beam by beam.
	if not loadnpy:
		print 'Reading filterbank files (it will take longer than reading numpy files)...'
		print
	else:
		print 'Reading numpy files if already existing...'
		print
	if savenpy:
		print 'The input data will be saved as numpy files.'
		print

	t=time_estimate(numbeams) #A class that prints estimated computation time.
	for beami in xrange(numbeams):
		t.display() #Shows the remaining computation time.
		t.increase() #Needed to calculate the remaining computation time.

		#Load/save data.
		npydatafile=samprdir+'BEAM_%.3i' %int(beami+1) #Name of the .npy file (to be either loaded or saved).
		if loadnpy and os.path.isfile(npydatafile+'.npy'): #Load .npy data.
			if saveonly: #If the .npy file already exists, move on to the next file.
				continue
			data=np.load(npydatafile+'.npy')
		else: #Load .fil data.
			datafile=inputdir+utc+'/'+'BEAM_%.3i/' %int(beami+1) + utc + '.fil' #Name of the data file.
			data_raw=FilReader(datafile)
			data=(data_raw.collapse()/data_raw.header.nchans).downsample(frac_sampr)*1./frac_sampr
		if savenpy: #Save input data as numpy files, if necessary and if desired.
			np.save(npydatafile,data)
		if saveonly: #Simply load .fil data and save .npy data.
			continue

		if maxnsamp!=True:
			data=data[0:maxnsamp]

		f_t_map[beami,:]=data 	#Stores raw fan beam versus time data.

		ras_ini=ra-hres*0.5+hsize*beami #Initial RAS of this particular beam. It will be the RAS of the left-most border of the fan beam.

		for dec_i in xrange(len(dec_vec)):
			dec_ini=dec_vec[dec_i] #Initial DEC of this particular beam.
			print dec_ini
			print ras_ini
			print
			if maptype=='drift':
				rvec, dvec=FN.move_drift(ras_ini, dec_ini, tvec) #RAS, DEC of this beam over time.
			elif maptype=='track':
				rvec, dvec=FN.move_track(lst, Mol_lat, ras_ini, dec_ini, tvec) #RAS, DEC of this beam over time.

			rvec_cor=rvec%360.

			rvec_cor=rvec###################
			print rvec
			print dvec

			RA_mat=np.tile(rvec_cor, (hpix, 1))
			DEC_mat=np.tile(dvec, (vpix, 1))

			ra_indices=np.argmin(abs(RA_mat-ras_mat), axis=0)
			dec_indices=np.argmin(abs((DEC_mat+90.)-(dec_mat+90.)), axis=0) #I add 90 to assure that DEC is positive, in order to avoid negative DEC to be identified with positive DEC and viceversa.

			py.ion()
			py.plot(ras_ini, dec_ini, 'o')
			py.plot(rvec, dvec,'.')
			raw_input('next')

			#sorting=rvec.argsort() #In order to use interp, the vectors must be increasing.
			#mapi[dec_i, :]+=np.interp(ra_vec, rvec[sorting], data[sorting])
			mapi[dec_indices,ra_indices]+=data

		if liveplot:
			if beami==numbeams or (beami+1)%plotevery==0:
				twod_live(ras_cor, dec_vec, tvec, mapi, beami+1, f_t_map) #Plot maps as they are produced.
				
	if not saveonly:
		print 'Saving numpy output data.'
		print
		#RAS,DEC=np.meshgrid(ras_cor,dec_vec)
		#hammer_x, hammer_y=hammer(ras_cor, dec_vec) #Hammer projected x, y coordinates.
		#hammer_x, hammer_y=hammer(RAS, DEC)
		#datadict={'RAS':RAS, 'DEC':DEC, 'MAP': mapi, 'Hammer_x':hammer_x, 'Hammer_y':hammer_y}
		datadict={'RAS':ras_cor, 'DEC':dec_vec, 'MAP': mapi}
		outputfile=samprdir.replace('input','output')[:-1]
		np.save(outputfile,datadict) #Save final map vector as numpy file.

		#Save 2D output plot.
		print 'Saving and preparing a 2D plot.'
		print
		outputplotfile=samprdir.replace('data/input','plots/twod')[:-1]
		inputplotfile=outputfile+'.npy' #The input file to produce the plots is the numpy file that was just created.
		twod_save(ras_cor, dec_vec, mapi, tvec, f_t_map, outputplotfile)
def get_ftmap(sampr=samprdef, inputdir=inputdirdef, utc=utcdef, first_beam=first_beam_def, last_beam=last_beam_def, first_tsamp=first_tsamp_def, last_tsamp=last_tsamp_def, savenpy=savenpydef, loadnpy=loadnpydef, saveonly=saveonlydef, liveplot=liveplotdef):
	'''Main operation within the script.'''

	#Decide if .npy input data should be loaded/saved, and create output dir for .npy data (if desired) and plots.
	numbeams=last_beam-first_beam+1
	beamsvec=np.arange(first_beam, last_beam+1)
	savenpy, samprdir, outputdir=load_and_save_check(inputdir, utc, sampr, savenpy, saveonly, first_tsamp, last_tsamp)

	#Obtain some parameters about the observation ("infodata", either as .npy or .fil file) and save it.
	infodata=load_and_save_infodata(inputdir, utc, sampr, savenpy, loadnpy)
	ra, dec, tobs, nsamp_full, tsamp, maptype=infodata['ra_rad'], infodata['dec_rad'], infodata['tobs'], infodata['nsamples'], infodata['tsamp'], infodata['maptype'] #A bit redundant, since nsamp_full=tobs/tsamp.+++
	old_sampr=1./tsamp #Old number of samples per second.
	ra=ra*180./np.pi
	dec=dec*180./np.pi
	lst_hms=commands.getstatusoutput('mopsr_getlst '+utc)[1].split(' ')[1].split(':') #Local sidereal time in HH:MM:SS.SSS.
	lst=(int(lst_hms[0])*3600.+int(lst_hms[1])*60.+float(lst_hms[2]))*360./sidday
	if maptype=='drift':
		#In the drift scan mode, the RA from the heather is always zero, so I calculate it from the LST.
		ra=lst

	#Derive or define some quantities that will be needed.
	frac_sampr=int(round(old_sampr*1./sampr)) #Number of old samples per new sample.
	nsamp_down=int(round(sampr*tobs)) #This is the number of samples that the data should have (if no samples were lost after downsampling).
	#tvec=np.arange(0., tobs, 1./sampr) #Vector of time (in seconds).
	#tvec=np.linspace(0.,tobs, nsamp_down)

	#Load data beam by beam.
	if not loadnpy:
		print 'Reading filterbank files (it will take longer than reading numpy files)...'
		print
	else:
		print 'Reading numpy files if already existing...'
		print
	if savenpy:
		print 'The input data will be saved as numpy files.'
		print

	t=time_estimate(numbeams) #A class that prints estimated computation time.
	for beami in beamsvec:
		t.display() #Shows the remaining computation time.
		t.increase() #Needed to calculate the remaining computation time.

		#Load/save data.
		npydatafile=samprdir+'BEAM_%.3i' %int(beami) #Name of the .npy file (to be either loaded or saved).
		if loadnpy and os.path.isfile(npydatafile+'.npy'): #Load .npy data.
			if saveonly: #If the .npy file already exists, move on to the next file.
				continue
			data=np.load(npydatafile+'.npy')
		else: #Load .fil data.
			datafile=inputdir+utc+'/'+'BEAM_%.3i/' %int(beami) + utc + '.fil' #Name of the data file.
			data_raw=FilReader(datafile)
			data_raw.setNthreads(1)
			data=(data_raw.collapse()/data_raw.header.nchans).downsample(frac_sampr)*1./frac_sampr
		if savenpy: #Save input data as numpy files, if necessary and if desired.
			np.save(npydatafile,data)
		if saveonly: #Simply load .fil data and save .npy data.
			continue

		if beami==beamsvec[0]:
			#if abs(len(data)-nsamp_down)>2:
			#	print 'Too many samples are missing when downsampling! Why?'
			#	print
			#	exit()
			tvec=np.linspace(0., tobs, nsamp_down)[0:len(data)]
			ftmap=np.zeros((totalnumbeams, len(tvec))) #Matrix of fan beam versus time (raw data from the beams).
			if first_tsamp>1 and last_tsamp==True:
				tvec=tvec[first_tsamp-1:]
				ftmap=ftmap[:,first_tsamp-1:]
			elif first_tsamp==1 and last_tsamp!=True:
				tvec=tvec[:(last_tsamp-1)]
				ftmap=ftmap[:,:(last_tsamp-1)]
			elif first_tsamp==1 and last_tsamp==True:
				pass
			else:
				tvec=tvec[(first_tsamp-1):(last_tsamp-1)]
				ftmap=ftmap[:,(first_tsamp-1):(last_tsamp-1)]

		if first_tsamp>1 and last_tsamp==True:
			data=data[first_tsamp-1:]
		elif first_tsamp==1 and last_tsamp!=True:
			data=data[:(last_tsamp-1)]
		elif first_tsamp==1 and last_tsamp==True:
			pass
		else:
			data=data[(first_tsamp-1):(last_tsamp-1)]

		ftmap[beami-1,:]=data 	#Stores raw fan beam versus time data.

	return tvec, ftmap, dec
Beispiel #4
0
 def test_collapse(self, filfile):
     myFil = FilReader(filfile)
     myTim = myFil.collapse()
     np.testing.assert_equal(myTim.dtype, np.float32)
     np.testing.assert_equal(myTim.size, myFil.header.nsamples)
     np.testing.assert_allclose(myTim.mean(), 104.7, atol=0.1)