In [ ]:
import xarray as xr
import numpy as np
In [ ]:
ds = xr.open_dataset('output.nc')
ds
In [ ]:
p = ds.p.values.T.flatten()
p.astype(np.str)
In [ ]:
u, v, w = ds.u.values.T.flatten(), ds.v.values.T.flatten(), ds.w.values.T.flatten()
In [ ]:
vel = np.c_[u,v,w].flatten()
vel.astype(np.str)
In [ ]:
import xml.etree.ElementTree as ET

# 親要素を追加する 
VTK = ET.Element('VTKFile', {'type':"ImageData"}) 
# 子要素を追加する 
s1 = ET.SubElement(VTK, 'ImageData', { 'WholeExtent':"0 20 0 20 0 20",  'Origin':"0 0 0",  'Spacing':"0.05 0.05 0.05" } )
s2 = ET.SubElement(s1, 'Piece', {'Extent':"0 20 0 20 0 20"}) 
s3 = ET.SubElement(s2, 'CellData',{'Scalars':"pressure", 'Vectors':"velocity"}) 
s41 = ET.SubElement(s3, 'DataArray', {'Name':"pressure",  'type':"Float64",  'format':"ascii"}) 
s42 = ET.SubElement(s3, 'DataArray', {'Name':"velocity", 'NumberOfComponents':"3", 'type':"Float64", 'format':"ascii"}) 

s41.text = ' '.join( p.astype(np.str) )
s42.text = ' '.join( vel.astype(np.str) )

tree = ET.ElementTree(VTK)
In [ ]:
 
In [ ]:
ET.dump(tree)
In [ ]:
 
In [ ]:
tree.write('simple.vti', encoding='UTF-8', xml_declaration=True)