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

推荐订阅源

C
Comments on: Blog
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
T
Tor Project blog
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
月光博客
月光博客
罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
P
Privacy International News Feed
T
Tenable Blog
阮一峰的网络日志
阮一峰的网络日志
AWS News Blog
AWS News Blog
T
ThreatConnect
博客园 - 三生石上(FineUI控件)
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
L
LINUX DO - 最新话题
美团技术团队
大猫的无限游戏
大猫的无限游戏
I
Intezer
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
小众软件
小众软件
T
Threatpost
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Project Zero
Project Zero
J
Java Code Geeks
Cyberwarzone
Cyberwarzone
IT之家
IT之家
MyScale Blog
MyScale Blog
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
腾讯CDC
S
SegmentFault 最新的问题
F
Fox-IT International blog
S
Security Archives - TechRepublic
Last Week in AI
Last Week in AI
G
GRAHAM CLULEY
M
MIT News - Artificial intelligence

俟河清

【牛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 俟河清
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)