Reprojecting polar to cartesian grid

前端 未结 5 1057
不思量自难忘°
不思量自难忘° 2020-12-24 14:22

I have a polar (r,theta) grid (which means that each cell is an annulus section) containing values of some physical quantity (e.g. temperature), and I would like to re-grid

5条回答
  •  甜味超标
    2020-12-24 15:07

    You can do this more compactly with scipy.ndimage.geometric_transform. Here is some sample code:

    import numpy as N
    import scipy as S
    import scipy.ndimage
    
    temperature =  
    # This is the data in your polar grid.
    # The 0th and 1st axes correspond to r and θ, respectively.
    # For the sake of simplicity, θ goes from 0 to 2π, 
    # and r's units are just its indices.
    
    def polar2cartesian(outcoords, inputshape, origin):
        """Coordinate transform for converting a polar array to Cartesian coordinates. 
        inputshape is a tuple containing the shape of the polar array. origin is a
        tuple containing the x and y indices of where the origin should be in the
        output array."""
    
        xindex, yindex = outcoords
        x0, y0 = origin
        x = xindex - x0
        y = yindex - y0
    
        r = N.sqrt(x**2 + y**2)
        theta = N.arctan2(y, x)
        theta_index = N.round((theta + N.pi) * inputshape[1] / (2 * N.pi))
    
        return (r,theta_index)
    
    temperature_cartesian = S.ndimage.geometric_transform(temperature, polar2cartesian, 
        order=0,
        output_shape = (temperature.shape[0] * 2, temperature.shape[0] * 2),
        extra_keywords = {'inputshape':temperature.shape,
            'center':(temperature.shape[0], temperature.shape[0])})
    

    You can change order=0 as desired for better interpolation. The output array temperature_cartesian is 2r by 2r here, but you can specify any size and origin you like.

提交回复
热议问题