Datasets:
ArXiv:
License:
Upload assemble_data.py with huggingface_hub
Browse files- assemble_data.py +79 -0
assemble_data.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
from netCDF4 import Dataset
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def assemble_data(input_dir, output_file):
|
| 7 |
+
nc_files = [f for f in os.listdir(input_dir) if f.endswith(".nc") and not f.startswith("c")]
|
| 8 |
+
c_files = [f for f in os.listdir(input_dir) if f.endswith(".nc") and f.startswith("c")]
|
| 9 |
+
nc_files.sort()
|
| 10 |
+
c_files.sort()
|
| 11 |
+
|
| 12 |
+
samples = [0]
|
| 13 |
+
with Dataset(os.path.join(input_dir, nc_files[0]), "r") as first_nc:
|
| 14 |
+
samples.append(first_nc.dimensions["sample"].size)
|
| 15 |
+
num_times = first_nc.dimensions["time"].size
|
| 16 |
+
try:
|
| 17 |
+
num_channels = first_nc.dimensions["channel"].size
|
| 18 |
+
except:
|
| 19 |
+
num_channels = None
|
| 20 |
+
x_size = first_nc.dimensions["x"].size
|
| 21 |
+
y_size = first_nc.dimensions["y"].size
|
| 22 |
+
dtype = first_nc.variables[nc_files[0].split("_")[0]].dtype
|
| 23 |
+
|
| 24 |
+
for nc_file in nc_files[1:]:
|
| 25 |
+
with Dataset(os.path.join(input_dir, nc_file), "r") as nc:
|
| 26 |
+
samples.append(nc.dimensions["sample"].size)
|
| 27 |
+
|
| 28 |
+
num_samples = sum(samples)
|
| 29 |
+
for i in range(1, len(samples)):
|
| 30 |
+
samples[i] += samples[i - 1]
|
| 31 |
+
with Dataset(output_file, "w") as out_nc:
|
| 32 |
+
out_nc.createDimension("sample", num_samples)
|
| 33 |
+
out_nc.createDimension("time", num_times)
|
| 34 |
+
if num_channels is not None:
|
| 35 |
+
out_nc.createDimension("channel", num_channels)
|
| 36 |
+
out_nc.createDimension("x", x_size)
|
| 37 |
+
out_nc.createDimension("y", y_size)
|
| 38 |
+
if num_channels is not None:
|
| 39 |
+
out_nc.createVariable(
|
| 40 |
+
nc_files[0].split("_")[0], dtype, ("sample", "time", "channel", "x", "y")
|
| 41 |
+
)
|
| 42 |
+
else:
|
| 43 |
+
out_nc.createVariable(
|
| 44 |
+
nc_files[0].split("_")[0], dtype, ("sample", "time", "x", "y")
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
out_nc.createVariable(
|
| 48 |
+
"c", dtype, ("sample", "x", "y")
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
for i, nc_file in enumerate(nc_files):
|
| 52 |
+
with Dataset(os.path.join(input_dir, nc_file), "r") as nc:
|
| 53 |
+
print(f"Processing {os.path.join(input_dir, nc_file)}")
|
| 54 |
+
variable = nc.variables[nc_file.split("_")[0]]
|
| 55 |
+
out_nc[nc_file.split("_")[0]][samples[i] : samples[i + 1]] = variable[:]
|
| 56 |
+
|
| 57 |
+
samples = [0]
|
| 58 |
+
for nc_file in c_files[]:
|
| 59 |
+
with Dataset(os.path.join(input_dir, nc_file), "r") as nc:
|
| 60 |
+
samples.append(nc.dimensions["sample"].size)
|
| 61 |
+
for i in range(1, len(samples)):
|
| 62 |
+
samples[i] += samples[i - 1]
|
| 63 |
+
|
| 64 |
+
for i, c_file in enumerate(c_files):
|
| 65 |
+
with Dataset(os.path.join(input_dir, c_file), "r") as nc:
|
| 66 |
+
print(f"Processing {os.path.join(input_dir, c_file)}")
|
| 67 |
+
variable = nc.variables[c_file.split("_")[0]]
|
| 68 |
+
out_nc[c_file.split("_")[0]][samples[i] : samples[i + 1]] = variable[:]
|
| 69 |
+
|
| 70 |
+
print(f"Saved data to {output_file}")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
parser = argparse.ArgumentParser()
|
| 75 |
+
parser.add_argument("--input_dir", type=str, required=True)
|
| 76 |
+
parser.add_argument("--output_file", type=str, required=True)
|
| 77 |
+
args = parser.parse_args()
|
| 78 |
+
|
| 79 |
+
assemble_data(args.input_dir, args.output_file)
|