Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# print('Ehtim reloaded...')
# #except: pass
import ehtim as eh
path_eh = os.path.dirname(eh.__file__)
print('Using eht-imaging library from ', path_eh)
if force_singlepol=='LL':
force_singlepol='L'
if force_singlepol=='RR':
force_singlepol='R'
if force_singlepol=='no':
print('reading data without singlepol, using polrep= ',polrep)
filen = pathf.split('/')[-1]
if polrep in ['circ','stokes']:
obsXX = eh.io.load.load_obs_uvfits(pathf,polrep=polrep)
print('Polrep is ', obsXX.polrep)
else:
obsXX = eh.io.load.load_obs_uvfits(pathf)
print('Polrep unspecified')
dfXX = obsdata_2_df(obsXX)
if 'RR' in filen:
dfXX['polarization'] = 'RR'
elif 'LL' in filen:
dfXX['polarization'] = 'LL'
else:
dfXX['polarization'] = 'WTF'
df = dfXX.copy()
df['band'] = band
#Scale sigma
df['sigma']=scale_sigma*df['sigma']
elif force_singlepol=='':
if polrep in ['circ','stokes']:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R',polrep=polrep)
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L',polrep=polrep)
else:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R')
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L')
dfRR = obsdata_2_df(obsRR)
dfLL = obsdata_2_df(obsLL)
dfRR['polarization'] = 'RR'
dfLL['polarization'] = 'LL'
df = pd.concat([dfRR,dfLL],ignore_index=True)
if only_parallel==False:
if polrep in ['circ','stokes']:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL',polrep=polrep)
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR',polrep=polrep)
else:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL')
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR')
dfRL = obsdata_2_df(obsRL)
dfLR = obsdata_2_df(obsLR)
dfRL['polarization'] = 'RL'
dfLR['polarization'] = 'LR'
df = pd.concat([df,dfLR,dfRL],ignore_index=True)
df['band'] = band
if rescale_noise==True:
rscRR = obsRR.estimate_noise_rescale_factor()
rscLL = obsLL.estimate_noise_rescale_factor()
rsc=0.5*(rscRR+rscLL)
df['sigma']=rsc*df['sigma']
if polrep in ['circ','stokes']:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R',polrep=polrep)
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L',polrep=polrep)
else:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R')
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L')
dfRR = obsdata_2_df(obsRR)
dfLL = obsdata_2_df(obsLL)
dfRR['polarization'] = 'RR'
dfLL['polarization'] = 'LL'
df = pd.concat([dfRR,dfLL],ignore_index=True)
if only_parallel==False:
if polrep in ['circ','stokes']:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL',polrep=polrep)
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR',polrep=polrep)
else:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL')
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR')
dfRL = obsdata_2_df(obsRL)
dfLR = obsdata_2_df(obsLR)
dfRL['polarization'] = 'RL'
dfLR['polarization'] = 'LR'
df = pd.concat([df,dfLR,dfRL],ignore_index=True)
df['band'] = band
if rescale_noise==True:
rscRR = obsRR.estimate_noise_rescale_factor()
rscLL = obsLL.estimate_noise_rescale_factor()
rsc=0.5*(rscRR+rscLL)
df['sigma']=rsc*df['sigma']
df['lrsigma'] = fix_sigma
if rescale_noise==True:
obsXX10 = obsXX.avg_coherent(10.)
rsc = obsXX10.estimate_noise_rescale_factor(max_diff_sec=1000.)
print('Applying noise rescaling to data, factor is: ', rsc)
df['sigma']=rsc*df['sigma']
if 'rrsigma' in df.columns:
df['rrsigma'] = rsc*df['rrsigma']
df['llsigma'] = rsc*df['llsigma']
df['rlsigma'] = rsc*df['rlsigma']
df['lrsigma'] = rsc*df['lrsigma']
elif force_singlepol=='':
if polrep in ['circ','stokes']:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R',polrep=polrep)
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L',polrep=polrep)
else:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R')
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L')
dfRR = obsdata_2_df(obsRR)
dfLL = obsdata_2_df(obsLL)
dfRR['polarization'] = 'RR'
dfLL['polarization'] = 'LL'
df = pd.concat([dfRR,dfLL],ignore_index=True)
if only_parallel==False:
if polrep in ['circ','stokes']:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL',polrep=polrep)
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR',polrep=polrep)
else:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL')
else:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R')
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L')
dfRR = obsdata_2_df(obsRR)
dfLL = obsdata_2_df(obsLL)
dfRR['polarization'] = 'RR'
dfLL['polarization'] = 'LL'
df = pd.concat([dfRR,dfLL],ignore_index=True)
if only_parallel==False:
if polrep in ['circ','stokes']:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL',polrep=polrep)
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR',polrep=polrep)
else:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL')
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR')
dfRL = obsdata_2_df(obsRL)
dfLR = obsdata_2_df(obsLR)
dfRL['polarization'] = 'RL'
dfLR['polarization'] = 'LR'
df = pd.concat([df,dfLR,dfRL],ignore_index=True)
df['band'] = band
if rescale_noise==True:
rscRR = obsRR.estimate_noise_rescale_factor()
rscLL = obsLL.estimate_noise_rescale_factor()
rsc=0.5*(rscRR+rscLL)
df['sigma']=rsc*df['sigma']
else:
obs = eh.io.load.load_obs_uvfits(pathf, force_singlepol=force_singlepol)
df = obsdata_2_df(obs)
def load_uvfits(fname, flipbl=False, force_singlepol=None, channel=all, IF=all):
"""Load observation data from a uvfits file.
Args:
fname (str): path to input text file
flipbl (bool): flip baseline phases if True.
force_singlepol (str): 'R' or 'L' to load only 1 polarization
channel (list): list of channels to average in the import. channel=all averages all channels
IF (list): list of IFs to average in the import. IF=all averages all IFS
Returns:
obs (Obsdata): Obsdata object loaded from file
"""
return ehtim.io.load.load_obs_uvfits(fname, flipbl=flipbl, force_singlepol=force_singlepol, channel=channel, IF=IF)
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L',polrep=polrep)
else:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R')
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L')
dfRR = obsdata_2_df(obsRR)
dfLL = obsdata_2_df(obsLL)
dfRR['polarization'] = 'RR'
dfLL['polarization'] = 'LL'
df = pd.concat([dfRR,dfLL],ignore_index=True)
if only_parallel==False:
if polrep in ['circ','stokes']:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL',polrep=polrep)
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR',polrep=polrep)
else:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL')
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR')
dfRL = obsdata_2_df(obsRL)
dfLR = obsdata_2_df(obsLR)
dfRL['polarization'] = 'RL'
dfLR['polarization'] = 'LR'
df = pd.concat([df,dfLR,dfRL],ignore_index=True)
df['band'] = band
if rescale_noise==True:
rscRR = obsRR.estimate_noise_rescale_factor()
rscLL = obsLL.estimate_noise_rescale_factor()
rsc=0.5*(rscRR+rscLL)
df['sigma']=rsc*df['sigma']
else:
obs = eh.io.load.load_obs_uvfits(pathf, force_singlepol=force_singlepol)
obsXX10 = obsXX.avg_coherent(10.)
rsc = obsXX10.estimate_noise_rescale_factor(max_diff_sec=1000.)
print('Applying noise rescaling to data, factor is: ', rsc)
df['sigma']=rsc*df['sigma']
if 'rrsigma' in df.columns:
df['rrsigma'] = rsc*df['rrsigma']
df['llsigma'] = rsc*df['llsigma']
df['rlsigma'] = rsc*df['rlsigma']
df['lrsigma'] = rsc*df['lrsigma']
elif force_singlepol=='':
if polrep in ['circ','stokes']:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R',polrep=polrep)
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L',polrep=polrep)
else:
obsRR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='R')
obsLL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='L')
dfRR = obsdata_2_df(obsRR)
dfLL = obsdata_2_df(obsLL)
dfRR['polarization'] = 'RR'
dfLL['polarization'] = 'LL'
df = pd.concat([dfRR,dfLL],ignore_index=True)
if only_parallel==False:
if polrep in ['circ','stokes']:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL',polrep=polrep)
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR',polrep=polrep)
else:
obsRL = eh.io.load.load_obs_uvfits(pathf, force_singlepol='RL')
obsLR = eh.io.load.load_obs_uvfits(pathf, force_singlepol='LR')
dfRL = obsdata_2_df(obsRL)
dfLR = obsdata_2_df(obsLR)