Datasets:

Languages:
English
ArXiv:
License:
File size: 7,030 Bytes
0ea62c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import argparse
import glob
import os

import dedalus.public as d3
import h5py as h5
import numpy as np


def populate_empty_file(file):
    create_dimensions(file)
    create_base_attributes(file)
    create_field_types(file)


def create_boundary_conditions(file):
    bcs = file.create_group("boundary_conditions")
    x = bcs.create_group("x_periodic")
    x.attrs["associated_dims"] = ["phi"]
    x.attrs["bc_type"] = "PERIODIC"
    x.attrs["associated_fields"] = []
    x.attrs["sample_varying"] = False
    x.attrs["time_varying"] = False
    mask = np.zeros_like(file["dimensions"]["phi"], dtype=bool)
    mask[0] = True
    mask[-1] = True
    xds = x.create_dataset("mask", data=mask, dtype=bool)

    y = bcs.create_group("y_open")
    y.attrs["associated_dims"] = ["theta"]
    y.attrs["bc_type"] = "OPEN"
    y.attrs["associated_fields"] = []
    mask = np.zeros_like(file["dimensions"]["theta"], dtype=bool)
    mask[0] = True
    mask[-1] = True
    yds = y.create_dataset("mask", data=mask, dtype=bool)
    y.attrs["sample_varying"] = False
    y.attrs["time_varying"] = False


def create_base_attributes(file):
    file.attrs["dataset_name"] = "dataset"
    file.attrs["n_spatial_dims"] = 3
    file.attrs["simulation_parameters"] = []
    file.attrs["grid_type"] = "cartesian"


def create_field_types(file):
    field_types = ["t0_fields", "t1_fields", "t2_fields", "scalars"]
    for field_type in field_types:
        gr = file.create_group(field_type)
        gr.attrs["field_names"] = []


def create_dimensions(file):
    file.create_group("dimensions")
    file["dimensions"].attrs["spatial_dims"] = ["phi", "theta"]
    # file['dimensions'].create_dataset('time', data=np.array([0]))


def earthswe_to_well(in_path, out_path):
    print("Starting file copy!")
    orig_file = h5.File(in_path, "r")

    print("orig keys", list(orig_file.keys()))
    if os.path.exists(out_path):
        os.remove(out_path)
    with h5.File(out_path, "w") as new_file:
        populate_empty_file(new_file)
        ## First populate the attributes
        new_file.attrs["dataset_name"] = "planetswe"
        new_file.attrs["n_spatial_dims"] = 2
        new_file.attrs["simulation_parameters"] = []
        new_file.attrs["grid_type"] = "equiangular"
        print("orig_file", orig_file.keys())
        new_file.attrs["n_trajectories"] = 1  # orig_file['c'].shape[0]
        # Make attributes for each simulation parameter
        # parameter_string = in_path.split('/')[-1][:-5].split('_')
        # print(parameter_string)
        new_file["scalars"].attrs["field_names"] = new_file.attrs[
            "simulation_parameters"
        ]
        # Now let's populate the dimensions
        new_file["dimensions"].attrs["spatial_dims"] = ["theta", "phi"]
        time = new_file["dimensions"].create_dataset(
            "time", data=orig_file["scales"]["sim_time"], dtype="f4"
        )
        time.attrs["sample_varying"] = False
        # Same coordinates for x and y in this specific data
        print(orig_file["scales"].keys())
        d = new_file["dimensions"].create_dataset(
            "phi",
            data=orig_file["scales"][
                "phi_hash_7b8ec7cabc40ac4b596a5ef833e9eab019f07d46"
            ],
            dtype="f4",
        )
        d.attrs["time_varying"] = False
        d.attrs["sample_varying"] = False
        d = new_file["dimensions"].create_dataset(
            "theta",
            data=orig_file["scales"][
                "theta_hash_47f1a1c5acad69381fef2149e23fb804716211f6"
            ],
            dtype="f4",
        )
        d.attrs["time_varying"] = False
        d.attrs["sample_varying"] = False

        h, u = dedalus_interpolate(
            orig_file["tasks"]["h"][:], orig_file["tasks"]["u"][:]
        )
        # T0 Data
        new_file["t0_fields"].attrs["field_names"] = ["height"]
        f = new_file["t0_fields"].create_dataset(
            "height", data=np.transpose(h[np.newaxis, ...], (0, 1, 3, 2)), dtype="f4"
        )
        f.attrs["time_varying"] = True
        f.attrs["sample_varying"] = True
        f.attrs["dim_varying"] = [True, True]

        # T1 Data
        new_file["t1_fields"].attrs["field_names"] = ["velocity"]
        f = new_file["t1_fields"].create_dataset(
            "velocity",
            data=np.transpose(u[np.newaxis, ...], (0, 1, 4, 3, 2)),
            dtype="f4",
        )
        f.attrs["time_varying"] = True
        f.attrs["sample_varying"] = True
        f.attrs["dim_varying"] = [True, True]
        # T2 Data
        new_file["t2_fields"].attrs["field_names"] = []

        create_boundary_conditions(new_file)


