New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing a more flexibel data ingest for a_b() #141
Changes from 5 commits
6403b67
0a84889
1680ef7
187acb3
7440056
8704174
135048a
6c02aa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from __future__ import division | ||
import numpy as np | ||
import xarray as xr | ||
from scipy.interpolate import interp1d | ||
|
||
from .xarray_wrapper import xarray_apply_along_time_dim | ||
|
||
|
@@ -36,8 +38,8 @@ def calc_R_from_A( | |
Frequency in GHz. If provided together with `pol`, it will be used to | ||
derive the parameters a and b for the k-R power law. | ||
pol : string, optional | ||
Polarization, that is either 'H' for horizontal or 'V' for vertical. Has | ||
to be provided together with `f_GHz`. It will be used to derive the | ||
Polarization, that is either 'H' for horizontal or 'V' for vertical. Has | ||
to be provided together with `f_GHz`. It will be used to derive the | ||
parameters a and b for the k-R power law. | ||
a : float, optional | ||
Parameter of A-R relationship | ||
|
@@ -183,32 +185,70 @@ def a_b(f_GHz, pol, approx_type="ITU_2005"): | |
prediction methods", International Telecommunication Union, P.838-2 (04/2003) P.838-3 (03/2005) | ||
|
||
""" | ||
from scipy.interpolate import interp1d | ||
if isinstance(f_GHz, xr.DataArray): | ||
return_xarray = True | ||
f_GHz_coords = f_GHz.coords | ||
else: | ||
return_xarray = False | ||
|
||
f_GHz = xr.DataArray(f_GHz) | ||
|
||
if isinstance(pol, str): | ||
pol = xr.full_like(f_GHz, pol, dtype=object) | ||
pol = xr.DataArray(pol) | ||
|
||
tmp_dims_f_GHz = f_GHz.dims | ||
tmp_dims_pol = pol.dims | ||
|
||
f_GHz = np.asarray(f_GHz) | ||
if tmp_dims_f_GHz != tmp_dims_pol: | ||
raise ValueError("Frequency and polarization must have identical " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not yet covered by a test. Please add one simple test for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fixed already by commit 7440056. This code coverage report is three hours old right now. |
||
"dimensions.") | ||
|
||
f_GHz = np.atleast_1d(f_GHz) | ||
pol = np.atleast_1d(pol) | ||
|
||
pol_v_str_variants = ['v','V','vertical','Vertical'] | ||
pol_h_str_variants = ['h','H','horizontal','Horizontal'] | ||
|
||
if f_GHz.min() < 1 or f_GHz.max() > 100: | ||
raise ValueError("Frequency must be between 1 Ghz and 100 GHz.") | ||
if not np.isin(pol,pol_v_str_variants+pol_h_str_variants).all(): | ||
raise ValueError("Polarization must be V, v, Vertical, vertical, H," | ||
"Horizontal or horizontal.") | ||
# select ITU table | ||
if approx_type == "ITU_2003": | ||
ITU_table = ITU_table_2003.copy() | ||
elif approx_type == "ITU_2005": | ||
ITU_table = ITU_table_2005.copy() | ||
else: | ||
# select ITU table | ||
if approx_type == "ITU_2003": | ||
ITU_table = ITU_table_2003.copy() | ||
elif approx_type == "ITU_2005": | ||
ITU_table = ITU_table_2005.copy() | ||
else: | ||
raise ValueError("Approximation type not available.") | ||
|
||
if pol == "V" or pol == "v" or pol == 'Vertical' or pol == "vertical": | ||
f_a = interp1d(ITU_table[0, :], ITU_table[2, :], kind="cubic") | ||
f_b = interp1d(ITU_table[0, :], ITU_table[4, :], kind="cubic") | ||
elif pol == "H" or pol == "h" or pol == "Horizontal" or pol == "horizontal": | ||
f_a = interp1d(ITU_table[0, :], ITU_table[1, :], kind="cubic") | ||
f_b = interp1d(ITU_table[0, :], ITU_table[3, :], kind="cubic") | ||
else: | ||
raise ValueError("Polarization must be V, v, Vertical, vertical, H," | ||
"Horizontal or horizontal.") | ||
a = f_a(f_GHz) | ||
b = f_b(f_GHz) | ||
raise ValueError("Approximation type not available.") | ||
|
||
interp_a_v = interp1d(ITU_table[0, :], ITU_table[2, :], kind="cubic") | ||
interp_b_v = interp1d(ITU_table[0, :], ITU_table[4, :], kind="cubic") | ||
interp_a_h = interp1d(ITU_table[0, :], ITU_table[1, :], kind="cubic") | ||
interp_b_h = interp1d(ITU_table[0, :], ITU_table[3, :], kind="cubic") | ||
|
||
a_v = interp_a_v(f_GHz) | ||
b_v = interp_b_v(f_GHz) | ||
a_h = interp_a_h(f_GHz) | ||
b_h = interp_b_h(f_GHz) | ||
|
||
a = np.full_like(f_GHz, fill_value=np.nan, dtype=float) | ||
b = np.full_like(f_GHz, fill_value=np.nan, dtype=float) | ||
|
||
pol_mask_v = np.isin(pol, pol_v_str_variants) | ||
pol_mask_h = np.isin(pol, pol_h_str_variants) | ||
|
||
a[pol_mask_h] = a_h[pol_mask_h] | ||
a[pol_mask_v] = a_v[pol_mask_v] | ||
b[pol_mask_h] = b_h[pol_mask_h] | ||
b[pol_mask_v] = b_v[pol_mask_v] | ||
|
||
if return_xarray: | ||
if tmp_dims_f_GHz != (): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment with an explanation here, why this is needed. |
||
a = xr.DataArray(a, coords=f_GHz_coords) | ||
b = xr.DataArray(b, coords=f_GHz_coords) | ||
|
||
return a, b | ||
|
||
|
||
|
@@ -337,3 +377,4 @@ def a_b(f_GHz, pol, approx_type="ITU_2005"): | |
) | ||
|
||
# fmt: on | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We allow certain combinations of np.ndarray, xr.DataArray and float or str. But other combinations are not allowed. I suggest to add that info to the docstring at
f_GHz
andpol
.I know that we do not mention xr.DataArray as input data type in many docstrings, even though they are handled via the xarray-wrapper decorator. I actually always wanted to add the featuer that this wrappers add a note about that behavior to the docstring of the wrapped function. Yet another todo... But here, since we actually handled xr.DataArrays expliclity in the function body, I would adjust the docstring accordingly.