-
Notifications
You must be signed in to change notification settings - Fork 17
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
ADD: Apply accessor #203
base: main
Are you sure you want to change the base?
ADD: Apply accessor #203
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #203 +/- ##
==========================================
+ Coverage 91.97% 91.99% +0.02%
==========================================
Files 23 23
Lines 4573 4585 +12
==========================================
+ Hits 4206 4218 +12
Misses 367 367
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
@syedhamidali Thanks for being that fast. Yes, this is around the lines I was talking before. I've added a couple of comments and suggestion, which we can discuss.
@kmuehlbauer Thanks for these suggestions. Do these changes meet your expectations? I’ve run the methods on some data, and they’re working well. Let me know your thoughts! |
what about using we just use |
5 vs 16 characters? OK if we count @aladinor How would we handle this with |
that's a really good question. We need to make sure it only uses the "sweep" nodes! |
Yes, this is what we are doing (or want to do) in our boilerplate code. The user should not have to care about that, so we have to take responsibility for that. The only thing which remains, and this is already somehow mis-implemented in the current datatree-accessors, is that the original datatree is overwritten instead of returning a newly created tree. |
Hi @kmuehlbauer and @aladinor, Thanks for your suggestions and for pointing out the node issue, I hadn’t caught that earlier. I’ve made the necessary adjustments and would appreciate your feedback. |
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.
One more suggestion here - can you add an example here @syedhamidali ?
ExampleApplying to DataTreeimport xradar as xd
from open_radar_data import DATASETS
# Fetch the sample radar file
filename = DATASETS.fetch("sample_sgp_data.nc")
# Open the radar file into a DataTree object
dtree = xd.io.open_cfradial1_datatree(filename)
# Define a function to calculate rain rate from reflectivity
def calculate_rain_rate(ds, ref_field='DBZH'):
def _rain_rate(dbz, a=200.0, b=1.6):
Z = 10.0 ** (dbz / 10.0)
return (Z / a) ** (1.0 / b)
ds['RAIN_RATE'] = _rain_rate(ds[ref_field])
ds['RAIN_RATE'].attrs = {'units': 'mm/h', 'long_name': 'Rain Rate'}
return ds
# Apply the function across all sweeps
dtree = dtree.xradar.apply(calculate_rain_rate, ref_field='corrected_reflectivity_horizontal') Applying to Dataset# Extract a single sweep as a Dataset
ds = dtree['sweep_0'].to_dataset()
# Apply the function to the Dataset using the xradar accessor
ds = ds.xradar.apply(calculate_rain_rate, ref_field='corrected_reflectivity_horizontal') Applying to DataArray# Extract the reflectivity field as a DataArray
da = ds['corrected_reflectivity_horizontal']
# Define a function to add a constant value to the DataArray
def add_constant(da, constant=10):
return da + constant
# Apply the function using the xradar accessor
da_modified = da.xradar.apply(add_constant, constant=5) |
@syedhamidali How about making a little notebook of that, which we can refer to in the docs? This is a very nice feature. Thanks for pushing this forward. |
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
@kmuehlbauer Sounds good! Added a notebook, I will update this notebook sometime in the future with more examples. |
@kmuehlbauer, @mgrover1, let me know if there’s anything else you’d like me to address. I believe these PRs are ready for merging. |
history.md
This PR introduces an apply method to the xradar accessor, allowing users to apply functions to DataTree, Dataset, and DataArray objects seamlessly. This enhancement follows the discussion on generalizing the approach using accessors and aims to provide a more consistent and intuitive API.
Applying to DataTree
Applying to Dataset
Applying to DataArray