Created
October 19, 2014 00:03
-
-
Save grandadmiral-thrawn/b9a60558b2f34e1dc7cb to your computer and use it in GitHub Desktop.
campbellold.py
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
import csv | |
import glob | |
import datetime | |
from decimal import Decimal | |
## This is a script (soon to be a module) for reading old campbell logger files | |
## These files usually end in an extension such as .099 or .012 indicative of their year | |
## The example name for this file is seen in the main runing loop of this script at the bottom | |
## The "delimiting" between the file and values is done on a "+" sign or "-" sign depending on the signature of the value | |
## Often one array (loggerid) will have a longer instruction than others, and will be delimited differently. | |
## The script will autoparse the files generated by an old logger based on the campbell conventions. | |
def iscampbell(file): | |
# determines if it's an old campbell datalogger file | |
f = open(file,'r') | |
line = f.readline() | |
line = line.split('+') | |
if line[0][0:2] == "01": | |
itscampbell = 1 | |
else: | |
itscampbell = 0 | |
return (itscampbell,line,f) | |
def getloggerid(line): | |
# gets the loggerid from the line | |
loggerid = line[1][1:4] | |
return loggerid | |
def getyear(line): | |
# gets the year from the line | |
isyear = Decimal(line[3][0:4]) | |
if isyear < 1970: | |
isyear = Decimal(line[2][0:4]) | |
year = str(isyear) | |
doypos = 3 | |
else: | |
year = str(isyear) | |
doypos = 4 | |
return (year, doypos) | |
def getdoy(line, doypos, loggerid): | |
# gets the doy from the line | |
if loggerid != "129": | |
doy = line[doypos][1:4] | |
else: | |
doy = line[doypos][0:3] | |
return doy | |
def gethhmm(line, doypos): | |
hhmm = line[doypos+1][0:4] | |
return hhmm | |
def getprogid(line,doypos): | |
if doypos == 3: | |
progid = line[5][0:3] | |
else: | |
progid = line[2][0:3] | |
return progid | |
def getval(line): | |
allvalues = [] | |
for value in range(5,len(line)-1): | |
val = line[value][0:5] | |
allvalues.append(val) | |
return allvalues | |
def remove_plus(getloggerid, getyear, getdoy, gethhmm, getprogid, getval, line,itscampbell): | |
# run this if the campbell statement is true only | |
if itscampbell == 1: | |
(theyear,doypos) = getyear(line) | |
theloggerid = getloggerid(line) | |
newline = [getloggerid(line), theyear, getdoy(line, doypos,theloggerid), gethhmm(line,doypos), getprogid(line,doypos), getval(line)] | |
else: | |
pass | |
print(newline) | |
return(newline) | |
def iterate(remove_plus, itscampbell,file): | |
if itscampbell == 1: | |
print "its a campbell" | |
#lineset = [] | |
with open(file) as f: | |
for line in f: | |
line = line.split('+') | |
newline = remove_plus(getloggerid, getyear, getdoy, gethhmm, getprogid, getval, line,itscampbell) | |
# lineset.append(newline) | |
#return lineset | |
return newline | |
#def writefile(ofile,newline): | |
# with open(ofile,'w') as outfile: | |
# writer = csv.writer(outfile,quoting=csv.QUOTE_NONNUMERIC) | |
# writer.writerow(newline) | |
######################### | |
file = "MACK0159.099" | |
(itscampbell,line,f) = iscampbell(file) | |
print itscampbell | |
#newline = remove_plus(getloggerid, getprogid, getyear, getdoy, gethhmm, getval, line,itscampbell) | |
#lineset = iterate(remove_plus, itscampbell,file) | |
newline = iterate(remove_plus,itscampbell,file) | |
print newline | |
#print lineset | |
#if newline[0] == '110': | |
# print('the line is line 110') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment