Commits

Roberto De Almeida committed 902bd6b

Added paging for mmap().

  • Participants
  • Parent commits 07b95f1

Comments (0)

Files changed (1)

 # -*- coding: utf-8 -*-
+from __future__ import division
+
 u"""
 NetCDF reader/writer module.
 
 
 
 from operator import mul
-from mmap import mmap, ACCESS_READ
+from mmap import mmap, ACCESS_READ, ALLOCATIONGRANULARITY
 
 import numpy as np
 from numpy.compat import asbytes, asstr
         format* and 2 means *64-bit offset format*.  Default is 1.  See
         `here <http://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Which-Format.html>`_
         for more info.
+    maskandscale : True or False
+        Whether data is automagically scaled and masked.
 
     """
     def __init__(self, filename, mode='r', mmap=None, version=1, maskandscale=False):
                 data = None
             else:  # not a record variable
                 # Calculate size to avoid problems with vsize (above)
-                a_size = reduce(mul, shape, 1) * type.itemsize
+                size = reduce(mul, shape, 1) * type.itemsize
                 pos = self.fp.tell()
                 if self.use_mmap:
-                    mm = mmap(self.fp.fileno(), begin_+a_size, access=ACCESS_READ)
+                    offset = begin_ - (begin_ % ALLOCATIONGRANULARITY)
+                    begin_ = begin_ % ALLOCATIONGRANULARITY
+                    mm = mmap(self.fp.fileno(), begin_+size, access=ACCESS_READ, offset=offset)
                     data = ndarray.__new__(ndarray, shape, dtype=type,
                             buffer=mm, offset=begin_, order=0)
                 else:
                     self.fp.seek(begin_)
-                    data = fromstring(self.fp.read(a_size), type)
+                    data = fromstring(self.fp.read(size), type)
                     data.shape = shape
                 self.fp.seek(pos)
 
             # Build rec array.
             pos = self.fp.tell()
             if self.use_mmap:
-                mm = mmap(self.fp.fileno(), begin+self._recs*self._recsize, access=ACCESS_READ)
+                offset = begin - (begin % ALLOCATIONGRANULARITY)
+                begin = begin % ALLOCATIONGRANULARITY
+                mm = mmap(self.fp.fileno(), begin+self._recs*self._recsize, access=ACCESS_READ, offset=offset)
                 rec_array = ndarray.__new__(ndarray, (self._recs,), dtype=dtypes,
                         buffer=mm, offset=begin, order=0)
             else:
     attributes : dict, optional
         Attribute values (any type) keyed by string names.  These attributes
         become attributes for the netcdf_variable object.
+    maskandscale: True or False
+        Whether data is automagically scaled and masked.
 
 
     Attributes