source: issm/trunk/src/m/exp/expwrite.py@ 28275

Last change on this file since 28275 was 27035, checked in by Mathieu Morlighem, 3 years ago

merged trunk-jpl and trunk for revision 27033

File size: 2.9 KB
RevLine 
[21341]1import numpy as np
[13341]2
3
[24313]4def expwrite(contours, filename):
[26744]5 """EXPWRITE - write an Argus file from a dictionary given in input
[13341]6
[26744]7 This routine writes an Argus file from a dict containing the fields:
8 x and y of the coordinates of the points.
[27035]9 The first argument is the list containing the points coordinates and the
[26744]10 second one the file to be written.
[13341]11
[26744]12 Usage:
13 expwrite(contours, filename)
[13341]14
[26744]15 Example:
16 expwrite(coordstruct, 'domainoutline.exp')
[13341]17
[26744]18 See also EXPDOC, EXPREAD, EXPWRITEASVERTICES
[24313]19 """
[14310]20
[24313]21 fid = open(filename, 'w')
[27035]22 #if it is a list we need to loop on several contours
23 if isinstance(contours, list):
24 for contour in contours:
25 #if it is some kind of array it is a contour and we loop on indexes
26 if isinstance(contour['x'], (list, tuple, np.ndarray)):
27 writegeomlist(contour, fid, filename)
28 #else it is an index and we just write it down
29 else:
30 writegeom(contour, fid, filename)
31 #if it is a dict type it means just one contour
32 else:
33 #if it is some kind of array it is a contour and we loop on indexes
34 if isinstance(contours['x'], (list, tuple, np.ndarray)):
35 writegeomlist(contours, fid, filename)
36 #else it is an index and we just write it down
37 else:
38 writegeom(contours, fid, filename)
[26744]39
[27035]40 fid.close()
41
42
43def writegeomlist(contour, fid, filename):
44 if len(contour['x']) != len(contour['y']):
45 raise RuntimeError('contours x and y coordinates must be of identical size')
46 if 'name' in contour:
47 fid.write('{}{}\n'.format('## Name:', contour['name']))
48 else:
49 fid.write('{}{}\n'.format('## Name:', filename))
50
51 fid.write('{}\n'.format('## Icon:0'))
52 fid.write('{}\n'.format('# Points Count Value'))
53 if 'density' in contour:
54 if isinstance(contour['density'], int):
55 fid.write('{} {}\n'.format(np.size(contour['x']), contour['density']))
[24313]56 else:
[27035]57 fid.write('{} {}\n'.format(np.size(contour['x']), 1.))
58 else:
59 fid.write('{} {}\n'.format(np.size(contour['x']), 1.))
60 fid.write('{}\n'.format('# X pos Y pos'))
61 for x, y in zip(contour['x'], contour['y']):
62 fid.write('%10.10f %10.10f\n' % (x, y))
63 fid.write('\n')
[13341]64
[27035]65
66def writegeom(contour, fid, filename):
67 if 'name' in contour:
68 fid.write('{}{}\n'.format('## Name:', contour['name']))
69 else:
70 fid.write('{}{}\n'.format('## Name:', filename))
71
72 fid.write('{}\n'.format('## Icon:0'))
73 fid.write('{}\n'.format('# Points Count Value'))
74 if 'density' in contour:
75 if isinstance(contour['density'], int):
76 fid.write('{} {}\n'.format(1, contour['density']))
[26744]77 else:
[27035]78 fid.write('{} {}\n'.format(1, 1.))
79 else:
80 fid.write('{} {}\n'.format(1, 1.))
81 fid.write('{}\n'.format('# X pos Y pos'))
82 fid.write('%10.10f %10.10f\n' % (contour['x'], contour['y']))
83 fid.write('\n')
Note: See TracBrowser for help on using the repository browser.