Changeset 1813 for inundation/pyvolution/ermapper_grids.py
- Timestamp:
- Sep 9, 2005, 9:26:34 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/ermapper_grids.py
r1812 r1813 4 4 import Numeric 5 5 6 def write_ermapper_grid(ofile, data, header = {}): 7 # function write_ermapper_grid(ofile, data, header = {}): 8 # 9 # Function to write a 2D Numeric array to an ERMapper grid 10 # 11 # Input Parameters: 12 # ofile 6 13 7 def write_ermapper_header(ofile, datum = 'AGD66', projection = 'GEOGRAPHIC', 8 coordinate_type = 'LL', rotation = '0:0:0.0', 9 cell_type = 'IEEE4ByteReal', null_value = '-99999', 10 x_dim = '100', x_ll = '0:0:0', y_dim = '100', y_ll = '0:0:0', 11 num_lines = '3', num_cells = '4', num_bands = '1', 12 band_id = 'band1'): 14 # extract filenames for header and data files from ofile 15 ers_index = ofile.find('.ers') 16 if ers_index > 0: 17 data_file = ofile[0:ers_index] 18 header_file = ofile 19 else: 20 data_file = ofile 21 header_file = ofile + '.ers' 13 22 23 24 # Check that the data is a 2 dimensional array 25 data_size = Numeric.shape(data) 26 assert len(data_size) == 2 27 28 header['nroflines'] = str(data_size[0]) 29 header['nrofcellsperline'] = str(data_size[1]) 30 31 write_ermapper_data(data,data_file) 32 write_ermapper_header(header_file,header) 33 34 def read_ermapper_grid(ifile): 35 36 ers_index = ifile.find('.ers') 37 if ers_index > 0: 38 data_file = ifile[0:ers_index] 39 header_file = ifile 40 else: 41 data_file = ifile 42 header_file = ifile + '.ers' 43 44 header = read_ermapper_header(header_file) 45 46 nroflines = int(header['nroflines']) 47 nrofcellsperlines = int(header['nrofcellsperline']) 48 data = read_ermapper_data(data_file) 49 data = Numeric.reshape(data,(nroflines,nrofcellsperlines)) 50 return data 51 52 53 def write_ermapper_header(ofile, header = {}): 54 55 header = create_default_header(header) 14 56 # Determine if the dataset is in lats/longs or eastings/northings and set header parameters 15 57 # accordingly 16 if coordinate_type is'LL':58 if header['coordinatetype'] == 'LL': 17 59 X_Class = 'Longitude' 18 60 Y_Class = 'Latitude' 19 elif coordinate_type is'EN':61 elif header['coordinatetype'] == 'EN': 20 62 X_Class = 'Eastings' 21 63 Y_Class = 'Northings' … … 26 68 # Begin writing the header 27 69 fid.write('DatasetHeader Begin\n') 28 fid.write('\tVersion\t\t \t= "6.4"\n')29 fid.write('\tDatasetType\t \t= ERStorage\n')30 fid.write('\tDataType\t \t= Raster\n')31 fid.write('\tByteOrder\t \t= LSBFirst\n')70 fid.write('\tVersion\t\t= "6.4"\n') 71 fid.write('\tDatasetType\t= ERStorage\n') 72 fid.write('\tDataType\t= Raster\n') 73 fid.write('\tByteOrder\t= LSBFirst\n') 32 74 33 75 # Write the coordinate space information 34 76 fid.write('\tCoordinateSpace Begin\n') 35 fid.write('\t\tDatum\t\t\t = "' + datum + '"\n')36 fid.write('\t\tProjection\t\t = "' + projection + '"\n')37 fid.write('\t\tCoordinateType\t = ' + coordinate_type+ '\n')38 fid.write('\t\tRotation\t\t = ' + rotation+ '\n')77 fid.write('\t\tDatum\t\t\t = ' + header['datum'] + '\n') 78 fid.write('\t\tProjection\t\t = ' + header['projection'] + '\n') 79 fid.write('\t\tCoordinateType\t = ' + header['coordinatetype'] + '\n') 80 fid.write('\t\tRotation\t\t = ' + header['rotation'] + '\n') 39 81 fid.write('\tCoordinateSpace End\n') 40 82 41 83 # Write the raster information 42 84 fid.write('\tRasterInfo Begin\n') 43 fid.write('\t\tCellType\t\t\t = ' + cell_type+ '\n')44 fid.write('\t\tNullCellValue\t\t = ' + null_value+ '\n')45 fid.write('\t\tRegistrationCellX\t\t = 0\n')46 fid.write('\t\tRegistrationCellY\t\t = ' + str(int(num_lines) - 1)+'\n')85 fid.write('\t\tCellType\t\t\t = ' + header['celltype'] + '\n') 86 fid.write('\t\tNullCellValue\t\t = ' + header['nullcellvalue'] + '\n') 87 fid.write('\t\tRegistrationCellX\t\t = ' + header['registrationcellx'] +'\n') 88 fid.write('\t\tRegistrationCellY\t\t = ' + header['registrationcelly'] +'\n') 47 89 # Write the cellsize information 48 90 fid.write('\t\tCellInfo Begin\n') 49 fid.write('\t\t\tXDimension\t\t\t = ' + x_dim+ '\n')50 fid.write('\t\t\tYDimension\t\t\t = ' + y_dim+ '\n')91 fid.write('\t\t\tXDimension\t\t\t = ' + header['xdimension'] + '\n') 92 fid.write('\t\t\tYDimension\t\t\t = ' + header['ydimension'] + '\n') 51 93 fid.write('\t\tCellInfo End\n') 52 94 # Continue with wrting the raster information 53 fid.write('\t\tNrOfLines\t\t\t = ' + num_lines+ '\n')54 fid.write('\t\tNrOfCellsPerLine\t = ' + num_cells+ '\n')95 fid.write('\t\tNrOfLines\t\t\t = ' + header['nroflines'] + '\n') 96 fid.write('\t\tNrOfCellsPerLine\t = ' + header['nrofcellsperline'] + '\n') 55 97 # Write the registration coordinate information 56 98 fid.write('\t\tRegistrationCoord Begin\n') 57 fid.write('\t\t\t' + X_Class + '\t\t\t = ' + x_ll+ '\n')58 fid.write('\t\t\t' + Y_Class + '\t\t\t = ' + y_ll+ '\n')99 fid.write('\t\t\t' + X_Class + '\t\t\t = ' + header[X_Class.lower()] + '\n') 100 fid.write('\t\t\t' + Y_Class + '\t\t\t = ' + header[Y_Class.lower()] + '\n') 59 101 fid.write('\t\tRegistrationCoord End\n') 60 102 # Continue with wrting the raster information 61 fid.write('\t\tNrOfBands\t\t\t = ' + num_bands+ '\n')103 fid.write('\t\tNrOfBands\t\t\t = ' + header['nrofbands'] + '\n') 62 104 fid.write('\t\tBandID Begin\n') 63 fid.write('\t\t\tValue\t\t\t\t = "' + band_id + '"\n')105 fid.write('\t\t\tValue\t\t\t\t = ' + header['value'] + '\n') 64 106 fid.write('\t\tBandID End\n') 65 107 fid.write('\tRasterInfo End\n') … … 67 109 68 110 fid.close 69 111 112 def read_ermapper_header(ifile): 113 # function for reading an ERMapper header from file 114 header = {} 115 116 fid = open(ifile,'rt') 117 header_string = fid.readlines() 118 fid.close() 119 120 for line in header_string: 121 if line.find('=') > 0: 122 tmp_string = line.strip().split('=') 123 header[tmp_string[0].strip().lower()]= tmp_string[1].strip() 124 125 return header 70 126 71 127 def write_ermapper_data(grid, ofile, data_format = Numeric.Float32): … … 92 148 return grid_as_float 93 149 94 def create_default_header(header ):150 def create_default_header(header = {}): 95 151 # fill any blanks in a header dictionary with default values 96 152 # input parameters: … … 98 154 # to be filled with default values 99 155 100 ## projection = 'GEOGRAPHIC', 101 ## coordinate_type = 'LL', rotation = '0:0:0.0', 102 ## cell_type = 'IEEE4ByteReal', null_value = '-99999', 103 ## x_dim = '100', x_ll = '0:0:0', y_dim = '100', y_ll = '0:0:0', 104 ## num_lines = '3', num_cells = '4', num_bands = '1', 105 ## band_id = 'band1') 156 106 157 if not header.has_key('datum'): 107 header['datum'] = ' GDA94'158 header['datum'] = '"GDA94"' 108 159 if not header.has_key('projection'): 109 header['datum'] = 'GEOGRAPHIC' 160 header['projection'] = '"GEOGRAPHIC"' 161 if not header.has_key('coordinatetype'): 162 header['coordinatetype'] = 'LL' 163 if not header.has_key('rotation'): 164 header['rotation'] = '0:0:0.0' 165 if not header.has_key('celltype'): 166 header['celltype'] = 'IEEE4ByteReal' 167 if not header.has_key('nullcellvalue'): 168 header['nullcellvalue'] = '-99999' 169 if not header.has_key('xdimension'): 170 header['xdimension'] = '100' 171 if not header.has_key('latitude'): 172 header['latitude'] = '0:0:0' 173 if not header.has_key('longitude'): 174 header['longitude'] = '0:0:0' 175 if not header.has_key('ydimension'): 176 header['ydimension'] = '100' 177 if not header.has_key('nroflines'): 178 header['nroflines'] = '3' 179 if not header.has_key('nrofcellsperline'): 180 header['nrofcellsperline'] = '4' 181 if not header.has_key('registrationcellx'): 182 header['registrationcellx'] = '0' 183 if not header.has_key('registrationcelly'): 184 header['registrationcelly'] = str(int(header['nroflines'])-1) 185 if not header.has_key('nrofbands'): 186 header['nrofbands'] = '1' 187 if not header.has_key('value'): 188 header['value'] = '"Default_Band"' 189 110 190 111 191 return header
Note: See TracChangeset
for help on using the changeset viewer.