Skip to content

Save

el_paso.save

Attributes

Classes

Functions:

el_paso.save.save

save

Saves variables to files based on the specified saving strategy and time intervals.

This function iterates through defined time intervals, calling the saving strategy to save a set of variables to corresponding files. It handles variable filtering and includes a check for missing data.

Parameters:

Name Type Description Default
variables_dict dict[str, Variable]

A dictionary mapping variable names to their Variable objects to be saved.

required
saving_strategy SavingStrategy

The strategy object that defines how to organize, standardize, and save the data (e.g., file paths, formats).

required
start_time datetime

The start of the overall time range for which data should be saved.

required
end_time datetime

The end of the overall time range.

required
time_var Variable

The variable representing time. If not provided, the saving strategy must handle time internally. Defaults to None.

None
append bool

If True, data will be appended to existing files rather than overwriting them. Defaults to False.

False
ignore_validation bool

If True, validation of the input against the ep.typing.InternalName variables will be skipped. Defaults to False.

False

Raises:

Type Description
TypeError

If variables_dict is not a dictionary of Variable objects.

KeyError

If variables_dict contains invalid internal variable names.

UserWarning

If the saving process is attempted for an output file but one or more of its required variables are missing from variables_dict.

Source code in el_paso/save.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@timed_function()
def save(
    variables_dict: dict[InternalName, Variable],
    saving_strategy: SavingStrategy,
    start_time: datetime,
    end_time: datetime,
    time_var: Variable | None = None,
    *,
    append: bool = False,
    ignore_validation: bool = False,
) -> None:
    """Saves variables to files based on the specified saving strategy and time intervals.

    This function iterates through defined time intervals, calling the saving strategy
    to save a set of variables to corresponding files. It handles variable filtering
    and includes a check for missing data.

    Args:
        variables_dict (dict[str, Variable]): A dictionary mapping variable names to their
            `Variable` objects to be saved.
        saving_strategy (SavingStrategy): The strategy object that defines how to
            organize, standardize, and save the data (e.g., file paths, formats).
        start_time (datetime): The start of the overall time range for which data
            should be saved.
        end_time (datetime): The end of the overall time range.
        time_var (Variable, optional): The variable representing time. If not provided,
            the saving strategy must handle time internally. Defaults to None.
        append (bool, optional): If `True`, data will be appended to existing files
            rather than overwriting them. Defaults to `False`.
        ignore_validation (bool, optional): If `True`, validation of the input against the `ep.typing.InternalName`
            variables will be skipped. Defaults to `False`.

    Raises:
        TypeError: If `variables_dict` is not a dictionary of `Variable` objects.
        KeyError: If `variables_dict` contains invalid internal variable names.
        UserWarning: If the saving process is attempted for an output file but one
            or more of its required variables are missing from `variables_dict`.
    """
    if not ignore_validation:
        _validate_variables_dict(variables_dict)

    start_time = enforce_utc_timezone(start_time)
    end_time = enforce_utc_timezone(end_time)

    time_intervals_to_save = saving_strategy.get_time_intervals_to_save(start_time, end_time)

    for interval_start, interval_end in time_intervals_to_save:
        for output_file in saving_strategy.output_files:
            file_path = saving_strategy.get_file_path(interval_start, interval_end, output_file)

            target_variables = saving_strategy.get_target_variables(
                output_file, variables_dict, time_var, interval_start, interval_end
            )

            if target_variables is None:
                logger.warning(
                    f"Saving attempted, but product is missing some required variables for output {output_file.name}!",
                    stacklevel=2,
                )
            else:
                data_dict = _get_data_dict_to_save(target_variables)
                saving_strategy.save_single_file(file_path, data_dict, append=append)
                file_path.chmod(0o660)