def summary_df2obsdict(smry_df: pd.DataFrame) -> List[dict]: """Generate a dictionary structure suitable for yaml for summary observations in dataframe representation At level 2 in the obs-dict, "key" is the Eclipse summary vector name The label is an artificial label used in the ERT observation format, and is superfluous in the dict format. The "key" cannot in general be used as label, since it is not unique over dates, while the label has to be. Note: The labels in ert observation format is not possible to preserve while going through the dictionary format (because the way dates are rolled over). But a potential column LABEL will be included as "label". Args: sum_df Returns: List of dictionaries, each dict has "key" and "observation" """ assert isinstance(smry_df, pd.DataFrame) if "CLASS" in smry_df: assert len(smry_df["CLASS"].unique()) == 1 smry_df.drop("CLASS", axis=1, inplace=True) smry_obs_list = [] if isinstance(smry_df, pd.DataFrame): smry_df.dropna(axis=1, how="all", inplace=True) if "DATE" not in smry_df: raise ValueError("Can't have summary observation without a date") smry_df = convert_dframe_date_to_str(smry_df) if "KEY" not in smry_df: logger.warning( "KEY not provided when generating YAML for summary observations") logger.warning( "Using LABEL, but this might not be Eclipse summary vectors.") smry_df["KEY"] = smry_df["LABEL"] for smrykey, smrykey_df in smry_df.groupby("KEY"): smry_obs_element = {} smry_obs_element["key"] = smrykey if "COMMENT" in smrykey_df and not pd.isnull( smrykey_df["COMMENT"]).all(): smry_obs_element["comment"] = smrykey_df["COMMENT"].unique()[0] if isinstance(smrykey_df, pd.DataFrame): smrykey_df.drop("KEY", axis=1, inplace=True) if "SUBCOMMENT" in smrykey_df: smrykey_df["COMMENT"] = smrykey_df["SUBCOMMENT"] del smrykey_df["SUBCOMMENT"] observations = [ lowercase_dictkeys(dict(keyvalues.dropna())) for _, keyvalues in smrykey_df.iterrows() ] smry_obs_element["observations"] = observations smry_obs_list.append(smry_obs_element) return smry_obs_list
def block_df2obsdict(block_df: pd.DataFrame) -> List[dict]: """Generate a dictionary structure suitable for yaml for block observations in dataframe representation Args: block_df Returns: List of dictionaries, each dict has "well", "date" and "observations" """ assert isinstance(block_df, pd.DataFrame) block_obs_list = [] if "CLASS" in block_df: assert len(block_df["CLASS"].unique()) == 1 block_df.drop("CLASS", axis=1, inplace=True) if "DATE" not in block_df: raise ValueError("Can't have rft/block observation without a date") block_df = convert_dframe_date_to_str(block_df) block_df.dropna(axis=1, how="all", inplace=True) for blocklabel, blocklabel_df in block_df.groupby(["LABEL", "DATE"]): blocklabel_dict = {} if "WELL" not in blocklabel_df: blocklabel_dict["well"] = blocklabel[0] else: blocklabel_dict["well"] = blocklabel_df["WELL"].unique()[0] blocklabel_dict["label"] = blocklabel[0] blocklabel_dict["date"] = blocklabel[1] if "FIELD" in blocklabel_df: blocklabel_dict["field"] = blocklabel_df["FIELD"].unique()[0] if "COMMENT" in blocklabel_df: blocklabel_dict["comment"] = blocklabel_df["COMMENT"].unique()[0] # Now overwrite the COMMENT column in order to inject # the SUBCOMMENT at the lower level in the dict. if "SUBCOMMENT" in blocklabel_df: blocklabel_df["COMMENT"] = blocklabel_df["SUBCOMMENT"] blocklabel_dict["observations"] = [ lowercase_dictkeys(dict(keyvalues.dropna())) for _, keyvalues in blocklabel_df.drop( ["FIELD", "LABEL", "DATE", "WELL", "SUBCOMMENT"], axis=1, errors="ignore", ).iterrows() ] # if "subcomment" in blocklabel_dict: # blocklabel_dict["comment"] = blocklabel_dict.pop("subcomment") block_obs_list.append(blocklabel_dict) return block_obs_list