[yt-svn] commit/yt-3.0: MatthewTurk: Rewrite _read_vector to work with strings and files.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Mar 28 12:11:52 PDT 2013


1 new commit in yt-3.0:

https://bitbucket.org/yt_analysis/yt-3.0/commits/1d07aeeb15c1/
Changeset:   1d07aeeb15c1
Branch:      yt-3.0
User:        MatthewTurk
Date:        2013-03-28 20:11:33
Summary:     Rewrite _read_vector to work with strings and files.
Affected #:  1 file

diff -r 6c9860ffd783d43ff12fcd50c370309795eb2be2 -r 1d07aeeb15c1f20febd1691ae3047bbc5acca694 yt/utilities/fortran_utils.py
--- a/yt/utilities/fortran_utils.py
+++ b/yt/utilities/fortran_utils.py
@@ -117,18 +117,21 @@
     >>> f = open("fort.3", "rb")
     >>> rv = read_vector(f, 'd')
     """
-    fmt = endian+"%s" % d
-    size = struct.calcsize(fmt)
-    padfmt = endian + "I"
-    padsize = struct.calcsize(padfmt)
-    length = struct.unpack(padfmt,f.read(padsize))[0]
-    if length % size!= 0:
+    pad_fmt = "%sI" % (endian)
+    pad_size = struct.calcsize(pad_fmt)
+    vec_len = struct.unpack(pad_fmt,f.read(pad_size))[0] # bytes
+    vec_fmt = "%s%s" % (endian, d)
+    vec_size = struct.calcsize(vec_fmt)
+    if vec_len % vec_size != 0:
         print "fmt = '%s' ; length = %s ; size= %s" % (fmt, length, size)
         raise RuntimeError
-    count = length/ size
-    tr = np.fromfile(f,fmt,count=count)
-    length2= struct.unpack(padfmt,f.read(padsize))[0]
-    assert(length == length2)
+    vec_num = vec_len / vec_size
+    if isinstance(f, file): # Needs to be explicitly a file
+        tr = np.fromfile(f, vec_fmt, count=vec_num)
+    else:
+        tr = np.fromstring(f.read(vec_len), vec_fmt, count=vec_num)
+    vec_len2 = struct.unpack(pad_fmt,f.read(pad_size))[0]
+    assert(vec_len == vec_len2)
     return tr
 
 def skip(f, n=1, endian='='):

Repository URL: https://bitbucket.org/yt_analysis/yt-3.0/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list