Skip to content
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

Bug in Either map_over_sweeps or in to_cfradial1 #254

Open
syedhamidali opened this issue Dec 14, 2024 · 0 comments
Open

Bug in Either map_over_sweeps or in to_cfradial1 #254

syedhamidali opened this issue Dec 14, 2024 · 0 comments

Comments

@syedhamidali
Copy link
Contributor

syedhamidali commented Dec 14, 2024

  • xradar version: 0.8.1
  • Python version: 3.12.7
  • Operating System: MacOS

Description

When applying any operation on a datatree using map_over_sweeps, and then try to export it to cfradial1, it produces an error, although they look identical when compared to each other.

What I Did

import cmweather
import numpy as np
import xarray as xr
import xradar as xd
from open_radar_data import DATASETS
xd.__version__
'0.8.1.dev7+g830d86b'
file = DATASETS.fetch("swx_20120520_0641.nc")

dtree = xd.io.open_cfradial1_datatree(file)

xd.io.to_cfradial1(dtree, filename='test_cfradial1.nc')

dtree2 = xd.io.open_cfradial1_datatree("test_cfradial1.nc")

np.testing.assert_equal(dtree.groups, dtree2.groups)

np.testing.assert_equal(dtree, dtree2)

xd.io.to_cfradial1(dtree2, filename='test_cfradial2.nc')

dtree3 = dtree.xradar.georeference()
dtree3['sweep_0']['corrected_reflectivity_horizontal'].plot(x='x', y='y', vmin=-10, vmax=75, cmap='NWSRef')

image

def filter_radar(ds, vel_name='VELH', ref_name='DBZH'):
    vel_texture = ds[vel_name].rolling(range=30, min_periods=2, center=True).std()
    ds = ds.assign(velocity_texture=vel_texture)
    ds = ds.where((ds.velocity_texture<10) & ((ds[ref_name]>=-10) & (ds[ref_name]<=75)))
    return ds

dtree3 = dtree3.xradar.map_over_sweeps(filter_radar,
                                       vel_name='mean_doppler_velocity',
                                       ref_name='corrected_reflectivity_horizontal')

dtree3['sweep_0']['corrected_reflectivity_horizontal'].plot(x='x', y='y', vmin=-10, vmax=75, cmap='NWSRef')

image

xd.io.to_cfradial1(dtree3, 'test_cfradial_filt.nc')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[37], line 1
----> 1 xd.io.to_cfradial1(dtree3, 'test_cfradial_filt.nc')

File ~/micromamba/envs/xradar_latest/lib/python3.12/site-packages/xradar/io/export/cfradial1.py:298, in to_cfradial1(dtree, filename, calibs)
    283 """
    284 Convert a radar xarray.DataTree to the CFRadial1 format
    285 and save it to a file. Ensure that the resulting dataset
   (...)
    295     Whether to include calibration parameters.
    296 """
    297 # Generate the initial ds_cf using the existing mapping functions
--> 298 dataset = _variable_mapper(dtree)
    300 # Handle calibration parameters
    301 if calibs:

File ~/micromamba/envs/xradar_latest/lib/python3.12/site-packages/xradar/io/export/cfradial1.py:105, in _variable_mapper(dtree, dim0)
     88 def _variable_mapper(dtree, dim0=None):
     89     """
     90     Map radar variables for different sweep groups.
     91 
   (...)
    102         Dataset containing mapped radar variables.
    103     """
--> 105     sweep_info = _sweep_info_mapper(dtree)
    106     vol_info = _main_info_mapper(dtree)
    107     if "fixed_angle" in vol_info:

File ~/micromamba/envs/xradar_latest/lib/python3.12/site-packages/xradar/io/export/cfradial1.py:198, in _sweep_info_mapper(dtree)
    184 sweep_vars = [
    185     "sweep_number",
    186     "sweep_mode",
   (...)
    192     "sweep_end_ray_index",
    193 ]
    195 for var_name in sweep_vars:
    196     var_data_list = [
    197         (
--> 198             np.unique(dtree[s][var_name].values[np.newaxis, ...])
    199             if var_name in dtree[s]
    200             else np.array([np.nan])
    201         )
    202         for s in dtree.groups
    203         if "sweep" in s
    204     ]
    206     var_attrs_list = [
    207         dtree[s][var_name].attrs if var_name in dtree[s] else {}
    208         for s in dtree.groups
    209         if "sweep" in s
    210     ]
    212     if not var_data_list:

File ~/micromamba/envs/xradar_latest/lib/python3.12/site-packages/numpy/lib/_arraysetops_impl.py:289, in unique(ar, return_index, return_inverse, return_counts, axis, equal_nan)
    287 ar = np.asanyarray(ar)
    288 if axis is None:
