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

CDAT Migration Phase 3 - Port QBO Wavelet feature to Xarray/xCDAT codebase #860

Merged
merged 4 commits into from
Oct 1, 2024

Conversation

tomvothecoder
Copy link
Collaborator

@tomvothecoder tomvothecoder commented Oct 1, 2024

Description

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

If applicable:

  • New and existing unit tests pass with my changes (locally and CI/CD build)
  • I have added tests that prove my fix is effective or that my feature works
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have noted that this is a breaking change for a major release (fix or feature that would cause existing functionality to not work as expected)

@tomvothecoder tomvothecoder changed the title Refactor/850 qbo wavelet CDAT Migration Phase 3 - Port QBO Wavelet feature to Xarray/xCDAT codebase Oct 1, 2024
@tomvothecoder tomvothecoder self-assigned this Oct 1, 2024
@tomvothecoder tomvothecoder added enhancement cdat-migration-fy24 CDAT Migration FY24 Task labels Oct 1, 2024
@tomvothecoder tomvothecoder marked this pull request as ready for review October 1, 2024 18:52
Copy link
Collaborator Author

@tomvothecoder tomvothecoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @chengzhuzhang, this PR ended up being trivial to implement (about < 1 hour of work). I copied all of the code updates from #860 and performed some additional refactoring for cleaner code.

The results look visibly close to main. I will merge once the build passes.

Comment on lines +360 to +395
def _calculate_wavelet(var: xr.DataArray) -> Tuple[np.ndarray, np.ndarray]:
"""
Calculate the wavelet spectrum for a given data array at a specified power
spectral level.

Parameters
----------
data : xr.DataArray
The variable data.

Returns
-------
Tuple[np.ndarray, np.ndarray]
The wavelet period and wavelet array.
"""
# Find the closest value for power spectral level in the list
test_lev = xc.get_dim_coords(var, axis="Z")
test_lev_list = list(test_lev)
closest_lev = min(test_lev_list, key=lambda x: abs(x - POW_SPEC_LEV))
closest_index = test_lev_list.index(closest_lev)

# Grab target vertical level
data_avg = var.values[:, closest_index]

# Convert to anomalies
data_avg = data_avg - data_avg.mean()

# Detrend the data
detrended_data = detrend(data_avg)

wave_period, wavelet = _get_psd_from_wavelet(detrended_data)

# Get square root values of wavelet spectra
wavelet = np.sqrt(wavelet)

return wave_period, wavelet
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted this private function since the logic is the same for test and ref (look here).

Thanks GitHub Co-Pilot.

@tomvothecoder tomvothecoder merged commit 8607bc4 into cdat-migration-fy24 Oct 1, 2024
4 checks passed
@tomvothecoder tomvothecoder deleted the refactor/850-qbo-wavelet branch October 1, 2024 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cdat-migration-fy24 CDAT Migration FY24 Task enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant