source: inundation/ga/storm_surge/pyvolution/combine_pts.py @ 1266

Last change on this file since 1266 was 1101, checked in by duncan, 20 years ago

bug fix

File size: 7.0 KB
Line 
1"""Add two point files by the union of the fine and coarse points.
2Course points that are in the extent of the fine points are removed.
3
4The extent of the fine points file is assumed to be a rectangle, parallel
5to the x and y axis.
6
7   Ole Nielsen, Stephen Roberts, Duncan Gray
8   Geoscience Australia, 2005.   
9"""
10from util import outside_polygon, inside_polygon
11from Numeric import take, concatenate
12import time
13
14import exceptions
15class AttributeError(exceptions.Exception): pass
16
17def combine_rectangular_points_files(fine_points_file,
18                                    coarse_points_file,
19                                    output_points_file,
20                                     verbose = False):
21   
22    from load_mesh.loadASCII import load_points_file, extent, point_atts2array, write_pts, add_point_dictionaries,take_points
23
24   
25    # load fine points file
26    if verbose:print "loading Point files"
27    try:
28        fine = load_points_file(fine_points_file,delimiter = ',')
29    except SyntaxError,e:
30        fine = load_points_file(fine_points_file,delimiter = ' ')
31   
32   
33    # load course points file
34    try:
35        coarse = load_points_file(coarse_points_file,
36                                              delimiter = ',')
37    except SyntaxError,e:
38        coarse = load_points_file(coarse_points_file,
39                                              delimiter = ' ')
40       
41    if verbose:print "doing other stuff"
42    # convert to Numeric   
43    fine = point_atts2array(fine)
44    coarse = point_atts2array(coarse)
45   
46    #check that the attribute info is the same
47    #FIXME is not sorting this a problem?
48    if not fine['attributelist'].keys() == coarse['attributelist'].keys():
49        raise AttributeError
50   
51    # set points to the same geo ref -  say the fine points geo ref
52    if fine.has_key('geo_reference')and not fine['geo_reference'] is None:
53        if coarse.has_key('geo_reference'):
54            coarse['pointlist'] = \
55             fine['geo_reference'].change_points_geo_ref(coarse['pointlist'],
56                                                         points_geo_ref=coarse['geo_reference']) 
57    # find extent of course points
58    extent = extent(fine['pointlist'])   
59
60    # find out_points - coarse points outside of fine points extent
61
62    if verbose:
63        print "starting outside_polygon"
64        t0 = time.time()
65    outside_coarse_indices = outside_polygon(coarse['pointlist'],
66                                             extent,closed=True)
67    if verbose:
68        print "Points outside determined"
69        print 'That took %.2f seconds' %(time.time()-t0)
70
71    # Remove points from the coarse data
72    coarse = take_points(coarse, outside_coarse_indices)
73       
74    # add fine points and out_points
75    if verbose:print "Adding points"
76    combined = add_point_dictionaries(fine, coarse)
77
78    # save
79    if verbose:print "writing points"
80    write_pts(output_points_file, combined)
81
82def add_points_files(fine_points_file,
83                                    coarse_points_file,
84                                    output_points_file,
85                                     verbose = False):
86   
87    from load_mesh.loadASCII import load_points_file, extent, point_atts2array, write_pts, add_point_dictionaries,take_points
88
89   
90    # load fine points file
91    if verbose:print "loading Point files"
92    try:
93        fine = load_points_file(fine_points_file,delimiter = ',')
94    except SyntaxError,e:
95        fine = load_points_file(fine_points_file,delimiter = ' ')
96   
97   
98    # load course points file
99    try:
100        coarse = load_points_file(coarse_points_file,
101                                              delimiter = ',')
102    except SyntaxError,e:
103        coarse = load_points_file(coarse_points_file,
104                                              delimiter = ' ')
105       
106    if verbose:print "doing other stuff"
107    # convert to Numeric   
108    fine = point_atts2array(fine)
109    coarse = point_atts2array(coarse)
110   
111    #check that the attribute info is the same
112    #FIXME is not sorting this a problem?
113    if not fine['attributelist'].keys() == coarse['attributelist'].keys():
114        raise AttributeError
115   
116    # set points to the same geo ref -  say the fine points geo ref
117    if fine.has_key('geo_reference')and not fine['geo_reference'] is None:
118        if coarse.has_key('geo_reference'):
119            coarse['pointlist'] = \
120             fine['geo_reference'].change_points_geo_ref(coarse['pointlist'],
121                                                         points_geo_ref=coarse['geo_reference']) 
122       
123    # add fine points and out_points
124    if verbose:print "Adding points"
125    combined = add_point_dictionaries(fine, coarse)
126
127    # save
128    if verbose:print "writing points"
129    write_pts(output_points_file, combined)
130
131def reduce_points_to_mesh_extent(points_file,
132                                    mesh_file,
133                                    output_points_file,
134                                     verbose = False):
135   
136    from load_mesh.loadASCII import load_points_file, extent, point_atts2array, write_pts, add_point_dictionaries,take_points, mesh_file_to_mesh_dictionary
137
138   
139    # load  points file
140    try:
141        points = load_points_file(points_file,delimiter = ',')
142    except SyntaxError,e:
143        points = load_points_file(points_file,delimiter = ' ')
144   
145    # load  mesh file
146    mesh = mesh_file_to_mesh_dictionary(mesh_file)
147
148    # get extent of mesh
149    extent = extent(mesh['vertices'])
150    #print "extent",extent
151    # set points to the same geo ref - the points geo ref
152    if points.has_key('geo_reference')and not points['geo_reference'] is None:
153        if mesh.has_key('geo_reference'):
154            extent = \
155             points['geo_reference'].change_points_geo_ref(extent,
156                                                         points_geo_ref=mesh['geo_reference']) 
157    #print "extent",extent
158
159    #get a list of in point indexes
160    #FIXME closed doesn't seems to work here.
161    inside_indices = inside_polygon(points['pointlist'],
162                                             extent,closed=True)
163    #create a list of in points
164    points = take_points(points, inside_indices)
165
166    # save the list, along with geo-ref
167    write_pts(output_points_file,points)
168   
169    #-------------------------------------------------------------
170if __name__ == "__main__":
171    """
172    Load in two data points files.
173    Save a new points file.
174    """
175    import os, sys
176    usage = "usage: %s fine_input.pts coarse_input.pts output.pts " \
177            %os.path.basename(sys.argv[0])
178
179    if len(sys.argv) < 4:
180        print usage
181    else:
182        fine_file  = sys.argv[1]
183        coarse_file = sys.argv[2]
184        output_file = sys.argv[3]
185       
186        combine_rectangular_points_files(fine_file,
187                                         coarse_file,
188                                         output_file)
Note: See TracBrowser for help on using the repository browser.