--> 289     ret = _unique1d(ar, return_index, return_inverse, return_counts, 
    290                     equal_nan=equal_nan, inverse_shape=ar.shape, axis=None)
    291     return _unpack_tuple(ret)
    293 # axis was specified and not None

File ~/micromamba/envs/xradar_latest/lib/python3.12/site-packages/numpy/lib/_arraysetops_impl.py:356, in _unique1d(ar, return_index, return_inverse, return_counts, equal_nan, inverse_shape, axis)
    354     aux = ar[perm]
    355 else:
--> 356     ar.sort()
    357     aux = ar
    358 mask = np.empty(aux.shape, dtype=np.bool)

TypeError: '<' not supported between instances of 'float' and 'str'
xr.testing.assert_identical(dtree['sweep_0'], dtree3['sweep_0'])
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[43], line 1
----> 1 xr.testing.assert_identical(dtree['sweep_0'], dtree3['sweep_0'])

    [... skipping hidden 1 frame]

File ~/micromamba/envs/xradar_latest/lib/python3.12/site-packages/xarray/testing/assertions.py:181, in assert_identical(a, b)
    179     assert a.identical(b), formatting.diff_coords_repr(a, b, "identical")
    180 elif isinstance(a, DataTree):
--> 181     assert a.identical(b), diff_datatree_repr(a, b, "identical")
    182 else:
    183     raise TypeError(f"{type(a)} not supported by assertion comparison")

AssertionError: Left and right DataTree objects are not identical

Data at root node does not match:
    Differing data variables:
    L   diff_reflectivity                  (azimuth, range) float32 1MB 0.0 ... 0.0
    R   diff_reflectivity                  (azimuth, range) float32 1MB 0.0 ... nan
    L   recalculated_diff_phase            (azimuth, range) float32 1MB -0.0 ... ...
    R   recalculated_diff_phase            (azimuth, range) float32 1MB -0.0 ... nan
    L   diff_phase                         (azimuth, range) float32 1MB 0.0 ... nan
    R   diff_phase                         (azimuth, range) float32 1MB 0.0 ... nan
    L   proc_dp_phase_shift                (azimuth, range) float32 1MB 18.09 ......
    R   proc_dp_phase_shift                (azimuth, range) float32 1MB 18.09 ......
    L   corrected_reflectivity_horizontal  (azimuth, range) float32 1MB -5.617 .....
    R   corrected_reflectivity_horizontal  (azimuth, range) float32 1MB -5.617 .....
    L   specific_attenuation               (azimuth, range) float32 1MB 0.0004272...
    R   specific_attenuation               (azimuth, range) float32 1MB 0.0004272...
    L   mean_doppler_velocity              (azimuth, range) float32 1MB 0.0 ... -...
    R   mean_doppler_velocity              (azimuth, range) float32 1MB 0.0 ... nan
    L   rain_rate_A                        (azimuth, range) float32 1MB 0.0625 .....
    R   rain_rate_A                        (azimuth, range) float32 1MB 0.0625 .....
    L   norm_coherent_power                (azimuth, range) float32 1MB 1.0 ... 0.17
    R   norm_coherent_power                (azimuth, range) float32 1MB 1.0 ... nan
    L   copol_coeff                        (azimuth, range) float32 1MB 1.0 ... nan
    R   copol_coeff                        (azimuth, range) float32 1MB 1.0 ... nan
    L   sweep_mode                         <U20 80B 'azimuth_surveillance'
    R   sweep_mode                         (azimuth, range) object 2MB 'azimuth_s...
    L   sweep_fixed_angle                  float64 8B 0.5
    R   sweep_fixed_angle                  (azimuth, range) float64 2MB 0.5 ... nan
    L   sweep_number                       int32 4B 0
    R   sweep_number                       (azimuth, range) float64 2MB 0.0 ... nan
    L   unf_dp_phase_shift                 (azimuth, range) float32 1MB 0.0 ... 28.4
    R   unf_dp_phase_shift                 (azimuth, range) float32 1MB 0.0 ... nan
    L   dp_phase_shift                     (azimuth, range) float32 1MB 90.0 ... nan
    R   dp_phase_shift                     (azimuth, range) float32 1MB 90.0 ... nan
    L   reflectivity_horizontal            (azimuth, range) float32 1MB -6.203 .....
    R   reflectivity_horizontal            (azimuth, range) float32 1MB -6.203 .....
    Data variables only on the right object:
        velocity_texture                   (azimuth, range) float32 1MB 4.744 ......

I tried it on another dataset, and even though they look identical, they still can’t be exported or transformed to cfradial1 after applying any operation using map_over_sweeps

temp_compare_dtree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant