Changeset 834
- Timestamp:
- Feb 4, 2005, 1:58:09 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r832 r834 6 6 7 7 All data is assumed to reside at vertex locations. 8 9 10 Formats used within GANUGA: 11 12 .sww: Netcdf format for storing model output 13 .xya: Either ASCII or NetCDF format for storing arbitrary points and associated attributed 14 .dem: NetCDF representation of regular DEM data 15 .tsh: ASCII format for storing meshes and associated boundary and region info 16 FIXME: What else 8 17 9 18 """ … … 790 799 return cls(domain, mode) 791 800 792 793 794 801 def dem2xya(filename, verbose=False): 795 """Read Digitial Elevation model from the following NetCDF format (. asc)802 """Read Digitial Elevation model from the following NetCDF format (.dem) 796 803 797 804 Example: … … 813 820 814 821 root, ext = os.path.splitext(filename) 815 fid = open(filename) 822 823 #Get NetCDF 824 infile = NetCDFFile(filename, 'r') #Open existing netcdf file for read 816 825 817 826 if verbose: print 'Reading DEM from %s' %filename 818 lines = fid.readlines() 819 fid.close() 820 821 if verbose: print 'Got', len(lines), ' lines' 822 823 ncols = int(lines[0].split()[1].strip()) 824 nrows = int(lines[1].split()[1].strip()) 825 xllcorner = float(lines[2].split()[1].strip()) 826 yllcorner = float(lines[3].split()[1].strip()) 827 cellsize = float(lines[4].split()[1].strip()) 828 NODATA_value = int(lines[5].split()[1].strip()) 829 830 assert len(lines) == nrows + 6 831 832 netcdfname = root + '.xya' 833 if verbose: print 'Store to NetCDF file %s' %netcdfname 827 828 ncols = infile.ncols[0] 829 nrows = infile.nrows[0] 830 xllcorner = infile.xllcorner[0] 831 yllcorner = infile.yllcorner[0] 832 cellsize = infile.cellsize[0] 833 NODATA_value = infile.NODATA_value[0] 834 elevation = infile.variables['elevation'] 835 836 #Get output file 837 xyaname = root + '.xya' 838 if verbose: print 'Store to NetCDF file %s' %xyaname 834 839 # NetCDF file definition 835 fid = NetCDFFile(netcdfname, 'w')840 outfile = NetCDFFile(xyaname, 'w') 836 841 837 842 #Create new file 838 fid.institution = 'Geoscience Australia'839 fid.description = 'NetCDF xya format for compact and portable storage ' +\843 outfile.institution = 'Geoscience Australia' 844 outfile.description = 'NetCDF xya format for compact and portable storage ' +\ 840 845 'of spatial point data' 841 846 842 fid.ncols = ncols843 fid.nrows = nrows847 outfile.ncols = ncols 848 outfile.nrows = nrows 844 849 845 850 846 851 # dimension definitions 847 fid.createDimension('number_of_points', nrows*ncols)848 fid.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt849 fid.createDimension('number_of_dimensions', 2) #This is 2d data852 outfile.createDimension('number_of_points', nrows*ncols) 853 outfile.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt 854 outfile.createDimension('number_of_dimensions', 2) #This is 2d data 850 855 851 856 852 857 # variable definitions 853 fid.createVariable('points', Float, ('number_of_points',858 outfile.createVariable('points', Float, ('number_of_points', 854 859 'number_of_dimensions')) 855 fid.createVariable('attributes', Float, ('number_of_points',860 outfile.createVariable('attributes', Float, ('number_of_points', 856 861 'number_of_attributes')) 857 862 858 863 # Get handles to the variables 859 points = fid.variables['points']860 attributes = fid.variables['attributes']864 points = outfile.variables['points'] 865 attributes = outfile.variables['attributes'] 861 866 862 867 #Store data 863 868 #FIXME: Could be faster using array operations 864 #FIXME: Perhaps the y dimension needs to be reversed 865 #FIXME: x and y definitely needs to be swapped 866 for i, line in enumerate(lines[6:]): 867 fields = line.split() 869 for i in range(nrows): 868 870 if verbose: print 'Processing row %d of %d' %(i, nrows) 869 871 870 x = i*cellsize871 for j , field in enumerate(fields):872 y = (nrows-i)*cellsize 873 for j in range(ncols): 872 874 index = i*ncols + j 873 875 874 y= j*cellsize876 x = j*cellsize 875 877 points[index, :] = [x,y] 876 877 z = float(field) 878 attributes[index, 0] = z 879 880 fid.close() 878 attributes[index, 0] = elevation[i, j] 879 880 infile.close() 881 outfile.close() 882 883 884 # def dem2xya(filename, verbose=False): 885 # """Read Digitial Elevation model from the following ASCII format (.asc) 886 887 # Example: 888 889 # ncols 3121 890 # nrows 1800 891 # xllcorner 722000 892 # yllcorner 5893000 893 # cellsize 25 894 # NODATA_value -9999 895 # 138.3698 137.4194 136.5062 135.5558 .......... 896 897 # Convert to NetCDF xyz format (.xya) 898 # """ 899 900 # import os 901 # from Scientific.IO.NetCDF import NetCDFFile 902 # from Numeric import Float, arrayrange, concatenate 903 904 # root, ext = os.path.splitext(filename) 905 # fid = open(filename) 906 907 # if verbose: print 'Reading DEM from %s' %filename 908 # lines = fid.readlines() 909 # fid.close() 910 911 # if verbose: print 'Got', len(lines), ' lines' 912 913 # ncols = int(lines[0].split()[1].strip()) 914 # nrows = int(lines[1].split()[1].strip()) 915 # xllcorner = float(lines[2].split()[1].strip()) 916 # yllcorner = float(lines[3].split()[1].strip()) 917 # cellsize = float(lines[4].split()[1].strip()) 918 # NODATA_value = int(lines[5].split()[1].strip()) 919 920 # assert len(lines) == nrows + 6 921 922 # netcdfname = root + '.xya' 923 # if verbose: print 'Store to NetCDF file %s' %netcdfname 924 # # NetCDF file definition 925 # fid = NetCDFFile(netcdfname, 'w') 926 927 # #Create new file 928 # fid.institution = 'Geoscience Australia' 929 # fid.description = 'NetCDF xya format for compact and portable storage ' +\ 930 # 'of spatial point data' 931 932 # fid.ncols = ncols 933 # fid.nrows = nrows 934 935 936 # # dimension definitions 937 # fid.createDimension('number_of_points', nrows*ncols) 938 # fid.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt 939 # fid.createDimension('number_of_dimensions', 2) #This is 2d data 940 941 942 # # variable definitions 943 # fid.createVariable('points', Float, ('number_of_points', 944 # 'number_of_dimensions')) 945 # fid.createVariable('attributes', Float, ('number_of_points', 946 # 'number_of_attributes')) 947 948 # # Get handles to the variables 949 # points = fid.variables['points'] 950 # attributes = fid.variables['attributes'] 951 952 # #Store data 953 # #FIXME: Could be faster using array operations 954 # #FIXME: Perhaps the y dimension needs to be reversed 955 # #FIXME: x and y definitely needs to be swapped 956 # for i, line in enumerate(lines[6:]): 957 # fields = line.split() 958 # if verbose: print 'Processing row %d of %d' %(i, nrows) 959 960 # x = i*cellsize 961 # for j, field in enumerate(fields): 962 # index = i*ncols + j 963 964 # y = j*cellsize 965 # points[index, :] = [x,y] 966 967 # z = float(field) 968 # attributes[index, 0] = z 969 970 # fid.close() 881 971 882 972 883 def asciidem2netcdf(filename, verbose=False):973 def convert_dem_from_ascii2netcdf(filename, verbose=False): 884 974 """Read Digitial Elevation model from the following ASCII format (.asc) 885 975 -
inundation/ga/storm_surge/pyvolution/least_squares.py
r832 r834 148 148 149 149 def xya2rectangular(xya_name, M, N, alpha = DEFAULT_ALPHA, 150 verbose = False, reduction = 1 ):150 verbose = False, reduction = 1, format = 'netcdf'): 151 151 """Fits attributes from xya file to MxN rectangular mesh 152 152 … … 161 161 162 162 if verbose: print 'Read xya' 163 points, attributes, _ = util.read_xya(xya_name )163 points, attributes, _ = util.read_xya(xya_name, format) 164 164 165 165 #Reduce number of points a bit -
inundation/ga/storm_surge/pyvolution/test_data_manager.py
r832 r834 523 523 ref_attributes = [] 524 524 for i in range(6): 525 x = i*25.0525 y = (6-i)*25.0 526 526 for j in range(5): 527 y= j*25.0527 x = j*25.0 528 528 z = x+2*y 529 529 … … 536 536 537 537 #Convert to NetCDF xya 538 asciidem2netcdf(filename)539 dem2xya( filename)538 convert_dem_from_ascii2netcdf(filename) 539 dem2xya(root + '.dem') 540 540 541 541 #Check contents -
inundation/ga/storm_surge/pyvolution/test_least_squares.py
r820 r834 499 499 fid.close() 500 500 501 points, triangles, boundary, attributes = xya2rectangular(FN, 4, 4) 501 points, triangles, boundary, attributes =\ 502 xya2rectangular(FN, 4, 4, format = 'asc') 502 503 503 504 505 506 504 z1 = [2, 4] 507 505 z2 = [4, 8] -
inundation/ga/storm_surge/pyvolution/test_util.py
r832 r834 346 346 fid.close() 347 347 348 points, attributes, _ = read_xya(FN )348 points, attributes, _ = read_xya(FN, format = 'asc') 349 349 350 350 assert allclose(points, [ [0,1], [1,0], [1,1] ]) … … 364 364 fid.close() 365 365 366 points, attributes, attribute_names = read_xya(FN )366 points, attributes, attribute_names = read_xya(FN, format = 'asc') 367 367 368 368 assert attribute_names[0] == 'a1' -
inundation/ga/storm_surge/pyvolution/util.py
r833 r834 286 286 return res 287 287 288 def read_xya(filename ):288 def read_xya(filename, format = 'netcdf'): 289 289 """Read simple xya file, possibly with a header in the first line, just 290 290 x y [attributes] 291 291 separated by whitespace 292 292 293 If xya is a NetCDF file it will be read otherwise attemot to read as ASCII294 293 Format can be either ASCII or NetCDF 294 295 295 Return list of points, list of attributes and 296 296 attribute names if present otherwise None … … 298 298 299 299 from Scientific.IO.NetCDF import NetCDFFile 300 try: 300 301 if format.lower() == 'netcdf': 301 302 #Get NetCDF 302 303 303 #FIXME: How can we suppress error message if this fails?304 304 fid = NetCDFFile(filename, 'r') 305 305 … … 310 310 #Don't close - arrays are needed outside this function, 311 311 #alternatively take a copy here 312 e xcept:312 else: 313 313 #Read as ASCII file assuming that it is separated by whitespace 314 314 fid = open(filename)
Note: See TracChangeset
for help on using the changeset viewer.