Skip to content

Commit

Permalink
Merge pull request #23 from decarlof/master
Browse files Browse the repository at this point in the history
clean-up and module re-orgs
  • Loading branch information
decarlof committed Apr 1, 2024
2 parents 80ebc4e + 2846717 commit 6d01734
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 197 deletions.
114 changes: 112 additions & 2 deletions dmagic/__main__.py
Expand Up @@ -54,6 +54,7 @@

import os
import sys
import pytz
import time
import datetime
import argparse
Expand All @@ -63,6 +64,8 @@
from dmagic import pv
from dmagic import log
from dmagic import config
from dmagic import authorize
from dmagic import utils


def init(args):
Expand All @@ -73,16 +76,123 @@ def init(args):


def show(args):
"""
Show the currently active proposal info running at beamline
Returns
-------
Show experiment information
"""
now = datetime.datetime.today() + dt.timedelta(args.set)
log.info("Today's date: %s" % now)
scheduling.print_current_experiment_info(args)

auth = authorize.basic()
run = scheduling.current_run(auth, args)
proposals = scheduling.beamtime_requests(run, auth, args)
# pprint.pprint(proposals, compact=True)
if not proposals:
log.error('No valid current experiment')
return None
try:
log.error(proposals['message'])
return None
except:
pass

proposal = scheduling.get_current_proposal(proposals, args)
if proposal != None:
proposal_pi = scheduling.get_current_pi(proposal)
user_name = proposal_pi['firstName']
user_last_name = proposal_pi['lastName']
user_affiliation = proposal_pi['institution']
user_email = proposal_pi['email']
user_badge = proposal_pi['badge']

proposal_gup = scheduling.get_current_proposal_id(proposal)
proposal_title = scheduling.get_current_proposal_title(proposal)
proposal_user_emails = scheduling.get_current_emails(proposal, False)
proposal_start = dt.datetime.fromisoformat(utils.fix_iso(proposal['startTime']))
proposal_end = dt.datetime.fromisoformat(utils.fix_iso(proposal['endTime']))
log.info("\tRun: %s" % run)
log.info("\tPI Name: %s %s" % (user_name, user_last_name))
log.info("\tPI affiliation: %s" % (user_affiliation))
log.info("\tPI e-mail: %s" % (user_email))
log.info("\tPI badge: %s" % (user_badge))
log.info("\tProposal GUP: %s" % (proposal_gup))
log.info("\tProposal Title: %s" % (proposal_title))
log.info("\tStart time: %s" % (proposal_start))
log.info("\tEnd Time: %s" % (proposal_end))
log.info("\tUser email address: ")
for ue in proposal_user_emails:
log.info("\t\t %s" % (ue))
else:
time_now = dt.datetime.now(pytz.timezone('America/Chicago')) + dt.timedelta(args.set)
log.warning('No proposal run on %s during %s' % (time_now, run))


def tag(args):
"""
Update the EPICS PVs with user and experiment information associated with the current experiment
Parameters
----------
args : parameters passed at the CLI, see config.py for full options
"""
# set the experiment date
now = datetime.datetime.today()
log.info("Today's date: %s" % now)
args = pv.update(args, now)

auth = authorize.basic()
run = scheduling.current_run(auth, args)
proposals = scheduling.beamtime_requests(run, auth, args)

if not proposals:
log.error('No valid current experiment')
return None
try:
log.error(proposals['message'])
return None
except:
pass

proposal = scheduling.get_current_proposal(proposals, args)
if not proposal:
log.warning('No valid current proposal')
return

# get PI information
pi = scheduling.get_current_pi(proposal)

user_pvs = pv.init_PVs(args)

log.info("User/Experiment PV update")
user_pvs['user_name'].put(pi['firstName'])
log.info('Updating user_name EPICS PV with: %s' % pi['firstName'])
user_pvs['user_last_name'].put(pi['lastName'])
log.info('Updating user_last_name EPICS PV with: %s' % pi['lastName'])
user_pvs['user_affiliation'].put(pi['institution'])
log.info('Updating user_affiliation EPICS PV with: %s' % pi['institution'])
user_pvs['user_email'].put(pi['email'])
log.info('Updating user_email EPICS PV with: %s' % pi['email'])
user_pvs['user_badge'].put(pi['badge'])
log.info('Updating user_badge EPICS PV with: %s' % pi['badge'])

# set iso format time
central = pytz.timezone('US/Central')
local_time = central.localize(now)
local_time_iso = local_time.isoformat()
user_pvs['user_info_update_time'].put(local_time_iso)
log.info('Updating user_info_update_time EPICS PV with: %s' % local_time_iso)

# get experiment information
user_pvs['proposal_number'].put(scheduling.get_current_proposal_id(proposal))
log.info('Updating proposal_number EPICS PV with: %s' % scheduling.get_current_proposal_id(proposal))
user_pvs['proposal_title'].put(scheduling.get_current_proposal_title(proposal))
log.info('Updating proposal_title EPICS PV with: %s' % scheduling.get_current_proposal_title(proposal))
#Make the start date of the experiment into a year - month
start_datetime = datetime.datetime.strptime(utils.fix_iso(proposal['startTime']),'%Y-%m-%dT%H:%M:%S%z')
user_pvs['experiment_date'].put(start_datetime.strftime('%Y-%m'))
log.info('Updating experiment_date EPICS PV with: %s' % start_datetime.strftime('%Y-%m'))


