Datasets:

ArXiv:
License:
maximilianherde commited on
Commit
90f9e91
·
verified ·
1 Parent(s): d06cd11

Upload assemble_data.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. assemble_data.py +72 -0
assemble_data.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ for i, c_file in enumerate(c_files):
58
+ with Dataset(os.path.join(input_dir, c_file), "r") as nc:
59
+ print(f"Processing {os.path.join(input_dir, c_file)}")
60
+ variable = nc.variables[c_file.split("_")[0]]
61
+ out_nc[c_file.split("_")[0]][samples[i] : samples[i + 1]] = variable[:]
62
+
63
+ print(f"Saved data to {output_file}")
64
+
65
+
66
+ if __name__ == "__main__":
67
+ parser = argparse.ArgumentParser()
68
+ parser.add_argument("--input_dir", type=str, required=True)
69
+ parser.add_argument("--output_file", type=str, required=True)
70
+ args = parser.parse_args()
71
+
72
+ assemble_data(args.input_dir, args.output_file)