#!/usr/bin/env python

# Mike Zelazny
# Generates a report for Klystron PAD Temperatures

import os
import datetime
import subprocess
import subprocessca
import KlystronList
import string

f=open(os.getenv('LCLS_DATA') + '/physics/amrf/klydata/tpad/' + str(datetime.date.today()), 'w')

f.write('Out of Tolerence Klystron PAD Temperatures\n\n')
f.write('   Report Generated ' + str(datetime.date.today()) + ' ' + datetime.datetime.now().strftime("%H:%M:%S") + '\n\n')

f.write('     STATION       NAME     TPAD\n')
f.write('   ------------   ------   -------\n')

KLYS = KlystronList.KlystronList()
for klystron in KLYS:

    try:
         process = subprocess.Popen([ 'aidaget',  klystron + '//TPAD' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         tpad = string.rstrip(process.communicate()[0])
         if float(tpad) == 0:
              tpad = '  ' + string.strip(subprocessca.get(klystron + ':TPAD'))
         if float(tpad) >= 50:
              process = subprocess.Popen([ 'aidaget',  klystron + '//NAME' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
              name = string.rstrip(process.communicate()[0])
              f.write('   ' + string.replace(string.ljust(klystron,13,' '),':',' ') + string.ljust(name,9,' ') + tpad + '\n\n')
    except ValueError as e:
         pass # Do nothing

f.close()
