Datasets:

ArXiv:
License:
maximilianherde commited on
Commit
34bd142
1 Parent(s): bbaa8e3

Upload assemble_data.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. assemble_data.py +60 -0
assemble_data.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")]
8
+ nc_files.sort()
9
+
10
+ samples = [0]
11
+ with Dataset(os.path.join(input_dir, nc_files[0]), "r") as first_nc:
12
+ samples.append(first_nc.dimensions["sample"].size)
13
+ num_times = first_nc.dimensions["time"].size
14
+ try:
15
+ num_channels = first_nc.dimensions["channel"].size
16
+ except:
17
+ num_channels = None
18
+ x_size = first_nc.dimensions["x"].size
19
+ y_size = first_nc.dimensions["y"].size
20
+ dtype = first_nc.variables[nc_files[0].split("_")[0]].dtype
21
+
22
+ for nc_file in nc_files[1:]:
23
+ with Dataset(os.path.join(input_dir, nc_file), "r") as nc:
24
+ samples.append(nc.dimensions["sample"].size)
25
+
26
+ num_samples = sum(samples)
27
+ for i in range(1, len(samples)):
28
+ samples[i] += samples[i - 1]
29
+ with Dataset(output_file, "w") as out_nc:
30
+ out_nc.createDimension("sample", num_samples)
31
+ out_nc.createDimension("time", num_times)
32
+ if num_channels is not None:
33
+ out_nc.createDimension("channel", num_channels)
34
+ out_nc.createDimension("x", x_size)
35
+ out_nc.createDimension("y", y_size)
36
+ if num_channels is not None:
37
+ out_nc.createVariable(
38
+ nc_files[0].split("_")[0], dtype, ("sample", "time", "channel", "x", "y")
39
+ )
40
+ else:
41
+ out_nc.createVariable(
42
+ nc_files[0].split("_")[0], dtype, ("sample", "time", "x", "y")
43
+ )
44
+
45
+ for i, nc_file in enumerate(nc_files):
46
+ with Dataset(os.path.join(input_dir, nc_file), "r") as nc:
47
+ print(f"Processing {os.path.join(input_dir, nc_file)}")
48
+ variable = nc.variables[nc_file.split("_")[0]]
49
+ out_nc[nc_file.split("_")[0]][samples[i] : samples[i + 1]] = variable[:]
50
+
51
+ print(f"Saved data to {output_file}")
52
+
53
+
54
+ if __name__ == "__main__":
55
+ parser = argparse.ArgumentParser()
56
+ parser.add_argument("--input_dir", type=str, required=True)
57
+ parser.add_argument("--output_file", type=str, required=True)
58
+ args = parser.parse_args()
59
+
60
+ assemble_data(args.input_dir, args.output_file)