|
1 | 1 |
|
2 | | -from numpy import array, loadtxt |
3 | | - |
| 2 | +import numpy as np |
| 3 | + |
| 4 | + |
| 5 | +# Write an GEOS-compatible ascii table |
| 6 | +# axes_values = A list of the axes values in order |
| 7 | +# properties = A dictionary of properties given as an ndarray |
| 8 | +# The shape of the array must be consistent with the axes |
| 9 | +# axes_names = A list of axes names (optional) |
| 10 | +# string_format = Format for table values |
| 11 | +def write_GEOS_table(axes_values, properties, axes_names=['x', 'y', 'z', 't'], string_format='%1.5e'): |
| 12 | + # Check to make sure the axes/property files have the correct shape |
| 13 | + axes_shape = tuple([len(x) for x in axes_values]) |
| 14 | + for k in properties.keys(): |
| 15 | + if (np.shape(properties[k]) != axes_shape): |
| 16 | + raise Exception("Shape of parameter %s is incompatible with given axes" % (k)) |
4 | 17 |
|
5 | | -def writeGEOSTable(spatial, properties): |
6 | | - # Open spatial files |
7 | | - spatial_names = ('x', 'y', 'z', 't') |
8 | | - for ii in range(0, len(spatial)): |
9 | | - with open('%s.txt' % spatial_names[ii], 'w') as f: |
10 | | - for p in spatial[ii]: |
11 | | - f.write('%s\n' % (p)) |
| 18 | + # Write axes files |
| 19 | + for ii in range(0, len(axes_values)): |
| 20 | + np.savetxt('%s.geos' % (axes_names[ii]), axes_values[ii], fmt=string_format, delimiter=',') |
12 | 21 |
|
13 | | - # Open property files |
| 22 | + # Write property files |
14 | 23 | for k in properties.keys(): |
15 | | - with open('%s.txt' % (k), 'w') as f: |
16 | | - # Header |
17 | | - for ii in range(0, len(spatial)): |
18 | | - f.write('%s\n' % (len(spatial[ii]))) |
19 | | - |
20 | | - # Table |
21 | | - values = array(properties[k]).reshape(-1, order='F') |
22 | | - for v in values: |
23 | | - f.write('%1.3e\n' % (v)) |
| 24 | + tmp = np.reshape(properties[k], (-1), order='F') |
| 25 | + np.savetxt('%s.geos' % (k), tmp, fmt=string_format, delimiter=',') |
24 | 26 |
|
25 | 27 |
|
26 | | -def readGEOSTable(sfiles, pfiles): |
| 28 | +# Read an GEOS-compatible ascii table |
| 29 | +# axes_files = A list of the axes file names in order |
| 30 | +# property_files = A list of the property file names |
| 31 | +def read_GEOS_table(axes_files, property_files): |
27 | 32 | # Open spatial files |
28 | | - spatial = [] |
29 | | - for s in sfiles: |
30 | | - spatial.append(loadtxt('%s.txt' % (s), unpack=True)) |
| 33 | + axes_values = [] |
| 34 | + for f in axes_files: |
| 35 | + axes_values.append(np.loadtxt('%s.geos' % (f), unpack=True, delimiter=',')) |
| 36 | + axes_shape = tuple([len(x) for x in axes_values]) |
31 | 37 |
|
32 | 38 | # Open property files |
33 | 39 | properties = {} |
34 | | - for p in pfiles: |
35 | | - tmp = loadtxt('%s.txt' % (p), unpack=True) |
36 | | - properties[p] = tmp[3:].reshape(tmp[:3], order='F') |
| 40 | + for f in property_files: |
| 41 | + tmp = np.loadtxt('%s.geos' % (f), unpack=True, delimiter=',') |
| 42 | + properties[f] = np.reshape(tmp, axes_shape, order='F') |
| 43 | + |
| 44 | + return axes_values, properties |
| 45 | + |
| 46 | + |
| 47 | +# Example of how to read/write GEOS tables using the above functions |
| 48 | +def write_read_GEOS_table_example(): |
| 49 | + # Define table axes |
| 50 | + a = np.array([0.0, 1.0]) |
| 51 | + b = np.array([0.0, 0.5, 1.0]) |
| 52 | + axes_values = [a, b] |
| 53 | + |
| 54 | + # Generate table values (note: the indexing argument is important) |
| 55 | + A, B = np.meshgrid(a, b, indexing='ij') |
| 56 | + properties = {'c': A + 2.0*B} |
| 57 | + |
| 58 | + # Write, then read tables |
| 59 | + write_GEOS_table(axes_values, properties, axes_names=['a', 'b']) |
| 60 | + axes_b, properties_b = read_GEOS_table(['a', 'b'], ['c']) |
| 61 | + |
| 62 | + |
37 | 63 |
|
38 | | - return spatial, properties |
|
0 commit comments