예제 #1
0
def getvar(db, key):
    X = db[key]

    dims = []
    for x in X.dims:
        if len(x):
            dimcls = dimrep[cast_unicode(x[0].attrs.get("dimtype",
                                                        "DimSweep"))]
            dim = dimcls(x[0].name.strip("/"),
                         x[0][...],
                         unit=x[0].attrs.get("unit", None))
            dims.append(dim)

    unit = X.attrs.get("unit", None)
    outputformat = X.attrs.get("outputformat", None)
    dtype = X.attrs.get("dtype", None)
    if len(dims) == len(X.dims):
        return hfarray(X.value,
                       dims=dims,
                       unit=unit,
                       dtype=dtype,
                       outputformat=outputformat)
    else:
        dimcls = dimrep[X.attrs.get("dimtype", "DimSweep")]
        dim = dimcls(X.name.strip("/"), X[...], unit=unit)
        return hfarray(X.value,
                       dims=(dim, ),
                       unit=unit,
                       dtype=dtype,
                       outputformat=outputformat)
예제 #2
0
 def read_hdf5(h5file, name="datablock", **kw):
     with hdf5context(h5file) as fil:
         version = cast_unicode(fil.attrs.get("hftools file version", ""))
         if version == "0.2":
             return v_02.read_hdf5(fil, name=name, **kw)
         else:
             return v_01.read_hdf5(fil, name=name, **kw)
예제 #3
0
def read_hdf5(h5file, name="datablock", **kw):
    if isinstance(h5file, string_types):
        fil = h5py.File(h5file, "r")
    else:
        fil = h5file
    db = DataBlock()
    grp = fil[name]
    blockname = grp.attrs["Blockname"]
    if blockname.lower() == "none":
        blockname = None
    db.blockname = blockname
    comments = grp["Comments"]

    if "fullcomments" in comments and len(comments["fullcomments"]):
        db.comments = Comments([
            cast_unicode(x).strip() for x in np.array(comments["fullcomments"])
        ])
    else:
        db.comments = Comments()
    ivardata = grp["ivardata"]
    vardata = grp["vardata"]
    for k in ivardata:
        v = ivardata[k]
        datadtype = v.attrs[r"info\dtype"] or None
        dimcls = dims_dict.get(v.attrs[r"info\class"], DimRep)
        unit = str(v.attrs.get(r"info\unit", "none"))
        if unit.lower() == "none":
            unit = None
        vdata = np.array(np.array(v), dtype=datadtype)
        dim = dimcls(k, vdata, unit=unit)
        db[k] = dim
    for k in vardata:
        v = vardata[k]
        datadtype = v.attrs[r"data\dtype"] or None
        dims = tuple(db.ivardata[cast_unicode(dimname)]
                     for dimname in v.attrs[r"info\name"])
        unit = cast_unicode(v.attrs.get(r"data\unit", "none"))
        if unit.lower() == "none":
            unit = None
        db[k] = hfarray(np.array(v), dtype=datadtype, dims=dims, unit=unit)
    if isinstance(h5file, string_types):
        fil.close()

    if kw.get("property_to_vars", False):
        db.values_from_property()
    return db
예제 #4
0
파일: readbase.py 프로젝트: ychaim/hftools
 def read_file(cls,
               filename,
               make_complex=True,
               property_to_vars=True,
               guess_unit=True,
               normalize=True,
               make_matrix=False,
               merge=True,
               verbose=False,
               multiple_files=True,
               hyper=False,
               encoding="cp1252",
               **kw):
     obj = cls(make_complex=make_complex,
               property_to_vars=property_to_vars,
               guess_unit=guess_unit,
               normalize=normalize,
               make_matrix=make_matrix,
               merge=merge,
               verbose=verbose,
               hyper=hyper,
               **kw)
     obj.filename = filename
     if multiple_files:
         if isinstance(filename, (list, tuple)):
             filenames = []
             for f in filename:
                 filenames.extend(glob(f))
         else:
             filenames = glob(filename)
     else:
         filenames = [filename]
     filenames = [path(f) for f in filenames]
     objs = []
     if not filenames:
         raise IOError("Pattern %r did not match any files" % filename)
     for idx, fname in enumerate(filenames):
         if verbose:
             print("\r%-80s\r" % fname.basename(), end="")
         with io.open(fname, encoding=encoding) as fil:
             res = obj.do_file(fil)
             if multiple_files:
                 #res["FILEINDEX"] = DimPartial("FILEINDEX", [idx])
                 fname = py3.cast_unicode(fname)
                 if "FILENAME" not in res:
                     res["FILENAME"] = hfarray(fname)
             objs.append(res)
     if multiple_files:
         res = obj._merge(objs)
     else:
         res = objs
     if verbose:
         print("\r%80s\r" % "")
     return res
예제 #5
0
 def blockname(self):
     if self._blockname is None:
         if "FILENAME" in self:
             return cast_unicode(self.FILENAME.flat[0])
     return self._blockname