<div dir="ltr">you have to use the offset keyword argument of np.memmap, else it will always start from the beginning of the file<br><br>>>> np.memmap(fd, dtype="float32", mode="r", offset=offset)</div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Oct 10, 2013 at 2:43 PM, Andreas Hilboll <span dir="ltr"><<a href="mailto:lists@hilboll.de" target="_blank">lists@hilboll.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I have a problem using memmap correctly. I need to read a data file<br>
which consists of an ASCII header and appended binary single precision<br>
floating point values. memmap complains that the "Size of available data<br>
is not a multiple of the data-type size." But as far as I can tell, the<br>
size *doas* match the data-type size.<br>
<br>
The problem is illustrated by the following code:<br>
<br>
---8<-------<br>
<br>
url = "<a href="http://www.iup.uni-bremen.de/~hilboll/download/20120204.XD4_N2" target="_blank">http://www.iup.uni-bremen.de/~hilboll/download/20120204.XD4_N2</a>"<br>
localfile = "np_memmap.dat"<br>
<br>
import os<br>
import urllib<br>
<br>
import numpy as np<br>
<br>
<br>
# download data file<br>
if not os.access(localfile, os.R_OK):<br>
  urllib.urlretrieve(url, localfile)<br>
<br>
with open(localfile, "r") as fd:<br>
  # read offset from first line of file<br>
  offset = int(fd.readlines()[0].split()[-2])<br>
  # jump to begin of data block<br>
  fd.seek(offset)<br>
  # read until EOF<br>
  blob = fd.read()<br>
  print("Size of data blob [bytes]: {}".format(len(blob)))<br>
  print("This can actually be divided by 4: {} / 4.0 = {}".format(<br>
            len(blob), len(blob) / 4.0))<br>
  # go back to begin of data block<br>
  fd.seek(offset)<br>
  print("But it cannot be loaded as np.memmap with dtype float32:")<br>
  np.memmap(fd, dtype="float32", mode="r")<br>
<br>
---8<-------<br>
<br>
Any help is greatly appreciated :)<br>
<br>
-- Andreas.<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div><br></div>