惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
W
WeLiveSecurity
P
Proofpoint News Feed
月光博客
月光博客
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
Webroot Blog
Webroot Blog
T
Threatpost
Y
Y Combinator Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Threat Research - Cisco Blogs
Vercel News
Vercel News
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
Recent Announcements
Recent Announcements
S
Security @ Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
T
The Exploit Database - CXSecurity.com
云风的 BLOG
云风的 BLOG
C
Cisco Blogs
雷峰网
雷峰网
量子位
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
I
Intezer
T
The Blog of Author Tim Ferriss
G
GRAHAM CLULEY
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
罗磊的独立博客

俟河清

【牛GN】白色相簿 2 动画版 / IC 浅谈 【牛GN】最终幻想 16 【牛体力学】流体力学的基本概念 1 【牛体力学】 前置:场论及张量 【牛GN】iPhone 80 Ultra & Apple Watch 6 Pro 2025 年总结:变了很多,但也没变。 【牛GN】最终幻想 7:重生 【牛 GN】女神异闻录 3 重制版 【牛GN】那些年用过的外围设备 MODIS 遥感产品数据处理及转换 【牛GN】那些年用过的电脑们 经纬度转换的那些麻烦事 【牛GN】那些年用过的爪机们 【牛GN】葬送的芙莉莲 【All in Boom】第 2 期:服务部署 【牛GN】Darling in the Franxx 动漫随笔:败犬女主实在是太多了 【All in Boom】第 1.5 期:反向代理及内网 HTTPS 构建 动漫随笔:Mygo、Ave Mujica和孤独摇滚
降水序列重建思路
SatyrLee · 2025-05-09 · via 俟河清

前几天接到了一批不同时间长度的降雨序列数据,需要将其处理为逐小时降水序列数据。然而处理的时候发现了一些问题,时间序列长度不一致,且有冲突。通过结合历史资料分析,提出了一种不是很合理的解决思路,并将其编写为代码,整理如下。

其实最简单的思路就是直接将长时间的数据除以时间并进行拼接即可,但是在原数据中发现了问题:14:00-16:00 的降水量是 0.5 mm,而 14:00-15:00 的降水量是 0.8 mm,这在时间段降水量定义上明显存在冲突。

但是我们不能简单就将其视为是异常数据就处理掉,因为在多个时间段内数据均有这样的问题。这时我想到在这里可能是表示为折算至每小时的降水量。同样,也不能简单的做出定论。需要进行数据支持。还好,有一些网站提供了历史降水量数据,可以进行简单比对。

考虑雨量计的工作原理,雨量计是通过测量降水的高度来计算降水量的。雨量计通常会有一个容器,当降水落入容器时,容器内的水位会随着降水量的增加而上升。雨量计会定期记录容器内的水位高度,并将其转换为降水量。

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
import pandas as pd
import os

def read_xlsx(file_path):
"""
Read an Excel file and return a DataFrame.
"""
if os.path.exists(file_path):
df = pd.read_excel(file_path)
return df
else:
raise FileNotFoundError(f"The file {file_path} does not exist.")

def first_processing(df, t_begin_col, t_end_col, data_col):
"""
INPUT: pandas.DataFrame, t_begin_col, t_end_col, data_col
OUTPUT: pandas.DataFrame
"""
df[t_begin_col] = pd.to_datetime(df[t_begin_col])
df[t_end_col] = pd.to_datetime(df[t_end_col])

df = df[df[t_begin_col] <= df[t_end_col]].copy()

global_begin = df[t_begin_col].min()
global_end = df[t_end_col].max()

full_range = pd.date_range(start=global_begin, end=global_end, freq='h')

mapping = {}
for _, row in df.iterrows():
start = row[t_begin_col]
end = row[t_end_col]
value = row[data_col]
if end - start == pd.Timedelta(hours=1):
mapping[start] = value

new_rows = []
for ts in full_range:
new_rows.append({t_begin_col: ts, data_col: mapping.get(ts, 0),"type": 1 if ts in mapping else 0})

new_df = pd.DataFrame(new_rows)
return new_df

def further_processing(df, t_begin_col, t_end_col, data_col, target_df, type_val):
"""
INPUT: pandas.DataFrame, t_begin_col, t_end_col, data_col, target_df, type_val
OUTPUT: pandas.DataFrame
"""

interval = pd.Timedelta(hours=type_val)

filtered_df = df[(df[t_end_col] - df[t_begin_col] == interval)].drop_duplicates(
subset=[t_begin_col, t_end_col, data_col])

for _, row in filtered_df.iterrows():
current_time = row[t_begin_col]
selected_value = row[data_col]
period_start = current_time
period_end = current_time + interval

mask = (target_df[t_begin_col] >= period_start) & (target_df[t_begin_col] < period_end)
sub_df = target_df.loc[mask]

existing_nonzero_sum = sub_df[sub_df[data_col] != 0][data_col].sum()

remain_value = selected_value - existing_nonzero_sum

if remain_value > 0:

empty_mask = sub_df[data_col] == 0
empty_count = empty_mask.sum()
if empty_count > 0:
average_val = remain_value / empty_count
target_df.loc[mask & empty_mask, data_col] = average_val
target_df.loc[mask & empty_mask, "type"] = type_val
return target_df

def prep_extract(df,prep0,time,excel_name):
"""
INPUT: pandas.DataFrame, prep0,time,excel_name
OUTPUT: Excel file with sequences of rainfall
"""

results = []
current_sequence = []
current_type = None

for index, row in df.iterrows():
if row['prep'] > prep0:
current_sequence.append(row)
else:
if len(current_sequence) >= time:
results.append((current_sequence, current_type))
current_sequence = []

if len(current_sequence) >= time:
results.append((current_sequence, current_type))

with pd.ExcelWriter(excel_name) as writer:
for i, (sequence, seq_type) in enumerate(results):
sequence_df = pd.DataFrame(sequence)
sequence_df.to_excel(writer, sheet_name=f"Seq_{i+1}", index=False)

if __name__ == "__main__":

file_path = 'data.xlsx'
df = pd.read_excel(file_path)

t_begin_col = 'begin'
t_end_col = 'end'
data_col = 'prep'
types=[2,3,4,5,6,7,8,9,10,11,12,13,14,15]

hourly_df = first_processing(df, t_begin_col, t_end_col, data_col)
for i in types:
hourly_df = further_processing(df, t_begin_col, t_end_col, data_col, hourly_df, i)
hourly_df.to_excel('hourly.xlsx', index=False)
prep_extract(hourly_df,2,5,'prep_extract.xlsx')
print(hourly_df)