Moved LICENSE -> COPYING. A more traditional name
[data_logger.git] / data_logger.py
index 1b6b0a1e154297085413e2b5c79a01f9edcfcb53..a901733cddcd4f59ddc74622e0de31eda30863fc 100644 (file)
@@ -1,7 +1,28 @@
 #!/user/bin/python
 #
-# Define some simple data logging classes for consistency
-# see the test functions for some usage examples
+# 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
@@ -10,6 +31,10 @@ import time
 import string
 import numpy
 
+VERSION = "0.2"
+DEFAULT_PATH = "~/rsrch/data"
+DEFAULT_PATH_REPLACE_STRING = "$DEFAULT$/"
+
 class error (Exception) :
     "Basic module error class"
     pass
@@ -17,6 +42,13 @@ class error (Exception) :
 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.
@@ -25,6 +57,9 @@ class data_log :
     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 necessary) named YYYYMMDD,
     where YYYYMMDD is the current day in localtime.
@@ -73,7 +108,7 @@ class data_log :
         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
@@ -169,7 +204,12 @@ class data_load :
         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()