disdrodb 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- disdrodb/__init__.py +68 -34
- disdrodb/_config.py +5 -4
- disdrodb/_version.py +16 -3
- disdrodb/accessor/__init__.py +20 -0
- disdrodb/accessor/methods.py +125 -0
- disdrodb/api/checks.py +177 -24
- disdrodb/api/configs.py +3 -3
- disdrodb/api/info.py +13 -13
- disdrodb/api/io.py +281 -22
- disdrodb/api/path.py +184 -195
- disdrodb/api/search.py +18 -9
- disdrodb/cli/disdrodb_create_summary.py +103 -0
- disdrodb/cli/disdrodb_create_summary_station.py +91 -0
- disdrodb/cli/disdrodb_run_l0.py +1 -1
- disdrodb/cli/disdrodb_run_l0_station.py +1 -1
- disdrodb/cli/disdrodb_run_l0a_station.py +1 -1
- disdrodb/cli/disdrodb_run_l0b.py +1 -1
- disdrodb/cli/disdrodb_run_l0b_station.py +3 -3
- disdrodb/cli/disdrodb_run_l0c.py +1 -1
- disdrodb/cli/disdrodb_run_l0c_station.py +3 -3
- disdrodb/cli/disdrodb_run_l1_station.py +2 -2
- disdrodb/cli/disdrodb_run_l2e_station.py +2 -2
- disdrodb/cli/disdrodb_run_l2m_station.py +2 -2
- disdrodb/configs.py +149 -4
- disdrodb/constants.py +61 -0
- disdrodb/data_transfer/download_data.py +127 -11
- disdrodb/etc/configs/attributes.yaml +339 -0
- disdrodb/etc/configs/encodings.yaml +473 -0
- disdrodb/etc/products/L1/global.yaml +13 -0
- disdrodb/etc/products/L2E/10MIN.yaml +12 -0
- disdrodb/etc/products/L2E/1MIN.yaml +1 -0
- disdrodb/etc/products/L2E/global.yaml +22 -0
- disdrodb/etc/products/L2M/10MIN.yaml +12 -0
- disdrodb/etc/products/L2M/GAMMA_ML.yaml +8 -0
- disdrodb/etc/products/L2M/NGAMMA_GS_LOG_ND_MAE.yaml +6 -0
- disdrodb/etc/products/L2M/NGAMMA_GS_ND_MAE.yaml +6 -0
- disdrodb/etc/products/L2M/NGAMMA_GS_Z_MAE.yaml +6 -0
- disdrodb/etc/products/L2M/global.yaml +26 -0
- disdrodb/issue/writer.py +2 -0
- disdrodb/l0/__init__.py +13 -0
- disdrodb/l0/configs/LPM/l0b_cf_attrs.yml +4 -4
- disdrodb/l0/configs/PARSIVEL/l0b_cf_attrs.yml +1 -1
- disdrodb/l0/configs/PARSIVEL/l0b_encodings.yml +3 -3
- disdrodb/l0/configs/PARSIVEL/raw_data_format.yml +1 -1
- disdrodb/l0/configs/PARSIVEL2/l0b_cf_attrs.yml +5 -5
- disdrodb/l0/configs/PARSIVEL2/l0b_encodings.yml +3 -3
- disdrodb/l0/configs/PARSIVEL2/raw_data_format.yml +1 -1
- disdrodb/l0/configs/PWS100/l0b_cf_attrs.yml +4 -4
- disdrodb/l0/configs/PWS100/raw_data_format.yml +1 -1
- disdrodb/l0/l0a_processing.py +37 -32
- disdrodb/l0/l0b_nc_processing.py +118 -8
- disdrodb/l0/l0b_processing.py +30 -65
- disdrodb/l0/l0c_processing.py +369 -259
- disdrodb/l0/readers/LPM/ARM/ARM_LPM.py +7 -0
- disdrodb/l0/readers/LPM/NETHERLANDS/DELFT_LPM_NC.py +66 -0
- disdrodb/l0/readers/LPM/SLOVENIA/{CRNI_VRH.py → UL.py} +3 -0
- disdrodb/l0/readers/LPM/SWITZERLAND/INNERERIZ_LPM.py +195 -0
- disdrodb/l0/readers/PARSIVEL/GPM/PIERS.py +0 -2
- disdrodb/l0/readers/PARSIVEL/JAPAN/JMA.py +4 -1
- disdrodb/l0/readers/PARSIVEL/NCAR/PECAN_MOBILE.py +1 -1
- disdrodb/l0/readers/PARSIVEL/NCAR/VORTEX2_2009.py +1 -1
- disdrodb/l0/readers/PARSIVEL2/ARM/ARM_PARSIVEL2.py +4 -0
- disdrodb/l0/readers/PARSIVEL2/BELGIUM/ILVO.py +168 -0
- disdrodb/l0/readers/PARSIVEL2/CANADA/UQAM_NC.py +69 -0
- disdrodb/l0/readers/PARSIVEL2/DENMARK/DTU.py +165 -0
- disdrodb/l0/readers/PARSIVEL2/FINLAND/FMI_PARSIVEL2.py +69 -0
- disdrodb/l0/readers/PARSIVEL2/FRANCE/ENPC_PARSIVEL2.py +255 -134
- disdrodb/l0/readers/PARSIVEL2/FRANCE/OSUG.py +525 -0
- disdrodb/l0/readers/PARSIVEL2/FRANCE/SIRTA_PARSIVEL2.py +1 -1
- disdrodb/l0/readers/PARSIVEL2/GPM/GCPEX.py +9 -7
- disdrodb/l0/readers/PARSIVEL2/KIT/BURKINA_FASO.py +1 -1
- disdrodb/l0/readers/PARSIVEL2/KIT/TEAMX.py +123 -0
- disdrodb/l0/readers/PARSIVEL2/{NETHERLANDS/DELFT.py → MPI/BCO_PARSIVEL2.py} +41 -71
- disdrodb/l0/readers/PARSIVEL2/MPI/BOWTIE.py +220 -0
- disdrodb/l0/readers/PARSIVEL2/NASA/APU.py +120 -0
- disdrodb/l0/readers/PARSIVEL2/NASA/LPVEX.py +109 -0
- disdrodb/l0/readers/PARSIVEL2/NCAR/FARM_PARSIVEL2.py +1 -0
- disdrodb/l0/readers/PARSIVEL2/NCAR/PECAN_FP3.py +1 -1
- disdrodb/l0/readers/PARSIVEL2/NCAR/PERILS_MIPS.py +126 -0
- disdrodb/l0/readers/PARSIVEL2/NCAR/PERILS_PIPS.py +165 -0
- disdrodb/l0/readers/PARSIVEL2/NCAR/VORTEX_SE_2016_P2.py +1 -1
- disdrodb/l0/readers/PARSIVEL2/NCAR/VORTEX_SE_2016_PIPS.py +20 -12
- disdrodb/l0/readers/PARSIVEL2/NETHERLANDS/DELFT_NC.py +5 -0
- disdrodb/l0/readers/PARSIVEL2/SPAIN/CENER.py +144 -0
- disdrodb/l0/readers/PARSIVEL2/SPAIN/CR1000DL.py +201 -0
- disdrodb/l0/readers/PARSIVEL2/SPAIN/LIAISE.py +137 -0
- disdrodb/l0/readers/PARSIVEL2/USA/C3WE.py +146 -0
- disdrodb/l0/readers/PWS100/FRANCE/ENPC_PWS100.py +105 -99
- disdrodb/l0/readers/PWS100/FRANCE/ENPC_PWS100_SIRTA.py +151 -0
- disdrodb/l1/__init__.py +5 -0
- disdrodb/l1/fall_velocity.py +46 -0
- disdrodb/l1/filters.py +34 -20
- disdrodb/l1/processing.py +46 -45
- disdrodb/l1/resampling.py +77 -66
- disdrodb/l1_env/routines.py +18 -3
- disdrodb/l2/__init__.py +7 -0
- disdrodb/l2/empirical_dsd.py +58 -10
- disdrodb/l2/processing.py +268 -117
- disdrodb/metadata/checks.py +132 -125
- disdrodb/metadata/standards.py +3 -1
- disdrodb/psd/fitting.py +631 -345
- disdrodb/psd/models.py +9 -6
- disdrodb/routines/__init__.py +54 -0
- disdrodb/{l0/routines.py → routines/l0.py} +316 -355
- disdrodb/{l1/routines.py → routines/l1.py} +76 -116
- disdrodb/routines/l2.py +1019 -0
- disdrodb/{routines.py → routines/wrappers.py} +98 -10
- disdrodb/scattering/__init__.py +16 -4
- disdrodb/scattering/axis_ratio.py +61 -37
- disdrodb/scattering/permittivity.py +504 -0
- disdrodb/scattering/routines.py +746 -184
- disdrodb/summary/__init__.py +17 -0
- disdrodb/summary/routines.py +4196 -0
- disdrodb/utils/archiving.py +434 -0
- disdrodb/utils/attrs.py +68 -125
- disdrodb/utils/cli.py +5 -5
- disdrodb/utils/compression.py +30 -1
- disdrodb/utils/dask.py +121 -9
- disdrodb/utils/dataframe.py +61 -7
- disdrodb/utils/decorators.py +31 -0
- disdrodb/utils/directories.py +35 -15
- disdrodb/utils/encoding.py +37 -19
- disdrodb/{l2 → utils}/event.py +15 -173
- disdrodb/utils/logger.py +14 -7
- disdrodb/utils/manipulations.py +81 -0
- disdrodb/utils/routines.py +166 -0
- disdrodb/utils/subsetting.py +214 -0
- disdrodb/utils/time.py +35 -177
- disdrodb/utils/writer.py +20 -7
- disdrodb/utils/xarray.py +5 -4
- disdrodb/viz/__init__.py +13 -0
- disdrodb/viz/plots.py +398 -0
- {disdrodb-0.1.2.dist-info → disdrodb-0.1.4.dist-info}/METADATA +4 -3
- {disdrodb-0.1.2.dist-info → disdrodb-0.1.4.dist-info}/RECORD +139 -98
- {disdrodb-0.1.2.dist-info → disdrodb-0.1.4.dist-info}/entry_points.txt +2 -0
- disdrodb/l1/encoding_attrs.py +0 -642
- disdrodb/l2/processing_options.py +0 -213
- disdrodb/l2/routines.py +0 -868
- /disdrodb/l0/readers/PARSIVEL/SLOVENIA/{UL_FGG.py → UL.py} +0 -0
- {disdrodb-0.1.2.dist-info → disdrodb-0.1.4.dist-info}/WHEEL +0 -0
- {disdrodb-0.1.2.dist-info → disdrodb-0.1.4.dist-info}/licenses/LICENSE +0 -0
- {disdrodb-0.1.2.dist-info → disdrodb-0.1.4.dist-info}/top_level.txt +0 -0
|
@@ -17,12 +17,112 @@
|
|
|
17
17
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18
18
|
# -----------------------------------------------------------------------------.
|
|
19
19
|
"""DISDRODB reader for ENPC PWS100 raw text data."""
|
|
20
|
-
import zipfile
|
|
21
|
-
|
|
22
20
|
import pandas as pd
|
|
23
21
|
|
|
24
22
|
from disdrodb.l0.l0_reader import is_documented_by, reader_generic_docstring
|
|
25
|
-
from disdrodb.utils.logger import log_error, log_warning
|
|
23
|
+
from disdrodb.utils.logger import log_error # , log_warning
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def read_txt_file(file, filename, logger):
|
|
27
|
+
r"""Parse a single txt file within the daily zip file.
|
|
28
|
+
|
|
29
|
+
The file starts with \\x020 and ends with \\x03\\r\\n.
|
|
30
|
+
"""
|
|
31
|
+
# Read file
|
|
32
|
+
try:
|
|
33
|
+
txt = file.readline().decode("utf-8")
|
|
34
|
+
except Exception:
|
|
35
|
+
log_error(logger=logger, msg=f"{filename} is corrupted", verbose=False)
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
# Check file is not empty
|
|
39
|
+
if txt == "":
|
|
40
|
+
log_error(logger=logger, msg=f"{filename} is empty", verbose=False)
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
# Remove everything before \x020
|
|
44
|
+
# - At start it can appear: \x013 0 \x02PSU voltage too low 13.3\x03\r\n\
|
|
45
|
+
txt = txt.split("x020 ", 1)[-1]
|
|
46
|
+
|
|
47
|
+
# Remove everything after \\x03
|
|
48
|
+
txt = txt.split("x03", 1)[0]
|
|
49
|
+
|
|
50
|
+
# if "PSU voltage too low" in txt or "volt" in txt:
|
|
51
|
+
# log_warning(logger=logger, msg=f"PSU voltage too low in {filename}", verbose=False)
|
|
52
|
+
# return None
|
|
53
|
+
|
|
54
|
+
# if "Error - message" in txt:
|
|
55
|
+
# log_warning(logger=logger, msg=f"Error message in {filename}", verbose=False)
|
|
56
|
+
# return None
|
|
57
|
+
|
|
58
|
+
# Clean up the line
|
|
59
|
+
txt = txt.replace(" 00 ", " 0 0 ")
|
|
60
|
+
txt = txt.replace(" ", " 0 ")
|
|
61
|
+
|
|
62
|
+
# Split the cleaned line
|
|
63
|
+
buf = txt.split(" ")
|
|
64
|
+
|
|
65
|
+
# Helper to convert list of floats to comma-separated string
|
|
66
|
+
def int_list_to_str(lst):
|
|
67
|
+
return ",".join(f"{int(i)}" for i in lst)
|
|
68
|
+
|
|
69
|
+
# Try to get the drop_size distribution:
|
|
70
|
+
try:
|
|
71
|
+
drop_size_distribution = int_list_to_str(buf[29:329]) # Drop size distribution (message field 42)
|
|
72
|
+
except Exception:
|
|
73
|
+
log_error(logger, msg=f"Corrupted drop_size_distribution field in {filename}", verbose=False)
|
|
74
|
+
return None
|
|
75
|
+
|
|
76
|
+
# Try to get peak_to_pedestal_hist
|
|
77
|
+
try:
|
|
78
|
+
peak_to_pedestal_hist = int_list_to_str(buf[1498:1548])
|
|
79
|
+
except Exception:
|
|
80
|
+
log_error(
|
|
81
|
+
logger,
|
|
82
|
+
msg=f"Corrupted raw_drop_number or peak_to_pedestal_hist field in {filename}",
|
|
83
|
+
verbose=False,
|
|
84
|
+
)
|
|
85
|
+
return None
|
|
86
|
+
|
|
87
|
+
# Parse fields
|
|
88
|
+
data = {
|
|
89
|
+
"mor_visibility": float(buf[1]), # Visibility Range (message field 20)
|
|
90
|
+
"weather_code_synop_4680": float(buf[2]), # Present Weather Code (WMO) (message field 21)
|
|
91
|
+
"weather_code_metar_4678": buf[3], # Present Weather Code (METAR) (message field 22)
|
|
92
|
+
"weather_code_nws": buf[4], # Present Weather Code (NWS) (message field 23)
|
|
93
|
+
"alarms": int_list_to_str(buf[5:21]), # Alarms (message field (24))
|
|
94
|
+
"sensor_status": buf[21], # Fault status of PWS100 (message field 25)
|
|
95
|
+
"air_temperature": float(buf[22]), # Temperature (°C) (message field 30)
|
|
96
|
+
"relative_humidity": float(buf[23]), # Sampled relative humidity (%) (message field 30)
|
|
97
|
+
"wetbulb_temperature": float(buf[24]), # Average wetbulb temperature (°C)(message field 30)
|
|
98
|
+
"air_temperature_max": float(buf[25]), # Maximum temperature (°C)(message field 31)
|
|
99
|
+
"air_temperature_min": float(buf[26]), # Minimum temperature (°C)(message field 31)
|
|
100
|
+
"rainfall_rate": float(buf[27]), # Precipitation rate (mm/h)(message field 40)
|
|
101
|
+
"rainfall_accumulated": float(buf[28]), # Precipitation accumulation (mm/h)(message field 41)
|
|
102
|
+
"drop_size_distribution": drop_size_distribution, # Drop size distribution (message field 42)
|
|
103
|
+
"average_drop_velocity": float(buf[329]), # Average velocity (mm/s)(message field 43)
|
|
104
|
+
"average_drop_size": float(buf[330]), # Average size (mm/h)(message field 43)
|
|
105
|
+
"type_distribution": int_list_to_str(buf[331:342]), # Type distribution (message field 44)
|
|
106
|
+
"raw_drop_number": int_list_to_str(buf[342:1498]), # Size/velocity spectrum (34*34) (message field 47)
|
|
107
|
+
"peak_to_pedestal_hist": (
|
|
108
|
+
peak_to_pedestal_hist # Peak to pedestal ratio distribution histogram (message field 48)
|
|
109
|
+
),
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
# Convert to single-row DataFrame
|
|
113
|
+
df = pd.DataFrame([data])
|
|
114
|
+
|
|
115
|
+
# Define datetime "time" column from filename
|
|
116
|
+
datetime_str = " ".join(filename.replace(".txt", "").split("_")[-6:])
|
|
117
|
+
df["time"] = pd.to_datetime(datetime_str, format="%Y %m %d %H %M %S")
|
|
118
|
+
|
|
119
|
+
# # Drop columns not agreeing with DISDRODB L0 standards
|
|
120
|
+
# columns_to_drop = [
|
|
121
|
+
# "peak_to_pedestal_hist",
|
|
122
|
+
# "type_distribution",
|
|
123
|
+
# ]
|
|
124
|
+
# df = df.drop(columns=columns_to_drop)
|
|
125
|
+
return df
|
|
26
126
|
|
|
27
127
|
|
|
28
128
|
@is_documented_by(reader_generic_docstring)
|
|
@@ -31,102 +131,9 @@ def reader(
|
|
|
31
131
|
logger=None,
|
|
32
132
|
):
|
|
33
133
|
"""Reader."""
|
|
134
|
+
import zipfile
|
|
34
135
|
|
|
35
|
-
|
|
36
|
-
#### Define function to read each txt file inside each daily zip file
|
|
37
|
-
def read_txt_file(file, filename, logger): # noqa PLR0911
|
|
38
|
-
"""Parse a single txt file within the daily zip file."""
|
|
39
|
-
# Read file
|
|
40
|
-
try:
|
|
41
|
-
txt = file.readline().decode("utf-8")
|
|
42
|
-
except Exception:
|
|
43
|
-
log_warning(logger=logger, msg=f"{filename} is corrupted", verbose=False)
|
|
44
|
-
return None
|
|
45
|
-
|
|
46
|
-
# Check file is not empty
|
|
47
|
-
if txt == "":
|
|
48
|
-
log_warning(logger=logger, msg=f"{filename} is empty", verbose=False)
|
|
49
|
-
return None
|
|
50
|
-
|
|
51
|
-
if "PSU voltage too low" in txt or "volt" in txt:
|
|
52
|
-
log_warning(logger=logger, msg=f"PSU voltage too low in {filename}", verbose=False)
|
|
53
|
-
return None
|
|
54
|
-
|
|
55
|
-
if "Error - message" in txt:
|
|
56
|
-
log_warning(logger=logger, msg=f"Error message in {filename}", verbose=False)
|
|
57
|
-
return None
|
|
58
|
-
|
|
59
|
-
# Clean up the line
|
|
60
|
-
txt = txt.replace(" 00 ", " 0 0 ")
|
|
61
|
-
txt = txt.replace(" ", " 0 ")
|
|
62
|
-
txt = txt[1:-8]
|
|
63
|
-
|
|
64
|
-
# Split the cleaned line
|
|
65
|
-
buf = txt.split(" ")
|
|
66
|
-
|
|
67
|
-
# Helper to convert list of floats to comma-separated string
|
|
68
|
-
def int_list_to_str(lst):
|
|
69
|
-
return ",".join(f"{int(i)}" for i in lst)
|
|
70
|
-
|
|
71
|
-
# Try to get the drop_size distribution:
|
|
72
|
-
try:
|
|
73
|
-
drop_size_distribution = int_list_to_str(buf[30:330]) # Drop size distribution (message field 42)
|
|
74
|
-
except Exception:
|
|
75
|
-
log_warning(logger, msg=f"Corrupted drop_size_distribution field in {filename}", verbose=False)
|
|
76
|
-
return None
|
|
77
|
-
|
|
78
|
-
# Try to get peak_to_pedestal_hist
|
|
79
|
-
try:
|
|
80
|
-
peak_to_pedestal_hist = int_list_to_str(buf[1499:1549])
|
|
81
|
-
except Exception:
|
|
82
|
-
log_warning(
|
|
83
|
-
logger,
|
|
84
|
-
msg=f"Corrupted raw_drop_number or peak_to_pedestal_hist field in {filename}",
|
|
85
|
-
verbose=False,
|
|
86
|
-
)
|
|
87
|
-
return None
|
|
88
|
-
# Parse fields
|
|
89
|
-
data = {
|
|
90
|
-
"mor_visibility": float(buf[2]), # Visibility Range (message field 20)
|
|
91
|
-
"weather_code_synop_4680": float(buf[3]), # Present Weather Code (WMO) (message field 21)
|
|
92
|
-
"weather_code_metar_4678": buf[4], # Present Weather Code (METAR) (message field 22)
|
|
93
|
-
"weather_code_nws": buf[5], # Present Weather Code (NWS) (message field 23)
|
|
94
|
-
"alarms": int_list_to_str(buf[6:22]), # Alarms (message field (24))
|
|
95
|
-
"sensor_status": buf[22], # Fault status of PWS100 (message field 25)
|
|
96
|
-
"air_temperature": float(buf[23]), # Temperature (°C) (message field 30)
|
|
97
|
-
"relative_humidity": float(buf[24]), # Sampled relative humidity (%) (message field 30)
|
|
98
|
-
"wetbulb_temperature": float(buf[25]), # Average wetbulb temperature (°C)(message field 30)
|
|
99
|
-
"air_temperature_max": float(buf[26]), # Maximum temperature (°C)(message field 31)
|
|
100
|
-
"air_temperature_min": float(buf[27]), # Minimum temperature (°C)(message field 31)
|
|
101
|
-
"rainfall_rate": float(buf[28]), # Precipitation rate (mm/h)(message field 40)
|
|
102
|
-
"rainfall_accumulated": float(buf[29]), # Precipitation accumulation (mm/h)(message field 41)
|
|
103
|
-
"drop_size_distribution": drop_size_distribution, # Drop size distribution (message field 42)
|
|
104
|
-
"average_drop_velocity": float(buf[330]), # Average velocity (mm/s)(message field 43)
|
|
105
|
-
"average_drop_size": float(buf[331]), # Average size (mm/h)(message field 43)
|
|
106
|
-
"type_distribution": int_list_to_str(buf[332:343]), # Type distribution (message field 44)
|
|
107
|
-
"raw_drop_number": int_list_to_str(buf[343:1499]), # Size/velocity spectrum (34*34) (message field 47)
|
|
108
|
-
"peak_to_pedestal_hist": (
|
|
109
|
-
peak_to_pedestal_hist # Peak to pedestal ratio distribution histogram (message field 48)
|
|
110
|
-
),
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
# Convert to single-row DataFrame
|
|
114
|
-
df = pd.DataFrame([data])
|
|
115
|
-
|
|
116
|
-
# Define datetime "time" column from filename
|
|
117
|
-
datetime_str = " ".join(filename.replace(".txt", "").split("_")[-6:])
|
|
118
|
-
df["time"] = pd.to_datetime(datetime_str, format="%Y %m %d %H %M %S")
|
|
119
|
-
|
|
120
|
-
# # Drop columns not agreeing with DISDRODB L0 standards
|
|
121
|
-
# columns_to_drop = [
|
|
122
|
-
# "peak_to_pedestal_hist",
|
|
123
|
-
# "type_distribution",
|
|
124
|
-
# ]
|
|
125
|
-
# df = df.drop(columns=columns_to_drop)
|
|
126
|
-
return df
|
|
127
|
-
|
|
128
|
-
# ---------------------------------------------------------------------.
|
|
129
|
-
#### Iterate over all files (aka timesteps) in the daily zip archive
|
|
136
|
+
# Iterate over all files (aka timesteps) in the daily zip archive
|
|
130
137
|
# - Each file contain a single timestep !
|
|
131
138
|
list_df = []
|
|
132
139
|
with zipfile.ZipFile(filepath, "r") as zip_ref:
|
|
@@ -146,5 +153,4 @@ def reader(
|
|
|
146
153
|
# Concatenate all dataframes into a single one
|
|
147
154
|
df = pd.concat(list_df)
|
|
148
155
|
|
|
149
|
-
# ---------------------------------------------------------------------.
|
|
150
156
|
return df
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# -----------------------------------------------------------------------------.
|
|
2
|
+
# Copyright (c) 2021-2023 DISDRODB developers
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
# -----------------------------------------------------------------------------.
|
|
17
|
+
"""DISDRODB reader for ENPC PWS100 raw text data."""
|
|
18
|
+
import zipfile
|
|
19
|
+
|
|
20
|
+
import pandas as pd
|
|
21
|
+
|
|
22
|
+
from disdrodb.l0.l0_reader import is_documented_by, reader_generic_docstring
|
|
23
|
+
from disdrodb.utils.logger import log_error # , log_warning
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@is_documented_by(reader_generic_docstring)
|
|
27
|
+
def reader(
|
|
28
|
+
filepath,
|
|
29
|
+
logger=None,
|
|
30
|
+
):
|
|
31
|
+
"""Reader."""
|
|
32
|
+
|
|
33
|
+
##------------------------------------------------------------------------.
|
|
34
|
+
#### Define function to read each txt file inside each daily zip file
|
|
35
|
+
def read_txt_file(file, filename, logger):
|
|
36
|
+
r"""Parse a single txt file within the daily zip file.
|
|
37
|
+
|
|
38
|
+
The file starts with \x020 and ends with \x03\r\r\n.
|
|
39
|
+
"""
|
|
40
|
+
# Read file
|
|
41
|
+
try:
|
|
42
|
+
txt = file.readline().decode("utf-8")
|
|
43
|
+
except Exception:
|
|
44
|
+
log_error(logger=logger, msg=f"{filename} is corrupted", verbose=False)
|
|
45
|
+
return None
|
|
46
|
+
|
|
47
|
+
# Check file is not empty
|
|
48
|
+
if txt == "":
|
|
49
|
+
log_error(logger=logger, msg=f"{filename} is empty", verbose=False)
|
|
50
|
+
return None
|
|
51
|
+
|
|
52
|
+
# if "PSU voltage too low" in txt or "volt" in txt:
|
|
53
|
+
# log_warning(logger=logger, msg=f"PSU voltage too low in {filename}", verbose=False)
|
|
54
|
+
# return None
|
|
55
|
+
|
|
56
|
+
# if "Error - message" in txt:
|
|
57
|
+
# log_warning(logger=logger, msg=f"Error message in {filename}", verbose=False)
|
|
58
|
+
# return None
|
|
59
|
+
|
|
60
|
+
# Clean up the line
|
|
61
|
+
txt = txt.replace(" 00 ", " 0 0 ")
|
|
62
|
+
txt = txt.replace(" ", " 0 ")
|
|
63
|
+
txt = txt[1:-8]
|
|
64
|
+
|
|
65
|
+
# Split the cleaned line
|
|
66
|
+
buf = txt.split(" ")
|
|
67
|
+
|
|
68
|
+
# Helper to convert list of floats to comma-separated string
|
|
69
|
+
def int_list_to_str(lst):
|
|
70
|
+
return ",".join(f"{int(i)}" for i in lst)
|
|
71
|
+
|
|
72
|
+
# Try to get the drop_size distribution:
|
|
73
|
+
try:
|
|
74
|
+
drop_size_distribution = int_list_to_str(buf[30:330]) # Drop size distribution (message field 42)
|
|
75
|
+
except Exception:
|
|
76
|
+
log_error(logger, msg=f"Corrupted drop_size_distribution field in {filename}", verbose=False)
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
# Try to get peak_to_pedestal_hist
|
|
80
|
+
try:
|
|
81
|
+
peak_to_pedestal_hist = int_list_to_str(buf[1499:1549])
|
|
82
|
+
except Exception:
|
|
83
|
+
log_error(
|
|
84
|
+
logger,
|
|
85
|
+
msg=f"Corrupted raw_drop_number or peak_to_pedestal_hist field in {filename}",
|
|
86
|
+
verbose=False,
|
|
87
|
+
)
|
|
88
|
+
return None
|
|
89
|
+
# Parse fields
|
|
90
|
+
data = {
|
|
91
|
+
"mor_visibility": float(buf[2]), # Visibility Range (message field 20)
|
|
92
|
+
"weather_code_synop_4680": float(buf[3]), # Present Weather Code (WMO) (message field 21)
|
|
93
|
+
"weather_code_metar_4678": buf[4], # Present Weather Code (METAR) (message field 22)
|
|
94
|
+
"weather_code_nws": buf[5], # Present Weather Code (NWS) (message field 23)
|
|
95
|
+
"alarms": int_list_to_str(buf[6:22]), # Alarms (message field (24))
|
|
96
|
+
"sensor_status": buf[22], # Fault status of PWS100 (message field 25)
|
|
97
|
+
"air_temperature": float(buf[23]), # Temperature (°C) (message field 30)
|
|
98
|
+
"relative_humidity": float(buf[24]), # Sampled relative humidity (%) (message field 30)
|
|
99
|
+
"wetbulb_temperature": float(buf[25]), # Average wetbulb temperature (°C)(message field 30)
|
|
100
|
+
"air_temperature_max": float(buf[26]), # Maximum temperature (°C)(message field 31)
|
|
101
|
+
"air_temperature_min": float(buf[27]), # Minimum temperature (°C)(message field 31)
|
|
102
|
+
"rainfall_rate": float(buf[28]), # Precipitation rate (mm/h)(message field 40)
|
|
103
|
+
"rainfall_accumulated": float(buf[29]), # Precipitation accumulation (mm/h)(message field 41)
|
|
104
|
+
"drop_size_distribution": drop_size_distribution, # Drop size distribution (message field 42)
|
|
105
|
+
"average_drop_velocity": float(buf[330]), # Average velocity (mm/s)(message field 43)
|
|
106
|
+
"average_drop_size": float(buf[331]), # Average size (mm/h)(message field 43)
|
|
107
|
+
"type_distribution": int_list_to_str(buf[332:343]), # Type distribution (message field 44)
|
|
108
|
+
"raw_drop_number": int_list_to_str(buf[343:1499]), # Size/velocity spectrum (34*34) (message field 47)
|
|
109
|
+
"peak_to_pedestal_hist": (
|
|
110
|
+
peak_to_pedestal_hist # Peak to pedestal ratio distribution histogram (message field 48)
|
|
111
|
+
),
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# Convert to single-row DataFrame
|
|
115
|
+
df = pd.DataFrame([data])
|
|
116
|
+
|
|
117
|
+
# Define datetime "time" column from filename
|
|
118
|
+
datetime_str = " ".join(filename.replace(".txt", "").split("_")[-6:])
|
|
119
|
+
df["time"] = pd.to_datetime(datetime_str, format="%Y %m %d %H %M %S")
|
|
120
|
+
|
|
121
|
+
# # Drop columns not agreeing with DISDRODB L0 standards
|
|
122
|
+
# columns_to_drop = [
|
|
123
|
+
# "peak_to_pedestal_hist",
|
|
124
|
+
# "type_distribution",
|
|
125
|
+
# ]
|
|
126
|
+
# df = df.drop(columns=columns_to_drop)
|
|
127
|
+
return df
|
|
128
|
+
|
|
129
|
+
# ---------------------------------------------------------------------.
|
|
130
|
+
#### Iterate over all files (aka timesteps) in the daily zip archive
|
|
131
|
+
# - Each file contain a single timestep !
|
|
132
|
+
list_df = []
|
|
133
|
+
with zipfile.ZipFile(filepath, "r") as zip_ref:
|
|
134
|
+
filenames = sorted(zip_ref.namelist())
|
|
135
|
+
for filename in filenames:
|
|
136
|
+
if filename.endswith(".txt"):
|
|
137
|
+
# Open file
|
|
138
|
+
with zip_ref.open(filename) as f:
|
|
139
|
+
try:
|
|
140
|
+
df = read_txt_file(file=f, filename=filename, logger=logger)
|
|
141
|
+
if df is not None:
|
|
142
|
+
list_df.append(df)
|
|
143
|
+
except Exception as e:
|
|
144
|
+
msg = f"An error occurred while reading {filename}. The error is: {e}."
|
|
145
|
+
log_error(logger=logger, msg=msg, verbose=True)
|
|
146
|
+
|
|
147
|
+
# Concatenate all dataframes into a single one
|
|
148
|
+
df = pd.concat(list_df)
|
|
149
|
+
|
|
150
|
+
# ---------------------------------------------------------------------.
|
|
151
|
+
return df
|
disdrodb/l1/__init__.py
CHANGED
|
@@ -15,3 +15,8 @@
|
|
|
15
15
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
16
|
# -----------------------------------------------------------------------------.
|
|
17
17
|
"""DISDRODB L1 module."""
|
|
18
|
+
from disdrodb.l1.processing import generate_l1
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"generate_l1",
|
|
22
|
+
]
|
disdrodb/l1/fall_velocity.py
CHANGED
|
@@ -195,6 +195,7 @@ def ensure_valid_coordinates(ds, default_altitude=0, default_latitude=0, default
|
|
|
195
195
|
The dataset with invalid coordinates replaced by default values.
|
|
196
196
|
|
|
197
197
|
"""
|
|
198
|
+
# TODO raise error if not present
|
|
198
199
|
invalid_altitude = np.logical_or(np.isnan(ds["altitude"]), ds["altitude"] == -9999)
|
|
199
200
|
ds["altitude"] = ds["altitude"].where(~invalid_altitude, default_altitude)
|
|
200
201
|
|
|
@@ -250,6 +251,12 @@ def get_raindrop_fall_velocity(diameter, method, ds_env=None):
|
|
|
250
251
|
raise ValueError(f"{method} is an invalid fall velocity method. Valid methods: {available_methods}.")
|
|
251
252
|
# Copy diameter
|
|
252
253
|
diameter = diameter.copy()
|
|
254
|
+
# Initialize ds_env if None
|
|
255
|
+
# if ds_env is None:
|
|
256
|
+
# ds_env = load_env_dataset(ds_env)
|
|
257
|
+
|
|
258
|
+
# TODO: wrapper for DISDRODB product !
|
|
259
|
+
|
|
253
260
|
# Ensure valid altitude and geolocation (if missing set defaults)
|
|
254
261
|
# - altitude required by Beard
|
|
255
262
|
# - latitude required for gravity
|
|
@@ -258,3 +265,42 @@ def get_raindrop_fall_velocity(diameter, method, ds_env=None):
|
|
|
258
265
|
func = dict_methods[method]
|
|
259
266
|
fall_velocity = func(diameter, ds_env=ds_env) if method == "Beard1976" else func(diameter)
|
|
260
267
|
return fall_velocity
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def get_dataset_fall_velocity(ds, method="Brandes2002"):
|
|
271
|
+
"""Compute the fall velocity and add it to the dataset.
|
|
272
|
+
|
|
273
|
+
Parameters
|
|
274
|
+
----------
|
|
275
|
+
ds : xarray.Dataset
|
|
276
|
+
DISDRODB L0C dataset.
|
|
277
|
+
method : str, optional
|
|
278
|
+
Method to compute fall velocity. The default method is ``"Brandes2002"``.
|
|
279
|
+
|
|
280
|
+
Returns
|
|
281
|
+
-------
|
|
282
|
+
xarray.Dataset
|
|
283
|
+
DISDRODB L0C dataset with an additional variable 'fall_velocity'.
|
|
284
|
+
"""
|
|
285
|
+
from disdrodb.constants import DIAMETER_DIMENSION
|
|
286
|
+
from disdrodb.l1_env.routines import load_env_dataset
|
|
287
|
+
|
|
288
|
+
# Check if diameter dimension exists
|
|
289
|
+
if DIAMETER_DIMENSION not in ds.dims:
|
|
290
|
+
raise ValueError(f"Diameter dimension '{DIAMETER_DIMENSION}' not found in dataset dimensions.")
|
|
291
|
+
|
|
292
|
+
# Retrieve diameter values (in mm)
|
|
293
|
+
diameter_bin_center = ds["diameter_bin_center"]
|
|
294
|
+
|
|
295
|
+
# Ensure valid altitude and geolocation (if missing set defaults)
|
|
296
|
+
# TODO: MOBILE CASE !
|
|
297
|
+
default_geolocation = {"altitude": 0, "latitude": 0, "longitude": 0}
|
|
298
|
+
dataset_coords = {key: ds[key] for key in default_geolocation if key in ds}
|
|
299
|
+
default_geolocation.update(dataset_coords)
|
|
300
|
+
ds = ds.assign_coords(default_geolocation)
|
|
301
|
+
|
|
302
|
+
# TODO: deal with ENV dataset
|
|
303
|
+
ds_env = load_env_dataset(ds)
|
|
304
|
+
|
|
305
|
+
fall_velocity = get_raindrop_fall_velocity(diameter_bin_center, method=method, ds_env=ds_env)
|
|
306
|
+
return fall_velocity
|
disdrodb/l1/filters.py
CHANGED
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
import numpy as np
|
|
20
20
|
import xarray as xr
|
|
21
21
|
|
|
22
|
+
from disdrodb.constants import DIAMETER_DIMENSION, VELOCITY_DIMENSION
|
|
23
|
+
|
|
22
24
|
|
|
23
25
|
def filter_diameter_bins(ds, minimum_diameter=None, maximum_diameter=None):
|
|
24
26
|
"""
|
|
@@ -29,10 +31,10 @@ def filter_diameter_bins(ds, minimum_diameter=None, maximum_diameter=None):
|
|
|
29
31
|
ds : xarray.Dataset
|
|
30
32
|
The dataset containing diameter bin data.
|
|
31
33
|
minimum_diameter : float, optional
|
|
32
|
-
The minimum diameter to
|
|
34
|
+
The minimum diameter to be included, in millimeters.
|
|
33
35
|
Defaults to the minimum value in `ds["diameter_bin_lower"]`.
|
|
34
36
|
maximum_diameter : float, optional
|
|
35
|
-
The maximum diameter to
|
|
37
|
+
The maximum diameter to be included, in millimeters.
|
|
36
38
|
Defaults to the maximum value in `ds["diameter_bin_upper"]`.
|
|
37
39
|
|
|
38
40
|
Returns
|
|
@@ -40,22 +42,28 @@ def filter_diameter_bins(ds, minimum_diameter=None, maximum_diameter=None):
|
|
|
40
42
|
xarray.Dataset
|
|
41
43
|
The filtered dataset containing only the specified diameter bins.
|
|
42
44
|
"""
|
|
45
|
+
# Put data into memory
|
|
46
|
+
ds["diameter_bin_lower"] = ds["diameter_bin_lower"].compute()
|
|
47
|
+
ds["diameter_bin_upper"] = ds["diameter_bin_upper"].compute()
|
|
48
|
+
|
|
43
49
|
# Initialize default arguments
|
|
44
50
|
if minimum_diameter is None:
|
|
45
51
|
minimum_diameter = ds["diameter_bin_lower"].min().item()
|
|
46
52
|
if maximum_diameter is None:
|
|
47
53
|
maximum_diameter = ds["diameter_bin_upper"].max().item()
|
|
48
|
-
|
|
54
|
+
|
|
55
|
+
# Select bins which overlap the specified diameters
|
|
49
56
|
valid_indices = np.logical_and(
|
|
50
|
-
ds["
|
|
51
|
-
ds["
|
|
52
|
-
)
|
|
53
|
-
ds = ds.isel({"diameter_bin_center": valid_indices})
|
|
54
|
-
# Update history
|
|
55
|
-
history = ds.attrs.get("history", "")
|
|
56
|
-
ds.attrs["history"] = (
|
|
57
|
-
history + f" Selected drops with diameters between {minimum_diameter} and {maximum_diameter} mm \n"
|
|
57
|
+
ds["diameter_bin_upper"] > minimum_diameter,
|
|
58
|
+
ds["diameter_bin_lower"] < maximum_diameter,
|
|
58
59
|
)
|
|
60
|
+
|
|
61
|
+
# Select bins with diameter values entirely inside the specified min/max values
|
|
62
|
+
# valid_indices = np.logical_and(
|
|
63
|
+
# ds["diameter_bin_lower"] >= minimum_diameter,
|
|
64
|
+
# ds["diameter_bin_upper"] <= maximum_diameter,
|
|
65
|
+
# )
|
|
66
|
+
ds = ds.isel({DIAMETER_DIMENSION: valid_indices})
|
|
59
67
|
return ds
|
|
60
68
|
|
|
61
69
|
|
|
@@ -79,22 +87,28 @@ def filter_velocity_bins(ds, minimum_velocity=0, maximum_velocity=12):
|
|
|
79
87
|
xarray.Dataset
|
|
80
88
|
The filtered dataset containing only the specified velocity bins.
|
|
81
89
|
"""
|
|
90
|
+
# Put data into memory
|
|
91
|
+
ds["velocity_bin_lower"] = ds["velocity_bin_lower"].compute()
|
|
92
|
+
ds["velocity_bin_upper"] = ds["velocity_bin_upper"].compute()
|
|
93
|
+
|
|
82
94
|
# Initialize default arguments
|
|
83
95
|
if minimum_velocity is None:
|
|
84
96
|
minimum_velocity = ds["velocity_bin_lower"].min().item()
|
|
85
97
|
if maximum_velocity is None:
|
|
86
98
|
maximum_velocity = ds["velocity_bin_upper"].max().item()
|
|
87
|
-
|
|
99
|
+
|
|
100
|
+
# Select bins which overlap the specified velocities
|
|
88
101
|
valid_indices = np.logical_and(
|
|
89
|
-
ds["
|
|
90
|
-
ds["
|
|
91
|
-
)
|
|
92
|
-
ds = ds.isel({"velocity_bin_center": valid_indices})
|
|
93
|
-
# Update history
|
|
94
|
-
history = ds.attrs.get("history", "")
|
|
95
|
-
ds.attrs["history"] = (
|
|
96
|
-
history + f" Selected drops with fall velocity between {minimum_velocity} and {maximum_velocity} m/s \n"
|
|
102
|
+
ds["velocity_bin_upper"] > minimum_velocity,
|
|
103
|
+
ds["velocity_bin_lower"] < maximum_velocity,
|
|
97
104
|
)
|
|
105
|
+
|
|
106
|
+
# Select bins with velocity values entirely inside the specified min/max values
|
|
107
|
+
# valid_indices = np.logical_and(
|
|
108
|
+
# ds["velocity_bin_lower"] >= minimum_velocity,
|
|
109
|
+
# ds["velocity_bin_upper"] <= maximum_velocity,
|
|
110
|
+
# )
|
|
111
|
+
ds = ds.isel({VELOCITY_DIMENSION: valid_indices})
|
|
98
112
|
return ds
|
|
99
113
|
|
|
100
114
|
|