1 | """Create several realisations of bathymetries for use with run_model. |
---|
2 | |
---|
3 | This script will create a number of random samples of the input bathymetry and |
---|
4 | then fit all of them to arrays that can be used in set_quantity inside run_model. |
---|
5 | |
---|
6 | The reason for this is that doing all fits in one sweep saves computation time |
---|
7 | """ |
---|
8 | |
---|
9 | from RandomArray import normal |
---|
10 | from Numeric import concatenate, NewAxis |
---|
11 | import cPickle |
---|
12 | from load_mesh.loadASCII import import_points_file, import_mesh_file |
---|
13 | from pyvolution.least_squares import fit_to_mesh |
---|
14 | import project |
---|
15 | |
---|
16 | bathymetry_filename = project.bathymetry_filename |
---|
17 | mesh_filename = project.mesh_filename |
---|
18 | |
---|
19 | |
---|
20 | # Read mesh |
---|
21 | mesh_dict = import_mesh_file(mesh_filename) |
---|
22 | vertex_coordinates = mesh_dict['vertices'] |
---|
23 | triangles = mesh_dict['triangles'] |
---|
24 | |
---|
25 | # Read basic bathymetry from NetCDF .pts file |
---|
26 | print 'Reading bathymetry' |
---|
27 | base_bathymetry = import_points_file(bathymetry_filename) |
---|
28 | |
---|
29 | |
---|
30 | # Extract relevant Numeric arrays |
---|
31 | print 'Extract bathymetry' |
---|
32 | point_coordinates = base_bathymetry['pointlist'] |
---|
33 | elevation = base_bathymetry['attributelist']['elevation'] |
---|
34 | N = len(elevation) |
---|
35 | print 'read', N, 'points' |
---|
36 | |
---|
37 | # Create different bathymetries based on sampling (Suresh to fill this in) |
---|
38 | print 'Create different realisations' |
---|
39 | Z = None |
---|
40 | block_number = 0 |
---|
41 | for n in range(project.number_of_samples): |
---|
42 | |
---|
43 | print 'Creating sample #%d' %n |
---|
44 | # nth realisation of bathymetry |
---|
45 | z = elevation + normal(project.mean, |
---|
46 | project.std_dev, |
---|
47 | N) |
---|
48 | |
---|
49 | |
---|
50 | #Concatenate into array with each realisation a column |
---|
51 | if Z is None: |
---|
52 | Z = z[:, NewAxis] # The first realisation of this block |
---|
53 | else: |
---|
54 | Z = concatenate((Z, z[:, NewAxis]), axis=1) |
---|
55 | |
---|
56 | if (n+1)%project.blocksize == 0 or n == project.number_of_samples: |
---|
57 | #Fit all of them to computational mesh |
---|
58 | print 'fit block %d to the mesh' %block_number |
---|
59 | V = fit_to_mesh(vertex_coordinates, |
---|
60 | triangles, |
---|
61 | point_coordinates, |
---|
62 | Z, |
---|
63 | alpha = 0.0001, |
---|
64 | verbose = True, |
---|
65 | expand_search = True, |
---|
66 | precrop = True, |
---|
67 | use_cache = True) |
---|
68 | |
---|
69 | |
---|
70 | Z = None |
---|
71 | |
---|
72 | #Store this block for use with model |
---|
73 | print 'Store fitted quantities in block %d for use with model'\ |
---|
74 | %block_number |
---|
75 | |
---|
76 | filename = project.basename + '_block%d.pck'\ |
---|
77 | %block_number |
---|
78 | |
---|
79 | block_number += 1 |
---|
80 | |
---|
81 | |
---|
82 | fid = open(filename, 'wb') |
---|
83 | cPickle.dump(V, fid) |
---|
84 | fid.close() |
---|
85 | |
---|