Changeset 2891 for inundation/pyvolution
- Timestamp:
- May 17, 2006, 12:11:37 PM (18 years ago)
- Location:
- inundation/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/data_manager.py
r2884 r2891 1665 1665 1666 1666 1667 # Get quantity and reduce if applicable1667 # Get quantity and reduce if applicable 1668 1668 if verbose: print 'Processing quantity %s' %quantity 1669 1669 1670 # Turn NetCDF objects into Numeric arrays1670 # Turn NetCDF objects into Numeric arrays 1671 1671 quantity_dict = {} 1672 1672 for name in fid.variables.keys(): … … 1674 1674 1675 1675 1676 1677 # Convert quantity expression to quantities found in sww file 1676 1678 q = apply_expression_to_dictionary(quantity, quantity_dict) 1677 1679 … … 1909 1911 verbose = verbose, 1910 1912 origin = origin, 1911 datum = datum,1912 format = 'ers')1913 datum = datum, 1914 format = 'ers') 1913 1915 ################################# END COMPATIBILITY ############## 1916 1917 1918 1919 def sww2pts(basename_in, basename_out=None, 1920 data_points=None, 1921 quantity=None, 1922 timestep=None, 1923 reduction=None, 1924 NODATA_value=-9999, 1925 verbose=False, 1926 origin=None): 1927 #datum = 'WGS84') 1928 1929 1930 """Read SWW file and convert to interpolated values at selected points 1931 1932 The parameter quantity' must be the name of an existing quantity or 1933 an expression involving existing quantities. The default is 1934 'elevation'. 1935 1936 if timestep (an index) is given, output quantity at that timestep 1937 1938 if reduction is given use that to reduce quantity over all timesteps. 1939 1940 data_points (Nx2 array) give locations of points where quantity is to be computed. 1941 1942 """ 1943 1944 import sys 1945 from Numeric import array, Float, concatenate, NewAxis, zeros, reshape, sometrue 1946 from Numeric import array2string 1947 1948 from utilities.polygon import inside_polygon, outside_polygon, separate_points_by_polygon 1949 from util import apply_expression_to_dictionary 1950 1951 from geospatial_data import Geospatial_data 1952 1953 if quantity is None: 1954 quantity = 'elevation' 1955 1956 if reduction is None: 1957 reduction = max 1958 1959 if basename_out is None: 1960 basename_out = basename_in + '_%s' %quantity 1961 1962 swwfile = basename_in + '.sww' 1963 ptsfile = basename_out + '.pts' 1964 1965 # Read sww file 1966 if verbose: print 'Reading from %s' %swwfile 1967 from Scientific.IO.NetCDF import NetCDFFile 1968 fid = NetCDFFile(swwfile) 1969 1970 # Get extent and reference 1971 x = fid.variables['x'][:] 1972 y = fid.variables['y'][:] 1973 volumes = fid.variables['volumes'][:] 1974 1975 number_of_timesteps = fid.dimensions['number_of_timesteps'] 1976 number_of_points = fid.dimensions['number_of_points'] 1977 if origin is None: 1978 1979 # Get geo_reference 1980 # sww files don't have to have a geo_ref 1981 try: 1982 geo_reference = Geo_reference(NetCDFObject=fid) 1983 except AttributeError, e: 1984 geo_reference = Geo_reference() #Default georef object 1985 1986 xllcorner = geo_reference.get_xllcorner() 1987 yllcorner = geo_reference.get_yllcorner() 1988 zone = geo_reference.get_zone() 1989 else: 1990 zone = origin[0] 1991 xllcorner = origin[1] 1992 yllcorner = origin[2] 1993 1994 1995 1996 # FIXME: Refactor using code from file_function.statistics 1997 # Something like print swwstats(swwname) 1998 if verbose: 1999 x = fid.variables['x'][:] 2000 y = fid.variables['y'][:] 2001 times = fid.variables['time'][:] 2002 print '------------------------------------------------' 2003 print 'Statistics of SWW file:' 2004 print ' Name: %s' %swwfile 2005 print ' Reference:' 2006 print ' Lower left corner: [%f, %f]'\ 2007 %(xllcorner, yllcorner) 2008 print ' Start time: %f' %fid.starttime[0] 2009 print ' Extent:' 2010 print ' x [m] in [%f, %f], len(x) == %d'\ 2011 %(min(x.flat), max(x.flat), len(x.flat)) 2012 print ' y [m] in [%f, %f], len(y) == %d'\ 2013 %(min(y.flat), max(y.flat), len(y.flat)) 2014 print ' t [s] in [%f, %f], len(t) == %d'\ 2015 %(min(times), max(times), len(times)) 2016 print ' Quantities [SI units]:' 2017 for name in ['stage', 'xmomentum', 'ymomentum', 'elevation']: 2018 q = fid.variables[name][:].flat 2019 print ' %s in [%f, %f]' %(name, min(q), max(q)) 2020 2021 2022 2023 # Get quantity and reduce if applicable 2024 if verbose: print 'Processing quantity %s' %quantity 2025 2026 # Turn NetCDF objects into Numeric arrays 2027 quantity_dict = {} 2028 for name in fid.variables.keys(): 2029 quantity_dict[name] = fid.variables[name][:] 2030 2031 2032 2033 # Convert quantity expression to quantities found in sww file 2034 q = apply_expression_to_dictionary(quantity, quantity_dict) 2035 2036 2037 2038 if len(q.shape) == 2: 2039 # q has a time component and needs to be reduced along 2040 # the temporal dimension 2041 if verbose: print 'Reducing quantity %s' %quantity 2042 q_reduced = zeros( number_of_points, Float ) 2043 2044 for k in range(number_of_points): 2045 q_reduced[k] = reduction( q[:,k] ) 2046 2047 q = q_reduced 2048 2049 # Post condition: Now q has dimension: number_of_points 2050 assert len(q.shape) == 1 2051 assert q.shape[0] == number_of_points 2052 2053 2054 if verbose: 2055 print 'Processed values for %s are in [%f, %f]' %(quantity, min(q), max(q)) 2056 2057 2058 # Create grid and update xll/yll corner and x,y 2059 vertex_points = concatenate ((x[:, NewAxis] ,y[:, NewAxis]), axis = 1) 2060 assert len(vertex_points.shape) == 2 2061 2062 # Interpolate 2063 from fit_interpolate.interpolate import Interpolate 2064 interp = Interpolate(vertex_points, volumes, verbose = verbose) 2065 2066 # Interpolate using quantity values 2067 if verbose: print 'Interpolating' 2068 interpolated_values = interp.interpolate(q, data_points).flat 2069 2070 2071 if verbose: 2072 print 'Interpolated values are in [%f, %f]' %(min(interpolated_values), 2073 max(interpolated_values)) 2074 2075 # Assign NODATA_value to all points outside bounding polygon (from interpolation mesh) 2076 P = interp.mesh.get_boundary_polygon() 2077 outside_indices = outside_polygon(data_points, P, closed=True) 2078 2079 for i in outside_indices: 2080 interpolated_values[i] = NODATA_value 2081 2082 2083 # Store results 2084 G = Geospatial_data(data_points=data_points, 2085 attributes=interpolated_values) 2086 2087 G.export_points_file(ptsfile, absolute = True) 1914 2088 1915 2089 -
inundation/pyvolution/test_data_manager.py
r2658 r2891 1996 1996 1997 1997 #Export to ers files 1998 #sww2ers(self.domain.filename,1999 # quantity = 'elevation',2000 # cellsize = cellsize,2001 # verbose = False)2002 2003 1998 sww2dem(self.domain.filename, 2004 1999 quantity = 'elevation', … … 2049 2044 os.remove(self.domain.filename + '_elevation') 2050 2045 os.remove(self.domain.filename + '_elevation.ers') 2046 2047 2048 2049 def test_sww2pts_centroids(self): 2050 """Test that sww information can be converted correctly to pts data at specified coordinates 2051 - in this case, the centroids. 2052 """ 2053 2054 import time, os 2055 from Numeric import array, zeros, allclose, Float, concatenate, NewAxis 2056 from Scientific.IO.NetCDF import NetCDFFile 2057 from geospatial_data import Geospatial_data 2058 2059 # Used for points that lie outside mesh 2060 NODATA_value = 1758323 2061 2062 # Setup 2063 self.domain.filename = 'datatest' 2064 2065 ptsfile = self.domain.filename + '_elevation.pts' 2066 swwfile = self.domain.filename + '.sww' 2067 2068 self.domain.set_datadir('.') 2069 self.domain.format = 'sww' 2070 self.smooth = True #self.set_store_vertices_uniquely(False) 2071 self.domain.set_quantity('elevation', lambda x,y: -x-y) 2072 2073 self.domain.geo_reference = Geo_reference(56,308500,6189000) 2074 2075 sww = get_dataobject(self.domain) 2076 sww.store_connectivity() 2077 sww.store_timestep('stage') 2078 2079 self.domain.evolve_to_end(finaltime = 0.01) 2080 sww.store_timestep('stage') 2081 2082 # Check contents in NetCDF 2083 fid = NetCDFFile(sww.filename, 'r') 2084 2085 # Get the variables 2086 x = fid.variables['x'][:] 2087 y = fid.variables['y'][:] 2088 elevation = fid.variables['elevation'][:] 2089 time = fid.variables['time'][:] 2090 stage = fid.variables['stage'][:] 2091 2092 volumes = fid.variables['volumes'][:] 2093 2094 2095 # Invoke interpolation for vertex points 2096 points = concatenate( (x[:,NewAxis],y[:,NewAxis]), axis=1 ) 2097 sww2pts(self.domain.filename, 2098 quantity = 'elevation', 2099 data_points = points, 2100 NODATA_value = NODATA_value, 2101 verbose = False) 2102 ref_point_values = elevation 2103 point_values = Geospatial_data(ptsfile).get_attributes() 2104 #print 'P', point_values 2105 #print 'Ref', ref_point_values 2106 assert allclose(point_values, ref_point_values) 2107 2108 2109 2110 # Invoke interpolation for centroids 2111 points = self.domain.get_centroid_coordinates() 2112 #print points 2113 sww2pts(self.domain.filename, 2114 quantity = 'elevation', 2115 data_points = points, 2116 NODATA_value = NODATA_value, 2117 verbose = False) 2118 ref_point_values = [-0.5, -0.5, -1, -1, -1, -1, -1.5, -1.5] #At centroids 2119 2120 2121 point_values = Geospatial_data(ptsfile).get_attributes() 2122 #print 'P', point_values 2123 #print 'Ref', ref_point_values 2124 assert allclose(point_values, ref_point_values) 2125 2126 2127 2128 fid.close() 2129 2130 #Cleanup 2131 os.remove(sww.filename) 2132 os.remove(ptsfile) 2133 2051 2134 2052 2135 -
inundation/pyvolution/util.py
r2884 r2891 570 570 Name = gauges_timeseries followed by gauge name 571 571 - latex file will be generated in same directory as where script is 572 run (usually production scenario direc otry.572 run (usually production scenario directory. 573 573 Name = latexoutputlabel_id.tex 574 574
Note: See TracChangeset
for help on using the changeset viewer.