snapshot.py 2.22 KB
Newer Older
mashun1's avatar
veros  
mashun1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import copy

from veros import time, logger
from veros.diagnostics.base import VerosDiagnostic


DEFAULT_OUTPUT_VARS = [
    "dxt",
    "dxu",
    "dyt",
    "dyu",
    "zt",
    "zw",
    "dzt",
    "dzw",
    "ht",
    "hu",
    "hv",
    "beta",
    "area_t",
    "area_u",
    "area_v",
    "rho",
    "prho",
    "int_drhodT",
    "int_drhodS",
    "Nsqr",
    "Hd",
    "temp",
    "salt",
    "forc_temp_surface",
    "forc_salt_surface",
    "u",
    "v",
    "w",
    "p_hydro",
    "kappaM",
    "kappaH",
    "surface_taux",
    "surface_tauy",
    "forc_rho_surface",
    "psi",
    "isle",
    "psin",
    "xt",
    "xu",
    "yt",
    "yu",
    "temp_source",
    "salt_source",
    "u_source",
    "v_source",
    "tke",
    "forc_tke_surface",
    "eke",
    "E_iw",
    "forc_iw_surface",
    "forc_iw_bottom",
]


class Snapshot(VerosDiagnostic):
    """Writes snapshots of the current solution. Also reads and writes the main restart
    data required for restarting a Veros simulation.
    """

    output_path = "{identifier}.snapshot.nc"
    """File to write to. May contain format strings that are replaced with Veros attributes."""
    name = "snapshot"  #:
    output_frequency = None  #: Frequency (in seconds) in which output is written.

    def __init__(self, state):
        self.output_variables = []

        for var in DEFAULT_OUTPUT_VARS:
            active = state.var_meta[var].active
            if callable(active):
                active = active(state.settings)

            if active:
                self.output_variables.append(var)

    def initialize(self, state):
        vs = state.variables

        self.var_meta = {var: copy.copy(state.var_meta[var]) for var in self.output_variables}
        for var in self.var_meta.values():
            var.write_to_restart = False

        self.variables = vs
        self.initialize_output(state)

    def diagnose(self, state):
        pass

    def output(self, state):
        vs = state.variables

        time_length, time_unit = time.format_time(vs.time)
        logger.info(f" Writing snapshot at {time_length:.2f} {time_unit}")

        if not os.path.isfile(self.get_output_file_name(state)):
            self.initialize_output(state)

        self.write_output(state)