def __init__(self, antennas): self.antenna_list = antennas self.n_antennas = len(antennas) self.baselines = lsqrs.triangle_l(self.antenna_list) self.e_baselines = lsqrs.triangle_l(range(self.n_antennas)) self.ant_ind_map = dict(zip(self.antenna_list, range(self.n_antennas))) self.ant_inv_map = utils.invert_map(self.ant_ind_map)
def all_cals_to_ref(msname, polind, pol_id, timeq, solint=300): swids = utils.get_spectral_windows(msname) sm = utils.get_antenna_map(msname, 'NAME') antennas2 = utils.get_antennas(msname, timeq) ism = utils.invert_map(sm) ref_antenna = 0 # FIXME # cal_table_names = [] for swid in range(1): anffd = ffd.FFData.make_FFD(msname, antennas2, swid, polind, pol_id, timeq, solint=solint) anffd.fft(32) # leave it fixed. all_phis = [] all_dels = [] stl = [] for i, s in enumerate(antennas2): stl.append(ism[s]) if s == ref_antenna: all_phis.append(0) all_dels.append(0) continue d = anffd.data[ref_antenna, i] args=(anffd.tgrid0, anffd.fgrid0, d) p_est = anffd.get_fringe_peak(ref_antenna, i) phi, delay, rate = scipy.optimize.leastsq(bl_fun, p_est, args=args, full_output=0)[0] all_phis.append(-180*phi/np.pi) all_dels.append(-delay*1.0e9) antennas = ",".join(stl) ph_name = 'phases_{}.G'.format(swid) del_name = 'dels_{}.G'.format(swid) #gencal(infile='afile', vis=msname, caltable=ph_name, caltype='ph', # spw='{}'.format(swid), antenna=antennas, pol='L', # parameter = all_phis) #gencal(infile='afile', vis=msname, caltable=del_name, caltype='sbd', # spw='{}'.format(swid), antenna=antennas, pol='L', # parameter = all_dels) #cal_table_names.append(ph_name) #cal_table_names.append(del_name) return all_phis, all_dels
phases.append(phs) rates.append(rs) all_delays.append(delays) all_phases.append(phases) all_rates.append(rates) print "#\n#{}, IF={}\n#".format(timeq, swid) utils.print_res(station_map, stations2, phases, delays, rates) all_delays = np.array(all_delays) all_rates = np.array(all_rates) all_phases = np.array(all_phases) sm = utils.get_station_map(msname, 'NAME') ism = utils.invert_map(sm) stationt = ",".join([ism[s] for s in stations2]) spwt = ",".join(str(i) for i in swids) freqs=utils.get_reference_freqs(msname) l_delay = utils.flattenificate(-1e9*all_delays) l_phase = utils.flattenificate(-1*180/np.pi*all_phases) if False: gencal(vis=msname, caltable='huh.G', caltype='sbd', spw=spwt, antenna=stationt, pol='R,L', parameter = l_delay) gencal(vis=msname, caltable='huh_ph.G', caltype='ph', spw=spwt, antenna=stationt, pol='R,L', parameter = l_phase)
def __init__(self, msname, fj_name, ref_antenna_name=None, scans=None, threshold=None, snr_threshold=None, antennas=None, spectral_windows=None, solint=None, solmin=0, solsub=1, dofloat=True): self.msname = msname if scans is None: raise UnhandledCase("No scans selected!") else: self.scans = scans self.threshold_method = None self.solint = solint self.solmin = solmin self.solsub = solsub self.dofloat = dofloat if snr_threshold is None: self.snr_threshold = 5.0 else: self.snr_threshold = snr_threshold self.threshhold_method = 'snr' if threshold is None: self.threshold = 2000 else: self.threshold = threshold # SNR takes precedence if both thresholds are set. if self.threshold_method is None: self.threshhold_method = 'raw' if self.threshold_method is None: self.threshold_method = 'snr' # if spectral_windows is None: self.spectral_windows = utils.get_spectral_windows(self.msname) else: self.spectral_windows = spectral_windows self.timeqs = self.make_time_qs_from_scans(scans) self.antenna_map = utils.get_antenna_map(self.msname) self.ism = utils.invert_map(self.antenna_map) if antennas is None: # FIXME! Should do this per scan! self.antennas2 = sorted( ffd.actual_antennas(self.msname, self.timeqs[0]).keys()) else: self.antennas2 = antennas self.antenna_names = [self.ism[s] for s in self.antennas2] if ref_antenna_name is None: self.ref_antenna = self.antennas2[0] self.ref_antenna_name = self.ism[self.ref_antenna] casalog.post("No reference antenna selected; using {} ({})" "".format(self.ref_antenna_name, self.ref_antenna)) else: self.ref_antenna_name = ref_antenna_name self.ref_antenna = self.antenna_map[self.ref_antenna_name] pol_ids = utils.get_polarization_ids(msname) if len(pol_ids) > 1: raise UnhandledCase("Non-unique polarisation id") else: self.pol_id = pol_ids[0] n_pols = utils.get_n_polarizations(msname, self.pol_id) if n_pols == 4: self.polinds = [0, 3] elif n_pols == 2: self.polinds = [0] else: raise UnhandledCase("Can't do {} polarizations".format(n_pols)) self.bad_antennas = set() self.fj_name = fj_name make_table.make_table(self.msname, self.fj_name) self.rowcount = 0 # shape = (len(self.polinds), len(self.antennas2)) # Looks like we don't need 'F'? Leave it in for now. # FIXME: find out one way or another. self.delays = np.zeros(shape, np.float, order='F') self.phases = np.zeros(shape, np.float, order='F') self.rates = np.zeros(shape, np.float, order='F') self.flags = np.zeros(shape, np.bool, order='F') self.sigs = []
import casa import numpy as np, scipy, itertools import glob, lsqrs, ffd, fringer, utils msname = globals()['msname'] if False: flagdata(msname, spw='*:0~1;30~31', mode='manual', flagbackup=True) ms.open(msname) timeqs = ["SCAN_NUMBER=2"] station_map = utils.get_station_map(msname) stations2 = sorted(ffd.actual_stations(msname, timeqs[0]).keys()) ism = utils.invert_map(station_map) station_names = [ism[s] for s in stations2] ref_station_name = 'EF' ref_station2 = station_map[ref_station_name] ref_s_ind2 = stations2.index(ref_station2) polind = 0 swids = range(8) shape = (2, len(stations2), len(swids)) delays = np.zeros(shape, np.float, order='F') # Looks like we don't need 'F'. phases = np.zeros(shape, np.float, order='F') rates = np.zeros(shape, np.float, order='F') sigs = [] for timeq, swid in itertools.product(timeqs, swids): casalog.post("doing a thing {}".format(swid))
def fit_multiband_fringe(msname, scan_number, ctname): ms.open(msname) timeqs = ["SCAN_NUMBER={}".format(scan_number)] station_map = utils.get_station_map(msname) stations2 = sorted(ffd.actual_stations(msname, timeqs[0]).keys()) ism = utils.invert_map(station_map) station_names = [ism[s] for s in stations2] ref_station_name = 'EF' ref_station2 = station_map[ref_station_name] ref_s_ind2 = stations2.index(ref_station2) polind = 0 swids = range(8) reffreqs = utils.get_min_freqs(msname) minfreq = utils.get_min_freqs(msname)[0] make_table.make_table(msname, ctname) shape = (2, len(stations2)) delays = np.zeros(shape, np.float) phases = np.zeros(shape, np.float) rates = np.zeros(shape, np.float) sigs = [] rowcount = 0 for timeq in timeqs[:1]: timeq2 = ffd.actual_timerangeq(msname, timeq) for pol_id in [0,1]: casalog.post("Getting data") anffd = ffd.FFData.make_FFD_multiband(msname, stations2, polind, pol_id, timeq2, datacol="CORRECTED_DATA", solint=500) casalog.post("Fitting fringes") dels, phs, rs, sig = fringer.fit_fringe_ffd(anffd, ref_station2, stations2) delays[pol_id, :] = dels phases[pol_id, :] = phs rates[pol_id, :] = rs sigs.append(sig) obsid, field, scan = [ffd.distinct_thing(msname, timeq, col) for col in ['OBSERVATION_ID', 'FIELD_ID', 'SCAN_NUMBER']] darr = -delays*1e9 pharr = -phases # radians! rarr = -rates for i,s in enumerate(stations2): antenna = s assert (anffd.get_station_index(s) == i) time = anffd.get_ref_time() # time = anffd.times[0] interval = anffd.get_interval() midfreqs = utils.get_mid_freqs(msname) for swid in swids: # Df = (reffreqs[swid]-minfreq) Df = (midfreqs[swid]-minfreq) phase_offsets = utils.turns_to_radians(Df * darr/1e9 + interval/2 * rarr) ph = pharr + phase_offsets param = np.zeros(shape=(6,1), dtype='float32') param[:, 0] = [ph[0, i], darr[0, i], rates[0,i], ph[1, i], darr[1, i], rates[1,i] ] make_table.add_row(ctname, rowcount, time, interval, antenna, field, scan, obsid, swid, param) rowcount += 1
pos[tag].add(word) # Convert all the sets to lists, since sets aren't serializable seen = list(seen) vocab = dict((i, c) for i, c in enumerate(seen)) inverted_vocab = dict((c, i) for i, c in enumerate(seen)) for k, v in rhyme.items(): rhyme[k] = list(v) for k, v in meter.items(): meter[k] = list(v) for k, v in pos.items(): pos[k] = list(v) # Create inverse mappings to lookup words inverted_rhyme = invert_map(rhyme) inverted_meter = invert_map(meter) inverted_pos = invert_map(pos) with open('models/shakespeare_words/vocab.json', 'w') as f: json.dump(vocab, f) with open('models/shakespeare_words/inverted_vocab.json', 'w') as f: json.dump(inverted_vocab, f) with open('models/shakespeare_words/rhyme.json', 'w') as f: json.dump(rhyme, f) with open('models/shakespeare_words/inverted_rhyme.json', 'w') as f: json.dump(inverted_rhyme, f) with open('models/shakespeare_words/meter.json', 'w') as f: json.dump(meter, f)