Skip to content

Commit

Permalink
Add thies PT processing
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Jan 15, 2025
1 parent 1543a12 commit 68a44f3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/processing/harmonizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
from .halo_calibrated import harmonize_halo_calibrated_file
from .hatpro import harmonize_hatpro_file
from .parsivel import harmonize_parsivel_file
from .rain_gauge import harmonize_pluvio_nc
from .rain_gauge import harmonize_pluvio_nc, harmonize_thies_pt_nc
from .ws import harmonize_ws_file
10 changes: 4 additions & 6 deletions src/processing/harmonizer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ def copy_variable(self, key: str, time_ind: list | None = None):
if key not in self.nc_raw.variables.keys():
logging.warning(f"Key {key} not found from the source file.")
return

variable = self.nc_raw.variables[key]
dtype = variable.dtype

if key == "time" and "int64" in str(dtype):
dtype = "f8"

dtype = (
"f8" if key == "time" and "int" in str(variable.dtype) else variable.dtype
)
var_out = self.nc.createVariable(
key,
dtype,
Expand Down Expand Up @@ -173,6 +170,7 @@ def get_valid_time_indices(self) -> list:

if "seconds since" in time.units:
time_stamps = np.array(cloudnetpy.utils.seconds2hours(time_stamps))

max_time = 1440 if "minutes" in time.units else 24
valid_ind: list[int] = []
for ind, t in enumerate(time_stamps):
Expand Down
45 changes: 41 additions & 4 deletions src/processing/harmonizer/rain_gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@
from processing.harmonizer import core


def harmonize_thies_pt_nc(data: dict) -> str:
if "output_path" not in data:
temp_file = NamedTemporaryFile()
with (
netCDF4.Dataset(data["full_path"], "r") as nc_raw,
netCDF4.Dataset(
data["output_path"] if "output_path" in data else temp_file.name,
"w",
format="NETCDF4_CLASSIC",
) as nc,
):
gauge = RainGaugeNc(nc_raw, nc, data)
ind = gauge.get_valid_time_indices()
gauge.nc.createDimension("time", len(ind))
gauge.copy_data(("time", "int_m", "am_tot"), ind)
gauge.mask_bad_data_values()
gauge.fix_variable_names()
gauge.clean_global_attributes()
gauge.convert_units()
for key in (
"rainfall_rate",
"rainfall_amount",
):
gauge.harmonize_standard_attributes(key)
gauge.fix_long_names()
uuid = gauge.add_uuid()
gauge.add_global_attributes("rain-gauge", instruments.THIES_PT)
gauge.add_date()
gauge.convert_time()
gauge.add_geolocation()
gauge.add_history("rain-gauge")
if "output_path" not in data:
shutil.copy(temp_file.name, data["full_path"])
return uuid


def harmonize_pluvio_nc(data: dict) -> str:
if "output_path" not in data:
temp_file = NamedTemporaryFile()
Expand All @@ -30,9 +66,6 @@ def harmonize_pluvio_nc(data: dict) -> str:
gauge.fix_variable_names()
gauge.convert_units()
for key in (
"latitude",
"longitude",
"altitude",
"rainfall_rate",
"rainfall_amount",
):
Expand Down Expand Up @@ -69,7 +102,7 @@ def _copy_variable(self, key: str, time_ind: list):
return

variable = self.nc_raw.variables[key]
dtype = "f8" if key == "time" and "int64" in str(variable.dtype) else "f4"
dtype = "f8" if key == "time" and "int" in str(variable.dtype) else "f4"
fill_value = netCDF4.default_fillvals[dtype] if key != "time" else None
var_out = self.nc.createVariable(
key, dtype, "time", zlib=True, fill_value=fill_value
Expand All @@ -88,7 +121,9 @@ def _copy_variable_attributes(var_in: netCDF4.Variable, var_out: netCDF4.Variabl
def fix_variable_names(self):
keymap = {
"rain_rate": "rainfall_rate",
"int_m": "rainfall_rate",
"total_accum_NRT": "rainfall_amount",
"am_tot": "rainfall_amount",
}
self.fix_name(keymap)

Expand All @@ -108,5 +143,7 @@ def convert_units(self):
0
]
for key in ("r_accum_RT", "r_accum_NRT", "rainfall_amount"):
if key not in self.nc.variables:
continue
self.nc.variables[key].units = "m"
self.nc.variables[key][:] *= mm_to_m
5 changes: 5 additions & 0 deletions src/processing/instrument_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ def process_pluvio(self):
data = self._get_payload_for_nc_file_augmenter(full_path)
self.uuid.product = harmonizer.harmonize_pluvio_nc(data)

def process_thies_precipitation_transmitter(self):
full_path, self.uuid.raw = self.download_instrument(largest_only=True)
data = self._get_payload_for_nc_file_augmenter(full_path)
self.uuid.product = harmonizer.harmonize_thies_pt_nc(data)


class ProcessWeatherStation(ProcessInstrument):
def process_weather_station(self):
Expand Down

0 comments on commit 68a44f3

Please sign in to comment.