def dedalus_interpolate(h, u):
    meter = 1 / 6.37122e6
    hour = 1
    second = hour / 3600
    g = 9.80616 * meter / second**2
    Nphi = 512
    Ntheta = 256
    dtype = np.float64
    coords = d3.S2Coordinates("phi", "theta")
    dist = d3.Distributor(coords, dtype=dtype)
    basis = d3.SphereBasis(coords, (Nphi, Ntheta), radius=1, dealias=1, dtype=dtype)
    h3 = dist.Field(name="h", bases=basis)
    u3 = dist.VectorField(coords, name="u", bases=basis)

    nphi = h.shape[1]
    ntheta = h.shape[2]
    u_out = np.zeros(u.shape)
    h_out = np.zeros(h.shape)
    delta = np.pi / (ntheta + 1)
    for j in range(u.shape[0]):
        if j % 50 == 0:
            print("row", j)
        u3["g"] = u[j]
        h3["g"] = h[j]
        print("field shape!", u3["g"].shape)

        for i, pt in enumerate(np.linspace(np.pi - delta / 2, delta / 2, ntheta)):
            u_interp = d3.Interpolate(u3, "theta", pt).evaluate()["g"]
            h_interp = d3.Interpolate(h3, "theta", pt).evaluate()["g"]
            u_out[j, ..., i : i + 1] = u_interp * second / meter
            h_out[j, ..., i : i + 1] = h_interp / meter

    return h_out, u_out


if __name__ == "__main__":
    print("HAVE WE EVEN STARTED CODE YET?")
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--source",
        default="/mnt/home/polymathic/ceph/the_well/testing_before_adding/earthswe",
    )
    parser.add_argument(
        "--dest", default="/mnt/home/polymathic/ceph/the_well/datasets/planetswe/data"
    )
    parser.add_argument("--index", default="0")
    args = parser.parse_args()

    current_path = args.source
    write_path = args.dest
    ic_file = int(args.index)
    max_ic_train = 32
    max_ic_valid = 36

    ic_folders = sorted(glob.glob(f"{current_path}/IC*"))
    target_ic = ic_folders[ic_file]
    print("picked source", target_ic)
    # for i, folder in enumerate(ic_folders):
    ic_num = int(target_ic.split("_")[-1])
    if ic_num < max_ic_train:
        split = "train"
    elif ic_num < max_ic_valid:
        split = "valid"
    else:
        split = "test"
    for i in range(10):
        print(i)
    for file in glob.glob(f"{target_ic}/*.h5"):
        file_idx = file.split("_")[-1][:-3]
        target_path = f"{write_path}/{split}/planetswe_IC{ic_num:02d}_{file_idx}.h5"
        print(file, target_path)
        earthswe_to_well(file, target_path)