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

Using 2D latitude and longitude as dims in power spectrum #198

Open
vic1309 opened this issue Apr 27, 2023 · 2 comments
Open

Using 2D latitude and longitude as dims in power spectrum #198

vic1309 opened this issue Apr 27, 2023 · 2 comments

Comments

@vic1309
Copy link

vic1309 commented Apr 27, 2023

Hello all

First of all, congratulations for the nice package.

I have a set of gridded variables. Latitude and longitude have NxN dimensions. A snippet is provided below:

# Define shape of the area
dx = 2.5
dy = dx

Nx = np.shape(eke[:,:])[1] # N of points in x 
Ny = np.shape(eke[:,:])[0] # N of points in y

Lx = Nx*dx
Ly = Ny*dy

x = np.arange(0, Lx, dx)
y = np.arange(0, Ly, dy)

# Build Array
LAT = df.lat.values[p0-pr:p0+pr, p1-pr:p1+pr]
LON = df.lon.values[p0-pr:p0+pr, p1-pr:p1+pr]

uo = xr.DataArray(eke, coords={'x':x, 'y':y, 'lat':(['x','y'], LAT), 'lon': (['x','y'], LON)}, dims=('x', 'y'))

where EKE is my 2D field.

When I run:

iso = xrft.isotropic_power_spectrum(uo, dim=['x','y'], detrend='linear', window=True)

I get the following error:

ValueError: The input array contains coordinate variable(s) (['lat', 'lon']) whose dims include the transform dimension(s) `x`. Please drop these coordinates (`.drop(['lat', 'lon']`) before invoking xrft.

Any ideas on how to solve it? I can provided the eke file if necessary.

@rabernat
Copy link
Collaborator

The error message is telling you what to do. Drop lat and lon, or just don't include them in the first place.

uo = uo.drop(['lat', 'lon'])

Such coordinates have no meaning after the Fourier transform, so xgcm forces you to explicitly drop them.

@vic1309
Copy link
Author

vic1309 commented Apr 27, 2023

Thanks for the prompt reply, Ryan.

My goal on creating a single array also containing the coordinates is for convenience as I plan to use the same data later e.g. this example .

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

No branches or pull requests

2 participants