!git checkout master
!git branch
import os
import sys
sys.path.append("/home/jovyan/work/shared/nanograv_timing_2017/util/")
from nanograv_utils import *
sys.path.append("/home/jovyan/work/shared/")
from pypulse.tim import Tim
%matplotlib inline
# Load the notebook utilities
nanoutils = NANOGravUtils()
tempoutils = TempoUtils()
pintutils = PINTUtils()
enterpriseutils = EnterpriseUtils()
FLAGS = "-G" # Use "-G" for Generalized least squares fit
NITER = 50000 #10000 # How many iterations to run PAL2 for
EXCISEPARFILE = "J2229+2643.excise.par"
EXCISETIMFILE = "J2229+2643.excise.tim"
PARFILE = "J2229+2643.working.par"
TIMFILE = "J2229+2643.working.tim"
# Copy initial par/tim files into working files
call("cp J2229+2643.dmx.par %s"%EXCISEPARFILE)
call("cp J2229+2643.dmx.tim %s"%EXCISETIMFILE)
tempoutils.tempo(EXCISEPARFILE,EXCISETIMFILE,c=True)
tempoutils.plot_resids()
# TOA excision from orphaned receivers
with open("../../orphan_data.txt",'r') as FILE:
lines = FILE.readlines()
fes = []
for line in lines:
if "J2229+2643" in line:
orphanfe = line.strip().split()[-1]
# Since there can be multiple entries, just do this the slow way
tim = Tim(EXCISETIMFILE)
for toa in tim.toas:
fe = toa.get('fe')
if fe == orphanfe:
fes.append(fe)
toa.comment(cut="orphaned")
tim.save(EXCISETIMFILE)
# Remove JUMPs
with open(EXCISEPARFILE,'r') as FILE:
lines = FILE.readlines()
output = ""
for line in lines:
writeout = True
for fe in fes:
if fe in line:
writeout = False
if writeout:
output += line
with open(EXCISEPARFILE,'w') as FILE:
FILE.write(output)
# TOA excision from outlier analysis
outlierfilenames = np.loadtxt("outlier/J2229+2643.dmx-outliers.txt",unpack=True,usecols=(0,),dtype=np.str,ndmin=1)
outlierchans = np.loadtxt("outlier/J2229+2643.dmx-outliers.txt",unpack=True,usecols=(6,),dtype=np.int,ndmin=1)
outliersubints = np.loadtxt("outlier/J2229+2643.dmx-outliers.txt",unpack=True,usecols=(8,),dtype=np.int,ndmin=1)
outlierpouts = np.loadtxt("outlier/J2229+2643.dmx-outliers.txt",unpack=True,usecols=(12,),dtype=np.float,ndmin=1)
tim = Tim(EXCISETIMFILE)
for toa in tim.toas:
filename,chan,subint = toa.getFilename(),int(toa.get('chan')),int(toa.get('subint'))
for i,outlierfilename in enumerate(outlierfilenames):
if outlierfilename == filename and outlierchans[i] == chan and outliersubints[i] == subint and outlierpouts[i] >= 0.10:
toa.comment(cut="outlier10")
break
tim.save(EXCISETIMFILE)
nanoutils.plot_multipage_pdf("outlier/J2229+2643.dmx-residuals.pdf")#,height=400)
# Ensure correct DMX binning, create PARFILE and TIMFILE
! ../../util/./dmx_fixer.py J2229+2643.excise.par J2229+2643.excise.tim excise
# Center the EPOCHs
! $TEMPO/util/center_epoch/center_epoch.py J2229+2643.excise.par J2229+2643.excise.tim J2229+2643.excise.par
# Noise parameter addition from outlier analysis
with open("outlier/J2229+2643.dmx-noise.txt",'r') as FILE:
outlierlines = FILE.readlines()
with open(EXCISEPARFILE,'r') as FILE:
lines = FILE.readlines()
output = ""
for line in lines:
if "EFAC" in line or "EQUAD" in line or "ECORR" in line or "RNAMP" in line or "RNIDX" in line: #remove these parameters
continue
output += line
rnoutput = ""
for line in outlierlines: #add these parameters
if "efac" in line:
output += line.replace("efac-","T2EFAC -f ")
elif "equad" in line:
value = 10**(float(line.split()[-1].strip()))
string = line.replace("equad-","T2EQUAD -f ")
output += "%s %0.5f\n"%(" ".join(string.split()[:-1]),value)
elif "jitter" in line:
value = 10**(float(line.split()[-1]))
string = line.replace("jitter-","ECORR -f ")
output += "%s %0.5f\n"%(" ".join(string.split()[:-1]),value)
elif "RN-Amplitude" in line:
value = float(line.split()[1])
RNAMP = tempoutils.convert_to_RNAMP(value)
RNAMP = str("%0.4e"%RNAMP).replace('e','D')
rnoutput += "RNAMP %s\n"%RNAMP
elif "RN-spectral-index" in line:
gamma = float(line.split()[1])
rnoutput += "RNIDX %0.4f\n"%(-gamma)
if not (gamma < 1.0):
output += rnoutput
with open(EXCISEPARFILE,'w') as FILE:
FILE.write(output)
# Adjust parameters
with open(EXCISEPARFILE,'r') as FILE:
lines = FILE.readlines()
output = ""
for line in lines:
output += line
output += "XDOT 0.0 1 0.0\n"
with open(EXCISEPARFILE,'w') as FILE:
FILE.write(output)
# Copy excise files to working files
call("cp %s %s"%(EXCISEPARFILE,PARFILE))
call("cp %s %s"%(EXCISETIMFILE,TIMFILE))
! ../../util/./finalize_timing.py -G J2229+2643.working.par J2229+2643.working.tim
nanoutils.plot_multipage_pdf("J2229+2643.summary.pdf")
NOISEDIR = "noisemodel/"
CHAINSDIR = "noisemodel/chains/J2229+2643/"
call("mkdir %s"%NOISEDIR)
call("mkdir noise_output")
#call("mkdir %s"%CHAINSDIR)
call("cp %s %s%s"%(TIMFILE,NOISEDIR,TIMFILE))
call("cp %s %s%s"%(PARFILE,NOISEDIR,PARFILE))
with open("%s%s"%(NOISEDIR,PARFILE),'r') as FILE:
lines = FILE.readlines()
output = ""
for line in lines:
write = True
for param in ["T2EFAC","T2EQUAD","ECORR","RNAMP","RNIDX"]:
if param in line:
write = False
continue
if write:
output += line
with open("%s%s"%(NOISEDIR,PARFILE),'w') as FILE:
FILE.write(output)
enterpriseutils.setup_model(NOISEDIR+PARFILE,NOISEDIR+TIMFILE)
enterpriseutils.setup_sampler(CHAINSDIR)
enterpriseutils.run_sampler(N=NITER)
enterpriseutils.process_noise('%s/chain_1.txt'%CHAINSDIR,"noise_output/t1_noise.txt") #need to add in T2 noise!
enterpriseutils.plot_corner('%s/chain_1.txt'%CHAINSDIR)
# No need to comment out low-spectral-index red noise as this is taken care of in EnterpriseUtils.process_noise() now
with open("noise_output/t1_noise.txt",'r') as FILE:
noiselines = FILE.readlines()
with open("%s%s"%(NOISEDIR,PARFILE),'r') as FILE: #read in the noisemodel version
lines = FILE.readlines()
output = ""
for line in lines:
if "JUMP" in line:
for noiseline in noiselines:
output += noiseline
output += line
with open("%s"%PARFILE,'w') as FILE: #write out to the main version
FILE.write(output)
tempoutils.tempo(PARFILE,TIMFILE,G=True,c=True)
call("mv J2229+2643.par %s"%PARFILE)
! ../../util/./finalize_timing.py -G J2229+2643.working.par J2229+2643.working.tim
nanoutils.plot_multipage_pdf("J2229+2643.summary.pdf")
!git add J2229+2643.working.par J2229+2643.working.tim process.ipynb parameterchange.txt
!git commit -m "J2229+2643 run" J2229+2643.working.par J2229+2643.working.tim process.ipynb parameterchange.txt
!git pull
!git push