Exemplo n.º 1
0
    def env_vars(self):
        """Get env var definitions for:

            - The path to the preprocessed data file for this variable,
            - The name for this variable in that data file,
            - The names for all of this variable's coordinate axes in that file,
            - The names of the bounds variables for all of those coordinate
              dimensions, if provided by the data.

        """
        if self.status != core.ObjectStatus.SUCCEEDED:
            # Signal to POD's code that vars are not provided by setting
            # variable to the empty string.
            return {self.env_var: "", self.path_variable: ""}

        assert self.dest_path
        d = util.ConsistentDict()
        d.update({
            self.env_var: self.name_in_model,
            self.path_variable: self.dest_path
        })
        for ax, dim in self.dim_axes.items():
            trans_dim = self.translation.dim_axes[ax]
            d[dim.name + _coord_env_var_suffix] = trans_dim.name
            if trans_dim.has_bounds:
                d[dim.name + _coord_bounds_env_var_suffix] = trans_dim.bounds
        return d
Exemplo n.º 2
0
    def __post_init__(self, coords=None):
        # inherited from two dataclasses, so need to call post_init on each directly
        core.MDTFObjectBase.__post_init__(self)
        # set up log (VarlistEntryLoggerMixin)
        self.init_log()
        data_model.DMVariable.__post_init__(self, coords)

        # (re)initialize mutable fields here so that if we copy VE (eg with .replace)
        # the fields on the copy won't point to the same object as the fields on
        # the original.
        self.translation = None
        self.data: util.ConsistentDict()
        # activate required vars
        if self.status == core.ObjectStatus.NOTSET:
            if self.requirement == VarlistEntryRequirement.REQUIRED:
                self.status = core.ObjectStatus.ACTIVE
            else:
                self.status = core.ObjectStatus.INACTIVE

        # env_vars
        if not self.env_var:
            self.env_var = self.name + _var_name_env_var_suffix
        if not self.path_variable:
            self.path_variable = self.name.upper() + _file_env_var_suffix

        # self.alternates is either [] or a list of nonempty lists of VEs
        if self.alternates:
            if not isinstance(self.alternates[0], list):
                self.alternates = [self.alternates]
            self.alternates = [vs for vs in self.alternates if vs]
Exemplo n.º 3
0
    def query_attrs(self, key_synonyms=None):
        """Returns a dict of attributes relevant for DataSource.query_dataset()
        (ie, which describe the variable itself and aren't specific to the
        MDTF implementation.)
        """
        if key_synonyms is None:
            key_synonyms = dict()

        def iter_query_attrs(obj):
            """Recursive generator yielding name:value pairs for all dataclass
            fields marked with query attribute in their metadata.
            """
            for f in dc.fields(obj):
                val = getattr(obj, f.name, None)
                if dc.is_dataclass(val):
                    yield from iter_query_attrs(val)
                if f.metadata.get('query', False):
                    key = key_synonyms.get(f.name, f.name)
                    yield (key, val)

        d = util.ConsistentDict()
        d.update(dict(iter_query_attrs(self)))
        for dim in self.dims:
            d.update(dict(iter_query_attrs(dim)))
        return d
Exemplo n.º 4
0
    def setup(self, data_mgr, pod):
        """Make a lookup table to map :class:`~diagnostic.VarlistEntry` IDs to
        the set of metadata that we need to alter.

        If user has provided the name of variable used by the data files (via the
        ``var_name`` attribute), set that as the translated variable name.
        Otherwise, variables are untranslated, and we use the herusitics in
        :meth:`xr_parser.DefaultDatasetParser.guess_dependent_var` to determine
        the name.
        """
        super(MetadataRewriteParser, self).setup(data_mgr, pod)

        for var in pod.iter_children():
            # set user-supplied translated name
            # note: currently have to do this here, rather than in setup_var(),
            # because query for this data source relies on the *un*translated
            # name (ie, the POD's name for the var) being set in the translated
            # name attribute.
            if pod.name in data_mgr._config \
                and var.name in data_mgr._config[pod.name]:
                translated_name = data_mgr._config[pod.name][var.name].var_name
                if translated_name:
                    var.translation.name = translated_name
                    var.log.debug(("Set translated name of %s to user-specified "
                        "value '%s'."), var.full_name, translated_name)

            # add var's info to lookup table of metadata changes
            new_metadata = util.ConsistentDict()
            for d_key in var.data.values():
                idxs = list(d_key.value)
                glob_ids = data_mgr.df['glob_id'].loc[idxs].to_list()
                for id_ in glob_ids:
                    entry = data_mgr.config_by_id[id_]
                    new_metadata.update(entry.metadata)
            self.id_lut[var._id] = new_metadata