Esempio n. 1
0
 def _parse_parameter_file(self):
     fh = h5py.File(self.filename, "r")
     for attr in ["hubble_constant", "omega_matter", "omega_lambda"]:
         setattr(self, attr, fh.attrs[attr])
     if "unit_registry_json" in fh.attrs:
         self.unit_registry = \
           UnitRegistry.from_json(
               fh.attrs["unit_registry_json"].astype(str))
     self.unit_registry.modify("h", self.hubble_constant)
     self.box_size = _hdf5_yt_attr(fh,
                                   "box_size",
                                   unit_registry=self.unit_registry)
     field_list = []
     fi = {}
     for field in fh["data"]:
         d = fh["data"][field]
         units = _hdf5_yt_attr(d, "units")
         if isinstance(units, bytes):
             units = units.decode("utf")
         if len(d.shape) > 1:
             for ax in "xyz":
                 my_field = "%s_%s" % (field, ax)
                 field_list.append(my_field)
                 fi[my_field] = {"vector": True, "units": units}
         else:
             field_list.append(field)
             fi[field] = {"units": units}
     fh.close()
     self.field_list = field_list
     self.field_info.update(fi)
Esempio n. 2
0
    def _parse_parameter_file(self):
        self.refine_by = 2
        with h5py.File(self.parameter_filename, mode="r") as f:
            for key in f.attrs.keys():
                v = parse_h5_attr(f, key)
                if key == "con_args":
                    try:
                        v = eval(v)
                    except ValueError:
                        # support older ytdata outputs
                        v = v.astype('str')
                self.parameters[key] = v
            self._with_parameter_file_open(f)

        # if saved, restore unit registry from the json string
        if "unit_registry_json" in self.parameters:
            self.unit_registry = UnitRegistry.from_json(
                self.parameters["unit_registry_json"])
            # reset self.arr and self.quan to use new unit_registry
            self._arr = None
            self._quan = None
            for dim in [
                    "length", "mass", "pressure", "temperature", "time",
                    "velocity"
            ]:
                cu = "code_" + dim
                if cu not in self.unit_registry:
                    self.unit_registry.add(cu, 1.0, getattr(dimensions, dim))
            if "code_magnetic" not in self.unit_registry:
                self.unit_registry.add("code_magnetic", 1.0,
                                       dimensions.magnetic_field)

        # if saved, set unit system
        if "unit_system_name" in self.parameters:
            unit_system = self.parameters["unit_system_name"]
            del self.parameters["unit_system_name"]
        else:
            unit_system = "cgs"
        # reset unit system since we may have a new unit registry
        self._assign_unit_system(unit_system)

        # assign units to parameters that have associated unit string
        del_pars = []
        for par in self.parameters:
            ustr = "%s_units" % par
            if ustr in self.parameters:
                if isinstance(self.parameters[par], np.ndarray):
                    to_u = self.arr
                else:
                    to_u = self.quan
                self.parameters[par] = to_u(self.parameters[par],
                                            self.parameters[ustr])
                del_pars.append(ustr)
        for par in del_pars:
            del self.parameters[par]

        for attr in self._con_attrs:
            setattr(self, attr, self.parameters.get(attr))
Esempio n. 3
0
    def _parse_parameter_file(self):
        fh = h5py.File(self.filename, "r")

        for attr in ["hubble_constant", "omega_matter", "omega_lambda"]:
            setattr(self, attr, fh.attrs[attr])

        my_ur = UnitRegistry.from_json(parse_h5_attr(fh, "unit_registry_json"))
        right = _hdf5_yt_attr(fh, "domain_right_edge", unit_registry=my_ur)
        left = _hdf5_yt_attr(fh, "domain_left_edge", unit_registry=my_ur)
        # Drop the "cm" suffix because all lengths will
        # be in comoving units.
        self.box_size = self.quan((right - left)[0].to("Mpccm/h"), "Mpc/h")
        fh.close()
Esempio n. 4
0
 def _parse_parameter_file(self):
     self._prefix = \
       self.filename[:self.filename.rfind(self._suffix)]
     fh = h5py.File(self.filename, "r")
     for attr in ["hubble_constant", "omega_matter", "omega_lambda"]:
         setattr(self, attr, fh.attrs[attr])
     if "unit_registry_json" in fh.attrs:
         self.unit_registry = \
           UnitRegistry.from_json(
               parse_h5_attr(fh, "unit_registry_json"))
     self.box_size = _hdf5_yt_attr(fh,
                                   "box_size",
                                   unit_registry=self.unit_registry)
     self.field_info.update(json.loads(parse_h5_attr(fh, "field_info")))
     self.field_list = list(self.field_info.keys())
     fh.close()
Esempio n. 5
0
    def _parse_parameter_file(self):
        self.refine_by = 2
        with h5py.File(self.parameter_filename, mode="r") as f:
            for key in f.attrs.keys():
                v = parse_h5_attr(f, key)
                if key == "con_args":
                    try:
                        v = eval(v)
                    except ValueError:
                        # support older ytdata outputs
                        v = v.astype("str")
                    except NameError:
                        # This is the most common error we expect, and it
                        # results from having the eval do a concatenated decoded
                        # set of the values.
                        v = [_.decode("utf8") for _ in v]
                self.parameters[key] = v
            self._with_parameter_file_open(f)

        # if saved, restore unit registry from the json string
        if "unit_registry_json" in self.parameters:
            self.unit_registry = UnitRegistry.from_json(
                self.parameters["unit_registry_json"])
            # reset self.arr and self.quan to use new unit_registry
            self._arr = None
            self._quan = None
            for dim in [
                    "length",
                    "mass",
                    "pressure",
                    "temperature",
                    "time",
                    "velocity",
            ]:
                cu = "code_" + dim
                if cu not in self.unit_registry:
                    self.unit_registry.add(cu, 1.0, getattr(dimensions, dim))
            if "code_magnetic" not in self.unit_registry:
                self.unit_registry.add("code_magnetic", 1.0,
                                       dimensions.magnetic_field)

        # if saved, set unit system
        if "unit_system_name" in self.parameters:
            unit_system = self.parameters["unit_system_name"]
            del self.parameters["unit_system_name"]
        else:
            unit_system = "cgs"
        # reset unit system since we may have a new unit registry
        self._assign_unit_system(unit_system)

        # assign units to parameters that have associated unit string
        del_pars = []
        for par in self.parameters:
            ustr = f"{par}_units"
            if ustr in self.parameters:
                if isinstance(self.parameters[par], np.ndarray):
                    to_u = self.arr
                else:
                    to_u = self.quan
                self.parameters[par] = to_u(self.parameters[par],
                                            self.parameters[ustr])
                del_pars.append(ustr)
        for par in del_pars:
            del self.parameters[par]

        for attr in self._con_attrs:
            try:
                sattr = _set_attrs.get(attr, attr)
                setattr(self, sattr, self.parameters.get(attr))
            except TypeError:
                # some Dataset attributes are properties with setters
                # which may not accept None as an input
                pass

        if self.geometry is None:
            self.geometry = "cartesian"
Esempio n. 6
0
 def hubble_constant(self, value):
     self._hubble_constant = value
     # reset the unit registry lut while preserving other changes
     self.unit_registry = UnitRegistry.from_json(
         self.unit_registry.to_json())
     self.unit_registry.modify("h", self.hubble_constant)
Esempio n. 7
0
def test_registry_json():
    reg = UnitRegistry()
    json_reg = reg.to_json()
    unserialized_reg = UnitRegistry.from_json(json_reg)

    assert_equal(reg.lut, unserialized_reg.lut)