Skip to content

Instantly share code, notes, and snippets.

@dpshelio
Forked from anonymous/impex_calls.py
Last active December 22, 2015 06:49
Show Gist options
  • Save dpshelio/6434146 to your computer and use it in GitHub Desktop.
Save dpshelio/6434146 to your computer and use it in GitHub Desktop.
How to use IMPEx (FMI) from python2.7 with suds
from suds.client import Client
import random
import astropy.constants as const
import numpy as np
url = 'http://impex-fp7.fmi.fi/impex/IMPExServer.php?wsdl'
client = Client(url)
print client #Shows all the methods available and their inputs
# getDataPointValue_spacecraft example
result = client.service.getDataPointValue_spacecraft(ResourceID="impex://FMI/HWA/HYB/venus/run01_nominal_with_cx/O+_ave_hybstate", Spacecraft_name="VenusExpress", StartTime="2007-01-11T00:00:00", StopTime="2007-01-21T00:00:00", Sampling="600")
print result
# getDataPointValue, by using a dictionary as input
values = {'ResourceID': 'impex://FMI/HWA/HYB/venus/run01_nominal_with_cx/O+_ave_hybstate',
'url_XYZ': 'http://impex-fp7.fmi.fi/ws/ws_dps/ex.votable'}
result = client.service.getDataPointValue(**values)
print result
#getParticleTrajectory for 100 starting points randomly distributed in the plane x = 12e6; between y and z range [-12e6,12e6]
X = ['12e6'] * 100
Y = [str(random.randrange(-12e6, 12e6)) for x in range(100)]
Z = [str(random.randrange(-12e6, 12e6)) for x in range(100)]
Vx = Vy = Vz = ['0'] * 100
mass = [str(16 * const.m_p.si.value)] * 100
charge = [str(const.e.si.value)] * 100
values = {x:','.join(eval(x)) for x in ['X', 'Y', 'Z', 'Vx', 'Vy', 'Vz', 'mass', 'charge']}
url_votable = client.service.getVOTableURL(**values)
values = {'ResourceID': 'impex://FMI/HWA/HYB/venus/run01_nominal_with_cx/O+_ave_hybstate',
'url_XYZ': url_votable}
url_particle = client.service.getParticleTrajectory(**values)
# Plot the particles trayectory
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.colors as colors
import matplotlib.cm as cmx
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', aspect='equal')
# Select colours from colortable: http://stackoverflow.com/questions/8931268/using-colormaps-to-set-color-of-line-in-matplotlib
jet = cm = plt.get_cmap('jet')
cNorm = colors.Normalize(vmin=0, vmax=100)
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)
## Read the output with astropy
from astropy.io import votable
def plot_vot(url):
vot = votable.parse(url)
# Extract a dictionary with posx, posy and posz for each line in the vot:
axis = ['x', 'y', 'z']
types = ['pos.cartesian.'+l for l in axis]
for idx, table in enumerate(vot.iter_tables()):
points = {}
for column in table.iter_fields_and_params():
ucd = str(column.ucd)
if ucd.lower() in types:
findaxis = lambda x: x in column.name.lower()
mask = map(findaxis, axis)
if True in mask:
column_values = table.array[column.ID].data * column.unit
points[column.ID] = column_values.si.value
# select the colour for each line
colorVal = scalarMap.to_rgba(idx)
ax.plot(points['posx'][:,0], points['posy'][:,0], points['posz'][:,0], color = colorVal)
# Plot a sphere
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 6e6 * np.outer(np.cos(u), np.sin(v))
y = 6e6 * np.outer(np.sin(u), np.sin(v))
z = 6e6 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
#Plot the iontraces
plot_vot(url_particle)
#Plot also field lines for the same starting points
values = {'ResourceID': 'impex://FMI/HWA/HYB/venus/venus_nominal_lowres_cx_20121120/Mag',
'url_XYZ': 'http://impex-fp7.fmi.fi/ws/ws_dps/ex.votable',
'Variable': 'B',
'Direction': 'backward'}
url_field = client.service.getFieldLine(**values)
plot_vot(url_field)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment