Ejemplo n.º 1
0
def load_and_save_infodata(inputdir, utc, sampr, savenpy, loadnpy):
	'''Load infodata (either .npy or .fil) and decide if "infodata.npy" should be saved. Return infodata. Here, "infodata" contains some parameters about the observations.'''

	#Load and save information about this observation.
	npydir='../data/input/'+utc+'/'
	samprname=('%.3e' %sampr).replace('+','').replace('-','').replace('.','p')
	samprdir=npydir+'sampr'+samprname+'/'
	npydatafile=samprdir+'INFO'

	if loadnpy and os.path.isfile(npydatafile+'.npy'):
		infodata=np.load(npydatafile+'.npy')[()]
	else:
		obsfilename=inputdir+utc+'/obs.info'
		obsfile=open(obsfilename,'r')
		for line in obsfile:
			if line[0:6]=='CONFIG':
				maptypeinfo=str(line).split(' ')[-1][:-1]
				obsfile.close()
				if maptypeinfo=='FAN_BEAM':
					print 'This is a scan-drift observation.'
					print
					maptype='drift'
				elif maptypeinfo=='TIED_ARRAY_FAN_BEAM':
					print 'This is a tracking observation.'
					print
					maptype='track'
				else:
					print 'Type of observation not recognised! Missing "obs.info" file?'
					print
					exit()
				break

		fildatafile=inputdir+utc+'/'+'BEAM_001/'+utc+'.fil'
		fildata=FilReader(fildatafile)
		fildata.setNthreads(1)
		infodata={'ra_rad':fildata.header['ra_rad'], 'dec_rad':fildata.header['dec_rad'], 'tobs':fildata.header['tobs'], 'nsamples':fildata.header['nsamples'], 'tsamp':fildata.header['tsamp'], 'maptype':maptype}
		if savenpy:
			np.save(npydatafile,infodata)

	return infodata
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def test_threads(self, filfile):
     myFil = FilReader(filfile)
     myFil.setNthreads()
     assert lib.getNthreads == 4
     myFil.setNthreads(nthreads=2)
     assert lib.getNthreads == 2