You are on page 1of 3

###########################################################################

# program: pump_data_convert.py
# author: Tom Irvine
# Email: tom@irvinemail.org
# version: 1.0
# date: October 1, 2018
# description: Data conversion utility
# The input file must have three columns: date, time & amplitude
# Line Example: 09/20/2018 08:23:56 0.1765432
#
# The output file will have two columns: time(sec) & amplitude
# The time will start at zero.
#
###########################################################################

from __future__ import print_function

import sys

if sys.version_info[0] == 2:
print ("Python 2.x")
import Tkinter as tk
from tkFileDialog import askopenfilename
from tkFileDialog import asksaveasfilename

if sys.version_info[0] == 3:
print ("Python 3.x")
import tkinter as tk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename

import os
import re
import numpy as np

import datetime
import calendar

###########################################################################

def read_three_columns_from_dialog(label):
"""
Read data from file using a dialog box
"""
while(1):
root = tk.Tk() ; root.withdraw()
input_file_path = askopenfilename(parent=root,title=label)

file_path = input_file_path.rstrip('\n')
#
if not os.path.exists(file_path):
print ("This file doesn't exist")
#
if os.path.exists(file_path):
print ("This file exists")
print (" ")
infile = open(file_path,"rb")
lines = infile.readlines()
infile.close()

a = []
b = []
c = []
num=0
for line in lines:
#
if sys.version_info[0] == 3:
line = line.decode(encoding='UTF‐8')

if re.search(r"(\d+)", line): # matches a digit


iflag=0
else:
iflag=1 # did not find digit
#
if re.search(r"#", line):
iflag=1
#
if iflag==0:
line=line.lower()
if re.search(r"([a‐d])([f‐z])", line): # ignore header lines
iflag=1
else:
line = line.replace(","," ")
col1,col2,col3=line.split()
a.append(col1)
b.append(col2)
c.append(col3)
num=num+1
break

print ("\n samples = %d " % num)

return a,b,c,num

###############################################################################

print(" ")
print("The input file must have three columns: date, time & amplitude")
a,b,c,num=read_three_columns_from_dialog('Select Input File')

t = np.zeros((num,1))

for i in range (0,num):

month, day, year = a[i].split('/')

dday=datetime.datetime(int(year), int(month), int(day), 0, 0)


tep=float(calendar.timegm(dday.timetuple()))

h, m, s = b[i].split(':')
t[i]=tep + float(h)*3600 + float(m)*60 + float(s)

# print (" %s %s %s " % (a[i],b[i],c[i]))


# print(" %s %g" %(s,t))

t=t‐t[0]

###############################################################################

root = tk.Tk() ; root.withdraw()


output_file_path = asksaveasfilename(parent=root,title='Output file')
output_file = output_file_path.rstrip('\n')
outfile = open(output_file,"w")

if(len(t)==len(c)):
nn=len(t)

for i in range (0, nn):


cc=float(c[i])
outfile.write(' %11.7e \t %8.4e \n' % (t[i],cc))

outfile.close()
print(' conversion complete')
else:
print(' length error' )

You might also like