[Paraview] Converting Longitude, latitude and Radius (distance) to XYZ (spherical coordinates)‏

Favre Jean jfavre at cscs.ch
Fri Aug 13 13:38:02 EDT 2010


I was trying the same exercise for fun, and I got it done with a Python Programmable Filter.  When you import math, you also get math.pi

Here is how I did it:

1) generate a structured grid with a programmable source and your given bounds
2) remap its coordinates with a programmable filter

Here is the source for both:

1) The Structured Grid

The Script for Information part:

  executive = self.GetExecutive()
  outInfo = executive.GetOutputInformation(0)
  executive.SetExtentTranslator(outInfo, vtk.vtkExtentTranslator())
  outInfo.Set(executive.WHOLE_EXTENT(), 0, 29, 0, 19, 0, 9)

The Script part:

  executive = self.GetExecutive()
  outInfo = executive.GetOutputInformation(0)
  updateExtent = [executive.UPDATE_EXTENT().Get(outInfo, i) for i in xrange(6)]
 
  imageData = self.GetOutput()
  imageData.SetExtent(updateExtent)

 
  dimensions = imageData.GetDimensions()
  pts = vtk.vtkPoints()

  for k in range(dimensions[2]):
    for j in range(-90, 90, 180/dimensions[1]):
      for i in xrange(-180, 180, 360/dimensions[0]):
        pts.InsertNextPoint(i,j,k)
  output.SetPoints(pts)

2) the programmable filter

import math
pdi = self.GetStructuredGridInput()
pdo =  self.GetStructuredGridOutput()
newPoints = vtk.vtkPoints()
numPoints = pdi.GetNumberOfPoints()
for i in range(0, numPoints):
    coord = pdi.GetPoint(i)
    x, y, z = coord[:3]
    Xf=z * cos (x*math.pi/180.0) * cos (y*math.pi/180.0)
    Yf=z * sin (x*math.pi/180.0) * cos (y*math.pi/180.0)
    Zf=z * sin (y*math.pi/180.0)
    newPoints.InsertPoint(i, Xf, Yf, Zf)
pdo.SetPoints(newPoints)


it is actually missing one layer of cells at the meridian. It depends on how people dump their data. You will be able to adjust the python loop to read data from your files (not included here for obvious reasons)

The issues I have seen in the past are:

I had one layer of cells missing, so I added a duplicate of the 0-meridian to close the sphere. I also copied my last layer of data
Vectors (from spherical space) also have to be converted, and you remember it is a messy formula.
There is no continuity of "flow" across the main meridian. This can usually be alleviated by removing the duplicate points - making a water-tight surface - on the shell you are looking at

For all these reasons, I did all of this with C programming to read our data. Python is fun, but it gets cumbersome. Doing it all in parallel, is an added challenge. Closing the meridian actually does not involve MPI communications. I just gave that last layer to one proc.



-----------------
Jean M. Favre
Swiss National Supercomputing Center


More information about the ParaView mailing list