张洋洋
2025-01-10 096abd16d07228aba3189302a14b24f56bb63abd
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
 
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
public class ZarrUtils {
//    public static void main(String[] args) throws Exception {
//        // 输入 CSV 文件的路径
//        String csvFilePath = "D:\\城市内涝\\sem\\雨量站点数据\\雨量站包含坐标.csv";
//        // 输出 Zarr 文件的路径
//        String zarrFilePath = "D:\\城市内涝\\sem\\雨量站点数据\\大庞村";
//        try (CSVParser csvParser = new CSVParser(new FileReader(csvFilePath), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
//            // 获取 CSV 列数
//            int numColumns = csvParser.getHeaderMap().size();
//            // 计算 CSV 行数
//            int numRows = 0;
//            for (CSVRecord record : csvParser) {
//                numRows++;
//            }
//            // 重新初始化 CSV 解析器,因为之前的遍历已经耗尽了迭代器
//            csvParser.close();
//            CSVParser csvRecords = new CSVParser(new FileReader(csvFilePath), CSVFormat.DEFAULT.withFirstRecordAsHeader());
//            // 创建 Zarr 组
//            Path zarrPath = Paths.get(zarrFilePath);
//            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 (CSVRecord record : csvRecords) {
//                double value = Double.parseDouble(record.get(1));
//                zarrArray.write(value);
//                rowIndex++;
//            }
//            // 关闭 Zarr 组
//            //zarrGroup.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
 
    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();
                zarrArray.write(value);
                rowIndex++;
            }
            // 关闭 Zarr 组
            //zarrGroup.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) throws Exception{
        System.out.println("2023/7/31 6:30");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = sdf.parse("2023/7/31 6:30");
        System.out.println(date.getTime());
    }
}