Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:22:53+00:00 2026-06-11T02:22:53+00:00

I’m a bit confused on how to accurately slice and sort 3D array in

  • 0

I’m a bit confused on how to accurately slice and sort 3D array in numpy.
There seem to be many ways to do this manually but I need to do this using numpy.where(). For example if lo360 are 2D longitude values, lat2d latitude values in 2D, yi is a 1D array of longitude values, and xi is a 1D array of latitude values.

xi and yi dynamically change to represent a small geographical region while lo360 and lat2d are static latitudes and longitudes of the planet of the type (-90,90) and (0,360). xi of similar form as lo360 but yi is descending instead of ascending. So if I have a 3D array representing A(levels,lat,lon) and I want to extract a region:

slice2d = np.where( (lo360 <= xi.max()) &                                                    
                    (lo360 >= xi.min()) &                                                    
                    (lat2d <= yi.max()) &                                                    
                    (lat2d >= yi.min()) )
lon_old = lo360[slice2d]; print lon_old.shape
(441,)

This returns a 1D array when I wanted a 2D slice. The data is correct though so this is not my problem.

Then when I tried to slice the 3D array A[i][slice2d] I get a 1D array that is not easy to verify dynamically. I used griddata to the 3D array to xi and yi resolution but I change the yi lat to ascending: yi = yi[::-1]:

for i in np.arange(4):                                                          
    nvals[i] = matplotlib.mlab.griddata(lat_old,lon_old,                                     
                                            mvals[i][slice2d],                                   
                                            yi,xi)

Here is where I think the problem starts, I need the results to have descending lats so I do this to nvals: nvals = nvals[:,::-1,:]. But the data is all screwed up. I suspect some error in the indexing but since python returned no errors then I’m doing something with the indexing thinking one thing but getting another.

Perhaps one of you experts can spot something screwy or maybe suggest a better way. I’ll attach the image when I figure out how to attach files.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:22:54+00:00Added an answer on June 11, 2026 at 2:22 am

    It seems like the result returned by griddata is transposed – it gives the x-axis along the columns and y-axis along the rows:

    import numpy as np
    
    #lats x lons
    a2d=np.arange(20).reshape( (4,5) )
    print a2d
    
    lats=np.arange(4)
    lats2d=np.ones(5)*lats[:,None]
    yi=[1,3]
    nlats=np.sum(np.bitwise_and(lats>=np.min(yi),lats<=np.max(yi)))
    
    lons=np.arange(5)
    lons2d=np.ones(4)[:,None]*lons
    xi=[1,2]
    nlons=np.sum(np.bitwise_and(lons>=np.min(xi),lons<=np.max(xi)))
    
    
    #slice= lats2d>=1 & lats2d<=2 & lons2d>=1 & lons2d<=2
    s1=np.bitwise_and(lats2d>=np.min(yi),lats2d<=np.max(yi))
    s2=np.bitwise_and(lons2d>=np.min(xi),lons2d<=np.max(xi))
    slice=np.bitwise_and(s1,s2)
    print slice
    slice=np.where(slice)
    print a2d[slice].reshape( (nlats,nlons) )
    
    import matplotlib.mlab as mlab
    
    print mlab.griddata(lats2d[slice],lons2d[slice],a2d[slice],
    #              np.array([1.3,2.1,2.9]),np.array([1.1,1.9]))
                np.array([1,2,3]),np.array([1,2]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
This could be a duplicate question, but I have no idea what search terms
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.