File size: 1,691 Bytes
e416ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
In-place repair for a Zarr store with mismatched time dimension lengths.

- Compares the length of the 1D `time` array with the leading dimension of
  the 3D `maxunitstreamflow` array.
- Resizes the longer one down to the shorter to restore consistency.

Usage:
  ml conda; conda activate credit
  python /glade/work/li1995/FLASH/data/fix.py
"""

from __future__ import annotations

import zarr


STORE_PATH = "/glade/derecho/scratch/li1995/data/maxunitstreamflow_2021_2025_10min.zarr"


def main() -> None:
    root = zarr.open(STORE_PATH, mode="r+")

    time_arr = root["time"]  # shape: (T,)
    data_arr = root["maxunitstreamflow"]  # shape: (T, Y, X)
    lat_arr = root.get("latitude")
    lon_arr = root.get("longitude")

    t_time = int(time_arr.shape[0])
    t_data = int(data_arr.shape[0])
    y_size = int(data_arr.shape[1])
    x_size = int(data_arr.shape[2])

    print(f"Before: time={t_time}, data_time={t_data}, data_shape={data_arr.shape}")
    if lat_arr is not None:
        print(f"Latitude size: {lat_arr.shape}")
    if lon_arr is not None:
        print(f"Longitude size: {lon_arr.shape}")

    target_len = min(t_time, t_data)
    if t_data > target_len:
        print(f"Resizing maxunitstreamflow from {t_data} to {target_len}")
        data_arr.resize((target_len, y_size, x_size))
    if t_time > target_len:
        print(f"Resizing time from {t_time} to {target_len}")
        time_arr.resize((target_len,))

    # Re-read and report
    t_time2 = int(time_arr.shape[0])
    t_data2 = int(data_arr.shape[0])
    print(f"After: time={t_time2}, data_time={t_data2}, data_shape={data_arr.shape}")


if __name__ == "__main__":
    main()