source: development/stochastic_study/create_realisations.py @ 2898

Last change on this file since 2898 was 2726, checked in by ole, 19 years ago

Forgot to add create_realisations.py

File size: 2.6 KB
RevLine 
[2726]1"""Create several realisations of bathymetries for use with run_model.
2
3This script will create a number of random samples of the input bathymetry and
4then fit all of them to arrays that can be used in set_quantity inside run_model.
5
6The reason for this is that doing all fits in one sweep saves computation time
7"""
8
9from RandomArray import normal
10from Numeric import concatenate, NewAxis
11import cPickle
12from load_mesh.loadASCII import import_points_file, import_mesh_file
13from pyvolution.least_squares import fit_to_mesh
14import project
15
16bathymetry_filename = project.bathymetry_filename
17mesh_filename = project.mesh_filename
18
19
20# Read mesh
21mesh_dict = import_mesh_file(mesh_filename)
22vertex_coordinates = mesh_dict['vertices']
23triangles = mesh_dict['triangles']
24
25# Read basic bathymetry from NetCDF .pts file
26print 'Reading bathymetry'
27base_bathymetry = import_points_file(bathymetry_filename)
28
29
30# Extract relevant Numeric arrays
31print 'Extract bathymetry'
32point_coordinates = base_bathymetry['pointlist']
33elevation = base_bathymetry['attributelist']['elevation']
34N = len(elevation)
35print 'read', N, 'points'
36
37# Create different bathymetries based on sampling (Suresh to fill this in)
38print 'Create different realisations'
39Z = None
40block_number = 0
41for 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       
Note: See TracBrowser for help on using the repository browser.