import logging
import numpy as np
from os.path import basename
from vcstools.job_submit import submit_slurm
logger = logging.getLogger(__name__)
[docs]def RVM_fit(cfg, depends_on=None, depend_type="afterany"):
"""Calculates parameters for a PSRSALSA ppolFit job and submits it"""
alpha = cfg["pol"]["alpha"]
beta = cfg["pol"]["beta"]
if not cfg["completed"]["RVM_initial"] and not cfg["completed"]["RVM_final"]: # Initial
trials = 200
alpha_range = np.array((0, 180))
beta_range = np.array((-30, 30))
#Decide the longitude range to fit
my_comp = cfg["source"]["my_component"]
component_min = cfg["source"]["gfit"]["comp_idx"][my_comp][0] * 360/len(cfg["source"]["gfit"]["profile"])
component_max = cfg["source"]["gfit"]["comp_idx"][my_comp][-1] * 360/len(cfg["source"]["gfit"]["profile"])
l_cmd = f" -l '{component_min} 1'"
maxdl_cmd = f" -maxdl {component_max - component_min}"
chigrid_file = basename(cfg['files']['chigrid_initial_ps']) # Can't have names greater than 100 characters - use basenames instead
paswing_file = basename(cfg['files']['paswing_initial_ps'])
outfile = cfg['files']['RVM_fit_initial']
name = f"RVM_fit_initial_{cfg['files']['file_precursor']}"
cfg["completed"]["RVM_initial"] = True
else: # Final
trials = 400
alpha_range = np.array((alpha - 20, alpha + 20))
beta_range = np.array((beta - 10, beta + 10))
alpha_range.clip(0, 180)
beta_range.clip(-30, 30) # forcing the range to reasonable values
l_cmd = f" -l '{cfg['pol']['l0'] - 10} 1'"
maxdl_cmd = " -maxdl 20"
chigrid_file = basename(cfg['files']['chigrid_final_ps'])
paswing_file = basename(cfg['files']['paswing_final_ps'])
outfile = cfg['files']['RVM_fit_final']
name = f"RVM_fit_final_{cfg['files']['file_precursor']}"
cfg["completed"]["RVM_final"] = True
# Create the job commands
commands = [f"cd {cfg['files']['psr_dir']}"]
ppol_cmd = "ppolFit -showwedge"
ppol_cmd += f" -g '{trials} {trials}'"
ppol_cmd += f" -A '{alpha_range[0]} {alpha_range[1]}'" # Alpha range
ppol_cmd += f" -B '{beta_range[0]} {beta_range[1]}'" # Beta range
ppol_cmd += l_cmd # longitude start and step size
ppol_cmd += maxdl_cmd #longitude search range
ppol_cmd += " -best" # return the best fit values
ppol_cmd += f" -device1 {chigrid_file}/cps"
ppol_cmd += f" -device2 {paswing_file}/cps"
ppol_cmd += f" -device1res '900 900'"
ppol_cmd += f" *.paswing"
ppol_cmd += f" > {outfile}" #write to stdout
commands.append(ppol_cmd)
#Submit job
slurm_kwargs = {"time": "02:00:00"}
mem = 32768
modules = ["psrsalsa"]
jid = submit_slurm(name, commands,
slurm_kwargs=slurm_kwargs, module_list=modules, mem=mem, batch_dir=cfg["files"]["batch_dir"], depend=depends_on,
depend_type=depend_type, vcstools_version=cfg["run_ops"]["vcstools"], submit=True)
logger.info(f"Submitted relaunch of ppp: {name}")
logger.info(f"job ID: {jid}")
if depends_on:
logger.info(f"Job depends on job id(s): {depends_on}")
return jid
def RVM_file_to_cfg(cfg):
if cfg["completed"]["RVM_final"]:
RVM_file = cfg['files']['RVM_fit_final']
else:
RVM_file = cfg['files']['RVM_fit_initial']
alpha, beta, l0, pa0, chi = read_RVM_fit(RVM_file)
cfg["pol"]["alpha"] = alpha
cfg["pol"]["beta"] = beta
cfg["pol"]["l0"] = l0
cfg["pol"]["pa0"] = pa0
cfg["pol"]["chi"] = chi
[docs]def read_RVM_fit(RVM_file):
"""Reads a stdout file generated by ppolFit"""
# The breaks in the loop force the function to read the first instance of each param
with open(RVM_file, "r") as f:
lines = f.readlines()
alpha = None
beta = None
l0 = None
pa0 = None
chi = None
for line in lines:
if "alpha =" in line:
alpha = float(line.split()[2])
break
for line in lines:
if "beta =" in line:
beta = float(line.split()[2])
break
for line in lines:
if "l0 =" in line:
l0 = float(line.split()[2])
break
for line in lines:
if "pa0 =" in line:
pa0 = float(line.split()[2])
break
for line in lines:
if "chi^2=" in line:
chi = float(line.split()[1].split("=")[1])
break
if None in (alpha, beta, l0, pa0, chi):
raise ValueError(f"""
None value in file: {RVM_file}
alpha: {alpha}
beta: {beta}
l0: {l0}
pa0: {pa0}
chi: {chi}
""")
return (alpha, beta, l0, pa0, chi)