-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepRain.py
More file actions
executable file
·106 lines (85 loc) · 2.63 KB
/
prepRain.py
File metadata and controls
executable file
·106 lines (85 loc) · 2.63 KB
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
"""
Code for precipitation data simulation preparation
Author: Vaclav Steinbach
Date: 15.08.2025
Dissertation Work
"""
import pandas as pd
import numpy as np
import os
"""
=-=-= CONFIG =-=-=
"""
campaign = "Amalie_2025-08-26_2025-09-07"
data_dir = "data/"+campaign+"/"
out_dir = "out/"
os.makedirs(out_dir, exist_ok=True)
# Select which file you want to process
# leave the other commented
filename = "LES2-S1" # Spruce forest probably..
csv_header = "LES2-S1" # this adresses the column var name in csv
filename = "LES1-S1" # Beech forest probably..
csv_header = "LES1-S1" # this adresses the column var name in csv
out_file = "rain_tree.in"
filename = "krajina_scintilometr"
out_file = "rain_free.in"
csv_header = "Scintilometr"
# Set the start and end date, set to none if you want all available data
start_date = "2025-09-05 00:00:00"
end_date = "2025-09-06 12:00:00"
# start_date = None
# end_date = None
# Set flag to true if you want to convert precipitation mm --> m
convert = True
"""
=-=-= TRANSFORM DATA =-=-=
"""
df = pd.read_csv(data_dir+filename+".csv")
# Rename columns
df = df.rename(columns={csv_header: "precip"})
df = df.rename(columns={csv_header: "precip"})
# Reformat time column to datetime
df["Time"] = pd.to_datetime(df["Time"])
# The raw data is sampled in 00:00:03 times
df["Time"] = df["Time"].dt.floor("10min")
# Select timerange
if start_date is not None and end_date is not None:
df = df[(df["Time"] >= pd.to_datetime(start_date)) &
(df["Time"] <= pd.to_datetime(end_date))]
# Convert Time to string in desired format
time_str = df["Time"].dt.strftime("%Y-%m-%dT%H:%M:%S")
# Save to file
np.savetxt(
data_dir + "time.in",
time_str,
fmt="%s", # important: save as string
header="DTM",
comments="# "
)
# Reformat precipitation into numeric
df["precip"] = pd.to_numeric(df["precip"], errors="coerce")
# Set datetime index and collapse duplicates (safe for resampling)
df = df.groupby("Time", as_index=True)["precip"].mean()
# Resample to 10-minute frequency and interpolate linearly
df = df.resample("10min").interpolate("linear")
# Reset index
df = df.reset_index()
# Convert precipitation from mm -> m
if convert:
df["precip"] = df["precip"] / 1000.0 # m
else:
df["precip"] = df["precip"] # mm
# Calculate seconds since start
start_time = df["Time"].iloc[0]
df["seconds"] = (df["Time"] - start_time).dt.total_seconds().astype(int)
# Reorder columns to seconds + precip
rain_arr = df[["seconds", "precip"]].to_numpy()
# Save to .in file
np.savetxt(
data_dir+out_file,
rain_arr,
fmt="%d %.10f",
header="seconds precipitation [m]",
comments="# "
)
print("Data converted!")