#!/usr/bin/env python
#
# Makes an archive viewer config file for LI22, 23, 25, 26, 27, 28, 29 
#  QUAD, XCOR, or YCOR BDES 8 magnets
#
# Auth: Janice Nelson
# Date: 14 Jun 2020
#
# Usage:
#  edm shell button : makeLIxxMgntAVcfg.py $(sector) (prim) ; lclsarch \"LIxxMgnt.xml -plot \""
#
import sys
import os
import subprocess
import xmlrpclib
proxy = xmlrpclib.ServerProxy('http://lcls-archsrv/cgi-bin/ArchiveDataServer.cgi')
result = proxy.archiver.archives()
#
colors = [ -65536, -16776961, -16711936, -65281, -16777216, -10092391, -39424, -13395559, -6710887 ]
unit_list = [ '20', '30', '40', '50', '60', '70', '80', '90' ]
quad=0
xcor=1
ycor=2
#
f=open(os.getenv('ARCHCONFIGFILES') + '/LIxxMgnt.xml', 'w')
#f=open('LIxxMgnt.xml','w')
f.write('<?xml version="1.0" encoding="UTF-16"?>\n')
f.write('<AVConfiguration>\n')
f.write('    <connection_parameter>pbraw://lcls-archapp.slac.stanford.edu/retrieval</connection_parameter>\n')
f.write('    <time_axis name="Main Time Axis">\n')
f.write('        <start>-1d</start>\n')
f.write('        <end>now</end>\n')
f.write('        <location>bottom</location>\n')
f.write('    </time_axis>\n')
f.write('    <range_axis name="Main Range Axis">\n')
f.write('        <min/>\n')
f.write('        <max/>\n')
f.write('        <type>normal</type>\n')
f.write('        <location>left</location>\n')
f.write('    </range_axis>\n')
f.write('    <legend_configuration show_ave_name="true" show_directory_name="false" show_range="true" show_units="true"/>\n')
f.write('    <plot_title/>\n')
#
micr=sys.argv[1]
prim=sys.argv[2]

for nn in range(8):
  if prim.upper() == 'QUAD':
    pv = 'QUAD:' + micr.upper() + ':' + unit_list[nn] + '1:BDES'
  elif nn==7:
    pv = prim.upper() + ':' + micr.upper() + ':' + unit_list[nn] + '0:BDES'
  elif prim.upper() == 'XCOR':
    pv = 'XCOR:' + micr.upper() + ':' + unit_list[nn] + '2:BDES'
  else:
    pv = 'YCOR:' + micr.upper() + ':' + unit_list[nn] + '3:BDES'
#  print(pv)

  f.write('    <pv directory_name="Default" name="' + pv + '">\n')
  f.write('        <time_axis_name>Main Time Axis</time_axis_name>\n')
  f.write('        <range_axis_name>Main Range Axis</range_axis_name>\n')
  f.write('        <color>' + str(colors[nn]) + '</color>\n')
  f.write('        <draw_type>steps</draw_type>\n')
  f.write('        <draw_width>1.0</draw_width>\n')
  f.write('        <visibility>true</visibility>\n')
  f.write('    </pv>\n')
#

#
f.write('</AVConfiguration>\n')
f.close()
