Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command Line Arguments #3

Merged
merged 5 commits into from
Jul 24, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 81 additions & 34 deletions dpmData.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,94 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 20 11:43:17 2020

@author: snag
"""
import argparse
import datetime
import sys
import pandas as pd
import DPM

DEVICE_LIMIT = 0
DEVICE_LIST = []
def main():
parser = argparse.ArgumentParser()
#Add positional/optional parameters
parser.add_argument('-d', '--device_limit', help='Limit for number of devices. Default: 0', type=int, default=0)
parser.add_argument('-f', '--device_file', help='Csv file name containing the list of devices. Newline delimited. No default value.')
sakshi-fermi marked this conversation as resolved.
Show resolved Hide resolved
parser.add_argument('-o', '--output_file', help='Name of the output file for the hdf5 file. Default: data.h5', default='data.h5')
parser.add_argument('-n', '--node', help='Name of the node.', default=None, type=str)
#Error when not using the default value
#group 1
#Midnight to midnight currently.
parser.add_argument("-s", "--start_date", type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'), help='Enter the start time/date. Do not use the duration tag', required=False)
parser.add_argument("-e", "--end_date", type=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'), help='Enter the start time/date. Do not use the duration tag', required=False)
#group 2
parser.add_argument("-du", "--duration", help="Enter LOGGERDURATION in sec", required=False)
#Parse the args
args = parser.parse_args()
sys.stdout.write(str(hdf_code(args)))

with open('DeviceList.csv') as f:
DEVICE_LIST = [line.rstrip() for index, line in enumerate(f)
if index < DEVICE_LIMIT or DEVICE_LIMIT < 1]
def local_to_utc(date):
sakshi-fermi marked this conversation as resolved.
Show resolved Hide resolved
utc_timezone = datetime.timezone(datetime.timedelta(0))
utc_datetime_obj = date.astimezone(utc_timezone)
time_in_ms = int(utc_datetime_obj.timestamp() * 1000)
return time_in_ms

dpm = DPM.Blocking()
for index, device in enumerate(DEVICE_LIST):
dpm.add_entry(index, device)
def hdf_code(args):
START_DATE = args.start_date
END_DATE = args.end_date
DURATION = args.duration
DEVICE_LIMIT = args.device_limit
DEVICE_FILE = args.device_file
OUTPUT_FILE = args.output_file
NODE = args.node

data_done = [None] * len(DEVICE_LIST)
request_string = '' #Used later to provide input string for DPM

if DURATION and (START_DATE or END_DATE):
print("-d and -s|-e are mutually exclusive! Exiting ...")
sys.exit(2)

hdf = pd.HDFStore('data.h5')
for event_response in dpm.process('LOGGERDURATION:360000'):
if hasattr(event_response, 'data'):
d = {'Timestamps' : event_response.micros, 'Data' : event_response.data}
df = pd.DataFrame(data=d)
hdf.append(DEVICE_LIST[event_response.tag], df)
elif(START_DATE and END_DATE):
time_window = 'LOGGER:' + str(local_to_utc(START_DATE)) + ':' + str(local_to_utc(END_DATE))
request_string = time_window
sakshi-fermi marked this conversation as resolved.
Show resolved Hide resolved

if len(event_response.data) == 0:
data_done[event_response.tag] = True
elif DURATION:
sakshi-fermi marked this conversation as resolved.
Show resolved Hide resolved
DURATION *= 1000
DURATION = str(DURATION)
DURATION = 'LOGGERDURATION:' + DURATION
request_string = DURATION
sakshi-fermi marked this conversation as resolved.
Show resolved Hide resolved

if NODE:
request_string += ':' + NODE

#The input is line separated devices.
DEVICE_LIST = []
with open(DEVICE_FILE) as f:
DEVICE_LIST = [line.rstrip() for index, line in enumerate(f)
sakshi-fermi marked this conversation as resolved.
Show resolved Hide resolved
if index < DEVICE_LIMIT or DEVICE_LIMIT < 1]
dpm = DPM.Blocking(None) #Do not commit with 'DPMJ@VIRT01'
for index, device in enumerate(DEVICE_LIST):
dpm.add_entry(index, device)

#Status instead of actual data.
else:
#Want to make it status, but can't because of the bug
data_done[event_response.tag] = False
print(DEVICE_LIST[event_response.tag], event_response.status)
data_done = [None] * len(DEVICE_LIST)
hdf = pd.HDFStore(OUTPUT_FILE)
for event_response in dpm.process(request_string):
if hasattr(event_response, 'data'):
d = {'Timestamps' : event_response.micros, 'Data' : event_response.data}
df = pd.DataFrame(data=d)
hdf.append(DEVICE_LIST[event_response.tag], df)
if len(event_response.data) == 0:
data_done[event_response.tag] = True
#Status instead of actual data.
else:
#Want to make it status, but can't because of the bug
data_done[event_response.tag] = False
print(DEVICE_LIST[event_response.tag], event_response.status)
if data_done.count(None) == 0:
print(data_done)
break

if data_done.count(None) == 0:
print(data_done)
break
#READ THE HDF5 FILE
for k in list(hdf.keys()):
df = hdf[k]
print(k, ':\n', df)

#READ THE HDF5 FILE
for k in list(hdf.keys()):
df = hdf[k]
print(k, ':\n', df)
if __name__ == '__main__':
main()