File size: 2,786 Bytes
c84ff43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import xarray as xr

__all__ = ["chunk", "print_xarray", "save_with_progress"]


def chunk(ds, time=1, **kwargs):
    if isinstance(ds, xr.DataArray):
        dims = {k: v for k, v in zip(ds.dims, ds.shape)}
    else:
        dims = {k: v for k, v in ds.sizes.items()}

    if "time" in dims:
        dims["time"] = time

    for k, v in kwargs.items():
        if k in dims:
            dims[k] = v

    ds = ds.chunk(dims)
    return ds


def print_xarray(ds, desp="", names=[]):

    if isinstance(ds, xr.Dataset):
        for v in ds:
            print_xarray(ds[v], desp=desp, names=names)
        return
    
    v = ds
    msg = f"{v.name.upper()} {desp}: \nshape: \033[94m{v.shape}\033[0m"

    if "time" in ds.dims:
        start_date = ds.time[0].dt.date.item()
        end_date = ds.time[-1].dt.date.item()
        msg += f", time: \033[94m{len(ds.time)} = ({start_date} ~ {end_date})\033[0m"

    if "lat" in ds.dims and "lon" in ds.dims:
        lat = ds.lat.values
        lon = ds.lon.values
        msg += f", latlon: \033[94m({lat[0]:.3f} ~ {lat[-1]:.3f}) x ({lon[0]:.3f} ~ {lon[-1]:.3f})\033[0m"

    if "level" in v.dims:
        if len(names) > 0:
            v = v.sel(level=np.intersect1d(names, v.level))
        for lvl in v.level.data:
            x = v.sel(level=lvl).values
            msg += f"\nlevel: {lvl:04d}, value: \033[91m{x.min():.3f} ~ {x.max():.3f}\033[0m"
    elif "depth" in v.dims:
        for lvl in v.depth.data:
            x = v.sel(depth=lvl).values
            msg += f"\ndepth: {lvl:.2f}, value: \033[91m{x.min():.3f} ~ {x.max():.3f}\033[0m"
    elif "channel" in v.dims:
        if len(names) > 0:
            v = v.sel(channel=np.intersect1d(names, v.channel))        
        for ch in v.channel.data:
            x = v.sel(channel=ch).values
            msg += f"\nchannel: {ch}, value: \033[91m{x.min():.3f} ~ {x.max():.3f}\033[0m"            
    else:
        x = v.values
        msg += f", value: \033[91m{x.min():.3f} ~ {x.max():.3f}\033[0m"

    print(msg)



def save_with_progress(ds, save_name, dtype=np.float32):
    from dask.diagnostics import ProgressBar

    if 'time' in ds.dims:
        ds = ds.assign_coords(time=ds.time.astype(np.datetime64))
    
    ds = ds.astype(dtype)
    if 'channel' in ds.coords and ds.coords['channel'].dtype == 'object':
        ds = ds.assign_coords(
            channel=ds.coords['channel'].astype('U50')  # 'U50' 表示最大50字符的Unicode字符串
        )

    if save_name.endswith(".nc"):
        obj = ds.to_netcdf(save_name, compute=False)
    elif save_name.endswith(".zarr"):
        obj = ds.to_zarr(save_name, compute=False)
    else:
        raise ValueError("save_type must be 'nc' or 'zarr'!")
    
    with ProgressBar():
        obj.compute()