#!/usr/bin/env python"""This a command line tool to convert XML injection files"""importargparseimportjsonimportosfrommathimportpiimportlalsimulationaslalsimimportpandasaspdfromgwpy.tableimportTableimportbilbytry:importligo.lw# noqa F401exceptImportError:raiseImportError("You do not have ligo.lw install: $ pip install python-liw-lw")
[docs]defmain():parser=argparse.ArgumentParser(prog="bilby_pipe_xml_converter",description=__doc__)parser.add_arg("xml_file",type=str,default=None,help="The xml file to convert")parser.add_arg("--format",type=str,default="json",choices=["json","dat"],help="The output injection format to use",)parser.add_arg("--reference-frequency",type=float,default=None,help=("The reference frequency to use for converting from xml"),required=True,)parser.add_arg("--convert-negative-ra",default=False,help=("Convert (-pi,pi) RA range from (0,2pi)"),action="store_true",required=False,)args=parser.parse_args()injection_values=xml_to_dataframe(args.xml_file,args.reference_frequency,args.convert_negative_ra)basename=os.path.splitext(args.xml_file)[0]path=basename+os.path.extsep+args.formatifargs.format=="json":injections=dict(injections=injection_values)withopen(path,"w")asfile:json.dump(injections,file,indent=2,cls=bilby.core.result.BilbyJsonEncoder)elifargs.format=="dat":injection_values.to_csv(path,index=False,header=True,sep=" ")