#!/user/bin/python
#
-# Define some simple data logging classes for consistency
+# data_logger - classes for consistently logging data in an organized
+# fasion. See the test functions for some usage examples
+#
+# Copyright (C) 2008, William Trevor King
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+# 02111-1307, USA.
+#
+# The author may be contacted at <wking@drexel.edu> on the Internet, or
+# write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
+# Philadelphia PA 19104, USA.
import os, os.path
import stat
import string
import numpy
+VERSION = "0.2"
+DEFAULT_PATH = "~/rsrch/data"
+DEFAULT_PATH_REPLACE_STRING = "$DEFAULT$/"
+
class error (Exception) :
"Basic module error class"
pass
class errorDirExists (error) :
"The specified directory already exists"
+def normalize_logdir(log_dir):
+ length = len(DEFAULT_PATH_REPLACE_STRING)
+ if log_dir[:length] == DEFAULT_PATH_REPLACE_STRING:
+ log_dir = os.path.join(DEFAULT_PATH, log_dir[length:])
+ log_dir = os.path.expanduser(log_dir)
+ return log_dir
+
class data_log :
"""
Data logging class.
Initialized with log_dir and log_name.
log_dir specifies the base data directory.
If it doesn't exist, log_dir is created.
+
+ If log_dir begins with '$DEFAULT$/', that portion of the path is replaced
+ with the then-current contents of the DEFAULT_PATH module global.
- A subdir of log_dir is created (if neccessary) named YYYYMMDD,
+ A subdir of log_dir is created (if necessary) named YYYYMMDD,
where YYYYMMDD is the current day in localtime.
If noclobber_logsubdir == True, this dir must not exist yet.
log_name specifies the base name for the created log files (in the log subdir).
The created log filenames are prefixed with a YYYYMMDDHHMMSS timestamp.
If the target filename already exists, the filename is postfixed with
- '_N', where N is the lowest integer that doesn't clober an existing file.
+ '_N', where N is the lowest integer that doesn't clobber an existing file.
General data is saved to the log files with the write(obj) method.
By default, write() cPickles the object passed.
cleanname = filename.translate(self.transtable, self.delete_chars)
return cleanname
def _create_logdir(self, log_dir) :
- log_dir = os.path.expanduser(log_dir)
+ log_dir = normalize_logdir(log_dir)
if not os.path.exists(log_dir) :
os.mkdir(log_dir, 0755)
return log_dir
def get_filename(self, timestamp=None) :
"""
Get a filename (using localtime if timestamp==None),
- appending integers as neccessary to avoid clobbering.
+ appending integers as necessary to avoid clobbering.
For use in write() routines.
Returns (filepath, timestamp)
"""
Load an object saved with data_log.write_binary()
The file-name must not have been altered.
"""
- raise Exception, "not implemented"
+ type = file.split("_")[-1]
+ if type == "float" :
+ t = numpy.float
+ else :
+ raise Exception, "read_binary() not implemented for type %s" % (type)
+ return numpy.fromfile(file, dtype=t)
def read_dict_of_arrays(self, basefile) :
"""
Load an object saved with data_log.write_binary()
raise error, "Saved %d uint16s, read %d" % (npts, len(data_in))
for i in range(npts) :
if data_in[i] != data[i] :
- print "Dissagreement in element %d" % i
+ print "Disagreement in element %d" % i
print "Saved %d, read back %d" % (data[i], data_in[i])
raise error, "Poorly saved"
os.remove(filepath)
data2_in = fromfile(filepath+"_data_2", dtype=uint16)
for i in range(npts) :
if data1_in[i] != data1[i] :
- print "Dissagreement in element %d of data1" % i
+ print "Disagreement in element %d of data1" % i
print "Saved %d, read back %d" % (data1[i], data1_in[i])
raise error, "Poorly saved"
if data2_in[i] != data2[i] :
- print "Dissagreement in element %d of data2" % i
+ print "Disagreement in element %d of data2" % i
print "Saved %d, read back %d" % (data2[i], data2_in[i])
raise error, "Poorly saved"
os.remove(filepath)