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();
|
}
|
}
|
|
}
|