Changeset 1660
- Timestamp:
- Aug 1, 2005, 11:58:24 AM (19 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r1657 r1660 1 """datamanager.py - input output for AnuGA 2 3 4 This module takes care of reading and writing datafiles such as topograhies, 5 model output, etc 6 7 8 Formats used within AnuGA: 1 """Functions to store and retrieve data for the Shallow Water Wave equation. 2 There are two kinds of data 3 4 1: Constant data: Vertex coordinates and field values. Stored once 5 2: Variable data: Conserved quantities. Stored once per timestep. 6 7 All data is assumed to reside at vertex locations. 8 9 10 Formats used within ANUGA: 9 11 10 12 .sww: Netcdf format for storing model output … … 207 209 #Class for storing output to e.g. visualisation 208 210 class Data_format_sww(Data_format): 209 """Interface to native NetCDF format (.sww) for storing model output 210 211 There are two kinds of data 212 213 1: Constant data: Vertex coordinates and field values. Stored once 214 2: Variable data: Conserved quantities. Stored once per timestep. 215 216 All data is assumed to reside at vertex locations. 211 """Interface to native NetCDF format (.sww) 217 212 """ 218 213 … … 253 248 if hasattr(domain, 'texture'): 254 249 fid.texture = domain.texture 255 else: 256 pass 257 #fid.texture = 'None' 250 else: 251 fid.texture = 'None' 258 252 259 253 #Reference point … … 927 921 return cls(domain, mode) 928 922 929 930 def xya2pts(basename_in, basename_out=None, verbose=False,931 #easting_min=None, easting_max=None,932 #northing_min=None, northing_max=None,933 attribute_name = 'elevation'):934 """Read points data from ascii (.xya)935 936 Example:937 938 x(m) y(m) z(m)939 0.00000e+00 0.00000e+00 1.3535000e-01940 0.00000e+00 1.40000e-02 1.3535000e-01941 942 943 944 Convert to NetCDF pts format which is945 946 points: (Nx2) Float array947 elevation: N Float array948 949 Only lines that contain three numeric values are processed950 """951 952 import os953 from Scientific.IO.NetCDF import NetCDFFile954 from Numeric import Float, arrayrange, concatenate955 956 root, ext = os.path.splitext(basename_in)957 958 if ext == '': ext = '.xya'959 960 #Get NetCDF961 infile = open(root + ext, 'r') #Open existing xya file for read962 963 if verbose: print 'Reading xya points from %s' %(root + ext)964 965 points = []966 attribute = []967 for i, line in enumerate(infile.readlines()):968 fields = line.split()969 970 try:971 assert len(fields) == 3972 except:973 print 'WARNING: Line %d doesn\'t have 3 elements: %s' %(i, line)974 975 try:976 x = float( fields[0] )977 y = float( fields[1] )978 z = float( fields[2] )979 except:980 continue981 982 points.append( [x, y] )983 attribute.append(z)984 985 986 #Get output file987 if basename_out == None:988 ptsname = root + '.pts'989 else:990 ptsname = basename_out + '.pts'991 992 if verbose: print 'Store to NetCDF file %s' %ptsname993 # NetCDF file definition994 outfile = NetCDFFile(ptsname, 'w')995 996 #Create new file997 outfile.institution = 'Geoscience Australia'998 outfile.description = 'NetCDF pts format for compact and '\999 'portable storage of spatial point data'1000 1001 1002 #Georeferencing1003 from coordinate_transforms.geo_reference import Geo_reference1004 Geo_reference().write_NetCDF(outfile)1005 1006 1007 outfile.createDimension('number_of_points', len(points))1008 outfile.createDimension('number_of_dimensions', 2) #This is 2d data1009 1010 # variable definitions1011 outfile.createVariable('points', Float, ('number_of_points',1012 'number_of_dimensions'))1013 outfile.createVariable(attribute_name, Float, ('number_of_points',))1014 1015 # Get handles to the variables1016 nc_points = outfile.variables['points']1017 nc_attribute = outfile.variables[attribute_name]1018 1019 #Store data1020 nc_points[:, :] = points1021 nc_attribute[:] = attribute1022 1023 infile.close()1024 outfile.close()1025 1026 1027 923 def dem2pts(basename_in, basename_out=None, verbose=False, 1028 924 easting_min=None, easting_max=None, … … 1048 944 import os 1049 945 from Scientific.IO.NetCDF import NetCDFFile 1050 from Numeric import Float, arrayrange, concatenate946 from Numeric import Float, zeros 1051 947 1052 948 root = basename_in … … 1117 1013 1118 1014 # dimension definitions 1119 nrows_in_bounding_box = int(round(( easting_max-easting_min)/cellsize))1120 ncols_in_bounding_box = int(round(( northing_max-northing_min)/cellsize))1015 nrows_in_bounding_box = int(round((northing_max-northing_min)/cellsize)) 1016 ncols_in_bounding_box = int(round((easting_max-easting_min)/cellsize)) 1121 1017 outfile.createDimension('number_of_points', nrows_in_bounding_box*ncols_in_bounding_box) 1122 1018 outfile.createDimension('number_of_dimensions', 2) #This is 2d data … … 1132 1028 1133 1029 #Store data 1134 #FIXME: Could perhaps be faster using array operations 1135 index = 01030 #FIXME: Could perhaps be faster using array operations (Fixed 27/7/05) 1031 global_index = 0 1136 1032 for i in range(nrows): 1137 1033 if verbose: print 'Processing row %d of %d' %(i, nrows) 1034 lower_index = global_index 1035 tpoints = zeros((ncols_in_bounding_box, 2), Float) 1036 telev = zeros(ncols_in_bounding_box, Float) 1037 local_index = 0 1138 1038 1139 1039 y = (nrows-i)*cellsize + yllcorner 1140 1040 for j in range(ncols): 1141 #index = i*ncols + j1142 1041 1143 1042 x = j*cellsize + xllcorner 1144 1043 if easting_min <= x <= easting_max and \ 1145 1044 northing_min <= y <= northing_max: 1146 points[index, :] = [x-easting_min,y-northing_min] 1147 elevation[index] = dem_elevation[i, j] 1148 index += 1 1149 1150 #print easting_min, easting_max 1151 #print index, nrows_in_bounding_box*ncols_in_bounding_box 1152 #print nrows, ncols 1153 assert index == nrows_in_bounding_box*ncols_in_bounding_box, 'index not equal to number of points' 1045 tpoints[local_index, :] = [x-easting_min,y-northing_min] 1046 telev[local_index] = dem_elevation[i, j] 1047 global_index += 1 1048 local_index += 1 1049 1050 upper_index = global_index 1051 1052 if upper_index == lower_index + ncols_in_bounding_box: 1053 points[lower_index:upper_index, :] = tpoints 1054 elevation[lower_index:upper_index] = telev 1055 1056 assert global_index == nrows_in_bounding_box*ncols_in_bounding_box, 'index not equal to number of points' 1154 1057 1155 1058 infile.close() … … 2379 2282 2380 2283 volumes = fid.variables['volumes'] #Connectivity 2284 2285 -
inundation/ga/storm_surge/pyvolution/test_data_manager.py
r1577 r1660 1819 1819 1820 1820 1821 1821 1822 #------------------------------------------------------------- 1822 1823 if __name__ == "__main__": 1823 suite = unittest.makeSuite(Test_Data_Manager,'test') 1824 #suite = unittest.makeSuite(Test_Data_Manager,'test_sww2domain') 1824 #suite = unittest.makeSuite(Test_Data_Manager,'test') 1825 #suite = unittest.makeSuite(Test_Data_Manager,'test_dem2pts_bounding_box') 1826 #suite = unittest.makeSuite(Test_Data_Manager,'test_decimate_dem') 1825 1827 runner = unittest.TextTestRunner() 1826 1828 runner.run(suite)
Note: See TracChangeset
for help on using the changeset viewer.