在处理水文监测数据时,经常会遇到流量(Q值)缺失的情况。比如水位计故障、数据传输中断等原因,导致某些时间点的流量数据为空。如果直接拿这样的数据去分析或者建模,结果显然不准确。
今天给大家分享一个Java工具类 ,可以自动读取CSV文件,对缺失的Q值进行线性插值填补,最终输出完整的数据文件。
先看效果:原始CSV中某些行的Q列为空,运行程序后,这些空值被自动计算并填入了合理的数值。
整个方案分为如下步骤:
1.过程
#mermaid-svg-7g2GNurLZo4UK65C{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7g2GNurLZo4UK65C .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7g2GNurLZo4UK65C .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7g2GNurLZo4UK65C .error-icon{fill:#552222;}#mermaid-svg-7g2GNurLZo4UK65C .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7g2GNurLZo4UK65C .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7g2GNurLZo4UK65C .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7g2GNurLZo4UK65C .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7g2GNurLZo4UK65C .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7g2GNurLZo4UK65C .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7g2GNurLZo4UK65C .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7g2GNurLZo4UK65C .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7g2GNurLZo4UK65C .marker.cross{stroke:#333333;}#mermaid-svg-7g2GNurLZo4UK65C svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7g2GNurLZo4UK65C p{margin:0;}#mermaid-svg-7g2GNurLZo4UK65C .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7g2GNurLZo4UK65C .cluster-label text{fill:#333;}#mermaid-svg-7g2GNurLZo4UK65C .cluster-label span{color:#333;}#mermaid-svg-7g2GNurLZo4UK65C .cluster-label span p{background-color:transparent;}#mermaid-svg-7g2GNurLZo4UK65C .label text,#mermaid-svg-7g2GNurLZo4UK65C span{fill:#333;color:#333;}#mermaid-svg-7g2GNurLZo4UK65C .node rect,#mermaid-svg-7g2GNurLZo4UK65C .node circle,#mermaid-svg-7g2GNurLZo4UK65C .node ellipse,#mermaid-svg-7g2GNurLZo4UK65C .node polygon,#mermaid-svg-7g2GNurLZo4UK65C .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7g2GNurLZo4UK65C .rough-node .label text,#mermaid-svg-7g2GNurLZo4UK65C .node .label text,#mermaid-svg-7g2GNurLZo4UK65C .image-shape .label,#mermaid-svg-7g2GNurLZo4UK65C .icon-shape .label{text-anchor:middle;}#mermaid-svg-7g2GNurLZo4UK65C .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7g2GNurLZo4UK65C .rough-node .label,#mermaid-svg-7g2GNurLZo4UK65C .node .label,#mermaid-svg-7g2GNurLZo4UK65C .image-shape .label,#mermaid-svg-7g2GNurLZo4UK65C .icon-shape .label{text-align:center;}#mermaid-svg-7g2GNurLZo4UK65C .node.clickable{cursor:pointer;}#mermaid-svg-7g2GNurLZo4UK65C .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7g2GNurLZo4UK65C .arrowheadPath{fill:#333333;}#mermaid-svg-7g2GNurLZo4UK65C .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7g2GNurLZo4UK65C .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7g2GNurLZo4UK65C .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7g2GNurLZo4UK65C .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7g2GNurLZo4UK65C .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7g2GNurLZo4UK65C .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7g2GNurLZo4UK65C .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7g2GNurLZo4UK65C .cluster text{fill:#333;}#mermaid-svg-7g2GNurLZo4UK65C .cluster span{color:#333;}#mermaid-svg-7g2GNurLZo4UK65C div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7g2GNurLZo4UK65C .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7g2GNurLZo4UK65C rect.text{fill:none;stroke-width:0;}#mermaid-svg-7g2GNurLZo4UK65C .icon-shape,#mermaid-svg-7g2GNurLZo4UK65C .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7g2GNurLZo4UK65C .icon-shape p,#mermaid-svg-7g2GNurLZo4UK65C .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7g2GNurLZo4UK65C .icon-shape .label rect,#mermaid-svg-7g2GNurLZo4UK65C .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7g2GNurLZo4UK65C .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7g2GNurLZo4UK65C .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7g2GNurLZo4UK65C :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 读取CSV文件
提取时间列TM和Q值列
调用插值算法生成完整时间序列
将插值结果回填到原始数据行
按时间排序并输出新文件
插值算法的核心逻辑:
- 找到缺失点前后最近的有效数据点
- 根据时间间隔按比例计算缺失值
- 支持按小时、3小时、6小时、天等不同粒度插值
2代码详解
2.1 数据结构:DataPoint
java
@Data
public class DataPoint {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
public Date tm; // 时间
public Double value; // 数值(Q值)
public DataPoint(Date tm, Double value) {
this.tm = tm;
this.value = value;
}
}
2.2 时间粒度枚举
java
public enum Granularity {
HOURLY(1), // 逐小时
THREE_HOURLY(3), // 逐3小时
SIX_HOURLY(6), // 逐6小时
DAILY(24); // 逐日
}
2.3 核心插值算法
java
public static List<DataPoint> interpolate(List<DataPoint> data, Granularity granularity) {
// 1. 按时间排序
List<DataPoint> sorted = data.stream()
.sorted(Comparator.comparing(d -> d.tm))
.collect(Collectors.toList());
// 2. 构建有序Map
TreeMap<Date, Double> existing = new TreeMap<>();
for (DataPoint dp : sorted) {
existing.put(dp.tm, dp.value);
}
// 3. 生成目标时间序列(按粒度对齐)
List<Date> targetTimes = generateTargetTimes(start, end, granularity);
// 4. 线性插值
for (Date t : targetTimes) {
if (existing.containsKey(t)) {
result.add(new DataPoint(t, existing.get(t)));
} else {
// 找前后有效点进行线性插值
Map.Entry<Date, Double> floor = findPrevValid(existing, t);
Map.Entry<Date, Double> ceiling = findNextValid(existing, t);
if (floor != null && ceiling != null) {
double ratio = hoursBetween(floor, t) / hoursBetween(floor, ceiling);
double interpValue = floor.getValue() +
(ceiling.getValue() - floor.getValue()) * ratio;
result.add(new DataPoint(t, interpValue));
}
}
}
return result;
}
2.4 CSV读写与主流程
主程序CsvQInterpolator.java负责:
- 读取
无标题.csv - 识别Q列为空的行
- 调用插值工具填补
- 将结果写回新文件
无标题_插值后.csv
3、输入输出对比
输入片段(Q值为空)
csv
"20810350","2025-7-10 18:00:00","87.46",,,...
"20810350","2025-7-10 19:00:00","87.47",,,...
输出片段(Q值已填补)
csv
"20810350","2025-7-10 18:00:00","87.46","165.0",...
"20810350","2025-7-10 19:00:00","87.47","165.0",...
可以看到,原本缺失的Q值被成功计算出来并填入了165.0。