Created
March 8, 2014 13:58
-
-
Save mshuler/9430956 to your computer and use it in GitHub Desktop.
Star2000 CSV etime Timestamp Modification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# vim: ai ts=4 sw=4 sts=4 et | |
# | |
# http://www.star.bnl.gov/ | |
# http://crd-legacy.lbl.gov/~kewu/fastbit/data/samples.html | |
# | |
# star2000 etime column date format examples: | |
# 20000625.0617250018 | |
# 20000625.061728999 | |
# 20000625.062128 | |
# 20000625.08072 | |
# 20000627.1206 | |
# 20000627.143 | |
# (I think 3 decimal places is the least, but .0000000000 could be in there) | |
# | |
# 20000625.0617250018 = 2000-06-25 06:17:25.0018 | |
# | |
# datetime takes microseconds and right pads smaller than 6 places (000143), so we need to left justify: | |
# actually, we'll need to left justify and fill *all* of hour|min|sec, then split the parts.. anyway.. | |
# ms = '143'.ljust(6, '0') | |
# print(datetime.datetime(2000, 06, 25, 06, 17, 25, int(ms))) | |
# result: 2000-06-25 06:17:25.143000 | |
# | |
# example csv rows - timestamp is col 6 (row[5]): | |
# 84, 314, 173724, 173726, 3528, 20000818.0320390016, 1230070, 111, 0, 293, 0.4678776, 3 | |
# 23, 197, 173724, 173726, 3529, 20000818.0320390016, 1230070, 4, -0.44206637, 81, 0.41065842, 71 | |
# 19, 193, 173724, 173726, 3530, 20000818.0320390016, 1230070, 3, 0, 72, 0.60175335, 4 | |
# 0, 42, 173724, 173726, 3531, 20000818.0320390016, 1230070, 0, 0, 0, -9999, 74 | |
# 25, 244, 173724, 173726, 3532, 20000818.03204, 1230070, 8, -5.3493352, 91, 0.6936518, 4 | |
# 95, 499, 173724, 173726, 3533, 20000818.03204, 1230070, 47, -7.4228468, 313, 0.54136318, 3 | |
# 40, 250, 173724, 173726, 3534, 20000818.03204, 1230070, 8, 1.9150425, 123, 0.61817312, 5 | |
import datetime | |
import csv | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f', | |
dest = 'filename', | |
required = True, | |
help = 'file to parse') | |
args = parser.parse_args() | |
#csv_in = 'x.csv' | |
csv_in = args.filename | |
csv_out = csv_in + '.timestamped' | |
with open(csv_in, 'rb') as csvfile: | |
csvreader = csv.reader(csvfile, delimiter=',') | |
out = open(csv_out, 'w') | |
for row in csvreader: | |
#print row[5] + ' <-- original' | |
dt = row[5].ljust(22, '0') # 1 leading space + 19 chars from date + 2 more zeros | |
#print dt + ' <-- filled' | |
year = int(dt[1:5]) # 0 is a leading space | |
month = int(dt[5:7]) | |
day = int(dt[7:9]) | |
hour = int(dt[10:12]) | |
minute = int(dt[12:14]) | |
# minutes and seconds have a few random 99's (no other non-0..59 values), just set to to 59 | |
if minute == 99: | |
minute = 59 | |
sec = int(dt[14:16]) | |
if sec == 99: | |
sec = 59 | |
ms = int(dt[16:22]) | |
try: | |
t = datetime.datetime(year, month, day, hour, minute, sec, ms) | |
#print t | |
etime = t.strftime('%Y-%m-%d %H:%M:%S.%f') | |
#print etime | |
row[5] = ' ' + etime[:-3] # add back leading space; trim to millisecond resolution | |
except Exception, err: | |
print '------> bogus timestamp..', year, month, day, hour, minute, sec, ms | |
print '------> ', err | |
#print ','.join(row) | |
out.write(','.join(row) + '\n') | |
out.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment