{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataset('output.nc')\n", "ds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = ds.p.values.T.flatten()\n", "p.astype(np.str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "u, v, w = ds.u.values.T.flatten(), ds.v.values.T.flatten(), ds.w.values.T.flatten()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vel = np.c_[u,v,w].flatten()\n", "vel.astype(np.str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xml.etree.ElementTree as ET\n", "\n", "# 親要素を追加する \n", "VTK = ET.Element('VTKFile', {'type':\"ImageData\"}) \n", "# 子要素を追加する \n", "s1 = ET.SubElement(VTK, 'ImageData', { 'WholeExtent':\"0 20 0 20 0 20\", 'Origin':\"0 0 0\", 'Spacing':\"0.05 0.05 0.05\" } )\n", "s2 = ET.SubElement(s1, 'Piece', {'Extent':\"0 20 0 20 0 20\"}) \n", "s3 = ET.SubElement(s2, 'CellData',{'Scalars':\"pressure\", 'Vectors':\"velocity\"}) \n", "s41 = ET.SubElement(s3, 'DataArray', {'Name':\"pressure\", 'type':\"Float64\", 'format':\"ascii\"}) \n", "s42 = ET.SubElement(s3, 'DataArray', {'Name':\"velocity\", 'NumberOfComponents':\"3\", 'type':\"Float64\", 'format':\"ascii\"}) \n", "\n", "s41.text = ' '.join( p.astype(np.str) )\n", "s42.text = ' '.join( vel.astype(np.str) )\n", "\n", "tree = ET.ElementTree(VTK)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ET.dump(tree)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tree.write('simple.vti', encoding='UTF-8', xml_declaration=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }