Beispiel #1
0
    def __init__(
        self,
        name: str,
        provider: EnsembleSummaryProvider,
        vectors: List[str],
        expressions: Optional[List[ExpressionInfo]] = None,
        resampling_frequency: Optional[Frequency] = None,
        relative_date: Optional[datetime.datetime] = None,
    ) -> None:
        # Initialize base class
        super().__init__(provider.realizations())

        self._name = name
        self._provider = provider
        self._provider_vectors = [
            vector for vector in vectors if vector in self._provider.vector_names()
        ]
        self._per_interval_and_per_day_vectors = [
            vector
            for vector in vectors
            if is_per_interval_or_per_day_vector(vector)
            and get_cumulative_vector_name(vector) in provider.vector_names()
        ]
        self._vector_calculator_expressions = (
            get_selected_expressions(expressions, vectors)
            if expressions is not None
            else []
        )
        self._resampling_frequency = (
            resampling_frequency if self._provider.supports_resampling() else None
        )
        self._relative_date = relative_date
def create_leafnodetype_maps(
    leafnodes: pd.DataFrame, provider: EnsembleSummaryProvider,
    all_wells: List[str]
) -> Tuple[Dict[str, Any], Dict[str, Any], Dict[str, Any]]:
    """Returns three dictionaries classifying leaf nodes as producer,
    injector and/or other (f.ex observation well).

    Well leaf nodes are classified using WSTAT and group leaf nodes
    are classified using summary data.
    """
    # pylint: disable=too-many-locals
    is_prod_map, is_inj_map, is_other_map = {}, {}, {}
    wstat_df = provider.get_vectors_df([f"WSTAT:{well}" for well in all_wells],
                                       None)

    for _, leafnode in leafnodes.iterrows():
        nodename = leafnode["CHILD"]
        nodekeyword = leafnode["KEYWORD"]

        if nodekeyword == "WELSPECS":
            # The leaf node is a well
            wstat = wstat_df[f"WSTAT:{nodename}"].unique()
            is_prod_map[nodename] = 1 in wstat
            is_inj_map[nodename] = 2 in wstat
            is_other_map[nodename] = (1 not in wstat) and (2 not in wstat)
        else:
            # The leaf node is a group
            prod_sumvecs = [
                get_sumvec(datatype, nodename, nodekeyword)
                for datatype in ["oilrate", "gasrate", "waterrate"]
            ]
            inj_sumvecs = ([
                get_sumvec(datatype, nodename, nodekeyword)
                for datatype in ["waterinjrate", "gasinjrate"]
            ] if nodekeyword != "BRANPROP" else [])

            smry = provider.get_vectors_df(
                [
                    sumvec for sumvec in (prod_sumvecs + inj_sumvecs)
                    if sumvec in provider.vector_names()
                ],
                None,
            )

            sumprod = sum([
                smry[sumvec].sum() for sumvec in prod_sumvecs
                if sumvec in smry.columns
            ])

            suminj = sum([
                smry[sumvec].sum() for sumvec in inj_sumvecs
                if sumvec in smry.columns
            ])

            is_prod_map[nodename] = sumprod > 0
            is_inj_map[nodename] = suminj > 0
            is_other_map[nodename] = (sumprod == 0) and (suminj == 0)
    return is_prod_map, is_inj_map, is_other_map
    def __init__(
        self,
        ensemble_name: str,
        provider: EnsembleSummaryProvider,
        gruptree_model: GruptreeModel,
        well_attributes_model: WellAttributesModel,
        filter_out_startswith: Optional[str] = None,
    ):
        self._ensemble_name = ensemble_name
        self._gruptree_model = gruptree_model
        self._well_attributes_model = well_attributes_model
        self._provider = provider
        self._vector_names = self._provider.vector_names()
        self._realizations = self._provider.realizations()
        self._wells: List[str] = [
            vec.split(":")[1] for vec in self._vector_names
            if vec.startswith("WOPT:")
        ]
        if filter_out_startswith is not None:
            self._wells = [
                well for well in self._wells
                if not well.startswith(filter_out_startswith)
            ]

        well_sumvecs = [
            vec for vec in self._vector_names if vec.startswith("W")
        ]
        group_sumvecs = [
            vec for vec in self._vector_names if vec.startswith("GPR:")
        ]
        self._smry = provider.get_vectors_df(well_sumvecs + group_sumvecs,
                                             None)