def main():
Expand Down
39 changes: 39 additions & 0 deletions dmagic/authorize.py
@@ -0,0 +1,39 @@
import pathlib
from requests.auth import HTTPBasicAuth

from dmagic import log


__author__ = "Francesco De Carlo"
__copyright__ = "Copyright (c) 2015, UChicago Argonne, LLC."
__docformat__ = 'restructuredtext en'
__all__ = ['basic',
'read_credentials', ]

debug = False


def basic(filename='.scheduling_credentials'):
"""
Get authorization using username and password contained in filename.
"""
credentials = read_credentials(pathlib.PurePath(pathlib.Path.home(), filename))

username = credentials[0][0]
password = credentials[0][1]
auth = HTTPBasicAuth(username, password)

return auth


def read_credentials(filename):
"""
Read username and password from filename.
Must create filename in the user home directory with | separated values: user|pwd
"""
credentials = []
with open(filename, 'r') as file:
for line in file:
username, password = line.strip().split('|')
credentials.append((username, password))
return credentials
70 changes: 2 additions & 68 deletions dmagic/pv.py
Expand Up @@ -49,19 +49,14 @@
"""
Module generating user and proposal info PVs
"""
import datetime
import pytz
from epics import PV

from dmagic import log
from dmagic import utils
from dmagic import scheduling

__author__ = "Francesco De Carlo"
__copyright__ = "Copyright (c) 2015-2016, UChicago Argonne, LLC."
__docformat__ = 'restructuredtext en'
__all__ = ['init_PVs',
'update']
__all__ = ['init_PVs', ]


def init_PVs(args):
Expand All @@ -80,6 +75,7 @@ def init_PVs(args):

user_pvs = {}
tomoscan_prefix = args.tomoscan_prefix
log.info("Trying to initiale EPICS PVs")
user_pvs['user_name'] = PV(tomoscan_prefix + 'UserName')
user_pvs['user_last_name'] = PV(tomoscan_prefix + 'UserLastName')
user_pvs['user_affiliation'] = PV(tomoscan_prefix + 'UserInstitution')
Expand All @@ -90,65 +86,3 @@ def init_PVs(args):
user_pvs['user_info_update_time'] = PV(tomoscan_prefix + 'UserInfoUpdate')
user_pvs['experiment_date'] = PV(tomoscan_prefix + 'ExperimentYearMonth')
return user_pvs


def update(args, date=None):
"""
Update the EPICS PVs with user and experiment information associated with the current experiment
Parameters
----------
args : parameters passed at the CLI, see config.py for full options
"""

auth = scheduling.authorize()
run = scheduling.current_run(auth, args)
proposals = scheduling.beamtime_requests(run, auth, args)

if not proposals:
log.error('No valid current experiment')
return None
try:
log.error(proposals['message'])
return None
except:
pass

proposal = scheduling.get_current_proposal(proposals, args)
if not proposal:
log.warning('No valid current proposal')
return

# get PI information
pi = scheduling.get_current_pi(proposal)

user_pvs = init_PVs(args)

log.info("User/Experiment PV update")
user_pvs['user_name'].put(pi['firstName'])
log.info('Updated EPICS PV with: %s' % pi['firstName'])
user_pvs['user_last_name'].put(pi['lastName'])
log.info('Updated EPICS PV with: %s' % pi['lastName'])
user_pvs['user_affiliation'].put(pi['institution'])
log.info('Updated EPICS PV with: %s' % pi['institution'])
user_pvs['user_email'].put(pi['email'])
log.info('Updated EPICS PV with: %s' % pi['email'])
user_pvs['user_badge'].put(pi['badge'])
log.info('Updated EPICS PV with: %s' % pi['badge'])

# set iso format time
central = pytz.timezone('US/Central')
local_time = central.localize(date)
local_time_iso = local_time.isoformat()
user_pvs['user_info_update_time'].put(local_time_iso)
log.info('Updated EPICS PV with: %s' % local_time_iso)

# get experiment information
user_pvs['proposal_number'].put(scheduling.get_current_proposal_id(proposal))
log.info('Updated EPICS PV with: %s' % scheduling.get_current_proposal_id(proposal))
user_pvs['proposal_title'].put(scheduling.get_current_proposal_title(proposal))
log.info('Updated EPICS PV with: %s' % scheduling.get_current_proposal_title(proposal))
#Make the start date of the experiment into a year - month
start_datetime = datetime.datetime.strptime(utils.fix_iso(proposal['startTime']),'%Y-%m-%dT%H:%M:%S%z')
user_pvs['experiment_date'].put(start_datetime.strftime('%Y-%m'))
log.info('Updated EPICS PV with: %s' % start_datetime.strftime('%Y-%m'))

0 comments on commit 6d01734

Please sign in to comment.