张洋洋
2025-02-10 36abe49367dc914eb1f5ccf9a9f253080b69d04d
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
package com.se.simu.utils;
 
import com.bc.zarr.ArrayParams;
import com.bc.zarr.DataType;
import com.bc.zarr.ZarrArray;
import com.bc.zarr.ZarrGroup;
import com.se.simu.domain.vo.StationRainVo;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.List;
 
public class ZarrUtils {
 
    public static void saveZarrRainfall(String path, List<StationRainVo> stationRainVos){
        try {
            // 获取 CSV 列数
            int numColumns = 5;
            // 计算 CSV 行数
            int numRows = stationRainVos.size();
            // 创建 Zarr 组
            Path zarrPath = Paths.get(path);
            ZarrGroup zarrGroup = ZarrGroup.create(zarrPath);
            // 定义 Zarr 数组的维度
            int[] shape = {numRows, numColumns};
            // 定义分块大小
            int[] chunks = {1440};
            // 创建 Zarr 数组
            ArrayParams params = new ArrayParams();
            params.shape(chunks);
            params.chunks(chunks);
            params.dataType(DataType.f4);
            ZarrArray zarrArray = zarrGroup.createArray("rainfall", params);
            int rowIndex = 0;
            for (StationRainVo record : stationRainVos) {
                double value = record.getRainfall();
                zarrArray.write(value);
                rowIndex++;
            }
            // 关闭 Zarr 组
            //zarrGroup.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void saveZarrTime(String path, List<StationRainVo> stationRainVos){
        try {
            // 获取 CSV 列数
            int numColumns = 5;
            // 计算 CSV 行数
            int numRows = stationRainVos.size();
            // 创建 Zarr 组
            Path zarrPath = Paths.get(path);
            ZarrGroup zarrGroup = ZarrGroup.create(zarrPath);
            // 定义 Zarr 数组的维度
            int[] shape = {numRows, numColumns};
            // 定义分块大小
            int[] chunks = {1440};
            // 创建 Zarr 数组
            ArrayParams params = new ArrayParams();
            params.shape(chunks);
            params.chunks(chunks);
            params.dataType(DataType.i4);
            ZarrArray zarrArray = zarrGroup.createArray("time", params);
            int rowIndex = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            for (StationRainVo record : stationRainVos) {
                long value = sdf.parse(record.getDatetime()).getTime()/1000;
                zarrArray.write(value);
                rowIndex++;
            }
            // 关闭 Zarr 组
            //zarrGroup.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}