1 | """This scripts recreates fitting problem where values seem to go towards zero |
---|
2 | in areas with missing data. |
---|
3 | """ |
---|
4 | from os.path import join |
---|
5 | from anuga.utilities.polygon import read_polygon |
---|
6 | from anuga.shallow_water.data_manager import convert_dem_from_ascii2netcdf |
---|
7 | from anuga.shallow_water.data_manager import dem2pts |
---|
8 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
9 | from anuga.interface import create_domain_from_regions |
---|
10 | from anuga.interface import Reflective_boundary |
---|
11 | |
---|
12 | import project, os |
---|
13 | import sys |
---|
14 | from anuga.shallow_water.data_manager import start_screen_catcher |
---|
15 | from anuga.shallow_water.data_manager import copy_code_files |
---|
16 | from anuga.shallow_water.data_manager import convert_dem_from_ascii2netcdf, dem2pts |
---|
17 | from anuga.shallow_water.data_manager import sww2dem |
---|
18 | from os import sep |
---|
19 | |
---|
20 | # Application specific imports |
---|
21 | from setup_model import project # Definition of file names and polygons |
---|
22 | |
---|
23 | |
---|
24 | #------------------------------------------------------------------------------- |
---|
25 | # Setup |
---|
26 | #------------------------------------------------------------------------------- |
---|
27 | |
---|
28 | scenario_name = 'fitting_problem_' + project.extent |
---|
29 | |
---|
30 | # Initial bounding polygon for data clipping |
---|
31 | bounding_polygon = read_polygon(join(project.polygons_folder, project.extent)+'.csv') |
---|
32 | |
---|
33 | output_run = join(project.output_folder, project.run_time) + scenario_name |
---|
34 | |
---|
35 | #------------------------------------------------------------------------------- |
---|
36 | # Copy scripts to time stamped output directory and capture screen |
---|
37 | # output to file. Copy script must be before screen_catcher |
---|
38 | #------------------------------------------------------------------------------- |
---|
39 | copy_code_files(output_run, __file__, |
---|
40 | os.path.join(os.path.dirname(project.__file__), |
---|
41 | project.__name__+'.py')) |
---|
42 | start_screen_catcher(output_run, 0, 1) |
---|
43 | |
---|
44 | #----------------------------------------------------------------------------- |
---|
45 | # Preparation of topographic data |
---|
46 | # |
---|
47 | # Convert ASC 2 DEM 2 PTS using source data and store result in source data |
---|
48 | # Do for coarse and fine data |
---|
49 | # Fine pts file to be clipped to area of interest |
---|
50 | #----------------------------------------------------------------------------- |
---|
51 | |
---|
52 | print 'bounding_polygon', bounding_polygon |
---|
53 | combined_elevation = project.combined_elevation + '_' + scenario_name + '.pts' |
---|
54 | print 'combined_elevation', combined_elevation |
---|
55 | if not os.path.exists(combined_elevation): |
---|
56 | print 'Hello, Create Geospatial data' |
---|
57 | # Creating Geospatial data from ASCII files' |
---|
58 | geospatial_data = {} |
---|
59 | topographies_folder = join(project.topographies_folder, 'original') |
---|
60 | if not project.ascii_grid_filenames == []: |
---|
61 | for filename in project.ascii_grid_filenames: |
---|
62 | absolute_filename = join(topographies_folder, filename) |
---|
63 | convert_dem_from_ascii2netcdf(absolute_filename, |
---|
64 | basename_out=absolute_filename, |
---|
65 | use_cache=True, |
---|
66 | verbose=True) |
---|
67 | dem2pts(absolute_filename, use_cache=True, verbose=True) |
---|
68 | |
---|
69 | G_grid = Geospatial_data(file_name=absolute_filename+'.pts', |
---|
70 | verbose=True) |
---|
71 | print 'Clip geospatial object' |
---|
72 | geospatial_data[filename] = G_grid.clip(bounding_polygon) |
---|
73 | |
---|
74 | # Create Geospatial data from TXT files |
---|
75 | if not project.point_filenames == []: |
---|
76 | for filename in project.point_filenames: |
---|
77 | absolute_filename = join(topographies_folder, filename) |
---|
78 | G_points = Geospatial_data(file_name=absolute_filename, |
---|
79 | verbose=True) |
---|
80 | print 'Clip geospatial object' |
---|
81 | geospatial_data[filename] = G_points.clip(bounding_polygon) |
---|
82 | |
---|
83 | |
---|
84 | #------------------------------------------------------------------------------ |
---|
85 | # Combine, clip and export dataset |
---|
86 | #------------------------------------------------------------------------------ |
---|
87 | |
---|
88 | print 'Add geospatial objects' |
---|
89 | G = None |
---|
90 | for key in geospatial_data: |
---|
91 | G += geospatial_data[key] |
---|
92 | |
---|
93 | print 'Export combined DEM file' |
---|
94 | G.export_points_file(combined_elevation) |
---|
95 | |
---|
96 | print 'Do txt version too' |
---|
97 | # Use for comparision in ARC |
---|
98 | G.export_points_file(combined_elevation[:-4] + '.txt') |
---|
99 | |
---|
100 | |
---|
101 | #------------------------------------------------------------------------------ |
---|
102 | # Create ANUGA mesh and fit |
---|
103 | #------------------------------------------------------------------------------ |
---|
104 | |
---|
105 | tags = {'x': range(len(bounding_polygon))} # All segments tagged the same |
---|
106 | domain = create_domain_from_regions(bounding_polygon, |
---|
107 | boundary_tags=tags, |
---|
108 | maximum_triangle_area=project.extent_maxarea, |
---|
109 | interior_regions=project.interior_regions, |
---|
110 | mesh_filename=project.meshes, |
---|
111 | use_cache=False, |
---|
112 | verbose=True) |
---|
113 | print domain.statistics() |
---|
114 | |
---|
115 | |
---|
116 | domain.set_name(scenario_name) |
---|
117 | domain.set_datadir(output_run) |
---|
118 | domain.set_minimum_storable_height(0.01) # Don't store depth less than 1cm |
---|
119 | |
---|
120 | #------------------------------------------------------------------------------ |
---|
121 | # Setup initial conditions |
---|
122 | #------------------------------------------------------------------------------ |
---|
123 | |
---|
124 | print 'Setup initial conditions' |
---|
125 | domain.set_quantity('stage', 0.0) |
---|
126 | domain.set_quantity('friction', 0.01) |
---|
127 | domain.set_quantity('elevation', |
---|
128 | filename=combined_elevation, |
---|
129 | use_cache=True, |
---|
130 | verbose=True, |
---|
131 | alpha=0.1) |
---|
132 | |
---|
133 | #------------------------------------------------------------------------------ |
---|
134 | # Setup boundary conditions |
---|
135 | #------------------------------------------------------------------------------ |
---|
136 | |
---|
137 | print 'Set boundary - available tags:', domain.get_boundary_tags() |
---|
138 | Br = Reflective_boundary(domain) |
---|
139 | domain.set_boundary({'x': Br}) |
---|
140 | |
---|
141 | #----------------------------------------------------------------------------- |
---|
142 | # Take a few steps |
---|
143 | #----------------------------------------------------------------------------- |
---|
144 | |
---|
145 | |
---|
146 | for t in domain.evolve(yieldstep=1, |
---|
147 | finaltime=3): |
---|
148 | print domain.timestepping_statistics() |
---|
149 | print domain.boundary_statistics(tags='x') |
---|
150 | |
---|
151 | print 'Done' |
---|
152 | |
---|
153 | |
---|
154 | #----------------------------------------------------------------------------- |
---|
155 | # Exporting Grid |
---|
156 | # Define allowed variable names and associated equations to generate values. |
---|
157 | # This would not normally change. |
---|
158 | #----------------------------------------------------------------------------- |
---|
159 | var_equations = {'stage': 'stage', |
---|
160 | 'momentum': '(xmomentum**2 + ymomentum**2)**0.5', |
---|
161 | 'depth': 'stage-elevation', |
---|
162 | 'speed': '(xmomentum**2 + ymomentum**2)**0.5/(stage-elevation+1.e-6)', |
---|
163 | 'elevation': 'elevation' } |
---|
164 | |
---|
165 | # one or more key strings from var_equations above |
---|
166 | var = ['elevation'] |
---|
167 | |
---|
168 | ###### |
---|
169 | # Start running the various conversions we require. |
---|
170 | ###### |
---|
171 | |
---|
172 | for which_var in var: |
---|
173 | if which_var not in var_equations: |
---|
174 | print 'Unrecognized variable name: %s' % which_var |
---|
175 | break |
---|
176 | |
---|
177 | name = join(output_run, scenario_name) |
---|
178 | outname = name + '_' + which_var |
---|
179 | quantityname = var_equations[which_var] |
---|
180 | |
---|
181 | print 'start sww2dem: name=%s' % name |
---|
182 | |
---|
183 | sww2dem(name, basename_out = outname, |
---|
184 | quantity = quantityname, |
---|
185 | timestep = 0, |
---|
186 | cellsize = 10, |
---|
187 | reduction = max, |
---|
188 | verbose = True, |
---|
189 | format = 'asc') |
---|
190 | |
---|
191 | |
---|
192 | |
---|