Changeset 826 for inundation/ga/storm_surge/pyvolution/data_manager.py
- Timestamp:
- Feb 1, 2005, 5:18:44 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/data_manager.py
r815 r826 792 792 793 793 794 def dem2xya(filename, verbose=False): 795 """Read Digitial Elevation model from the following ASCII format (.asc) 796 797 Example: 798 799 ncols 3121 800 nrows 1800 801 xllcorner 722000 802 yllcorner 5893000 803 cellsize 25 804 NODATA_value -9999 805 138.3698 137.4194 136.5062 135.5558 .......... 806 807 Convert to NetCDF xyz format (.xya) 808 """ 809 810 import os 811 from Scientific.IO.NetCDF import NetCDFFile 812 from Numeric import Float, arrayrange, concatenate 813 814 root, ext = os.path.splitext(filename) 815 fid = open(filename) 816 817 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 834 # NetCDF file definition 835 fid = NetCDFFile(netcdfname, 'w') 836 837 #Create new file 838 fid.institution = 'Geoscience Australia' 839 fid.description = 'NetCDF xya format for compact and portable storage ' +\ 840 'of spatial point data' 841 842 fid.ncols = ncols 843 fid.nrows = nrows 844 845 846 # dimension definitions 847 fid.createDimension('number_of_points', nrows*ncols) 848 fid.createDimension('number_of_attributes', 1) #Always 1 with the dem fmt 849 fid.createDimension('number_of_dimensions', 2) #This is 2d data 850 851 852 # variable definitions 853 fid.createVariable('points', Float, ('number_of_points', 854 'number_of_dimensions')) 855 fid.createVariable('attributes', Float, ('number_of_points', 856 'number_of_attributes')) 857 858 # Get handles to the variables 859 points = fid.variables['points'] 860 attributes = fid.variables['attributes'] 861 862 #Store data 863 #FIXME: Could be faster using array operations 864 #FIXME: I think I swapped x and y here - 865 # but this function is probably obsolete anyway 866 for i, line in enumerate(lines[6:]): 867 fields = line.split() 868 if verbose: print 'Processing row %d of %d' %(i, nrows) 869 870 x = i*cellsize 871 for j, field in enumerate(fields): 872 index = i*ncols + j 873 874 y = j*cellsize 875 points[index, :] = [x,y] 876 877 z = float(field) 878 attributes[index, 0] = z 879 880 fid.close() 881 882 883 884 def dem2netcdf(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 format (.dem) mimcing the ASCII format closely. 898 """ 899 900 import os 901 from Scientific.IO.NetCDF import NetCDFFile 902 from Numeric import Float, array 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 + '.dem' 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 DEM format for compact and portable storage ' +\ 930 'of spatial point data' 931 932 fid.ncols = ncols 933 fid.nrows = nrows 934 fid.xllcorner = xllcorner 935 fid.yllcorner = yllcorner 936 fid.cellsize = cellsize 937 fid.NODATA_value = NODATA_value 938 939 # dimension definitions 940 fid.createDimension('number_of_rows', nrows) 941 fid.createDimension('number_of_columns', ncols) 942 943 # variable definitions 944 fid.createVariable('elevation', Float, ('number_of_rows', 945 'number_of_columns')) 946 947 # Get handles to the variables 948 elevation = fid.variables['elevation'] 949 950 #Store data 951 for i, line in enumerate(lines[6:]): 952 fields = line.split() 953 if verbose: print 'Processing row %d of %d' %(i, nrows) 954 955 elevation[i, :] = array([float(x) for x in fields]) 956 957 fid.close() 958 959 960 961 794 962 795 963 #OBSOLETE STUFF
Note: See TracChangeset
for help on using the changeset viewer.