comparison hedm_npz_to_tesr.py @ 0:bd74229f61e8 draft

"planemo upload for repository https://github.com/ximg-chess/galaxytools/tools/hedm_npz_to_tesr commit 0db0d2633f65e2ceca2e34c8899a52b18965d059-dirty"
author ximgchess
date Thu, 01 Apr 2021 18:30:44 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bd74229f61e8
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Created on Mon Nov 19 08:32:06 2018
5
6 @author: dcp99
7 """
8
9
10 #%%
11
12 import argparse
13 import numpy as np
14 import re
15
16 def __main__():
17
18 parser = argparse.ArgumentParser(
19 description='Convert an HEDM_map.npz file to a neper tesr file')
20 parser.add_argument(
21 'input',
22 type=argparse.FileType('rb'),
23 help='HEDM_map.npz')
24 parser.add_argument(
25 'output',
26 type=argparse.FileType('w'),
27 help='neper tesr file' )
28 parser.add_argument('-s', '--voxel_spacing', type=float, default=0.005, help='voxel spacing')
29 parser.add_argument('-x', '--x_name', default='^.*X.*$', help='X array name')
30 parser.add_argument('-y', '--y_name', default='^.*Y.*$', help='Y array name')
31 parser.add_argument('-z', '--z_name', default='^.*Z.*$', help='Z array name')
32 parser.add_argument('-g', '--grain_map', default='^.*grain.*$', help='grain map name')
33 parser.add_argument('-d', '--debug', action='store_true', help='Debug')
34 args = parser.parse_args()
35 data=np.load(args.input)
36 grain_map = Xs = Ys = Zs = None
37 for f in data.files:
38 if re.match(args.grain_map, f):
39 grain_map = data[f]
40 elif re.match(args.x_name, f):
41 Xs = data[f]
42 elif re.match(args.y_name, f):
43 Ys = data[f]
44 elif re.match(args.z_name, f):
45 Zs = data[f]
46 voxel_spacing = args.voxel_spacing
47
48 #CREATE ASSEMBLED DATA -- LIST OF [VOXEL COORDINATES (X,Y,Z),GRAIN ID]
49
50 coordinate_list=np.vstack((Xs.ravel(),Ys.ravel(),Zs.ravel()))
51 assembled_data=np.hstack((coordinate_list.T,np.atleast_2d(grain_map.ravel()).T))
52
53 #%% SORT BY ROWS Z AND THEN Y
54
55 assembled_data=assembled_data[assembled_data[:,2].argsort()]
56 total_size=int(grain_map.shape[0]*grain_map.shape[1]*grain_map.shape[2])
57
58 stack_size=int(grain_map.shape[0]*grain_map.shape[1])
59 for ii in np.arange(int(total_size/stack_size)):
60 tmp_args=assembled_data[ii*stack_size:(ii+1)*stack_size,1].argsort()
61 assembled_data[ii*stack_size:(ii+1)*stack_size,:]=assembled_data[ii*stack_size+tmp_args,:]
62
63 stack_size=grain_map.shape[1]
64 for ii in np.arange(int(total_size/stack_size)):
65 tmp_args=assembled_data[ii*stack_size:(ii+1)*stack_size,0].argsort()
66 assembled_data[ii*stack_size:(ii+1)*stack_size,:]=assembled_data[ii*stack_size+tmp_args,:]
67
68 #%%
69
70 np.set_printoptions(threshold=np.inf)
71 l1 = '***tesr'
72 l2 = ' **format'
73 l3 = ' 2.0 ascii'
74 l4 = ' **general'
75 l5 = ' 3'
76 l6 = ' ' + str(grain_map.shape[1]) + ' ' + str(grain_map.shape[0]) + ' ' + str(grain_map.shape[2])
77 l7 = ' ' + str(voxel_spacing) + ' ' + str(voxel_spacing) + ' ' + str(voxel_spacing)
78 l8 = ' **cell';
79 l9 = ' ' + str(np.max(grain_map).astype('int'))
80 l10 = ' *id';
81 l11 = ' ' + str(np.arange(1,np.max(grain_map)+1).astype('int'))[1:-1]
82 l12 = ' **data'
83 #l13 = ' ' + str(assembled_data[:,3].astype('int'))[1:-1]
84 l14 = '***end'
85
86 #%%
87 output = args.output
88 output.write('%s\n' % l1)
89 output.write('%s\n' % l2)
90 output.write('%s\n' % l3)
91 output.write('%s\n' % l4)
92 output.write('%s\n' % l5)
93 output.write('%s\n' % l6)
94 output.write('%s\n' % l7)
95 output.write('%s\n' % l8)
96 output.write('%s\n' % l9)
97 output.write('%s\n' % l10)
98 output.write('%s\n' % l11)
99 output.write('%s\n' % l12)
100 output.write(' ')
101 np.savetxt(output,np.atleast_2d(assembled_data[:,3]).T,fmt='%d')
102 #output.write('%s\n' % l13)
103 output.write('%s\n' % l14)
104 output.close()
105
106
107 if __name__ == "__main__":
108 __main__()