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