Ejemplo n.º 1
0
 def _del(self, key, **kw_args):
     ext.hcdir(self.__rz_path, " ")
     # Kill the sucka.
     if self.isdir(key):
         ext.hddir(key)
         # Update the subdirectory cache.
         if key in self.__subdirs:
             # The directory no longer exists, so don't try to purge
             # cycles when cleaning it up.
             self.__subdirs[key].purge_cycles = False
             # Uncahce it.
             del self.__subdirs[key]
     else:
         # We need its RZ ID to delete it.
         rz_id = self.__info[key].rz_id
         ext.hscr(rz_id, 0, " ")
     # Update the info cache.
     del self.__info[key]
Ejemplo n.º 2
0
def _hlnext(rz_path):
    """An iterator over entries in a directory.

    'path' -- The RZ path to the directory.

    yields -- '(key, rz_id, type)' for each element in the directory."""

    ext.hcdir(rz_path, " ")
    rz_id = 0
    while True:
        rz_id, rz_type, key = ext.hlnext(rz_id, "12ND")
        if rz_id == 0:
            raise StopIteration
        # Always use lower-case names.
        key = key.lower()
        yield key, rz_id, {
            "1": "1D histogram",
            "2": "2D histogram",
            "N": "table",
            "D": "directory",
        }[rz_type]
Ejemplo n.º 3
0
    def _set(self, key, value, **kw_args):
        # Was the RZ ID specified as a keyword argument?
        rz_id = kw_args.get("rz_id", None)
        if rz_id is None:
            if hasattr(object, "rz_id"):
                # The object has an 'rz_id' attribute, so use its value.
                rz_id = int(object.rz_id)
            else:
                # No RZ ID specified; choose an ID.
                rz_id = self._make_rz_id()

        if hep.isMap(value):
            type = "directory"
            # Make a directory.
            ext.hcdir(self.__rz_path, " ")
            ext.hmdir(key, " ")
            # Fill in the initial entries, if some where specified.
            if len(value) > 0:
                self.get(key, **kw_args).update(value, **kw_args)
            # Invalidate the directory cache.
            if key in self.__subdirs:
                del self.__subdirs[key]

        elif hep.hist.isHistogram(value):
            # Make the histogram object in PAWC memory.
            if value.dimensions == 1:
                type = "1D histogram"
                self._make1DHistogram(value, key, rz_id)
            elif value.dimensions == 2:
                type = "2D histogram"
                self._make2DHistogram(value, key, rz_id)
            else:
                raise NotImplementedError, \
                      "cannot save an %d-D histogram in an HBOOK file" \
                      % dimensions
            # Save the histogram to the HBOOK file.
            ext.hcdir(self.__rz_path, " ")
            ext.hrout(rz_id, " ")
            # Clean up the in-memory PAW histogram.
            ext.hcdir("//PAWC", " ")
            ext.hdelet(rz_id)

        else:
            raise NotImplementedError, \
                  "cannot save a %s in an HBOOK file" \
                  % type(value).__name__

        # Update the info cache.
        self.__info[key] = self.__makeinfo(key, rz_id, type)
Ejemplo n.º 4
0
    def _get(self, key, **kw_args):
        # Look up the specified path.
        info = self.__info[key]

        if info.type == "directory":
            # It's a directory.  Return a 'Directory' object, checking
            # the cache first.
            if not self.__subdirs.has_key(key):
                self.__subdirs[key] = Directory(
                    self.__file, join(self.__path, key), self.purge_cycles)
            return self.__subdirs[key]

        # Otherwise, load it into memory.
        ext.hcdir(self.__rz_path, " ")
        ext.hrin(info.rz_id, 999999, 0)

        # Construct a Python object for it.
        if info.type == "table":
            # Build a table handle.
            table =  ext.openTuple(
                info.rz_id, self.__rz_path, self.writable)
            # Associate this file with this table, so that the file
            # isn't closed while the table handle still exists.
            table.file = self
            return table

        elif info.type == "1D histogram":
            histogram = self._read1DHistogram(info.rz_id)
            # Remove the HBOOK object from memory.
            ext.hcdir("//PAWC", " ")
            ext.hdelet(info.rz_id)
            # Return the histogram.
            return histogram

        elif info.type == "2D histogram":
            histogram = self._read2DHistogram(info.rz_id)
            # Remove the HBOOK object from memory.
            ext.hcdir("//PAWC", " ")
            ext.hdelet(info.rz_id)
            # Return the histogram.
            return histogram

        else:
            raise NotImplementedError, "type '%s'" % info.type
Ejemplo n.º 5
0
 def __del__(self):
     # Purge cycles now, if selected.
     if self.purge_cycles:
         ext.hcdir(self.__rz_path, " ")
         ext.rzpurg(1)