def create_calculated_vector_df(
    expression: ExpressionInfo,
    provider: EnsembleSummaryProvider,
    realizations: Optional[Sequence[int]],
    resampling_frequency: Optional[Frequency],
) -> pd.DataFrame:
    """Create dataframe with calculated vector from expression

    If expression is not successfully evaluated, empty dataframe is returned

    `Return:`
    * Dataframe with calculated vector data made form expression - columns:\n
        ["DATE","REAL", calculated_vector]
    * Return empty dataframe if expression evaluation returns None
    """
    name: str = expression["name"]
    expr: str = expression["expression"]

    variable_vector_dict: Dict[str,
                               str] = VectorCalculator.variable_vector_dict(
                                   expression["variableVectorMap"])
    vector_names = list(variable_vector_dict.values())

    # Retrieve data for vectors in expression
    vectors_df = provider.get_vectors_df(vector_names, resampling_frequency,
                                         realizations)

    values: Dict[str, np.ndarray] = {}
    for variable, vector in variable_vector_dict.items():
        values[variable] = vectors_df[vector].values

    evaluated_expression = VectorCalculator.evaluate_expression(expr, values)
    if evaluated_expression is not None:
        vectors_df[name] = evaluated_expression
        return vectors_df[["DATE", "REAL", name]]

    return pd.DataFrame()
Beispiel #5
0
def create_history_vectors_df(
    provider: EnsembleSummaryProvider,
    vector_names: List[str],
    resampling_frequency: Optional[Frequency],
) -> pd.DataFrame:
    """Get dataframe with existing historical vector data for provided vectors.

    The returned dataframe contains columns with name of vector and corresponding historical
    data

    `Input:`
    * ensemble: str - Ensemble name
    * vector_names: List[str] - list of vectors to get historical data for
    [vector1, ... , vectorN]

    `Output:`
    * dataframe with non-historical vector names in columns and their historical data in rows.
    `Columns` in dataframe: ["DATE", "REAL", vector1, ..., vectorN]

    ---------------------
    `NOTE:`
    * Raise ValueError if vector does not exist for ensemble
    * If historical data does not exist for provided vector, vector is excluded from
    the returned dataframe.
    * Column names are not the historical vector name, but the original vector name,
    i.e. `WOPTH:OP_1` data is placed in colum with name `WOPT:OP_1`
    """
    if len(vector_names) < 1:
        raise ValueError("Empty list of vector names!")

    provider_vectors = provider.vector_names()
    resampling_frequency = (resampling_frequency
                            if provider.supports_resampling() else None)

    # Verify for provider
    for elm in vector_names:
        if elm not in provider_vectors:
            raise ValueError(
                f'Vector "{elm}" not present among vectors for provider')

    # Dict with historical vector name as key, and non-historical vector name as value
    historical_vector_and_vector_name_dict: Dict[str, str] = {}
    for vector in vector_names:
        # TODO: Create new historical_vector according to new provider metadata?
        historical_vector_name = historical_vector(vector=vector,
                                                   smry_meta=None)
        if historical_vector_name and historical_vector_name in provider.vector_names(
        ):
            historical_vector_and_vector_name_dict[
                historical_vector_name] = vector

    # Get lowest valid realization number
    realization = min(provider.realizations(), default=None)
    if not historical_vector_and_vector_name_dict or realization is None:
        return pd.DataFrame()

    historical_vector_names = list(
        historical_vector_and_vector_name_dict.keys())
    historical_vectors_df = provider.get_vectors_df(historical_vector_names,
                                                    resampling_frequency,
                                                    realizations=[realization])
    return historical_vectors_df.rename(
        columns=historical_vector_and_vector_name_dict)