Skip to content

Commit

Permalink
Format attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tukiains committed Mar 28, 2024
1 parent 64c717d commit 0a3d320
Showing 1 changed file with 35 additions and 41 deletions.
76 changes: 35 additions & 41 deletions mwrpy/level2/write_lev2_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ def get_products(
f"No suitable data found for processing for data type: {data_type}"
)

coeff["retrieval_elevation_angles"] = str(
np.sort(np.unique(ele_retrieval(elevation_angle[index], coeff)))
)
coeff["retrieval_frequencies"] = str(
np.sort(np.unique(coeff["FR"][coeff["FR"][:] > 0.0]))
coeff["retrieval_elevation_angles"] = _format_attribute_array(
ele_retrieval(elevation_angle[index], coeff)
)
coeff["retrieval_frequencies"] = _get_retrieval_frequencies(coeff)

if coeff["RT"] < 2:
coeff_offset = offset(elevation_angle[index])
Expand Down Expand Up @@ -239,13 +237,12 @@ def get_products(
raise MissingInputData(
f"No suitable data found for processing for data type: {data_type}"
)
coeff["retrieval_elevation_angles"] = str(
np.sort(np.unique(ele_retrieval(elevation_angle[index], coeff)))
)
coeff["retrieval_frequencies"] = str(
np.sort(np.unique(coeff["FR"][coeff["FR"][:] > 0.0]))
coeff["retrieval_elevation_angles"] = _format_attribute_array(
ele_retrieval(elevation_angle[index], coeff)
)

coeff["retrieval_frequencies"] = _get_retrieval_frequencies(coeff)

rpg_dat["height"] = coeff["AL"][:] + params["altitude"]

if coeff["RT"] < 2:
Expand Down Expand Up @@ -337,9 +334,7 @@ def get_products(
_, freq_bl, _ = np.intersect1d(
coeff["FR"], coeff["FR_BL"], assume_unique=False, return_indices=True
)
coeff["retrieval_frequencies"] = str(
np.sort(np.unique(coeff["FR"][coeff["FR"][:] > 0.0]))
)
coeff["retrieval_frequencies"] = _get_retrieval_frequencies(coeff)

ix0 = np.where(
(elevation_angle[:] > coeff["AG"][0] - 0.5)
Expand Down Expand Up @@ -439,36 +434,14 @@ def get_products(
hum_dat = load_product(hum_file)

coeff, index = {}, np.empty(0, np.int32)
coeff["retrieval_frequencies"] = str(
np.unique(
np.sort(
np.concatenate(
[
get_ret_info(tem_dat["temperature"].retrieval_frequencies),
get_ret_info(
hum_dat["absolute_humidity"].retrieval_frequencies
),
]
)
)
)

coeff["retrieval_frequencies"] = _combine_array_attributes(
tem_dat, hum_dat, "retrieval_frequencies"
)
coeff["retrieval_elevation_angles"] = str(
np.unique(
np.sort(
np.concatenate(
[
get_ret_info(
tem_dat["temperature"].retrieval_elevation_angles
),
get_ret_info(
hum_dat["absolute_humidity"].retrieval_elevation_angles
),
]
)
)
)
coeff["retrieval_elevation_angles"] = _combine_array_attributes(
tem_dat, hum_dat, "retrieval_elevation_angles"
)

coeff["retrieval_type"] = "derived product"
coeff["dependencies"] = temp_file + ", " + hum_file
if len(hum_dat.variables["height"][:]) == len(tem_dat.variables["height"][:]):
Expand Down Expand Up @@ -719,3 +692,24 @@ def retrieval_input(lev1: dict | nc.Dataset, coeff: dict) -> np.ndarray:
def decimal_hour2unix(date: list, time: np.ndarray) -> np.ndarray | int:
unix_timestamp = np.datetime64("-".join(date)).astype("datetime64[s]").astype("int")
return (time * 60 * 60 + unix_timestamp).astype(int)


def _get_retrieval_frequencies(coeff: dict) -> str:
if isinstance(coeff["FR"], ma.MaskedArray):
frequencies = coeff["FR"][~coeff["FR"][:].mask]
else:
frequencies = coeff["FR"]
return _format_attribute_array(frequencies)


def _combine_array_attributes(tem_dat: dict, hum_dat: dict, name: str) -> str:
a = get_ret_info(getattr(tem_dat["temperature"], name))
b = get_ret_info(getattr(hum_dat["absolute_humidity"], name))
combined = np.concatenate((np.array(a), np.array(b)))
return _format_attribute_array(combined)


def _format_attribute_array(array: np.ndarray | list) -> str:
array = np.round(np.sort(np.unique(array)), 3)
array = [str(f) for f in array]
return ", ".join(array)

0 comments on commit 0a3d320

Please sign in to comment.