<div dir="ltr">On Wed, Aug 31, 2016 at 4:00 PM, Florian Lindner <<a href="mailto:mailinglists@xgm.de">mailinglists@xgm.de</a>> wrote:<br>><br>> Hello,<br>><br>> I have mesh (more exactly: just a bunch of nodes) description with values associated to the nodes in a file, e.g. for a<br>> 3x3 mesh:<br>><br>> 0   0   10<br>> 0   0.3 11<br>> 0   0.6 12<br>> 0.3 0   20<br>> 0.3 0.3 21<br>> 0.3 0.6 22<br>> 0.6 0   30<br>> 0.6 0.3 31<br>> 0.6 0.6 32<br>><br>> What is best way to read it in and get data structures like the ones I get from np.meshgrid?<br>><br>> Of course, I know about np.loadtxt, but I'm having trouble getting the resulting arrays (x, y, values) in the right form<br>> and to retain association to the values.<br><br>For this particular case (known shape and ordering), this is what I would do. Maybe throw in a .T or three depending on exactly how you want them to be laid out.<br><br><div>[~/scratch]</div><div>|1> !cat mesh.txt</div><div><br></div><div>0   0   10</div><div>0   0.3 11</div><div>0   0.6 12</div><div>0.3 0   20</div><div>0.3 0.3 21</div><div>0.3 0.6 22</div><div>0.6 0   30</div><div>0.6 0.3 31</div><div>0.6 0.6 32</div><div><br></div><div>[~/scratch]</div><div>|2> nodes = np.loadtxt('mesh.txt')</div><div><br></div><div>[~/scratch]</div><div>|3> nodes</div><div>array([[  0. ,   0. ,  10. ],</div><div>       [  0. ,   0.3,  11. ],</div><div>       [  0. ,   0.6,  12. ],</div><div>       [  0.3,   0. ,  20. ],</div><div>       [  0.3,   0.3,  21. ],</div><div>       [  0.3,   0.6,  22. ],</div><div>       [  0.6,   0. ,  30. ],</div><div>       [  0.6,   0.3,  31. ],</div><div>       [  0.6,   0.6,  32. ]])</div><div><br></div><div>[~/scratch]</div><div>|4> reshaped = nodes.reshape((3, 3, -1))</div><div><br></div><div>[~/scratch]</div><div>|5> reshaped</div><div>array([[[  0. ,   0. ,  10. ],</div><div>        [  0. ,   0.3,  11. ],</div><div>        [  0. ,   0.6,  12. ]],</div><div><br></div><div>       [[  0.3,   0. ,  20. ],</div><div>        [  0.3,   0.3,  21. ],</div><div>        [  0.3,   0.6,  22. ]],</div><div><br></div><div>       [[  0.6,   0. ,  30. ],</div><div>        [  0.6,   0.3,  31. ],</div><div>        [  0.6,   0.6,  32. ]]])</div><div><br></div><div>[~/scratch]</div><div>|7> x = reshaped[..., 0]</div><div><br></div><div>[~/scratch]</div><div>|8> y = reshaped[..., 1]</div><div><br></div><div>[~/scratch]</div><div>|9> values = reshaped[..., 2]</div><div><br></div><div>[~/scratch]</div><div>|10> x</div><div>array([[ 0. ,  0. ,  0. ],</div><div>       [ 0.3,  0.3,  0.3],</div><div>       [ 0.6,  0.6,  0.6]])</div><div><br></div><div>[~/scratch]</div><div>|11> y</div><div>array([[ 0. ,  0.3,  0.6],</div><div>       [ 0. ,  0.3,  0.6],</div><div>       [ 0. ,  0.3,  0.6]])</div><div><br></div><div>[~/scratch]</div><div>|12> values</div><div>array([[ 10.,  11.,  12.],</div><div>       [ 20.,  21.,  22.],</div><div>       [ 30.,  31.,  32.]])</div><div><br></div>--<br>Robert Kern</div>