水文时间序列Q值自动修复工具

在处理水文监测数据时,经常会遇到流量(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负责:

  1. 读取无标题.csv
  2. 识别Q列为空的行
  3. 调用插值工具填补
  4. 将结果写回新文件无标题_插值后.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


相关推荐
星座5284 天前
破解水环境空间分析难题,迈向智慧水环境管理:ArcGIS水质评价、污染预测与洪水监测核心技术揭秘
arcgis·水环境·水文
xiao5kou4chang6kai49 天前
R和Python都能用的Copula实战路线:检验、选模、Vine、贝叶斯
回归·时间序列分析·水文·copula
梦想的初衷~1 个月前
AI辅助下基于ArcGIS Pro的SWAT模型全流程高效建模实践与深度进阶应用
人工智能·arcgis·气候·水文·地理信息·环境科学
xiao5kou4chang6kai41 个月前
HYPE水文模型全流程实战——以黑河上游流域为例
土地利用·水文·hpye
xiao5kou4chang6kai42 个月前
SWAT模型无资料地区建模、不确定分析及气候、土地利用变化对水资源与面源污染影响分析
土地利用·水文·流域·面源污染·swat
梦想的初衷~3 个月前
大数据驱动下的自然科学建模:从统计学习到深度神经网络的系统性实践
生态学·遥感·水文·环境科学·地球科学·农业科学·大气环境
梦想的初衷~6 个月前
ArcGIS在水土流失模拟与流域管理中的全流程应用
arcgis·生态学·农业·水文·环境科学·地理学·水土保持
维维180-3121-14557 个月前
ArcGIS与ENVI在生态影响评价中的融合应用:八大专题图制作全解析
生态·农业·林业·水文·地质·矿业
星座5287 个月前
AI+CMIP6数据分析与可视化、降尺度技术与气候变化的区域影响、极端气候分析
人工智能·ai·气候·水文·cmip6